1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_TOOLS_BALSA_BALSA_HEADERS_H_
6 #define NET_TOOLS_BALSA_BALSA_HEADERS_H_
15 #include "base/logging.h"
16 #include "base/port.h"
17 #include "base/strings/string_piece.h"
18 #include "net/tools/balsa/balsa_enums.h"
19 #include "net/tools/balsa/string_piece_utils.h"
24 // Note that -no- char* returned by any function in this
25 // file is null-terminated.
27 // This class exists to service the specific needs of BalsaHeaders.
30 // 1) provide a backing-store for all of the StringPieces that BalsaHeaders
31 // returns. Every StringPiece returned from BalsaHeaders should remain
32 // valid until the BalsaHeader's object is cleared, or the header-line is
34 // 2) provide a backing-store for BalsaFrame, which requires contiguous memory
35 // for its fast-path parsing functions. Note that the cost of copying is
36 // less than the cost of requiring the parser to do slow-path parsing, as
37 // it would have to check for bounds every byte, instead of every 16 bytes.
39 // This class is optimized for the case where headers are stored in one of two
40 // buffers. It doesn't make a lot of effort to densely pack memory-- in fact,
41 // it -may- be somewhat memory inefficient. This possible inefficiency allows a
42 // certain simplicity of implementation and speed which makes it worthwhile.
43 // If, in the future, better memory density is required, it should be possible
44 // to reuse the abstraction presented by this object to achieve those goals.
46 // In the most common use-case, this memory inefficiency should be relatively
49 // Alternate implementations of BalsaBuffer may include:
50 // - vector of strings, one per header line (similar to HTTPHeaders)
51 // - densely packed strings:
52 // - keep a sorted array/map of free-space linked lists or numbers.
53 // - use the entry that most closely first your needs.
54 // - at this point, perhaps just use a vector of strings, and let
55 // the allocator do the right thing.
59 static const size_t kDefaultBlocksize
= 4096;
60 // We have two friends here. These exist as friends as we
61 // want to allow access to the constructors for the test
62 // class and the Balsa* classes. We put this into the
63 // header file as we want this class to be inlined into the
64 // BalsaHeaders implementation, yet be testable.
65 friend class BalsaBufferTestSpouse
;
66 friend class BalsaHeaders
;
67 friend class BalsaBufferTest
;
69 // The BufferBlock is a structure used internally by the
70 // BalsaBuffer class to store the base buffer pointers to
71 // each block, as well as the important metadata for buffer
72 // sizes and bytes free.
79 size_t bytes_used() const {
80 return buffer_size
- bytes_free
;
82 char* start_of_unused_bytes() const {
83 return buffer
+ bytes_used();
86 BufferBlock() : buffer(NULL
), buffer_size(0), bytes_free(0) {}
89 BufferBlock(char* buf
, size_t size
, size_t free
) :
90 buffer(buf
), buffer_size(size
), bytes_free(free
) {}
91 // Yes we want this to be copyable (it gets stuck into vectors).
92 // For this reason, we don't use scoped ptrs, etc. here-- it
93 // is more efficient to manage this memory externally to this
97 typedef std::vector
<BufferBlock
> Blocks
;
101 // Returns the total amount of memory used by the buffer blocks.
102 size_t GetTotalBufferBlockSize() const;
104 const char* GetPtr(Blocks::size_type block_idx
) const {
105 DCHECK_LT(block_idx
, blocks_
.size())
106 << block_idx
<< ", " << blocks_
.size();
107 return blocks_
[block_idx
].buffer
;
110 char* GetPtr(Blocks::size_type block_idx
) {
111 DCHECK_LT(block_idx
, blocks_
.size())
112 << block_idx
<< ", " << blocks_
.size();
113 return blocks_
[block_idx
].buffer
;
116 // This function is different from Write(), as it ensures that the data
117 // stored via subsequent calls to this function are all contiguous (and in
118 // the order in which these writes happened). This is essentially the same
119 // as a string append.
121 // You may call this function at any time between object
122 // construction/Clear(), and the calling of the
123 // NoMoreWriteToContiguousBuffer() function.
125 // You must not call this function after the NoMoreWriteToContiguousBuffer()
126 // function is called, unless a Clear() has been called since.
127 // If you do, the program will abort().
129 // This condition is placed upon this code so that calls to Write() can
130 // append to the buffer in the first block safely, and without invaliding
131 // the StringPiece which it returns.
133 // This function's main intended user is the BalsaFrame class, which,
134 // for reasons of efficiency, requires that the buffer from which it parses
135 // the headers be contiguous.
137 void WriteToContiguousBuffer(const base::StringPiece
& sp
);
139 void NoMoreWriteToContiguousBuffer() {
140 can_write_to_contiguous_buffer_
= false;
143 // Takes a StringPiece and writes it to "permanent" storage, then returns a
144 // StringPiece which points to that data. If block_idx != NULL, it will be
145 // assigned the index of the block into which the data was stored.
146 // Note that the 'permanent' storage in which it stores data may be in
147 // the first block IFF the NoMoreWriteToContiguousBuffer function has
148 // been called since the last Clear/Construction.
149 base::StringPiece
Write(const base::StringPiece
& sp
,
150 Blocks::size_type
* block_buffer_idx
);
152 // Reserves "permanent" storage of the size indicated. Returns a pointer to
153 // the beginning of that storage, and assigns the index of the block used to
154 // block_buffer_idx. This function uses the first block IFF the
155 // NoMoreWriteToContiguousBuffer function has been called since the last
156 // Clear/Construction.
157 char* Reserve(size_t size
, Blocks::size_type
* block_buffer_idx
);
161 void Swap(BalsaBuffer
* b
);
163 void CopyFrom(const BalsaBuffer
& b
);
165 const char* StartOfFirstBlock() const {
166 return blocks_
[0].buffer
;
169 const char* EndOfFirstBlock() const {
170 return blocks_
[0].buffer
+ blocks_
[0].bytes_used();
173 bool can_write_to_contiguous_buffer() const {
174 return can_write_to_contiguous_buffer_
;
176 size_t blocksize() const { return blocksize_
; }
177 Blocks::size_type
num_blocks() const { return blocks_
.size(); }
178 size_t buffer_size(size_t idx
) const { return blocks_
[idx
].buffer_size
; }
179 size_t bytes_used(size_t idx
) const { return blocks_
[idx
].bytes_used(); }
184 explicit BalsaBuffer(size_t blocksize
);
186 BufferBlock
AllocBlock();
188 BufferBlock
AllocCustomBlock(size_t blocksize
);
190 BufferBlock
CopyBlock(const BufferBlock
& b
);
192 // Cleans up the object.
193 // The block at start_idx, and all subsequent blocks
194 // will be cleared and have associated memory deleted.
195 void CleanupBlocksStartingFrom(Blocks::size_type start_idx
);
197 // A container of BufferBlocks
200 // The default allocation size for a block.
201 // In general, blocksize_ bytes will be allocated for
205 // If set to true, then the first block cannot be used for Write() calls as
206 // the WriteToContiguous... function will modify the base pointer for this
207 // block, and the Write() calls need to be sure that the base pointer will
208 // not be changing in order to provide the user with StringPieces which
209 // continue to be valid.
210 bool can_write_to_contiguous_buffer_
;
213 ////////////////////////////////////////////////////////////////////////////////
215 // All of the functions in the BalsaHeaders class use string pieces, by either
216 // using the StringPiece class, or giving an explicit size and char* (as these
217 // are the native representation for these string pieces).
218 // This is done for several reasons.
219 // 1) This minimizes copying/allocation/deallocation as compared to using
221 // 2) This reduces the number of strlen() calls done (as the length of any
222 // string passed in is relatively likely to be known at compile time, and for
223 // those strings passed back we obviate the need for a strlen() to determine
224 // the size of new storage allocations if a new allocation is required.
225 // 3) This class attempts to store all of its data in two linear buffers in
226 // order to enhance the speed of parsing and writing out to a buffer. As a
227 // result, many string pieces are -not- terminated by '\0', and are not
228 // c-strings. Since this is the case, we must delineate the length of the
229 // string explicitly via a length.
231 // WARNING: The side effect of using StringPiece is that if the underlying
232 // buffer changes (due to modifying the headers) the StringPieces which point
233 // to the data which was modified, may now contain "garbage", and should not
235 // For example, If you fetch some component of the first-line, (request or
236 // response), and then you modify the first line, the StringPieces you
237 // originally received from the original first-line may no longer be valid).
239 // StringPieces pointing to pieces of header lines which have not been
240 // erased() or modified should be valid until the object is cleared or
245 struct HeaderLineDescription
{
246 HeaderLineDescription(size_t first_character_index
,
247 size_t key_end_index
,
248 size_t value_begin_index
,
249 size_t last_character_index
,
250 size_t buffer_base_index
) :
251 first_char_idx(first_character_index
),
252 key_end_idx(key_end_index
),
253 value_begin_idx(value_begin_index
),
254 last_char_idx(last_character_index
),
255 buffer_base_idx(buffer_base_index
),
258 HeaderLineDescription() :
266 size_t first_char_idx
;
268 size_t value_begin_idx
;
269 size_t last_char_idx
;
270 BalsaBuffer::Blocks::size_type buffer_base_idx
;
274 typedef std::vector
<base::StringPiece
> HeaderTokenList
;
275 friend bool ParseHTTPFirstLine(const char* begin
,
278 size_t max_request_uri_length
,
279 BalsaHeaders
* headers
,
280 BalsaFrameEnums::ErrorCode
* error_code
);
283 typedef std::vector
<HeaderLineDescription
> HeaderLines
;
285 // Why these base classes (iterator_base, reverse_iterator_base)? Well, if
286 // we do want to export both iterator and const_iterator types (currently we
287 // only have const_iterator), then this is useful to avoid code duplication.
288 // Additionally, having this base class makes comparisons of iterators of
289 // different types (they're different types to ensure that operator= and
290 // constructors do not work in the places where they're expected to not work)
291 // work properly. There could be as many as 4 iterator types, all based on
292 // the same data as iterator_base... so it makes sense to simply have some
295 class iterator_base
{
297 friend class BalsaHeaders
;
298 friend class reverse_iterator_base
;
299 typedef std::pair
<base::StringPiece
, base::StringPiece
> StringPiecePair
;
300 typedef StringPiecePair value_type
;
301 typedef value_type
& reference
;
302 typedef value_type
* pointer
;
304 typedef std::forward_iterator_tag iterator_category
;
305 typedef ptrdiff_t difference_type
;
307 typedef iterator_base self
;
309 // default constructor.
313 iterator_base(const iterator_base
& it
);
315 reference
operator*() const {
319 pointer
operator->() const {
320 return &(this->operator*());
323 bool operator==(const self
& it
) const {
324 return idx_
== it
.idx_
;
327 bool operator<(const self
& it
) const {
328 return idx_
< it
.idx_
;
331 bool operator<=(const self
& it
) const {
332 return idx_
<= it
.idx_
;
335 bool operator!=(const self
& it
) const {
336 return !(*this == it
);
339 bool operator>(const self
& it
) const {
343 bool operator>=(const self
& it
) const {
347 // This mainly exists so that we can have interesting output for
348 // unittesting. The EXPECT_EQ, EXPECT_NE functions require that
349 // operator<< work for the classes it sees. It would be better if there
350 // was an additional traits-like system for the gUnit output... but oh
352 std::ostream
& operator<<(std::ostream
& os
) const;
355 iterator_base(const BalsaHeaders
* headers
, HeaderLines::size_type index
);
358 const HeaderLines
& header_lines
= headers_
->header_lines_
;
359 const HeaderLines::size_type header_lines_size
= header_lines
.size();
360 const HeaderLines::size_type original_idx
= idx_
;
363 } while (idx_
< header_lines_size
&& header_lines
[idx_
].skip
== true);
364 // The condition below exists so that ++(end() - 1) == end(), even
365 // if there are only 'skip == true' elements between the end() iterator
366 // and the end of the vector of HeaderLineDescriptions.
367 // TODO(fenix): refactor this list so that we don't have to do
368 // linear scanning through skipped headers (and this condition is
370 if (idx_
== header_lines_size
) {
371 idx_
= original_idx
+ 1;
376 const HeaderLines
& header_lines
= headers_
->header_lines_
;
377 const HeaderLines::size_type header_lines_size
= header_lines
.size();
378 const HeaderLines::size_type original_idx
= idx_
;
381 } while (idx_
< header_lines_size
&& header_lines
[idx_
].skip
== true);
382 // The condition below exists so that --(rbegin() + 1) == rbegin(), even
383 // if there are only 'skip == true' elements between the rbegin() iterator
384 // and the beginning of the vector of HeaderLineDescriptions.
385 // TODO(fenix): refactor this list so that we don't have to do
386 // linear scanning through skipped headers (and this condition is
388 if (idx_
> header_lines_size
) {
389 idx_
= original_idx
- 1;
393 reference
Lookup(HeaderLines::size_type index
) const {
394 DCHECK_LT(index
, headers_
->header_lines_
.size());
395 const HeaderLineDescription
& line
= headers_
->header_lines_
[index
];
396 const char* stream_begin
= headers_
->GetPtr(line
.buffer_base_idx
);
398 base::StringPiece(stream_begin
+ line
.first_char_idx
,
399 line
.key_end_idx
- line
.first_char_idx
),
400 base::StringPiece(stream_begin
+ line
.value_begin_idx
,
401 line
.last_char_idx
- line
.value_begin_idx
));
402 DCHECK_GE(line
.key_end_idx
, line
.first_char_idx
);
403 DCHECK_GE(line
.last_char_idx
, line
.value_begin_idx
);
407 const BalsaHeaders
* headers_
;
408 HeaderLines::size_type idx_
;
409 mutable StringPiecePair value_
;
412 class reverse_iterator_base
: public iterator_base
{
414 typedef reverse_iterator_base self
;
415 typedef iterator_base::reference reference
;
416 typedef iterator_base::pointer pointer
;
417 using iterator_base::headers_
;
418 using iterator_base::idx_
;
420 reverse_iterator_base() : iterator_base() {}
422 // This constructor is no explicit purposely.
423 reverse_iterator_base(const iterator_base
& it
) : // NOLINT
427 self
& operator=(const iterator_base
& it
) {
429 headers_
= it
.headers_
;
433 self
& operator=(const reverse_iterator_base
& it
) {
435 headers_
= it
.headers_
;
439 reference
operator*() const {
440 return Lookup(idx_
- 1);
443 pointer
operator->() const {
444 return &(this->operator*());
447 reverse_iterator_base(const reverse_iterator_base
& it
) :
448 iterator_base(it
) { }
453 iterator_base::decrement();
459 iterator_base::increment();
463 reverse_iterator_base(const BalsaHeaders
* headers
,
464 HeaderLines::size_type index
) :
465 iterator_base(headers
, index
) {}
469 class const_header_lines_iterator
: public iterator_base
{
470 friend class BalsaHeaders
;
472 typedef const_header_lines_iterator self
;
473 const_header_lines_iterator() : iterator_base() {}
475 const_header_lines_iterator(const const_header_lines_iterator
& it
) :
476 iterator_base(it
.headers_
, it
.idx_
) {}
479 iterator_base::increment();
484 iterator_base::decrement();
488 const_header_lines_iterator(const BalsaHeaders
* headers
,
489 HeaderLines::size_type index
) :
490 iterator_base(headers
, index
) {}
493 class const_reverse_header_lines_iterator
: public reverse_iterator_base
{
495 typedef const_reverse_header_lines_iterator self
;
496 const_reverse_header_lines_iterator() : reverse_iterator_base() {}
498 const_reverse_header_lines_iterator(
499 const const_header_lines_iterator
& it
) :
500 reverse_iterator_base(it
.headers_
, it
.idx_
) {}
502 const_reverse_header_lines_iterator(
503 const const_reverse_header_lines_iterator
& it
) :
504 reverse_iterator_base(it
.headers_
, it
.idx_
) {}
506 const_header_lines_iterator
base() {
507 return const_header_lines_iterator(headers_
, idx_
);
511 reverse_iterator_base::increment();
516 reverse_iterator_base::decrement();
520 const_reverse_header_lines_iterator(const BalsaHeaders
* headers
,
521 HeaderLines::size_type index
) :
522 reverse_iterator_base(headers
, index
) {}
524 friend class BalsaHeaders
;
527 // An iterator that only stops at lines with a particular key.
528 // See also GetIteratorForKey.
530 // Check against header_lines_key_end() to determine when iteration is
531 // finished. header_lines_end() will also work.
532 class const_header_lines_key_iterator
: public iterator_base
{
533 friend class BalsaHeaders
;
535 typedef const_header_lines_key_iterator self
;
536 const_header_lines_key_iterator(const const_header_lines_key_iterator
&);
540 iterator_base::increment();
542 !StringPieceUtils::EqualIgnoreCase(key_
, (**this).first
));
546 void operator++(int ignore
) {
550 // Only forward-iteration makes sense, so no operator-- defined.
553 const_header_lines_key_iterator(const BalsaHeaders
* headers
,
554 HeaderLines::size_type index
,
555 const base::StringPiece
& key
);
557 // Should only be used for creating an end iterator.
558 const_header_lines_key_iterator(const BalsaHeaders
* headers
,
559 HeaderLines::size_type index
);
562 return *this >= headers_
->header_lines_end();
565 base::StringPiece key_
;
568 // TODO(fenix): Revisit the amount of bytes initially allocated to the second
569 // block of the balsa_buffer_. It may make sense to pre-allocate some amount
570 // (roughly the amount we'd append in new headers such as X-User-Ip, etc.)
574 const_header_lines_iterator
header_lines_begin() {
575 return HeaderLinesBeginHelper
<const_header_lines_iterator
>();
578 const_header_lines_iterator
header_lines_begin() const {
579 return HeaderLinesBeginHelper
<const_header_lines_iterator
>();
582 const_header_lines_iterator
header_lines_end() {
583 return HeaderLinesEndHelper
<const_header_lines_iterator
>();
586 const_header_lines_iterator
header_lines_end() const {
587 return HeaderLinesEndHelper
<const_header_lines_iterator
>();
590 const_reverse_header_lines_iterator
header_lines_rbegin() {
591 return const_reverse_header_lines_iterator(header_lines_end());
594 const_reverse_header_lines_iterator
header_lines_rbegin() const {
595 return const_reverse_header_lines_iterator(header_lines_end());
598 const_reverse_header_lines_iterator
header_lines_rend() {
599 return const_reverse_header_lines_iterator(header_lines_begin());
602 const_reverse_header_lines_iterator
header_lines_rend() const {
603 return const_reverse_header_lines_iterator(header_lines_begin());
606 const_header_lines_key_iterator
header_lines_key_end() const {
607 return HeaderLinesEndHelper
<const_header_lines_key_iterator
>();
610 void erase(const const_header_lines_iterator
& it
) {
611 DCHECK_EQ(it
.headers_
, this);
612 DCHECK_LT(it
.idx_
, header_lines_
.size());
613 DCHECK_GE(it
.idx_
, 0u);
614 header_lines_
[it
.idx_
].skip
= true;
619 void Swap(BalsaHeaders
* other
);
621 void CopyFrom(const BalsaHeaders
& other
);
623 void HackHeader(const base::StringPiece
& key
, const base::StringPiece
& value
);
625 // Same as AppendToHeader, except that it will attempt to preserve
627 // Note that this will always append to an existing header, if available,
628 // without moving the header around, or collapsing multiple header lines
629 // with the same key together. For this reason, it only 'attempts' to
630 // preserve header ordering.
631 // TODO(fenix): remove this function and rename all occurances
632 // of it in the code to AppendToHeader when the condition above
633 // has been satisified.
634 void HackAppendToHeader(const base::StringPiece
& key
,
635 const base::StringPiece
& value
);
637 // Replaces header entries with key 'key' if they exist, or appends
638 // a new header if none exist. See 'AppendHeader' below for additional
639 // comments about ContentLength and TransferEncoding headers. Note that this
640 // will allocate new storage every time that it is called.
641 // TODO(fenix): modify this function to reuse existing storage
642 // if it is available.
643 void ReplaceOrAppendHeader(const base::StringPiece
& key
,
644 const base::StringPiece
& value
);
646 // Append a new header entry to the header object. Clients who wish to append
647 // Content-Length header should use SetContentLength() method instead of
648 // adding the content length header using AppendHeader (manually adding the
649 // content length header will not update the content_length_ and
650 // content_length_status_ values).
651 // Similarly, clients who wish to add or remove the transfer encoding header
652 // in order to apply or remove chunked encoding should use SetChunkEncoding()
654 void AppendHeader(const base::StringPiece
& key
,
655 const base::StringPiece
& value
);
657 // Appends ',value' to an existing header named 'key'. If no header with the
658 // correct key exists, it will call AppendHeader(key, value). Calling this
659 // function on a key which exists several times in the headers will produce
660 // unpredictable results.
661 void AppendToHeader(const base::StringPiece
& key
,
662 const base::StringPiece
& value
);
664 // Prepends 'value,' to an existing header named 'key'. If no header with the
665 // correct key exists, it will call AppendHeader(key, value). Calling this
666 // function on a key which exists several times in the headers will produce
667 // unpredictable results.
668 void PrependToHeader(const base::StringPiece
& key
,
669 const base::StringPiece
& value
);
671 const base::StringPiece
GetHeader(const base::StringPiece
& key
) const;
673 // Iterates over all currently valid header lines, appending their
674 // values into the vector 'out', in top-to-bottom order.
675 // Header-lines which have been erased are not currently valid, and
676 // will not have their values appended. Empty values will be
677 // represented as empty string. If 'key' doesn't exist in the headers at
678 // all, out will not be changed. We do not clear the vector out
679 // before adding new entries. If there are header lines with matching
680 // key but empty value then they are also added to the vector out.
681 // (Basically empty values are not treated in any special manner).
685 // "GET / HTTP/1.0\r\n"
692 // vector out is initially: ["foo"]
693 // vector out after GetAllOfHeader("key1", &out) is:
694 // ["foo", "v1", "", "", "v2", "v1", "v2"]
696 void GetAllOfHeader(const base::StringPiece
& key
,
697 std::vector
<base::StringPiece
>* out
) const;
699 // Joins all values for key into a comma-separated string in out.
700 // More efficient than calling JoinStrings on result of GetAllOfHeader if
701 // you don't need the intermediate vector<StringPiece>.
702 void GetAllOfHeaderAsString(const base::StringPiece
& key
,
703 std::string
* out
) const;
705 // Returns true if RFC 2616 Section 14 indicates that header can
706 // have multiple values.
707 static bool IsMultivaluedHeader(const base::StringPiece
& header
);
709 // Determine if a given header is present.
710 inline bool HasHeader(const base::StringPiece
& key
) const {
711 return (GetConstHeaderLinesIterator(key
, header_lines_
.begin()) !=
712 header_lines_
.end());
715 // Returns true iff any header 'key' exists with non-empty value.
716 bool HasNonEmptyHeader(const base::StringPiece
& key
) const;
718 const_header_lines_iterator
GetHeaderPosition(
719 const base::StringPiece
& key
) const;
721 // Returns a forward-only iterator that only stops at lines matching key.
722 // String backing 'key' must remain valid for lifetime of iterator.
724 // Check returned iterator against header_lines_key_end() to determine when
725 // iteration is finished.
726 const_header_lines_key_iterator
GetIteratorForKey(
727 const base::StringPiece
& key
) const;
729 void RemoveAllOfHeader(const base::StringPiece
& key
);
731 // Removes all headers starting with 'key' [case insensitive]
732 void RemoveAllHeadersWithPrefix(const base::StringPiece
& key
);
734 // Returns the lower bound of memory used by this header object, including
735 // all internal buffers and data structure. Some of the memory used cannot be
736 // directly measure. For example, memory used for bookkeeping by standard
738 size_t GetMemoryUsedLowerBound() const;
740 // Returns the upper bound on the required buffer space to fully write out
741 // the header object (this include the first line, all header lines, and the
742 // final CRLF that marks the ending of the header).
743 size_t GetSizeForWriteBuffer() const;
745 // The following WriteHeader* methods are template member functions that
746 // place one requirement on the Buffer class: it must implement a Write
747 // method that takes a pointer and a length. The buffer passed in is not
748 // required to be stretchable. For non-stretchable buffers, the user must
749 // call GetSizeForWriteBuffer() to find out the upper bound on the output
750 // buffer space required to make sure that the entire header is serialized.
751 // BalsaHeaders will not check that there is adequate space in the buffer
752 // object during the write.
754 // Writes the entire header and the final CRLF that marks the end of the HTTP
755 // header section to the buffer. After this method returns, no more header
756 // data should be written to the buffer.
757 template <typename Buffer
>
758 void WriteHeaderAndEndingToBuffer(Buffer
* buffer
) const {
759 WriteToBuffer(buffer
);
760 WriteHeaderEndingToBuffer(buffer
);
763 // Writes the final CRLF to the buffer to terminate the HTTP header section.
764 // After this method returns, no more header data should be written to the
766 template <typename Buffer
>
767 static void WriteHeaderEndingToBuffer(Buffer
* buffer
) {
768 buffer
->Write("\r\n", 2);
771 // Writes the entire header to the buffer without the CRLF that terminates
772 // the HTTP header. This lets users append additional header lines using
773 // WriteHeaderLineToBuffer and then terminate the header with
774 // WriteHeaderEndingToBuffer as the header is serialized to the
775 // buffer, without having to first copy the header.
776 template <typename Buffer
>
777 void WriteToBuffer(Buffer
* buffer
) const {
778 // write the first line.
779 const size_t firstline_len
= whitespace_4_idx_
- non_whitespace_1_idx_
;
780 const char* stream_begin
= GetPtr(firstline_buffer_base_idx_
);
781 buffer
->Write(stream_begin
+ non_whitespace_1_idx_
, firstline_len
);
782 buffer
->Write("\r\n", 2);
783 const HeaderLines::size_type end
= header_lines_
.size();
784 for (HeaderLines::size_type i
= 0; i
< end
; ++i
) {
785 const HeaderLineDescription
& line
= header_lines_
[i
];
789 const char* line_ptr
= GetPtr(line
.buffer_base_idx
);
790 WriteHeaderLineToBuffer(
792 base::StringPiece(line_ptr
+ line
.first_char_idx
,
793 line
.key_end_idx
- line
.first_char_idx
),
794 base::StringPiece(line_ptr
+ line
.value_begin_idx
,
795 line
.last_char_idx
- line
.value_begin_idx
));
799 // Takes a header line in the form of a key/value pair and append it to the
800 // buffer. This function should be called after WriteToBuffer to
801 // append additional header lines to the header without copying the header.
802 // When the user is done with appending to the buffer,
803 // WriteHeaderEndingToBuffer must be used to terminate the HTTP
804 // header in the buffer. This method is a no-op if key is empty.
805 template <typename Buffer
>
806 static void WriteHeaderLineToBuffer(Buffer
* buffer
,
807 const base::StringPiece
& key
,
808 const base::StringPiece
& value
) {
809 // if the key is empty, we don't want to write the rest because it
810 // will not be a well-formed header line.
812 buffer
->Write(key
.data(), key
.size());
813 buffer
->Write(": ", 2);
814 buffer
->Write(value
.data(), value
.size());
815 buffer
->Write("\r\n", 2);
819 // Dump the textural representation of the header object to a string, which
820 // is suitable for writing out to logs. All CRLF will be printed out as \n.
821 // This function can be called on a header object in any state. Raw header
822 // data will be printed out if the header object is not completely parsed,
823 // e.g., when there was an error in the middle of parsing.
824 // The header content is appended to the string; the original content is not
826 void DumpToString(std::string
* str
) const;
828 const base::StringPiece
first_line() const {
829 DCHECK_GE(whitespace_4_idx_
, non_whitespace_1_idx_
);
830 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_1_idx_
,
831 whitespace_4_idx_
- non_whitespace_1_idx_
);
834 // Returns the parsed value of the response code if it has been parsed.
835 // Guaranteed to return 0 when unparsed (though it is a much better idea to
836 // verify that the BalsaFrame had no errors while parsing).
837 // This may return response codes which are outside the normal bounds of
838 // HTTP response codes-- it is up to the user of this class to ensure that
839 // the response code is one which is interpretable.
840 size_t parsed_response_code() const { return parsed_response_code_
; }
842 const base::StringPiece
request_method() const {
843 DCHECK_GE(whitespace_2_idx_
, non_whitespace_1_idx_
);
844 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_1_idx_
,
845 whitespace_2_idx_
- non_whitespace_1_idx_
);
848 const base::StringPiece
response_version() const {
849 // Note: There is no difference between request_method() and
850 // response_version(). They both could be called
851 // GetFirstTokenFromFirstline()... but that wouldn't be anywhere near as
853 return request_method();
856 const base::StringPiece
request_uri() const {
857 DCHECK_GE(whitespace_3_idx_
, non_whitespace_2_idx_
);
858 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_2_idx_
,
859 whitespace_3_idx_
- non_whitespace_2_idx_
);
862 const base::StringPiece
response_code() const {
863 // Note: There is no difference between request_uri() and response_code().
864 // They both could be called GetSecondtTokenFromFirstline(), but, as noted
865 // in an earlier comment, that wouldn't be as descriptive.
866 return request_uri();
869 const base::StringPiece
request_version() const {
870 DCHECK_GE(whitespace_4_idx_
, non_whitespace_3_idx_
);
871 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_3_idx_
,
872 whitespace_4_idx_
- non_whitespace_3_idx_
);
875 const base::StringPiece
response_reason_phrase() const {
876 // Note: There is no difference between request_version() and
877 // response_reason_phrase(). They both could be called
878 // GetThirdTokenFromFirstline(), but, as noted in an earlier comment, that
879 // wouldn't be as descriptive.
880 return request_version();
883 // Note that SetFirstLine will not update the internal indices for the
884 // various bits of the first-line (and may set them all to zero).
885 // If you'd like to use the accessors for the various bits of the firstline,
886 // then you should use the Set* functions, or SetFirstlineFromStringPieces,
889 void SetFirstlineFromStringPieces(const base::StringPiece
& firstline_a
,
890 const base::StringPiece
& firstline_b
,
891 const base::StringPiece
& firstline_c
);
893 void SetRequestFirstlineFromStringPieces(const base::StringPiece
& method
,
894 const base::StringPiece
& uri
,
895 const base::StringPiece
& version
) {
896 SetFirstlineFromStringPieces(method
, uri
, version
);
899 void SetResponseFirstlineFromStringPieces(
900 const base::StringPiece
& version
,
901 const base::StringPiece
& code
,
902 const base::StringPiece
& reason_phrase
) {
903 SetFirstlineFromStringPieces(version
, code
, reason_phrase
);
906 // These functions are exactly the same, except that their names are
907 // different. This is done so that the code using this class is more
909 void SetRequestMethod(const base::StringPiece
& method
);
910 void SetResponseVersion(const base::StringPiece
& version
);
912 void SetRequestUri(const base::StringPiece
& uri
);
913 void SetResponseCode(const base::StringPiece
& code
);
914 void set_parsed_response_code(size_t parsed_response_code
) {
915 parsed_response_code_
= parsed_response_code
;
917 void SetParsedResponseCodeAndUpdateFirstline(size_t parsed_response_code
);
919 // These functions are exactly the same, except that their names are
920 // different. This is done so that the code using this class is more
922 void SetRequestVersion(const base::StringPiece
& version
);
923 void SetResponseReasonPhrase(const base::StringPiece
& reason_phrase
);
925 // The biggest problem with SetFirstLine is that we don't want to use a
926 // separate buffer for it. The second biggest problem with it is that the
927 // first biggest problem requires that we store offsets into a buffer instead
928 // of pointers into a buffer. Cuteness aside, SetFirstLine doesn't parse
929 // the individual fields of the firstline, and so accessors to those fields
930 // will not work properly after calling SetFirstLine. If you want those
931 // accessors to work, use the Set* functions above this one.
932 // SetFirstLine is stuff useful, however, if all you care about is correct
933 // serialization with the rest of the header object.
934 void SetFirstLine(const base::StringPiece
& line
);
936 // Simple accessors to some of the internal state
937 bool transfer_encoding_is_chunked() const {
938 return transfer_encoding_is_chunked_
;
941 static bool ResponseCodeImpliesNoBody(size_t code
) {
942 // From HTTP spec section 6.1.1 all 1xx responses must not have a body,
943 // as well as 204 No Content and 304 Not Modified.
944 return ((code
>= 100) && (code
<= 199)) || (code
== 204) || (code
== 304);
947 // Note: never check this for requests. Nothing bad will happen if you do,
948 // but spec does not allow requests framed by connection close.
949 // TODO(vitaliyl): refactor.
950 bool is_framed_by_connection_close() const {
951 // We declare that response is framed by connection close if it has no
952 // content-length, no transfer encoding, and is allowed to have a body by
954 // parsed_response_code_ is 0 for requests, so ResponseCodeImpliesNoBody
955 // will return false.
956 return (content_length_status_
== BalsaHeadersEnums::NO_CONTENT_LENGTH
) &&
957 !transfer_encoding_is_chunked_
&&
958 !ResponseCodeImpliesNoBody(parsed_response_code_
);
961 size_t content_length() const { return content_length_
; }
962 BalsaHeadersEnums::ContentLengthStatus
content_length_status() const {
963 return content_length_status_
;
966 // SetContentLength and SetChunkEncoding modifies the header object to use
967 // content-length and transfer-encoding headers in a consistent manner. They
968 // set all internal flags and status so client can get a consistent view from
969 // various accessors.
970 void SetContentLength(size_t length
);
971 void SetChunkEncoding(bool chunk_encode
);
974 friend class BalsaFrame
;
975 friend class SpdyFrame
;
976 friend class HTTPMessage
;
977 friend class BalsaHeadersTokenUtils
;
979 const char* BeginningOfFirstLine() const {
980 return GetPtr(firstline_buffer_base_idx_
);
983 char* GetPtr(BalsaBuffer::Blocks::size_type block_idx
) {
984 return balsa_buffer_
.GetPtr(block_idx
);
987 const char* GetPtr(BalsaBuffer::Blocks::size_type block_idx
) const {
988 return balsa_buffer_
.GetPtr(block_idx
);
991 void WriteFromFramer(const char* ptr
, size_t size
) {
992 balsa_buffer_
.WriteToContiguousBuffer(base::StringPiece(ptr
, size
));
995 void DoneWritingFromFramer() {
996 balsa_buffer_
.NoMoreWriteToContiguousBuffer();
999 const char* OriginalHeaderStreamBegin() const {
1000 return balsa_buffer_
.StartOfFirstBlock();
1003 const char* OriginalHeaderStreamEnd() const {
1004 return balsa_buffer_
.EndOfFirstBlock();
1007 size_t GetReadableBytesFromHeaderStream() const {
1008 return OriginalHeaderStreamEnd() - OriginalHeaderStreamBegin();
1011 void GetReadablePtrFromHeaderStream(const char** p
, size_t* s
) {
1012 *p
= OriginalHeaderStreamBegin();
1013 *s
= GetReadableBytesFromHeaderStream();
1016 base::StringPiece
GetValueFromHeaderLineDescription(
1017 const HeaderLineDescription
& line
) const;
1019 void AddAndMakeDescription(const base::StringPiece
& key
,
1020 const base::StringPiece
& value
,
1021 HeaderLineDescription
* d
);
1023 void AppendOrPrependAndMakeDescription(const base::StringPiece
& key
,
1024 const base::StringPiece
& value
,
1026 HeaderLineDescription
* d
);
1028 // Removes all header lines with the given key starting at start.
1029 void RemoveAllOfHeaderStartingAt(const base::StringPiece
& key
,
1030 HeaderLines::iterator start
);
1032 // If the 'key' does not exist in the headers, calls
1033 // AppendHeader(key, value). Otherwise if append is true, appends ',value'
1034 // to the first existing header with key 'key'. If append is false, prepends
1035 // 'value,' to the first existing header with key 'key'.
1036 void AppendOrPrependToHeader(const base::StringPiece
& key
,
1037 const base::StringPiece
& value
,
1040 HeaderLines::const_iterator
GetConstHeaderLinesIterator(
1041 const base::StringPiece
& key
,
1042 HeaderLines::const_iterator start
) const;
1044 HeaderLines::iterator
GetHeaderLinesIteratorNoSkip(
1045 const base::StringPiece
& key
,
1046 HeaderLines::iterator start
);
1048 HeaderLines::iterator
GetHeaderLinesIterator(
1049 const base::StringPiece
& key
,
1050 HeaderLines::iterator start
);
1052 template <typename IteratorType
>
1053 const IteratorType
HeaderLinesBeginHelper() const {
1054 if (header_lines_
.empty()) {
1055 return IteratorType(this, 0);
1057 const HeaderLines::size_type header_lines_size
= header_lines_
.size();
1058 for (HeaderLines::size_type i
= 0; i
< header_lines_size
; ++i
) {
1059 if (header_lines_
[i
].skip
== false) {
1060 return IteratorType(this, i
);
1063 return IteratorType(this, 0);
1066 template <typename IteratorType
>
1067 const IteratorType
HeaderLinesEndHelper() const {
1068 if (header_lines_
.empty()) {
1069 return IteratorType(this, 0);
1071 const HeaderLines::size_type header_lines_size
= header_lines_
.size();
1072 HeaderLines::size_type i
= header_lines_size
;
1075 if (header_lines_
[i
].skip
== false) {
1076 return IteratorType(this, i
+ 1);
1079 return IteratorType(this, 0);
1082 // At the moment, this function will always return the original headers.
1083 // In the future, it may not do so after erasing header lines, modifying
1084 // header lines, or modifying the first line.
1085 // For this reason, it is strongly suggested that use of this function is
1086 // only acceptable for the purpose of debugging parse errors seen by the
1087 // BalsaFrame class.
1088 base::StringPiece
OriginalHeadersForDebugging() const {
1089 return base::StringPiece(OriginalHeaderStreamBegin(),
1090 OriginalHeaderStreamEnd() - OriginalHeaderStreamBegin());
1093 BalsaBuffer balsa_buffer_
;
1095 size_t content_length_
;
1096 BalsaHeadersEnums::ContentLengthStatus content_length_status_
;
1097 size_t parsed_response_code_
;
1098 // HTTP firstlines all have the following structure:
1099 // LWS NONWS LWS NONWS LWS NONWS NOTCRLF CRLF
1100 // [\t \r\n]+ [^\t ]+ [\t ]+ [^\t ]+ [\t ]+ [^\t ]+ [^\r\n]+ "\r\n"
1101 // ws1 nws1 ws2 nws2 ws3 nws3 ws4
1102 // | [-------) [-------) [----------------)
1103 // REQ: method request_uri version
1104 // RESP: version statuscode reason
1106 // The first NONWS->LWS component we'll call firstline_a.
1107 // The second firstline_b, and the third firstline_c.
1109 // firstline_a goes from nws1 to (but not including) ws2
1110 // firstline_b goes from nws2 to (but not including) ws3
1111 // firstline_c goes from nws3 to (but not including) ws4
1114 // ws1 == whitespace_1_idx_
1115 // nws1 == non_whitespace_1_idx_
1116 // ws2 == whitespace_2_idx_
1117 // nws2 == non_whitespace_2_idx_
1118 // ws3 == whitespace_3_idx_
1119 // nws3 == non_whitespace_3_idx_
1120 // ws4 == whitespace_4_idx_
1121 BalsaBuffer::Blocks::size_type firstline_buffer_base_idx_
;
1122 size_t whitespace_1_idx_
;
1123 size_t non_whitespace_1_idx_
;
1124 size_t whitespace_2_idx_
;
1125 size_t non_whitespace_2_idx_
;
1126 size_t whitespace_3_idx_
;
1127 size_t non_whitespace_3_idx_
;
1128 size_t whitespace_4_idx_
;
1129 size_t end_of_firstline_idx_
;
1131 bool transfer_encoding_is_chunked_
;
1133 HeaderLines header_lines_
;
1138 #endif // NET_TOOLS_BALSA_BALSA_HEADERS_H_