[email protected] | 39c48fc | 2012-03-12 18:42:12 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 1407b6e | 2010-08-27 21:39:48 | [diff] [blame] | 5 | #ifndef NET_SPDY_SPDY_FRAME_BUILDER_H_ |
| 6 | #define NET_SPDY_SPDY_FRAME_BUILDER_H_ |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 7 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 8 | #include <string> |
| 9 | |
[email protected] | 1407b6e | 2010-08-27 21:39:48 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | f707c34 | 2013-01-09 04:40:25 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 12 | #include "base/strings/string_piece.h" |
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 13 | #include "base/sys_byteorder.h" |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 14 | #include "net/base/net_export.h" |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 15 | #include "net/spdy/spdy_protocol.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 16 | |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 17 | namespace net { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 18 | |
[email protected] | 4e97435 | 2013-02-15 17:56:57 | [diff] [blame] | 19 | class SpdyFramer; |
| 20 | |
[email protected] | f707c34 | 2013-01-09 04:40:25 | [diff] [blame] | 21 | // This class provides facilities for basic binary value packing |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 22 | // into Spdy frames. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 23 | // |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 24 | // The SpdyFrameBuilder supports appending primitive values (int, string, etc) |
| 25 | // to a frame instance. The SpdyFrameBuilder grows its internal memory buffer |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 26 | // dynamically to hold the sequence of primitive values. The internal memory |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 27 | // buffer is exposed as the "data" of the SpdyFrameBuilder. |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 28 | class NET_EXPORT_PRIVATE SpdyFrameBuilder { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 29 | public: |
[email protected] | a944f74e | 2012-12-19 23:10:28 | [diff] [blame] | 30 | // Initializes a SpdyFrameBuilder with a buffer of given size |
| 31 | explicit SpdyFrameBuilder(size_t size); |
| 32 | |
[email protected] | f707c34 | 2013-01-09 04:40:25 | [diff] [blame] | 33 | ~SpdyFrameBuilder(); |
| 34 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 35 | // Returns the size of the SpdyFrameBuilder's data. |
[email protected] | bbfc65e | 2012-04-21 01:32:40 | [diff] [blame] | 36 | size_t length() const { return length_; } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 37 | |
[email protected] | 4e97435 | 2013-02-15 17:56:57 | [diff] [blame] | 38 | // Returns a writeable buffer of given size in bytes, to be appended to the |
| 39 | // currently written frame. Does bounds checking on length but does not |
| 40 | // increment the underlying iterator. To do so, consumers should subsequently |
| 41 | // call Seek(). |
| 42 | // In general, consumers should use Write*() calls instead of this. |
| 43 | // Returns NULL on failure. |
| 44 | char* GetWritableBuffer(size_t length); |
| 45 | |
| 46 | // Seeks forward by the given number of bytes. Useful in conjunction with |
| 47 | // GetWriteableBuffer() above. |
| 48 | bool Seek(size_t length); |
| 49 | |
[email protected] | eb566e51 | 2013-02-26 23:42:18 | [diff] [blame] | 50 | // Populates this frame with a SPDY control frame header using |
| 51 | // version-specific information from the |framer| and length information from |
[email protected] | 7b3fee16 | 2013-04-09 18:05:03 | [diff] [blame] | 52 | // capacity_. The given type must be a control frame type. |
[email protected] | d5b42bdd | 2013-05-15 20:42:36 | [diff] [blame] | 53 | // Used only for SPDY versions <4. |
[email protected] | eb566e51 | 2013-02-26 23:42:18 | [diff] [blame] | 54 | bool WriteControlFrameHeader(const SpdyFramer& framer, |
[email protected] | 7b3fee16 | 2013-04-09 18:05:03 | [diff] [blame] | 55 | SpdyFrameType type, |
[email protected] | eb566e51 | 2013-02-26 23:42:18 | [diff] [blame] | 56 | uint8 flags); |
| 57 | |
| 58 | // Populates this frame with a SPDY data frame header using version-specific |
| 59 | // information from the |framer| and length information from capacity_. |
| 60 | bool WriteDataFrameHeader(const SpdyFramer& framer, |
| 61 | SpdyStreamId stream_id, |
| 62 | SpdyDataFlags flags); |
| 63 | |
[email protected] | d5b42bdd | 2013-05-15 20:42:36 | [diff] [blame] | 64 | // Populates this frame with a SPDY4/HTTP2 frame prefix using |
| 65 | // version-specific information from the |framer| and length information from |
| 66 | // capacity_. The given type must be a control frame type. |
| 67 | // Used only for SPDY versions >=4. |
| 68 | bool WriteFramePrefix(const SpdyFramer& framer, |
| 69 | SpdyFrameType type, |
| 70 | uint8 flags, |
| 71 | SpdyStreamId stream_id); |
| 72 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 73 | // Takes the buffer from the SpdyFrameBuilder. |
| 74 | SpdyFrame* take() { |
[email protected] | e251db17 | 2013-02-26 01:45:15 | [diff] [blame] | 75 | SpdyFrame* rv = new SpdyFrame(buffer_.release(), length_, true); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 76 | capacity_ = 0; |
| 77 | length_ = 0; |
| 78 | return rv; |
| 79 | } |
| 80 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 81 | // Methods for adding to the payload. These values are appended to the end |
[email protected] | 39c48fc | 2012-03-12 18:42:12 | [diff] [blame] | 82 | // of the SpdyFrameBuilder payload. Note - binary integers are converted from |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 83 | // host to network form. |
[email protected] | ca33c88 | 2012-03-23 01:39:30 | [diff] [blame] | 84 | bool WriteUInt8(uint8 value) { |
| 85 | return WriteBytes(&value, sizeof(value)); |
| 86 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 87 | bool WriteUInt16(uint16 value) { |
| 88 | value = htons(value); |
| 89 | return WriteBytes(&value, sizeof(value)); |
| 90 | } |
| 91 | bool WriteUInt32(uint32 value) { |
| 92 | value = htonl(value); |
| 93 | return WriteBytes(&value, sizeof(value)); |
| 94 | } |
[email protected] | bd254d5 | 2014-02-12 19:03:55 | [diff] [blame^] | 95 | bool WriteUInt64(uint64 value) { |
| 96 | uint32 upper = htonl(value >> 32); |
| 97 | uint32 lower = htonl(value); |
| 98 | return (WriteBytes(&upper, sizeof(upper)) && |
| 99 | WriteBytes(&lower, sizeof(lower))); |
| 100 | } |
[email protected] | 39c48fc | 2012-03-12 18:42:12 | [diff] [blame] | 101 | // TODO(hkhalil) Rename to WriteStringPiece16(). |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 102 | bool WriteString(const std::string& value); |
[email protected] | 39c48fc | 2012-03-12 18:42:12 | [diff] [blame] | 103 | bool WriteStringPiece32(const base::StringPiece& value); |
[email protected] | fb2323d7 | 2011-11-29 03:13:03 | [diff] [blame] | 104 | bool WriteBytes(const void* data, uint32 data_len); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 105 | |
[email protected] | 4e97435 | 2013-02-15 17:56:57 | [diff] [blame] | 106 | // Update (in-place) the length field in the frame being built to reflect the |
| 107 | // current actual length of bytes written to said frame through this builder. |
| 108 | // The framer parameter is used to determine version-specific location and |
| 109 | // size information of the length field to be written, and must be initialized |
| 110 | // with the correct version for the frame being written. |
| 111 | bool RewriteLength(const SpdyFramer& framer); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 112 | |
[email protected] | 3b6c899 | 2013-02-19 22:10:02 | [diff] [blame] | 113 | // Update (in-place) the length field in the frame being built to reflect the |
| 114 | // given length. |
| 115 | // The framer parameter is used to determine version-specific location and |
| 116 | // size information of the length field to be written, and must be initialized |
| 117 | // with the correct version for the frame being written. |
| 118 | bool OverwriteLength(const SpdyFramer& framer, size_t length); |
| 119 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 120 | private: |
[email protected] | 4e97435 | 2013-02-15 17:56:57 | [diff] [blame] | 121 | // Checks to make sure that there is an appropriate amount of space for a |
| 122 | // write of given size, in bytes. |
| 123 | bool CanWrite(size_t length) const; |
[email protected] | 5e8bf9c | 2012-04-13 00:47:53 | [diff] [blame] | 124 | |
[email protected] | f707c34 | 2013-01-09 04:40:25 | [diff] [blame] | 125 | scoped_ptr<char[]> buffer_; |
[email protected] | eb566e51 | 2013-02-26 23:42:18 | [diff] [blame] | 126 | size_t capacity_; // Allocation size of payload, set by constructor. |
| 127 | size_t length_; // Current length of the buffer. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 128 | }; |
| 129 | |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 130 | } // namespace net |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 131 | |
[email protected] | 1407b6e | 2010-08-27 21:39:48 | [diff] [blame] | 132 | #endif // NET_SPDY_SPDY_FRAME_BUILDER_H_ |