1 // Copyright (c) 2010 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/string_split.h" 9 #include "base/string_util.h" 10 #include "net/http/http_util.h" 11 12 namespace net { 13 14 const char HttpRequestHeaders::kGetMethod[] = "GET"; 15 const char HttpRequestHeaders::kAcceptCharset[] = "Accept-Charset"; 16 const char HttpRequestHeaders::kAcceptEncoding[] = "Accept-Encoding"; 17 const char HttpRequestHeaders::kAcceptLanguage[] = "Accept-Language"; 18 const char HttpRequestHeaders::kCacheControl[] = "Cache-Control"; 19 const char HttpRequestHeaders::kConnection[] = "Connection"; 20 const char HttpRequestHeaders::kContentLength[] = "Content-Length"; 21 const char HttpRequestHeaders::kContentType[] = "Content-Type"; 22 const char HttpRequestHeaders::kCookie[] = "Cookie"; 23 const char HttpRequestHeaders::kHost[] = "Host"; 24 const char HttpRequestHeaders::kIfModifiedSince[] = "If-Modified-Since"; 25 const char HttpRequestHeaders::kIfNoneMatch[] = "If-None-Match"; 26 const char HttpRequestHeaders::kIfRange[] = "If-Range"; 27 const char HttpRequestHeaders::kOrigin[] = "Origin"; 28 const char HttpRequestHeaders::kPragma[] = "Pragma"; 29 const char HttpRequestHeaders::kProxyConnection[] = "Proxy-Connection"; 30 const char HttpRequestHeaders::kRange[] = "Range"; 31 const char HttpRequestHeaders::kReferer[] = "Referer"; 32 const char HttpRequestHeaders::kUserAgent[] = "User-Agent"; 33 const char HttpRequestHeaders::kTransferEncoding[] = "Transfer-Encoding"; 34 35 HttpRequestHeaders::HeaderKeyValuePair::HeaderKeyValuePair() { 36 } 37 38 HttpRequestHeaders::HeaderKeyValuePair::HeaderKeyValuePair( 39 const base::StringPiece& key, const base::StringPiece& value) 40 : key(key.data(), key.size()), value(value.data(), value.size()) { 41 } 42 43 44 HttpRequestHeaders::Iterator::Iterator(const HttpRequestHeaders& headers) 45 : started_(false), 46 curr_(headers.headers_.begin()), 47 end_(headers.headers_.end()) {} 48 49 HttpRequestHeaders::Iterator::~Iterator() {} 50 51 bool HttpRequestHeaders::Iterator::GetNext() { 52 if (!started_) { 53 started_ = true; 54 return curr_ != end_; 55 } 56 57 if (curr_ == end_) 58 return false; 59 60 ++curr_; 61 return curr_ != end_; 62 } 63 64 HttpRequestHeaders::HttpRequestHeaders() {} 65 HttpRequestHeaders::~HttpRequestHeaders() {} 66 67 bool HttpRequestHeaders::GetHeader(const base::StringPiece& key, 68 std::string* out) const { 69 HeaderVector::const_iterator it = FindHeader(key); 70 if (it == headers_.end()) 71 return false; 72 out->assign(it->value); 73 return true; 74 } 75 76 void HttpRequestHeaders::Clear() { 77 headers_.clear(); 78 } 79 80 void HttpRequestHeaders::SetHeader(const base::StringPiece& key, 81 const base::StringPiece& value) { 82 HeaderVector::iterator it = FindHeader(key); 83 if (it != headers_.end()) 84 it->value = value.as_string(); 85 else 86 headers_.push_back(HeaderKeyValuePair(key.as_string(), value.as_string())); 87 } 88 89 void HttpRequestHeaders::SetHeaderIfMissing(const base::StringPiece& key, 90 const base::StringPiece& value) { 91 HeaderVector::iterator it = FindHeader(key); 92 if (it == headers_.end()) 93 headers_.push_back(HeaderKeyValuePair(key.as_string(), value.as_string())); 94 } 95 96 void HttpRequestHeaders::RemoveHeader(const base::StringPiece& key) { 97 HeaderVector::iterator it = FindHeader(key); 98 if (it != headers_.end()) 99 headers_.erase(it); 100 } 101 102 void HttpRequestHeaders::AddHeaderFromString( 103 const base::StringPiece& header_line) { 104 DCHECK_EQ(std::string::npos, header_line.find("\r\n")) 105 << "\"" << header_line << "\" contains CRLF."; 106 107 const std::string::size_type key_end_index = header_line.find(":"); 108 if (key_end_index == std::string::npos) { 109 LOG(DFATAL) << "\"" << header_line << "\" is missing colon delimiter."; 110 return; 111 } 112 113 if (key_end_index == 0) { 114 LOG(DFATAL) << "\"" << header_line << "\" is missing header key."; 115 return; 116 } 117 118 const base::StringPiece header_key(header_line.data(), key_end_index); 119 120 const std::string::size_type value_index = key_end_index + 1; 121 122 if (value_index < header_line.size()) { 123 std::string header_value(header_line.data() + value_index, 124 header_line.size() - value_index); 125 std::string::const_iterator header_value_begin = 126 header_value.begin(); 127 std::string::const_iterator header_value_end = 128 header_value.end(); 129 HttpUtil::TrimLWS(&header_value_begin, &header_value_end); 130 131 if (header_value_begin == header_value_end) { 132 // Value was all LWS. 133 SetHeader(header_key, ""); 134 } else { 135 SetHeader(header_key, 136 base::StringPiece(&*header_value_begin, 137 header_value_end - header_value_begin)); 138 } 139 } else if (value_index == header_line.size()) { 140 SetHeader(header_key, ""); 141 } else { 142 NOTREACHED(); 143 } 144 } 145 146 void HttpRequestHeaders::AddHeadersFromString( 147 const base::StringPiece& headers) { 148 // TODO(willchan): Consider adding more StringPiece support in string_util.h 149 // to eliminate copies. 150 std::vector<std::string> header_line_vector; 151 base::SplitStringUsingSubstr(headers.as_string(), "\r\n", 152 &header_line_vector); 153 for (std::vector<std::string>::const_iterator it = header_line_vector.begin(); 154 it != header_line_vector.end(); ++it) { 155 if (!it->empty()) 156 AddHeaderFromString(*it); 157 } 158 } 159 160 void HttpRequestHeaders::MergeFrom(const HttpRequestHeaders& other) { 161 for (HeaderVector::const_iterator it = other.headers_.begin(); 162 it != other.headers_.end(); ++it ) { 163 SetHeader(it->key, it->value); 164 } 165 } 166 167 std::string HttpRequestHeaders::ToString() const { 168 std::string output; 169 for (HeaderVector::const_iterator it = headers_.begin(); 170 it != headers_.end(); ++it) { 171 if (!it->value.empty()) { 172 base::StringAppendF(&output, "%s: %s\r\n", 173 it->key.c_str(), it->value.c_str()); 174 } else { 175 base::StringAppendF(&output, "%s:\r\n", it->key.c_str()); 176 } 177 } 178 output.append("\r\n"); 179 return output; 180 } 181 182 HttpRequestHeaders::HeaderVector::iterator 183 HttpRequestHeaders::FindHeader(const base::StringPiece& key) { 184 for (HeaderVector::iterator it = headers_.begin(); 185 it != headers_.end(); ++it) { 186 if (key.length() == it->key.length() && 187 !base::strncasecmp(key.data(), it->key.data(), key.length())) 188 return it; 189 } 190 191 return headers_.end(); 192 } 193 194 HttpRequestHeaders::HeaderVector::const_iterator 195 HttpRequestHeaders::FindHeader(const base::StringPiece& key) const { 196 for (HeaderVector::const_iterator it = headers_.begin(); 197 it != headers_.end(); ++it) { 198 if (key.length() == it->key.length() && 199 !base::strncasecmp(key.data(), it->key.data(), key.length())) 200 return it; 201 } 202 203 return headers_.end(); 204 } 205 206 } // namespace net 207