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