 Chromium Code Reviews
 Chromium Code Reviews Issue 2936663002:
  Simplify CombineFrames() interface.  (Closed)
    
  
    Issue 2936663002:
  Simplify CombineFrames() interface.  (Closed) 
  | OLD | NEW | 
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "net/spdy/chromium/spdy_test_util_common.h" | 5 #include "net/spdy/chromium/spdy_test_util_common.h" | 
| 6 | 6 | 
| 7 #include <stdint.h> | 7 #include <stdint.h> | 
| 8 | 8 | 
| 9 #include <cstddef> | 9 #include <cstddef> | 
| 10 #include <utility> | 10 #include <utility> | 
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 MockRead CreateMockRead(const SpdySerializedFrame& resp, int seq) { | 155 MockRead CreateMockRead(const SpdySerializedFrame& resp, int seq) { | 
| 156 return CreateMockRead(resp, seq, ASYNC); | 156 return CreateMockRead(resp, seq, ASYNC); | 
| 157 } | 157 } | 
| 158 | 158 | 
| 159 // Create a MockRead from the given SpdySerializedFrame and sequence number. | 159 // Create a MockRead from the given SpdySerializedFrame and sequence number. | 
| 160 MockRead CreateMockRead(const SpdySerializedFrame& resp, int seq, IoMode mode) { | 160 MockRead CreateMockRead(const SpdySerializedFrame& resp, int seq, IoMode mode) { | 
| 161 return MockRead(mode, resp.data(), resp.size(), seq); | 161 return MockRead(mode, resp.data(), resp.size(), seq); | 
| 162 } | 162 } | 
| 163 | 163 | 
| 164 // Combines the given SpdyFrames into the given char array and returns | 164 // Combines the given SpdyFrames into the given char array and returns | 
| 165 // the total length. | 165 // the total length. | 
| 
Zhongyi Shi
2017/06/12 19:34:12
ditto.
 
Bence
2017/06/12 19:49:13
Done.
 | |
