| 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 #include "net/http/http_request_headers.h" | 5 #include "net/http/http_request_headers.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "net/http/http_util.h" | 9 #include "net/http/http_util.h" |
| 10 | 10 |
| 11 namespace net { | 11 namespace net { |
| 12 | 12 |
| 13 const char HttpRequestHeaders::kGetMethod[] = "GET"; | 13 const char HttpRequestHeaders::kGetMethod[] = "GET"; |
| 14 | 14 const char HttpRequestHeaders::kAcceptCharset[] = "Accept-Charset"; |
| 15 const char HttpRequestHeaders::kAcceptEncoding[] = "Accept-Encoding"; |
| 16 const char HttpRequestHeaders::kAcceptLanguage[] = "Accept-Language"; |
| 15 const char HttpRequestHeaders::kCacheControl[] = "Cache-Control"; | 17 const char HttpRequestHeaders::kCacheControl[] = "Cache-Control"; |
| 16 const char HttpRequestHeaders::kConnection[] = "Connection"; | 18 const char HttpRequestHeaders::kConnection[] = "Connection"; |
| 19 const char HttpRequestHeaders::kCookie[] = "Cookie"; |
| 17 const char HttpRequestHeaders::kContentLength[] = "Content-Length"; | 20 const char HttpRequestHeaders::kContentLength[] = "Content-Length"; |
| 18 const char HttpRequestHeaders::kHost[] = "Host"; | 21 const char HttpRequestHeaders::kHost[] = "Host"; |
| 22 const char HttpRequestHeaders::kIfModifiedSince[] = "If-Modified-Since"; |
| 23 const char HttpRequestHeaders::kIfNoneMatch[] = "If-None-Match"; |
| 24 const char HttpRequestHeaders::kIfRange[] = "If-Range"; |
| 19 const char HttpRequestHeaders::kPragma[] = "Pragma"; | 25 const char HttpRequestHeaders::kPragma[] = "Pragma"; |
| 20 const char HttpRequestHeaders::kProxyConnection[] = "Proxy-Connection"; | 26 const char HttpRequestHeaders::kProxyConnection[] = "Proxy-Connection"; |
| 27 const char HttpRequestHeaders::kRange[] = "Range"; |
| 21 const char HttpRequestHeaders::kReferer[] = "Referer"; | 28 const char HttpRequestHeaders::kReferer[] = "Referer"; |
| 22 const char HttpRequestHeaders::kUserAgent[] = "User-Agent"; | 29 const char HttpRequestHeaders::kUserAgent[] = "User-Agent"; |
| 23 | 30 |
| 31 HttpRequestHeaders::Iterator::Iterator(const HttpRequestHeaders& headers) |
| 32 : started_(false), |
| 33 curr_(headers.headers_.begin()), |
| 34 end_(headers.headers_.end()) {} |
| 35 |
| 36 HttpRequestHeaders::Iterator::~Iterator() {} |
| 37 |
| 38 bool HttpRequestHeaders::Iterator::GetNext() { |
| 39 if (!started_) { |
| 40 started_ = true; |
| 41 return curr_ != end_; |
| 42 } |
| 43 |
| 44 if (curr_ == end_) |
| 45 return false; |
| 46 |
| 47 ++curr_; |
| 48 return curr_ != end_; |
| 49 } |
| 50 |
| 24 HttpRequestHeaders::HttpRequestHeaders() {} | 51 HttpRequestHeaders::HttpRequestHeaders() {} |
| 25 HttpRequestHeaders::~HttpRequestHeaders() {} | 52 HttpRequestHeaders::~HttpRequestHeaders() {} |
| 26 | 53 |
| 27 void HttpRequestHeaders::SetRequestLine(const base::StringPiece& method, | 54 bool HttpRequestHeaders::GetHeader(const base::StringPiece& key, |
| 28 const base::StringPiece& path, | 55 std::string* out) const { |
| 29 const base::StringPiece& version) { | 56 HeaderVector::const_iterator it = FindHeader(key); |
| 30 DCHECK(!method.empty()); | 57 if (it == headers_.end()) |
| 31 DCHECK(!path.empty()); | 58 return false; |
| 32 DCHECK(!version.empty()); | 59 out->assign(it->value); |
| 60 return true; |
| 61 } |
| 33 | 62 |
| 34 method_.assign(method.data(), method.length()); | 63 void HttpRequestHeaders::Clear() { |
| 35 path_.assign(path.data(), path.length()); | 64 headers_.clear(); |
| 36 version_.assign(version.data(), version.length()); | |
| 37 } | 65 } |
| 38 | 66 |
| 39 void HttpRequestHeaders::SetHeader(const base::StringPiece& key, | 67 void HttpRequestHeaders::SetHeader(const base::StringPiece& key, |
| 40 const base::StringPiece& value) { | 68 const base::StringPiece& value) { |
| 41 HeaderVector::iterator it = FindHeader(key); | 69 HeaderVector::iterator it = FindHeader(key); |
| 42 if (it != headers_.end()) | 70 if (it != headers_.end()) |
| 43 it->value = value.as_string(); | 71 it->value = value.as_string(); |
| 44 else | 72 else |
| 45 headers_.push_back(HeaderKeyValuePair(key.as_string(), value.as_string())); | 73 headers_.push_back(HeaderKeyValuePair(key.as_string(), value.as_string())); |
| 46 } | 74 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 base::StringPiece(&*header_value_begin, | 116 base::StringPiece(&*header_value_begin, |
| 89 header_value_end - header_value_begin)); | 117 header_value_end - header_value_begin)); |
| 90 } | 118 } |
| 91 } else if (value_index == header_line.size()) { | 119 } else if (value_index == header_line.size()) { |
| 92 SetHeader(header_key, ""); | 120 SetHeader(header_key, ""); |
| 93 } else { | 121 } else { |
| 94 NOTREACHED(); | 122 NOTREACHED(); |
| 95 } | 123 } |
| 96 } | 124 } |
| 97 | 125 |
| 126 void HttpRequestHeaders::AddHeadersFromString( |
| 127 const base::StringPiece& headers) { |
| 128 // TODO(willchan): Consider adding more StringPiece support in string_util.h |
| 129 // to eliminate copies. |
| 130 std::vector<std::string> header_line_vector; |
| 131 SplitStringUsingSubstr(headers.as_string(), "\r\n", &header_line_vector); |
| 132 for (std::vector<std::string>::const_iterator it = header_line_vector.begin(); |
| 133 it != header_line_vector.end(); ++it) { |
| 134 if (!it->empty()) |
| 135 AddHeaderFromString(*it); |
| 136 } |
| 137 } |
| 138 |
| 98 void HttpRequestHeaders::MergeFrom(const HttpRequestHeaders& other) { | 139 void HttpRequestHeaders::MergeFrom(const HttpRequestHeaders& other) { |
| 99 DCHECK(other.method_.empty()); | |
| 100 DCHECK(other.path_.empty()); | |
| 101 DCHECK(other.version_.empty()); | |
| 102 | |
| 103 for (HeaderVector::const_iterator it = other.headers_.begin(); | 140 for (HeaderVector::const_iterator it = other.headers_.begin(); |
| 104 it != other.headers_.end(); ++it ) { | 141 it != other.headers_.end(); ++it ) { |
| 105 SetHeader(it->key, it->value); | 142 SetHeader(it->key, it->value); |
| 106 } | 143 } |
| 107 } | 144 } |
| 108 | 145 |
| 109 std::string HttpRequestHeaders::ToString() const { | 146 std::string HttpRequestHeaders::ToString() const { |
| 110 std::string output; | 147 std::string output; |
| 111 if (!method_.empty()) { | |
| 112 DCHECK(!path_.empty()); | |
| 113 DCHECK(!version_.empty()); | |
| 114 output = StringPrintf( | |
| 115 "%s %s HTTP/%s\r\n", method_.c_str(), path_.c_str(), version_.c_str()); | |
| 116 } | |
| 117 for (HeaderVector::const_iterator it = headers_.begin(); | 148 for (HeaderVector::const_iterator it = headers_.begin(); |
| 118 it != headers_.end(); ++it) { | 149 it != headers_.end(); ++it) { |
| 119 if (!it->value.empty()) | 150 if (!it->value.empty()) |
| 120 StringAppendF(&output, "%s: %s\r\n", it->key.c_str(), it->value.c_str()); | 151 StringAppendF(&output, "%s: %s\r\n", it->key.c_str(), it->value.c_str()); |
| 121 else | 152 else |
| 122 StringAppendF(&output, "%s:\r\n", it->key.c_str()); | 153 StringAppendF(&output, "%s:\r\n", it->key.c_str()); |
| 123 } | 154 } |
| 124 output.append("\r\n"); | 155 output.append("\r\n"); |
| 125 return output; | 156 return output; |
| 126 } | 157 } |
| 127 | 158 |
| 128 HttpRequestHeaders::HeaderVector::iterator | 159 HttpRequestHeaders::HeaderVector::iterator |
| 129 HttpRequestHeaders::FindHeader(const base::StringPiece& key) { | 160 HttpRequestHeaders::FindHeader(const base::StringPiece& key) { |
| 130 for (HeaderVector::iterator it = headers_.begin(); | 161 for (HeaderVector::iterator it = headers_.begin(); |
| 131 it != headers_.end(); ++it) { | 162 it != headers_.end(); ++it) { |
| 132 if (key.length() == it->key.length() && | 163 if (key.length() == it->key.length() && |
| 133 !base::strncasecmp(key.data(), it->key.data(), key.length())) | 164 !base::strncasecmp(key.data(), it->key.data(), key.length())) |
| 134 return it; | 165 return it; |
| 135 } | 166 } |
| 136 | 167 |
| 137 return headers_.end(); | 168 return headers_.end(); |
| 138 } | 169 } |
| 139 | 170 |
| 171 HttpRequestHeaders::HeaderVector::const_iterator |
| 172 HttpRequestHeaders::FindHeader(const base::StringPiece& key) const { |
| 173 for (HeaderVector::const_iterator it = headers_.begin(); |
| 174 it != headers_.end(); ++it) { |
| 175 if (key.length() == it->key.length() && |
| 176 !base::strncasecmp(key.data(), it->key.data(), key.length())) |
| 177 return it; |
| 178 } |
| 179 |
| 180 return headers_.end(); |
| 181 } |
| 182 |
| 140 } // namespace net | 183 } // namespace net |
| OLD | NEW |