blob: 2cecd65b50e6a8392cc064d09c414f1489f5b42f [file] [log] [blame]
[email protected]39c48fc2012-03-12 18:42:121// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]aea80602009-09-18 00:55:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1407b6e2010-08-27 21:39:485#ifndef NET_SPDY_SPDY_FRAME_BUILDER_H_
6#define NET_SPDY_SPDY_FRAME_BUILDER_H_
[email protected]aea80602009-09-18 00:55:087
[email protected]aea80602009-09-18 00:55:088#include <string>
9
[email protected]1407b6e2010-08-27 21:39:4810#include "base/basictypes.h"
[email protected]f707c342013-01-09 04:40:2511#include "base/memory/scoped_ptr.h"
[email protected]d069c11a2013-04-13 00:01:5512#include "base/strings/string_piece.h"
[email protected]0e6f6192011-12-28 23:18:2113#include "base/sys_byteorder.h"
[email protected]172da1b2011-08-12 15:52:2614#include "net/base/net_export.h"
[email protected]dab9c7d2010-02-06 21:44:3215#include "net/spdy/spdy_protocol.h"
[email protected]aea80602009-09-18 00:55:0816
[email protected]ff98d7f02012-03-22 21:44:1917namespace net {
[email protected]aea80602009-09-18 00:55:0818
[email protected]4e974352013-02-15 17:56:5719class SpdyFramer;
20
[email protected]f707c342013-01-09 04:40:2521// This class provides facilities for basic binary value packing
[email protected]955fc2e72010-02-08 20:37:3022// into Spdy frames.
[email protected]aea80602009-09-18 00:55:0823//
[email protected]955fc2e72010-02-08 20:37:3024// The SpdyFrameBuilder supports appending primitive values (int, string, etc)
25// to a frame instance. The SpdyFrameBuilder grows its internal memory buffer
[email protected]aea80602009-09-18 00:55:0826// dynamically to hold the sequence of primitive values. The internal memory
[email protected]955fc2e72010-02-08 20:37:3027// buffer is exposed as the "data" of the SpdyFrameBuilder.
[email protected]172da1b2011-08-12 15:52:2628class NET_EXPORT_PRIVATE SpdyFrameBuilder {
[email protected]aea80602009-09-18 00:55:0829 public:
[email protected]a944f74e2012-12-19 23:10:2830 // Initializes a SpdyFrameBuilder with a buffer of given size
[email protected]144a8c72014-04-29 17:02:0631 SpdyFrameBuilder(size_t size, SpdyMajorVersion version);
[email protected]a944f74e2012-12-19 23:10:2832
[email protected]f707c342013-01-09 04:40:2533 ~SpdyFrameBuilder();
34
[email protected]144a8c72014-04-29 17:02:0635 // Returns the total size of the SpdyFrameBuilder's data, which may include
36 // multiple frames.
37 size_t length() const { return offset_ + length_; }
[email protected]aea80602009-09-18 00:55:0838
[email protected]4e974352013-02-15 17:56:5739 // Returns a writeable buffer of given size in bytes, to be appended to the
40 // currently written frame. Does bounds checking on length but does not
41 // increment the underlying iterator. To do so, consumers should subsequently
42 // call Seek().
43 // In general, consumers should use Write*() calls instead of this.
44 // Returns NULL on failure.
45 char* GetWritableBuffer(size_t length);
46
47 // Seeks forward by the given number of bytes. Useful in conjunction with
48 // GetWriteableBuffer() above.
49 bool Seek(size_t length);
50
[email protected]eb566e512013-02-26 23:42:1851 // Populates this frame with a SPDY control frame header using
52 // version-specific information from the |framer| and length information from
[email protected]7b3fee162013-04-09 18:05:0353 // capacity_. The given type must be a control frame type.
[email protected]d5b42bdd2013-05-15 20:42:3654 // Used only for SPDY versions <4.
[email protected]eb566e512013-02-26 23:42:1855 bool WriteControlFrameHeader(const SpdyFramer& framer,
[email protected]7b3fee162013-04-09 18:05:0356 SpdyFrameType type,
[email protected]eb566e512013-02-26 23:42:1857 uint8 flags);
58
59 // Populates this frame with a SPDY data frame header using version-specific
60 // information from the |framer| and length information from capacity_.
61 bool WriteDataFrameHeader(const SpdyFramer& framer,
62 SpdyStreamId stream_id,
[email protected]c143f2742014-03-31 00:48:5463 uint8 flags);
[email protected]eb566e512013-02-26 23:42:1864
[email protected]d5b42bdd2013-05-15 20:42:3665 // Populates this frame with a SPDY4/HTTP2 frame prefix using
66 // version-specific information from the |framer| and length information from
67 // capacity_. The given type must be a control frame type.
68 // Used only for SPDY versions >=4.
[email protected]144a8c72014-04-29 17:02:0669 bool BeginNewFrame(const SpdyFramer& framer,
70 SpdyFrameType type,
71 uint8 flags,
72 SpdyStreamId stream_id);
[email protected]d5b42bdd2013-05-15 20:42:3673
[email protected]955fc2e72010-02-08 20:37:3074 // Takes the buffer from the SpdyFrameBuilder.
75 SpdyFrame* take() {
[email protected]144a8c72014-04-29 17:02:0676 if (version_ > SPDY3) {
77 DLOG_IF(DFATAL, SpdyConstants::GetFrameMaximumSize(version_) < length_)
78 << "Frame length " << length_
79 << " is longer than the maximum allowed length.";
80 }
81 SpdyFrame* rv = new SpdyFrame(buffer_.release(), length(), true);
[email protected]aea80602009-09-18 00:55:0882 capacity_ = 0;
83 length_ = 0;
[email protected]144a8c72014-04-29 17:02:0684 offset_ = 0;
[email protected]aea80602009-09-18 00:55:0885 return rv;
86 }
87
[email protected]aea80602009-09-18 00:55:0888 // Methods for adding to the payload. These values are appended to the end
[email protected]39c48fc2012-03-12 18:42:1289 // of the SpdyFrameBuilder payload. Note - binary integers are converted from
[email protected]aea80602009-09-18 00:55:0890 // host to network form.
[email protected]ca33c882012-03-23 01:39:3091 bool WriteUInt8(uint8 value) {
92 return WriteBytes(&value, sizeof(value));
93 }
[email protected]aea80602009-09-18 00:55:0894 bool WriteUInt16(uint16 value) {
95 value = htons(value);
96 return WriteBytes(&value, sizeof(value));
97 }
[email protected]66961b22014-08-21 20:17:3898 bool WriteUInt24(uint32 value) {
99 value = htonl(value);
100 return WriteBytes(reinterpret_cast<char*>(&value) + 1,
101 sizeof(value) - 1);
102 }
[email protected]aea80602009-09-18 00:55:08103 bool WriteUInt32(uint32 value) {
104 value = htonl(value);
105 return WriteBytes(&value, sizeof(value));
106 }
[email protected]bd254d52014-02-12 19:03:55107 bool WriteUInt64(uint64 value) {
108 uint32 upper = htonl(value >> 32);
pkasting16628102014-12-05 22:49:40109 uint32 lower = htonl(static_cast<uint32>(value));
[email protected]bd254d52014-02-12 19:03:55110 return (WriteBytes(&upper, sizeof(upper)) &&
111 WriteBytes(&lower, sizeof(lower)));
112 }
zhuoyu.qian848a33922015-03-18 01:58:00113 bool WriteStringPiece16(const base::StringPiece& value);
[email protected]39c48fc2012-03-12 18:42:12114 bool WriteStringPiece32(const base::StringPiece& value);
[email protected]fb2323d72011-11-29 03:13:03115 bool WriteBytes(const void* data, uint32 data_len);
[email protected]aea80602009-09-18 00:55:08116
[email protected]4e974352013-02-15 17:56:57117 // Update (in-place) the length field in the frame being built to reflect the
118 // current actual length of bytes written to said frame through this builder.
119 // The framer parameter is used to determine version-specific location and
120 // size information of the length field to be written, and must be initialized
121 // with the correct version for the frame being written.
122 bool RewriteLength(const SpdyFramer& framer);
[email protected]aea80602009-09-18 00:55:08123
[email protected]3b6c8992013-02-19 22:10:02124 // Update (in-place) the length field in the frame being built to reflect the
125 // given length.
126 // The framer parameter is used to determine version-specific location and
127 // size information of the length field to be written, and must be initialized
128 // with the correct version for the frame being written.
129 bool OverwriteLength(const SpdyFramer& framer, size_t length);
130
[email protected]c143f2742014-03-31 00:48:54131 // Update (in-place) the flags field in the frame being built to reflect the
132 // given flags value.
133 // Used only for SPDY versions >=4.
134 bool OverwriteFlags(const SpdyFramer& framer, uint8 flags);
135
[email protected]aea80602009-09-18 00:55:08136 private:
[email protected]4e974352013-02-15 17:56:57137 // Checks to make sure that there is an appropriate amount of space for a
138 // write of given size, in bytes.
139 bool CanWrite(size_t length) const;
[email protected]5e8bf9c2012-04-13 00:47:53140
[email protected]f707c342013-01-09 04:40:25141 scoped_ptr<char[]> buffer_;
[email protected]eb566e512013-02-26 23:42:18142 size_t capacity_; // Allocation size of payload, set by constructor.
[email protected]144a8c72014-04-29 17:02:06143 size_t length_; // Length of the latest frame in the buffer.
144 size_t offset_; // Position at which the latest frame begins.
145
146 const SpdyMajorVersion version_;
[email protected]aea80602009-09-18 00:55:08147};
148
[email protected]ff98d7f02012-03-22 21:44:19149} // namespace net
[email protected]aea80602009-09-18 00:55:08150
[email protected]1407b6e2010-08-27 21:39:48151#endif // NET_SPDY_SPDY_FRAME_BUILDER_H_