| 166 int CombineFrames(const SpdySerializedFrame** frames, | 166 SpdySerializedFrame CombineFrames( | 
| 167 int num_frames, | 167 std::vector<const SpdySerializedFrame*> frames) { | 
| 168 char* buf, | 168 int total_size = 0; | 
| 169 int buf_len) { | 169 for (const auto* frame : frames) { | 
| 170 int total_len = 0; | 170 total_size += frame->size(); | 
| 171 for (int i = 0; i < num_frames; ++i) { | |
| 172 total_len += frames[i]->size(); | |
| 173 } | 171 } | 
| 174 DCHECK_LE(total_len, buf_len); | 172 auto data = base::MakeUnique<char[]>(total_size); | 
| 175 char* ptr = buf; | 173 char* ptr = data.get(); | 
| 176 for (int i = 0; i < num_frames; ++i) { | 174 for (const auto* frame : frames) { | 
| 177 int len = frames[i]->size(); | 175 memcpy(ptr, frame->data(), frame->size()); | 
| 178 memcpy(ptr, frames[i]->data(), len); | 176 ptr += frame->size(); | 
| 179 ptr += len; | |
| 180 } | 177 } | 
| 181 return total_len; | 178 return SpdySerializedFrame(data.release(), total_size, | 
| 179 /* owns_buffer = */ true); | |
| 182 } | 180 } | 
| 183 | 181 | 
| 184 namespace { | 182 namespace { | 
| 185 | 183 | 
| 186 class PriorityGetter : public BufferedSpdyFramerVisitorInterface { | 184 class PriorityGetter : public BufferedSpdyFramerVisitorInterface { | 
| 187 public: | 185 public: | 
| 188 PriorityGetter() : priority_(0) {} | 186 PriorityGetter() : priority_(0) {} | 
| 189 ~PriorityGetter() override {} | 187 ~PriorityGetter() override {} | 
| 190 | 188 | 
| 191 SpdyPriority priority() const { | 189 SpdyPriority priority() const { | 
| (...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 891 response_spdy_framer_.SerializeFrame(push_promise)); | 889 response_spdy_framer_.SerializeFrame(push_promise)); | 
| 892 | 890 | 
| 893 SpdyHeaderBlock headers_header_block; | 891 SpdyHeaderBlock headers_header_block; | 
| 894 headers_header_block[GetStatusKey()] = "200"; | 892 headers_header_block[GetStatusKey()] = "200"; | 
| 895 headers_header_block["hello"] = "bye"; | 893 headers_header_block["hello"] = "bye"; | 
| 896 AppendToHeaderBlock(extra_headers, extra_header_count, &headers_header_block); | 894 AppendToHeaderBlock(extra_headers, extra_header_count, &headers_header_block); | 
| 897 SpdyHeadersIR headers(stream_id, std::move(headers_header_block)); | 895 SpdyHeadersIR headers(stream_id, std::move(headers_header_block)); | 
| 898 SpdySerializedFrame headers_frame( | 896 SpdySerializedFrame headers_frame( | 
| 899 response_spdy_framer_.SerializeFrame(headers)); | 897 response_spdy_framer_.SerializeFrame(headers)); | 
| 900 | 898 | 
| 901 int joint_data_size = push_promise_frame.size() + headers_frame.size(); | 899 return CombineFrames({&push_promise_frame, &headers_frame}); | 
| 902 auto data = base::MakeUnique<char[]>(joint_data_size); | |
| 903 const SpdySerializedFrame* frames[2] = { | |
| 904 &push_promise_frame, &headers_frame, | |
| 905 }; | |
| 906 int combined_size = | |
| 907 CombineFrames(frames, arraysize(frames), data.get(), joint_data_size); | |
| 908 DCHECK_EQ(combined_size, joint_data_size); | |
| 909 return SpdySerializedFrame(data.release(), joint_data_size, true); | |
| 910 } | 900 } | 
| 911 | 901 | 
| 912 SpdySerializedFrame SpdyTestUtil::ConstructSpdyPush( | 902 SpdySerializedFrame SpdyTestUtil::ConstructSpdyPush( | 
| 913 const char* const extra_headers[], | 903 const char* const extra_headers[], | 
| 914 int extra_header_count, | 904 int extra_header_count, | 
| 915 int stream_id, | 905 int stream_id, | 
| 916 int associated_stream_id, | 906 int associated_stream_id, | 
| 917 const char* url, | 907 const char* url, | 
| 918 const char* status, | 908 const char* status, | 
| 919 const char* location) { | 909 const char* location) { | 
| 920 SpdyHeaderBlock push_promise_header_block; | 910 SpdyHeaderBlock push_promise_header_block; | 
| 921 AddUrlToHeaderBlock(url, &push_promise_header_block); | 911 AddUrlToHeaderBlock(url, &push_promise_header_block); | 
| 922 SpdyPushPromiseIR push_promise(associated_stream_id, stream_id, | 912 SpdyPushPromiseIR push_promise(associated_stream_id, stream_id, | 
| 923 std::move(push_promise_header_block)); | 913 std::move(push_promise_header_block)); | 
| 924 SpdySerializedFrame push_promise_frame( | 914 SpdySerializedFrame push_promise_frame( | 
| 925 response_spdy_framer_.SerializeFrame(push_promise)); | 915 response_spdy_framer_.SerializeFrame(push_promise)); | 
| 926 | 916 | 
| 927 SpdyHeaderBlock headers_header_block; | 917 SpdyHeaderBlock headers_header_block; | 
| 928 headers_header_block["hello"] = "bye"; | 918 headers_header_block["hello"] = "bye"; | 
| 929 headers_header_block[GetStatusKey()] = status; | 919 headers_header_block[GetStatusKey()] = status; | 
| 930 headers_header_block["location"] = location; | 920 headers_header_block["location"] = location; | 
| 931 AppendToHeaderBlock(extra_headers, extra_header_count, &headers_header_block); | 921 AppendToHeaderBlock(extra_headers, extra_header_count, &headers_header_block); | 
| 932 SpdyHeadersIR headers(stream_id, std::move(headers_header_block)); | 922 SpdyHeadersIR headers(stream_id, std::move(headers_header_block)); | 
| 933 SpdySerializedFrame headers_frame( | 923 SpdySerializedFrame headers_frame( | 
| 934 response_spdy_framer_.SerializeFrame(headers)); | 924 response_spdy_framer_.SerializeFrame(headers)); | 
| 935 | 925 | 
| 936 int joint_data_size = push_promise_frame.size() + headers_frame.size(); | 926 return CombineFrames({&push_promise_frame, &headers_frame}); | 
| 937 auto data = base::MakeUnique<char[]>(joint_data_size); | |
| 938 const SpdySerializedFrame* frames[2] = { | |
| 939 &push_promise_frame, &headers_frame, | |
| 940 }; | |
| 941 int combined_size = | |
| 942 CombineFrames(frames, arraysize(frames), data.get(), joint_data_size); | |
| 943 DCHECK_EQ(combined_size, joint_data_size); | |
| 944 return SpdySerializedFrame(data.release(), joint_data_size, true); | |
| 945 } | 927 } | 
| 946 | 928 | 
| 947 SpdySerializedFrame SpdyTestUtil::ConstructInitialSpdyPushFrame( | 929 SpdySerializedFrame SpdyTestUtil::ConstructInitialSpdyPushFrame( | 
| 948 SpdyHeaderBlock headers, | 930 SpdyHeaderBlock headers, | 
| 949 int stream_id, | 931 int stream_id, | 
| 950 int associated_stream_id) { | 932 int associated_stream_id) { | 
| 951 SpdyPushPromiseIR push_promise(associated_stream_id, stream_id, | 933 SpdyPushPromiseIR push_promise(associated_stream_id, stream_id, | 
| 952 std::move(headers)); | 934 std::move(headers)); | 
| 953 return SpdySerializedFrame( | 935 return SpdySerializedFrame( | 
| 954 response_spdy_framer_.SerializeFrame(push_promise)); | 936 response_spdy_framer_.SerializeFrame(push_promise)); | 
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1159 headers[GetSchemeKey()] = scheme.c_str(); | 1141 headers[GetSchemeKey()] = scheme.c_str(); | 
| 1160 headers[GetPathKey()] = path.c_str(); | 1142 headers[GetPathKey()] = path.c_str(); | 
| 1161 if (content_length) { | 1143 if (content_length) { | 
| 1162 SpdyString length_str = base::Int64ToString(*content_length); | 1144 SpdyString length_str = base::Int64ToString(*content_length); | 
| 1163 headers["content-length"] = length_str; | 1145 headers["content-length"] = length_str; | 
| 1164 } | 1146 } | 
| 1165 return headers; | 1147 return headers; | 
| 1166 } | 1148 } | 
| 1167 | 1149 | 
| 1168 } // namespace net | 1150 } // namespace net | 
| OLD | NEW |