henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <time.h> |
| 12 | |
| 13 | #include "webrtc/base/httpcommon-inl.h" |
| 14 | |
| 15 | #include "webrtc/base/asyncsocket.h" |
| 16 | #include "webrtc/base/common.h" |
| 17 | #include "webrtc/base/diskcache.h" |
| 18 | #include "webrtc/base/httpclient.h" |
| 19 | #include "webrtc/base/logging.h" |
| 20 | #include "webrtc/base/pathutils.h" |
andresp@webrtc.org | f7e5f22 | 2014-09-12 13:35:05 +0000 | [diff] [blame] | 21 | #include "webrtc/base/scoped_ptr.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | #include "webrtc/base/socketstream.h" |
| 23 | #include "webrtc/base/stringencode.h" |
| 24 | #include "webrtc/base/stringutils.h" |
| 25 | #include "webrtc/base/thread.h" |
| 26 | |
| 27 | namespace rtc { |
| 28 | |
| 29 | ////////////////////////////////////////////////////////////////////// |
| 30 | // Helpers |
| 31 | ////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | const size_t kCacheHeader = 0; |
| 36 | const size_t kCacheBody = 1; |
| 37 | |
| 38 | // Convert decimal string to integer |
| 39 | bool HttpStringToUInt(const std::string& str, size_t* val) { |
| 40 | ASSERT(NULL != val); |
| 41 | char* eos = NULL; |
| 42 | *val = strtoul(str.c_str(), &eos, 10); |
| 43 | return (*eos == '\0'); |
| 44 | } |
| 45 | |
| 46 | bool HttpShouldCache(const HttpTransaction& t) { |
| 47 | bool verb_allows_cache = (t.request.verb == HV_GET) |
| 48 | || (t.request.verb == HV_HEAD); |
| 49 | bool is_range_response = t.response.hasHeader(HH_CONTENT_RANGE, NULL); |
| 50 | bool has_expires = t.response.hasHeader(HH_EXPIRES, NULL); |
| 51 | bool request_allows_cache = |
| 52 | has_expires || (std::string::npos != t.request.path.find('?')); |
| 53 | bool response_allows_cache = |
| 54 | has_expires || HttpCodeIsCacheable(t.response.scode); |
| 55 | |
| 56 | bool may_cache = verb_allows_cache |
| 57 | && request_allows_cache |
| 58 | && response_allows_cache |
| 59 | && !is_range_response; |
| 60 | |
| 61 | std::string value; |
| 62 | if (t.response.hasHeader(HH_CACHE_CONTROL, &value)) { |
| 63 | HttpAttributeList directives; |
| 64 | HttpParseAttributes(value.data(), value.size(), directives); |
| 65 | // Response Directives Summary: |
| 66 | // public - always cacheable |
| 67 | // private - do not cache in a shared cache |
| 68 | // no-cache - may cache, but must revalidate whether fresh or stale |
| 69 | // no-store - sensitive information, do not cache or store in any way |
| 70 | // max-age - supplants Expires for staleness |
| 71 | // s-maxage - use as max-age for shared caches, ignore otherwise |
| 72 | // must-revalidate - may cache, but must revalidate after stale |
| 73 | // proxy-revalidate - shared cache must revalidate |
| 74 | if (HttpHasAttribute(directives, "no-store", NULL)) { |
| 75 | may_cache = false; |
| 76 | } else if (HttpHasAttribute(directives, "public", NULL)) { |
| 77 | may_cache = true; |
| 78 | } |
| 79 | } |
| 80 | return may_cache; |
| 81 | } |
| 82 | |
| 83 | enum HttpCacheState { |
| 84 | HCS_FRESH, // In cache, may use |
| 85 | HCS_STALE, // In cache, must revalidate |
| 86 | HCS_NONE // Not in cache |
| 87 | }; |
| 88 | |
| 89 | HttpCacheState HttpGetCacheState(const HttpTransaction& t) { |
| 90 | // Temporaries |
| 91 | std::string s_temp; |
| 92 | time_t u_temp; |
| 93 | |
| 94 | // Current time |
henrike@webrtc.org | 1711104 | 2014-09-10 22:10:24 +0000 | [diff] [blame] | 95 | time_t now = time(0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 96 | |
| 97 | HttpAttributeList cache_control; |
| 98 | if (t.response.hasHeader(HH_CACHE_CONTROL, &s_temp)) { |
| 99 | HttpParseAttributes(s_temp.data(), s_temp.size(), cache_control); |
| 100 | } |
| 101 | |
| 102 | // Compute age of cache document |
| 103 | time_t date; |
| 104 | if (!t.response.hasHeader(HH_DATE, &s_temp) |
| 105 | || !HttpDateToSeconds(s_temp, &date)) |
| 106 | return HCS_NONE; |
| 107 | |
| 108 | // TODO: Timestamp when cache request sent and response received? |
| 109 | time_t request_time = date; |
| 110 | time_t response_time = date; |
| 111 | |
| 112 | time_t apparent_age = 0; |
| 113 | if (response_time > date) { |
| 114 | apparent_age = response_time - date; |
| 115 | } |
| 116 | |
henrike@webrtc.org | 1711104 | 2014-09-10 22:10:24 +0000 | [diff] [blame] | 117 | time_t corrected_received_age = apparent_age; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 118 | size_t i_temp; |
| 119 | if (t.response.hasHeader(HH_AGE, &s_temp) |
| 120 | && HttpStringToUInt(s_temp, (&i_temp))) { |
| 121 | u_temp = static_cast<time_t>(i_temp); |
| 122 | corrected_received_age = stdmax(apparent_age, u_temp); |
| 123 | } |
| 124 | |
henrike@webrtc.org | 1711104 | 2014-09-10 22:10:24 +0000 | [diff] [blame] | 125 | time_t response_delay = response_time - request_time; |
| 126 | time_t corrected_initial_age = corrected_received_age + response_delay; |
| 127 | time_t resident_time = now - response_time; |
| 128 | time_t current_age = corrected_initial_age + resident_time; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 129 | |
| 130 | // Compute lifetime of document |
henrike@webrtc.org | 1711104 | 2014-09-10 22:10:24 +0000 | [diff] [blame] | 131 | time_t lifetime; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 132 | if (HttpHasAttribute(cache_control, "max-age", &s_temp)) { |
| 133 | lifetime = atoi(s_temp.c_str()); |
| 134 | } else if (t.response.hasHeader(HH_EXPIRES, &s_temp) |
| 135 | && HttpDateToSeconds(s_temp, &u_temp)) { |
| 136 | lifetime = u_temp - date; |
| 137 | } else if (t.response.hasHeader(HH_LAST_MODIFIED, &s_temp) |
| 138 | && HttpDateToSeconds(s_temp, &u_temp)) { |
| 139 | // TODO: Issue warning 113 if age > 24 hours |
| 140 | lifetime = static_cast<size_t>(now - u_temp) / 10; |
| 141 | } else { |
| 142 | return HCS_STALE; |
| 143 | } |
| 144 | |
| 145 | return (lifetime > current_age) ? HCS_FRESH : HCS_STALE; |
| 146 | } |
| 147 | |
| 148 | enum HttpValidatorStrength { |
| 149 | HVS_NONE, |
| 150 | HVS_WEAK, |
| 151 | HVS_STRONG |
| 152 | }; |
| 153 | |
| 154 | HttpValidatorStrength |
| 155 | HttpRequestValidatorLevel(const HttpRequestData& request) { |
| 156 | if (HV_GET != request.verb) |
| 157 | return HVS_STRONG; |
| 158 | return request.hasHeader(HH_RANGE, NULL) ? HVS_STRONG : HVS_WEAK; |
| 159 | } |
| 160 | |
| 161 | HttpValidatorStrength |
| 162 | HttpResponseValidatorLevel(const HttpResponseData& response) { |
| 163 | std::string value; |
| 164 | if (response.hasHeader(HH_ETAG, &value)) { |
| 165 | bool is_weak = (strnicmp(value.c_str(), "W/", 2) == 0); |
| 166 | return is_weak ? HVS_WEAK : HVS_STRONG; |
| 167 | } |
| 168 | if (response.hasHeader(HH_LAST_MODIFIED, &value)) { |
| 169 | time_t last_modified, date; |
| 170 | if (HttpDateToSeconds(value, &last_modified) |
| 171 | && response.hasHeader(HH_DATE, &value) |
| 172 | && HttpDateToSeconds(value, &date) |
| 173 | && (last_modified + 60 < date)) { |
| 174 | return HVS_STRONG; |
| 175 | } |
| 176 | return HVS_WEAK; |
| 177 | } |
| 178 | return HVS_NONE; |
| 179 | } |
| 180 | |
| 181 | std::string GetCacheID(const HttpRequestData& request) { |
| 182 | std::string id, url; |
| 183 | id.append(ToString(request.verb)); |
| 184 | id.append("_"); |
| 185 | request.getAbsoluteUri(&url); |
| 186 | id.append(url); |
| 187 | return id; |
| 188 | } |
| 189 | |
| 190 | } // anonymous namespace |
| 191 | |
| 192 | ////////////////////////////////////////////////////////////////////// |
| 193 | // Public Helpers |
| 194 | ////////////////////////////////////////////////////////////////////// |
| 195 | |
| 196 | bool HttpWriteCacheHeaders(const HttpResponseData* response, |
| 197 | StreamInterface* output, size_t* size) { |
| 198 | size_t length = 0; |
| 199 | // Write all unknown and end-to-end headers to a cache file |
| 200 | for (HttpData::const_iterator it = response->begin(); |
| 201 | it != response->end(); ++it) { |
| 202 | HttpHeader header; |
| 203 | if (FromString(header, it->first) && !HttpHeaderIsEndToEnd(header)) |
| 204 | continue; |
| 205 | length += it->first.length() + 2 + it->second.length() + 2; |
| 206 | if (!output) |
| 207 | continue; |
| 208 | std::string formatted_header(it->first); |
| 209 | formatted_header.append(": "); |
| 210 | formatted_header.append(it->second); |
| 211 | formatted_header.append("\r\n"); |
| 212 | StreamResult result = output->WriteAll(formatted_header.data(), |
| 213 | formatted_header.length(), |
| 214 | NULL, NULL); |
| 215 | if (SR_SUCCESS != result) { |
| 216 | return false; |
| 217 | } |
| 218 | } |
| 219 | if (output && (SR_SUCCESS != output->WriteAll("\r\n", 2, NULL, NULL))) { |
| 220 | return false; |
| 221 | } |
| 222 | length += 2; |
| 223 | if (size) |
| 224 | *size = length; |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | bool HttpReadCacheHeaders(StreamInterface* input, HttpResponseData* response, |
| 229 | HttpData::HeaderCombine combine) { |
| 230 | while (true) { |
| 231 | std::string formatted_header; |
| 232 | StreamResult result = input->ReadLine(&formatted_header); |
| 233 | if ((SR_EOS == result) || (1 == formatted_header.size())) { |
| 234 | break; |
| 235 | } |
| 236 | if (SR_SUCCESS != result) { |
| 237 | return false; |
| 238 | } |
| 239 | size_t end_of_name = formatted_header.find(':'); |
| 240 | if (std::string::npos == end_of_name) { |
| 241 | LOG_F(LS_WARNING) << "Malformed cache header"; |
| 242 | continue; |
| 243 | } |
| 244 | size_t start_of_value = end_of_name + 1; |
| 245 | size_t end_of_value = formatted_header.length(); |
| 246 | while ((start_of_value < end_of_value) |
| 247 | && isspace(formatted_header[start_of_value])) |
| 248 | ++start_of_value; |
| 249 | while ((start_of_value < end_of_value) |
| 250 | && isspace(formatted_header[end_of_value-1])) |
| 251 | --end_of_value; |
| 252 | size_t value_length = end_of_value - start_of_value; |
| 253 | |
| 254 | std::string name(formatted_header.substr(0, end_of_name)); |
| 255 | std::string value(formatted_header.substr(start_of_value, value_length)); |
| 256 | response->changeHeader(name, value, combine); |
| 257 | } |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | ////////////////////////////////////////////////////////////////////// |
| 262 | // HttpClient |
| 263 | ////////////////////////////////////////////////////////////////////// |
| 264 | |
| 265 | const size_t kDefaultRetries = 1; |
| 266 | const size_t kMaxRedirects = 5; |
| 267 | |
| 268 | HttpClient::HttpClient(const std::string& agent, StreamPool* pool, |
| 269 | HttpTransaction* transaction) |
| 270 | : agent_(agent), pool_(pool), |
| 271 | transaction_(transaction), free_transaction_(false), |
| 272 | retries_(kDefaultRetries), attempt_(0), redirects_(0), |
| 273 | redirect_action_(REDIRECT_DEFAULT), |
| 274 | uri_form_(URI_DEFAULT), cache_(NULL), cache_state_(CS_READY), |
| 275 | resolver_(NULL) { |
| 276 | base_.notify(this); |
| 277 | if (NULL == transaction_) { |
| 278 | free_transaction_ = true; |
| 279 | transaction_ = new HttpTransaction; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | HttpClient::~HttpClient() { |
| 284 | base_.notify(NULL); |
| 285 | base_.abort(HE_SHUTDOWN); |
| 286 | if (resolver_) { |
| 287 | resolver_->Destroy(false); |
| 288 | } |
| 289 | release(); |
| 290 | if (free_transaction_) |
| 291 | delete transaction_; |
| 292 | } |
| 293 | |
| 294 | void HttpClient::reset() { |
| 295 | server_.Clear(); |
| 296 | request().clear(true); |
| 297 | response().clear(true); |
| 298 | context_.reset(); |
| 299 | redirects_ = 0; |
| 300 | base_.abort(HE_OPERATION_CANCELLED); |
| 301 | } |
| 302 | |
| 303 | void HttpClient::OnResolveResult(AsyncResolverInterface* resolver) { |
| 304 | if (resolver != resolver_) { |
| 305 | return; |
| 306 | } |
| 307 | int error = resolver_->GetError(); |
| 308 | server_ = resolver_->address(); |
| 309 | resolver_->Destroy(false); |
| 310 | resolver_ = NULL; |
| 311 | if (error != 0) { |
| 312 | LOG(LS_ERROR) << "Error " << error << " resolving name: " |
| 313 | << server_; |
| 314 | onHttpComplete(HM_CONNECT, HE_CONNECT_FAILED); |
| 315 | } else { |
| 316 | connect(); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | void HttpClient::StartDNSLookup() { |
| 321 | resolver_ = new AsyncResolver(); |
| 322 | resolver_->SignalDone.connect(this, &HttpClient::OnResolveResult); |
| 323 | resolver_->Start(server_); |
| 324 | } |
| 325 | |
| 326 | void HttpClient::set_server(const SocketAddress& address) { |
| 327 | server_ = address; |
| 328 | // Setting 'Host' here allows it to be overridden before starting the request, |
| 329 | // if necessary. |
| 330 | request().setHeader(HH_HOST, HttpAddress(server_, false), true); |
| 331 | } |
| 332 | |
| 333 | StreamInterface* HttpClient::GetDocumentStream() { |
| 334 | return base_.GetDocumentStream(); |
| 335 | } |
| 336 | |
| 337 | void HttpClient::start() { |
| 338 | if (base_.mode() != HM_NONE) { |
| 339 | // call reset() to abort an in-progress request |
| 340 | ASSERT(false); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | ASSERT(!IsCacheActive()); |
| 345 | |
| 346 | if (request().hasHeader(HH_TRANSFER_ENCODING, NULL)) { |
| 347 | // Exact size must be known on the client. Instead of using chunked |
| 348 | // encoding, wrap data with auto-caching file or memory stream. |
| 349 | ASSERT(false); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | attempt_ = 0; |
| 354 | |
| 355 | // If no content has been specified, using length of 0. |
| 356 | request().setHeader(HH_CONTENT_LENGTH, "0", false); |
| 357 | |
| 358 | if (!agent_.empty()) { |
| 359 | request().setHeader(HH_USER_AGENT, agent_, false); |
| 360 | } |
| 361 | |
| 362 | UriForm uri_form = uri_form_; |
| 363 | if (PROXY_HTTPS == proxy_.type) { |
| 364 | // Proxies require absolute form |
| 365 | uri_form = URI_ABSOLUTE; |
| 366 | request().version = HVER_1_0; |
| 367 | request().setHeader(HH_PROXY_CONNECTION, "Keep-Alive", false); |
| 368 | } else { |
| 369 | request().setHeader(HH_CONNECTION, "Keep-Alive", false); |
| 370 | } |
| 371 | |
| 372 | if (URI_ABSOLUTE == uri_form) { |
| 373 | // Convert to absolute uri form |
| 374 | std::string url; |
| 375 | if (request().getAbsoluteUri(&url)) { |
| 376 | request().path = url; |
| 377 | } else { |
| 378 | LOG(LS_WARNING) << "Couldn't obtain absolute uri"; |
| 379 | } |
| 380 | } else if (URI_RELATIVE == uri_form) { |
| 381 | // Convert to relative uri form |
| 382 | std::string host, path; |
| 383 | if (request().getRelativeUri(&host, &path)) { |
| 384 | request().setHeader(HH_HOST, host); |
| 385 | request().path = path; |
| 386 | } else { |
| 387 | LOG(LS_WARNING) << "Couldn't obtain relative uri"; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if ((NULL != cache_) && CheckCache()) { |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | connect(); |
| 396 | } |
| 397 | |
| 398 | void HttpClient::connect() { |
| 399 | int stream_err; |
| 400 | if (server_.IsUnresolvedIP()) { |
| 401 | StartDNSLookup(); |
| 402 | return; |
| 403 | } |
| 404 | StreamInterface* stream = pool_->RequestConnectedStream(server_, &stream_err); |
| 405 | if (stream == NULL) { |
| 406 | ASSERT(0 != stream_err); |
| 407 | LOG(LS_ERROR) << "RequestConnectedStream error: " << stream_err; |
| 408 | onHttpComplete(HM_CONNECT, HE_CONNECT_FAILED); |
| 409 | } else { |
| 410 | base_.attach(stream); |
| 411 | if (stream->GetState() == SS_OPEN) { |
| 412 | base_.send(&transaction_->request); |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | void HttpClient::prepare_get(const std::string& url) { |
| 418 | reset(); |
| 419 | Url<char> purl(url); |
| 420 | set_server(SocketAddress(purl.host(), purl.port())); |
| 421 | request().verb = HV_GET; |
| 422 | request().path = purl.full_path(); |
| 423 | } |
| 424 | |
| 425 | void HttpClient::prepare_post(const std::string& url, |
| 426 | const std::string& content_type, |
| 427 | StreamInterface* request_doc) { |
| 428 | reset(); |
| 429 | Url<char> purl(url); |
| 430 | set_server(SocketAddress(purl.host(), purl.port())); |
| 431 | request().verb = HV_POST; |
| 432 | request().path = purl.full_path(); |
| 433 | request().setContent(content_type, request_doc); |
| 434 | } |
| 435 | |
| 436 | void HttpClient::release() { |
| 437 | if (StreamInterface* stream = base_.detach()) { |
| 438 | pool_->ReturnConnectedStream(stream); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | bool HttpClient::ShouldRedirect(std::string* location) const { |
| 443 | // TODO: Unittest redirection. |
| 444 | if ((REDIRECT_NEVER == redirect_action_) |
| 445 | || !HttpCodeIsRedirection(response().scode) |
| 446 | || !response().hasHeader(HH_LOCATION, location) |
| 447 | || (redirects_ >= kMaxRedirects)) |
| 448 | return false; |
| 449 | return (REDIRECT_ALWAYS == redirect_action_) |
| 450 | || (HC_SEE_OTHER == response().scode) |
| 451 | || (HV_HEAD == request().verb) |
| 452 | || (HV_GET == request().verb); |
| 453 | } |
| 454 | |
| 455 | bool HttpClient::BeginCacheFile() { |
| 456 | ASSERT(NULL != cache_); |
| 457 | ASSERT(CS_READY == cache_state_); |
| 458 | |
| 459 | std::string id = GetCacheID(request()); |
| 460 | CacheLock lock(cache_, id, true); |
| 461 | if (!lock.IsLocked()) { |
| 462 | LOG_F(LS_WARNING) << "Couldn't lock cache"; |
| 463 | return false; |
| 464 | } |
| 465 | |
| 466 | if (HE_NONE != WriteCacheHeaders(id)) { |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | scoped_ptr<StreamInterface> stream(cache_->WriteResource(id, kCacheBody)); |
| 471 | if (!stream) { |
| 472 | LOG_F(LS_ERROR) << "Couldn't open body cache"; |
| 473 | return false; |
| 474 | } |
| 475 | lock.Commit(); |
| 476 | |
| 477 | // Let's secretly replace the response document with Folgers Crystals, |
| 478 | // er, StreamTap, so that we can mirror the data to our cache. |
| 479 | StreamInterface* output = response().document.release(); |
| 480 | if (!output) { |
| 481 | output = new NullStream; |
| 482 | } |
| 483 | StreamTap* tap = new StreamTap(output, stream.release()); |
| 484 | response().document.reset(tap); |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | HttpError HttpClient::WriteCacheHeaders(const std::string& id) { |
| 489 | scoped_ptr<StreamInterface> stream(cache_->WriteResource(id, kCacheHeader)); |
| 490 | if (!stream) { |
| 491 | LOG_F(LS_ERROR) << "Couldn't open header cache"; |
| 492 | return HE_CACHE; |
| 493 | } |
| 494 | |
| 495 | if (!HttpWriteCacheHeaders(&transaction_->response, stream.get(), NULL)) { |
| 496 | LOG_F(LS_ERROR) << "Couldn't write header cache"; |
| 497 | return HE_CACHE; |
| 498 | } |
| 499 | |
| 500 | return HE_NONE; |
| 501 | } |
| 502 | |
| 503 | void HttpClient::CompleteCacheFile() { |
| 504 | // Restore previous response document |
| 505 | StreamTap* tap = static_cast<StreamTap*>(response().document.release()); |
| 506 | response().document.reset(tap->Detach()); |
| 507 | |
| 508 | int error; |
| 509 | StreamResult result = tap->GetTapResult(&error); |
| 510 | |
| 511 | // Delete the tap and cache stream (which completes cache unlock) |
| 512 | delete tap; |
| 513 | |
| 514 | if (SR_SUCCESS != result) { |
| 515 | LOG(LS_ERROR) << "Cache file error: " << error; |
| 516 | cache_->DeleteResource(GetCacheID(request())); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | bool HttpClient::CheckCache() { |
| 521 | ASSERT(NULL != cache_); |
| 522 | ASSERT(CS_READY == cache_state_); |
| 523 | |
| 524 | std::string id = GetCacheID(request()); |
| 525 | if (!cache_->HasResource(id)) { |
| 526 | // No cache file available |
| 527 | return false; |
| 528 | } |
| 529 | |
| 530 | HttpError error = ReadCacheHeaders(id, true); |
| 531 | |
| 532 | if (HE_NONE == error) { |
| 533 | switch (HttpGetCacheState(*transaction_)) { |
| 534 | case HCS_FRESH: |
| 535 | // Cache content is good, read from cache |
| 536 | break; |
| 537 | case HCS_STALE: |
| 538 | // Cache content may be acceptable. Issue a validation request. |
| 539 | if (PrepareValidate()) { |
| 540 | return false; |
| 541 | } |
| 542 | // Couldn't validate, fall through. |
kjellander@webrtc.org | 7d2b6a9 | 2015-01-28 18:37:58 +0000 | [diff] [blame] | 543 | FALLTHROUGH(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 544 | case HCS_NONE: |
| 545 | // Cache content is not useable. Issue a regular request. |
| 546 | response().clear(false); |
| 547 | return false; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (HE_NONE == error) { |
| 552 | error = ReadCacheBody(id); |
| 553 | cache_state_ = CS_READY; |
| 554 | } |
| 555 | |
| 556 | if (HE_CACHE == error) { |
| 557 | LOG_F(LS_WARNING) << "Cache failure, continuing with normal request"; |
| 558 | response().clear(false); |
| 559 | return false; |
| 560 | } |
| 561 | |
| 562 | SignalHttpClientComplete(this, error); |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | HttpError HttpClient::ReadCacheHeaders(const std::string& id, bool override) { |
| 567 | scoped_ptr<StreamInterface> stream(cache_->ReadResource(id, kCacheHeader)); |
| 568 | if (!stream) { |
| 569 | return HE_CACHE; |
| 570 | } |
| 571 | |
| 572 | HttpData::HeaderCombine combine = |
| 573 | override ? HttpData::HC_REPLACE : HttpData::HC_AUTO; |
| 574 | |
| 575 | if (!HttpReadCacheHeaders(stream.get(), &transaction_->response, combine)) { |
| 576 | LOG_F(LS_ERROR) << "Error reading cache headers"; |
| 577 | return HE_CACHE; |
| 578 | } |
| 579 | |
| 580 | response().scode = HC_OK; |
| 581 | return HE_NONE; |
| 582 | } |
| 583 | |
| 584 | HttpError HttpClient::ReadCacheBody(const std::string& id) { |
| 585 | cache_state_ = CS_READING; |
| 586 | |
| 587 | HttpError error = HE_NONE; |
| 588 | |
| 589 | size_t data_size; |
| 590 | scoped_ptr<StreamInterface> stream(cache_->ReadResource(id, kCacheBody)); |
| 591 | if (!stream || !stream->GetAvailable(&data_size)) { |
| 592 | LOG_F(LS_ERROR) << "Unavailable cache body"; |
| 593 | error = HE_CACHE; |
| 594 | } else { |
| 595 | error = OnHeaderAvailable(false, false, data_size); |
| 596 | } |
| 597 | |
| 598 | if ((HE_NONE == error) |
| 599 | && (HV_HEAD != request().verb) |
| 600 | && response().document) { |
andresp@webrtc.org | f7e5f22 | 2014-09-12 13:35:05 +0000 | [diff] [blame] | 601 | // Allocate on heap to not explode the stack. |
| 602 | const int array_size = 1024 * 64; |
| 603 | scoped_ptr<char[]> buffer(new char[array_size]); |
| 604 | StreamResult result = Flow(stream.get(), buffer.get(), array_size, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 605 | response().document.get()); |
| 606 | if (SR_SUCCESS != result) { |
| 607 | error = HE_STREAM; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | return error; |
| 612 | } |
| 613 | |
| 614 | bool HttpClient::PrepareValidate() { |
| 615 | ASSERT(CS_READY == cache_state_); |
| 616 | // At this point, request() contains the pending request, and response() |
| 617 | // contains the cached response headers. Reformat the request to validate |
| 618 | // the cached content. |
| 619 | HttpValidatorStrength vs_required = HttpRequestValidatorLevel(request()); |
| 620 | HttpValidatorStrength vs_available = HttpResponseValidatorLevel(response()); |
| 621 | if (vs_available < vs_required) { |
| 622 | return false; |
| 623 | } |
| 624 | std::string value; |
| 625 | if (response().hasHeader(HH_ETAG, &value)) { |
| 626 | request().addHeader(HH_IF_NONE_MATCH, value); |
| 627 | } |
| 628 | if (response().hasHeader(HH_LAST_MODIFIED, &value)) { |
| 629 | request().addHeader(HH_IF_MODIFIED_SINCE, value); |
| 630 | } |
| 631 | response().clear(false); |
| 632 | cache_state_ = CS_VALIDATING; |
| 633 | return true; |
| 634 | } |
| 635 | |
| 636 | HttpError HttpClient::CompleteValidate() { |
| 637 | ASSERT(CS_VALIDATING == cache_state_); |
| 638 | |
| 639 | std::string id = GetCacheID(request()); |
| 640 | |
| 641 | // Merge cached headers with new headers |
| 642 | HttpError error = ReadCacheHeaders(id, false); |
| 643 | if (HE_NONE != error) { |
| 644 | // Rewrite merged headers to cache |
| 645 | CacheLock lock(cache_, id); |
| 646 | error = WriteCacheHeaders(id); |
| 647 | } |
| 648 | if (HE_NONE != error) { |
| 649 | error = ReadCacheBody(id); |
| 650 | } |
| 651 | return error; |
| 652 | } |
| 653 | |
| 654 | HttpError HttpClient::OnHeaderAvailable(bool ignore_data, bool chunked, |
| 655 | size_t data_size) { |
| 656 | // If we are ignoring the data, this is an intermediate header. |
| 657 | // TODO: don't signal intermediate headers. Instead, do all header-dependent |
| 658 | // processing now, and either set up the next request, or fail outright. |
| 659 | // TODO: by default, only write response documents with a success code. |
| 660 | SignalHeaderAvailable(this, !ignore_data, ignore_data ? 0 : data_size); |
| 661 | if (!ignore_data && !chunked && (data_size != SIZE_UNKNOWN) |
| 662 | && response().document) { |
| 663 | // Attempt to pre-allocate space for the downloaded data. |
| 664 | if (!response().document->ReserveSize(data_size)) { |
| 665 | return HE_OVERFLOW; |
| 666 | } |
| 667 | } |
| 668 | return HE_NONE; |
| 669 | } |
| 670 | |
| 671 | // |
| 672 | // HttpBase Implementation |
| 673 | // |
| 674 | |
| 675 | HttpError HttpClient::onHttpHeaderComplete(bool chunked, size_t& data_size) { |
| 676 | if (CS_VALIDATING == cache_state_) { |
| 677 | if (HC_NOT_MODIFIED == response().scode) { |
| 678 | return CompleteValidate(); |
| 679 | } |
| 680 | // Should we remove conditional headers from request? |
| 681 | cache_state_ = CS_READY; |
| 682 | cache_->DeleteResource(GetCacheID(request())); |
| 683 | // Continue processing response as normal |
| 684 | } |
| 685 | |
| 686 | ASSERT(!IsCacheActive()); |
| 687 | if ((request().verb == HV_HEAD) || !HttpCodeHasBody(response().scode)) { |
| 688 | // HEAD requests and certain response codes contain no body |
| 689 | data_size = 0; |
| 690 | } |
| 691 | if (ShouldRedirect(NULL) |
| 692 | || ((HC_PROXY_AUTHENTICATION_REQUIRED == response().scode) |
| 693 | && (PROXY_HTTPS == proxy_.type))) { |
| 694 | // We're going to issue another request, so ignore the incoming data. |
| 695 | base_.set_ignore_data(true); |
| 696 | } |
| 697 | |
| 698 | HttpError error = OnHeaderAvailable(base_.ignore_data(), chunked, data_size); |
| 699 | if (HE_NONE != error) { |
| 700 | return error; |
| 701 | } |
| 702 | |
| 703 | if ((NULL != cache_) |
| 704 | && !base_.ignore_data() |
| 705 | && HttpShouldCache(*transaction_)) { |
| 706 | if (BeginCacheFile()) { |
| 707 | cache_state_ = CS_WRITING; |
| 708 | } |
| 709 | } |
| 710 | return HE_NONE; |
| 711 | } |
| 712 | |
| 713 | void HttpClient::onHttpComplete(HttpMode mode, HttpError err) { |
| 714 | if (((HE_DISCONNECTED == err) || (HE_CONNECT_FAILED == err) |
| 715 | || (HE_SOCKET_ERROR == err)) |
| 716 | && (HC_INTERNAL_SERVER_ERROR == response().scode) |
| 717 | && (attempt_ < retries_)) { |
| 718 | // If the response code has not changed from the default, then we haven't |
| 719 | // received anything meaningful from the server, so we are eligible for a |
| 720 | // retry. |
| 721 | ++attempt_; |
| 722 | if (request().document && !request().document->Rewind()) { |
| 723 | // Unable to replay the request document. |
| 724 | err = HE_STREAM; |
| 725 | } else { |
| 726 | release(); |
| 727 | connect(); |
| 728 | return; |
| 729 | } |
| 730 | } else if (err != HE_NONE) { |
| 731 | // fall through |
| 732 | } else if (mode == HM_CONNECT) { |
| 733 | base_.send(&transaction_->request); |
| 734 | return; |
| 735 | } else if ((mode == HM_SEND) || HttpCodeIsInformational(response().scode)) { |
| 736 | // If you're interested in informational headers, catch |
| 737 | // SignalHeaderAvailable. |
| 738 | base_.recv(&transaction_->response); |
| 739 | return; |
| 740 | } else { |
| 741 | if (!HttpShouldKeepAlive(response())) { |
| 742 | LOG(LS_VERBOSE) << "HttpClient: closing socket"; |
| 743 | base_.stream()->Close(); |
| 744 | } |
| 745 | std::string location; |
| 746 | if (ShouldRedirect(&location)) { |
| 747 | Url<char> purl(location); |
| 748 | set_server(SocketAddress(purl.host(), purl.port())); |
| 749 | request().path = purl.full_path(); |
| 750 | if (response().scode == HC_SEE_OTHER) { |
| 751 | request().verb = HV_GET; |
| 752 | request().clearHeader(HH_CONTENT_TYPE); |
| 753 | request().clearHeader(HH_CONTENT_LENGTH); |
| 754 | request().document.reset(); |
| 755 | } else if (request().document && !request().document->Rewind()) { |
| 756 | // Unable to replay the request document. |
| 757 | ASSERT(REDIRECT_ALWAYS == redirect_action_); |
| 758 | err = HE_STREAM; |
| 759 | } |
| 760 | if (err == HE_NONE) { |
| 761 | ++redirects_; |
| 762 | context_.reset(); |
| 763 | response().clear(false); |
| 764 | release(); |
| 765 | start(); |
| 766 | return; |
| 767 | } |
| 768 | } else if ((HC_PROXY_AUTHENTICATION_REQUIRED == response().scode) |
| 769 | && (PROXY_HTTPS == proxy_.type)) { |
| 770 | std::string authorization, auth_method; |
| 771 | HttpData::const_iterator begin = response().begin(HH_PROXY_AUTHENTICATE); |
| 772 | HttpData::const_iterator end = response().end(HH_PROXY_AUTHENTICATE); |
| 773 | for (HttpData::const_iterator it = begin; it != end; ++it) { |
| 774 | HttpAuthContext *context = context_.get(); |
| 775 | HttpAuthResult res = HttpAuthenticate( |
| 776 | it->second.data(), it->second.size(), |
| 777 | proxy_.address, |
| 778 | ToString(request().verb), request().path, |
| 779 | proxy_.username, proxy_.password, |
| 780 | context, authorization, auth_method); |
| 781 | context_.reset(context); |
| 782 | if (res == HAR_RESPONSE) { |
| 783 | request().setHeader(HH_PROXY_AUTHORIZATION, authorization); |
| 784 | if (request().document && !request().document->Rewind()) { |
| 785 | err = HE_STREAM; |
| 786 | } else { |
| 787 | // Explicitly do not reset the HttpAuthContext |
| 788 | response().clear(false); |
| 789 | // TODO: Reuse socket when authenticating? |
| 790 | release(); |
| 791 | start(); |
| 792 | return; |
| 793 | } |
| 794 | } else if (res == HAR_IGNORE) { |
| 795 | LOG(INFO) << "Ignoring Proxy-Authenticate: " << auth_method; |
| 796 | continue; |
| 797 | } else { |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | if (CS_WRITING == cache_state_) { |
| 804 | CompleteCacheFile(); |
| 805 | cache_state_ = CS_READY; |
| 806 | } else if (CS_READING == cache_state_) { |
| 807 | cache_state_ = CS_READY; |
| 808 | } |
| 809 | release(); |
| 810 | SignalHttpClientComplete(this, err); |
| 811 | } |
| 812 | |
| 813 | void HttpClient::onHttpClosed(HttpError err) { |
| 814 | // This shouldn't occur, since we return the stream to the pool upon command |
| 815 | // completion. |
| 816 | ASSERT(false); |
| 817 | } |
| 818 | |
| 819 | ////////////////////////////////////////////////////////////////////// |
| 820 | // HttpClientDefault |
| 821 | ////////////////////////////////////////////////////////////////////// |
| 822 | |
| 823 | HttpClientDefault::HttpClientDefault(SocketFactory* factory, |
| 824 | const std::string& agent, |
| 825 | HttpTransaction* transaction) |
| 826 | : ReuseSocketPool(factory ? factory : Thread::Current()->socketserver()), |
| 827 | HttpClient(agent, NULL, transaction) { |
| 828 | set_pool(this); |
| 829 | } |
| 830 | |
| 831 | ////////////////////////////////////////////////////////////////////// |
| 832 | |
| 833 | } // namespace rtc |