Home | History | Annotate | Download | only in http
      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   HeaderVector::iterator it = FindHeader(key);
     88   if (it != headers_.end())
     89     it->value.assign(value.data(), value.size());
     90   else
     91     headers_.push_back(HeaderKeyValuePair(key, value));
     92 }
     93 
     94 void HttpRequestHeaders::SetHeaderIfMissing(const base::StringPiece& key,
     95                                             const base::StringPiece& value) {
     96   HeaderVector::iterator it = FindHeader(key);
     97   if (it == headers_.end())
     98     headers_.push_back(HeaderKeyValuePair(key, value));
     99 }
    100 
    101 void HttpRequestHeaders::RemoveHeader(const base::StringPiece& key) {
    102   HeaderVector::iterator it = FindHeader(key);
    103   if (it != headers_.end())
    104     headers_.erase(it);
    105 }
    106 
    107 void HttpRequestHeaders::AddHeaderFromString(
    108     const base::StringPiece& header_line) {
    109   DCHECK_EQ(std::string::npos, header_line.find("\r\n"))
    110       << "\"" << header_line << "\" contains CRLF.";
    111 
    112   const std::string::size_type key_end_index = header_line.find(":");
    113   if (key_end_index == std::string::npos) {
    114     LOG(DFATAL) << "\"" << header_line << "\" is missing colon delimiter.";
    115     return;
    116   }
    117 
    118   if (key_end_index == 0) {
    119     LOG(DFATAL) << "\"" << header_line << "\" is missing header key.";
    120     return;
    121   }
    122 
    123   const base::StringPiece header_key(header_line.data(), key_end_index);
    124 
    125   const std::string::size_type value_index = key_end_index + 1;
    126 
    127   if (value_index < header_line.size()) {
    128     std::string header_value(header_line.data() + value_index,
    129                              header_line.size() - value_index);
    130     std::string::const_iterator header_value_begin =
    131         header_value.begin();
    132     std::string::const_iterator header_value_end =
    133         header_value.end();
    134     HttpUtil::TrimLWS(&header_value_begin, &header_value_end);
    135 
    136     if (header_value_begin == header_value_end) {
    137       // Value was all LWS.
    138       SetHeader(header_key, "");
    139     } else {
    140       SetHeader(header_key,
    141                 base::StringPiece(&*header_value_begin,
    142                                   header_value_end - header_value_begin));
    143     }
    144   } else if (value_index == header_line.size()) {
    145     SetHeader(header_key, "");
    146   } else {
    147     NOTREACHED();
    148   }
    149 }
    150 
    151 void HttpRequestHeaders::AddHeadersFromString(
    152     const base::StringPiece& headers) {
    153   // TODO(willchan): Consider adding more StringPiece support in string_util.h
    154   // to eliminate copies.
    155   std::vector<std::string> header_line_vector;
    156   base::SplitStringUsingSubstr(headers.as_string(), "\r\n",
    157                                &header_line_vector);
    158   for (std::vector<std::string>::const_iterator it = header_line_vector.begin();
    159        it != header_line_vector.end(); ++it) {
    160     if (!it->empty())
    161       AddHeaderFromString(*it);
    162   }
    163 }
    164 
    165 void HttpRequestHeaders::MergeFrom(const HttpRequestHeaders& other) {
    166   for (HeaderVector::const_iterator it = other.headers_.begin();
    167        it != other.headers_.end(); ++it ) {
    168     SetHeader(it->key, it->value);
    169   }
    170 }
    171 
    172 std::string HttpRequestHeaders::ToString() const {
    173   std::string output;
    174   for (HeaderVector::const_iterator it = headers_.begin();
    175        it != headers_.end(); ++it) {
    176     if (!it->value.empty()) {
    177       base::StringAppendF(&output, "%s: %s\r\n",
    178                           it->key.c_str(), it->value.c_str());
    179     } else {
    180       base::StringAppendF(&output, "%s:\r\n", it->key.c_str());
    181     }
    182   }
    183   output.append("\r\n");
    184   return output;
    185 }
    186 
    187 base::Value* HttpRequestHeaders::NetLogCallback(
    188     const std::string* request_line,
    189     NetLog::LogLevel log_level) const {
    190   base::DictionaryValue* dict = new base::DictionaryValue();
    191   dict->SetString("line", *request_line);
    192   base::ListValue* headers = new base::ListValue();
    193   for (HeaderVector::const_iterator it = headers_.begin();
    194        it != headers_.end(); ++it) {
    195     std::string log_value = ElideHeaderValueForNetLog(
    196         log_level, it->key, it->value);
    197     headers->Append(new base::StringValue(
    198         base::StringPrintf("%s: %s",
    199                            it->key.c_str(), log_value.c_str())));
    200   }
    201   dict->Set("headers", headers);
    202   return dict;
    203 }
    204 
    205 // static
    206 bool HttpRequestHeaders::FromNetLogParam(const base::Value* event_param,
    207                                          HttpRequestHeaders* headers,
    208                                          std::string* request_line) {
    209   headers->Clear();
    210   *request_line = "";
    211 
    212   const base::DictionaryValue* dict = NULL;
    213   const base::ListValue* header_list = NULL;
    214 
    215   if (!event_param ||
    216       !event_param->GetAsDictionary(&dict) ||
    217       !dict->GetList("headers", &header_list) ||
    218       !dict->GetString("line", request_line)) {
    219     return false;
    220   }
    221 
    222   for (base::ListValue::const_iterator it = header_list->begin();
    223        it != header_list->end();
    224        ++it) {
    225     std::string header_line;
    226     if (!(*it)->GetAsString(&header_line)) {
    227       headers->Clear();
    228       *request_line = "";
    229       return false;
    230     }
    231     headers->AddHeaderFromString(header_line);
    232   }
    233   return true;
    234 }
    235 
    236 HttpRequestHeaders::HeaderVector::iterator
    237 HttpRequestHeaders::FindHeader(const base::StringPiece& key) {
    238   for (HeaderVector::iterator it = headers_.begin();
    239        it != headers_.end(); ++it) {
    240     if (key.length() == it->key.length() &&
    241         !base::strncasecmp(key.data(), it->key.data(), key.length()))
    242       return it;
    243   }
    244 
    245   return headers_.end();
    246 }
    247 
    248 HttpRequestHeaders::HeaderVector::const_iterator
    249 HttpRequestHeaders::FindHeader(const base::StringPiece& key) const {
    250   for (HeaderVector::const_iterator it = headers_.begin();
    251        it != headers_.end(); ++it) {
    252     if (key.length() == it->key.length() &&
    253         !base::strncasecmp(key.data(), it->key.data(), key.length()))
    254       return it;
    255   }
    256 
    257   return headers_.end();
    258 }
    259 
    260 }  // namespace net
    261