| 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 #ifndef NET_SPDY_SPDY_FRAMER_H_ | 5 #ifndef NET_SPDY_SPDY_FRAMER_H_ |
| 6 #define NET_SPDY_SPDY_FRAMER_H_ | 6 #define NET_SPDY_SPDY_FRAMER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 } // namespace test | 49 } // namespace test |
| 50 | 50 |
| 51 // A datastructure for holding a set of headers from a HEADERS, PUSH_PROMISE, | 51 // A datastructure for holding a set of headers from a HEADERS, PUSH_PROMISE, |
| 52 // SYN_STREAM, or SYN_REPLY frame. | 52 // SYN_STREAM, or SYN_REPLY frame. |
| 53 typedef std::map<std::string, std::string> SpdyHeaderBlock; | 53 typedef std::map<std::string, std::string> SpdyHeaderBlock; |
| 54 | 54 |
| 55 // A datastructure for holding the ID and flag fields for SETTINGS. | 55 // A datastructure for holding the ID and flag fields for SETTINGS. |
| 56 // Conveniently handles converstion to/from wire format. | 56 // Conveniently handles converstion to/from wire format. |
| 57 class NET_EXPORT_PRIVATE SettingsFlagsAndId { | 57 class NET_EXPORT_PRIVATE SettingsFlagsAndId { |
| 58 public: | 58 public: |
| 59 static SettingsFlagsAndId FromWireFormat(int version, uint32 wire); | 59 static SettingsFlagsAndId FromWireFormat(SpdyMajorVersion version, |
| 60 uint32 wire); |
| 60 | 61 |
| 61 SettingsFlagsAndId() : flags_(0), id_(0) {} | 62 SettingsFlagsAndId() : flags_(0), id_(0) {} |
| 62 | 63 |
| 63 // TODO(hkhalil): restrict to enums instead of free-form ints. | 64 // TODO(hkhalil): restrict to enums instead of free-form ints. |
| 64 SettingsFlagsAndId(uint8 flags, uint32 id); | 65 SettingsFlagsAndId(uint8 flags, uint32 id); |
| 65 | 66 |
| 66 uint32 GetWireFormat(int version) const; | 67 uint32 GetWireFormat(SpdyMajorVersion version) const; |
| 67 | 68 |
| 68 uint32 id() const { return id_; } | 69 uint32 id() const { return id_; } |
| 69 uint8 flags() const { return flags_; } | 70 uint8 flags() const { return flags_; } |
| 70 | 71 |
| 71 private: | 72 private: |
| 72 static void ConvertFlagsAndIdForSpdy2(uint32* val); | 73 static void ConvertFlagsAndIdForSpdy2(uint32* val); |
| 73 | 74 |
| 74 uint8 flags_; | 75 uint8 flags_; |
| 75 uint32 id_; | 76 uint32 id_; |
| 76 }; | 77 }; |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 // Constant for invalid (or unknown) stream IDs. | 314 // Constant for invalid (or unknown) stream IDs. |
| 314 static const SpdyStreamId kInvalidStream; | 315 static const SpdyStreamId kInvalidStream; |
| 315 | 316 |
| 316 // The maximum size of header data chunks delivered to the framer visitor | 317 // The maximum size of header data chunks delivered to the framer visitor |
| 317 // through OnControlFrameHeaderData. (It is exposed here for unit test | 318 // through OnControlFrameHeaderData. (It is exposed here for unit test |
| 318 // purposes.) | 319 // purposes.) |
| 319 static const size_t kHeaderDataChunkMaxSize; | 320 static const size_t kHeaderDataChunkMaxSize; |
| 320 | 321 |
| 321 // Serializes a SpdyHeaderBlock. | 322 // Serializes a SpdyHeaderBlock. |
| 322 static void WriteHeaderBlock(SpdyFrameBuilder* frame, | 323 static void WriteHeaderBlock(SpdyFrameBuilder* frame, |
| 323 const int spdy_version, | 324 const SpdyMajorVersion spdy_version, |
| 324 const SpdyHeaderBlock* headers); | 325 const SpdyHeaderBlock* headers); |
| 325 | 326 |
| 326 // Retrieve serialized length of SpdyHeaderBlock. | 327 // Retrieve serialized length of SpdyHeaderBlock. |
| 327 // TODO(hkhalil): Remove, or move to quic code. | 328 // TODO(hkhalil): Remove, or move to quic code. |
| 328 static size_t GetSerializedLength(const int spdy_version, | 329 static size_t GetSerializedLength( |
| 329 const SpdyHeaderBlock* headers); | 330 const SpdyMajorVersion spdy_version, |
| 331 const SpdyHeaderBlock* headers); |
| 330 | 332 |
| 331 // Create a new Framer, provided a SPDY version. | 333 // Create a new Framer, provided a SPDY version. |
| 332 explicit SpdyFramer(SpdyMajorVersion version); | 334 explicit SpdyFramer(SpdyMajorVersion version); |
| 333 virtual ~SpdyFramer(); | 335 virtual ~SpdyFramer(); |
| 334 | 336 |
| 335 // Set callbacks to be called from the framer. A visitor must be set, or | 337 // Set callbacks to be called from the framer. A visitor must be set, or |
| 336 // else the framer will likely crash. It is acceptable for the visitor | 338 // else the framer will likely crash. It is acceptable for the visitor |
| 337 // to do nothing. If this is called multiple times, only the last visitor | 339 // to do nothing. If this is called multiple times, only the last visitor |
| 338 // will be used. | 340 // will be used. |
| 339 void set_visitor(SpdyFramerVisitorInterface* visitor) { | 341 void set_visitor(SpdyFramerVisitorInterface* visitor) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 364 // Given a buffer containing a decompressed header block in SPDY | 366 // Given a buffer containing a decompressed header block in SPDY |
| 365 // serialized format, parse out a SpdyHeaderBlock, putting the results | 367 // serialized format, parse out a SpdyHeaderBlock, putting the results |
| 366 // in the given header block. | 368 // in the given header block. |
| 367 // Returns number of bytes consumed if successfully parsed, 0 otherwise. | 369 // Returns number of bytes consumed if successfully parsed, 0 otherwise. |
| 368 size_t ParseHeaderBlockInBuffer(const char* header_data, | 370 size_t ParseHeaderBlockInBuffer(const char* header_data, |
| 369 size_t header_length, | 371 size_t header_length, |
| 370 SpdyHeaderBlock* block) const; | 372 SpdyHeaderBlock* block) const; |
| 371 | 373 |
| 372 // Serialize a data frame. | 374 // Serialize a data frame. |
| 373 SpdySerializedFrame* SerializeData(const SpdyDataIR& data) const; | 375 SpdySerializedFrame* SerializeData(const SpdyDataIR& data) const; |
| 374 // Serializes just the data frame header, excluding actual data payload. | 376 // Serializes the data frame header and optionally padding length fields, |
| 375 SpdySerializedFrame* SerializeDataFrameHeader(const SpdyDataIR& data) const; | 377 // excluding actual data payload and padding. |
| 378 SpdySerializedFrame* SerializeDataFrameHeaderWithPaddingLengthField( |
| 379 const SpdyDataIR& data) const; |
| 376 | 380 |
| 377 // Serializes a SYN_STREAM frame. | 381 // Serializes a SYN_STREAM frame. |
| 378 SpdySerializedFrame* SerializeSynStream(const SpdySynStreamIR& syn_stream); | 382 SpdySerializedFrame* SerializeSynStream(const SpdySynStreamIR& syn_stream); |
| 379 | 383 |
| 380 // Serialize a SYN_REPLY SpdyFrame. | 384 // Serialize a SYN_REPLY SpdyFrame. |
| 381 SpdySerializedFrame* SerializeSynReply(const SpdySynReplyIR& syn_reply); | 385 SpdySerializedFrame* SerializeSynReply(const SpdySynReplyIR& syn_reply); |
| 382 | 386 |
| 383 SpdySerializedFrame* SerializeRstStream( | 387 SpdySerializedFrame* SerializeRstStream( |
| 384 const SpdyRstStreamIR& rst_stream) const; | 388 const SpdyRstStreamIR& rst_stream) const; |
| 385 | 389 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 | 482 |
| 479 // Returns the maximum size a frame can be (data or control). | 483 // Returns the maximum size a frame can be (data or control). |
| 480 size_t GetFrameMaximumSize() const; | 484 size_t GetFrameMaximumSize() const; |
| 481 | 485 |
| 482 // Returns the maximum size that a control frame can be. | 486 // Returns the maximum size that a control frame can be. |
| 483 size_t GetControlFrameMaximumSize() const; | 487 size_t GetControlFrameMaximumSize() const; |
| 484 | 488 |
| 485 // Returns the maximum payload size of a DATA frame. | 489 // Returns the maximum payload size of a DATA frame. |
| 486 size_t GetDataFrameMaximumPayload() const; | 490 size_t GetDataFrameMaximumPayload() const; |
| 487 | 491 |
| 492 // Returns the prefix length for the given frame type. |
| 493 size_t GetPrefixLength(SpdyFrameType type) const; |
| 494 |
| 488 // For debugging. | 495 // For debugging. |
| 489 static const char* StateToString(int state); | 496 static const char* StateToString(int state); |
| 490 static const char* ErrorCodeToString(int error_code); | 497 static const char* ErrorCodeToString(int error_code); |
| 491 static const char* StatusCodeToString(int status_code); | 498 static const char* StatusCodeToString(int status_code); |
| 492 static const char* FrameTypeToString(SpdyFrameType type); | 499 static const char* FrameTypeToString(SpdyFrameType type); |
| 493 | 500 |
| 494 SpdyMajorVersion protocol_version() const { return spdy_version_; } | 501 SpdyMajorVersion protocol_version() const { return spdy_version_; } |
| 495 | 502 |
| 496 bool probable_http_response() const { return probable_http_response_; } | 503 bool probable_http_response() const { return probable_http_response_; } |
| 497 | 504 |
| 498 SpdyStreamId expect_continuation() const { return expect_continuation_; } | 505 SpdyStreamId expect_continuation() const { return expect_continuation_; } |
| 499 | 506 |
| 500 SpdyPriority GetLowestPriority() const { return spdy_version_ < 3 ? 3 : 7; } | 507 SpdyPriority GetLowestPriority() const { |
| 508 return spdy_version_ < SPDY3 ? 3 : 7; |
| 509 } |
| 510 |
| 501 SpdyPriority GetHighestPriority() const { return 0; } | 511 SpdyPriority GetHighestPriority() const { return 0; } |
| 502 | 512 |
| 503 // Deliver the given control frame's compressed headers block to the visitor | 513 // Deliver the given control frame's compressed headers block to the visitor |
| 504 // in decompressed form, in chunks. Returns true if the visitor has | 514 // in decompressed form, in chunks. Returns true if the visitor has |
| 505 // accepted all of the chunks. | 515 // accepted all of the chunks. |
| 506 bool IncrementallyDecompressControlFrameHeaderData( | 516 bool IncrementallyDecompressControlFrameHeaderData( |
| 507 SpdyStreamId stream_id, | 517 SpdyStreamId stream_id, |
| 508 const char* data, | 518 const char* data, |
| 509 size_t len); | 519 size_t len); |
| 510 | 520 |
| 511 protected: | 521 protected: |
| 522 // TODO(jgraettinger): Switch to test peer pattern. |
| 512 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, BasicCompression); | 523 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, BasicCompression); |
| 513 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, ControlFrameSizesAreValidated); | 524 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, ControlFrameSizesAreValidated); |
| 514 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, HeaderCompression); | 525 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, HeaderCompression); |
| 515 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, DecompressUncompressedFrame); | 526 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, DecompressUncompressedFrame); |
| 516 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, ExpandBuffer_HeapSmash); | 527 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, ExpandBuffer_HeapSmash); |
| 517 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, HugeHeaderBlock); | 528 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, HugeHeaderBlock); |
| 518 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, UnclosedStreamDataCompressors); | 529 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, UnclosedStreamDataCompressors); |
| 519 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, | 530 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, |
| 520 UnclosedStreamDataCompressorsOneByteAtATime); | 531 UnclosedStreamDataCompressorsOneByteAtATime); |
| 521 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, | 532 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, |
| 522 UncompressLargerThanFrameBufferInitialSize); | 533 UncompressLargerThanFrameBufferInitialSize); |
| 523 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, ReadLargeSettingsFrame); | 534 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, ReadLargeSettingsFrame); |
| 524 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, | 535 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, |
| 525 ReadLargeSettingsFrameInSmallChunks); | 536 ReadLargeSettingsFrameInSmallChunks); |
| 526 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, ControlFrameAtMaxSizeLimit); | 537 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, ControlFrameAtMaxSizeLimit); |
| 527 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, ControlFrameTooLarge); | 538 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, ControlFrameTooLarge); |
| 539 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, |
| 540 TooLargeHeadersFrameUsesContinuation); |
| 541 FRIEND_TEST_ALL_PREFIXES(SpdyFramerTest, |
| 542 TooLargePushPromiseFrameUsesContinuation); |
| 528 friend class net::HttpNetworkLayer; // This is temporary for the server. | 543 friend class net::HttpNetworkLayer; // This is temporary for the server. |
| 529 friend class net::HttpNetworkTransactionTest; | 544 friend class net::HttpNetworkTransactionTest; |
| 530 friend class net::HttpProxyClientSocketPoolTest; | 545 friend class net::HttpProxyClientSocketPoolTest; |
| 531 friend class net::SpdyHttpStreamTest; | 546 friend class net::SpdyHttpStreamTest; |
| 532 friend class net::SpdyNetworkTransactionTest; | 547 friend class net::SpdyNetworkTransactionTest; |
| 533 friend class net::SpdyProxyClientSocketTest; | 548 friend class net::SpdyProxyClientSocketTest; |
| 534 friend class net::SpdySessionTest; | 549 friend class net::SpdySessionTest; |
| 535 friend class net::SpdyStreamTest; | 550 friend class net::SpdyStreamTest; |
| 536 friend class net::SpdyWebSocketStreamTest; | 551 friend class net::SpdyWebSocketStreamTest; |
| 537 friend class net::WebSocketJobTest; | 552 friend class net::WebSocketJobTest; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 548 // whether data is treated as HPACK- vs SPDY3-encoded. | 563 // whether data is treated as HPACK- vs SPDY3-encoded. |
| 549 size_t ProcessControlFrameHeaderBlock(const char* data, | 564 size_t ProcessControlFrameHeaderBlock(const char* data, |
| 550 size_t len, | 565 size_t len, |
| 551 bool is_hpack_header_block); | 566 bool is_hpack_header_block); |
| 552 size_t ProcessFramePaddingLength(const char* data, size_t len); | 567 size_t ProcessFramePaddingLength(const char* data, size_t len); |
| 553 size_t ProcessFramePadding(const char* data, size_t len); | 568 size_t ProcessFramePadding(const char* data, size_t len); |
| 554 size_t ProcessDataFramePayload(const char* data, size_t len); | 569 size_t ProcessDataFramePayload(const char* data, size_t len); |
| 555 size_t ProcessGoAwayFramePayload(const char* data, size_t len); | 570 size_t ProcessGoAwayFramePayload(const char* data, size_t len); |
| 556 size_t ProcessRstStreamFramePayload(const char* data, size_t len); | 571 size_t ProcessRstStreamFramePayload(const char* data, size_t len); |
| 557 size_t ProcessSettingsFramePayload(const char* data, size_t len); | 572 size_t ProcessSettingsFramePayload(const char* data, size_t len); |
| 573 size_t ProcessIgnoredControlFramePayload(/*const char* data,*/ size_t len); |
| 558 | 574 |
| 559 // TODO(jgraettinger): To be removed with migration to | 575 // TODO(jgraettinger): To be removed with migration to |
| 560 // SpdyHeadersHandlerInterface. | 576 // SpdyHeadersHandlerInterface. |
| 561 // Serializes the last-processed header block of |hpack_decoder_| as | 577 // Serializes the last-processed header block of |hpack_decoder_| as |
| 562 // a SPDY3 format block, and delivers it to the visitor via reentrant | 578 // a SPDY3 format block, and delivers it to the visitor via reentrant |
| 563 // call to ProcessControlFrameHeaderBlock(). | 579 // call to ProcessControlFrameHeaderBlock(). |
| 564 void DeliverHpackBlockAsSpdy3Block(); | 580 void DeliverHpackBlockAsSpdy3Block(); |
| 565 | 581 |
| 566 // Helpers for above internal breakouts from ProcessInput. | 582 // Helpers for above internal breakouts from ProcessInput. |
| 567 void ProcessControlFrameHeader(uint16 control_frame_type_field); | 583 void ProcessControlFrameHeader(uint16 control_frame_type_field); |
| 568 // Always passed exactly 1 setting's worth of data. | 584 // Always passed exactly 1 setting's worth of data. |
| 569 bool ProcessSetting(const char* data); | 585 bool ProcessSetting(const char* data); |
| 570 | 586 |
| 571 // Retrieve serialized length of SpdyHeaderBlock. If compression is enabled, a | 587 // Retrieve serialized length of SpdyHeaderBlock. If compression is enabled, a |
| 572 // maximum estimate is returned. | 588 // maximum estimate is returned. |
| 573 size_t GetSerializedLength(const SpdyHeaderBlock& headers); | 589 size_t GetSerializedLength(const SpdyHeaderBlock& headers); |
| 574 | 590 |
| 575 // Get (and lazily initialize) the ZLib state. | 591 // Get (and lazily initialize) the ZLib state. |
| 576 z_stream* GetHeaderCompressor(); | 592 z_stream* GetHeaderCompressor(); |
| 577 z_stream* GetHeaderDecompressor(); | 593 z_stream* GetHeaderDecompressor(); |
| 578 | 594 |
| 595 size_t GetNumberRequiredContinuationFrames(size_t size); |
| 596 |
| 597 void WritePayloadWithContinuation(SpdyFrameBuilder* builder, |
| 598 const std::string& hpack_encoding, |
| 599 SpdyStreamId stream_id, |
| 600 SpdyFrameType type); |
| 601 |
| 579 private: | 602 private: |
| 580 // Deliver the given control frame's uncompressed headers block to the | 603 // Deliver the given control frame's uncompressed headers block to the |
| 581 // visitor in chunks. Returns true if the visitor has accepted all of the | 604 // visitor in chunks. Returns true if the visitor has accepted all of the |
| 582 // chunks. | 605 // chunks. |
| 583 bool IncrementallyDeliverControlFrameHeaderData(SpdyStreamId stream_id, | 606 bool IncrementallyDeliverControlFrameHeaderData(SpdyStreamId stream_id, |
| 584 const char* data, | 607 const char* data, |
| 585 size_t len); | 608 size_t len); |
| 586 | 609 |
| 587 // Utility to copy the given data block to the current frame buffer, up | 610 // Utility to copy the given data block to the current frame buffer, up |
| 588 // to the given maximum number of bytes, and update the buffer | 611 // to the given maximum number of bytes, and update the buffer |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 713 // If a HEADERS frame is followed by a CONTINUATION frame, the FIN/END_STREAM | 736 // If a HEADERS frame is followed by a CONTINUATION frame, the FIN/END_STREAM |
| 714 // flag is still carried in the HEADERS frame. If it's set, flip this so that | 737 // flag is still carried in the HEADERS frame. If it's set, flip this so that |
| 715 // we know to terminate the stream when the entire header block has been | 738 // we know to terminate the stream when the entire header block has been |
| 716 // processed. | 739 // processed. |
| 717 bool end_stream_when_done_; | 740 bool end_stream_when_done_; |
| 718 }; | 741 }; |
| 719 | 742 |
| 720 } // namespace net | 743 } // namespace net |
| 721 | 744 |
| 722 #endif // NET_SPDY_SPDY_FRAMER_H_ | 745 #endif // NET_SPDY_SPDY_FRAMER_H_ |
| OLD | NEW |