[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] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 8 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 9 | #include <string> |
| 10 | |
[email protected] | 1407b6e | 2010-08-27 21:39:48 | [diff] [blame] | 11 | #include "base/basictypes.h" |
[email protected] | 39c48fc | 2012-03-12 18:42:12 | [diff] [blame] | 12 | #include "base/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 | |
| 19 | // This class provides facilities for basic binary value packing and unpacking |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 20 | // into Spdy frames. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 21 | // |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 22 | // The SpdyFrameBuilder supports appending primitive values (int, string, etc) |
| 23 | // to a frame instance. The SpdyFrameBuilder grows its internal memory buffer |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 24 | // dynamically to hold the sequence of primitive values. The internal memory |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 25 | // buffer is exposed as the "data" of the SpdyFrameBuilder. |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 26 | class NET_EXPORT_PRIVATE SpdyFrameBuilder { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 27 | public: |
[email protected] | fb2323d7 | 2011-11-29 03:13:03 | [diff] [blame] | 28 | ~SpdyFrameBuilder(); |
| 29 | |
[email protected] | 69099324 | 2012-04-16 18:06:11 | [diff] [blame] | 30 | // Initializes a SpdyFrameBuilder with a buffer of given size, |
[email protected] | a053b44 | 2012-04-18 00:28:41 | [diff] [blame] | 31 | // populate with a SPDY control frame header based on |
| 32 | // |type|, |flags|, and |spdy_version|. |
[email protected] | 69099324 | 2012-04-16 18:06:11 | [diff] [blame] | 33 | SpdyFrameBuilder(SpdyControlType type, SpdyControlFlags flags, |
| 34 | int spdy_version, size_t size); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 35 | |
[email protected] | 69099324 | 2012-04-16 18:06:11 | [diff] [blame] | 36 | // Initiailizes a SpdyFrameBuilder with a buffer of given size, |
| 37 | // populated with a SPDY data frame header based on |
| 38 | // |stream_id| and |flags|. |
| 39 | SpdyFrameBuilder(SpdyStreamId stream_id, SpdyDataFlags flags, size_t size); |
[email protected] | 8ac2a02 | 2011-11-04 00:01:57 | [diff] [blame] | 40 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 41 | // Returns the size of the SpdyFrameBuilder's data. |
[email protected] | bbfc65e | 2012-04-21 01:32:40 | [diff] [blame^] | 42 | size_t length() const { return length_; } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 43 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 44 | // Takes the buffer from the SpdyFrameBuilder. |
| 45 | SpdyFrame* take() { |
| 46 | SpdyFrame* rv = new SpdyFrame(buffer_, true); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 47 | buffer_ = NULL; |
| 48 | capacity_ = 0; |
| 49 | length_ = 0; |
| 50 | return rv; |
| 51 | } |
| 52 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 53 | // Methods for adding to the payload. These values are appended to the end |
[email protected] | 39c48fc | 2012-03-12 18:42:12 | [diff] [blame] | 54 | // of the SpdyFrameBuilder payload. Note - binary integers are converted from |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 55 | // host to network form. |
[email protected] | ca33c88 | 2012-03-23 01:39:30 | [diff] [blame] | 56 | bool WriteUInt8(uint8 value) { |
| 57 | return WriteBytes(&value, sizeof(value)); |
| 58 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 59 | bool WriteUInt16(uint16 value) { |
| 60 | value = htons(value); |
| 61 | return WriteBytes(&value, sizeof(value)); |
| 62 | } |
| 63 | bool WriteUInt32(uint32 value) { |
| 64 | value = htonl(value); |
| 65 | return WriteBytes(&value, sizeof(value)); |
| 66 | } |
[email protected] | 39c48fc | 2012-03-12 18:42:12 | [diff] [blame] | 67 | // TODO(hkhalil) Rename to WriteStringPiece16(). |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 68 | bool WriteString(const std::string& value); |
[email protected] | 39c48fc | 2012-03-12 18:42:12 | [diff] [blame] | 69 | bool WriteStringPiece32(const base::StringPiece& value); |
[email protected] | fb2323d7 | 2011-11-29 03:13:03 | [diff] [blame] | 70 | bool WriteBytes(const void* data, uint32 data_len); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 71 | |
| 72 | // Write an integer to a particular offset in the data buffer. |
| 73 | bool WriteUInt32ToOffset(int offset, uint32 value) { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 74 | value = htonl(value); |
[email protected] | 289b93d7 | 2009-10-20 15:32:32 | [diff] [blame] | 75 | return WriteBytesToOffset(offset, &value, sizeof(value)); |
| 76 | } |
| 77 | |
| 78 | // Write to a particular offset in the data buffer. |
| 79 | bool WriteBytesToOffset(int offset, const void* data, uint32 data_len) { |
| 80 | if (offset + data_len > length_) |
| 81 | return false; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 82 | char *ptr = buffer_ + offset; |
[email protected] | 289b93d7 | 2009-10-20 15:32:32 | [diff] [blame] | 83 | memcpy(ptr, data, data_len); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 87 | // Returns true if the given iterator could point to data with the given |
| 88 | // length. If there is no room for the given data before the end of the |
| 89 | // payload, returns false. |
| 90 | bool IteratorHasRoomFor(const void* iter, int len) const { |
| 91 | const char* end_of_region = reinterpret_cast<const char*>(iter) + len; |
[email protected] | d8c21a5 | 2009-10-07 01:55:21 | [diff] [blame] | 92 | if (len < 0 || |
| 93 | iter < buffer_ || |
| 94 | iter > end_of_payload() || |
| 95 | iter > end_of_region || |
| 96 | end_of_region > end_of_payload()) |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 97 | return false; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 98 | |
| 99 | // Watch out for overflow in pointer calculation, which wraps. |
| 100 | return (iter <= end_of_region) && (end_of_region <= end_of_payload()); |
| 101 | } |
| 102 | |
| 103 | protected: |
| 104 | size_t capacity() const { |
| 105 | return capacity_; |
| 106 | } |
| 107 | |
| 108 | const char* end_of_payload() const { return buffer_ + length_; } |
| 109 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 110 | // Completes the write operation by padding the data with NULL bytes until it |
| 111 | // is padded. Should be paired with BeginWrite, but it does not necessarily |
| 112 | // have to be called after the data is written. |
| 113 | void EndWrite(char* dest, int length); |
| 114 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 115 | // Moves the iterator by the given number of bytes. |
| 116 | static void UpdateIter(void** iter, int bytes) { |
| 117 | *iter = static_cast<char*>(*iter) + bytes; |
| 118 | } |
| 119 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 120 | private: |
[email protected] | 5e8bf9c | 2012-04-13 00:47:53 | [diff] [blame] | 121 | // Returns the location that the data should be written at, or NULL if there |
| 122 | // is not enough room. Call EndWrite with the returned offset and the given |
| 123 | // length to pad out for the next write. |
| 124 | char* BeginWrite(size_t length); |
| 125 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 126 | char* buffer_; |
| 127 | size_t capacity_; // Allocation size of payload (or -1 if buffer is const). |
| 128 | size_t length_; // current length of the buffer |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 129 | }; |
| 130 | |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 131 | } // namespace net |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 132 | |
[email protected] | 1407b6e | 2010-08-27 21:39:48 | [diff] [blame] | 133 | #endif // NET_SPDY_SPDY_FRAME_BUILDER_H_ |