| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // HttpRequestHeaders manages the request headers (including the request line). | |
| 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 | |
| 8 // operations. | |
| 9 | |
| 10 #ifndef NET_HTTP_HTTP_REQUEST_HEADERS_H_ | |
| 11 #define NET_HTTP_HTTP_REQUEST_HEADERS_H_ | |
| 12 | |
| 13 #include <string> | |
| 14 #include <vector> | |
| 15 #include "base/basictypes.h" | |
| 16 #include "base/string_piece.h" | |
| 17 | |
| 18 namespace net { | |
| 19 | |
| 20 class HttpRequestHeaders { | |
| 21 public: | |
| 22 static const char kGetMethod[]; | |
| 23 | |
| 24 static const char kCacheControl[]; | |
| 25 static const char kConnection[]; | |
| 26 static const char kContentLength[]; | |
| 27 static const char kHost[]; | |
| 28 static const char kPragma[]; | |
| 29 static const char kProxyConnection[]; | |
| 30 static const char kReferer[]; | |
| 31 static const char kUserAgent[]; | |
| 32 | |
| 33 HttpRequestHeaders(); | |
| 34 ~HttpRequestHeaders(); | |
| 35 | |
| 36 void SetRequestLine(const base::StringPiece& method, | |
| 37 const base::StringPiece& path, | |
| 38 const base::StringPiece& version); | |
| 39 | |
| 40 // 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 | |
| 42 // in the vector remains the same. When comparing |key|, case is ignored. | |
| 43 void SetHeader(const base::StringPiece& key, const base::StringPiece& value); | |
| 44 | |
| 45 // Removes the first header that matches (case insensitive) |key|. | |
| 46 void RemoveHeader(const base::StringPiece& key); | |
| 47 | |
| 48 // 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: | |
| 50 // | |
| 51 // message-header = field-name ":" [ field-value ] | |
| 52 // field-name = token | |
| 53 // field-value = *( field-content | LWS ) | |
| 54 // field-content = <the OCTETs making up the field-value | |
| 55 // and consisting of either *TEXT or combinations | |
| 56 // of token, separators, and quoted-string> | |
| 57 // | |
| 58 // AddHeaderFromString() will trim any LWS surrounding the | |
| 59 // field-content. | |
| 60 void AddHeaderFromString(const base::StringPiece& header_line); | |
| 61 | |
| 62 // Calls SetHeader() on each header from |other|, maintaining order. | |
| 63 void MergeFrom(const HttpRequestHeaders& other); | |
| 64 | |
| 65 // Serializes HttpRequestHeaders to a string representation. Joins all the | |
| 66 // header keys and values with ": ", and inserts "\r\n" between each header | |
| 67 // line, and adds the trailing "\r\n". | |
| 68 std::string ToString() const; | |
| 69 | |
| 70 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); | |
| 84 HeaderVector::const_iterator FindHeader(const base::StringPiece& key) const; | |
| 85 | |
| 86 std::string method_; | |
| 87 std::string path_; | |
| 88 std::string version_; | |
| 89 | |
| 90 HeaderVector headers_; | |
| 91 | |
| 92 DISALLOW_COPY_AND_ASSIGN(HttpRequestHeaders); | |
| 93 }; | |
| 94 | |
| 95 } // namespace net | |
| 96 | |
| 97 #endif // NET_HTTP_HTTP_REQUEST_HEADERS_H_ | |
| OLD | NEW |