Fix crash in SpeechRecognizerImpl introduced in AudioParams refactor.
[chromium-blink-merge.git] / net / tools / balsa / balsa_headers.h
blob3fc01c4c157a0c1651f686a776426f97cec02304
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/strings/string_piece.h"
17 #include "net/tools/balsa/balsa_enums.h"
18 #include "net/tools/balsa/string_piece_utils.h"
20 namespace net {
22 // WARNING:
23 // Note that -no- char* returned by any function in this
24 // file is null-terminated.
26 // This class exists to service the specific needs of BalsaHeaders.
28 // Functional goals:
29 // 1) provide a backing-store for all of the StringPieces that BalsaHeaders
30 // returns. Every StringPiece returned from BalsaHeaders should remain
31 // valid until the BalsaHeader's object is cleared, or the header-line is
32 // erased.
33 // 2) provide a backing-store for BalsaFrame, which requires contiguous memory
34 // for its fast-path parsing functions. Note that the cost of copying is
35 // less than the cost of requiring the parser to do slow-path parsing, as
36 // it would have to check for bounds every byte, instead of every 16 bytes.
38 // This class is optimized for the case where headers are stored in one of two
39 // buffers. It doesn't make a lot of effort to densely pack memory-- in fact,
40 // it -may- be somewhat memory inefficient. This possible inefficiency allows a
41 // certain simplicity of implementation and speed which makes it worthwhile.
42 // If, in the future, better memory density is required, it should be possible
43 // to reuse the abstraction presented by this object to achieve those goals.
45 // In the most common use-case, this memory inefficiency should be relatively
46 // small.
48 // Alternate implementations of BalsaBuffer may include:
49 // - vector of strings, one per header line (similar to HTTPHeaders)
50 // - densely packed strings:
51 // - keep a sorted array/map of free-space linked lists or numbers.
52 // - use the entry that most closely first your needs.
53 // - at this point, perhaps just use a vector of strings, and let
54 // the allocator do the right thing.
56 class BalsaBuffer {
57 public:
58 static const size_t kDefaultBlocksize = 4096;
59 // We have two friends here. These exist as friends as we
60 // want to allow access to the constructors for the test
61 // class and the Balsa* classes. We put this into the
62 // header file as we want this class to be inlined into the
63 // BalsaHeaders implementation, yet be testable.
64 friend class BalsaBufferTestSpouse;
65 friend class BalsaHeaders;
66 friend class BalsaBufferTest;
68 // The BufferBlock is a structure used internally by the
69 // BalsaBuffer class to store the base buffer pointers to
70 // each block, as well as the important metadata for buffer
71 // sizes and bytes free.
72 struct BufferBlock {
73 public:
74 char* buffer;
75 size_t buffer_size;
76 size_t bytes_free;
78 size_t bytes_used() const {
79 return buffer_size - bytes_free;
81 char* start_of_unused_bytes() const {
82 return buffer + bytes_used();
85 BufferBlock() : buffer(NULL), buffer_size(0), bytes_free(0) {}
86 ~BufferBlock() {}
88 BufferBlock(char* buf, size_t size, size_t free) :
89 buffer(buf), buffer_size(size), bytes_free(free) {}
90 // Yes we want this to be copyable (it gets stuck into vectors).
91 // For this reason, we don't use scoped ptrs, etc. here-- it
92 // is more efficient to manage this memory externally to this
93 // object.
96 typedef std::vector<BufferBlock> Blocks;
98 ~BalsaBuffer();
100 // Returns the total amount of memory used by the buffer blocks.
101 size_t GetTotalBufferBlockSize() const;
103 const char* GetPtr(Blocks::size_type block_idx) const {
104 DCHECK_LT(block_idx, blocks_.size())
105 << block_idx << ", " << blocks_.size();
106 return blocks_[block_idx].buffer;
109 char* GetPtr(Blocks::size_type block_idx) {
110 DCHECK_LT(block_idx, blocks_.size())
111 << block_idx << ", " << blocks_.size();
112 return blocks_[block_idx].buffer;
115 // This function is different from Write(), as it ensures that the data
116 // stored via subsequent calls to this function are all contiguous (and in
117 // the order in which these writes happened). This is essentially the same
118 // as a string append.
120 // You may call this function at any time between object
121 // construction/Clear(), and the calling of the
122 // NoMoreWriteToContiguousBuffer() function.
124 // You must not call this function after the NoMoreWriteToContiguousBuffer()
125 // function is called, unless a Clear() has been called since.
126 // If you do, the program will abort().
128 // This condition is placed upon this code so that calls to Write() can
129 // append to the buffer in the first block safely, and without invaliding
130 // the StringPiece which it returns.
132 // This function's main intended user is the BalsaFrame class, which,
133 // for reasons of efficiency, requires that the buffer from which it parses
134 // the headers be contiguous.
136 void WriteToContiguousBuffer(const base::StringPiece& sp);
138 void NoMoreWriteToContiguousBuffer() {
139 can_write_to_contiguous_buffer_ = false;
142 // Takes a StringPiece and writes it to "permanent" storage, then returns a
143 // StringPiece which points to that data. If block_idx != NULL, it will be
144 // assigned the index of the block into which the data was stored.
145 // Note that the 'permanent' storage in which it stores data may be in
146 // the first block IFF the NoMoreWriteToContiguousBuffer function has
147 // been called since the last Clear/Construction.
148 base::StringPiece Write(const base::StringPiece& sp,
149 Blocks::size_type* block_buffer_idx);
151 // Reserves "permanent" storage of the size indicated. Returns a pointer to
152 // the beginning of that storage, and assigns the index of the block used to
153 // block_buffer_idx. This function uses the first block IFF the
154 // NoMoreWriteToContiguousBuffer function has been called since the last
155 // Clear/Construction.
156 char* Reserve(size_t size, Blocks::size_type* block_buffer_idx);
158 void Clear();
160 void Swap(BalsaBuffer* b);
162 void CopyFrom(const BalsaBuffer& b);
164 const char* StartOfFirstBlock() const {
165 return blocks_[0].buffer;
168 const char* EndOfFirstBlock() const {
169 return blocks_[0].buffer + blocks_[0].bytes_used();
172 bool can_write_to_contiguous_buffer() const {
173 return can_write_to_contiguous_buffer_;
175 size_t blocksize() const { return blocksize_; }
176 Blocks::size_type num_blocks() const { return blocks_.size(); }
177 size_t buffer_size(size_t idx) const { return blocks_[idx].buffer_size; }
178 size_t bytes_used(size_t idx) const { return blocks_[idx].bytes_used(); }
180 protected:
181 BalsaBuffer();
183 explicit BalsaBuffer(size_t blocksize);
185 BufferBlock AllocBlock();
187 BufferBlock AllocCustomBlock(size_t blocksize);
189 BufferBlock CopyBlock(const BufferBlock& b);
191 // Cleans up the object.
192 // The block at start_idx, and all subsequent blocks
193 // will be cleared and have associated memory deleted.
194 void CleanupBlocksStartingFrom(Blocks::size_type start_idx);
196 // A container of BufferBlocks
197 Blocks blocks_;
199 // The default allocation size for a block.
200 // In general, blocksize_ bytes will be allocated for
201 // each buffer.
202 size_t blocksize_;
204 // If set to true, then the first block cannot be used for Write() calls as
205 // the WriteToContiguous... function will modify the base pointer for this
206 // block, and the Write() calls need to be sure that the base pointer will
207 // not be changing in order to provide the user with StringPieces which
208 // continue to be valid.
209 bool can_write_to_contiguous_buffer_;
212 ////////////////////////////////////////////////////////////////////////////////
214 // All of the functions in the BalsaHeaders class use string pieces, by either
215 // using the StringPiece class, or giving an explicit size and char* (as these
216 // are the native representation for these string pieces).
217 // This is done for several reasons.
218 // 1) This minimizes copying/allocation/deallocation as compared to using
219 // string parameters
220 // 2) This reduces the number of strlen() calls done (as the length of any
221 // string passed in is relatively likely to be known at compile time, and for
222 // those strings passed back we obviate the need for a strlen() to determine
223 // the size of new storage allocations if a new allocation is required.
224 // 3) This class attempts to store all of its data in two linear buffers in
225 // order to enhance the speed of parsing and writing out to a buffer. As a
226 // result, many string pieces are -not- terminated by '\0', and are not
227 // c-strings. Since this is the case, we must delineate the length of the
228 // string explicitly via a length.
230 // WARNING: The side effect of using StringPiece is that if the underlying
231 // buffer changes (due to modifying the headers) the StringPieces which point
232 // to the data which was modified, may now contain "garbage", and should not
233 // be dereferenced.
234 // For example, If you fetch some component of the first-line, (request or
235 // response), and then you modify the first line, the StringPieces you
236 // originally received from the original first-line may no longer be valid).
238 // StringPieces pointing to pieces of header lines which have not been
239 // erased() or modified should be valid until the object is cleared or
240 // destroyed.
242 class BalsaHeaders {
243 public:
244 struct HeaderLineDescription {
245 HeaderLineDescription(size_t first_character_index,
246 size_t key_end_index,
247 size_t value_begin_index,
248 size_t last_character_index,
249 size_t buffer_base_index) :
250 first_char_idx(first_character_index),
251 key_end_idx(key_end_index),
252 value_begin_idx(value_begin_index),
253 last_char_idx(last_character_index),
254 buffer_base_idx(buffer_base_index),
255 skip(false) {}
257 HeaderLineDescription() :
258 first_char_idx(0),
259 key_end_idx(0),
260 value_begin_idx(0),
261 last_char_idx(0),
262 buffer_base_idx(0),
263 skip(false) {}
265 size_t first_char_idx;
266 size_t key_end_idx;
267 size_t value_begin_idx;
268 size_t last_char_idx;
269 BalsaBuffer::Blocks::size_type buffer_base_idx;
270 bool skip;
273 typedef std::vector<base::StringPiece> HeaderTokenList;
274 friend bool ParseHTTPFirstLine(const char* begin,
275 const char* end,
276 bool is_request,
277 size_t max_request_uri_length,
278 BalsaHeaders* headers,
279 BalsaFrameEnums::ErrorCode* error_code);
281 protected:
282 typedef std::vector<HeaderLineDescription> HeaderLines;
284 // Why these base classes (iterator_base, reverse_iterator_base)? Well, if
285 // we do want to export both iterator and const_iterator types (currently we
286 // only have const_iterator), then this is useful to avoid code duplication.
287 // Additionally, having this base class makes comparisons of iterators of
288 // different types (they're different types to ensure that operator= and
289 // constructors do not work in the places where they're expected to not work)
290 // work properly. There could be as many as 4 iterator types, all based on
291 // the same data as iterator_base... so it makes sense to simply have some
292 // base classes.
294 class iterator_base {
295 public:
296 friend class BalsaHeaders;
297 friend class reverse_iterator_base;
298 typedef std::pair<base::StringPiece, base::StringPiece> StringPiecePair;
299 typedef StringPiecePair value_type;
300 typedef value_type& reference;
301 typedef value_type* pointer;
303 typedef std::forward_iterator_tag iterator_category;
304 typedef ptrdiff_t difference_type;
306 typedef iterator_base self;
308 // default constructor.
309 iterator_base();
311 // copy constructor.
312 iterator_base(const iterator_base& it);
314 reference operator*() const {
315 return Lookup(idx_);
318 pointer operator->() const {
319 return &(this->operator*());
322 bool operator==(const self& it) const {
323 return idx_ == it.idx_;
326 bool operator<(const self& it) const {
327 return idx_ < it.idx_;
330 bool operator<=(const self& it) const {
331 return idx_ <= it.idx_;
334 bool operator!=(const self& it) const {
335 return !(*this == it);
338 bool operator>(const self& it) const {
339 return it < *this;
342 bool operator>=(const self& it) const {
343 return it <= *this;
346 // This mainly exists so that we can have interesting output for
347 // unittesting. The EXPECT_EQ, EXPECT_NE functions require that
348 // operator<< work for the classes it sees. It would be better if there
349 // was an additional traits-like system for the gUnit output... but oh
350 // well.
351 std::ostream& operator<<(std::ostream& os) const;
353 protected:
354 iterator_base(const BalsaHeaders* headers, HeaderLines::size_type index);
356 void increment() {
357 const HeaderLines& header_lines = headers_->header_lines_;
358 const HeaderLines::size_type header_lines_size = header_lines.size();
359 const HeaderLines::size_type original_idx = idx_;
360 do {
361 ++idx_;
362 } while (idx_ < header_lines_size && header_lines[idx_].skip == true);
363 // The condition below exists so that ++(end() - 1) == end(), even
364 // if there are only 'skip == true' elements between the end() iterator
365 // and the end of the vector of HeaderLineDescriptions.
366 // TODO(fenix): refactor this list so that we don't have to do
367 // linear scanning through skipped headers (and this condition is
368 // then unnecessary)
369 if (idx_ == header_lines_size) {
370 idx_ = original_idx + 1;
374 void decrement() {
375 const HeaderLines& header_lines = headers_->header_lines_;
376 const HeaderLines::size_type header_lines_size = header_lines.size();
377 const HeaderLines::size_type original_idx = idx_;
378 do {
379 --idx_;
380 } while (idx_ < header_lines_size && header_lines[idx_].skip == true);
381 // The condition below exists so that --(rbegin() + 1) == rbegin(), even
382 // if there are only 'skip == true' elements between the rbegin() iterator
383 // and the beginning of the vector of HeaderLineDescriptions.
384 // TODO(fenix): refactor this list so that we don't have to do
385 // linear scanning through skipped headers (and this condition is
386 // then unnecessary)
387 if (idx_ > header_lines_size) {
388 idx_ = original_idx - 1;
392 reference Lookup(HeaderLines::size_type index) const {
393 DCHECK_LT(index, headers_->header_lines_.size());
394 const HeaderLineDescription& line = headers_->header_lines_[index];
395 const char* stream_begin = headers_->GetPtr(line.buffer_base_idx);
396 value_ = value_type(
397 base::StringPiece(stream_begin + line.first_char_idx,
398 line.key_end_idx - line.first_char_idx),
399 base::StringPiece(stream_begin + line.value_begin_idx,
400 line.last_char_idx - line.value_begin_idx));
401 DCHECK_GE(line.key_end_idx, line.first_char_idx);
402 DCHECK_GE(line.last_char_idx, line.value_begin_idx);
403 return value_;
406 const BalsaHeaders* headers_;
407 HeaderLines::size_type idx_;
408 mutable StringPiecePair value_;
411 class reverse_iterator_base : public iterator_base {
412 public:
413 typedef reverse_iterator_base self;
414 typedef iterator_base::reference reference;
415 typedef iterator_base::pointer pointer;
416 using iterator_base::headers_;
417 using iterator_base::idx_;
419 reverse_iterator_base() : iterator_base() {}
421 // This constructor is no explicit purposely.
422 reverse_iterator_base(const iterator_base& it) : // NOLINT
423 iterator_base(it) {
426 self& operator=(const iterator_base& it) {
427 idx_ = it.idx_;
428 headers_ = it.headers_;
429 return *this;
432 self& operator=(const reverse_iterator_base& it) {
433 idx_ = it.idx_;
434 headers_ = it.headers_;
435 return *this;
438 reference operator*() const {
439 return Lookup(idx_ - 1);
442 pointer operator->() const {
443 return &(this->operator*());
446 reverse_iterator_base(const reverse_iterator_base& it) :
447 iterator_base(it) { }
449 protected:
450 void increment() {
451 --idx_;
452 iterator_base::decrement();
453 ++idx_;
456 void decrement() {
457 ++idx_;
458 iterator_base::increment();
459 --idx_;
462 reverse_iterator_base(const BalsaHeaders* headers,
463 HeaderLines::size_type index) :
464 iterator_base(headers, index) {}
467 public:
468 class const_header_lines_iterator : public iterator_base {
469 friend class BalsaHeaders;
470 public:
471 typedef const_header_lines_iterator self;
472 const_header_lines_iterator() : iterator_base() {}
474 const_header_lines_iterator(const const_header_lines_iterator& it) :
475 iterator_base(it.headers_, it.idx_) {}
477 self& operator++() {
478 iterator_base::increment();
479 return *this;
482 self& operator--() {
483 iterator_base::decrement();
484 return *this;
486 protected:
487 const_header_lines_iterator(const BalsaHeaders* headers,
488 HeaderLines::size_type index) :
489 iterator_base(headers, index) {}
492 class const_reverse_header_lines_iterator : public reverse_iterator_base {
493 public:
494 typedef const_reverse_header_lines_iterator self;
495 const_reverse_header_lines_iterator() : reverse_iterator_base() {}
497 const_reverse_header_lines_iterator(
498 const const_header_lines_iterator& it) :
499 reverse_iterator_base(it.headers_, it.idx_) {}
501 const_reverse_header_lines_iterator(
502 const const_reverse_header_lines_iterator& it) :
503 reverse_iterator_base(it.headers_, it.idx_) {}
505 const_header_lines_iterator base() {
506 return const_header_lines_iterator(headers_, idx_);
509 self& operator++() {
510 reverse_iterator_base::increment();
511 return *this;
514 self& operator--() {
515 reverse_iterator_base::decrement();
516 return *this;
518 protected:
519 const_reverse_header_lines_iterator(const BalsaHeaders* headers,
520 HeaderLines::size_type index) :
521 reverse_iterator_base(headers, index) {}
523 friend class BalsaHeaders;
526 // An iterator that only stops at lines with a particular key.
527 // See also GetIteratorForKey.
529 // Check against header_lines_key_end() to determine when iteration is
530 // finished. header_lines_end() will also work.
531 class const_header_lines_key_iterator : public iterator_base {
532 friend class BalsaHeaders;
533 public:
534 typedef const_header_lines_key_iterator self;
535 const_header_lines_key_iterator(const const_header_lines_key_iterator&);
537 self& operator++() {
538 do {
539 iterator_base::increment();
540 } while (!AtEnd() &&
541 !base::EqualsCaseInsensitiveASCII(key_, (**this).first));
542 return *this;
545 void operator++(int ignore) {
546 ++(*this);
549 // Only forward-iteration makes sense, so no operator-- defined.
551 private:
552 const_header_lines_key_iterator(const BalsaHeaders* headers,
553 HeaderLines::size_type index,
554 const base::StringPiece& key);
556 // Should only be used for creating an end iterator.
557 const_header_lines_key_iterator(const BalsaHeaders* headers,
558 HeaderLines::size_type index);
560 bool AtEnd() const {
561 return *this >= headers_->header_lines_end();
564 base::StringPiece key_;
567 // TODO(fenix): Revisit the amount of bytes initially allocated to the second
568 // block of the balsa_buffer_. It may make sense to pre-allocate some amount
569 // (roughly the amount we'd append in new headers such as X-User-Ip, etc.)
570 BalsaHeaders();
571 ~BalsaHeaders();
573 const_header_lines_iterator header_lines_begin() {
574 return HeaderLinesBeginHelper<const_header_lines_iterator>();
577 const_header_lines_iterator header_lines_begin() const {
578 return HeaderLinesBeginHelper<const_header_lines_iterator>();
581 const_header_lines_iterator header_lines_end() {
582 return HeaderLinesEndHelper<const_header_lines_iterator>();
585 const_header_lines_iterator header_lines_end() const {
586 return HeaderLinesEndHelper<const_header_lines_iterator>();
589 const_reverse_header_lines_iterator header_lines_rbegin() {
590 return const_reverse_header_lines_iterator(header_lines_end());
593 const_reverse_header_lines_iterator header_lines_rbegin() const {
594 return const_reverse_header_lines_iterator(header_lines_end());
597 const_reverse_header_lines_iterator header_lines_rend() {
598 return const_reverse_header_lines_iterator(header_lines_begin());
601 const_reverse_header_lines_iterator header_lines_rend() const {
602 return const_reverse_header_lines_iterator(header_lines_begin());
605 const_header_lines_key_iterator header_lines_key_end() const {
606 return HeaderLinesEndHelper<const_header_lines_key_iterator>();
609 void erase(const const_header_lines_iterator& it) {
610 DCHECK_EQ(it.headers_, this);
611 DCHECK_LT(it.idx_, header_lines_.size());
612 DCHECK_GE(it.idx_, 0u);
613 header_lines_[it.idx_].skip = true;
616 void Clear();
618 void Swap(BalsaHeaders* other);
620 void CopyFrom(const BalsaHeaders& other);
622 void HackHeader(const base::StringPiece& key, const base::StringPiece& value);
624 // Same as AppendToHeader, except that it will attempt to preserve
625 // header ordering.
626 // Note that this will always append to an existing header, if available,
627 // without moving the header around, or collapsing multiple header lines
628 // with the same key together. For this reason, it only 'attempts' to
629 // preserve header ordering.
630 // TODO(fenix): remove this function and rename all occurances
631 // of it in the code to AppendToHeader when the condition above
632 // has been satisified.
633 void HackAppendToHeader(const base::StringPiece& key,
634 const base::StringPiece& value);
636 // Replaces header entries with key 'key' if they exist, or appends
637 // a new header if none exist. See 'AppendHeader' below for additional
638 // comments about ContentLength and TransferEncoding headers. Note that this
639 // will allocate new storage every time that it is called.
640 // TODO(fenix): modify this function to reuse existing storage
641 // if it is available.
642 void ReplaceOrAppendHeader(const base::StringPiece& key,
643 const base::StringPiece& value);
645 // Append a new header entry to the header object. Clients who wish to append
646 // Content-Length header should use SetContentLength() method instead of
647 // adding the content length header using AppendHeader (manually adding the
648 // content length header will not update the content_length_ and
649 // content_length_status_ values).
650 // Similarly, clients who wish to add or remove the transfer encoding header
651 // in order to apply or remove chunked encoding should use SetChunkEncoding()
652 // instead.
653 void AppendHeader(const base::StringPiece& key,
654 const base::StringPiece& value);
656 // Appends ',value' to an existing header named 'key'. If no header with the
657 // correct key exists, it will call AppendHeader(key, value). Calling this
658 // function on a key which exists several times in the headers will produce
659 // unpredictable results.
660 void AppendToHeader(const base::StringPiece& key,
661 const base::StringPiece& value);
663 // Prepends 'value,' to an existing header named 'key'. If no header with the
664 // correct key exists, it will call AppendHeader(key, value). Calling this
665 // function on a key which exists several times in the headers will produce
666 // unpredictable results.
667 void PrependToHeader(const base::StringPiece& key,
668 const base::StringPiece& value);
670 const base::StringPiece GetHeader(const base::StringPiece& key) const;
672 // Iterates over all currently valid header lines, appending their
673 // values into the vector 'out', in top-to-bottom order.
674 // Header-lines which have been erased are not currently valid, and
675 // will not have their values appended. Empty values will be
676 // represented as empty string. If 'key' doesn't exist in the headers at
677 // all, out will not be changed. We do not clear the vector out
678 // before adding new entries. If there are header lines with matching
679 // key but empty value then they are also added to the vector out.
680 // (Basically empty values are not treated in any special manner).
682 // Example:
683 // Input header:
684 // "GET / HTTP/1.0\r\n"
685 // "key1: v1\r\n"
686 // "key1: \r\n"
687 // "key1:\r\n"
688 // "key1: v1\r\n"
689 // "key1:v2\r\n"
691 // vector out is initially: ["foo"]
692 // vector out after GetAllOfHeader("key1", &out) is:
693 // ["foo", "v1", "", "", "v2", "v1", "v2"]
695 void GetAllOfHeader(const base::StringPiece& key,
696 std::vector<base::StringPiece>* out) const;
698 // Joins all values for key into a comma-separated string in out.
699 // More efficient than calling JoinStrings on result of GetAllOfHeader if
700 // you don't need the intermediate vector<StringPiece>.
701 void GetAllOfHeaderAsString(const base::StringPiece& key,
702 std::string* out) const;
704 // Returns true if RFC 2616 Section 14 indicates that header can
705 // have multiple values.
706 static bool IsMultivaluedHeader(const base::StringPiece& header);
708 // Determine if a given header is present.
709 inline bool HasHeader(const base::StringPiece& key) const {
710 return (GetConstHeaderLinesIterator(key, header_lines_.begin()) !=
711 header_lines_.end());
714 // Returns true iff any header 'key' exists with non-empty value.
715 bool HasNonEmptyHeader(const base::StringPiece& key) const;
717 const_header_lines_iterator GetHeaderPosition(
718 const base::StringPiece& key) const;
720 // Returns a forward-only iterator that only stops at lines matching key.
721 // String backing 'key' must remain valid for lifetime of iterator.
723 // Check returned iterator against header_lines_key_end() to determine when
724 // iteration is finished.
725 const_header_lines_key_iterator GetIteratorForKey(
726 const base::StringPiece& key) const;
728 void RemoveAllOfHeader(const base::StringPiece& key);
730 // Removes all headers starting with 'key' [case insensitive]
731 void RemoveAllHeadersWithPrefix(const base::StringPiece& key);
733 // Returns the lower bound of memory used by this header object, including
734 // all internal buffers and data structure. Some of the memory used cannot be
735 // directly measure. For example, memory used for bookkeeping by standard
736 // containers.
737 size_t GetMemoryUsedLowerBound() const;
739 // Returns the upper bound on the required buffer space to fully write out
740 // the header object (this include the first line, all header lines, and the
741 // final CRLF that marks the ending of the header).
742 size_t GetSizeForWriteBuffer() const;
744 // The following WriteHeader* methods are template member functions that
745 // place one requirement on the Buffer class: it must implement a Write
746 // method that takes a pointer and a length. The buffer passed in is not
747 // required to be stretchable. For non-stretchable buffers, the user must
748 // call GetSizeForWriteBuffer() to find out the upper bound on the output
749 // buffer space required to make sure that the entire header is serialized.
750 // BalsaHeaders will not check that there is adequate space in the buffer
751 // object during the write.
753 // Writes the entire header and the final CRLF that marks the end of the HTTP
754 // header section to the buffer. After this method returns, no more header
755 // data should be written to the buffer.
756 template <typename Buffer>
757 void WriteHeaderAndEndingToBuffer(Buffer* buffer) const {
758 WriteToBuffer(buffer);
759 WriteHeaderEndingToBuffer(buffer);
762 // Writes the final CRLF to the buffer to terminate the HTTP header section.
763 // After this method returns, no more header data should be written to the
764 // buffer.
765 template <typename Buffer>
766 static void WriteHeaderEndingToBuffer(Buffer* buffer) {
767 buffer->Write("\r\n", 2);
770 // Writes the entire header to the buffer without the CRLF that terminates
771 // the HTTP header. This lets users append additional header lines using
772 // WriteHeaderLineToBuffer and then terminate the header with
773 // WriteHeaderEndingToBuffer as the header is serialized to the
774 // buffer, without having to first copy the header.
775 template <typename Buffer>
776 void WriteToBuffer(Buffer* buffer) const {
777 // write the first line.
778 const size_t firstline_len = whitespace_4_idx_ - non_whitespace_1_idx_;
779 const char* stream_begin = GetPtr(firstline_buffer_base_idx_);
780 buffer->Write(stream_begin + non_whitespace_1_idx_, firstline_len);
781 buffer->Write("\r\n", 2);
782 const HeaderLines::size_type end = header_lines_.size();
783 for (HeaderLines::size_type i = 0; i < end; ++i) {
784 const HeaderLineDescription& line = header_lines_[i];
785 if (line.skip) {
786 continue;
788 const char* line_ptr = GetPtr(line.buffer_base_idx);
789 WriteHeaderLineToBuffer(
790 buffer,
791 base::StringPiece(line_ptr + line.first_char_idx,
792 line.key_end_idx - line.first_char_idx),
793 base::StringPiece(line_ptr + line.value_begin_idx,
794 line.last_char_idx - line.value_begin_idx));
798 // Takes a header line in the form of a key/value pair and append it to the
799 // buffer. This function should be called after WriteToBuffer to
800 // append additional header lines to the header without copying the header.
801 // When the user is done with appending to the buffer,
802 // WriteHeaderEndingToBuffer must be used to terminate the HTTP
803 // header in the buffer. This method is a no-op if key is empty.
804 template <typename Buffer>
805 static void WriteHeaderLineToBuffer(Buffer* buffer,
806 const base::StringPiece& key,
807 const base::StringPiece& value) {
808 // if the key is empty, we don't want to write the rest because it
809 // will not be a well-formed header line.
810 if (!key.empty()) {
811 buffer->Write(key.data(), key.size());
812 buffer->Write(": ", 2);
813 buffer->Write(value.data(), value.size());
814 buffer->Write("\r\n", 2);
818 // Dump the textural representation of the header object to a string, which
819 // is suitable for writing out to logs. All CRLF will be printed out as \n.
820 // This function can be called on a header object in any state. The header
821 // content is appended to the string; the original content is not cleared.
822 void DumpHeadersToString(std::string* str) const;
824 // Calls DumpHeadersToString to dump the textural representation of the header
825 // object to a string. Raw header data will be printed out if the header
826 // object is not completely parsed, e.g., when there was an error in the
827 // middle of parsing.
828 void DumpToString(std::string* str) const;
830 const base::StringPiece first_line() const {
831 DCHECK_GE(whitespace_4_idx_, non_whitespace_1_idx_);
832 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_1_idx_,
833 whitespace_4_idx_ - non_whitespace_1_idx_);
836 // Returns the parsed value of the response code if it has been parsed.
837 // Guaranteed to return 0 when unparsed (though it is a much better idea to
838 // verify that the BalsaFrame had no errors while parsing).
839 // This may return response codes which are outside the normal bounds of
840 // HTTP response codes-- it is up to the user of this class to ensure that
841 // the response code is one which is interpretable.
842 size_t parsed_response_code() const { return parsed_response_code_; }
844 const base::StringPiece request_method() const {
845 DCHECK_GE(whitespace_2_idx_, non_whitespace_1_idx_);
846 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_1_idx_,
847 whitespace_2_idx_ - non_whitespace_1_idx_);
850 const base::StringPiece response_version() const {
851 // Note: There is no difference between request_method() and
852 // response_version(). They both could be called
853 // GetFirstTokenFromFirstline()... but that wouldn't be anywhere near as
854 // descriptive.
855 return request_method();
858 const base::StringPiece request_uri() const {
859 DCHECK_GE(whitespace_3_idx_, non_whitespace_2_idx_);
860 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_2_idx_,
861 whitespace_3_idx_ - non_whitespace_2_idx_);
864 const base::StringPiece response_code() const {
865 // Note: There is no difference between request_uri() and response_code().
866 // They both could be called GetSecondtTokenFromFirstline(), but, as noted
867 // in an earlier comment, that wouldn't be as descriptive.
868 return request_uri();
871 const base::StringPiece request_version() const {
872 DCHECK_GE(whitespace_4_idx_, non_whitespace_3_idx_);
873 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_3_idx_,
874 whitespace_4_idx_ - non_whitespace_3_idx_);
877 const base::StringPiece response_reason_phrase() const {
878 // Note: There is no difference between request_version() and
879 // response_reason_phrase(). They both could be called
880 // GetThirdTokenFromFirstline(), but, as noted in an earlier comment, that
881 // wouldn't be as descriptive.
882 return request_version();
885 // Note that SetFirstLine will not update the internal indices for the
886 // various bits of the first-line (and may set them all to zero).
887 // If you'd like to use the accessors for the various bits of the firstline,
888 // then you should use the Set* functions, or SetFirstlineFromStringPieces,
889 // below, instead.
891 void SetFirstlineFromStringPieces(const base::StringPiece& firstline_a,
892 const base::StringPiece& firstline_b,
893 const base::StringPiece& firstline_c);
895 void SetRequestFirstlineFromStringPieces(const base::StringPiece& method,
896 const base::StringPiece& uri,
897 const base::StringPiece& version) {
898 SetFirstlineFromStringPieces(method, uri, version);
901 void SetResponseFirstlineFromStringPieces(
902 const base::StringPiece& version,
903 const base::StringPiece& code,
904 const base::StringPiece& reason_phrase) {
905 SetFirstlineFromStringPieces(version, code, reason_phrase);
908 // These functions are exactly the same, except that their names are
909 // different. This is done so that the code using this class is more
910 // expressive.
911 void SetRequestMethod(const base::StringPiece& method);
912 void SetResponseVersion(const base::StringPiece& version);
914 void SetRequestUri(const base::StringPiece& uri);
915 void SetResponseCode(const base::StringPiece& code);
916 void set_parsed_response_code(size_t parsed_response_code) {
917 parsed_response_code_ = parsed_response_code;
919 void SetParsedResponseCodeAndUpdateFirstline(size_t parsed_response_code);
921 // These functions are exactly the same, except that their names are
922 // different. This is done so that the code using this class is more
923 // expressive.
924 void SetRequestVersion(const base::StringPiece& version);
925 void SetResponseReasonPhrase(const base::StringPiece& reason_phrase);
927 // The biggest problem with SetFirstLine is that we don't want to use a
928 // separate buffer for it. The second biggest problem with it is that the
929 // first biggest problem requires that we store offsets into a buffer instead
930 // of pointers into a buffer. Cuteness aside, SetFirstLine doesn't parse
931 // the individual fields of the firstline, and so accessors to those fields
932 // will not work properly after calling SetFirstLine. If you want those
933 // accessors to work, use the Set* functions above this one.
934 // SetFirstLine is stuff useful, however, if all you care about is correct
935 // serialization with the rest of the header object.
936 void SetFirstLine(const base::StringPiece& line);
938 // Simple accessors to some of the internal state
939 bool transfer_encoding_is_chunked() const {
940 return transfer_encoding_is_chunked_;
943 static bool ResponseCodeImpliesNoBody(size_t code) {
944 // From HTTP spec section 6.1.1 all 1xx responses must not have a body,
945 // as well as 204 No Content and 304 Not Modified.
946 return ((code >= 100) && (code <= 199)) || (code == 204) || (code == 304);
949 // Note: never check this for requests. Nothing bad will happen if you do,
950 // but spec does not allow requests framed by connection close.
951 // TODO(vitaliyl): refactor.
952 bool is_framed_by_connection_close() const {
953 // We declare that response is framed by connection close if it has no
954 // content-length, no transfer encoding, and is allowed to have a body by
955 // the HTTP spec.
956 // parsed_response_code_ is 0 for requests, so ResponseCodeImpliesNoBody
957 // will return false.
958 return (content_length_status_ == BalsaHeadersEnums::NO_CONTENT_LENGTH) &&
959 !transfer_encoding_is_chunked_ &&
960 !ResponseCodeImpliesNoBody(parsed_response_code_);
963 size_t content_length() const { return content_length_; }
964 BalsaHeadersEnums::ContentLengthStatus content_length_status() const {
965 return content_length_status_;
968 // SetContentLength and SetChunkEncoding modifies the header object to use
969 // content-length and transfer-encoding headers in a consistent manner. They
970 // set all internal flags and status so client can get a consistent view from
971 // various accessors.
972 void SetContentLength(size_t length);
973 void SetChunkEncoding(bool chunk_encode);
975 protected:
976 friend class BalsaFrame;
977 friend class SpdyFrame;
978 friend class HTTPMessage;
979 friend class BalsaHeadersTokenUtils;
981 const char* BeginningOfFirstLine() const {
982 return GetPtr(firstline_buffer_base_idx_);
985 char* GetPtr(BalsaBuffer::Blocks::size_type block_idx) {
986 return balsa_buffer_.GetPtr(block_idx);
989 const char* GetPtr(BalsaBuffer::Blocks::size_type block_idx) const {
990 return balsa_buffer_.GetPtr(block_idx);
993 void WriteFromFramer(const char* ptr, size_t size) {
994 balsa_buffer_.WriteToContiguousBuffer(base::StringPiece(ptr, size));
997 void DoneWritingFromFramer() {
998 balsa_buffer_.NoMoreWriteToContiguousBuffer();
1001 const char* OriginalHeaderStreamBegin() const {
1002 return balsa_buffer_.StartOfFirstBlock();
1005 const char* OriginalHeaderStreamEnd() const {
1006 return balsa_buffer_.EndOfFirstBlock();
1009 size_t GetReadableBytesFromHeaderStream() const {
1010 return OriginalHeaderStreamEnd() - OriginalHeaderStreamBegin();
1013 void GetReadablePtrFromHeaderStream(const char** p, size_t* s) {
1014 *p = OriginalHeaderStreamBegin();
1015 *s = GetReadableBytesFromHeaderStream();
1018 base::StringPiece GetValueFromHeaderLineDescription(
1019 const HeaderLineDescription& line) const;
1021 void AddAndMakeDescription(const base::StringPiece& key,
1022 const base::StringPiece& value,
1023 HeaderLineDescription* d);
1025 void AppendOrPrependAndMakeDescription(const base::StringPiece& key,
1026 const base::StringPiece& value,
1027 bool append,
1028 HeaderLineDescription* d);
1030 // Removes all header lines with the given key starting at start.
1031 void RemoveAllOfHeaderStartingAt(const base::StringPiece& key,
1032 HeaderLines::iterator start);
1034 // If the 'key' does not exist in the headers, calls
1035 // AppendHeader(key, value). Otherwise if append is true, appends ',value'
1036 // to the first existing header with key 'key'. If append is false, prepends
1037 // 'value,' to the first existing header with key 'key'.
1038 void AppendOrPrependToHeader(const base::StringPiece& key,
1039 const base::StringPiece& value,
1040 bool append);
1042 HeaderLines::const_iterator GetConstHeaderLinesIterator(
1043 const base::StringPiece& key,
1044 HeaderLines::const_iterator start) const;
1046 HeaderLines::iterator GetHeaderLinesIteratorNoSkip(
1047 const base::StringPiece& key,
1048 HeaderLines::iterator start);
1050 HeaderLines::iterator GetHeaderLinesIterator(
1051 const base::StringPiece& key,
1052 HeaderLines::iterator start);
1054 template <typename IteratorType>
1055 const IteratorType HeaderLinesBeginHelper() const {
1056 if (header_lines_.empty()) {
1057 return IteratorType(this, 0);
1059 const HeaderLines::size_type header_lines_size = header_lines_.size();
1060 for (HeaderLines::size_type i = 0; i < header_lines_size; ++i) {
1061 if (header_lines_[i].skip == false) {
1062 return IteratorType(this, i);
1065 return IteratorType(this, 0);
1068 template <typename IteratorType>
1069 const IteratorType HeaderLinesEndHelper() const {
1070 if (header_lines_.empty()) {
1071 return IteratorType(this, 0);
1073 const HeaderLines::size_type header_lines_size = header_lines_.size();
1074 HeaderLines::size_type i = header_lines_size;
1075 do {
1076 --i;
1077 if (header_lines_[i].skip == false) {
1078 return IteratorType(this, i + 1);
1080 } while (i != 0);
1081 return IteratorType(this, 0);
1084 // At the moment, this function will always return the original headers.
1085 // In the future, it may not do so after erasing header lines, modifying
1086 // header lines, or modifying the first line.
1087 // For this reason, it is strongly suggested that use of this function is
1088 // only acceptable for the purpose of debugging parse errors seen by the
1089 // BalsaFrame class.
1090 base::StringPiece OriginalHeadersForDebugging() const {
1091 return base::StringPiece(OriginalHeaderStreamBegin(),
1092 OriginalHeaderStreamEnd() - OriginalHeaderStreamBegin());
1095 BalsaBuffer balsa_buffer_;
1097 size_t content_length_;
1098 BalsaHeadersEnums::ContentLengthStatus content_length_status_;
1099 size_t parsed_response_code_;
1100 // HTTP firstlines all have the following structure:
1101 // LWS NONWS LWS NONWS LWS NONWS NOTCRLF CRLF
1102 // [\t \r\n]+ [^\t ]+ [\t ]+ [^\t ]+ [\t ]+ [^\t ]+ [^\r\n]+ "\r\n"
1103 // ws1 nws1 ws2 nws2 ws3 nws3 ws4
1104 // | [-------) [-------) [----------------)
1105 // REQ: method request_uri version
1106 // RESP: version statuscode reason
1108 // The first NONWS->LWS component we'll call firstline_a.
1109 // The second firstline_b, and the third firstline_c.
1111 // firstline_a goes from nws1 to (but not including) ws2
1112 // firstline_b goes from nws2 to (but not including) ws3
1113 // firstline_c goes from nws3 to (but not including) ws4
1115 // In the code:
1116 // ws1 == whitespace_1_idx_
1117 // nws1 == non_whitespace_1_idx_
1118 // ws2 == whitespace_2_idx_
1119 // nws2 == non_whitespace_2_idx_
1120 // ws3 == whitespace_3_idx_
1121 // nws3 == non_whitespace_3_idx_
1122 // ws4 == whitespace_4_idx_
1123 BalsaBuffer::Blocks::size_type firstline_buffer_base_idx_;
1124 size_t whitespace_1_idx_;
1125 size_t non_whitespace_1_idx_;
1126 size_t whitespace_2_idx_;
1127 size_t non_whitespace_2_idx_;
1128 size_t whitespace_3_idx_;
1129 size_t non_whitespace_3_idx_;
1130 size_t whitespace_4_idx_;
1131 size_t end_of_firstline_idx_;
1133 bool transfer_encoding_is_chunked_;
1135 HeaderLines header_lines_;
1138 } // namespace net
1140 #endif // NET_TOOLS_BALSA_BALSA_HEADERS_H_