Chromium Code Reviews| 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 bool HttpRequestHeaders::FromNetLogParam(const base::Value* event_param, | |
| 204 HttpRequestHeaders* headers, | |
| 205 std::string* request_line) { | |
| 206 headers->Clear(); | |
| 207 *request_line = ""; | |
| 208 | |
| 209 const base::DictionaryValue *dict; | |
|
eroman
2012/06/06 21:27:11
nit: asterisk on left
mmenke
2012/06/07 19:20:46
Done.
| |
| 210 // Not const because there's no overload of Dictionary::GetList that takes | |
| 211 // a const list. | |
| 212 base::ListValue *header_list; | |
|
eroman
2012/06/06 21:27:11
nit: asterisk on left
mmenke
2012/06/07 19:20:46
Done.
| |
| 213 | |
| 214 if (!event_param || | |
| 215 !event_param->GetAsDictionary(&dict) || | |
| 216 !dict->GetList("headers", &header_list) || | |
| 217 !dict->GetString("line", request_line)) { | |
| 218 return false; | |
| 219 } | |
| 220 | |
| 221 for (base::ListValue::const_iterator it = header_list->begin(); | |
| 222 it != header_list->end(); | |
| 223 ++it) { | |
| 224 std::string header_line; | |
| 225 if (!(*it)->GetAsString(&header_line)) { | |
| 226 headers->Clear(); | |
| 227 *request_line = ""; | |
| 228 return false; | |
| 229 } | |
| 230 headers->AddHeaderFromString(header_line); | |
| 231 } | |
| 232 return true; | |
| 233 } | |
| 234 | |
| 185 HttpRequestHeaders::HeaderVector::iterator | 235 HttpRequestHeaders::HeaderVector::iterator |
| 186 HttpRequestHeaders::FindHeader(const base::StringPiece& key) { | 236 HttpRequestHeaders::FindHeader(const base::StringPiece& key) { |
| 187 for (HeaderVector::iterator it = headers_.begin(); | 237 for (HeaderVector::iterator it = headers_.begin(); |
| 188 it != headers_.end(); ++it) { | 238 it != headers_.end(); ++it) { |
| 189 if (key.length() == it->key.length() && | 239 if (key.length() == it->key.length() && |
| 190 !base::strncasecmp(key.data(), it->key.data(), key.length())) | 240 !base::strncasecmp(key.data(), it->key.data(), key.length())) |
| 191 return it; | 241 return it; |
| 192 } | 242 } |
| 193 | 243 |
| 194 return headers_.end(); | 244 return headers_.end(); |
| 195 } | 245 } |
| 196 | 246 |
| 197 HttpRequestHeaders::HeaderVector::const_iterator | 247 HttpRequestHeaders::HeaderVector::const_iterator |
| 198 HttpRequestHeaders::FindHeader(const base::StringPiece& key) const { | 248 HttpRequestHeaders::FindHeader(const base::StringPiece& key) const { |
| 199 for (HeaderVector::const_iterator it = headers_.begin(); | 249 for (HeaderVector::const_iterator it = headers_.begin(); |
| 200 it != headers_.end(); ++it) { | 250 it != headers_.end(); ++it) { |
| 201 if (key.length() == it->key.length() && | 251 if (key.length() == it->key.length() && |
| 202 !base::strncasecmp(key.data(), it->key.data(), key.length())) | 252 !base::strncasecmp(key.data(), it->key.data(), key.length())) |
| 203 return it; | 253 return it; |
| 204 } | 254 } |
| 205 | 255 |
| 206 return headers_.end(); | 256 return headers_.end(); |
| 207 } | 257 } |
| 208 | 258 |
| 209 } // namespace net | 259 } // namespace net |
| OLD | NEW |