balsa_headers.h revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2009 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_FLIP_SERVER_BALSA_HEADERS_H_
6#define NET_TOOLS_FLIP_SERVER_BALSA_HEADERS_H_
7#pragma once
8
9#include <algorithm>
10#include <iosfwd>
11#include <iterator>
12#include <string>
13#include <utility>
14#include <vector>
15
16#include "base/port.h"
17#include "base/logging.h"
18#include "base/string_piece.h"
19#include "net/tools/flip_server/balsa_enums.h"
20#include "net/tools/flip_server/string_piece_utils.h"
21
22namespace net {
23
24// WARNING:
25// Note that -no- char* returned by any function in this
26// file is null-terminated.
27
28// This class exists to service the specific needs of BalsaHeaders.
29//
30// Functional goals:
31//   1) provide a backing-store for all of the StringPieces that BalsaHeaders
32//      returns. Every StringPiece returned from BalsaHeaders should remain
33//      valid until the BalsaHeader's object is cleared, or the header-line is
34//      erased.
35//   2) provide a backing-store for BalsaFrame, which requires contiguous memory
36//      for its fast-path parsing functions. Note that the cost of copying is
37//      less than the cost of requiring the parser to do slow-path parsing, as
38//      it would have to check for bounds every byte, instead of every 16 bytes.
39//
40// This class is optimized for the case where headers are stored in one of two
41// buffers. It doesn't make a lot of effort to densely pack memory-- in fact,
42// it -may- be somewhat memory inefficient. This possible inefficiency allows a
43// certain simplicity of implementation and speed which makes it worthwhile.
44// If, in the future, better memory density is required, it should be possible
45// to reuse the abstraction presented by this object to achieve those goals.
46//
47// In the most common use-case, this memory inefficiency should be relatively
48// small.
49//
50// Alternate implementations of BalsaBuffer may include:
51//  - vector of strings, one per header line (similar to HTTPHeaders)
52//  - densely packed strings:
53//    - keep a sorted array/map of free-space linked lists or numbers.
54//      - use the entry that most closely first your needs.
55//    - at this point, perhaps just use a vector of strings, and let
56//      the allocator do the right thing.
57//
58class BalsaBuffer {
59 public:
60  static const size_t kDefaultBlocksize = 4096;
61  // We have two friends here. These exist as friends as we
62  // want to allow access to the constructors for the test
63  // class and the Balsa* classes. We put this into the
64  // header file as we want this class to be inlined into the
65  // BalsaHeaders implementation, yet be testable.
66  friend class BalsaBufferTestSpouse;
67  friend class BalsaHeaders;
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
243class 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() : headers_(NULL), idx_(0) { }
311
312    // copy constructor.
313    iterator_base(const iterator_base& it)
314      : headers_(it.headers_),
315        idx_(it.idx_) {}
316
317    reference operator*() const {
318      return Lookup(idx_);
319    }
320
321    pointer operator->() const {
322      return &(this->operator*());
323    }
324
325    bool operator==(const self& it) const {
326      return idx_ == it.idx_;
327    }
328
329    bool operator<(const self& it) const {
330      return idx_ < it.idx_;
331    }
332
333    bool operator<=(const self& it) const {
334      return idx_ <= it.idx_;
335    }
336
337    bool operator!=(const self& it) const {
338      return !(*this == it);
339    }
340
341    bool operator>(const self& it) const {
342      return it < *this;
343    }
344
345    bool operator>=(const self& it) const {
346      return it <= *this;
347    }
348
349    // This mainly exists so that we can have interesting output for
350    // unittesting. The EXPECT_EQ, EXPECT_NE functions require that
351    // operator<< work for the classes it sees.  It would be better if there
352    // was an additional traits-like system for the gUnit output... but oh
353    // well.
354    std::ostream& operator<<(std::ostream& os) const;
355
356   protected:
357    iterator_base(const BalsaHeaders* headers, HeaderLines::size_type index) :
358        headers_(headers),
359        idx_(index) {}
360
361    void increment() {
362      const HeaderLines& header_lines = headers_->header_lines_;
363      const HeaderLines::size_type header_lines_size = header_lines.size();
364      const HeaderLines::size_type original_idx = idx_;
365      do {
366        ++idx_;
367      } while (idx_ < header_lines_size && header_lines[idx_].skip == true);
368      // The condition below exists so that ++(end() - 1) == end(), even
369      // if there are only 'skip == true' elements between the end() iterator
370      // and the end of the vector of HeaderLineDescriptions.
371      // TODO(fenix): refactor this list so that we don't have to do
372      // linear scanning through skipped headers (and this condition is
373      // then unnecessary)
374      if (idx_ == header_lines_size) {
375        idx_ = original_idx + 1;
376      }
377    }
378
379    void decrement() {
380      const HeaderLines& header_lines = headers_->header_lines_;
381      const HeaderLines::size_type header_lines_size = header_lines.size();
382      const HeaderLines::size_type original_idx = idx_;
383      do {
384        --idx_;
385      } while (idx_ < header_lines_size && header_lines[idx_].skip == true);
386      // The condition below exists so that --(rbegin() + 1) == rbegin(), even
387      // if there are only 'skip == true' elements between the rbegin() iterator
388      // and the beginning of the vector of HeaderLineDescriptions.
389      // TODO(fenix): refactor this list so that we don't have to do
390      // linear scanning through skipped headers (and this condition is
391      // then unnecessary)
392      if (idx_ > header_lines_size) {
393        idx_ = original_idx - 1;
394      }
395    }
396
397    reference Lookup(HeaderLines::size_type index) const {
398      DCHECK_LT(index, headers_->header_lines_.size());
399      const HeaderLineDescription& line = headers_->header_lines_[index];
400      const char* stream_begin = headers_->GetPtr(line.buffer_base_idx);
401      value_ = value_type(
402          base::StringPiece(stream_begin + line.first_char_idx,
403                      line.key_end_idx - line.first_char_idx),
404          base::StringPiece(stream_begin + line.value_begin_idx,
405                      line.last_char_idx - line.value_begin_idx));
406      DCHECK_GE(line.key_end_idx, line.first_char_idx);
407      DCHECK_GE(line.last_char_idx, line.value_begin_idx);
408      return value_;
409    }
410
411    const BalsaHeaders* headers_;
412    HeaderLines::size_type idx_;
413    mutable StringPiecePair value_;
414  };
415
416  class reverse_iterator_base : public iterator_base {
417   public:
418    typedef reverse_iterator_base self;
419    typedef iterator_base::reference reference;
420    typedef iterator_base::pointer pointer;
421    using iterator_base::headers_;
422    using iterator_base::idx_;
423
424    reverse_iterator_base() : iterator_base() {}
425
426    // This constructor is no explicit purposely.
427    reverse_iterator_base(const iterator_base& it) :  // NOLINT
428        iterator_base(it) {
429    }
430
431    self& operator=(const iterator_base& it) {
432      idx_ = it.idx_;
433      headers_ = it.headers_;
434      return *this;
435    }
436
437    self& operator=(const reverse_iterator_base& it) {
438      idx_ = it.idx_;
439      headers_ = it.headers_;
440      return *this;
441    }
442
443    reference operator*() const {
444      return Lookup(idx_ - 1);
445    }
446
447    pointer operator->() const {
448      return &(this->operator*());
449    }
450
451    reverse_iterator_base(const reverse_iterator_base& it) :
452        iterator_base(it) { }
453
454   protected:
455    void increment() {
456      --idx_;
457      iterator_base::decrement();
458      ++idx_;
459    }
460
461    void decrement() {
462      ++idx_;
463      iterator_base::increment();
464      --idx_;
465    }
466
467    reverse_iterator_base(const BalsaHeaders* headers,
468                          HeaderLines::size_type index) :
469        iterator_base(headers, index) {}
470  };
471
472 public:
473  class const_header_lines_iterator : public iterator_base {
474    friend class BalsaHeaders;
475   public:
476    typedef const_header_lines_iterator self;
477    const_header_lines_iterator() : iterator_base() {}
478
479    const_header_lines_iterator(const const_header_lines_iterator& it) :
480        iterator_base(it.headers_, it.idx_) {}
481
482    self& operator++() {
483      iterator_base::increment();
484      return *this;
485    }
486
487    self& operator--() {
488      iterator_base::decrement();
489      return *this;
490    }
491   protected:
492    const_header_lines_iterator(const BalsaHeaders* headers,
493                                HeaderLines::size_type index) :
494        iterator_base(headers, index) {}
495  };
496
497  class const_reverse_header_lines_iterator : public reverse_iterator_base {
498   public:
499    typedef const_reverse_header_lines_iterator self;
500    const_reverse_header_lines_iterator() : reverse_iterator_base() {}
501
502    const_reverse_header_lines_iterator(
503      const const_header_lines_iterator& it) :
504        reverse_iterator_base(it.headers_, it.idx_) {}
505
506    const_reverse_header_lines_iterator(
507      const const_reverse_header_lines_iterator& it) :
508        reverse_iterator_base(it.headers_, it.idx_) {}
509
510    const_header_lines_iterator base() {
511      return const_header_lines_iterator(headers_, idx_);
512    }
513
514    self& operator++() {
515      reverse_iterator_base::increment();
516      return *this;
517    }
518
519    self& operator--() {
520      reverse_iterator_base::decrement();
521      return *this;
522    }
523   protected:
524    const_reverse_header_lines_iterator(const BalsaHeaders* headers,
525                                        HeaderLines::size_type index) :
526        reverse_iterator_base(headers, index) {}
527
528    friend class BalsaHeaders;
529  };
530
531  // An iterator that only stops at lines with a particular key.
532  // See also GetIteratorForKey.
533  //
534  // Check against header_lines_key_end() to determine when iteration is
535  // finished. header_lines_end() will also work.
536  class const_header_lines_key_iterator : public iterator_base {
537    friend class BalsaHeaders;
538   public:
539    typedef const_header_lines_key_iterator self;
540
541    self& operator++() {
542      do {
543        iterator_base::increment();
544      } while (!AtEnd() &&
545               !StringPieceUtils::EqualIgnoreCase(key_, (**this).first));
546      return *this;
547    }
548
549    void operator++(int ignore) {
550      ++(*this);
551    }
552
553    // Only forward-iteration makes sense, so no operator-- defined.
554
555   private:
556    const_header_lines_key_iterator(const BalsaHeaders* headers,
557                                    HeaderLines::size_type index,
558                                    const base::StringPiece& key)
559        : iterator_base(headers, index),
560          key_(key) {
561    }
562
563    // Should only be used for creating an end iterator.
564    const_header_lines_key_iterator(const BalsaHeaders* headers,
565                                    HeaderLines::size_type index)
566        : iterator_base(headers, index) {
567    }
568
569    bool AtEnd() const {
570      return *this >= headers_->header_lines_end();
571    }
572
573    base::StringPiece key_;
574  };
575
576  // TODO(fenix): Revisit the amount of bytes initially allocated to the second
577  // block of the balsa_buffer_. It may make sense to pre-allocate some amount
578  // (roughly the amount we'd append in new headers such as X-User-Ip, etc.)
579  BalsaHeaders();
580  ~BalsaHeaders();
581
582  const_header_lines_iterator header_lines_begin() {
583    return HeaderLinesBeginHelper<const_header_lines_iterator>();
584  }
585
586  const_header_lines_iterator header_lines_begin() const {
587    return HeaderLinesBeginHelper<const_header_lines_iterator>();
588  }
589
590  const_header_lines_iterator header_lines_end() {
591    return HeaderLinesEndHelper<const_header_lines_iterator>();
592  }
593
594  const_header_lines_iterator header_lines_end() const {
595    return HeaderLinesEndHelper<const_header_lines_iterator>();
596  }
597
598  const_reverse_header_lines_iterator header_lines_rbegin() {
599    return const_reverse_header_lines_iterator(header_lines_end());
600  }
601
602  const_reverse_header_lines_iterator header_lines_rbegin() const {
603    return const_reverse_header_lines_iterator(header_lines_end());
604  }
605
606  const_reverse_header_lines_iterator header_lines_rend() {
607    return const_reverse_header_lines_iterator(header_lines_begin());
608  }
609
610  const_reverse_header_lines_iterator header_lines_rend() const {
611    return const_reverse_header_lines_iterator(header_lines_begin());
612  }
613
614  const_header_lines_key_iterator header_lines_key_end() const {
615    return HeaderLinesEndHelper<const_header_lines_key_iterator>();
616  }
617
618  void erase(const const_header_lines_iterator& it) {
619    DCHECK_EQ(it.headers_, this);
620    DCHECK_LT(it.idx_, header_lines_.size());
621    DCHECK_GE(it.idx_, 0u);
622    header_lines_[it.idx_].skip = true;
623  }
624
625  void Clear();
626
627  void Swap(BalsaHeaders* other);
628
629  void CopyFrom(const BalsaHeaders& other);
630
631  void HackHeader(const base::StringPiece& key, const base::StringPiece& value);
632
633  // Same as AppendToHeader, except that it will attempt to preserve
634  // header ordering.
635  // Note that this will always append to an existing header, if available,
636  // without moving the header around, or collapsing multiple header lines
637  // with the same key together. For this reason, it only 'attempts' to
638  // preserve header ordering.
639  // TODO(fenix): remove this function and rename all occurances
640  // of it in the code to AppendToHeader when the condition above
641  // has been satisified.
642  void HackAppendToHeader(const base::StringPiece& key,
643                          const base::StringPiece& value);
644
645  // Replaces header entries with key 'key' if they exist, or appends
646  // a new header if none exist.  See 'AppendHeader' below for additional
647  // comments about ContentLength and TransferEncoding headers. Note that this
648  // will allocate new storage every time that it is called.
649  // TODO(fenix): modify this function to reuse existing storage
650  // if it is available.
651  void ReplaceOrAppendHeader(const base::StringPiece& key,
652                             const base::StringPiece& value);
653
654  // Append a new header entry to the header object. Clients who wish to append
655  // Content-Length header should use SetContentLength() method instead of
656  // adding the content length header using AppendHeader (manually adding the
657  // content length header will not update the content_length_ and
658  // content_length_status_ values).
659  // Similarly, clients who wish to add or remove the transfer encoding header
660  // in order to apply or remove chunked encoding should use SetChunkEncoding()
661  // instead.
662  void AppendHeader(const base::StringPiece& key,
663                    const base::StringPiece& value);
664
665  // Appends ',value' to an existing header named 'key'.  If no header with the
666  // correct key exists, it will call AppendHeader(key, value).  Calling this
667  // function on a key which exists several times in the headers will produce
668  // unpredictable results.
669  void AppendToHeader(const base::StringPiece& key,
670                      const base::StringPiece& value);
671
672  // Prepends 'value,' to an existing header named 'key'.  If no header with the
673  // correct key exists, it will call AppendHeader(key, value).  Calling this
674  // function on a key which exists several times in the headers will produce
675  // unpredictable results.
676  void PrependToHeader(const base::StringPiece& key,
677                       const base::StringPiece& value);
678
679  const base::StringPiece GetHeader(const base::StringPiece& key) const;
680
681  // Iterates over all currently valid header lines, appending their
682  // values into the vector 'out', in top-to-bottom order.
683  // Header-lines which have been erased are not currently valid, and
684  // will not have their values appended. Empty values will be
685  // represented as empty string. If 'key' doesn't exist in the headers at
686  // all, out will not be changed. We do not clear the vector out
687  // before adding new entries. If there are header lines with matching
688  // key but empty value then they are also added to the vector out.
689  // (Basically empty values are not treated in any special manner).
690  //
691  // Example:
692  // Input header:
693  // "GET / HTTP/1.0\r\n"
694  //    "key1: v1\r\n"
695  //    "key1: \r\n"
696  //    "key1:\r\n"
697  //    "key1:  v1\r\n"
698  //    "key1:v2\r\n"
699  //
700  //  vector out is initially: ["foo"]
701  //  vector out after GetAllOfHeader("key1", &out) is:
702  // ["foo", "v1", "", "", "v2", "v1", "v2"]
703
704  void GetAllOfHeader(const base::StringPiece& key,
705                      std::vector<base::StringPiece>* out) const;
706
707  // Joins all values for key into a comma-separated string in out.
708  // More efficient than calling JoinStrings on result of GetAllOfHeader if
709  // you don't need the intermediate vector<StringPiece>.
710  void GetAllOfHeaderAsString(const base::StringPiece& key,
711                              std::string* out) const;
712
713  // Returns true if RFC 2616 Section 14 indicates that header can
714  // have multiple values.
715  static bool IsMultivaluedHeader(const base::StringPiece& header);
716
717  // Determine if a given header is present.
718  inline bool HasHeader(const base::StringPiece& key) const {
719    return (GetConstHeaderLinesIterator(key, header_lines_.begin()) !=
720            header_lines_.end());
721  }
722
723  // Returns true iff any header 'key' exists with non-empty value.
724  bool HasNonEmptyHeader(const base::StringPiece& key) const;
725
726  const_header_lines_iterator GetHeaderPosition(
727      const base::StringPiece& key) const;
728
729  // Returns a forward-only iterator that only stops at lines matching key.
730  // String backing 'key' must remain valid for lifetime of iterator.
731  //
732  // Check returned iterator against header_lines_key_end() to determine when
733  // iteration is finished.
734  const_header_lines_key_iterator GetIteratorForKey(
735      const base::StringPiece& key) const;
736
737  void RemoveAllOfHeader(const base::StringPiece& key);
738
739  // Removes all headers starting with 'key' [case insensitive]
740  void RemoveAllHeadersWithPrefix(const base::StringPiece& key);
741
742  // Returns the lower bound of memory  used by this header object, including
743  // all internal buffers and data structure. Some of the memory used cannot be
744  // directly measure. For example, memory used for bookkeeping by standard
745  // containers.
746  size_t GetMemoryUsedLowerBound() const;
747
748  // Returns the upper bound on the required buffer space to fully write out
749  // the header object (this include the first line, all header lines, and the
750  // final CRLF that marks the ending of the header).
751  size_t GetSizeForWriteBuffer() const;
752
753  // The following WriteHeader* methods are template member functions that
754  // place one requirement on the Buffer class: it must implement a Write
755  // method that takes a pointer and a length. The buffer passed in is not
756  // required to be stretchable. For non-stretchable buffers, the user must
757  // call GetSizeForWriteBuffer() to find out the upper bound on the output
758  // buffer space required to make sure that the entire header is serialized.
759  // BalsaHeaders will not check that there is adequate space in the buffer
760  // object during the write.
761
762  // Writes the entire header and the final CRLF that marks the end of the HTTP
763  // header section to the buffer. After this method returns, no more header
764  // data should be written to the buffer.
765  template <typename Buffer>
766  void WriteHeaderAndEndingToBuffer(Buffer* buffer) const {
767    WriteToBuffer(buffer);
768    WriteHeaderEndingToBuffer(buffer);
769  }
770
771  // Writes the final CRLF to the buffer to terminate the HTTP header section.
772  // After this method returns, no more header data should be written to the
773  // buffer.
774  template <typename Buffer>
775  static void WriteHeaderEndingToBuffer(Buffer* buffer) {
776    buffer->Write("\r\n", 2);
777  }
778
779  // Writes the entire header to the buffer without the CRLF that terminates
780  // the HTTP header. This lets users append additional header lines using
781  // WriteHeaderLineToBuffer and then terminate the header with
782  // WriteHeaderEndingToBuffer as the header is serialized to the
783  // buffer, without having to first copy the header.
784  template <typename Buffer>
785  void WriteToBuffer(Buffer* buffer) const {
786    // write the first line.
787    const size_t firstline_len = whitespace_4_idx_ - non_whitespace_1_idx_;
788    const char* stream_begin = GetPtr(firstline_buffer_base_idx_);
789    buffer->Write(stream_begin + non_whitespace_1_idx_, firstline_len);
790    buffer->Write("\r\n", 2);
791    const HeaderLines::size_type end = header_lines_.size();
792    for (HeaderLines::size_type i = 0; i < end; ++i) {
793      const HeaderLineDescription& line = header_lines_[i];
794      if (line.skip) {
795        continue;
796      }
797      const char* line_ptr = GetPtr(line.buffer_base_idx);
798      WriteHeaderLineToBuffer(
799          buffer,
800          base::StringPiece(line_ptr + line.first_char_idx,
801                      line.key_end_idx - line.first_char_idx),
802          base::StringPiece(line_ptr + line.value_begin_idx,
803                      line.last_char_idx - line.value_begin_idx));
804    }
805  }
806
807  // Takes a header line in the form of a key/value pair and append it to the
808  // buffer. This function should be called after WriteToBuffer to
809  // append additional header lines to the header without copying the header.
810  // When the user is done with appending to the buffer,
811  // WriteHeaderEndingToBuffer must be used to terminate the HTTP
812  // header in the buffer. This method is a no-op if key is empty.
813  template <typename Buffer>
814  static void WriteHeaderLineToBuffer(Buffer* buffer,
815                                      const base::StringPiece& key,
816                                      const base::StringPiece& value) {
817    // if the key is empty, we don't want to write the rest because it
818    // will not be a well-formed header line.
819    if (!key.empty()) {
820      buffer->Write(key.data(), key.size());
821      buffer->Write(": ", 2);
822      buffer->Write(value.data(), value.size());
823      buffer->Write("\r\n", 2);
824    }
825  }
826
827  // Dump the textural representation of the header object to a string, which
828  // is suitable for writing out to logs. All CRLF will be printed out as \n.
829  // This function can be called on a header object in any state. Raw header
830  // data will be printed out if the header object is not completely parsed,
831  // e.g., when there was an error in the middle of parsing.
832  // The header content is appended to the string; the original content is not
833  // cleared.
834  void DumpToString(std::string* str) const;
835
836  const base::StringPiece first_line() const {
837    DCHECK_GE(whitespace_4_idx_, non_whitespace_1_idx_);
838    return base::StringPiece(BeginningOfFirstLine() + non_whitespace_1_idx_,
839                       whitespace_4_idx_ - non_whitespace_1_idx_);
840  }
841
842  // Returns the parsed value of the response code if it has been parsed.
843  // Guaranteed to return 0 when unparsed (though it is a much better idea to
844  // verify that the BalsaFrame had no errors while parsing).
845  // This may return response codes which are outside the normal bounds of
846  // HTTP response codes-- it is up to the user of this class to ensure that
847  // the response code is one which is interpretable.
848  size_t parsed_response_code() const { return parsed_response_code_; }
849
850  const base::StringPiece request_method() const {
851    DCHECK_GE(whitespace_2_idx_, non_whitespace_1_idx_);
852    return base::StringPiece(BeginningOfFirstLine() + non_whitespace_1_idx_,
853                       whitespace_2_idx_ - non_whitespace_1_idx_);
854  }
855
856  const base::StringPiece response_version() const {
857    // Note: There is no difference between request_method() and
858    // response_version(). They both could be called
859    // GetFirstTokenFromFirstline()... but that wouldn't be anywhere near as
860    // descriptive.
861    return request_method();
862  }
863
864  const base::StringPiece request_uri() const {
865    DCHECK_GE(whitespace_3_idx_, non_whitespace_2_idx_);
866    return base::StringPiece(BeginningOfFirstLine() + non_whitespace_2_idx_,
867                       whitespace_3_idx_ - non_whitespace_2_idx_);
868  }
869
870  const base::StringPiece response_code() const {
871    // Note: There is no difference between request_uri() and response_code().
872    // They both could be called GetSecondtTokenFromFirstline(), but, as noted
873    // in an earlier comment, that wouldn't be as descriptive.
874    return request_uri();
875  }
876
877  const base::StringPiece request_version() const {
878    DCHECK_GE(whitespace_4_idx_, non_whitespace_3_idx_);
879    return base::StringPiece(BeginningOfFirstLine() + non_whitespace_3_idx_,
880                       whitespace_4_idx_ - non_whitespace_3_idx_);
881  }
882
883  const base::StringPiece response_reason_phrase() const {
884    // Note: There is no difference between request_version() and
885    // response_reason_phrase(). They both could be called
886    // GetThirdTokenFromFirstline(), but, as noted in an earlier comment, that
887    // wouldn't be as descriptive.
888    return request_version();
889  }
890
891  // Note that SetFirstLine will not update the internal indices for the
892  // various bits of the first-line (and may set them all to zero).
893  // If you'd like to use the accessors for the various bits of the firstline,
894  // then you should use the Set* functions, or SetFirstlineFromStringPieces,
895  // below, instead.
896  //
897  void SetFirstlineFromStringPieces(const base::StringPiece& firstline_a,
898                                    const base::StringPiece& firstline_b,
899                                    const base::StringPiece& firstline_c);
900
901  void SetRequestFirstlineFromStringPieces(const base::StringPiece& method,
902                                           const base::StringPiece& uri,
903                                           const base::StringPiece& version) {
904    SetFirstlineFromStringPieces(method, uri, version);
905  }
906
907  void SetResponseFirstlineFromStringPieces(
908      const base::StringPiece& version,
909      const base::StringPiece& code,
910      const base::StringPiece& reason_phrase) {
911    SetFirstlineFromStringPieces(version, code, reason_phrase);
912  }
913
914  // These functions are exactly the same, except that their names are
915  // different. This is done so that the code using this class is more
916  // expressive.
917  void SetRequestMethod(const base::StringPiece& method);
918  void SetResponseVersion(const base::StringPiece& version);
919
920  void SetRequestUri(const base::StringPiece& uri);
921  void SetResponseCode(const base::StringPiece& code);
922  void set_parsed_response_code(size_t parsed_response_code) {
923    parsed_response_code_ = parsed_response_code;
924  }
925  void SetParsedResponseCodeAndUpdateFirstline(size_t parsed_response_code);
926
927  // These functions are exactly the same, except that their names are
928  // different. This is done so that the code using this class is more
929  // expressive.
930  void SetRequestVersion(const base::StringPiece& version);
931  void SetResponseReasonPhrase(const base::StringPiece& reason_phrase);
932
933  // The biggest problem with SetFirstLine is that we don't want to use a
934  // separate buffer for it.  The second biggest problem with it is that the
935  // first biggest problem requires that we store offsets into a buffer instead
936  // of pointers into a buffer. Cuteness aside, SetFirstLine doesn't parse
937  // the individual fields of the firstline, and so accessors to those fields
938  // will not work properly after calling SetFirstLine. If you want those
939  // accessors to work, use the Set* functions above this one.
940  // SetFirstLine is stuff useful, however, if all you care about is correct
941  // serialization with the rest of the header object.
942  void SetFirstLine(const base::StringPiece& line);
943
944  // Simple accessors to some of the internal state
945  bool transfer_encoding_is_chunked() const {
946    return transfer_encoding_is_chunked_;
947  }
948
949  static bool ResponseCodeImpliesNoBody(int code) {
950    // From HTTP spec section 6.1.1 all 1xx responses must not have a body,
951    // as well as 204 No Content and 304 Not Modified.
952    return ((code >= 100) && (code <= 199)) || (code == 204) || (code == 304);
953  }
954
955  // Note: never check this for requests. Nothing bad will happen if you do,
956  // but spec does not allow requests framed by connection close.
957  // TODO(vitaliyl): refactor.
958  bool is_framed_by_connection_close() const {
959    // We declare that response is framed by connection close if it has no
960    // content-length, no transfer encoding, and is allowed to have a body by
961    // the HTTP spec.
962    // parsed_response_code_ is 0 for requests, so ResponseCodeImpliesNoBody
963    // will return false.
964    return (content_length_status_ == BalsaHeadersEnums::NO_CONTENT_LENGTH) &&
965        !transfer_encoding_is_chunked_ &&
966        !ResponseCodeImpliesNoBody(parsed_response_code_);
967  }
968
969  size_t content_length() const { return content_length_; }
970  BalsaHeadersEnums::ContentLengthStatus content_length_status() const {
971    return content_length_status_;
972  }
973
974  // SetContentLength and SetChunkEncoding modifies the header object to use
975  // content-length and transfer-encoding headers in a consistent manner. They
976  // set all internal flags and status so client can get a consistent view from
977  // various accessors.
978  void SetContentLength(size_t length);
979  void SetChunkEncoding(bool chunk_encode);
980
981 protected:
982  friend class BalsaFrame;
983  friend class SpdyFrame;
984  friend class HTTPMessage;
985  friend class BalsaHeadersTokenUtils;
986
987  const char* BeginningOfFirstLine() const {
988    return GetPtr(firstline_buffer_base_idx_);
989  }
990
991  char* GetPtr(BalsaBuffer::Blocks::size_type block_idx) {
992    return balsa_buffer_.GetPtr(block_idx);
993  }
994
995  const char* GetPtr(BalsaBuffer::Blocks::size_type block_idx) const {
996    return balsa_buffer_.GetPtr(block_idx);
997  }
998
999  void WriteFromFramer(const char* ptr, size_t size) {
1000    balsa_buffer_.WriteToContiguousBuffer(base::StringPiece(ptr, size));
1001  }
1002
1003  void DoneWritingFromFramer() {
1004    balsa_buffer_.NoMoreWriteToContiguousBuffer();
1005  }
1006
1007  const char* OriginalHeaderStreamBegin() const {
1008    return balsa_buffer_.StartOfFirstBlock();
1009  }
1010
1011  const char* OriginalHeaderStreamEnd() const {
1012    return balsa_buffer_.EndOfFirstBlock();
1013  }
1014
1015  size_t GetReadableBytesFromHeaderStream() const {
1016    return OriginalHeaderStreamEnd() - OriginalHeaderStreamBegin();
1017  }
1018
1019  void GetReadablePtrFromHeaderStream(const char** p, size_t* s) {
1020    *p = OriginalHeaderStreamBegin();
1021    *s = GetReadableBytesFromHeaderStream();
1022  }
1023
1024  base::StringPiece GetValueFromHeaderLineDescription(
1025      const HeaderLineDescription& line) const;
1026
1027  void AddAndMakeDescription(const base::StringPiece& key,
1028                             const base::StringPiece& value,
1029                             HeaderLineDescription* d);
1030
1031  void AppendOrPrependAndMakeDescription(const base::StringPiece& key,
1032                                         const base::StringPiece& value,
1033                                         bool append,
1034                                         HeaderLineDescription* d);
1035
1036  // Removes all header lines with the given key starting at start.
1037  void RemoveAllOfHeaderStartingAt(const base::StringPiece& key,
1038                                   HeaderLines::iterator start);
1039
1040  // If the 'key' does not exist in the headers, calls
1041  // AppendHeader(key, value).  Otherwise if append is true, appends ',value'
1042  // to the first existing header with key 'key'.  If append is false, prepends
1043  // 'value,' to the first existing header with key 'key'.
1044  void AppendOrPrependToHeader(const base::StringPiece& key,
1045                               const base::StringPiece& value,
1046                               bool append);
1047
1048  HeaderLines::const_iterator GetConstHeaderLinesIterator(
1049      const base::StringPiece& key,
1050      HeaderLines::const_iterator start) const;
1051
1052  HeaderLines::iterator GetHeaderLinesIteratorNoSkip(
1053      const base::StringPiece& key,
1054      HeaderLines::iterator start);
1055
1056  HeaderLines::iterator GetHeaderLinesIterator(
1057      const base::StringPiece& key,
1058      HeaderLines::iterator start);
1059
1060  template <typename IteratorType>
1061  const IteratorType HeaderLinesBeginHelper() const {
1062    if (header_lines_.empty()) {
1063      return IteratorType(this, 0);
1064    }
1065    const HeaderLines::size_type header_lines_size = header_lines_.size();
1066    for (HeaderLines::size_type i = 0; i < header_lines_size; ++i) {
1067      if (header_lines_[i].skip == false) {
1068        return IteratorType(this, i);
1069      }
1070    }
1071    return IteratorType(this, 0);
1072  }
1073
1074  template <typename IteratorType>
1075  const IteratorType HeaderLinesEndHelper() const {
1076    if (header_lines_.empty()) {
1077      return IteratorType(this, 0);
1078    }
1079    const HeaderLines::size_type header_lines_size = header_lines_.size();
1080    HeaderLines::size_type i = header_lines_size;
1081    do {
1082      --i;
1083      if (header_lines_[i].skip == false) {
1084        return IteratorType(this, i + 1);
1085      }
1086    } while (i != 0);
1087    return IteratorType(this, 0);
1088  }
1089
1090  // At the moment, this function will always return the original headers.
1091  // In the future, it may not do so after erasing header lines, modifying
1092  // header lines, or modifying the first line.
1093  // For this reason, it is strongly suggested that use of this function is
1094  // only acceptable for the purpose of debugging parse errors seen by the
1095  // BalsaFrame class.
1096  base::StringPiece OriginalHeadersForDebugging() const {
1097    return base::StringPiece(OriginalHeaderStreamBegin(),
1098                       OriginalHeaderStreamEnd() - OriginalHeaderStreamBegin());
1099  }
1100
1101  BalsaBuffer balsa_buffer_;
1102
1103  size_t content_length_;
1104  BalsaHeadersEnums::ContentLengthStatus content_length_status_;
1105  size_t parsed_response_code_;
1106  // HTTP firstlines all have the following structure:
1107  //  LWS         NONWS  LWS    NONWS   LWS    NONWS   NOTCRLF  CRLF
1108  //  [\t \r\n]+ [^\t ]+ [\t ]+ [^\t ]+ [\t ]+ [^\t ]+ [^\r\n]+ "\r\n"
1109  //  ws1        nws1    ws2    nws2    ws3    nws3             ws4
1110  //  |          [-------)      [-------)      [----------------)
1111  //    REQ:     method         request_uri    version
1112  //   RESP:     version        statuscode     reason
1113  //
1114  //   The first NONWS->LWS component we'll call firstline_a.
1115  //   The second firstline_b, and the third firstline_c.
1116  //
1117  //   firstline_a goes from nws1 to (but not including) ws2
1118  //   firstline_b goes from nws2 to (but not including) ws3
1119  //   firstline_c goes from nws3 to (but not including) ws4
1120  //
1121  // In the code:
1122  //    ws1 == whitespace_1_idx_
1123  //   nws1 == non_whitespace_1_idx_
1124  //    ws2 == whitespace_2_idx_
1125  //   nws2 == non_whitespace_2_idx_
1126  //    ws3 == whitespace_3_idx_
1127  //   nws3 == non_whitespace_3_idx_
1128  //    ws4 == whitespace_4_idx_
1129  BalsaBuffer::Blocks::size_type firstline_buffer_base_idx_;
1130  size_t whitespace_1_idx_;
1131  size_t non_whitespace_1_idx_;
1132  size_t whitespace_2_idx_;
1133  size_t non_whitespace_2_idx_;
1134  size_t whitespace_3_idx_;
1135  size_t non_whitespace_3_idx_;
1136  size_t whitespace_4_idx_;
1137  size_t end_of_firstline_idx_;
1138
1139  bool transfer_encoding_is_chunked_;
1140
1141  HeaderLines header_lines_;
1142};
1143
1144}  // namespace net
1145
1146#endif  // NET_TOOLS_FLIP_SERVER_BALSA_HEADERS_H_
1147