Home | History | Annotate | Download | only in balsa
      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.
      4 
      5 #ifndef NET_TOOLS_BALSA_BALSA_HEADERS_H_
      6 #define NET_TOOLS_BALSA_BALSA_HEADERS_H_
      7 
      8 #include <algorithm>
      9 #include <iosfwd>
     10 #include <iterator>
     11 #include <string>
     12 #include <utility>
     13 #include <vector>
     14 
     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"
     20 
     21 namespace net {
     22 
     23 // WARNING:
     24 // Note that -no- char* returned by any function in this
     25 // file is null-terminated.
     26 
     27 // This class exists to service the specific needs of BalsaHeaders.
     28 //
     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.
     38 //
     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.
     45 //
     46 // In the most common use-case, this memory inefficiency should be relatively
     47 // small.
     48 //
     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.
     56 //
     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;
     68 
     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;
     78 
     79     size_t bytes_used() const {
     80       return buffer_size - bytes_free;
     81     }
     82     char* start_of_unused_bytes() const {
     83       return buffer + bytes_used();
     84     }
     85 
     86     BufferBlock() : buffer(NULL), buffer_size(0), bytes_free(0) {}
     87     ~BufferBlock() {}
     88 
     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.
     95   };
     96 
     97   typedef std::vector<BufferBlock> Blocks;
     98 
     99   ~BalsaBuffer();
    100 
    101   // Returns the total amount of memory used by the buffer blocks.
    102   size_t GetTotalBufferBlockSize() const;
    103 
    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;
    108   }
    109 
    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;
    114   }
    115 
    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.
    120   //
    121   // You may call this function at any time between object
    122   // construction/Clear(), and the calling of the
    123   // NoMoreWriteToContiguousBuffer() function.
    124   //
    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().
    128   //
    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.
    132   //
    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.
    136   //
    137   void WriteToContiguousBuffer(const base::StringPiece& sp);
    138 
    139   void NoMoreWriteToContiguousBuffer() {
    140     can_write_to_contiguous_buffer_ = false;
    141   }
    142 
    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);
    151 
    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);
    158 
    159   void Clear();
    160 
    161   void Swap(BalsaBuffer* b);
    162 
    163   void CopyFrom(const BalsaBuffer& b);
    164 
    165   const char* StartOfFirstBlock() const {
    166     return blocks_[0].buffer;
    167   }
    168 
    169   const char* EndOfFirstBlock() const {
    170     return blocks_[0].buffer + blocks_[0].bytes_used();
    171   }
    172 
    173   bool can_write_to_contiguous_buffer() const {
    174     return can_write_to_contiguous_buffer_;
    175   }
    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(); }
    180 
    181  protected:
    182   BalsaBuffer();
    183 
    184   explicit BalsaBuffer(size_t blocksize);
    185 
    186   BufferBlock AllocBlock();
    187 
    188   BufferBlock AllocCustomBlock(size_t blocksize);
    189 
    190   BufferBlock CopyBlock(const BufferBlock& b);
    191 
    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);
    196 
    197   // A container of BufferBlocks
    198   Blocks blocks_;
    199 
    200   // The default allocation size for a block.
    201   // In general, blocksize_ bytes will be allocated for
    202   // each buffer.
    203   size_t blocksize_;
    204 
    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_;
    211 };
    212 
    213 ////////////////////////////////////////////////////////////////////////////////
    214 
    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.
    230 //
    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).
    238 //
    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.
    242 
    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) {}
    257 
    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) {}
    265 
    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;
    272   };
    273 
    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);
    281 
    282  protected:
    283   typedef std::vector<HeaderLineDescription> HeaderLines;
    284 
    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.
    294 
    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;
    303 
    304     typedef std::forward_iterator_tag iterator_category;
    305     typedef ptrdiff_t difference_type;
    306 
    307     typedef iterator_base self;
    308 
    309     // default constructor.
    310     iterator_base();
    311 
    312     // copy constructor.
    313     iterator_base(const iterator_base& it);
    314 
    315     reference operator*() const {
    316       return Lookup(idx_);
    317     }
    318 
    319     pointer operator->() const {
    320       return &(this->operator*());
    321     }
    322 
    323     bool operator==(const self& it) const {
    324       return idx_ == it.idx_;
    325     }
    326 
    327     bool operator<(const self& it) const {
    328       return idx_ < it.idx_;
    329     }
    330 
    331     bool operator<=(const self& it) const {
    332       return idx_ <= it.idx_;
    333     }
    334 
    335     bool operator!=(const self& it) const {
    336       return !(*this == it);
    337     }
    338 
    339     bool operator>(const self& it) const {
    340       return it < *this;
    341     }
    342 
    343     bool operator>=(const self& it) const {
    344       return it <= *this;
    345     }
    346 
    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;
    353 
    354    protected:
    355     iterator_base(const BalsaHeaders* headers, HeaderLines::size_type index);
    356 
    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;
    372       }
    373     }
    374 
    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;
    390       }
    391     }
    392 
    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_;
    405     }
    406 
    407     const BalsaHeaders* headers_;
    408     HeaderLines::size_type idx_;
    409     mutable StringPiecePair value_;
    410   };
    411 
    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_;
    419 
    420     reverse_iterator_base() : iterator_base() {}
    421 
    422     // This constructor is no explicit purposely.
    423     reverse_iterator_base(const iterator_base& it) :  // NOLINT
    424         iterator_base(it) {
    425     }
    426 
    427     self& operator=(const iterator_base& it) {
    428       idx_ = it.idx_;
    429       headers_ = it.headers_;
    430       return *this;
    431     }
    432 
    433     self& operator=(const reverse_iterator_base& it) {
    434       idx_ = it.idx_;
    435       headers_ = it.headers_;
    436       return *this;
    437     }
    438 
    439     reference operator*() const {
    440       return Lookup(idx_ - 1);
    441     }
    442 
    443     pointer operator->() const {
    444       return &(this->operator*());
    445     }
    446 
    447     reverse_iterator_base(const reverse_iterator_base& it) :
    448         iterator_base(it) { }
    449 
    450    protected:
    451     void increment() {
    452       --idx_;
    453       iterator_base::decrement();
    454       ++idx_;
    455     }
    456 
    457     void decrement() {
    458       ++idx_;
    459       iterator_base::increment();
    460       --idx_;
    461     }
    462 
    463     reverse_iterator_base(const BalsaHeaders* headers,
    464                           HeaderLines::size_type index) :
    465         iterator_base(headers, index) {}
    466   };
    467 
    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() {}
    474 
    475     const_header_lines_iterator(const const_header_lines_iterator& it) :
    476         iterator_base(it.headers_, it.idx_) {}
    477 
    478     self& operator++() {
    479       iterator_base::increment();
    480       return *this;
    481     }
    482 
    483     self& operator--() {
    484       iterator_base::decrement();
    485       return *this;
    486     }
    487    protected:
    488     const_header_lines_iterator(const BalsaHeaders* headers,
    489                                 HeaderLines::size_type index) :
    490         iterator_base(headers, index) {}
    491   };
    492 
    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() {}
    497 
    498     const_reverse_header_lines_iterator(
    499       const const_header_lines_iterator& it) :
    500         reverse_iterator_base(it.headers_, it.idx_) {}
    501 
    502     const_reverse_header_lines_iterator(
    503       const const_reverse_header_lines_iterator& it) :
    504         reverse_iterator_base(it.headers_, it.idx_) {}
    505 
    506     const_header_lines_iterator base() {
    507       return const_header_lines_iterator(headers_, idx_);
    508     }
    509 
    510     self& operator++() {
    511       reverse_iterator_base::increment();
    512       return *this;
    513     }
    514 
    515     self& operator--() {
    516       reverse_iterator_base::decrement();
    517       return *this;
    518     }
    519    protected:
    520     const_reverse_header_lines_iterator(const BalsaHeaders* headers,
    521                                         HeaderLines::size_type index) :
    522         reverse_iterator_base(headers, index) {}
    523 
    524     friend class BalsaHeaders;
    525   };
    526 
    527   // An iterator that only stops at lines with a particular key.
    528   // See also GetIteratorForKey.
    529   //
    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&);
    537 
    538     self& operator++() {
    539       do {
    540         iterator_base::increment();
    541       } while (!AtEnd() &&
    542                !StringPieceUtils::EqualIgnoreCase(key_, (**this).first));
    543       return *this;
    544     }
    545 
    546     void operator++(int ignore) {
    547       ++(*this);
    548     }
    549 
    550     // Only forward-iteration makes sense, so no operator-- defined.
    551 
    552    private:
    553     const_header_lines_key_iterator(const BalsaHeaders* headers,
    554                                     HeaderLines::size_type index,
    555                                     const base::StringPiece& key);
    556 
    557     // Should only be used for creating an end iterator.
    558     const_header_lines_key_iterator(const BalsaHeaders* headers,
    559                                     HeaderLines::size_type index);
    560 
    561     bool AtEnd() const {
    562       return *this >= headers_->header_lines_end();
    563     }
    564 
    565     base::StringPiece key_;
    566   };
    567 
    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();
    573 
    574   const_header_lines_iterator header_lines_begin() {
    575     return HeaderLinesBeginHelper<const_header_lines_iterator>();
    576   }
    577 
    578   const_header_lines_iterator header_lines_begin() const {
    579     return HeaderLinesBeginHelper<const_header_lines_iterator>();
    580   }
    581 
    582   const_header_lines_iterator header_lines_end() {
    583     return HeaderLinesEndHelper<const_header_lines_iterator>();
    584   }
    585 
    586   const_header_lines_iterator header_lines_end() const {
    587     return HeaderLinesEndHelper<const_header_lines_iterator>();
    588   }
    589 
    590   const_reverse_header_lines_iterator header_lines_rbegin() {
    591     return const_reverse_header_lines_iterator(header_lines_end());
    592   }
    593 
    594   const_reverse_header_lines_iterator header_lines_rbegin() const {
    595     return const_reverse_header_lines_iterator(header_lines_end());
    596   }
    597 
    598   const_reverse_header_lines_iterator header_lines_rend() {
    599     return const_reverse_header_lines_iterator(header_lines_begin());
    600   }
    601 
    602   const_reverse_header_lines_iterator header_lines_rend() const {
    603     return const_reverse_header_lines_iterator(header_lines_begin());
    604   }
    605 
    606   const_header_lines_key_iterator header_lines_key_end() const {
    607     return HeaderLinesEndHelper<const_header_lines_key_iterator>();
    608   }
    609 
    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;
    615   }
    616 
    617   void Clear();
    618 
    619   void Swap(BalsaHeaders* other);
    620 
    621   void CopyFrom(const BalsaHeaders& other);
    622 
    623   void HackHeader(const base::StringPiece& key, const base::StringPiece& value);
    624 
    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);
    636 
    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);
    645 
    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);
    656 
    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);
    663 
    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);
    670 
    671   const base::StringPiece GetHeader(const base::StringPiece& key) const;
    672 
    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).
    682   //
    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"
    691   //
    692   //  vector out is initially: ["foo"]
    693   //  vector out after GetAllOfHeader("key1", &out) is:
    694   // ["foo", "v1", "", "", "v2", "v1", "v2"]
    695 
    696   void GetAllOfHeader(const base::StringPiece& key,
    697                       std::vector<base::StringPiece>* out) const;
    698 
    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;
    704 
    705   // Returns true if RFC 2616 Section 14 indicates that header can
    706   // have multiple values.
    707   static bool IsMultivaluedHeader(const base::StringPiece& header);
    708 
    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());
    713   }
    714 
    715   // Returns true iff any header 'key' exists with non-empty value.
    716   bool HasNonEmptyHeader(const base::StringPiece& key) const;
    717 
    718   const_header_lines_iterator GetHeaderPosition(
    719       const base::StringPiece& key) const;
    720 
    721   // Returns a forward-only iterator that only stops at lines matching key.
    722   // String backing 'key' must remain valid for lifetime of iterator.
    723   //
    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;
    728 
    729   void RemoveAllOfHeader(const base::StringPiece& key);
    730 
    731   // Removes all headers starting with 'key' [case insensitive]
    732   void RemoveAllHeadersWithPrefix(const base::StringPiece& key);
    733 
    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;
    739 
    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;
    744 
    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.
    753 
    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);
    761   }
    762 
    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);
    769   }
    770 
    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;
    788       }
    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));
    796     }
    797   }
    798 
    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);
    816     }
    817   }
    818 
    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
    825   // cleared.
    826   void DumpToString(std::string* str) const;
    827 
    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_);
    832   }
    833 
    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_; }
    841 
    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_);
    846   }
    847 
    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
    852     // descriptive.
    853     return request_method();
    854   }
    855 
    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_);
    860   }
    861 
    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();
    867   }
    868 
    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_);
    873   }
    874 
    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();
    881   }
    882 
    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,
    887   // below, instead.
    888   //
    889   void SetFirstlineFromStringPieces(const base::StringPiece& firstline_a,
    890                                     const base::StringPiece& firstline_b,
    891                                     const base::StringPiece& firstline_c);
    892 
    893   void SetRequestFirstlineFromStringPieces(const base::StringPiece& method,
    894                                            const base::StringPiece& uri,
    895                                            const base::StringPiece& version) {
    896     SetFirstlineFromStringPieces(method, uri, version);
    897   }
    898 
    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);
    904   }
    905 
    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
    908   // expressive.
    909   void SetRequestMethod(const base::StringPiece& method);
    910   void SetResponseVersion(const base::StringPiece& version);
    911 
    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;
    916   }
    917   void SetParsedResponseCodeAndUpdateFirstline(size_t parsed_response_code);
    918 
    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
    921   // expressive.
    922   void SetRequestVersion(const base::StringPiece& version);
    923   void SetResponseReasonPhrase(const base::StringPiece& reason_phrase);
    924 
    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);
    935 
    936   // Simple accessors to some of the internal state
    937   bool transfer_encoding_is_chunked() const {
    938     return transfer_encoding_is_chunked_;
    939   }
    940 
    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);
    945   }
    946 
    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
    953     // the HTTP spec.
    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_);
    959   }
    960 
    961   size_t content_length() const { return content_length_; }
    962   BalsaHeadersEnums::ContentLengthStatus content_length_status() const {
    963     return content_length_status_;
    964   }
    965 
    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);
    972 
    973  protected:
    974   friend class BalsaFrame;
    975   friend class SpdyFrame;
    976   friend class HTTPMessage;
    977   friend class BalsaHeadersTokenUtils;
    978 
    979   const char* BeginningOfFirstLine() const {
    980     return GetPtr(firstline_buffer_base_idx_);
    981   }
    982 
    983   char* GetPtr(BalsaBuffer::Blocks::size_type block_idx) {
    984     return balsa_buffer_.GetPtr(block_idx);
    985   }
    986 
    987   const char* GetPtr(BalsaBuffer::Blocks::size_type block_idx) const {
    988     return balsa_buffer_.GetPtr(block_idx);
    989   }
    990 
    991   void WriteFromFramer(const char* ptr, size_t size) {
    992     balsa_buffer_.WriteToContiguousBuffer(base::StringPiece(ptr, size));
    993   }
    994 
    995   void DoneWritingFromFramer() {
    996     balsa_buffer_.NoMoreWriteToContiguousBuffer();
    997   }
    998 
    999   const char* OriginalHeaderStreamBegin() const {
   1000     return balsa_buffer_.StartOfFirstBlock();
   1001   }
   1002 
   1003   const char* OriginalHeaderStreamEnd() const {
   1004     return balsa_buffer_.EndOfFirstBlock();
   1005   }
   1006 
   1007   size_t GetReadableBytesFromHeaderStream() const {
   1008     return OriginalHeaderStreamEnd() - OriginalHeaderStreamBegin();
   1009   }
   1010 
   1011   void GetReadablePtrFromHeaderStream(const char** p, size_t* s) {
   1012     *p = OriginalHeaderStreamBegin();
   1013     *s = GetReadableBytesFromHeaderStream();
   1014   }
   1015 
   1016   base::StringPiece GetValueFromHeaderLineDescription(
   1017       const HeaderLineDescription& line) const;
   1018 
   1019   void AddAndMakeDescription(const base::StringPiece& key,
   1020                              const base::StringPiece& value,
   1021                              HeaderLineDescription* d);
   1022 
   1023   void AppendOrPrependAndMakeDescription(const base::StringPiece& key,
   1024                                          const base::StringPiece& value,
   1025                                          bool append,
   1026                                          HeaderLineDescription* d);
   1027 
   1028   // Removes all header lines with the given key starting at start.
   1029   void RemoveAllOfHeaderStartingAt(const base::StringPiece& key,
   1030                                    HeaderLines::iterator start);
   1031 
   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,
   1038                                bool append);
   1039 
   1040   HeaderLines::const_iterator GetConstHeaderLinesIterator(
   1041       const base::StringPiece& key,
   1042       HeaderLines::const_iterator start) const;
   1043 
   1044   HeaderLines::iterator GetHeaderLinesIteratorNoSkip(
   1045       const base::StringPiece& key,
   1046       HeaderLines::iterator start);
   1047 
   1048   HeaderLines::iterator GetHeaderLinesIterator(
   1049       const base::StringPiece& key,
   1050       HeaderLines::iterator start);
   1051 
   1052   template <typename IteratorType>
   1053   const IteratorType HeaderLinesBeginHelper() const {
   1054     if (header_lines_.empty()) {
   1055       return IteratorType(this, 0);
   1056     }
   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);
   1061       }
   1062     }
   1063     return IteratorType(this, 0);
   1064   }
   1065 
   1066   template <typename IteratorType>
   1067   const IteratorType HeaderLinesEndHelper() const {
   1068     if (header_lines_.empty()) {
   1069       return IteratorType(this, 0);
   1070     }
   1071     const HeaderLines::size_type header_lines_size = header_lines_.size();
   1072     HeaderLines::size_type i = header_lines_size;
   1073     do {
   1074       --i;
   1075       if (header_lines_[i].skip == false) {
   1076         return IteratorType(this, i + 1);
   1077       }
   1078     } while (i != 0);
   1079     return IteratorType(this, 0);
   1080   }
   1081 
   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());
   1091   }
   1092 
   1093   BalsaBuffer balsa_buffer_;
   1094 
   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
   1105   //
   1106   //   The first NONWS->LWS component we'll call firstline_a.
   1107   //   The second firstline_b, and the third firstline_c.
   1108   //
   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
   1112   //
   1113   // In the code:
   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_;
   1130 
   1131   bool transfer_encoding_is_chunked_;
   1132 
   1133   HeaderLines header_lines_;
   1134 };
   1135 
   1136 }  // namespace net
   1137 
   1138 #endif  // NET_TOOLS_BALSA_BALSA_HEADERS_H_
   1139