| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // HttpRequestHeaders manages the request headers (including the request line). | 5 // HttpRequestHeaders manages the request headers. |
| 6 // It maintains these in a vector of header key/value pairs, thereby maintaining | 6 // It maintains these in a vector of header key/value pairs, thereby maintaining |
| 7 // the order of the headers. This means that any lookups are linear time | 7 // the order of the headers. This means that any lookups are linear time |
| 8 // operations. | 8 // operations. |
| 9 | 9 |
| 10 #ifndef NET_HTTP_HTTP_REQUEST_HEADERS_H_ | 10 #ifndef NET_HTTP_HTTP_REQUEST_HEADERS_H_ |
| 11 #define NET_HTTP_HTTP_REQUEST_HEADERS_H_ | 11 #define NET_HTTP_HTTP_REQUEST_HEADERS_H_ |
| 12 | 12 |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> | 14 #include <vector> |
| 15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 16 #include "base/string_piece.h" | 16 #include "base/string_piece.h" |
| 17 | 17 |
| 18 namespace net { | 18 namespace net { |
| 19 | 19 |
| 20 class HttpRequestHeaders { | 20 class HttpRequestHeaders { |
| 21 public: | 21 public: |
| 22 struct HeaderKeyValuePair { |
| 23 HeaderKeyValuePair() {} |
| 24 HeaderKeyValuePair(const base::StringPiece& key, |
| 25 const base::StringPiece& value) |
| 26 : key(key.data(), key.size()), value(value.data(), value.size()) {} |
| 27 |
| 28 std::string key; |
| 29 std::string value; |
| 30 }; |
| 31 |
| 32 typedef std::vector<HeaderKeyValuePair> HeaderVector; |
| 33 |
| 34 class Iterator { |
| 35 public: |
| 36 explicit Iterator(const HttpRequestHeaders& headers); |
| 37 ~Iterator(); |
| 38 |
| 39 // Advances the iterator to the next header, if any. Returns true if there |
| 40 // is a next header. Use name() and value() methods to access the resultant |
| 41 // header name and value. |
| 42 bool GetNext(); |
| 43 |
| 44 // These two accessors are only valid if GetNext() returned true. |
| 45 const std::string& name() const { return curr_->key; } |
| 46 const std::string& value() const { return curr_->value; } |
| 47 |
| 48 private: |
| 49 bool started_; |
| 50 HttpRequestHeaders::HeaderVector::const_iterator curr_; |
| 51 const HttpRequestHeaders::HeaderVector::const_iterator end_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(Iterator); |
| 54 }; |
| 55 |
| 22 static const char kGetMethod[]; | 56 static const char kGetMethod[]; |
| 23 | 57 |
| 58 static const char kAcceptCharset[]; |
| 59 static const char kAcceptEncoding[]; |
| 60 static const char kAcceptLanguage[]; |
| 24 static const char kCacheControl[]; | 61 static const char kCacheControl[]; |
| 25 static const char kConnection[]; | 62 static const char kConnection[]; |
| 63 static const char kCookie[]; |
| 26 static const char kContentLength[]; | 64 static const char kContentLength[]; |
| 27 static const char kHost[]; | 65 static const char kHost[]; |
| 66 static const char kIfModifiedSince[]; |
| 67 static const char kIfNoneMatch[]; |
| 68 static const char kIfRange[]; |
| 28 static const char kPragma[]; | 69 static const char kPragma[]; |
| 29 static const char kProxyConnection[]; | 70 static const char kProxyConnection[]; |
| 71 static const char kRange[]; |
| 30 static const char kReferer[]; | 72 static const char kReferer[]; |
| 31 static const char kUserAgent[]; | 73 static const char kUserAgent[]; |
| 32 | 74 |
| 33 HttpRequestHeaders(); | 75 HttpRequestHeaders(); |
| 34 ~HttpRequestHeaders(); | 76 ~HttpRequestHeaders(); |
| 35 | 77 |
| 36 void SetRequestLine(const base::StringPiece& method, | 78 bool IsEmpty() const { return headers_.empty(); } |
| 37 const base::StringPiece& path, | 79 |
| 38 const base::StringPiece& version); | 80 bool HasHeader(const base::StringPiece& key) const { |
| 81 return FindHeader(key) != headers_.end(); |
| 82 } |
| 83 |
| 84 // Gets the first header that matches |key|. If found, returns true and |
| 85 // writes the value to |out|. |
| 86 bool GetHeader(const base::StringPiece& key, std::string* out) const; |
| 87 |
| 88 // Clears all the headers. |
| 89 void Clear(); |
| 39 | 90 |
| 40 // Sets the header value pair for |key| and |value|. If |key| already exists, | 91 // Sets the header value pair for |key| and |value|. If |key| already exists, |
| 41 // then the header value is modified, but the key is untouched, and the order | 92 // then the header value is modified, but the key is untouched, and the order |
| 42 // in the vector remains the same. When comparing |key|, case is ignored. | 93 // in the vector remains the same. When comparing |key|, case is ignored. |
| 43 void SetHeader(const base::StringPiece& key, const base::StringPiece& value); | 94 void SetHeader(const base::StringPiece& key, const base::StringPiece& value); |
| 44 | 95 |
| 45 // Removes the first header that matches (case insensitive) |key|. | 96 // Removes the first header that matches (case insensitive) |key|. |
| 46 void RemoveHeader(const base::StringPiece& key); | 97 void RemoveHeader(const base::StringPiece& key); |
| 47 | 98 |
| 48 // Parses the header from a string and calls SetHeader() with it. This string | 99 // Parses the header from a string and calls SetHeader() with it. This string |
| 49 // should not contain any CRLF. As per RFC2616, the format is: | 100 // should not contain any CRLF. As per RFC2616, the format is: |
| 50 // | 101 // |
| 51 // message-header = field-name ":" [ field-value ] | 102 // message-header = field-name ":" [ field-value ] |
| 52 // field-name = token | 103 // field-name = token |
| 53 // field-value = *( field-content | LWS ) | 104 // field-value = *( field-content | LWS ) |
| 54 // field-content = <the OCTETs making up the field-value | 105 // field-content = <the OCTETs making up the field-value |
| 55 // and consisting of either *TEXT or combinations | 106 // and consisting of either *TEXT or combinations |
| 56 // of token, separators, and quoted-string> | 107 // of token, separators, and quoted-string> |
| 57 // | 108 // |
| 58 // AddHeaderFromString() will trim any LWS surrounding the | 109 // AddHeaderFromString() will trim any LWS surrounding the |
| 59 // field-content. | 110 // field-content. |
| 60 void AddHeaderFromString(const base::StringPiece& header_line); | 111 void AddHeaderFromString(const base::StringPiece& header_line); |
| 61 | 112 |
| 113 // Same thing as AddHeaderFromString() except that |headers| is a "\r\n" |
| 114 // delimited string of header lines. It will split up the string by "\r\n" |
| 115 // and call AddHeaderFromString() on each. |
| 116 void AddHeadersFromString(const base::StringPiece& headers); |
| 117 |
| 62 // Calls SetHeader() on each header from |other|, maintaining order. | 118 // Calls SetHeader() on each header from |other|, maintaining order. |
| 63 void MergeFrom(const HttpRequestHeaders& other); | 119 void MergeFrom(const HttpRequestHeaders& other); |
| 64 | 120 |
| 121 // Copies from |other| to |this|. |
| 122 void CopyFrom(const HttpRequestHeaders& other) { |
| 123 *this = other; |
| 124 } |
| 125 |
| 65 // Serializes HttpRequestHeaders to a string representation. Joins all the | 126 // Serializes HttpRequestHeaders to a string representation. Joins all the |
| 66 // header keys and values with ": ", and inserts "\r\n" between each header | 127 // header keys and values with ": ", and inserts "\r\n" between each header |
| 67 // line, and adds the trailing "\r\n". | 128 // line, and adds the trailing "\r\n". |
| 68 std::string ToString() const; | 129 std::string ToString() const; |
| 69 | 130 |
| 70 private: | 131 private: |
| 71 struct HeaderKeyValuePair { | |
| 72 HeaderKeyValuePair() {} | |
| 73 HeaderKeyValuePair(const base::StringPiece& key, | |
| 74 const base::StringPiece& value) | |
| 75 : key(key.data(), key.size()), value(value.data(), value.size()) {} | |
| 76 | |
| 77 std::string key; | |
| 78 std::string value; | |
| 79 }; | |
| 80 | |
| 81 typedef std::vector<HeaderKeyValuePair> HeaderVector; | |
| 82 | |
| 83 HeaderVector::iterator FindHeader(const base::StringPiece& key); | 132 HeaderVector::iterator FindHeader(const base::StringPiece& key); |
| 84 HeaderVector::const_iterator FindHeader(const base::StringPiece& key) const; | 133 HeaderVector::const_iterator FindHeader(const base::StringPiece& key) const; |
| 85 | 134 |
| 86 std::string method_; | |
| 87 std::string path_; | |
| 88 std::string version_; | |
| 89 | |
| 90 HeaderVector headers_; | 135 HeaderVector headers_; |
| 91 | 136 |
| 92 DISALLOW_COPY_AND_ASSIGN(HttpRequestHeaders); | 137 // Allow the copy construction and operator= to facilitate copying in |
| 138 // HttpRequestInfo. |
| 139 // TODO(willchan): Investigate to see if we can remove the need to copy |
| 140 // HttpRequestInfo. |
| 141 // DISALLOW_COPY_AND_ASSIGN(HttpRequestHeaders); |
| 93 }; | 142 }; |
| 94 | 143 |
| 95 } // namespace net | 144 } // namespace net |
| 96 | 145 |
| 97 #endif // NET_HTTP_HTTP_REQUEST_HEADERS_H_ | 146 #endif // NET_HTTP_HTTP_REQUEST_HEADERS_H_ |
| OLD | NEW |