| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/hpack_decoder.h" | 5 #include "net/spdy/hpack_decoder.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 } | 37 } |
| 38 HpackHeaderTable* header_table() { | 38 HpackHeaderTable* header_table() { |
| 39 return &decoder_->header_table_; | 39 return &decoder_->header_table_; |
| 40 } | 40 } |
| 41 void set_cookie_value(string value) { | 41 void set_cookie_value(string value) { |
| 42 decoder_->cookie_value_ = value; | 42 decoder_->cookie_value_ = value; |
| 43 } | 43 } |
| 44 string cookie_value() { | 44 string cookie_value() { |
| 45 return decoder_->cookie_value_; | 45 return decoder_->cookie_value_; |
| 46 } | 46 } |
| 47 const std::map<string, string>& decoded_block() const { | 47 const SpdyHeaderBlock& decoded_block() const { |
| 48 return decoder_->decoded_block_; | 48 return decoder_->decoded_block_; |
| 49 } | 49 } |
| 50 const string& headers_block_buffer() const { | 50 const string& headers_block_buffer() const { |
| 51 return decoder_->headers_block_buffer_; | 51 return decoder_->headers_block_buffer_; |
| 52 } | 52 } |
| 53 | 53 |
| 54 private: | 54 private: |
| 55 HpackDecoder* decoder_; | 55 HpackDecoder* decoder_; |
| 56 }; | 56 }; |
| 57 | 57 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 72 protected: | 72 protected: |
| 73 HpackDecoderTest() | 73 HpackDecoderTest() |
| 74 : decoder_(ObtainHpackHuffmanTable()), | 74 : decoder_(ObtainHpackHuffmanTable()), |
| 75 decoder_peer_(&decoder_) {} | 75 decoder_peer_(&decoder_) {} |
| 76 | 76 |
| 77 bool DecodeHeaderBlock(StringPiece str) { | 77 bool DecodeHeaderBlock(StringPiece str) { |
| 78 return decoder_.HandleControlFrameHeadersData(0, str.data(), str.size()) && | 78 return decoder_.HandleControlFrameHeadersData(0, str.data(), str.size()) && |
| 79 decoder_.HandleControlFrameHeadersComplete(0); | 79 decoder_.HandleControlFrameHeadersComplete(0); |
| 80 } | 80 } |
| 81 | 81 |
| 82 const std::map<string, string>& decoded_block() const { | 82 const SpdyHeaderBlock& decoded_block() const { |
| 83 // TODO(jgraettinger): HpackDecoderTest should implement | 83 // TODO(jgraettinger): HpackDecoderTest should implement |
| 84 // SpdyHeadersHandlerInterface, and collect headers for examination. | 84 // SpdyHeadersHandlerInterface, and collect headers for examination. |
| 85 return decoder_peer_.decoded_block(); | 85 return decoder_peer_.decoded_block(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 const std::map<string, string>& DecodeBlockExpectingSuccess(StringPiece str) { | 88 const SpdyHeaderBlock& DecodeBlockExpectingSuccess(StringPiece str) { |
| 89 EXPECT_TRUE(DecodeHeaderBlock(str)); | 89 EXPECT_TRUE(DecodeHeaderBlock(str)); |
| 90 return decoded_block(); | 90 return decoded_block(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 void expectEntry(size_t index, size_t size, const string& name, | 93 void expectEntry(size_t index, size_t size, const string& name, |
| 94 const string& value) { | 94 const string& value) { |
| 95 const HpackEntry* entry = decoder_peer_.header_table()->GetByIndex(index); | 95 const HpackEntry* entry = decoder_peer_.header_table()->GetByIndex(index); |
| 96 EXPECT_EQ(name, entry->name()) << "index " << index; | 96 EXPECT_EQ(name, entry->name()) << "index " << index; |
| 97 EXPECT_EQ(value, entry->value()); | 97 EXPECT_EQ(value, entry->value()); |
| 98 EXPECT_EQ(size, entry->Size()); | 98 EXPECT_EQ(size, entry->Size()); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 // One more than the number of static table entries. | 205 // One more than the number of static table entries. |
| 206 HpackInputStream input_stream(kLiteralBound, "\x3e"); | 206 HpackInputStream input_stream(kLiteralBound, "\x3e"); |
| 207 | 207 |
| 208 StringPiece string_piece; | 208 StringPiece string_piece; |
| 209 EXPECT_FALSE(decoder_peer_.DecodeNextName(&input_stream, &string_piece)); | 209 EXPECT_FALSE(decoder_peer_.DecodeNextName(&input_stream, &string_piece)); |
| 210 } | 210 } |
| 211 | 211 |
| 212 // Decoding indexed static table field should work. | 212 // Decoding indexed static table field should work. |
| 213 TEST_F(HpackDecoderTest, IndexedHeaderStatic) { | 213 TEST_F(HpackDecoderTest, IndexedHeaderStatic) { |
| 214 // Reference static table entries #2 and #5. | 214 // Reference static table entries #2 and #5. |
| 215 std::map<string, string> header_set1 = | 215 SpdyHeaderBlock header_set1 = DecodeBlockExpectingSuccess("\x82\x85"); |
| 216 DecodeBlockExpectingSuccess("\x82\x85"); | 216 SpdyHeaderBlock expected_header_set1; |
| 217 std::map<string, string> expected_header_set1; | |
| 218 expected_header_set1[":method"] = "GET"; | 217 expected_header_set1[":method"] = "GET"; |
| 219 expected_header_set1[":path"] = "/index.html"; | 218 expected_header_set1[":path"] = "/index.html"; |
| 220 EXPECT_EQ(expected_header_set1, header_set1); | 219 EXPECT_EQ(expected_header_set1, header_set1); |
| 221 | 220 |
| 222 // Reference static table entry #2. | 221 // Reference static table entry #2. |
| 223 std::map<string, string> header_set2 = | 222 SpdyHeaderBlock header_set2 = DecodeBlockExpectingSuccess("\x82"); |
| 224 DecodeBlockExpectingSuccess("\x82"); | 223 SpdyHeaderBlock expected_header_set2; |
| 225 std::map<string, string> expected_header_set2; | |
| 226 expected_header_set2[":method"] = "GET"; | 224 expected_header_set2[":method"] = "GET"; |
| 227 EXPECT_EQ(expected_header_set2, header_set2); | 225 EXPECT_EQ(expected_header_set2, header_set2); |
| 228 } | 226 } |
| 229 | 227 |
| 230 TEST_F(HpackDecoderTest, IndexedHeaderDynamic) { | 228 TEST_F(HpackDecoderTest, IndexedHeaderDynamic) { |
| 231 // First header block: add an entry to header table. | 229 // First header block: add an entry to header table. |
| 232 std::map<string, string> header_set1 = | 230 SpdyHeaderBlock header_set1 = |
| 233 DecodeBlockExpectingSuccess("\x40\x03" "foo" "\x03" "bar"); | 231 DecodeBlockExpectingSuccess("\x40\x03" "foo" "\x03" "bar"); |
| 234 std::map<string, string> expected_header_set1; | 232 SpdyHeaderBlock expected_header_set1; |
| 235 expected_header_set1["foo"] = "bar"; | 233 expected_header_set1["foo"] = "bar"; |
| 236 EXPECT_EQ(expected_header_set1, header_set1); | 234 EXPECT_EQ(expected_header_set1, header_set1); |
| 237 | 235 |
| 238 // Second header block: add another entry to header table. | 236 // Second header block: add another entry to header table. |
| 239 std::map<string, string> header_set2 = | 237 SpdyHeaderBlock header_set2 = |
| 240 DecodeBlockExpectingSuccess("\xbe\x40\x04" "spam" "\x04" "eggs"); | 238 DecodeBlockExpectingSuccess("\xbe\x40\x04" "spam" "\x04" "eggs"); |
| 241 std::map<string, string> expected_header_set2; | 239 SpdyHeaderBlock expected_header_set2; |
| 242 expected_header_set2["foo"] = "bar"; | 240 expected_header_set2["foo"] = "bar"; |
| 243 expected_header_set2["spam"] = "eggs"; | 241 expected_header_set2["spam"] = "eggs"; |
| 244 EXPECT_EQ(expected_header_set2, header_set2); | 242 EXPECT_EQ(expected_header_set2, header_set2); |
| 245 | 243 |
| 246 // Third header block: refer to most recently added entry. | 244 // Third header block: refer to most recently added entry. |
| 247 std::map<string, string> header_set3 = | 245 SpdyHeaderBlock header_set3 = DecodeBlockExpectingSuccess("\xbe"); |
| 248 DecodeBlockExpectingSuccess("\xbe"); | 246 SpdyHeaderBlock expected_header_set3; |
| 249 std::map<string, string> expected_header_set3; | |
| 250 expected_header_set3["spam"] = "eggs"; | 247 expected_header_set3["spam"] = "eggs"; |
| 251 EXPECT_EQ(expected_header_set3, header_set3); | 248 EXPECT_EQ(expected_header_set3, header_set3); |
| 252 } | 249 } |
| 253 | 250 |
| 254 // Test a too-large indexed header. | 251 // Test a too-large indexed header. |
| 255 TEST_F(HpackDecoderTest, InvalidIndexedHeader) { | 252 TEST_F(HpackDecoderTest, InvalidIndexedHeader) { |
| 256 // High-bit set, and a prefix of one more than the number of static entries. | 253 // High-bit set, and a prefix of one more than the number of static entries. |
| 257 EXPECT_FALSE(DecodeHeaderBlock(StringPiece("\xbe", 1))); | 254 EXPECT_FALSE(DecodeHeaderBlock(StringPiece("\xbe", 1))); |
| 258 } | 255 } |
| 259 | 256 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 decoder_peer_.header_table()->max_size()); | 308 decoder_peer_.header_table()->max_size()); |
| 312 } | 309 } |
| 313 } | 310 } |
| 314 | 311 |
| 315 // Decoding two valid encoded literal headers with no indexing should | 312 // Decoding two valid encoded literal headers with no indexing should |
| 316 // work. | 313 // work. |
| 317 TEST_F(HpackDecoderTest, LiteralHeaderNoIndexing) { | 314 TEST_F(HpackDecoderTest, LiteralHeaderNoIndexing) { |
| 318 // First header with indexed name, second header with string literal | 315 // First header with indexed name, second header with string literal |
| 319 // name. | 316 // name. |
| 320 const char input[] = "\x04\x0c/sample/path\x00\x06:path2\x0e/sample/path/2"; | 317 const char input[] = "\x04\x0c/sample/path\x00\x06:path2\x0e/sample/path/2"; |
| 321 std::map<string, string> header_set = | 318 SpdyHeaderBlock header_set = |
| 322 DecodeBlockExpectingSuccess(StringPiece(input, arraysize(input) - 1)); | 319 DecodeBlockExpectingSuccess(StringPiece(input, arraysize(input) - 1)); |
| 323 | 320 |
| 324 std::map<string, string> expected_header_set; | 321 SpdyHeaderBlock expected_header_set; |
| 325 expected_header_set[":path"] = "/sample/path"; | 322 expected_header_set[":path"] = "/sample/path"; |
| 326 expected_header_set[":path2"] = "/sample/path/2"; | 323 expected_header_set[":path2"] = "/sample/path/2"; |
| 327 EXPECT_EQ(expected_header_set, header_set); | 324 EXPECT_EQ(expected_header_set, header_set); |
| 328 } | 325 } |
| 329 | 326 |
| 330 // Decoding two valid encoded literal headers with incremental | 327 // Decoding two valid encoded literal headers with incremental |
| 331 // indexing and string literal names should work. | 328 // indexing and string literal names should work. |
| 332 TEST_F(HpackDecoderTest, LiteralHeaderIncrementalIndexing) { | 329 TEST_F(HpackDecoderTest, LiteralHeaderIncrementalIndexing) { |
| 333 const char input[] = "\x44\x0c/sample/path\x40\x06:path2\x0e/sample/path/2"; | 330 const char input[] = "\x44\x0c/sample/path\x40\x06:path2\x0e/sample/path/2"; |
| 334 std::map<string, string> header_set = | 331 SpdyHeaderBlock header_set = |
| 335 DecodeBlockExpectingSuccess(StringPiece(input, arraysize(input) - 1)); | 332 DecodeBlockExpectingSuccess(StringPiece(input, arraysize(input) - 1)); |
| 336 | 333 |
| 337 std::map<string, string> expected_header_set; | 334 SpdyHeaderBlock expected_header_set; |
| 338 expected_header_set[":path"] = "/sample/path"; | 335 expected_header_set[":path"] = "/sample/path"; |
| 339 expected_header_set[":path2"] = "/sample/path/2"; | 336 expected_header_set[":path2"] = "/sample/path/2"; |
| 340 EXPECT_EQ(expected_header_set, header_set); | 337 EXPECT_EQ(expected_header_set, header_set); |
| 341 } | 338 } |
| 342 | 339 |
| 343 TEST_F(HpackDecoderTest, LiteralHeaderWithIndexingInvalidNameIndex) { | 340 TEST_F(HpackDecoderTest, LiteralHeaderWithIndexingInvalidNameIndex) { |
| 344 decoder_.ApplyHeaderTableSizeSetting(0); | 341 decoder_.ApplyHeaderTableSizeSetting(0); |
| 345 | 342 |
| 346 // Name is the last static index. Works. | 343 // Name is the last static index. Works. |
| 347 EXPECT_TRUE(DecodeHeaderBlock(StringPiece("\x7d\x03ooo"))); | 344 EXPECT_TRUE(DecodeHeaderBlock(StringPiece("\x7d\x03ooo"))); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 360 // Name is the last static index. Works. | 357 // Name is the last static index. Works. |
| 361 EXPECT_TRUE(DecodeHeaderBlock(StringPiece("\x1f\x2e\x03ooo"))); | 358 EXPECT_TRUE(DecodeHeaderBlock(StringPiece("\x1f\x2e\x03ooo"))); |
| 362 // Name is one beyond the last static index. Fails. | 359 // Name is one beyond the last static index. Fails. |
| 363 EXPECT_FALSE(DecodeHeaderBlock(StringPiece("\x1f\x2f\x03ooo"))); | 360 EXPECT_FALSE(DecodeHeaderBlock(StringPiece("\x1f\x2f\x03ooo"))); |
| 364 } | 361 } |
| 365 | 362 |
| 366 // Round-tripping the header set from E.2.1 should work. | 363 // Round-tripping the header set from E.2.1 should work. |
| 367 TEST_F(HpackDecoderTest, BasicE21) { | 364 TEST_F(HpackDecoderTest, BasicE21) { |
| 368 HpackEncoder encoder(ObtainHpackHuffmanTable()); | 365 HpackEncoder encoder(ObtainHpackHuffmanTable()); |
| 369 | 366 |
| 370 std::map<string, string> expected_header_set; | 367 SpdyHeaderBlock expected_header_set; |
| 371 expected_header_set[":method"] = "GET"; | 368 expected_header_set[":method"] = "GET"; |
| 372 expected_header_set[":scheme"] = "http"; | 369 expected_header_set[":scheme"] = "http"; |
| 373 expected_header_set[":path"] = "/"; | 370 expected_header_set[":path"] = "/"; |
| 374 expected_header_set[":authority"] = "www.example.com"; | 371 expected_header_set[":authority"] = "www.example.com"; |
| 375 | 372 |
| 376 string encoded_header_set; | 373 string encoded_header_set; |
| 377 EXPECT_TRUE(encoder.EncodeHeaderSet( | 374 EXPECT_TRUE(encoder.EncodeHeaderSet( |
| 378 expected_header_set, &encoded_header_set)); | 375 expected_header_set, &encoded_header_set)); |
| 379 | 376 |
| 380 EXPECT_TRUE(DecodeHeaderBlock(encoded_header_set)); | 377 EXPECT_TRUE(DecodeHeaderBlock(encoded_header_set)); |
| 381 EXPECT_EQ(expected_header_set, decoded_block()); | 378 EXPECT_EQ(expected_header_set, decoded_block()); |
| 382 } | 379 } |
| 383 | 380 |
| 384 TEST_F(HpackDecoderTest, SectionD4RequestHuffmanExamples) { | 381 TEST_F(HpackDecoderTest, SectionD4RequestHuffmanExamples) { |
| 385 std::map<string, string> header_set; | 382 SpdyHeaderBlock header_set; |
| 386 | 383 |
| 387 // 82 | == Indexed - Add == | 384 // 82 | == Indexed - Add == |
| 388 // | idx = 2 | 385 // | idx = 2 |
| 389 // | -> :method: GET | 386 // | -> :method: GET |
| 390 // 86 | == Indexed - Add == | 387 // 86 | == Indexed - Add == |
| 391 // | idx = 6 | 388 // | idx = 6 |
| 392 // | -> :scheme: http | 389 // | -> :scheme: http |
| 393 // 84 | == Indexed - Add == | 390 // 84 | == Indexed - Add == |
| 394 // | idx = 4 | 391 // | idx = 4 |
| 395 // | -> :path: / | 392 // | -> :path: / |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 Pair(":scheme", "https"), | 483 Pair(":scheme", "https"), |
| 487 Pair("custom-key", "custom-value"))); | 484 Pair("custom-key", "custom-value"))); |
| 488 | 485 |
| 489 expectEntry(62, 54, "custom-key", "custom-value"); | 486 expectEntry(62, 54, "custom-key", "custom-value"); |
| 490 expectEntry(63, 53, "cache-control", "no-cache"); | 487 expectEntry(63, 53, "cache-control", "no-cache"); |
| 491 expectEntry(64, 57, ":authority", "www.example.com"); | 488 expectEntry(64, 57, ":authority", "www.example.com"); |
| 492 EXPECT_EQ(164u, decoder_peer_.header_table()->size()); | 489 EXPECT_EQ(164u, decoder_peer_.header_table()->size()); |
| 493 } | 490 } |
| 494 | 491 |
| 495 TEST_F(HpackDecoderTest, SectionD6ResponseHuffmanExamples) { | 492 TEST_F(HpackDecoderTest, SectionD6ResponseHuffmanExamples) { |
| 496 std::map<string, string> header_set; | 493 SpdyHeaderBlock header_set; |
| 497 decoder_.ApplyHeaderTableSizeSetting(256); | 494 decoder_.ApplyHeaderTableSizeSetting(256); |
| 498 | 495 |
| 499 // 48 | == Literal indexed == | 496 // 48 | == Literal indexed == |
| 500 // | Indexed name (idx = 8) | 497 // | Indexed name (idx = 8) |
| 501 // | :status | 498 // | :status |
| 502 // 82 | Literal value (len = 3) | 499 // 82 | Literal value (len = 3) |
| 503 // | Huffman encoded: | 500 // | Huffman encoded: |
| 504 // 6402 | d. | 501 // 6402 | d. |
| 505 // | Decoded: | 502 // | Decoded: |
| 506 // | 302 | 503 // | 302 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 expectEntry(62, 98, "set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU;" | 661 expectEntry(62, 98, "set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU;" |
| 665 " max-age=3600; version=1"); | 662 " max-age=3600; version=1"); |
| 666 expectEntry(63, 52, "content-encoding", "gzip"); | 663 expectEntry(63, 52, "content-encoding", "gzip"); |
| 667 expectEntry(64, 65, "date", "Mon, 21 Oct 2013 20:13:22 GMT"); | 664 expectEntry(64, 65, "date", "Mon, 21 Oct 2013 20:13:22 GMT"); |
| 668 EXPECT_EQ(215u, decoder_peer_.header_table()->size()); | 665 EXPECT_EQ(215u, decoder_peer_.header_table()->size()); |
| 669 } | 666 } |
| 670 | 667 |
| 671 } // namespace | 668 } // namespace |
| 672 | 669 |
| 673 } // namespace net | 670 } // namespace net |
| OLD | NEW |