| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 #include "net/http/http_request_headers.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/strings/string_split.h" | |
| 9 #include "base/strings/string_util.h" | |
| 10 #include "base/strings/stringprintf.h" | |
| 11 #include "base/values.h" | |
| 12 #include "net/http/http_log_util.h" | |
| 13 #include "net/http/http_util.h" | |
| 14 | |
| 15 namespace net { | |
| 16 | |
| 17 const char HttpRequestHeaders::kGetMethod[] = "GET"; | |
| 18 const char HttpRequestHeaders::kAcceptCharset[] = "Accept-Charset"; | |
| 19 const char HttpRequestHeaders::kAcceptEncoding[] = "Accept-Encoding"; | |
| 20 const char HttpRequestHeaders::kAcceptLanguage[] = "Accept-Language"; | |
| 21 const char HttpRequestHeaders::kAuthorization[] = "Authorization"; | |
| 22 const char HttpRequestHeaders::kCacheControl[] = "Cache-Control"; | |
| 23 const char HttpRequestHeaders::kConnection[] = "Connection"; | |
| 24 const char HttpRequestHeaders::kContentLength[] = "Content-Length"; | |
| 25 const char HttpRequestHeaders::kContentType[] = "Content-Type"; | |
| 26 const char HttpRequestHeaders::kCookie[] = "Cookie"; | |
| 27 const char HttpRequestHeaders::kHost[] = "Host"; | |
| 28 const char HttpRequestHeaders::kIfModifiedSince[] = "If-Modified-Since"; | |
| 29 const char HttpRequestHeaders::kIfNoneMatch[] = "If-None-Match"; | |
| 30 const char HttpRequestHeaders::kIfRange[] = "If-Range"; | |
| 31 const char HttpRequestHeaders::kOrigin[] = "Origin"; | |
| 32 const char HttpRequestHeaders::kPragma[] = "Pragma"; | |
| 33 const char HttpRequestHeaders::kProxyAuthorization[] = "Proxy-Authorization"; | |
| 34 const char HttpRequestHeaders::kProxyConnection[] = "Proxy-Connection"; | |
| 35 const char HttpRequestHeaders::kRange[] = "Range"; | |
| 36 const char HttpRequestHeaders::kReferer[] = "Referer"; | |
| 37 const char HttpRequestHeaders::kUserAgent[] = "User-Agent"; | |
| 38 const char HttpRequestHeaders::kTransferEncoding[] = "Transfer-Encoding"; | |
| 39 | |
| 40 HttpRequestHeaders::HeaderKeyValuePair::HeaderKeyValuePair() { | |
| 41 } | |
| 42 | |
| 43 HttpRequestHeaders::HeaderKeyValuePair::HeaderKeyValuePair( | |
| 44 const base::StringPiece& key, const base::StringPiece& value) | |
| 45 : key(key.data(), key.size()), value(value.data(), value.size()) { | |
| 46 } | |
| 47 | |
| 48 | |
| 49 HttpRequestHeaders::Iterator::Iterator(const HttpRequestHeaders& headers) | |
| 50 : started_(false), | |
| 51 curr_(headers.headers_.begin()), | |
| 52 end_(headers.headers_.end()) {} | |
| 53 | |
| 54 HttpRequestHeaders::Iterator::~Iterator() {} | |
| 55 | |
| 56 bool HttpRequestHeaders::Iterator::GetNext() { | |
| 57 if (!started_) { | |
| 58 started_ = true; | |
| 59 return curr_ != end_; | |
| 60 } | |
| 61 | |
| 62 if (curr_ == end_) | |
| 63 return false; | |
| 64 | |
| 65 ++curr_; | |
| 66 return curr_ != end_; | |
| 67 } | |
| 68 | |
| 69 HttpRequestHeaders::HttpRequestHeaders() {} | |
| 70 HttpRequestHeaders::~HttpRequestHeaders() {} | |
| 71 | |
| 72 bool HttpRequestHeaders::GetHeader(const base::StringPiece& key, | |
| 73 std::string* out) const { | |
| 74 HeaderVector::const_iterator it = FindHeader(key); | |
| 75 if (it == headers_.end()) | |
| 76 return false; | |
| 77 out->assign(it->value); | |
| 78 return true; | |
| 79 } | |
| 80 | |
| 81 void HttpRequestHeaders::Clear() { | |
| 82 headers_.clear(); | |
| 83 } | |
| 84 | |
| 85 void HttpRequestHeaders::SetHeader(const base::StringPiece& key, | |
| 86 const base::StringPiece& value) { | |
| 87 DCHECK(HttpUtil::IsValidHeaderName(key.as_string())); | |
| 88 DCHECK(HttpUtil::IsValidHeaderValue(value.as_string())); | |
| 89 HeaderVector::iterator it = FindHeader(key); | |
| 90 if (it != headers_.end()) | |
| 91 it->value.assign(value.data(), value.size()); | |
| 92 else | |
| 93 headers_.push_back(HeaderKeyValuePair(key, value)); | |
| 94 } | |
| 95 | |
| 96 void HttpRequestHeaders::SetHeaderIfMissing(const base::StringPiece& key, | |
| 97 const base::StringPiece& value) { | |
| 98 DCHECK(HttpUtil::IsValidHeaderName(key.as_string())); | |
| 99 DCHECK(HttpUtil::IsValidHeaderValue(value.as_string())); | |
| 100 HeaderVector::iterator it = FindHeader(key); | |
| 101 if (it == headers_.end()) | |
| 102 headers_.push_back(HeaderKeyValuePair(key, value)); | |
| 103 } | |
| 104 | |
| 105 void HttpRequestHeaders::RemoveHeader(const base::StringPiece& key) { | |
| 106 HeaderVector::iterator it = FindHeader(key); | |
| 107 if (it != headers_.end()) | |
| 108 headers_.erase(it); | |
| 109 } | |
| 110 | |
| 111 void HttpRequestHeaders::AddHeaderFromString( | |
| 112 const base::StringPiece& header_line) { | |
| 113 DCHECK_EQ(std::string::npos, header_line.find("\r\n")) | |
| 114 << "\"" << header_line << "\" contains CRLF."; | |
| 115 | |
| 116 const std::string::size_type key_end_index = header_line.find(":"); | |
| 117 if (key_end_index == std::string::npos) { | |
| 118 LOG(DFATAL) << "\"" << header_line << "\" is missing colon delimiter."; | |
| 119 return; | |
| 120 } | |
| 121 | |
| 122 if (key_end_index == 0) { | |
| 123 LOG(DFATAL) << "\"" << header_line << "\" is missing header key."; | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 const base::StringPiece header_key(header_line.data(), key_end_index); | |
| 128 | |
| 129 const std::string::size_type value_index = key_end_index + 1; | |
| 130 | |
| 131 if (value_index < header_line.size()) { | |
| 132 std::string header_value(header_line.data() + value_index, | |
| 133 header_line.size() - value_index); | |
| 134 std::string::const_iterator header_value_begin = | |
| 135 header_value.begin(); | |
| 136 std::string::const_iterator header_value_end = | |
| 137 header_value.end(); | |
| 138 HttpUtil::TrimLWS(&header_value_begin, &header_value_end); | |
| 139 | |
| 140 if (header_value_begin == header_value_end) { | |
| 141 // Value was all LWS. | |
| 142 SetHeader(header_key, ""); | |
| 143 } else { | |
| 144 SetHeader(header_key, | |
| 145 base::StringPiece(&*header_value_begin, | |
| 146 header_value_end - header_value_begin)); | |
| 147 } | |
| 148 } else if (value_index == header_line.size()) { | |
| 149 SetHeader(header_key, ""); | |
| 150 } else { | |
| 151 NOTREACHED(); | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 void HttpRequestHeaders::AddHeadersFromString( | |
| 156 const base::StringPiece& headers) { | |
| 157 // TODO(willchan): Consider adding more StringPiece support in string_util.h | |
| 158 // to eliminate copies. | |
| 159 std::vector<std::string> header_line_vector; | |
| 160 base::SplitStringUsingSubstr(headers.as_string(), "\r\n", | |
| 161 &header_line_vector); | |
| 162 for (std::vector<std::string>::const_iterator it = header_line_vector.begin(); | |
| 163 it != header_line_vector.end(); ++it) { | |
| 164 if (!it->empty()) | |
| 165 AddHeaderFromString(*it); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 void HttpRequestHeaders::MergeFrom(const HttpRequestHeaders& other) { | |
| 170 for (HeaderVector::const_iterator it = other.headers_.begin(); | |
| 171 it != other.headers_.end(); ++it ) { | |
| 172 SetHeader(it->key, it->value); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 std::string HttpRequestHeaders::ToString() const { | |
| 177 std::string output; | |
| 178 for (HeaderVector::const_iterator it = headers_.begin(); | |
| 179 it != headers_.end(); ++it) { | |
| 180 if (!it->value.empty()) { | |
| 181 base::StringAppendF(&output, "%s: %s\r\n", | |
| 182 it->key.c_str(), it->value.c_str()); | |
| 183 } else { | |
| 184 base::StringAppendF(&output, "%s:\r\n", it->key.c_str()); | |
| 185 } | |
| 186 } | |
| 187 output.append("\r\n"); | |
| 188 return output; | |
| 189 } | |
| 190 | |
| 191 base::Value* HttpRequestHeaders::NetLogCallback( | |
| 192 const std::string* request_line, | |
| 193 NetLog::LogLevel log_level) const { | |
| 194 base::DictionaryValue* dict = new base::DictionaryValue(); | |
| 195 dict->SetString("line", *request_line); | |
| 196 base::ListValue* headers = new base::ListValue(); | |
| 197 for (HeaderVector::const_iterator it = headers_.begin(); | |
| 198 it != headers_.end(); ++it) { | |
| 199 std::string log_value = ElideHeaderValueForNetLog( | |
| 200 log_level, it->key, it->value); | |
| 201 headers->Append(new base::StringValue( | |
| 202 base::StringPrintf("%s: %s", | |
| 203 it->key.c_str(), log_value.c_str()))); | |
| 204 } | |
| 205 dict->Set("headers", headers); | |
| 206 return dict; | |
| 207 } | |
| 208 | |
| 209 // static | |
| 210 bool HttpRequestHeaders::FromNetLogParam(const base::Value* event_param, | |
| 211 HttpRequestHeaders* headers, | |
| 212 std::string* request_line) { | |
| 213 headers->Clear(); | |
| 214 *request_line = ""; | |
| 215 | |
| 216 const base::DictionaryValue* dict = NULL; | |
| 217 const base::ListValue* header_list = NULL; | |
| 218 | |
| 219 if (!event_param || | |
| 220 !event_param->GetAsDictionary(&dict) || | |
| 221 !dict->GetList("headers", &header_list) || | |
| 222 !dict->GetString("line", request_line)) { | |
| 223 return false; | |
| 224 } | |
| 225 | |
| 226 for (base::ListValue::const_iterator it = header_list->begin(); | |
| 227 it != header_list->end(); | |
| 228 ++it) { | |
| 229 std::string header_line; | |
| 230 if (!(*it)->GetAsString(&header_line)) { | |
| 231 headers->Clear(); | |
| 232 *request_line = ""; | |
| 233 return false; | |
| 234 } | |
| 235 headers->AddHeaderFromString(header_line); | |
| 236 } | |
| 237 return true; | |
| 238 } | |
| 239 | |
| 240 HttpRequestHeaders::HeaderVector::iterator | |
| 241 HttpRequestHeaders::FindHeader(const base::StringPiece& key) { | |
| 242 for (HeaderVector::iterator it = headers_.begin(); | |
| 243 it != headers_.end(); ++it) { | |
| 244 if (key.length() == it->key.length() && | |
| 245 !base::strncasecmp(key.data(), it->key.data(), key.length())) | |
| 246 return it; | |
| 247 } | |
| 248 | |
| 249 return headers_.end(); | |
| 250 } | |
| 251 | |
| 252 HttpRequestHeaders::HeaderVector::const_iterator | |
| 253 HttpRequestHeaders::FindHeader(const base::StringPiece& key) const { | |
| 254 for (HeaderVector::const_iterator it = headers_.begin(); | |
| 255 it != headers_.end(); ++it) { | |
| 256 if (key.length() == it->key.length() && | |
| 257 !base::strncasecmp(key.data(), it->key.data(), key.length())) | |
| 258 return it; | |
| 259 } | |
| 260 | |
| 261 return headers_.end(); | |
| 262 } | |
| 263 | |
| 264 } // namespace net | |
| OLD | NEW |