| 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_spdy2.h" | 5 #include "net/spdy/spdy_test_util_spdy2.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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 // |num_chunks| is the number of chunks to create. | 83 // |num_chunks| is the number of chunks to create. |
| 84 MockRead* ChopReadFrame(const SpdyFrame& frame, int num_chunks) { | 84 MockRead* ChopReadFrame(const SpdyFrame& frame, int num_chunks) { |
| 85 return ChopReadFrame(frame.data(), frame.size(), num_chunks); | 85 return ChopReadFrame(frame.data(), frame.size(), num_chunks); |
| 86 } | 86 } |
| 87 | 87 |
| 88 // Adds headers and values to a map. | 88 // Adds headers and values to a map. |
| 89 // |extra_headers| is an array of { name, value } pairs, arranged as strings | 89 // |extra_headers| is an array of { name, value } pairs, arranged as strings |
| 90 // where the even entries are the header names, and the odd entries are the | 90 // where the even entries are the header names, and the odd entries are the |
| 91 // header values. | 91 // header values. |
| 92 // |headers| gets filled in from |extra_headers|. | 92 // |headers| gets filled in from |extra_headers|. |
| 93 void AppendHeadersToSpdyFrame(const char* const extra_headers[], | 93 void AppendToHeaderBlock(const char* const extra_headers[], |
| 94 int extra_header_count, | 94 int extra_header_count, |
| 95 SpdyHeaderBlock* headers) { | 95 SpdyHeaderBlock* headers) { |
| 96 std::string this_header; | 96 std::string this_header; |
| 97 std::string this_value; | 97 std::string this_value; |
| 98 | 98 |
| 99 if (!extra_header_count) | 99 if (!extra_header_count) |
| 100 return; | 100 return; |
| 101 | 101 |
| 102 // Sanity check: Non-NULL header list. | 102 // Sanity check: Non-NULL header list. |
| 103 DCHECK(NULL != extra_headers) << "NULL header value pair list"; | 103 DCHECK(NULL != extra_headers) << "NULL header value pair list"; |
| 104 // Sanity check: Non-NULL header map. | 104 // Sanity check: Non-NULL header map. |
| 105 DCHECK(NULL != headers) << "NULL header map"; | 105 DCHECK(NULL != headers) << "NULL header map"; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 125 // Append the new value. | 125 // Append the new value. |
| 126 new_value += this_value; | 126 new_value += this_value; |
| 127 } else { | 127 } else { |
| 128 // Not a duplicate, just write the value. | 128 // Not a duplicate, just write the value. |
| 129 new_value = this_value; | 129 new_value = this_value; |
| 130 } | 130 } |
| 131 (*headers)[this_header] = new_value; | 131 (*headers)[this_header] = new_value; |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 | 134 |
| 135 scoped_ptr<SpdyHeaderBlock> ConstructHeaderBlock(base::StringPiece url) { |
| 136 std::string scheme, host, path; |
| 137 ParseUrl(url.data(), &scheme, &host, &path); |
| 138 const char* const headers[] = { |
| 139 "method", "GET", |
| 140 "url", path.c_str(), |
| 141 "host", host.c_str(), |
| 142 "scheme", scheme.c_str(), |
| 143 "version", "HTTP/1.1" |
| 144 }; |
| 145 scoped_ptr<SpdyHeaderBlock> header_block(new SpdyHeaderBlock()); |
| 146 AppendToHeaderBlock(headers, arraysize(headers) / 2, header_block.get()); |
| 147 return header_block.Pass(); |
| 148 } |
| 149 |
| 135 // Writes |val| to a location of size |len|, in big-endian format. | 150 // Writes |val| to a location of size |len|, in big-endian format. |
| 136 // in the buffer pointed to by |buffer_handle|. | 151 // in the buffer pointed to by |buffer_handle|. |
| 137 // Updates the |*buffer_handle| pointer by |len| | 152 // Updates the |*buffer_handle| pointer by |len| |
| 138 // Returns the number of bytes written | 153 // Returns the number of bytes written |
| 139 int AppendToBuffer(int val, | 154 int AppendToBuffer(int val, |
| 140 int len, | 155 int len, |
| 141 unsigned char** buffer_handle, | 156 unsigned char** buffer_handle, |
| 142 int* buffer_len_remaining) { | 157 int* buffer_len_remaining) { |
| 143 if (len <= 0) | 158 if (len <= 0) |
| 144 return 0; | 159 return 0; |
| 145 DCHECK((size_t) len <= sizeof(len)) << "Data length too long for data type"; | 160 DCHECK((size_t) len <= sizeof(len)) << "Data length too long for data type"; |
| 146 DCHECK(NULL != buffer_handle) << "NULL buffer handle"; | 161 DCHECK(NULL != buffer_handle) << "NULL buffer handle"; |
| 147 DCHECK(NULL != *buffer_handle) << "NULL pointer"; | 162 DCHECK(NULL != *buffer_handle) << "NULL pointer"; |
| 148 DCHECK(NULL != buffer_len_remaining) | 163 DCHECK(NULL != buffer_len_remaining) |
| 149 << "NULL buffer remainder length pointer"; | 164 << "NULL buffer remainder length pointer"; |
| 150 DCHECK_GE(*buffer_len_remaining, len) << "Insufficient buffer size"; | 165 DCHECK_GE(*buffer_len_remaining, len) << "Insufficient buffer size"; |
| 151 for (int i = 0; i < len; i++) { | 166 for (int i = 0; i < len; i++) { |
| 152 int shift = (8 * (len - (i + 1))); | 167 int shift = (8 * (len - (i + 1))); |
| 153 unsigned char val_chunk = (val >> shift) & 0x0FF; | 168 unsigned char val_chunk = (val >> shift) & 0x0FF; |
| 154 *(*buffer_handle)++ = val_chunk; | 169 *(*buffer_handle)++ = val_chunk; |
| 155 *buffer_len_remaining += 1; | 170 *buffer_len_remaining += 1; |
| 156 } | 171 } |
| 157 return len; | 172 return len; |
| 158 } | 173 } |
| 159 | 174 |
| 160 // Construct a SPDY packet. | |
| 161 // |head| is the start of the packet, up to but not including | |
| 162 // the header value pairs. | |
| 163 // |extra_headers| are the extra header-value pairs, which typically | |
| 164 // will vary the most between calls. | |
| 165 // |tail| is any (relatively constant) header-value pairs to add. | |
| 166 // |buffer| is the buffer we're filling in. | |
| 167 // Returns a SpdyFrame. | |
| 168 SpdyFrame* ConstructSpdyPacket(const SpdyHeaderInfo& header_info, | 175 SpdyFrame* ConstructSpdyPacket(const SpdyHeaderInfo& header_info, |
| 169 const char* const extra_headers[], | 176 scoped_ptr<SpdyHeaderBlock> headers) { |
| 170 int extra_header_count, | 177 BufferedSpdyFramer framer(kSpdyVersion2, header_info.compressed); |
| 171 const char* const tail[], | |
| 172 int tail_header_count) { | |
| 173 BufferedSpdyFramer framer(2, header_info.compressed); | |
| 174 SpdyHeaderBlock headers; | |
| 175 // Copy in the extra headers to our map. | |
| 176 AppendHeadersToSpdyFrame(extra_headers, extra_header_count, &headers); | |
| 177 // Copy in the tail headers to our map. | |
| 178 if (tail && tail_header_count) | |
| 179 AppendHeadersToSpdyFrame(tail, tail_header_count, &headers); | |
| 180 SpdyFrame* frame = NULL; | 178 SpdyFrame* frame = NULL; |
| 181 switch (header_info.kind) { | 179 switch (header_info.kind) { |
| 182 case SYN_STREAM: | 180 case SYN_STREAM: |
| 183 frame = framer.CreateSynStream(header_info.id, header_info.assoc_id, | 181 frame = framer.CreateSynStream(header_info.id, header_info.assoc_id, |
| 184 header_info.priority, 0, | 182 header_info.priority, 0, |
| 185 header_info.control_flags, | 183 header_info.control_flags, |
| 186 header_info.compressed, &headers); | 184 header_info.compressed, headers.get()); |
| 187 break; | 185 break; |
| 188 case SYN_REPLY: | 186 case SYN_REPLY: |
| 189 frame = framer.CreateSynReply(header_info.id, header_info.control_flags, | 187 frame = framer.CreateSynReply(header_info.id, header_info.control_flags, |
| 190 header_info.compressed, &headers); | 188 header_info.compressed, headers.get()); |
| 191 break; | 189 break; |
| 192 case RST_STREAM: | 190 case RST_STREAM: |
| 193 frame = framer.CreateRstStream(header_info.id, header_info.status); | 191 frame = framer.CreateRstStream(header_info.id, header_info.status); |
| 194 break; | 192 break; |
| 195 case HEADERS: | 193 case HEADERS: |
| 196 frame = framer.CreateHeaders(header_info.id, header_info.control_flags, | 194 frame = framer.CreateHeaders(header_info.id, header_info.control_flags, |
| 197 header_info.compressed, &headers); | 195 header_info.compressed, headers.get()); |
| 198 break; | 196 break; |
| 199 default: | 197 default: |
| 200 frame = framer.CreateDataFrame(header_info.id, header_info.data, | 198 frame = framer.CreateDataFrame(header_info.id, header_info.data, |
| 201 header_info.data_length, | 199 header_info.data_length, |
| 202 header_info.data_flags); | 200 header_info.data_flags); |
| 203 break; | 201 break; |
| 204 } | 202 } |
| 205 return frame; | 203 return frame; |
| 206 } | 204 } |
| 207 | 205 |
| 206 SpdyFrame* ConstructSpdyPacket(const SpdyHeaderInfo& header_info, |
| 207 const char* const extra_headers[], |
| 208 int extra_header_count, |
| 209 const char* const tail[], |
| 210 int tail_header_count) { |
| 211 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock()); |
| 212 AppendToHeaderBlock(extra_headers, extra_header_count, headers.get()); |
| 213 if (tail && tail_header_count) |
| 214 AppendToHeaderBlock(tail, tail_header_count, headers.get()); |
| 215 return ConstructSpdyPacket(header_info, headers.Pass()); |
| 216 } |
| 217 |
| 208 // Construct an expected SPDY SETTINGS frame. | 218 // Construct an expected SPDY SETTINGS frame. |
| 209 // |settings| are the settings to set. | 219 // |settings| are the settings to set. |
| 210 // Returns the constructed frame. The caller takes ownership of the frame. | 220 // Returns the constructed frame. The caller takes ownership of the frame. |
| 211 SpdyFrame* ConstructSpdySettings(const SettingsMap& settings) { | 221 SpdyFrame* ConstructSpdySettings(const SettingsMap& settings) { |
| 212 BufferedSpdyFramer framer(2, false); | 222 BufferedSpdyFramer framer(2, false); |
| 213 return framer.CreateSettings(settings); | 223 return framer.CreateSettings(settings); |
| 214 } | 224 } |
| 215 | 225 |
| 216 // Construct an expected SPDY CREDENTIAL frame. | 226 // Construct an expected SPDY CREDENTIAL frame. |
| 217 // |credential| is the credential to sen. | 227 // |credential| is the credential to sen. |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 0, // Associated stream ID | 368 0, // Associated stream ID |
| 359 ConvertRequestPriorityToSpdyPriority(request_priority, 2), | 369 ConvertRequestPriorityToSpdyPriority(request_priority, 2), |
| 360 // Priority | 370 // Priority |
| 361 CONTROL_FLAG_FIN, // Control Flags | 371 CONTROL_FLAG_FIN, // Control Flags |
| 362 compressed, // Compressed | 372 compressed, // Compressed |
| 363 RST_STREAM_INVALID, // Status | 373 RST_STREAM_INVALID, // Status |
| 364 NULL, // Data | 374 NULL, // Data |
| 365 0, // Length | 375 0, // Length |
| 366 DATA_FLAG_NONE // Data Flags | 376 DATA_FLAG_NONE // Data Flags |
| 367 }; | 377 }; |
| 368 | 378 return ConstructSpdyPacket(kSynStartHeader, ConstructHeaderBlock(url)); |
| 369 std::string scheme, host, path; | |
| 370 ParseUrl(url, &scheme, &host, &path); | |
| 371 const char* const headers[] = { | |
| 372 "method", "GET", | |
| 373 "url", path.c_str(), | |
| 374 "host", host.c_str(), | |
| 375 "scheme", scheme.c_str(), | |
| 376 "version", "HTTP/1.1" | |
| 377 }; | |
| 378 return ConstructSpdyPacket( | |
| 379 kSynStartHeader, | |
| 380 NULL, | |
| 381 0, | |
| 382 headers, | |
| 383 arraysize(headers) / 2); | |
| 384 } | 379 } |
| 385 | 380 |
| 386 // Constructs a standard SPDY GET SYN packet, optionally compressed. | 381 // Constructs a standard SPDY GET SYN packet, optionally compressed. |
| 387 // |extra_headers| are the extra header-value pairs, which typically | 382 // |extra_headers| are the extra header-value pairs, which typically |
| 388 // will vary the most between calls. | 383 // will vary the most between calls. |
| 389 // Returns a SpdyFrame. | 384 // Returns a SpdyFrame. |
| 390 SpdyFrame* ConstructSpdyGet(const char* const extra_headers[], | 385 SpdyFrame* ConstructSpdyGet(const char* const extra_headers[], |
| 391 int extra_header_count, | 386 int extra_header_count, |
| 392 bool compressed, | 387 bool compressed, |
| 393 int stream_id, | 388 int stream_id, |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 726 int extra_header_count, | 721 int extra_header_count, |
| 727 char* buffer, | 722 char* buffer, |
| 728 int buffer_length) { | 723 int buffer_length) { |
| 729 int packet_size = 0; | 724 int packet_size = 0; |
| 730 char* buffer_write = buffer; | 725 char* buffer_write = buffer; |
| 731 int buffer_left = buffer_length; | 726 int buffer_left = buffer_length; |
| 732 SpdyHeaderBlock headers; | 727 SpdyHeaderBlock headers; |
| 733 if (!buffer || !buffer_length) | 728 if (!buffer || !buffer_length) |
| 734 return 0; | 729 return 0; |
| 735 // Copy in the extra headers. | 730 // Copy in the extra headers. |
| 736 AppendHeadersToSpdyFrame(extra_headers, extra_header_count, &headers); | 731 AppendToHeaderBlock(extra_headers, extra_header_count, &headers); |
| 737 // The iterator gets us the list of header/value pairs in sorted order. | 732 // The iterator gets us the list of header/value pairs in sorted order. |
| 738 SpdyHeaderBlock::iterator next = headers.begin(); | 733 SpdyHeaderBlock::iterator next = headers.begin(); |
| 739 SpdyHeaderBlock::iterator last = headers.end(); | 734 SpdyHeaderBlock::iterator last = headers.end(); |
| 740 for ( ; next != last; ++next) { | 735 for ( ; next != last; ++next) { |
| 741 // Write the header. | 736 // Write the header. |
| 742 int value_len, current_len, offset; | 737 int value_len, current_len, offset; |
| 743 const char* header_string = next->first.c_str(); | 738 const char* header_string = next->first.c_str(); |
| 744 packet_size += AppendToBuffer(header_string, | 739 packet_size += AppendToBuffer(header_string, |
| 745 next->first.length(), | 740 next->first.length(), |
| 746 &buffer_write, | 741 &buffer_write, |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 973 RST_STREAM_INVALID, // Status | 968 RST_STREAM_INVALID, // Status |
| 974 NULL, // Data | 969 NULL, // Data |
| 975 0, // Length | 970 0, // Length |
| 976 DATA_FLAG_NONE // Data Flags | 971 DATA_FLAG_NONE // Data Flags |
| 977 }; | 972 }; |
| 978 return kHeader; | 973 return kHeader; |
| 979 } | 974 } |
| 980 | 975 |
| 981 } // namespace test_spdy2 | 976 } // namespace test_spdy2 |
| 982 } // namespace net | 977 } // namespace net |
| OLD | NEW |