| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/values.h" |
| 11 #include "net/http/http_util.h" | 12 #include "net/http/http_util.h" |
| 12 | 13 |
| 13 namespace net { | 14 namespace net { |
| 14 | 15 |
| 15 const char HttpRequestHeaders::kGetMethod[] = "GET"; | 16 const char HttpRequestHeaders::kGetMethod[] = "GET"; |
| 16 const char HttpRequestHeaders::kAcceptCharset[] = "Accept-Charset"; | 17 const char HttpRequestHeaders::kAcceptCharset[] = "Accept-Charset"; |
| 17 const char HttpRequestHeaders::kAcceptEncoding[] = "Accept-Encoding"; | 18 const char HttpRequestHeaders::kAcceptEncoding[] = "Accept-Encoding"; |
| 18 const char HttpRequestHeaders::kAcceptLanguage[] = "Accept-Language"; | 19 const char HttpRequestHeaders::kAcceptLanguage[] = "Accept-Language"; |
| 19 const char HttpRequestHeaders::kAuthorization[] = "Authorization"; | 20 const char HttpRequestHeaders::kAuthorization[] = "Authorization"; |
| 20 const char HttpRequestHeaders::kCacheControl[] = "Cache-Control"; | 21 const char HttpRequestHeaders::kCacheControl[] = "Cache-Control"; |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 base::StringAppendF(&output, "%s: %s\r\n", | 176 base::StringAppendF(&output, "%s: %s\r\n", |
| 176 it->key.c_str(), it->value.c_str()); | 177 it->key.c_str(), it->value.c_str()); |
| 177 } else { | 178 } else { |
| 178 base::StringAppendF(&output, "%s:\r\n", it->key.c_str()); | 179 base::StringAppendF(&output, "%s:\r\n", it->key.c_str()); |
| 179 } | 180 } |
| 180 } | 181 } |
| 181 output.append("\r\n"); | 182 output.append("\r\n"); |
| 182 return output; | 183 return output; |
| 183 } | 184 } |
| 184 | 185 |
| 186 Value* HttpRequestHeaders::NetLogCallback( |
| 187 const std::string* request_line, |
| 188 NetLog::LogLevel /* log_level */) const { |
| 189 DictionaryValue* dict = new DictionaryValue(); |
| 190 dict->SetString("line", *request_line); |
| 191 ListValue* headers = new ListValue(); |
| 192 for (HeaderVector::const_iterator it = headers_.begin(); |
| 193 it != headers_.end(); ++it) { |
| 194 headers->Append( |
| 195 new StringValue(base::StringPrintf("%s: %s", |
| 196 it->key.c_str(), |
| 197 it->value.c_str()))); |
| 198 } |
| 199 dict->Set("headers", headers); |
| 200 return dict; |
| 201 } |
| 202 |
| 203 // static |
| 204 bool HttpRequestHeaders::FromNetLogParam(const base::Value* event_param, |
| 205 HttpRequestHeaders* headers, |
| 206 std::string* request_line) { |
| 207 headers->Clear(); |
| 208 *request_line = ""; |
| 209 |
| 210 const base::DictionaryValue* dict; |
| 211 base::ListValue* header_list; |
| 212 |
| 213 if (!event_param || |
| 214 !event_param->GetAsDictionary(&dict) || |
| 215 !dict->GetList("headers", &header_list) || |
| 216 !dict->GetString("line", request_line)) { |
| 217 return false; |
| 218 } |
| 219 |
| 220 for (base::ListValue::const_iterator it = header_list->begin(); |
| 221 it != header_list->end(); |
| 222 ++it) { |
| 223 std::string header_line; |
| 224 if (!(*it)->GetAsString(&header_line)) { |
| 225 headers->Clear(); |
| 226 *request_line = ""; |
| 227 return false; |
| 228 } |
| 229 headers->AddHeaderFromString(header_line); |
| 230 } |
| 231 return true; |
| 232 } |
| 233 |
| 185 HttpRequestHeaders::HeaderVector::iterator | 234 HttpRequestHeaders::HeaderVector::iterator |
| 186 HttpRequestHeaders::FindHeader(const base::StringPiece& key) { | 235 HttpRequestHeaders::FindHeader(const base::StringPiece& key) { |
| 187 for (HeaderVector::iterator it = headers_.begin(); | 236 for (HeaderVector::iterator it = headers_.begin(); |
| 188 it != headers_.end(); ++it) { | 237 it != headers_.end(); ++it) { |
| 189 if (key.length() == it->key.length() && | 238 if (key.length() == it->key.length() && |
| 190 !base::strncasecmp(key.data(), it->key.data(), key.length())) | 239 !base::strncasecmp(key.data(), it->key.data(), key.length())) |
| 191 return it; | 240 return it; |
| 192 } | 241 } |
| 193 | 242 |
| 194 return headers_.end(); | 243 return headers_.end(); |
| 195 } | 244 } |
| 196 | 245 |
| 197 HttpRequestHeaders::HeaderVector::const_iterator | 246 HttpRequestHeaders::HeaderVector::const_iterator |
| 198 HttpRequestHeaders::FindHeader(const base::StringPiece& key) const { | 247 HttpRequestHeaders::FindHeader(const base::StringPiece& key) const { |
| 199 for (HeaderVector::const_iterator it = headers_.begin(); | 248 for (HeaderVector::const_iterator it = headers_.begin(); |
| 200 it != headers_.end(); ++it) { | 249 it != headers_.end(); ++it) { |
| 201 if (key.length() == it->key.length() && | 250 if (key.length() == it->key.length() && |
| 202 !base::strncasecmp(key.data(), it->key.data(), key.length())) | 251 !base::strncasecmp(key.data(), it->key.data(), key.length())) |
| 203 return it; | 252 return it; |
| 204 } | 253 } |
| 205 | 254 |
| 206 return headers_.end(); | 255 return headers_.end(); |
| 207 } | 256 } |
| 208 | 257 |
| 209 } // namespace net | 258 } // namespace net |
| OLD | NEW |