We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / tools / balsa / balsa_headers.h
blob72f90c72925860509a3800cee0e3c6e800bb3f90
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_
8 #include <algorithm>
9 #include <iosfwd>
10 #include <iterator>
11 #include <string>
12 #include <utility>
13 #include <vector>
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"
21 namespace net {
23 // WARNING:
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.
29 // Functional goals:
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
33 // erased.
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
47 // small.
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.
57 class BalsaBuffer {
58 public:
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.
73 struct BufferBlock {
74 public:
75 char* buffer;
76 size_t buffer_size;
77 size_t 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) {}
87 ~BufferBlock() {}
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
94 // object.
97 typedef std::vector<BufferBlock> Blocks;
99 ~BalsaBuffer();
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);
159 void Clear();
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(); }
181 protected:
182 BalsaBuffer();
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
198 Blocks blocks_;
200 // The default allocation size for a block.
201 // In general, blocksize_ bytes will be allocated for
202 // each buffer.
203 size_t blocksize_;
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
220 // string parameters
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
234 // be dereferenced.
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
241 // destroyed.
243 class BalsaHeaders {
244 public:
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),
256 skip(false) {}
258 HeaderLineDescription() :
259 first_char_idx(0),
260 key_end_idx(0),
261 value_begin_idx(0),
262 last_char_idx(0),
263 buffer_base_idx(0),
264 skip(false) {}
266 size_t first_char_idx;
267 size_t key_end_idx;
268 size_t value_begin_idx;
269 size_t last_char_idx;
270 BalsaBuffer::Blocks::size_type buffer_base_idx;
271 bool skip;
274 typedef std::vector<base::StringPiece> HeaderTokenList;
275 friend bool ParseHTTPFirstLine(const char* begin,
276 const char* end,
277 bool is_request,
278 size_t max_request_uri_length,
279 BalsaHeaders* headers,
280 BalsaFrameEnums::ErrorCode* error_code);
282 protected:
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
293 // base classes.
295 class iterator_base {
296 public:
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.
310 iterator_base();
312 // copy constructor.
313 iterator_base(const iterator_base& it);
315 reference operator*() const {
316 return Lookup(idx_);
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 {
340 return it < *this;
343 bool operator>=(const self& it) const {
344 return it <= *this;
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
351 // well.
352 std::ostream& operator<<(std::ostream& os) const;
354 protected:
355 iterator_base(const BalsaHeaders* headers, HeaderLines::size_type index);
357 void increment() {
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_;
361 do {
362 ++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
369 // then unnecessary)
370 if (idx_ == header_lines_size) {
371 idx_ = original_idx + 1;
375 void decrement() {
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_;
379 do {
380 --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
387 // then unnecessary)
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);
397 value_ = value_type(
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);
404 return value_;
407 const BalsaHeaders* headers_;
408 HeaderLines::size_type idx_;
409 mutable StringPiecePair value_;
412 class reverse_iterator_base : public iterator_base {
413 public:
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
424 iterator_base(it) {
427 self& operator=(const iterator_base& it) {
428 idx_ = it.idx_;
429 headers_ = it.headers_;
430 return *this;
433 self& operator=(const reverse_iterator_base& it) {
434 idx_ = it.idx_;
435 headers_ = it.headers_;
436 return *this;
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) { }
450 protected:
451 void increment() {
452 --idx_;
453 iterator_base::decrement();
454 ++idx_;
457 void decrement() {
458 ++idx_;
459 iterator_base::increment();
460 --idx_;
463 reverse_iterator_base(const BalsaHeaders* headers,
464 HeaderLines::size_type index) :
465 iterator_base(headers, index) {}
468 public:
469 class const_header_lines_iterator : public iterator_base {
470 friend class BalsaHeaders;
471 public:
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_) {}
478 self& operator++() {
479 iterator_base::increment();
480 return *this;
483 self& operator--() {
484 iterator_base::decrement();
485 return *this;
487 protected:
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 {
494 public:
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_);
510 self& operator++() {
511 reverse_iterator_base::increment();
512 return *this;
515 self& operator--() {
516 reverse_iterator_base::decrement();
517 return *this;
519 protected:
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;
534 public:
535 typedef const_header_lines_key_iterator self;
536 const_header_lines_key_iterator(const const_header_lines_key_iterator&);
538 self& operator++() {
539 do {
540 iterator_base::increment();
541 } while (!AtEnd() &&
542 !StringPieceUtils::EqualIgnoreCase(key_, (**this).first));
543 return *this;
546 void operator++(int ignore) {
547 ++(*this);
550 // Only forward-iteration makes sense, so no operator-- defined.
552 private:
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);
561 bool AtEnd() const {
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.)
571 BalsaHeaders();
572 ~BalsaHeaders();
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;
617 void Clear();
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
626 // header ordering.
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()
653 // instead.
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).
683 // Example:
684 // Input header:
685 // "GET / HTTP/1.0\r\n"
686 // "key1: v1\r\n"
687 // "key1: \r\n"
688 // "key1:\r\n"
689 // "key1: v1\r\n"
690 // "key1:v2\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
737 // containers.
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
765 // buffer.
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];
786 if (line.skip) {
787 continue;
789 const char* line_ptr = GetPtr(line.buffer_base_idx);
790 WriteHeaderLineToBuffer(
791 buffer,
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.
811 if (!key.empty()) {
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. The header
822 // content is appended to the string; the original content is not cleared.
823 void DumpHeadersToString(std::string* str) const;
825 // Calls DumpHeadersToString to dump the textural representation of the header
826 // object to a string. Raw header data will be printed out if the header
827 // object is not completely parsed, e.g., when there was an error in the
828 // middle of parsing.
829 void DumpToString(std::string* str) const;
831 const base::StringPiece first_line() const {
832 DCHECK_GE(whitespace_4_idx_, non_whitespace_1_idx_);
833 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_1_idx_,
834 whitespace_4_idx_ - non_whitespace_1_idx_);
837 // Returns the parsed value of the response code if it has been parsed.
838 // Guaranteed to return 0 when unparsed (though it is a much better idea to
839 // verify that the BalsaFrame had no errors while parsing).
840 // This may return response codes which are outside the normal bounds of
841 // HTTP response codes-- it is up to the user of this class to ensure that
842 // the response code is one which is interpretable.
843 size_t parsed_response_code() const { return parsed_response_code_; }
845 const base::StringPiece request_method() const {
846 DCHECK_GE(whitespace_2_idx_, non_whitespace_1_idx_);
847 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_1_idx_,
848 whitespace_2_idx_ - non_whitespace_1_idx_);
851 const base::StringPiece response_version() const {
852 // Note: There is no difference between request_method() and
853 // response_version(). They both could be called
854 // GetFirstTokenFromFirstline()... but that wouldn't be anywhere near as
855 // descriptive.
856 return request_method();
859 const base::StringPiece request_uri() const {
860 DCHECK_GE(whitespace_3_idx_, non_whitespace_2_idx_);
861 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_2_idx_,
862 whitespace_3_idx_ - non_whitespace_2_idx_);
865 const base::StringPiece response_code() const {
866 // Note: There is no difference between request_uri() and response_code().
867 // They both could be called GetSecondtTokenFromFirstline(), but, as noted
868 // in an earlier comment, that wouldn't be as descriptive.
869 return request_uri();
872 const base::StringPiece request_version() const {
873 DCHECK_GE(whitespace_4_idx_, non_whitespace_3_idx_);
874 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_3_idx_,
875 whitespace_4_idx_ - non_whitespace_3_idx_);
878 const base::StringPiece response_reason_phrase() const {
879 // Note: There is no difference between request_version() and
880 // response_reason_phrase(). They both could be called
881 // GetThirdTokenFromFirstline(), but, as noted in an earlier comment, that
882 // wouldn't be as descriptive.
883 return request_version();
886 // Note that SetFirstLine will not update the internal indices for the
887 // various bits of the first-line (and may set them all to zero).
888 // If you'd like to use the accessors for the various bits of the firstline,
889 // then you should use the Set* functions, or SetFirstlineFromStringPieces,
890 // below, instead.
892 void SetFirstlineFromStringPieces(const base::StringPiece& firstline_a,
893 const base::StringPiece& firstline_b,
894 const base::StringPiece& firstline_c);
896 void SetRequestFirstlineFromStringPieces(const base::StringPiece& method,
897 const base::StringPiece& uri,
898 const base::StringPiece& version) {
899 SetFirstlineFromStringPieces(method, uri, version);
902 void SetResponseFirstlineFromStringPieces(
903 const base::StringPiece& version,
904 const base::StringPiece& code,
905 const base::StringPiece& reason_phrase) {
906 SetFirstlineFromStringPieces(version, code, reason_phrase);
909 // These functions are exactly the same, except that their names are
910 // different. This is done so that the code using this class is more
911 // expressive.
912 void SetRequestMethod(const base::StringPiece& method);
913 void SetResponseVersion(const base::StringPiece& version);
915 void SetRequestUri(const base::StringPiece& uri);
916 void SetResponseCode(const base::StringPiece& code);
917 void set_parsed_response_code(size_t parsed_response_code) {
918 parsed_response_code_ = parsed_response_code;
920 void SetParsedResponseCodeAndUpdateFirstline(size_t parsed_response_code);
922 // These functions are exactly the same, except that their names are
923 // different. This is done so that the code using this class is more
924 // expressive.
925 void SetRequestVersion(const base::StringPiece& version);
926 void SetResponseReasonPhrase(const base::StringPiece& reason_phrase);
928 // The biggest problem with SetFirstLine is that we don't want to use a
929 // separate buffer for it. The second biggest problem with it is that the
930 // first biggest problem requires that we store offsets into a buffer instead
931 // of pointers into a buffer. Cuteness aside, SetFirstLine doesn't parse
932 // the individual fields of the firstline, and so accessors to those fields
933 // will not work properly after calling SetFirstLine. If you want those
934 // accessors to work, use the Set* functions above this one.
935 // SetFirstLine is stuff useful, however, if all you care about is correct
936 // serialization with the rest of the header object.
937 void SetFirstLine(const base::StringPiece& line);
939 // Simple accessors to some of the internal state
940 bool transfer_encoding_is_chunked() const {
941 return transfer_encoding_is_chunked_;
944 static bool ResponseCodeImpliesNoBody(size_t code) {
945 // From HTTP spec section 6.1.1 all 1xx responses must not have a body,
946 // as well as 204 No Content and 304 Not Modified.
947 return ((code >= 100) && (code <= 199)) || (code == 204) || (code == 304);
950 // Note: never check this for requests. Nothing bad will happen if you do,
951 // but spec does not allow requests framed by connection close.
952 // TODO(vitaliyl): refactor.
953 bool is_framed_by_connection_close() const {
954 // We declare that response is framed by connection close if it has no
955 // content-length, no transfer encoding, and is allowed to have a body by
956 // the HTTP spec.
957 // parsed_response_code_ is 0 for requests, so ResponseCodeImpliesNoBody
958 // will return false.
959 return (content_length_status_ == BalsaHeadersEnums::NO_CONTENT_LENGTH) &&
960 !transfer_encoding_is_chunked_ &&
961 !ResponseCodeImpliesNoBody(parsed_response_code_);
964 size_t content_length() const { return content_length_; }
965 BalsaHeadersEnums::ContentLengthStatus content_length_status() const {
966 return content_length_status_;
969 // SetContentLength and SetChunkEncoding modifies the header object to use
970 // content-length and transfer-encoding headers in a consistent manner. They
971 // set all internal flags and status so client can get a consistent view from
972 // various accessors.
973 void SetContentLength(size_t length);
974 void SetChunkEncoding(bool chunk_encode);
976 protected:
977 friend class BalsaFrame;
978 friend class SpdyFrame;
979 friend class HTTPMessage;
980 friend class BalsaHeadersTokenUtils;
982 const char* BeginningOfFirstLine() const {
983 return GetPtr(firstline_buffer_base_idx_);
986 char* GetPtr(BalsaBuffer::Blocks::size_type block_idx) {
987 return balsa_buffer_.GetPtr(block_idx);
990 const char* GetPtr(BalsaBuffer::Blocks::size_type block_idx) const {
991 return balsa_buffer_.GetPtr(block_idx);
994 void WriteFromFramer(const char* ptr, size_t size) {
995 balsa_buffer_.WriteToContiguousBuffer(base::StringPiece(ptr, size));
998 void DoneWritingFromFramer() {
999 balsa_buffer_.NoMoreWriteToContiguousBuffer();
1002 const char* OriginalHeaderStreamBegin() const {
1003 return balsa_buffer_.StartOfFirstBlock();
1006 const char* OriginalHeaderStreamEnd() const {
1007 return balsa_buffer_.EndOfFirstBlock();
1010 size_t GetReadableBytesFromHeaderStream() const {
1011 return OriginalHeaderStreamEnd() - OriginalHeaderStreamBegin();
1014 void GetReadablePtrFromHeaderStream(const char** p, size_t* s) {
1015 *p = OriginalHeaderStreamBegin();
1016 *s = GetReadableBytesFromHeaderStream();
1019 base::StringPiece GetValueFromHeaderLineDescription(
1020 const HeaderLineDescription& line) const;
1022 void AddAndMakeDescription(const base::StringPiece& key,
1023 const base::StringPiece& value,
1024 HeaderLineDescription* d);
1026 void AppendOrPrependAndMakeDescription(const base::StringPiece& key,
1027 const base::StringPiece& value,
1028 bool append,
1029 HeaderLineDescription* d);
1031 // Removes all header lines with the given key starting at start.
1032 void RemoveAllOfHeaderStartingAt(const base::StringPiece& key,
1033 HeaderLines::iterator start);
1035 // If the 'key' does not exist in the headers, calls
1036 // AppendHeader(key, value). Otherwise if append is true, appends ',value'
1037 // to the first existing header with key 'key'. If append is false, prepends
1038 // 'value,' to the first existing header with key 'key'.
1039 void AppendOrPrependToHeader(const base::StringPiece& key,
1040 const base::StringPiece& value,
1041 bool append);
1043 HeaderLines::const_iterator GetConstHeaderLinesIterator(
1044 const base::StringPiece& key,
1045 HeaderLines::const_iterator start) const;
1047 HeaderLines::iterator GetHeaderLinesIteratorNoSkip(
1048 const base::StringPiece& key,
1049 HeaderLines::iterator start);
1051 HeaderLines::iterator GetHeaderLinesIterator(
1052 const base::StringPiece& key,
1053 HeaderLines::iterator start);
1055 template <typename IteratorType>
1056 const IteratorType HeaderLinesBeginHelper() const {
1057 if (header_lines_.empty()) {
1058 return IteratorType(this, 0);
1060 const HeaderLines::size_type header_lines_size = header_lines_.size();
1061 for (HeaderLines::size_type i = 0; i < header_lines_size; ++i) {
1062 if (header_lines_[i].skip == false) {
1063 return IteratorType(this, i);
1066 return IteratorType(this, 0);
1069 template <typename IteratorType>
1070 const IteratorType HeaderLinesEndHelper() const {
1071 if (header_lines_.empty()) {
1072 return IteratorType(this, 0);
1074 const HeaderLines::size_type header_lines_size = header_lines_.size();
1075 HeaderLines::size_type i = header_lines_size;
1076 do {
1077 --i;
1078 if (header_lines_[i].skip == false) {
1079 return IteratorType(this, i + 1);
1081 } while (i != 0);
1082 return IteratorType(this, 0);
1085 // At the moment, this function will always return the original headers.
1086 // In the future, it may not do so after erasing header lines, modifying
1087 // header lines, or modifying the first line.
1088 // For this reason, it is strongly suggested that use of this function is
1089 // only acceptable for the purpose of debugging parse errors seen by the
1090 // BalsaFrame class.
1091 base::StringPiece OriginalHeadersForDebugging() const {
1092 return base::StringPiece(OriginalHeaderStreamBegin(),
1093 OriginalHeaderStreamEnd() - OriginalHeaderStreamBegin());
1096 BalsaBuffer balsa_buffer_;
1098 size_t content_length_;
1099 BalsaHeadersEnums::ContentLengthStatus content_length_status_;
1100 size_t parsed_response_code_;
1101 // HTTP firstlines all have the following structure:
1102 // LWS NONWS LWS NONWS LWS NONWS NOTCRLF CRLF
1103 // [\t \r\n]+ [^\t ]+ [\t ]+ [^\t ]+ [\t ]+ [^\t ]+ [^\r\n]+ "\r\n"
1104 // ws1 nws1 ws2 nws2 ws3 nws3 ws4
1105 // | [-------) [-------) [----------------)
1106 // REQ: method request_uri version
1107 // RESP: version statuscode reason
1109 // The first NONWS->LWS component we'll call firstline_a.
1110 // The second firstline_b, and the third firstline_c.
1112 // firstline_a goes from nws1 to (but not including) ws2
1113 // firstline_b goes from nws2 to (but not including) ws3
1114 // firstline_c goes from nws3 to (but not including) ws4
1116 // In the code:
1117 // ws1 == whitespace_1_idx_
1118 // nws1 == non_whitespace_1_idx_
1119 // ws2 == whitespace_2_idx_
1120 // nws2 == non_whitespace_2_idx_
1121 // ws3 == whitespace_3_idx_
1122 // nws3 == non_whitespace_3_idx_
1123 // ws4 == whitespace_4_idx_
1124 BalsaBuffer::Blocks::size_type firstline_buffer_base_idx_;
1125 size_t whitespace_1_idx_;
1126 size_t non_whitespace_1_idx_;
1127 size_t whitespace_2_idx_;
1128 size_t non_whitespace_2_idx_;
1129 size_t whitespace_3_idx_;
1130 size_t non_whitespace_3_idx_;
1131 size_t whitespace_4_idx_;
1132 size_t end_of_firstline_idx_;
1134 bool transfer_encoding_is_chunked_;
1136 HeaderLines header_lines_;
1139 } // namespace net
1141 #endif // NET_TOOLS_BALSA_BALSA_HEADERS_H_