Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(469)

Side by Side Diff: net/spdy/chromium/spdy_test_util_common.cc

Issue 2936663002: Simplify CombineFrames() interface. (Closed)
Patch Set: Re: #7. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/spdy/chromium/spdy_test_util_common.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 // Create a MockRead from the given SpdySerializedFrame and sequence number. 154 // Create a MockRead from the given SpdySerializedFrame and sequence number.
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 vector of SpdySerializedFrame into a single frame.
165 // the total length. 165 SpdySerializedFrame CombineFrames(
166 int CombineFrames(const SpdySerializedFrame** frames, 166 std::vector<const SpdySerializedFrame*> frames) {
167 int num_frames, 167 int total_size = 0;
168 char* buf, 168 for (const auto* frame : frames) {
169 int buf_len) { 169 total_size += frame->size();
170 int total_len = 0;
171 for (int i = 0; i < num_frames; ++i) {
172 total_len += frames[i]->size();
173 } 170 }
174 DCHECK_LE(total_len, buf_len); 171 auto data = base::MakeUnique<char[]>(total_size);
175 char* ptr = buf; 172 char* ptr = data.get();
176 for (int i = 0; i < num_frames; ++i) { 173 for (const auto* frame : frames) {
177 int len = frames[i]->size(); 174 memcpy(ptr, frame->data(), frame->size());
178 memcpy(ptr, frames[i]->data(), len); 175 ptr += frame->size();
179 ptr += len;
180 } 176 }
181 return total_len; 177 return SpdySerializedFrame(data.release(), total_size,
178 /* owns_buffer = */ true);
182 } 179 }
183 180
184 namespace { 181 namespace {
185 182
186 class PriorityGetter : public BufferedSpdyFramerVisitorInterface { 183 class PriorityGetter : public BufferedSpdyFramerVisitorInterface {
187 public: 184 public:
188 PriorityGetter() : priority_(0) {} 185 PriorityGetter() : priority_(0) {}
189 ~PriorityGetter() override {} 186 ~PriorityGetter() override {}
190 187
191 SpdyPriority priority() const { 188 SpdyPriority priority() const {
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 response_spdy_framer_.SerializeFrame(push_promise)); 888 response_spdy_framer_.SerializeFrame(push_promise));
892 889
893 SpdyHeaderBlock headers_header_block; 890 SpdyHeaderBlock headers_header_block;
894 headers_header_block[GetStatusKey()] = "200"; 891 headers_header_block[GetStatusKey()] = "200";
895 headers_header_block["hello"] = "bye"; 892 headers_header_block["hello"] = "bye";
896 AppendToHeaderBlock(extra_headers, extra_header_count, &headers_header_block); 893 AppendToHeaderBlock(extra_headers, extra_header_count, &headers_header_block);
897 SpdyHeadersIR headers(stream_id, std::move(headers_header_block)); 894 SpdyHeadersIR headers(stream_id, std::move(headers_header_block));
898 SpdySerializedFrame headers_frame( 895 SpdySerializedFrame headers_frame(
899 response_spdy_framer_.SerializeFrame(headers)); 896 response_spdy_framer_.SerializeFrame(headers));
900 897
901 int joint_data_size = push_promise_frame.size() + headers_frame.size(); 898 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 } 899 }
911 900
912 SpdySerializedFrame SpdyTestUtil::ConstructSpdyPush( 901 SpdySerializedFrame SpdyTestUtil::ConstructSpdyPush(
913 const char* const extra_headers[], 902 const char* const extra_headers[],
914 int extra_header_count, 903 int extra_header_count,
915 int stream_id, 904 int stream_id,
916 int associated_stream_id, 905 int associated_stream_id,
917 const char* url, 906 const char* url,
918 const char* status, 907 const char* status,
919 const char* location) { 908 const char* location) {
920 SpdyHeaderBlock push_promise_header_block; 909 SpdyHeaderBlock push_promise_header_block;
921 AddUrlToHeaderBlock(url, &push_promise_header_block); 910 AddUrlToHeaderBlock(url, &push_promise_header_block);
922 SpdyPushPromiseIR push_promise(associated_stream_id, stream_id, 911 SpdyPushPromiseIR push_promise(associated_stream_id, stream_id,
923 std::move(push_promise_header_block)); 912 std::move(push_promise_header_block));
924 SpdySerializedFrame push_promise_frame( 913 SpdySerializedFrame push_promise_frame(
925 response_spdy_framer_.SerializeFrame(push_promise)); 914 response_spdy_framer_.SerializeFrame(push_promise));
926 915
927 SpdyHeaderBlock headers_header_block; 916 SpdyHeaderBlock headers_header_block;
928 headers_header_block["hello"] = "bye"; 917 headers_header_block["hello"] = "bye";
929 headers_header_block[GetStatusKey()] = status; 918 headers_header_block[GetStatusKey()] = status;
930 headers_header_block["location"] = location; 919 headers_header_block["location"] = location;
931 AppendToHeaderBlock(extra_headers, extra_header_count, &headers_header_block); 920 AppendToHeaderBlock(extra_headers, extra_header_count, &headers_header_block);
932 SpdyHeadersIR headers(stream_id, std::move(headers_header_block)); 921 SpdyHeadersIR headers(stream_id, std::move(headers_header_block));
933 SpdySerializedFrame headers_frame( 922 SpdySerializedFrame headers_frame(
934 response_spdy_framer_.SerializeFrame(headers)); 923 response_spdy_framer_.SerializeFrame(headers));
935 924
936 int joint_data_size = push_promise_frame.size() + headers_frame.size(); 925 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 } 926 }
946 927
947 SpdySerializedFrame SpdyTestUtil::ConstructInitialSpdyPushFrame( 928 SpdySerializedFrame SpdyTestUtil::ConstructInitialSpdyPushFrame(
948 SpdyHeaderBlock headers, 929 SpdyHeaderBlock headers,
949 int stream_id, 930 int stream_id,
950 int associated_stream_id) { 931 int associated_stream_id) {
951 SpdyPushPromiseIR push_promise(associated_stream_id, stream_id, 932 SpdyPushPromiseIR push_promise(associated_stream_id, stream_id,
952 std::move(headers)); 933 std::move(headers));
953 return SpdySerializedFrame( 934 return SpdySerializedFrame(
954 response_spdy_framer_.SerializeFrame(push_promise)); 935 response_spdy_framer_.SerializeFrame(push_promise));
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
1159 headers[GetSchemeKey()] = scheme.c_str(); 1140 headers[GetSchemeKey()] = scheme.c_str();
1160 headers[GetPathKey()] = path.c_str(); 1141 headers[GetPathKey()] = path.c_str();
1161 if (content_length) { 1142 if (content_length) {
1162 SpdyString length_str = base::Int64ToString(*content_length); 1143 SpdyString length_str = base::Int64ToString(*content_length);
1163 headers["content-length"] = length_str; 1144 headers["content-length"] = length_str;
1164 } 1145 }
1165 return headers; 1146 return headers;
1166 } 1147 }
1167 1148
1168 } // namespace net 1149 } // namespace net
OLDNEW
« no previous file with comments | « net/spdy/chromium/spdy_test_util_common.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698