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