| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/spdy_test_util_spdy3.h" | 5 #include "net/spdy/spdy_test_util_spdy3.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 // |num_chunks| is the number of chunks to create. | 127 // |num_chunks| is the number of chunks to create. |
| 128 MockRead* ChopReadFrame(const SpdyFrame& frame, int num_chunks) { | 128 MockRead* ChopReadFrame(const SpdyFrame& frame, int num_chunks) { |
| 129 return ChopReadFrame(frame.data(), frame.size(), num_chunks); | 129 return ChopReadFrame(frame.data(), frame.size(), num_chunks); |
| 130 } | 130 } |
| 131 | 131 |
| 132 // Adds headers and values to a map. | 132 // Adds headers and values to a map. |
| 133 // |extra_headers| is an array of { name, value } pairs, arranged as strings | 133 // |extra_headers| is an array of { name, value } pairs, arranged as strings |
| 134 // where the even entries are the header names, and the odd entries are the | 134 // where the even entries are the header names, and the odd entries are the |
| 135 // header values. | 135 // header values. |
| 136 // |headers| gets filled in from |extra_headers|. | 136 // |headers| gets filled in from |extra_headers|. |
| 137 void AppendHeadersToSpdyFrame(const char* const extra_headers[], | 137 void AppendToHeaderBlock(const char* const extra_headers[], |
| 138 int extra_header_count, | 138 int extra_header_count, |
| 139 SpdyHeaderBlock* headers) { | 139 SpdyHeaderBlock* headers) { |
| 140 std::string this_header; | 140 std::string this_header; |
| 141 std::string this_value; | 141 std::string this_value; |
| 142 | 142 |
| 143 if (!extra_header_count) | 143 if (!extra_header_count) |
| 144 return; | 144 return; |
| 145 | 145 |
| 146 // Sanity check: Non-NULL header list. | 146 // Sanity check: Non-NULL header list. |
| 147 DCHECK(NULL != extra_headers) << "NULL header value pair list"; | 147 DCHECK(NULL != extra_headers) << "NULL header value pair list"; |
| 148 // Sanity check: Non-NULL header map. | 148 // Sanity check: Non-NULL header map. |
| 149 DCHECK(NULL != headers) << "NULL header map"; | 149 DCHECK(NULL != headers) << "NULL header map"; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 169 // Append the new value. | 169 // Append the new value. |
| 170 new_value += this_value; | 170 new_value += this_value; |
| 171 } else { | 171 } else { |
| 172 // Not a duplicate, just write the value. | 172 // Not a duplicate, just write the value. |
| 173 new_value = this_value; | 173 new_value = this_value; |
| 174 } | 174 } |
| 175 (*headers)[this_header] = new_value; | 175 (*headers)[this_header] = new_value; |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 scoped_ptr<SpdyHeaderBlock> ConstructHeaderBlock(base::StringPiece url) { |
| 180 std::string scheme, host, path; |
| 181 ParseUrl(url.data(), &scheme, &host, &path); |
| 182 const char* const headers[] = { |
| 183 ":method", "GET", |
| 184 ":path", path.c_str(), |
| 185 ":host", host.c_str(), |
| 186 ":scheme", scheme.c_str(), |
| 187 ":version", "HTTP/1.1" |
| 188 }; |
| 189 scoped_ptr<SpdyHeaderBlock> header_block(new SpdyHeaderBlock()); |
| 190 AppendToHeaderBlock(headers, arraysize(headers) / 2, header_block.get()); |
| 191 return header_block.Pass(); |
| 192 } |
| 193 |
| 179 // Writes |val| to a location of size |len|, in big-endian format. | 194 // Writes |val| to a location of size |len|, in big-endian format. |
| 180 // in the buffer pointed to by |buffer_handle|. | 195 // in the buffer pointed to by |buffer_handle|. |
| 181 // Updates the |*buffer_handle| pointer by |len| | 196 // Updates the |*buffer_handle| pointer by |len| |
| 182 // Returns the number of bytes written | 197 // Returns the number of bytes written |
| 183 int AppendToBuffer(int val, | 198 int AppendToBuffer(int val, |
| 184 int len, | 199 int len, |
| 185 unsigned char** buffer_handle, | 200 unsigned char** buffer_handle, |
| 186 int* buffer_len_remaining) { | 201 int* buffer_len_remaining) { |
| 187 if (len <= 0) | 202 if (len <= 0) |
| 188 return 0; | 203 return 0; |
| 189 DCHECK((size_t) len <= sizeof(len)) << "Data length too long for data type"; | 204 DCHECK((size_t) len <= sizeof(len)) << "Data length too long for data type"; |
| 190 DCHECK(NULL != buffer_handle) << "NULL buffer handle"; | 205 DCHECK(NULL != buffer_handle) << "NULL buffer handle"; |
| 191 DCHECK(NULL != *buffer_handle) << "NULL pointer"; | 206 DCHECK(NULL != *buffer_handle) << "NULL pointer"; |
| 192 DCHECK(NULL != buffer_len_remaining) | 207 DCHECK(NULL != buffer_len_remaining) |
| 193 << "NULL buffer remainder length pointer"; | 208 << "NULL buffer remainder length pointer"; |
| 194 DCHECK_GE(*buffer_len_remaining, len) << "Insufficient buffer size"; | 209 DCHECK_GE(*buffer_len_remaining, len) << "Insufficient buffer size"; |
| 195 for (int i = 0; i < len; i++) { | 210 for (int i = 0; i < len; i++) { |
| 196 int shift = (8 * (len - (i + 1))); | 211 int shift = (8 * (len - (i + 1))); |
| 197 unsigned char val_chunk = (val >> shift) & 0x0FF; | 212 unsigned char val_chunk = (val >> shift) & 0x0FF; |
| 198 *(*buffer_handle)++ = val_chunk; | 213 *(*buffer_handle)++ = val_chunk; |
| 199 *buffer_len_remaining += 1; | 214 *buffer_len_remaining += 1; |
| 200 } | 215 } |
| 201 return len; | 216 return len; |
| 202 } | 217 } |
| 203 | 218 |
| 204 // Construct a SPDY packet. | |
| 205 // |head| is the start of the packet, up to but not including | |
| 206 // the header value pairs. | |
| 207 // |extra_headers| are the extra header-value pairs, which typically | |
| 208 // will vary the most between calls. | |
| 209 // |tail| is any (relatively constant) header-value pairs to add. | |
| 210 // |buffer| is the buffer we're filling in. | |
| 211 // Returns a SpdyFrame. | |
| 212 SpdyFrame* ConstructSpdyPacket(const SpdyHeaderInfo& header_info, | 219 SpdyFrame* ConstructSpdyPacket(const SpdyHeaderInfo& header_info, |
| 213 const char* const extra_headers[], | 220 scoped_ptr<SpdyHeaderBlock> headers) { |
| 214 int extra_header_count, | 221 BufferedSpdyFramer framer(kSpdyVersion3, header_info.compressed); |
| 215 const char* const tail[], | |
| 216 int tail_header_count) { | |
| 217 BufferedSpdyFramer framer(3, header_info.compressed); | |
| 218 SpdyHeaderBlock headers; | |
| 219 // Copy in the extra headers to our map. | |
| 220 AppendHeadersToSpdyFrame(extra_headers, extra_header_count, &headers); | |
| 221 // Copy in the tail headers to our map. | |
| 222 if (tail && tail_header_count) | |
| 223 AppendHeadersToSpdyFrame(tail, tail_header_count, &headers); | |
| 224 SpdyFrame* frame = NULL; | 222 SpdyFrame* frame = NULL; |
| 225 switch (header_info.kind) { | 223 switch (header_info.kind) { |
| 226 case SYN_STREAM: | 224 case SYN_STREAM: |
| 227 frame = framer.CreateSynStream(header_info.id, header_info.assoc_id, | 225 frame = framer.CreateSynStream(header_info.id, header_info.assoc_id, |
| 228 header_info.priority, | 226 header_info.priority, |
| 229 header_info.credential_slot, | 227 header_info.credential_slot, |
| 230 header_info.control_flags, | 228 header_info.control_flags, |
| 231 header_info.compressed, &headers); | 229 header_info.compressed, headers.get()); |
| 232 break; | 230 break; |
| 233 case SYN_REPLY: | 231 case SYN_REPLY: |
| 234 frame = framer.CreateSynReply(header_info.id, header_info.control_flags, | 232 frame = framer.CreateSynReply(header_info.id, header_info.control_flags, |
| 235 header_info.compressed, &headers); | 233 header_info.compressed, headers.get()); |
| 236 break; | 234 break; |
| 237 case RST_STREAM: | 235 case RST_STREAM: |
| 238 frame = framer.CreateRstStream(header_info.id, header_info.status); | 236 frame = framer.CreateRstStream(header_info.id, header_info.status); |
| 239 break; | 237 break; |
| 240 case HEADERS: | 238 case HEADERS: |
| 241 frame = framer.CreateHeaders(header_info.id, header_info.control_flags, | 239 frame = framer.CreateHeaders(header_info.id, header_info.control_flags, |
| 242 header_info.compressed, &headers); | 240 header_info.compressed, headers.get()); |
| 243 break; | 241 break; |
| 244 default: | 242 default: |
| 245 frame = framer.CreateDataFrame(header_info.id, header_info.data, | 243 frame = framer.CreateDataFrame(header_info.id, header_info.data, |
| 246 header_info.data_length, | 244 header_info.data_length, |
| 247 header_info.data_flags); | 245 header_info.data_flags); |
| 248 break; | 246 break; |
| 249 } | 247 } |
| 250 return frame; | 248 return frame; |
| 251 } | 249 } |
| 252 | 250 |
| 251 SpdyFrame* ConstructSpdyPacket(const SpdyHeaderInfo& header_info, |
| 252 const char* const extra_headers[], |
| 253 int extra_header_count, |
| 254 const char* const tail[], |
| 255 int tail_header_count) { |
| 256 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock()); |
| 257 AppendToHeaderBlock(extra_headers, extra_header_count, headers.get()); |
| 258 if (tail && tail_header_count) |
| 259 AppendToHeaderBlock(tail, tail_header_count, headers.get()); |
| 260 return ConstructSpdyPacket(header_info, headers.Pass()); |
| 261 } |
| 262 |
| 253 // Construct an expected SPDY SETTINGS frame. | 263 // Construct an expected SPDY SETTINGS frame. |
| 254 // |settings| are the settings to set. | 264 // |settings| are the settings to set. |
| 255 // Returns the constructed frame. The caller takes ownership of the frame. | 265 // Returns the constructed frame. The caller takes ownership of the frame. |
| 256 SpdyFrame* ConstructSpdySettings(const SettingsMap& settings) { | 266 SpdyFrame* ConstructSpdySettings(const SettingsMap& settings) { |
| 257 BufferedSpdyFramer framer(3, false); | 267 BufferedSpdyFramer framer(3, false); |
| 258 return framer.CreateSettings(settings); | 268 return framer.CreateSettings(settings); |
| 259 } | 269 } |
| 260 | 270 |
| 261 // Construct an expected SPDY CREDENTIAL frame. | 271 // Construct an expected SPDY CREDENTIAL frame. |
| 262 // |credential| is the credential to sen. | 272 // |credential| is the credential to sen. |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 ConvertRequestPriorityToSpdyPriority(request_priority, 3), | 415 ConvertRequestPriorityToSpdyPriority(request_priority, 3), |
| 406 // Priority | 416 // Priority |
| 407 0, // Credential Slot | 417 0, // Credential Slot |
| 408 CONTROL_FLAG_FIN, // Control Flags | 418 CONTROL_FLAG_FIN, // Control Flags |
| 409 compressed, // Compressed | 419 compressed, // Compressed |
| 410 RST_STREAM_INVALID, // Status | 420 RST_STREAM_INVALID, // Status |
| 411 NULL, // Data | 421 NULL, // Data |
| 412 0, // Length | 422 0, // Length |
| 413 DATA_FLAG_NONE // Data Flags | 423 DATA_FLAG_NONE // Data Flags |
| 414 }; | 424 }; |
| 415 | 425 return ConstructSpdyPacket(kSynStartHeader, ConstructHeaderBlock(url)); |
| 416 std::string scheme, host, path; | |
| 417 ParseUrl(url, &scheme, &host, &path); | |
| 418 const char* const headers[] = { | |
| 419 ":method", "GET", | |
| 420 ":path", path.c_str(), | |
| 421 ":host", host.c_str(), | |
| 422 ":scheme", scheme.c_str(), | |
| 423 ":version", "HTTP/1.1" | |
| 424 }; | |
| 425 return ConstructSpdyPacket( | |
| 426 kSynStartHeader, | |
| 427 NULL, | |
| 428 0, | |
| 429 headers, | |
| 430 arraysize(headers) / 2); | |
| 431 } | 426 } |
| 432 | 427 |
| 433 // Constructs a standard SPDY GET SYN packet, optionally compressed. | 428 // Constructs a standard SPDY GET SYN packet, optionally compressed. |
| 434 // |extra_headers| are the extra header-value pairs, which typically | 429 // |extra_headers| are the extra header-value pairs, which typically |
| 435 // will vary the most between calls. | 430 // will vary the most between calls. |
| 436 // Returns a SpdyFrame. | 431 // Returns a SpdyFrame. |
| 437 SpdyFrame* ConstructSpdyGet(const char* const extra_headers[], | 432 SpdyFrame* ConstructSpdyGet(const char* const extra_headers[], |
| 438 int extra_header_count, | 433 int extra_header_count, |
| 439 bool compressed, | 434 bool compressed, |
| 440 int stream_id, | 435 int stream_id, |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 767 int extra_header_count, | 762 int extra_header_count, |
| 768 char* buffer, | 763 char* buffer, |
| 769 int buffer_length) { | 764 int buffer_length) { |
| 770 int packet_size = 0; | 765 int packet_size = 0; |
| 771 char* buffer_write = buffer; | 766 char* buffer_write = buffer; |
| 772 int buffer_left = buffer_length; | 767 int buffer_left = buffer_length; |
| 773 SpdyHeaderBlock headers; | 768 SpdyHeaderBlock headers; |
| 774 if (!buffer || !buffer_length) | 769 if (!buffer || !buffer_length) |
| 775 return 0; | 770 return 0; |
| 776 // Copy in the extra headers. | 771 // Copy in the extra headers. |
| 777 AppendHeadersToSpdyFrame(extra_headers, extra_header_count, &headers); | 772 AppendToHeaderBlock(extra_headers, extra_header_count, &headers); |
| 778 // The iterator gets us the list of header/value pairs in sorted order. | 773 // The iterator gets us the list of header/value pairs in sorted order. |
| 779 SpdyHeaderBlock::iterator next = headers.begin(); | 774 SpdyHeaderBlock::iterator next = headers.begin(); |
| 780 SpdyHeaderBlock::iterator last = headers.end(); | 775 SpdyHeaderBlock::iterator last = headers.end(); |
| 781 for ( ; next != last; ++next) { | 776 for ( ; next != last; ++next) { |
| 782 // Write the header. | 777 // Write the header. |
| 783 int value_len, current_len, offset; | 778 int value_len, current_len, offset; |
| 784 const char* header_string = next->first.c_str(); | 779 const char* header_string = next->first.c_str(); |
| 785 if (header_string && header_string[0] == ':') | 780 if (header_string && header_string[0] == ':') |
| 786 header_string++; | 781 header_string++; |
| 787 packet_size += AppendToBuffer(header_string, | 782 packet_size += AppendToBuffer(header_string, |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1024 NULL, // Data | 1019 NULL, // Data |
| 1025 0, // Length | 1020 0, // Length |
| 1026 DATA_FLAG_NONE // Data Flags | 1021 DATA_FLAG_NONE // Data Flags |
| 1027 }; | 1022 }; |
| 1028 return kHeader; | 1023 return kHeader; |
| 1029 } | 1024 } |
| 1030 | 1025 |
| 1031 } // namespace test_spdy3 | 1026 } // namespace test_spdy3 |
| 1032 | 1027 |
| 1033 } // namespace net | 1028 } // namespace net |
| OLD | NEW |