Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(970)

Side by Side Diff: net/tools/balsa/balsa_headers.h

Issue 2477703002: Remove now unused Balsa code. (Closed)
Patch Set: Rebase Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/tools/balsa/balsa_frame_test.cc ('k') | net/tools/balsa/balsa_headers.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 <stddef.h>
9
10 #include <algorithm>
11 #include <iosfwd>
12 #include <iterator>
13 #include <string>
14 #include <utility>
15 #include <vector>
16
17 #include "base/logging.h"
18 #include "base/strings/string_piece.h"
19 #include "net/tools/balsa/balsa_enums.h"
20 #include "net/tools/balsa/string_piece_utils.h"
21
22 namespace 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 //
58 class 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 friend class BalsaBufferTest;
69
70 // The BufferBlock is a structure used internally by the
71 // BalsaBuffer class to store the base buffer pointers to
72 // each block, as well as the important metadata for buffer
73 // sizes and bytes free.
74 struct BufferBlock {
75 public:
76 char* buffer;
77 size_t buffer_size;
78 size_t bytes_free;
79
80 size_t bytes_used() const {
81 return buffer_size - bytes_free;
82 }
83 char* start_of_unused_bytes() const {
84 return buffer + bytes_used();
85 }
86
87 BufferBlock() : buffer(NULL), buffer_size(0), bytes_free(0) {}
88 ~BufferBlock() {}
89
90 BufferBlock(char* buf, size_t size, size_t free) :
91 buffer(buf), buffer_size(size), bytes_free(free) {}
92 // Yes we want this to be copyable (it gets stuck into vectors).
93 // For this reason, we don't use scoped ptrs, etc. here-- it
94 // is more efficient to manage this memory externally to this
95 // object.
96 };
97
98 typedef std::vector<BufferBlock> Blocks;
99
100 ~BalsaBuffer();
101
102 // Returns the total amount of memory used by the buffer blocks.
103 size_t GetTotalBufferBlockSize() const;
104
105 const char* GetPtr(Blocks::size_type block_idx) const {
106 DCHECK_LT(block_idx, blocks_.size())
107 << block_idx << ", " << blocks_.size();
108 return blocks_[block_idx].buffer;
109 }
110
111 char* GetPtr(Blocks::size_type block_idx) {
112 DCHECK_LT(block_idx, blocks_.size())
113 << block_idx << ", " << blocks_.size();
114 return blocks_[block_idx].buffer;
115 }
116
117 // This function is different from Write(), as it ensures that the data
118 // stored via subsequent calls to this function are all contiguous (and in
119 // the order in which these writes happened). This is essentially the same
120 // as a string append.
121 //
122 // You may call this function at any time between object
123 // construction/Clear(), and the calling of the
124 // NoMoreWriteToContiguousBuffer() function.
125 //
126 // You must not call this function after the NoMoreWriteToContiguousBuffer()
127 // function is called, unless a Clear() has been called since.
128 // If you do, the program will abort().
129 //
130 // This condition is placed upon this code so that calls to Write() can
131 // append to the buffer in the first block safely, and without invaliding
132 // the StringPiece which it returns.
133 //
134 // This function's main intended user is the BalsaFrame class, which,
135 // for reasons of efficiency, requires that the buffer from which it parses
136 // the headers be contiguous.
137 //
138 void WriteToContiguousBuffer(const base::StringPiece& sp);
139
140 void NoMoreWriteToContiguousBuffer() {
141 can_write_to_contiguous_buffer_ = false;
142 }
143
144 // Takes a StringPiece and writes it to "permanent" storage, then returns a
145 // StringPiece which points to that data. If block_idx != NULL, it will be
146 // assigned the index of the block into which the data was stored.
147 // Note that the 'permanent' storage in which it stores data may be in
148 // the first block IFF the NoMoreWriteToContiguousBuffer function has
149 // been called since the last Clear/Construction.
150 base::StringPiece Write(const base::StringPiece& sp,
151 Blocks::size_type* block_buffer_idx);
152
153 // Reserves "permanent" storage of the size indicated. Returns a pointer to
154 // the beginning of that storage, and assigns the index of the block used to
155 // block_buffer_idx. This function uses the first block IFF the
156 // NoMoreWriteToContiguousBuffer function has been called since the last
157 // Clear/Construction.
158 char* Reserve(size_t size, Blocks::size_type* block_buffer_idx);
159
160 void Clear();
161
162 void Swap(BalsaBuffer* b);
163
164 void CopyFrom(const BalsaBuffer& b);
165
166 const char* StartOfFirstBlock() const {
167 return blocks_[0].buffer;
168 }
169
170 const char* EndOfFirstBlock() const {
171 return blocks_[0].buffer + blocks_[0].bytes_used();
172 }
173
174 bool can_write_to_contiguous_buffer() const {
175 return can_write_to_contiguous_buffer_;
176 }
177 size_t blocksize() const { return blocksize_; }
178 Blocks::size_type num_blocks() const { return blocks_.size(); }
179 size_t buffer_size(size_t idx) const { return blocks_[idx].buffer_size; }
180 size_t bytes_used(size_t idx) const { return blocks_[idx].bytes_used(); }
181
182 protected:
183 BalsaBuffer();
184
185 explicit BalsaBuffer(size_t blocksize);
186
187 BufferBlock AllocBlock();
188
189 BufferBlock AllocCustomBlock(size_t blocksize);
190
191 BufferBlock CopyBlock(const BufferBlock& b);
192
193 // Cleans up the object.
194 // The block at start_idx, and all subsequent blocks
195 // will be cleared and have associated memory deleted.
196 void CleanupBlocksStartingFrom(Blocks::size_type start_idx);
197
198 // A container of BufferBlocks
199 Blocks blocks_;
200
201 // The default allocation size for a block.
202 // In general, blocksize_ bytes will be allocated for
203 // each buffer.
204 size_t blocksize_;
205
206 // If set to true, then the first block cannot be used for Write() calls as
207 // the WriteToContiguous... function will modify the base pointer for this
208 // block, and the Write() calls need to be sure that the base pointer will
209 // not be changing in order to provide the user with StringPieces which
210 // continue to be valid.
211 bool can_write_to_contiguous_buffer_;
212 };
213
214 ////////////////////////////////////////////////////////////////////////////////
215
216 // All of the functions in the BalsaHeaders class use string pieces, by either
217 // using the StringPiece class, or giving an explicit size and char* (as these
218 // are the native representation for these string pieces).
219 // This is done for several reasons.
220 // 1) This minimizes copying/allocation/deallocation as compared to using
221 // string parameters
222 // 2) This reduces the number of strlen() calls done (as the length of any
223 // string passed in is relatively likely to be known at compile time, and for
224 // those strings passed back we obviate the need for a strlen() to determine
225 // the size of new storage allocations if a new allocation is required.
226 // 3) This class attempts to store all of its data in two linear buffers in
227 // order to enhance the speed of parsing and writing out to a buffer. As a
228 // result, many string pieces are -not- terminated by '\0', and are not
229 // c-strings. Since this is the case, we must delineate the length of the
230 // string explicitly via a length.
231 //
232 // WARNING: The side effect of using StringPiece is that if the underlying
233 // buffer changes (due to modifying the headers) the StringPieces which point
234 // to the data which was modified, may now contain "garbage", and should not
235 // be dereferenced.
236 // For example, If you fetch some component of the first-line, (request or
237 // response), and then you modify the first line, the StringPieces you
238 // originally received from the original first-line may no longer be valid).
239 //
240 // StringPieces pointing to pieces of header lines which have not been
241 // erased() or modified should be valid until the object is cleared or
242 // destroyed.
243
244 class BalsaHeaders {
245 public:
246 struct HeaderLineDescription {
247 HeaderLineDescription(size_t first_character_index,
248 size_t key_end_index,
249 size_t value_begin_index,
250 size_t last_character_index,
251 size_t buffer_base_index) :
252 first_char_idx(first_character_index),
253 key_end_idx(key_end_index),
254 value_begin_idx(value_begin_index),
255 last_char_idx(last_character_index),
256 buffer_base_idx(buffer_base_index),
257 skip(false) {}
258
259 HeaderLineDescription() :
260 first_char_idx(0),
261 key_end_idx(0),
262 value_begin_idx(0),
263 last_char_idx(0),
264 buffer_base_idx(0),
265 skip(false) {}
266
267 size_t first_char_idx;
268 size_t key_end_idx;
269 size_t value_begin_idx;
270 size_t last_char_idx;
271 BalsaBuffer::Blocks::size_type buffer_base_idx;
272 bool skip;
273 };
274
275 typedef std::vector<base::StringPiece> HeaderTokenList;
276 friend bool ParseHTTPFirstLine(const char* begin,
277 const char* end,
278 bool is_request,
279 size_t max_request_uri_length,
280 BalsaHeaders* headers,
281 BalsaFrameEnums::ErrorCode* error_code);
282
283 protected:
284 typedef std::vector<HeaderLineDescription> HeaderLines;
285
286 // Why these base classes (iterator_base, reverse_iterator_base)? Well, if
287 // we do want to export both iterator and const_iterator types (currently we
288 // only have const_iterator), then this is useful to avoid code duplication.
289 // Additionally, having this base class makes comparisons of iterators of
290 // different types (they're different types to ensure that operator= and
291 // constructors do not work in the places where they're expected to not work)
292 // work properly. There could be as many as 4 iterator types, all based on
293 // the same data as iterator_base... so it makes sense to simply have some
294 // base classes.
295
296 class iterator_base {
297 public:
298 friend class BalsaHeaders;
299 friend class reverse_iterator_base;
300 typedef std::pair<base::StringPiece, base::StringPiece> StringPiecePair;
301 typedef StringPiecePair value_type;
302 typedef value_type& reference;
303 typedef value_type* pointer;
304
305 typedef std::forward_iterator_tag iterator_category;
306 typedef ptrdiff_t difference_type;
307
308 typedef iterator_base self;
309
310 // default constructor.
311 iterator_base();
312
313 // copy constructor.
314 iterator_base(const iterator_base& it);
315
316 reference operator*() const {
317 return Lookup(idx_);
318 }
319
320 pointer operator->() const {
321 return &(this->operator*());
322 }
323
324 bool operator==(const self& it) const {
325 return idx_ == it.idx_;
326 }
327
328 bool operator<(const self& it) const {
329 return idx_ < it.idx_;
330 }
331
332 bool operator<=(const self& it) const {
333 return idx_ <= it.idx_;
334 }
335
336 bool operator!=(const self& it) const {
337 return !(*this == it);
338 }
339
340 bool operator>(const self& it) const {
341 return it < *this;
342 }
343
344 bool operator>=(const self& it) const {
345 return it <= *this;
346 }
347
348 // This mainly exists so that we can have interesting output for
349 // unittesting. The EXPECT_EQ, EXPECT_NE functions require that
350 // operator<< work for the classes it sees. It would be better if there
351 // was an additional traits-like system for the gUnit output... but oh
352 // well.
353 std::ostream& operator<<(std::ostream& os) const;
354
355 protected:
356 iterator_base(const BalsaHeaders* headers, HeaderLines::size_type index);
357
358 void increment() {
359 const HeaderLines& header_lines = headers_->header_lines_;
360 const HeaderLines::size_type header_lines_size = header_lines.size();
361 const HeaderLines::size_type original_idx = idx_;
362 do {
363 ++idx_;
364 } while (idx_ < header_lines_size && header_lines[idx_].skip == true);
365 // The condition below exists so that ++(end() - 1) == end(), even
366 // if there are only 'skip == true' elements between the end() iterator
367 // and the end of the vector of HeaderLineDescriptions.
368 // TODO(fenix): refactor this list so that we don't have to do
369 // linear scanning through skipped headers (and this condition is
370 // then unnecessary)
371 if (idx_ == header_lines_size) {
372 idx_ = original_idx + 1;
373 }
374 }
375
376 void decrement() {
377 const HeaderLines& header_lines = headers_->header_lines_;
378 const HeaderLines::size_type header_lines_size = header_lines.size();
379 const HeaderLines::size_type original_idx = idx_;
380 do {
381 --idx_;
382 } while (idx_ < header_lines_size && header_lines[idx_].skip == true);
383 // The condition below exists so that --(rbegin() + 1) == rbegin(), even
384 // if there are only 'skip == true' elements between the rbegin() iterator
385 // and the beginning of the vector of HeaderLineDescriptions.
386 // TODO(fenix): refactor this list so that we don't have to do
387 // linear scanning through skipped headers (and this condition is
388 // then unnecessary)
389 if (idx_ > header_lines_size) {
390 idx_ = original_idx - 1;
391 }
392 }
393
394 reference Lookup(HeaderLines::size_type index) const {
395 DCHECK_LT(index, headers_->header_lines_.size());
396 const HeaderLineDescription& line = headers_->header_lines_[index];
397 const char* stream_begin = headers_->GetPtr(line.buffer_base_idx);
398 value_ = value_type(
399 base::StringPiece(stream_begin + line.first_char_idx,
400 line.key_end_idx - line.first_char_idx),
401 base::StringPiece(stream_begin + line.value_begin_idx,
402 line.last_char_idx - line.value_begin_idx));
403 DCHECK_GE(line.key_end_idx, line.first_char_idx);
404 DCHECK_GE(line.last_char_idx, line.value_begin_idx);
405 return value_;
406 }
407
408 const BalsaHeaders* headers_;
409 HeaderLines::size_type idx_;
410 mutable StringPiecePair value_;
411 };
412
413 class reverse_iterator_base : public iterator_base {
414 public:
415 typedef reverse_iterator_base self;
416 typedef iterator_base::reference reference;
417 typedef iterator_base::pointer pointer;
418 using iterator_base::headers_;
419 using iterator_base::idx_;
420
421 reverse_iterator_base() : iterator_base() {}
422
423 // This constructor is no explicit purposely.
424 reverse_iterator_base(const iterator_base& it) : // NOLINT
425 iterator_base(it) {
426 }
427
428 self& operator=(const iterator_base& it) {
429 idx_ = it.idx_;
430 headers_ = it.headers_;
431 return *this;
432 }
433
434 self& operator=(const reverse_iterator_base& it) {
435 idx_ = it.idx_;
436 headers_ = it.headers_;
437 return *this;
438 }
439
440 reference operator*() const {
441 return Lookup(idx_ - 1);
442 }
443
444 pointer operator->() const {
445 return &(this->operator*());
446 }
447
448 reverse_iterator_base(const reverse_iterator_base& it) :
449 iterator_base(it) { }
450
451 protected:
452 void increment() {
453 --idx_;
454 iterator_base::decrement();
455 ++idx_;
456 }
457
458 void decrement() {
459 ++idx_;
460 iterator_base::increment();
461 --idx_;
462 }
463
464 reverse_iterator_base(const BalsaHeaders* headers,
465 HeaderLines::size_type index) :
466 iterator_base(headers, index) {}
467 };
468
469 public:
470 class const_header_lines_iterator : public iterator_base {
471 friend class BalsaHeaders;
472 public:
473 typedef const_header_lines_iterator self;
474 const_header_lines_iterator() : iterator_base() {}
475
476 const_header_lines_iterator(const const_header_lines_iterator& it) :
477 iterator_base(it.headers_, it.idx_) {}
478
479 self& operator++() {
480 iterator_base::increment();
481 return *this;
482 }
483
484 self& operator--() {
485 iterator_base::decrement();
486 return *this;
487 }
488 protected:
489 const_header_lines_iterator(const BalsaHeaders* headers,
490 HeaderLines::size_type index) :
491 iterator_base(headers, index) {}
492 };
493
494 class const_reverse_header_lines_iterator : public reverse_iterator_base {
495 public:
496 typedef const_reverse_header_lines_iterator self;
497 const_reverse_header_lines_iterator() : reverse_iterator_base() {}
498
499 const_reverse_header_lines_iterator(
500 const const_header_lines_iterator& it) :
501 reverse_iterator_base(it.headers_, it.idx_) {}
502
503 const_reverse_header_lines_iterator(
504 const const_reverse_header_lines_iterator& it) :
505 reverse_iterator_base(it.headers_, it.idx_) {}
506
507 const_header_lines_iterator base() {
508 return const_header_lines_iterator(headers_, idx_);
509 }
510
511 self& operator++() {
512 reverse_iterator_base::increment();
513 return *this;
514 }
515
516 self& operator--() {
517 reverse_iterator_base::decrement();
518 return *this;
519 }
520 protected:
521 const_reverse_header_lines_iterator(const BalsaHeaders* headers,
522 HeaderLines::size_type index) :
523 reverse_iterator_base(headers, index) {}
524
525 friend class BalsaHeaders;
526 };
527
528 // An iterator that only stops at lines with a particular key.
529 // See also GetIteratorForKey.
530 //
531 // Check against header_lines_key_end() to determine when iteration is
532 // finished. header_lines_end() will also work.
533 class const_header_lines_key_iterator : public iterator_base {
534 friend class BalsaHeaders;
535 public:
536 typedef const_header_lines_key_iterator self;
537 const_header_lines_key_iterator(const const_header_lines_key_iterator&);
538
539 self& operator++() {
540 do {
541 iterator_base::increment();
542 } while (!AtEnd() &&
543 !base::EqualsCaseInsensitiveASCII(key_, (**this).first));
544 return *this;
545 }
546
547 void operator++(int ignore) {
548 ++(*this);
549 }
550
551 // Only forward-iteration makes sense, so no operator-- defined.
552
553 private:
554 const_header_lines_key_iterator(const BalsaHeaders* headers,
555 HeaderLines::size_type index,
556 const base::StringPiece& key);
557
558 // Should only be used for creating an end iterator.
559 const_header_lines_key_iterator(const BalsaHeaders* headers,
560 HeaderLines::size_type index);
561
562 bool AtEnd() const {
563 return *this >= headers_->header_lines_end();
564 }
565
566 base::StringPiece key_;
567 };
568
569 // TODO(fenix): Revisit the amount of bytes initially allocated to the second
570 // block of the balsa_buffer_. It may make sense to pre-allocate some amount
571 // (roughly the amount we'd append in new headers such as X-User-Ip, etc.)
572 BalsaHeaders();
573 ~BalsaHeaders();
574
575 const_header_lines_iterator header_lines_begin() {
576 return HeaderLinesBeginHelper<const_header_lines_iterator>();
577 }
578
579 const_header_lines_iterator header_lines_begin() const {
580 return HeaderLinesBeginHelper<const_header_lines_iterator>();
581 }
582
583 const_header_lines_iterator header_lines_end() {
584 return HeaderLinesEndHelper<const_header_lines_iterator>();
585 }
586
587 const_header_lines_iterator header_lines_end() const {
588 return HeaderLinesEndHelper<const_header_lines_iterator>();
589 }
590
591 const_reverse_header_lines_iterator header_lines_rbegin() {
592 return const_reverse_header_lines_iterator(header_lines_end());
593 }
594
595 const_reverse_header_lines_iterator header_lines_rbegin() const {
596 return const_reverse_header_lines_iterator(header_lines_end());
597 }
598
599 const_reverse_header_lines_iterator header_lines_rend() {
600 return const_reverse_header_lines_iterator(header_lines_begin());
601 }
602
603 const_reverse_header_lines_iterator header_lines_rend() const {
604 return const_reverse_header_lines_iterator(header_lines_begin());
605 }
606
607 const_header_lines_key_iterator header_lines_key_end() const {
608 return HeaderLinesEndHelper<const_header_lines_key_iterator>();
609 }
610
611 void erase(const const_header_lines_iterator& it) {
612 DCHECK_EQ(it.headers_, this);
613 DCHECK_LT(it.idx_, header_lines_.size());
614 DCHECK_GE(it.idx_, 0u);
615 header_lines_[it.idx_].skip = true;
616 }
617
618 void Clear();
619
620 void Swap(BalsaHeaders* other);
621
622 void CopyFrom(const BalsaHeaders& other);
623
624 void HackHeader(const base::StringPiece& key, const base::StringPiece& value);
625
626 // Same as AppendToHeader, except that it will attempt to preserve
627 // header ordering.
628 // Note that this will always append to an existing header, if available,
629 // without moving the header around, or collapsing multiple header lines
630 // with the same key together. For this reason, it only 'attempts' to
631 // preserve header ordering.
632 // TODO(fenix): remove this function and rename all occurances
633 // of it in the code to AppendToHeader when the condition above
634 // has been satisified.
635 void HackAppendToHeader(const base::StringPiece& key,
636 const base::StringPiece& value);
637
638 // Replaces header entries with key 'key' if they exist, or appends
639 // a new header if none exist. See 'AppendHeader' below for additional
640 // comments about ContentLength and TransferEncoding headers. Note that this
641 // will allocate new storage every time that it is called.
642 // TODO(fenix): modify this function to reuse existing storage
643 // if it is available.
644 void ReplaceOrAppendHeader(const base::StringPiece& key,
645 const base::StringPiece& value);
646
647 // Append a new header entry to the header object. Clients who wish to append
648 // Content-Length header should use SetContentLength() method instead of
649 // adding the content length header using AppendHeader (manually adding the
650 // content length header will not update the content_length_ and
651 // content_length_status_ values).
652 // Similarly, clients who wish to add or remove the transfer encoding header
653 // in order to apply or remove chunked encoding should use SetChunkEncoding()
654 // instead.
655 void AppendHeader(const base::StringPiece& key,
656 const base::StringPiece& value);
657
658 // Appends ',value' to an existing header named 'key'. If no header with the
659 // correct key exists, it will call AppendHeader(key, value). Calling this
660 // function on a key which exists several times in the headers will produce
661 // unpredictable results.
662 void AppendToHeader(const base::StringPiece& key,
663 const base::StringPiece& value);
664
665 // Prepends '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 PrependToHeader(const base::StringPiece& key,
670 const base::StringPiece& value);
671
672 const base::StringPiece GetHeader(const base::StringPiece& key) const;
673
674 // Iterates over all currently valid header lines, appending their
675 // values into the vector 'out', in top-to-bottom order.
676 // Header-lines which have been erased are not currently valid, and
677 // will not have their values appended. Empty values will be
678 // represented as empty string. If 'key' doesn't exist in the headers at
679 // all, out will not be changed. We do not clear the vector out
680 // before adding new entries. If there are header lines with matching
681 // key but empty value then they are also added to the vector out.
682 // (Basically empty values are not treated in any special manner).
683 //
684 // Example:
685 // Input header:
686 // "GET / HTTP/1.0\r\n"
687 // "key1: v1\r\n"
688 // "key1: \r\n"
689 // "key1:\r\n"
690 // "key1: v1\r\n"
691 // "key1:v2\r\n"
692 //
693 // vector out is initially: ["foo"]
694 // vector out after GetAllOfHeader("key1", &out) is:
695 // ["foo", "v1", "", "", "v2", "v1", "v2"]
696
697 void GetAllOfHeader(const base::StringPiece& key,
698 std::vector<base::StringPiece>* out) const;
699
700 // Joins all values for key into a comma-separated string in out.
701 // More efficient than calling JoinStrings on result of GetAllOfHeader if
702 // you don't need the intermediate vector<StringPiece>.
703 void GetAllOfHeaderAsString(const base::StringPiece& key,
704 std::string* out) const;
705
706 // Returns true if RFC 2616 Section 14 indicates that header can
707 // have multiple values.
708 static bool IsMultivaluedHeader(const base::StringPiece& header);
709
710 // Determine if a given header is present.
711 inline bool HasHeader(const base::StringPiece& key) const {
712 return (GetConstHeaderLinesIterator(key, header_lines_.begin()) !=
713 header_lines_.end());
714 }
715
716 // Returns true iff any header 'key' exists with non-empty value.
717 bool HasNonEmptyHeader(const base::StringPiece& key) const;
718
719 const_header_lines_iterator GetHeaderPosition(
720 const base::StringPiece& key) const;
721
722 // Returns a forward-only iterator that only stops at lines matching key.
723 // String backing 'key' must remain valid for lifetime of iterator.
724 //
725 // Check returned iterator against header_lines_key_end() to determine when
726 // iteration is finished.
727 const_header_lines_key_iterator GetIteratorForKey(
728 const base::StringPiece& key) const;
729
730 void RemoveAllOfHeader(const base::StringPiece& key);
731
732 // Removes all headers starting with 'key' [case insensitive]
733 void RemoveAllHeadersWithPrefix(const base::StringPiece& key);
734
735 // Returns the lower bound of memory used by this header object, including
736 // all internal buffers and data structure. Some of the memory used cannot be
737 // directly measure. For example, memory used for bookkeeping by standard
738 // containers.
739 size_t GetMemoryUsedLowerBound() const;
740
741 // Returns the upper bound on the required buffer space to fully write out
742 // the header object (this include the first line, all header lines, and the
743 // final CRLF that marks the ending of the header).
744 size_t GetSizeForWriteBuffer() const;
745
746 // The following WriteHeader* methods are template member functions that
747 // place one requirement on the Buffer class: it must implement a Write
748 // method that takes a pointer and a length. The buffer passed in is not
749 // required to be stretchable. For non-stretchable buffers, the user must
750 // call GetSizeForWriteBuffer() to find out the upper bound on the output
751 // buffer space required to make sure that the entire header is serialized.
752 // BalsaHeaders will not check that there is adequate space in the buffer
753 // object during the write.
754
755 // Writes the entire header and the final CRLF that marks the end of the HTTP
756 // header section to the buffer. After this method returns, no more header
757 // data should be written to the buffer.
758 template <typename Buffer>
759 void WriteHeaderAndEndingToBuffer(Buffer* buffer) const {
760 WriteToBuffer(buffer);
761 WriteHeaderEndingToBuffer(buffer);
762 }
763
764 // Writes the final CRLF to the buffer to terminate the HTTP header section.
765 // After this method returns, no more header data should be written to the
766 // buffer.
767 template <typename Buffer>
768 static void WriteHeaderEndingToBuffer(Buffer* buffer) {
769 buffer->Write("\r\n", 2);
770 }
771
772 // Writes the entire header to the buffer without the CRLF that terminates
773 // the HTTP header. This lets users append additional header lines using
774 // WriteHeaderLineToBuffer and then terminate the header with
775 // WriteHeaderEndingToBuffer as the header is serialized to the
776 // buffer, without having to first copy the header.
777 template <typename Buffer>
778 void WriteToBuffer(Buffer* buffer) const {
779 // write the first line.
780 const size_t firstline_len = whitespace_4_idx_ - non_whitespace_1_idx_;
781 const char* stream_begin = GetPtr(firstline_buffer_base_idx_);
782 buffer->Write(stream_begin + non_whitespace_1_idx_, firstline_len);
783 buffer->Write("\r\n", 2);
784 const HeaderLines::size_type end = header_lines_.size();
785 for (HeaderLines::size_type i = 0; i < end; ++i) {
786 const HeaderLineDescription& line = header_lines_[i];
787 if (line.skip) {
788 continue;
789 }
790 const char* line_ptr = GetPtr(line.buffer_base_idx);
791 WriteHeaderLineToBuffer(
792 buffer,
793 base::StringPiece(line_ptr + line.first_char_idx,
794 line.key_end_idx - line.first_char_idx),
795 base::StringPiece(line_ptr + line.value_begin_idx,
796 line.last_char_idx - line.value_begin_idx));
797 }
798 }
799
800 // Takes a header line in the form of a key/value pair and append it to the
801 // buffer. This function should be called after WriteToBuffer to
802 // append additional header lines to the header without copying the header.
803 // When the user is done with appending to the buffer,
804 // WriteHeaderEndingToBuffer must be used to terminate the HTTP
805 // header in the buffer. This method is a no-op if key is empty.
806 template <typename Buffer>
807 static void WriteHeaderLineToBuffer(Buffer* buffer,
808 const base::StringPiece& key,
809 const base::StringPiece& value) {
810 // if the key is empty, we don't want to write the rest because it
811 // will not be a well-formed header line.
812 if (!key.empty()) {
813 buffer->Write(key.data(), key.size());
814 buffer->Write(": ", 2);
815 buffer->Write(value.data(), value.size());
816 buffer->Write("\r\n", 2);
817 }
818 }
819
820 // Dump the textural representation of the header object to a string, which
821 // is suitable for writing out to logs. All CRLF will be printed out as \n.
822 // This function can be called on a header object in any state. The header
823 // content is appended to the string; the original content is not cleared.
824 void DumpHeadersToString(std::string* str) const;
825
826 // Calls DumpHeadersToString to dump the textural representation of the header
827 // object to a string. Raw header data will be printed out if the header
828 // object is not completely parsed, e.g., when there was an error in the
829 // middle of parsing.
830 void DumpToString(std::string* str) const;
831 std::string DebugString() const;
832
833 const base::StringPiece first_line() const {
834 DCHECK_GE(whitespace_4_idx_, non_whitespace_1_idx_);
835 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_1_idx_,
836 whitespace_4_idx_ - non_whitespace_1_idx_);
837 }
838
839 // Returns the parsed value of the response code if it has been parsed.
840 // Guaranteed to return 0 when unparsed (though it is a much better idea to
841 // verify that the BalsaFrame had no errors while parsing).
842 // This may return response codes which are outside the normal bounds of
843 // HTTP response codes-- it is up to the user of this class to ensure that
844 // the response code is one which is interpretable.
845 size_t parsed_response_code() const { return parsed_response_code_; }
846
847 const base::StringPiece request_method() const {
848 DCHECK_GE(whitespace_2_idx_, non_whitespace_1_idx_);
849 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_1_idx_,
850 whitespace_2_idx_ - non_whitespace_1_idx_);
851 }
852
853 const base::StringPiece response_version() const {
854 // Note: There is no difference between request_method() and
855 // response_version(). They both could be called
856 // GetFirstTokenFromFirstline()... but that wouldn't be anywhere near as
857 // descriptive.
858 return request_method();
859 }
860
861 const base::StringPiece request_uri() const {
862 DCHECK_GE(whitespace_3_idx_, non_whitespace_2_idx_);
863 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_2_idx_,
864 whitespace_3_idx_ - non_whitespace_2_idx_);
865 }
866
867 const base::StringPiece response_code() const {
868 // Note: There is no difference between request_uri() and response_code().
869 // They both could be called GetSecondtTokenFromFirstline(), but, as noted
870 // in an earlier comment, that wouldn't be as descriptive.
871 return request_uri();
872 }
873
874 const base::StringPiece request_version() const {
875 DCHECK_GE(whitespace_4_idx_, non_whitespace_3_idx_);
876 return base::StringPiece(BeginningOfFirstLine() + non_whitespace_3_idx_,
877 whitespace_4_idx_ - non_whitespace_3_idx_);
878 }
879
880 const base::StringPiece response_reason_phrase() const {
881 // Note: There is no difference between request_version() and
882 // response_reason_phrase(). They both could be called
883 // GetThirdTokenFromFirstline(), but, as noted in an earlier comment, that
884 // wouldn't be as descriptive.
885 return request_version();
886 }
887
888 // Note that SetFirstLine will not update the internal indices for the
889 // various bits of the first-line (and may set them all to zero).
890 // If you'd like to use the accessors for the various bits of the firstline,
891 // then you should use the Set* functions, or SetFirstlineFromStringPieces,
892 // below, instead.
893 //
894 void SetFirstlineFromStringPieces(const base::StringPiece& firstline_a,
895 const base::StringPiece& firstline_b,
896 const base::StringPiece& firstline_c);
897
898 void SetRequestFirstlineFromStringPieces(const base::StringPiece& method,
899 const base::StringPiece& uri,
900 const base::StringPiece& version) {
901 SetFirstlineFromStringPieces(method, uri, version);
902 }
903
904 void SetResponseFirstlineFromStringPieces(
905 const base::StringPiece& version,
906 const base::StringPiece& code,
907 const base::StringPiece& reason_phrase) {
908 SetFirstlineFromStringPieces(version, code, reason_phrase);
909 }
910
911 // These functions are exactly the same, except that their names are
912 // different. This is done so that the code using this class is more
913 // expressive.
914 void SetRequestMethod(const base::StringPiece& method);
915 void SetResponseVersion(const base::StringPiece& version);
916
917 void SetRequestUri(const base::StringPiece& uri);
918 void SetResponseCode(const base::StringPiece& code);
919 void set_parsed_response_code(size_t parsed_response_code) {
920 parsed_response_code_ = parsed_response_code;
921 }
922 void SetParsedResponseCodeAndUpdateFirstline(size_t parsed_response_code);
923
924 // These functions are exactly the same, except that their names are
925 // different. This is done so that the code using this class is more
926 // expressive.
927 void SetRequestVersion(const base::StringPiece& version);
928 void SetResponseReasonPhrase(const base::StringPiece& reason_phrase);
929
930 // The biggest problem with SetFirstLine is that we don't want to use a
931 // separate buffer for it. The second biggest problem with it is that the
932 // first biggest problem requires that we store offsets into a buffer instead
933 // of pointers into a buffer. Cuteness aside, SetFirstLine doesn't parse
934 // the individual fields of the firstline, and so accessors to those fields
935 // will not work properly after calling SetFirstLine. If you want those
936 // accessors to work, use the Set* functions above this one.
937 // SetFirstLine is stuff useful, however, if all you care about is correct
938 // serialization with the rest of the header object.
939 void SetFirstLine(const base::StringPiece& line);
940
941 // Simple accessors to some of the internal state
942 bool transfer_encoding_is_chunked() const {
943 return transfer_encoding_is_chunked_;
944 }
945
946 static bool ResponseCodeImpliesNoBody(size_t code) {
947 // From HTTP spec section 6.1.1 all 1xx responses must not have a body,
948 // as well as 204 No Content and 304 Not Modified.
949 return ((code >= 100) && (code <= 199)) || (code == 204) || (code == 304);
950 }
951
952 // Note: never check this for requests. Nothing bad will happen if you do,
953 // but spec does not allow requests framed by connection close.
954 // TODO(vitaliyl): refactor.
955 bool is_framed_by_connection_close() const {
956 // We declare that response is framed by connection close if it has no
957 // content-length, no transfer encoding, and is allowed to have a body by
958 // the HTTP spec.
959 // parsed_response_code_ is 0 for requests, so ResponseCodeImpliesNoBody
960 // will return false.
961 return (content_length_status_ == BalsaHeadersEnums::NO_CONTENT_LENGTH) &&
962 !transfer_encoding_is_chunked_ &&
963 !ResponseCodeImpliesNoBody(parsed_response_code_);
964 }
965
966 size_t content_length() const { return content_length_; }
967 BalsaHeadersEnums::ContentLengthStatus content_length_status() const {
968 return content_length_status_;
969 }
970
971 // SetContentLength and SetChunkEncoding modifies the header object to use
972 // content-length and transfer-encoding headers in a consistent manner. They
973 // set all internal flags and status so client can get a consistent view from
974 // various accessors.
975 void SetContentLength(size_t length);
976 void SetChunkEncoding(bool chunk_encode);
977
978 protected:
979 friend class BalsaFrame;
980 friend class SpdyFrame;
981 friend class HTTPMessage;
982 friend class BalsaHeadersTokenUtils;
983
984 const char* BeginningOfFirstLine() const {
985 return GetPtr(firstline_buffer_base_idx_);
986 }
987
988 char* GetPtr(BalsaBuffer::Blocks::size_type block_idx) {
989 return balsa_buffer_.GetPtr(block_idx);
990 }
991
992 const char* GetPtr(BalsaBuffer::Blocks::size_type block_idx) const {
993 return balsa_buffer_.GetPtr(block_idx);
994 }
995
996 void WriteFromFramer(const char* ptr, size_t size) {
997 balsa_buffer_.WriteToContiguousBuffer(base::StringPiece(ptr, size));
998 }
999
1000 void DoneWritingFromFramer() {
1001 balsa_buffer_.NoMoreWriteToContiguousBuffer();
1002 }
1003
1004 const char* OriginalHeaderStreamBegin() const {
1005 return balsa_buffer_.StartOfFirstBlock();
1006 }
1007
1008 const char* OriginalHeaderStreamEnd() const {
1009 return balsa_buffer_.EndOfFirstBlock();
1010 }
1011
1012 size_t GetReadableBytesFromHeaderStream() const {
1013 return OriginalHeaderStreamEnd() - OriginalHeaderStreamBegin();
1014 }
1015
1016 void GetReadablePtrFromHeaderStream(const char** p, size_t* s) {
1017 *p = OriginalHeaderStreamBegin();
1018 *s = GetReadableBytesFromHeaderStream();
1019 }
1020
1021 base::StringPiece GetValueFromHeaderLineDescription(
1022 const HeaderLineDescription& line) const;
1023
1024 void AddAndMakeDescription(const base::StringPiece& key,
1025 const base::StringPiece& value,
1026 HeaderLineDescription* d);
1027
1028 void AppendOrPrependAndMakeDescription(const base::StringPiece& key,
1029 const base::StringPiece& value,
1030 bool append,
1031 HeaderLineDescription* d);
1032
1033 // Removes all header lines with the given key starting at start.
1034 void RemoveAllOfHeaderStartingAt(const base::StringPiece& key,
1035 HeaderLines::iterator start);
1036
1037 // If the 'key' does not exist in the headers, calls
1038 // AppendHeader(key, value). Otherwise if append is true, appends ',value'
1039 // to the first existing header with key 'key'. If append is false, prepends
1040 // 'value,' to the first existing header with key 'key'.
1041 void AppendOrPrependToHeader(const base::StringPiece& key,
1042 const base::StringPiece& value,
1043 bool append);
1044
1045 HeaderLines::const_iterator GetConstHeaderLinesIterator(
1046 const base::StringPiece& key,
1047 HeaderLines::const_iterator start) const;
1048
1049 HeaderLines::iterator GetHeaderLinesIteratorNoSkip(
1050 const base::StringPiece& key,
1051 HeaderLines::iterator start);
1052
1053 HeaderLines::iterator GetHeaderLinesIterator(
1054 const base::StringPiece& key,
1055 HeaderLines::iterator start);
1056
1057 template <typename IteratorType>
1058 const IteratorType HeaderLinesBeginHelper() const {
1059 if (header_lines_.empty()) {
1060 return IteratorType(this, 0);
1061 }
1062 const HeaderLines::size_type header_lines_size = header_lines_.size();
1063 for (HeaderLines::size_type i = 0; i < header_lines_size; ++i) {
1064 if (header_lines_[i].skip == false) {
1065 return IteratorType(this, i);
1066 }
1067 }
1068 return IteratorType(this, 0);
1069 }
1070
1071 template <typename IteratorType>
1072 const IteratorType HeaderLinesEndHelper() const {
1073 if (header_lines_.empty()) {
1074 return IteratorType(this, 0);
1075 }
1076 const HeaderLines::size_type header_lines_size = header_lines_.size();
1077 HeaderLines::size_type i = header_lines_size;
1078 do {
1079 --i;
1080 if (header_lines_[i].skip == false) {
1081 return IteratorType(this, i + 1);
1082 }
1083 } while (i != 0);
1084 return IteratorType(this, 0);
1085 }
1086
1087 // At the moment, this function will always return the original headers.
1088 // In the future, it may not do so after erasing header lines, modifying
1089 // header lines, or modifying the first line.
1090 // For this reason, it is strongly suggested that use of this function is
1091 // only acceptable for the purpose of debugging parse errors seen by the
1092 // BalsaFrame class.
1093 base::StringPiece OriginalHeadersForDebugging() const {
1094 return base::StringPiece(OriginalHeaderStreamBegin(),
1095 OriginalHeaderStreamEnd() - OriginalHeaderStreamBegin());
1096 }
1097
1098 BalsaBuffer balsa_buffer_;
1099
1100 size_t content_length_;
1101 BalsaHeadersEnums::ContentLengthStatus content_length_status_;
1102 size_t parsed_response_code_;
1103 // HTTP firstlines all have the following structure:
1104 // LWS NONWS LWS NONWS LWS NONWS NOTCRLF CRLF
1105 // [\t \r\n]+ [^\t ]+ [\t ]+ [^\t ]+ [\t ]+ [^\t ]+ [^\r\n]+ "\r\n"
1106 // ws1 nws1 ws2 nws2 ws3 nws3 ws4
1107 // | [-------) [-------) [----------------)
1108 // REQ: method request_uri version
1109 // RESP: version statuscode reason
1110 //
1111 // The first NONWS->LWS component we'll call firstline_a.
1112 // The second firstline_b, and the third firstline_c.
1113 //
1114 // firstline_a goes from nws1 to (but not including) ws2
1115 // firstline_b goes from nws2 to (but not including) ws3
1116 // firstline_c goes from nws3 to (but not including) ws4
1117 //
1118 // In the code:
1119 // ws1 == whitespace_1_idx_
1120 // nws1 == non_whitespace_1_idx_
1121 // ws2 == whitespace_2_idx_
1122 // nws2 == non_whitespace_2_idx_
1123 // ws3 == whitespace_3_idx_
1124 // nws3 == non_whitespace_3_idx_
1125 // ws4 == whitespace_4_idx_
1126 BalsaBuffer::Blocks::size_type firstline_buffer_base_idx_;
1127 size_t whitespace_1_idx_;
1128 size_t non_whitespace_1_idx_;
1129 size_t whitespace_2_idx_;
1130 size_t non_whitespace_2_idx_;
1131 size_t whitespace_3_idx_;
1132 size_t non_whitespace_3_idx_;
1133 size_t whitespace_4_idx_;
1134 size_t end_of_firstline_idx_;
1135
1136 bool transfer_encoding_is_chunked_;
1137
1138 HeaderLines header_lines_;
1139 };
1140
1141 } // namespace net
1142
1143 #endif // NET_TOOLS_BALSA_BALSA_HEADERS_H_
OLDNEW
« no previous file with comments | « net/tools/balsa/balsa_frame_test.cc ('k') | net/tools/balsa/balsa_headers.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698