Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 "buffet/http_request.h" |
| 6 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 7 | #include <base/logging.h> |
| 8 | |
| 9 | #include "buffet/http_connection_curl.h" |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 10 | #include "buffet/http_transport_curl.h" |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 11 | #include "buffet/map_utils.h" |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 12 | #include "buffet/mime_utils.h" |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 13 | #include "buffet/string_utils.h" |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 14 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 15 | namespace buffet { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 16 | namespace http { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 17 | |
| 18 | // request_type |
| 19 | const char request_type::kOptions[] = "OPTIONS"; |
| 20 | const char request_type::kGet[] = "GET"; |
| 21 | const char request_type::kHead[] = "HEAD"; |
| 22 | const char request_type::kPost[] = "POST"; |
| 23 | const char request_type::kPut[] = "PUT"; |
| 24 | const char request_type::kPatch[] = "PATCH"; |
| 25 | const char request_type::kDelete[] = "DELETE"; |
| 26 | const char request_type::kTrace[] = "TRACE"; |
| 27 | const char request_type::kConnect[] = "CONNECT"; |
| 28 | const char request_type::kCopy[] = "COPY"; |
| 29 | const char request_type::kMove[] = "MOVE"; |
| 30 | |
| 31 | // request_header |
| 32 | const char request_header::kAccept[] = "Accept"; |
| 33 | const char request_header::kAcceptCharset[] = "Accept-Charset"; |
| 34 | const char request_header::kAcceptEncoding[] = "Accept-Encoding"; |
| 35 | const char request_header::kAcceptLanguage[] = "Accept-Language"; |
| 36 | const char request_header::kAllow[] = "Allow"; |
| 37 | const char request_header::kAuthorization[] = "Authorization"; |
| 38 | const char request_header::kCacheControl[] = "Cache-Control"; |
| 39 | const char request_header::kConnection[] = "Connection"; |
| 40 | const char request_header::kContentEncoding[] = "Content-Encoding"; |
| 41 | const char request_header::kContentLanguage[] = "Content-Language"; |
| 42 | const char request_header::kContentLength[] = "Content-Length"; |
| 43 | const char request_header::kContentLocation[] = "Content-Location"; |
| 44 | const char request_header::kContentMd5[] = "Content-MD5"; |
| 45 | const char request_header::kContentRange[] = "Content-Range"; |
| 46 | const char request_header::kContentType[] = "Content-Type"; |
| 47 | const char request_header::kCookie[] = "Cookie"; |
| 48 | const char request_header::kDate[] = "Date"; |
| 49 | const char request_header::kExpect[] = "Expect"; |
| 50 | const char request_header::kExpires[] = "Expires"; |
| 51 | const char request_header::kFrom[] = "From"; |
| 52 | const char request_header::kHost[] = "Host"; |
| 53 | const char request_header::kIfMatch[] = "If-Match"; |
| 54 | const char request_header::kIfModifiedSince[] = "If-Modified-Since"; |
| 55 | const char request_header::kIfNoneMatch[] = "If-None-Match"; |
| 56 | const char request_header::kIfRange[] = "If-Range"; |
| 57 | const char request_header::kIfUnmodifiedSince[] = "If-Unmodified-Since"; |
| 58 | const char request_header::kLastModified[] = "Last-Modified"; |
| 59 | const char request_header::kMaxForwards[] = "Max-Forwards"; |
| 60 | const char request_header::kPragma[] = "Pragma"; |
| 61 | const char request_header::kProxyAuthorization[] = "Proxy-Authorization"; |
| 62 | const char request_header::kRange[] = "Range"; |
| 63 | const char request_header::kReferer[] = "Referer"; |
| 64 | const char request_header::kTE[] = "TE"; |
| 65 | const char request_header::kTrailer[] = "Trailer"; |
| 66 | const char request_header::kTransferEncoding[] = "Transfer-Encoding"; |
| 67 | const char request_header::kUpgrade[] = "Upgrade"; |
| 68 | const char request_header::kUserAgent[] = "User-Agent"; |
| 69 | const char request_header::kVia[] = "Via"; |
| 70 | const char request_header::kWarning[] = "Warning"; |
| 71 | |
| 72 | // response_header |
| 73 | const char response_header::kAcceptRanges[] = "Accept-Ranges"; |
| 74 | const char response_header::kAge[] = "Age"; |
| 75 | const char response_header::kAllow[] = "Allow"; |
| 76 | const char response_header::kCacheControl[] = "Cache-Control"; |
| 77 | const char response_header::kConnection[] = "Connection"; |
| 78 | const char response_header::kContentEncoding[] = "Content-Encoding"; |
| 79 | const char response_header::kContentLanguage[] = "Content-Language"; |
| 80 | const char response_header::kContentLength[] = "Content-Length"; |
| 81 | const char response_header::kContentLocation[] = "Content-Location"; |
| 82 | const char response_header::kContentMd5[] = "Content-MD5"; |
| 83 | const char response_header::kContentRange[] = "Content-Range"; |
| 84 | const char response_header::kContentType[] = "Content-Type"; |
| 85 | const char response_header::kDate[] = "Date"; |
| 86 | const char response_header::kETag[] = "ETag"; |
| 87 | const char response_header::kExpires[] = "Expires"; |
| 88 | const char response_header::kLastModified[] = "Last-Modified"; |
| 89 | const char response_header::kLocation[] = "Location"; |
| 90 | const char response_header::kPragma[] = "Pragma"; |
| 91 | const char response_header::kProxyAuthenticate[] = "Proxy-Authenticate"; |
| 92 | const char response_header::kRetryAfter[] = "Retry-After"; |
| 93 | const char response_header::kServer[] = "Server"; |
| 94 | const char response_header::kSetCookie[] = "Set-Cookie"; |
| 95 | const char response_header::kTrailer[] = "Trailer"; |
| 96 | const char response_header::kTransferEncoding[] = "Transfer-Encoding"; |
| 97 | const char response_header::kUpgrade[] = "Upgrade"; |
| 98 | const char response_header::kVary[] = "Vary"; |
| 99 | const char response_header::kVia[] = "Via"; |
| 100 | const char response_header::kWarning[] = "Warning"; |
| 101 | const char response_header::kWwwAuthenticate[] = "WWW-Authenticate"; |
| 102 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 103 | // *********************************************************** |
| 104 | // ********************** Request Class ********************** |
| 105 | // *********************************************************** |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 106 | Request::Request(const std::string& url, const char* method, |
| 107 | std::shared_ptr<Transport> transport) : |
| 108 | transport_(transport), request_url_(url), method_(method) { |
| 109 | VLOG(1) << "http::Request created"; |
| 110 | if (!transport_) |
| 111 | transport_.reset(new http::curl::Transport()); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 114 | Request::~Request() { |
| 115 | VLOG(1) << "http::Request destroyed"; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void Request::AddRange(int64_t bytes) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 119 | if (bytes < 0) { |
| 120 | ranges_.emplace_back(Request::range_value_omitted, -bytes); |
| 121 | } else { |
| 122 | ranges_.emplace_back(bytes, Request::range_value_omitted); |
| 123 | } |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void Request::AddRange(uint64_t from_byte, uint64_t to_byte) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 127 | ranges_.emplace_back(from_byte, to_byte); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 130 | std::unique_ptr<Response> Request::GetResponse(ErrorPtr* error) { |
| 131 | if (!SendRequestIfNeeded(error) || !connection_->FinishRequest(error)) |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 132 | return std::unique_ptr<Response>(); |
| 133 | std::unique_ptr<Response> response(new Response(std::move(connection_))); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 134 | transport_.reset(); // Indicate that the response has been received |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 135 | return response; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 138 | void Request::SetAccept(const char* accept_mime_types) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 139 | accept_ = accept_mime_types; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | std::string Request::GetAccept() const { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 143 | return accept_; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 146 | void Request::SetContentType(const char* contentType) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 147 | content_type_ = contentType; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | std::string Request::GetContentType() const { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 151 | return content_type_; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 154 | void Request::AddHeader(const char* header, const char* value) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 155 | headers_[header] = value; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 158 | void Request::AddHeaders(const HeaderList& headers) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 159 | headers_.insert(headers.begin(), headers.end()); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 162 | bool Request::AddRequestBody(const void* data, size_t size, ErrorPtr* error) { |
| 163 | if (!SendRequestIfNeeded(error)) |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 164 | return false; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 165 | return connection_->WriteRequestData(data, size, error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 168 | void Request::SetReferer(const char* referer) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 169 | referer_ = referer; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | std::string Request::GetReferer() const { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 173 | return referer_; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 176 | void Request::SetUserAgent(const char* user_agent) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 177 | user_agent_ = user_agent; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | std::string Request::GetUserAgent() const { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 181 | return user_agent_; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 184 | bool Request::SendRequestIfNeeded(ErrorPtr* error) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 185 | if (transport_) { |
| 186 | if (!connection_) { |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 187 | http::HeaderList headers = MapToVector(headers_); |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 188 | std::vector<std::string> ranges; |
| 189 | if (method_ != request_type::kHead) { |
| 190 | ranges.reserve(ranges_.size()); |
| 191 | for (auto p : ranges_) { |
| 192 | if (p.first != range_value_omitted || |
| 193 | p.second != range_value_omitted) { |
| 194 | std::string range; |
| 195 | if (p.first != range_value_omitted) { |
Alex Vakulenko | 96c84d3 | 2014-06-06 11:07:32 -0700 | [diff] [blame] | 196 | range = string_utils::ToString(p.first); |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 197 | } |
| 198 | range += '-'; |
| 199 | if (p.second != range_value_omitted) { |
Alex Vakulenko | 96c84d3 | 2014-06-06 11:07:32 -0700 | [diff] [blame] | 200 | range += string_utils::ToString(p.second); |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 201 | } |
| 202 | ranges.push_back(range); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | if (!ranges.empty()) |
| 207 | headers.emplace_back(request_header::kRange, |
| 208 | "bytes=" + string_utils::Join(',', ranges)); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 209 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 210 | headers.emplace_back(request_header::kAccept, GetAccept()); |
| 211 | if (method_ != request_type::kGet && method_ != request_type::kHead) { |
| 212 | if (!content_type_.empty()) |
| 213 | headers.emplace_back(request_header::kContentType, content_type_); |
| 214 | } |
| 215 | connection_ = transport_->CreateConnection(transport_, request_url_, |
| 216 | method_, headers, |
| 217 | user_agent_, referer_, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 218 | error); |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | if (connection_) |
| 222 | return true; |
| 223 | } else { |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 224 | Error::AddTo(error, http::curl::kErrorDomain, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 225 | "request_already_received", "HTTP response already received"); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 226 | } |
| 227 | return false; |
| 228 | } |
| 229 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 230 | // ************************************************************ |
| 231 | // ********************** Response Class ********************** |
| 232 | // ************************************************************ |
| 233 | Response::Response(std::unique_ptr<Connection> connection) |
| 234 | : connection_(std::move(connection)) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 235 | VLOG(1) << "http::Response created"; |
| 236 | // Response object doesn't have streaming interface for response data (yet), |
| 237 | // so read the data into a buffer and cache it. |
| 238 | if (connection_) { |
| 239 | size_t size = static_cast<size_t>(connection_->GetResponseDataSize()); |
| 240 | response_data_.reserve(size); |
| 241 | unsigned char buffer[1024]; |
| 242 | size_t read = 0; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 243 | while (connection_->ReadResponseData(buffer, sizeof(buffer), |
| 244 | &read, nullptr) && read > 0) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 245 | response_data_.insert(response_data_.end(), buffer, buffer + read); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | Response::~Response() { |
| 251 | VLOG(1) << "http::Response destroyed"; |
| 252 | } |
| 253 | |
| 254 | bool Response::IsSuccessful() const { |
| 255 | int code = GetStatusCode(); |
| 256 | return code >= status_code::Continue && code < status_code::BadRequest; |
| 257 | } |
| 258 | |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 259 | int Response::GetStatusCode() const { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 260 | if (!connection_) |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 261 | return -1; |
| 262 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 263 | return connection_->GetResponseStatusCode(); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | std::string Response::GetStatusText() const { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 267 | if (!connection_) |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 268 | return std::string(); |
| 269 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 270 | return connection_->GetResponseStatusText(); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | std::string Response::GetContentType() const { |
| 274 | return GetHeader(response_header::kContentType); |
| 275 | } |
| 276 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 277 | const std::vector<unsigned char>& Response::GetData() const { |
| 278 | return response_data_; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | std::string Response::GetDataAsString() const { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 282 | if (response_data_.empty()) |
| 283 | return std::string(); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 284 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 285 | const char* data_buf = reinterpret_cast<const char*>(response_data_.data()); |
| 286 | return std::string(data_buf, data_buf + response_data_.size()); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 289 | std::string Response::GetHeader(const char* header_name) const { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 290 | if (connection_) |
| 291 | return connection_->GetResponseHeader(header_name); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 292 | |
| 293 | return std::string(); |
| 294 | } |
| 295 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 296 | } // namespace http |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 297 | } // namespace buffet |