nsHttp.cpp (36731B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=4 sw=2 sts=2 et cin: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 // HttpLog.h should generally be included first 8 #include "HttpLog.h" 9 10 #include "nsHttp.h" 11 #include "CacheControlParser.h" 12 #include "PLDHashTable.h" 13 #include "mozilla/DataMutex.h" 14 #include "mozilla/OriginAttributes.h" 15 #include "mozilla/Preferences.h" 16 #include "mozilla/StaticPrefs_network.h" 17 #include "nsCRT.h" 18 #include "nsContentUtils.h" 19 #include "nsHttpRequestHead.h" 20 #include "nsHttpResponseHead.h" 21 #include "nsHttpHandler.h" 22 #include "nsICacheEntry.h" 23 #include "nsIRequest.h" 24 #include "nsIStandardURL.h" 25 #include "nsJSUtils.h" 26 #include "nsStandardURL.h" 27 #include "sslerr.h" 28 #include <errno.h> 29 #include <functional> 30 #include "nsLiteralString.h" 31 #include <string.h> 32 33 namespace mozilla { 34 namespace net { 35 36 extern const char kProxyType_SOCKS[]; 37 38 // https://datatracker.ietf.org/doc/html/draft-ietf-webtrans-http3/#section-4.3 39 constexpr uint64_t kWebTransportErrorCodeStart = 0x52e4a40fa8db; 40 constexpr uint64_t kWebTransportErrorCodeEnd = 0x52e4a40fa9e2; 41 42 // find out how many atoms we have 43 #define HTTP_ATOM(_name, _value) Unused_##_name, 44 enum { 45 #include "nsHttpAtomList.h" 46 NUM_HTTP_ATOMS 47 }; 48 #undef HTTP_ATOM 49 50 MOZ_RUNINIT static StaticDataMutex< 51 nsTHashtable<nsCStringASCIICaseInsensitiveHashKey>> 52 sAtomTable("nsHttp::sAtomTable"); 53 54 // This is set to true in DestroyAtomTable so we don't try to repopulate the 55 // table if ResolveAtom gets called during shutdown for some reason. 56 static Atomic<bool> sTableDestroyed{false}; 57 58 // We put the atoms in a hash table for speedy lookup.. see ResolveAtom. 59 namespace nsHttp { 60 61 nsresult CreateAtomTable( 62 nsTHashtable<nsCStringASCIICaseInsensitiveHashKey>& base) { 63 if (sTableDestroyed) { 64 return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; 65 } 66 // fill the table with our known atoms 67 const nsHttpAtomLiteral* atoms[] = { 68 #define HTTP_ATOM(_name, _value) &(_name), 69 #include "nsHttpAtomList.h" 70 #undef HTTP_ATOM 71 }; 72 73 if (!base.IsEmpty()) { 74 return NS_OK; 75 } 76 for (const auto* atom : atoms) { 77 (void)base.PutEntry(atom->val(), fallible); 78 } 79 80 LOG(("Added static atoms to atomTable")); 81 return NS_OK; 82 } 83 84 nsresult CreateAtomTable() { 85 LOG(("CreateAtomTable")); 86 auto atomTable = sAtomTable.Lock(); 87 return CreateAtomTable(atomTable.ref()); 88 } 89 90 void DestroyAtomTable() { 91 LOG(("DestroyAtomTable")); 92 sTableDestroyed = true; 93 auto atomTable = sAtomTable.Lock(); 94 atomTable.ref().Clear(); 95 } 96 97 // this function may be called from multiple threads 98 nsHttpAtom ResolveAtom(const nsACString& str) { 99 if (str.IsEmpty()) { 100 return {}; 101 } 102 103 auto atomTable = sAtomTable.Lock(); 104 105 if (atomTable.ref().IsEmpty()) { 106 if (sTableDestroyed) { 107 NS_WARNING("ResolveAtom called during shutdown"); 108 return {}; 109 } 110 111 NS_WARNING("ResolveAtom called before CreateAtomTable"); 112 if (NS_FAILED(CreateAtomTable(atomTable.ref()))) { 113 return {}; 114 } 115 } 116 117 // Check if we already have an entry in the table 118 auto* entry = atomTable.ref().GetEntry(str); 119 if (entry) { 120 return nsHttpAtom(entry->GetKey()); 121 } 122 123 LOG(("Putting %s header into atom table", nsPromiseFlatCString(str).get())); 124 // Put the string in the table. If it works create the atom. 125 entry = atomTable.ref().PutEntry(str, fallible); 126 if (entry) { 127 return nsHttpAtom(entry->GetKey()); 128 } 129 return {}; 130 } 131 132 // 133 // From section 2.2 of RFC 2616, a token is defined as: 134 // 135 // token = 1*<any CHAR except CTLs or separators> 136 // CHAR = <any US-ASCII character (octets 0 - 127)> 137 // separators = "(" | ")" | "<" | ">" | "@" 138 // | "," | ";" | ":" | "\" | <"> 139 // | "/" | "[" | "]" | "?" | "=" 140 // | "{" | "}" | SP | HT 141 // CTL = <any US-ASCII control character 142 // (octets 0 - 31) and DEL (127)> 143 // SP = <US-ASCII SP, space (32)> 144 // HT = <US-ASCII HT, horizontal-tab (9)> 145 // 146 static const char kValidTokenMap[128] = { 147 0, 0, 0, 0, 0, 0, 0, 0, // 0 148 0, 0, 0, 0, 0, 0, 0, 0, // 8 149 0, 0, 0, 0, 0, 0, 0, 0, // 16 150 0, 0, 0, 0, 0, 0, 0, 0, // 24 151 152 0, 1, 0, 1, 1, 1, 1, 1, // 32 153 0, 0, 1, 1, 0, 1, 1, 0, // 40 154 1, 1, 1, 1, 1, 1, 1, 1, // 48 155 1, 1, 0, 0, 0, 0, 0, 0, // 56 156 157 0, 1, 1, 1, 1, 1, 1, 1, // 64 158 1, 1, 1, 1, 1, 1, 1, 1, // 72 159 1, 1, 1, 1, 1, 1, 1, 1, // 80 160 1, 1, 1, 0, 0, 0, 1, 1, // 88 161 162 1, 1, 1, 1, 1, 1, 1, 1, // 96 163 1, 1, 1, 1, 1, 1, 1, 1, // 104 164 1, 1, 1, 1, 1, 1, 1, 1, // 112 165 1, 1, 1, 0, 1, 0, 1, 0 // 120 166 }; 167 bool IsValidToken(const char* start, const char* end) { 168 if (start == end) return false; 169 170 for (; start != end; ++start) { 171 const unsigned char idx = *start; 172 if (idx > 127 || !kValidTokenMap[idx]) return false; 173 } 174 175 return true; 176 } 177 178 const char* GetProtocolVersion(HttpVersion pv) { 179 switch (pv) { 180 case HttpVersion::v3_0: 181 return "h3"; 182 case HttpVersion::v2_0: 183 return "h2"; 184 case HttpVersion::v1_0: 185 return "http/1.0"; 186 case HttpVersion::v1_1: 187 return "http/1.1"; 188 default: 189 NS_WARNING(nsPrintfCString("Unkown protocol version: 0x%X. " 190 "Please file a bug", 191 static_cast<uint32_t>(pv)) 192 .get()); 193 return "http/1.1"; 194 } 195 } 196 197 // static 198 void TrimHTTPWhitespace(const nsACString& aSource, nsACString& aDest) { 199 nsAutoCString str(aSource); 200 201 // HTTP whitespace 0x09: '\t', 0x0A: '\n', 0x0D: '\r', 0x20: ' ' 202 static const char kHTTPWhitespace[] = "\t\n\r "; 203 str.Trim(kHTTPWhitespace); 204 aDest.Assign(str); 205 } 206 207 // static 208 bool IsReasonableHeaderValue(const nsACString& s) { 209 // Header values MUST NOT contain line-breaks. RFC 2616 technically 210 // permits CTL characters, including CR and LF, in header values provided 211 // they are quoted. However, this can lead to problems if servers do not 212 // interpret quoted strings properly. Disallowing CR and LF here seems 213 // reasonable and keeps things simple. We also disallow a null byte. 214 const nsACString::char_type* end = s.EndReading(); 215 for (const nsACString::char_type* i = s.BeginReading(); i != end; ++i) { 216 if (*i == '\r' || *i == '\n' || *i == '\0') { 217 return false; 218 } 219 } 220 return true; 221 } 222 223 const char* FindToken(const char* input, const char* token, const char* seps) { 224 if (!input) return nullptr; 225 226 int inputLen = strlen(input); 227 int tokenLen = strlen(token); 228 229 if (inputLen < tokenLen) return nullptr; 230 231 const char* inputTop = input; 232 const char* inputEnd = input + inputLen - tokenLen; 233 for (; input <= inputEnd; ++input) { 234 if (nsCRT::strncasecmp(input, token, tokenLen) == 0) { 235 if (input > inputTop && !strchr(seps, *(input - 1))) continue; 236 if (input < inputEnd && !strchr(seps, *(input + tokenLen))) continue; 237 return input; 238 } 239 } 240 241 return nullptr; 242 } 243 244 bool ParseInt64(const char* input, const char** next, int64_t* r) { 245 MOZ_ASSERT(input); 246 MOZ_ASSERT(r); 247 248 char* end = nullptr; 249 errno = 0; // Clear errno to make sure its value is set by strtoll 250 int64_t value = strtoll(input, &end, /* base */ 10); 251 252 // Fail if: - the parsed number overflows. 253 // - the end points to the start of the input string. 254 // - we parsed a negative value. Consumers don't expect that. 255 if (errno != 0 || end == input || value < 0) { 256 LOG(("nsHttp::ParseInt64 value=%" PRId64 " errno=%d", value, errno)); 257 return false; 258 } 259 260 if (next) { 261 *next = end; 262 } 263 *r = value; 264 return true; 265 } 266 267 bool IsPermanentRedirect(uint32_t httpStatus) { 268 return httpStatus == 301 || httpStatus == 308; 269 } 270 271 bool ValidationRequired(bool isForcedValid, 272 nsHttpResponseHead* cachedResponseHead, 273 uint32_t loadFlags, bool allowStaleCacheContent, 274 bool forceValidateCacheContent, bool isImmutable, 275 bool customConditionalRequest, 276 nsHttpRequestHead& requestHead, nsICacheEntry* entry, 277 CacheControlParser& cacheControlRequest, 278 bool fromPreviousSession, 279 bool* performBackgroundRevalidation) { 280 if (performBackgroundRevalidation) { 281 *performBackgroundRevalidation = false; 282 } 283 284 // Check isForcedValid to see if it is possible to skip validation. 285 // Don't skip validation if we have serious reason to believe that this 286 // content is invalid (it's expired). 287 // See netwerk/cache2/nsICacheEntry.idl for details 288 if (isForcedValid && (!cachedResponseHead->ExpiresInPast() || 289 !cachedResponseHead->MustValidateIfExpired())) { 290 LOG(("NOT validating based on isForcedValid being true.\n")); 291 return false; 292 } 293 294 // If the LOAD_FROM_CACHE flag is set, any cached data can simply be used 295 if (loadFlags & nsIRequest::LOAD_FROM_CACHE || allowStaleCacheContent) { 296 LOG(("NOT validating based on LOAD_FROM_CACHE load flag\n")); 297 return false; 298 } 299 300 // If the VALIDATE_ALWAYS flag is set, any cached data won't be used until 301 // it's revalidated with the server. 302 if (((loadFlags & nsIRequest::VALIDATE_ALWAYS) || 303 forceValidateCacheContent) && 304 !isImmutable) { 305 LOG(("Validating based on VALIDATE_ALWAYS load flag\n")); 306 return true; 307 } 308 309 // Even if the VALIDATE_NEVER flag is set, there are still some cases in 310 // which we must validate the cached response with the server. 311 if (loadFlags & nsIRequest::VALIDATE_NEVER) { 312 LOG(("VALIDATE_NEVER set\n")); 313 // if no-store validate cached response (see bug 112564) 314 if (cachedResponseHead->NoStore()) { 315 LOG(("Validating based on no-store logic\n")); 316 return true; 317 } 318 LOG(("NOT validating based on VALIDATE_NEVER load flag\n")); 319 return false; 320 } 321 322 // check if validation is strictly required... 323 if (cachedResponseHead->MustValidate()) { 324 LOG(("Validating based on MustValidate() returning TRUE\n")); 325 return true; 326 } 327 328 // possibly serve from cache for a custom If-Match/If-Unmodified-Since 329 // conditional request 330 if (customConditionalRequest && !requestHead.HasHeader(nsHttp::If_Match) && 331 !requestHead.HasHeader(nsHttp::If_Unmodified_Since)) { 332 LOG(("Validating based on a custom conditional request\n")); 333 return true; 334 } 335 336 // previously we also checked for a query-url w/out expiration 337 // and didn't do heuristic on it. but defacto that is allowed now. 338 // 339 // Check if the cache entry has expired... 340 341 bool doValidation = true; 342 uint32_t now = NowInSeconds(); 343 344 uint32_t age = 0; 345 nsresult rv = cachedResponseHead->ComputeCurrentAge(now, now, &age); 346 if (NS_FAILED(rv)) { 347 return true; 348 } 349 350 uint32_t freshness = 0; 351 rv = cachedResponseHead->ComputeFreshnessLifetime(&freshness); 352 if (NS_FAILED(rv)) { 353 return true; 354 } 355 356 uint32_t expiration = 0; 357 rv = entry->GetExpirationTime(&expiration); 358 if (NS_FAILED(rv)) { 359 return true; 360 } 361 362 uint32_t maxAgeRequest, maxStaleRequest, minFreshRequest; 363 364 LOG((" NowInSeconds()=%u, expiration time=%u, freshness lifetime=%u, age=%u", 365 now, expiration, freshness, age)); 366 367 if (cacheControlRequest.NoCache()) { 368 LOG((" validating, no-cache request")); 369 doValidation = true; 370 } else if (cacheControlRequest.MaxStale(&maxStaleRequest)) { 371 uint32_t staleTime = age > freshness ? age - freshness : 0; 372 doValidation = staleTime > maxStaleRequest; 373 LOG((" validating=%d, max-stale=%u requested", doValidation, 374 maxStaleRequest)); 375 } else if (cacheControlRequest.MaxAge(&maxAgeRequest)) { 376 // The input information for age and freshness calculation are in seconds. 377 // Hence, the internal logic can't have better resolution than seconds too. 378 // To make max-age=0 case work even for requests made in less than a second 379 // after the last response has been received, we use >= for compare. This 380 // is correct because of the rounding down of the age calculated value. 381 doValidation = age >= maxAgeRequest; 382 LOG((" validating=%d, max-age=%u requested", doValidation, maxAgeRequest)); 383 } else if (cacheControlRequest.MinFresh(&minFreshRequest)) { 384 uint32_t freshTime = freshness > age ? freshness - age : 0; 385 doValidation = freshTime < minFreshRequest; 386 LOG((" validating=%d, min-fresh=%u requested", doValidation, 387 minFreshRequest)); 388 } else if (now < expiration) { 389 doValidation = false; 390 LOG((" not validating, expire time not in the past")); 391 } else if (cachedResponseHead->MustValidateIfExpired()) { 392 doValidation = true; 393 } else if (cachedResponseHead->StaleWhileRevalidate(now, expiration) && 394 StaticPrefs::network_http_stale_while_revalidate_enabled()) { 395 LOG((" not validating, in the stall-while-revalidate window")); 396 doValidation = false; 397 if (performBackgroundRevalidation) { 398 *performBackgroundRevalidation = true; 399 } 400 } else if (loadFlags & nsIRequest::VALIDATE_ONCE_PER_SESSION) { 401 // If the cached response does not include expiration infor- 402 // mation, then we must validate the response, despite whether 403 // or not this is the first access this session. This behavior 404 // is consistent with existing browsers and is generally expected 405 // by web authors. 406 if (freshness == 0) { 407 doValidation = true; 408 } else { 409 doValidation = fromPreviousSession; 410 } 411 } else { 412 doValidation = true; 413 } 414 415 LOG(("%salidating based on expiration time\n", doValidation ? "V" : "Not v")); 416 return doValidation; 417 } 418 419 nsresult GetHttpResponseHeadFromCacheEntry( 420 nsICacheEntry* entry, nsHttpResponseHead* cachedResponseHead) { 421 nsCString buf; 422 // A "original-response-headers" metadata element holds network original 423 // headers, i.e. the headers in the form as they arrieved from the network. We 424 // need to get the network original headers first, because we need to keep 425 // them in order. 426 nsresult rv = entry->GetMetaDataElement("original-response-headers", 427 getter_Copies(buf)); 428 if (NS_SUCCEEDED(rv)) { 429 rv = cachedResponseHead->ParseCachedOriginalHeaders((char*)buf.get()); 430 if (NS_FAILED(rv)) { 431 LOG((" failed to parse original-response-headers\n")); 432 } 433 } 434 435 buf.Adopt(nullptr); 436 // A "response-head" metadata element holds response head, e.g. response 437 // status line and headers in the form Firefox uses them internally (no 438 // dupicate headers, etc.). 439 rv = entry->GetMetaDataElement("response-head", getter_Copies(buf)); 440 NS_ENSURE_SUCCESS(rv, rv); 441 442 // Parse string stored in a "response-head" metadata element. 443 // These response headers will be merged with the orignal headers (i.e. the 444 // headers stored in a "original-response-headers" metadata element). 445 rv = cachedResponseHead->ParseCachedHead(buf.get()); 446 NS_ENSURE_SUCCESS(rv, rv); 447 buf.Adopt(nullptr); 448 449 return NS_OK; 450 } 451 452 nsresult CheckPartial(nsICacheEntry* aEntry, int64_t* aSize, 453 int64_t* aContentLength, 454 nsHttpResponseHead* responseHead) { 455 nsresult rv; 456 457 rv = aEntry->GetDataSize(aSize); 458 459 if (NS_ERROR_IN_PROGRESS == rv) { 460 *aSize = -1; 461 rv = NS_OK; 462 } 463 464 NS_ENSURE_SUCCESS(rv, rv); 465 466 if (!responseHead) { 467 return NS_ERROR_UNEXPECTED; 468 } 469 470 *aContentLength = responseHead->ContentLength(); 471 472 return NS_OK; 473 } 474 475 void DetermineFramingAndImmutability(nsICacheEntry* entry, 476 nsHttpResponseHead* responseHead, 477 bool isHttps, bool* weaklyFramed, 478 bool* isImmutable) { 479 nsCString framedBuf; 480 nsresult rv = 481 entry->GetMetaDataElement("strongly-framed", getter_Copies(framedBuf)); 482 // describe this in terms of explicitly weakly framed so as to be backwards 483 // compatible with old cache contents which dont have strongly-framed makers 484 *weaklyFramed = NS_SUCCEEDED(rv) && framedBuf.EqualsLiteral("0"); 485 *isImmutable = !*weaklyFramed && isHttps && responseHead->Immutable(); 486 } 487 488 bool IsBeforeLastActiveTabLoadOptimization(TimeStamp const& when) { 489 return gHttpHandler && 490 gHttpHandler->IsBeforeLastActiveTabLoadOptimization(when); 491 } 492 493 nsCString ConvertRequestHeadToString(nsHttpRequestHead& aRequestHead, 494 bool aHasRequestBody, 495 bool aRequestBodyHasHeaders, 496 bool aUsingConnect) { 497 // Make sure that there is "Content-Length: 0" header in the requestHead 498 // in case of POST and PUT methods when there is no requestBody and 499 // requestHead doesn't contain "Transfer-Encoding" header. 500 // 501 // RFC1945 section 7.2.2: 502 // HTTP/1.0 requests containing an entity body must include a valid 503 // Content-Length header field. 504 // 505 // RFC2616 section 4.4: 506 // For compatibility with HTTP/1.0 applications, HTTP/1.1 requests 507 // containing a message-body MUST include a valid Content-Length header 508 // field unless the server is known to be HTTP/1.1 compliant. 509 if ((aRequestHead.IsPost() || aRequestHead.IsPut()) && !aHasRequestBody && 510 !aRequestHead.HasHeader(nsHttp::Transfer_Encoding)) { 511 DebugOnly<nsresult> rv = 512 aRequestHead.SetHeader(nsHttp::Content_Length, "0"_ns); 513 MOZ_ASSERT(NS_SUCCEEDED(rv)); 514 } 515 516 nsCString reqHeaderBuf; 517 reqHeaderBuf.Truncate(); 518 519 // make sure we eliminate any proxy specific headers from 520 // the request if we are using CONNECT 521 aRequestHead.Flatten(reqHeaderBuf, aUsingConnect); 522 523 if (!aRequestBodyHasHeaders || !aHasRequestBody) { 524 reqHeaderBuf.AppendLiteral("\r\n"); 525 } 526 527 return reqHeaderBuf; 528 } 529 530 void NotifyActiveTabLoadOptimization() { 531 if (gHttpHandler) { 532 gHttpHandler->NotifyActiveTabLoadOptimization(); 533 } 534 } 535 536 TimeStamp GetLastActiveTabLoadOptimizationHit() { 537 return gHttpHandler ? gHttpHandler->GetLastActiveTabLoadOptimizationHit() 538 : TimeStamp(); 539 } 540 541 void SetLastActiveTabLoadOptimizationHit(TimeStamp const& when) { 542 if (gHttpHandler) { 543 gHttpHandler->SetLastActiveTabLoadOptimizationHit(when); 544 } 545 } 546 547 } // namespace nsHttp 548 549 template <typename T> 550 void localEnsureBuffer(UniquePtr<T[]>& buf, uint32_t newSize, uint32_t preserve, 551 uint32_t& objSize) { 552 if (objSize >= newSize) return; 553 554 // Leave a little slop on the new allocation - add 2KB to 555 // what we need and then round the result up to a 4KB (page) 556 // boundary. 557 558 objSize = (newSize + 2048 + 4095) & ~4095; 559 560 static_assert(sizeof(T) == 1, "sizeof(T) must be 1"); 561 auto tmp = MakeUnique<T[]>(objSize); 562 if (preserve) { 563 memcpy(tmp.get(), buf.get(), preserve); 564 } 565 buf = std::move(tmp); 566 } 567 568 void EnsureBuffer(UniquePtr<char[]>& buf, uint32_t newSize, uint32_t preserve, 569 uint32_t& objSize) { 570 localEnsureBuffer<char>(buf, newSize, preserve, objSize); 571 } 572 573 void EnsureBuffer(UniquePtr<uint8_t[]>& buf, uint32_t newSize, 574 uint32_t preserve, uint32_t& objSize) { 575 localEnsureBuffer<uint8_t>(buf, newSize, preserve, objSize); 576 } 577 578 static bool IsTokenSymbol(signed char chr) { 579 return !(chr < 33 || chr == 127 || chr == '(' || chr == ')' || chr == '<' || 580 chr == '>' || chr == '@' || chr == ',' || chr == ';' || chr == ':' || 581 chr == '"' || chr == '/' || chr == '[' || chr == ']' || chr == '?' || 582 chr == '=' || chr == '{' || chr == '}' || chr == '\\'); 583 } 584 585 ParsedHeaderPair::ParsedHeaderPair(const char* name, int32_t nameLen, 586 const char* val, int32_t valLen, 587 bool isQuotedValue) 588 : mName(nsDependentCSubstring(nullptr, size_t(0))), 589 mValue(nsDependentCSubstring(nullptr, size_t(0))), 590 mIsQuotedValue(isQuotedValue) { 591 if (nameLen > 0) { 592 mName.Rebind(name, name + nameLen); 593 } 594 if (valLen > 0) { 595 if (mIsQuotedValue) { 596 RemoveQuotedStringEscapes(val, valLen); 597 mValue.Rebind(mUnquotedValue.BeginWriting(), mUnquotedValue.Length()); 598 } else { 599 mValue.Rebind(val, val + valLen); 600 } 601 } 602 } 603 604 void ParsedHeaderPair::RemoveQuotedStringEscapes(const char* val, 605 int32_t valLen) { 606 mUnquotedValue.Truncate(); 607 const char* c = val; 608 for (int32_t i = 0; i < valLen; ++i) { 609 if (c[i] == '\\' && c[i + 1]) { 610 ++i; 611 } 612 mUnquotedValue.Append(c[i]); 613 } 614 } 615 616 static void Tokenize( 617 const char* input, uint32_t inputLen, const char token, 618 const std::function<void(const char*, uint32_t)>& consumer) { 619 auto trimWhitespace = [](const char* in, uint32_t inLen, const char** out, 620 uint32_t* outLen) { 621 *out = in; 622 *outLen = inLen; 623 if (inLen == 0) { 624 return; 625 } 626 627 // Trim leading space 628 while (nsCRT::IsAsciiSpace(**out)) { 629 (*out)++; 630 --(*outLen); 631 } 632 633 // Trim tailing space 634 for (const char* i = *out + *outLen - 1; i >= *out; --i) { 635 if (!nsCRT::IsAsciiSpace(*i)) { 636 break; 637 } 638 --(*outLen); 639 } 640 }; 641 642 const char* first = input; 643 bool inQuote = false; 644 const char* result = nullptr; 645 uint32_t resultLen = 0; 646 for (uint32_t index = 0; index < inputLen; ++index) { 647 if (inQuote && input[index] == '\\' && input[index + 1]) { 648 index++; 649 continue; 650 } 651 if (input[index] == '"') { 652 inQuote = !inQuote; 653 continue; 654 } 655 if (inQuote) { 656 continue; 657 } 658 if (input[index] == token) { 659 trimWhitespace(first, (input + index) - first, &result, &resultLen); 660 consumer(result, resultLen); 661 first = input + index + 1; 662 } 663 } 664 665 trimWhitespace(first, (input + inputLen) - first, &result, &resultLen); 666 consumer(result, resultLen); 667 } 668 669 ParsedHeaderValueList::ParsedHeaderValueList(const char* t, uint32_t len, 670 bool allowInvalidValue) { 671 if (!len) { 672 return; 673 } 674 675 ParsedHeaderValueList* self = this; 676 auto consumer = [=](const char* output, uint32_t outputLength) { 677 self->ParseNameAndValue(output, allowInvalidValue); 678 }; 679 680 Tokenize(t, len, ';', consumer); 681 } 682 683 void ParsedHeaderValueList::ParseNameAndValue(const char* input, 684 bool allowInvalidValue) { 685 const char* nameStart = input; 686 const char* nameEnd = nullptr; 687 const char* valueStart = input; 688 const char* valueEnd = nullptr; 689 bool isQuotedString = false; 690 bool invalidValue = false; 691 692 for (; *input && *input != ';' && *input != ',' && 693 !nsCRT::IsAsciiSpace(*input) && *input != '='; 694 input++) { 695 ; 696 } 697 698 nameEnd = input; 699 700 if (!(nameEnd - nameStart)) { 701 return; 702 } 703 704 // Check whether param name is a valid token. 705 for (const char* c = nameStart; c < nameEnd; c++) { 706 if (!IsTokenSymbol(*c)) { 707 nameEnd = c; 708 break; 709 } 710 } 711 712 if (!(nameEnd - nameStart)) { 713 return; 714 } 715 716 while (nsCRT::IsAsciiSpace(*input)) { 717 ++input; 718 } 719 720 if (!*input || *input++ != '=') { 721 mValues.AppendElement( 722 ParsedHeaderPair(nameStart, nameEnd - nameStart, nullptr, 0, false)); 723 return; 724 } 725 726 while (nsCRT::IsAsciiSpace(*input)) { 727 ++input; 728 } 729 730 if (*input != '"') { 731 // The value is a token, not a quoted string. 732 valueStart = input; 733 for (valueEnd = input; *valueEnd && !nsCRT::IsAsciiSpace(*valueEnd) && 734 *valueEnd != ';' && *valueEnd != ','; 735 valueEnd++) { 736 ; 737 } 738 if (!allowInvalidValue) { 739 for (const char* c = valueStart; c < valueEnd; c++) { 740 if (!IsTokenSymbol(*c)) { 741 valueEnd = c; 742 break; 743 } 744 } 745 } 746 } else { 747 bool foundQuotedEnd = false; 748 isQuotedString = true; 749 750 ++input; 751 valueStart = input; 752 for (valueEnd = input; *valueEnd; ++valueEnd) { 753 if (*valueEnd == '\\' && *(valueEnd + 1)) { 754 ++valueEnd; 755 } else if (*valueEnd == '"') { 756 foundQuotedEnd = true; 757 break; 758 } 759 } 760 if (!foundQuotedEnd) { 761 invalidValue = true; 762 } 763 764 input = valueEnd; 765 // *valueEnd != null means that *valueEnd is quote character. 766 if (*valueEnd) { 767 input++; 768 } 769 } 770 771 if (invalidValue) { 772 valueEnd = valueStart; 773 } 774 775 mValues.AppendElement(ParsedHeaderPair(nameStart, nameEnd - nameStart, 776 valueStart, valueEnd - valueStart, 777 isQuotedString)); 778 } 779 780 ParsedHeaderValueListList::ParsedHeaderValueListList( 781 const nsCString& fullHeader, bool allowInvalidValue) 782 : mFull(fullHeader) { 783 auto& values = mValues; 784 auto consumer = [&values, allowInvalidValue](const char* output, 785 uint32_t outputLength) { 786 values.AppendElement( 787 ParsedHeaderValueList(output, outputLength, allowInvalidValue)); 788 }; 789 790 Tokenize(mFull.BeginReading(), mFull.Length(), ',', consumer); 791 } 792 793 Maybe<nsCString> CallingScriptLocationString() { 794 if (!LOG4_ENABLED() && !xpc::IsInAutomation()) { 795 return Nothing(); 796 } 797 798 JSContext* cx = nsContentUtils::GetCurrentJSContext(); 799 if (!cx) { 800 return Nothing(); 801 } 802 803 auto location = JSCallingLocation::Get(cx); 804 nsCString logString = ""_ns; 805 logString.AppendPrintf("%s:%u:%u", location.FileName().get(), location.mLine, 806 location.mColumn); 807 return Some(logString); 808 } 809 810 void LogCallingScriptLocation(void* instance) { 811 Maybe<nsCString> logLocation = CallingScriptLocationString(); 812 LogCallingScriptLocation(instance, logLocation); 813 } 814 815 void LogCallingScriptLocation(void* instance, 816 const Maybe<nsCString>& aLogLocation) { 817 if (aLogLocation.isNothing()) { 818 return; 819 } 820 821 nsCString logString; 822 logString.AppendPrintf("%p called from script: ", instance); 823 logString.AppendPrintf("%s", aLogLocation->get()); 824 LOG(("%s", logString.get())); 825 } 826 827 void LogHeaders(const char* lineStart) { 828 nsAutoCString buf; 829 const char* endOfLine; 830 while ((endOfLine = strstr(lineStart, "\r\n"))) { 831 buf.Assign(lineStart, endOfLine - lineStart); 832 if (StaticPrefs::network_http_sanitize_headers_in_logs() && 833 (nsCRT::strcasestr(buf.get(), "authorization: ") || 834 nsCRT::strcasestr(buf.get(), "proxy-authorization: "))) { 835 char* p = strchr(buf.BeginWriting(), ' '); 836 while (p && *++p) { 837 *p = '*'; 838 } 839 } 840 LOG1((" %s\n", buf.get())); 841 lineStart = endOfLine + 2; 842 } 843 } 844 845 nsresult HttpProxyResponseToErrorCode(uint32_t aStatusCode) { 846 // In proxy CONNECT case, we treat every response code except 200 as an error. 847 // Even if the proxy server returns other 2xx codes (i.e. 206), this function 848 // still returns an error code. 849 MOZ_ASSERT(aStatusCode != 200); 850 851 nsresult rv; 852 switch (aStatusCode) { 853 case 300: 854 case 301: 855 case 302: 856 case 303: 857 case 307: 858 case 308: 859 // Bad redirect: not top-level, or it's a POST, bad/missing Location, 860 // or ProcessRedirect() failed for some other reason. Legal 861 // redirects that fail because site not available, etc., are handled 862 // elsewhere, in the regular codepath. 863 rv = NS_ERROR_CONNECTION_REFUSED; 864 break; 865 // Squid sends 404 if DNS fails (regular 404 from target is tunneled) 866 case 404: // HTTP/1.1: "Not Found" 867 // RFC 2616: "some deployed proxies are known to return 400 or 868 // 500 when DNS lookups time out." (Squid uses 500 if it runs 869 // out of sockets: so we have a conflict here). 870 case 400: // HTTP/1.1 "Bad Request" 871 case 500: // HTTP/1.1: "Internal Server Error" 872 rv = NS_ERROR_UNKNOWN_HOST; 873 break; 874 case 401: 875 rv = NS_ERROR_PROXY_UNAUTHORIZED; 876 break; 877 case 402: 878 rv = NS_ERROR_PROXY_PAYMENT_REQUIRED; 879 break; 880 case 403: 881 rv = NS_ERROR_PROXY_FORBIDDEN; 882 break; 883 case 405: 884 rv = NS_ERROR_PROXY_METHOD_NOT_ALLOWED; 885 break; 886 case 406: 887 rv = NS_ERROR_PROXY_NOT_ACCEPTABLE; 888 break; 889 case 407: // ProcessAuthentication() failed (e.g. no header) 890 rv = NS_ERROR_PROXY_AUTHENTICATION_FAILED; 891 break; 892 case 408: 893 rv = NS_ERROR_PROXY_REQUEST_TIMEOUT; 894 break; 895 case 409: 896 rv = NS_ERROR_PROXY_CONFLICT; 897 break; 898 case 410: 899 rv = NS_ERROR_PROXY_GONE; 900 break; 901 case 411: 902 rv = NS_ERROR_PROXY_LENGTH_REQUIRED; 903 break; 904 case 412: 905 rv = NS_ERROR_PROXY_PRECONDITION_FAILED; 906 break; 907 case 413: 908 rv = NS_ERROR_PROXY_REQUEST_ENTITY_TOO_LARGE; 909 break; 910 case 414: 911 rv = NS_ERROR_PROXY_REQUEST_URI_TOO_LONG; 912 break; 913 case 415: 914 rv = NS_ERROR_PROXY_UNSUPPORTED_MEDIA_TYPE; 915 break; 916 case 416: 917 rv = NS_ERROR_PROXY_REQUESTED_RANGE_NOT_SATISFIABLE; 918 break; 919 case 417: 920 rv = NS_ERROR_PROXY_EXPECTATION_FAILED; 921 break; 922 case 421: 923 rv = NS_ERROR_PROXY_MISDIRECTED_REQUEST; 924 break; 925 case 425: 926 rv = NS_ERROR_PROXY_TOO_EARLY; 927 break; 928 case 426: 929 rv = NS_ERROR_PROXY_UPGRADE_REQUIRED; 930 break; 931 case 428: 932 rv = NS_ERROR_PROXY_PRECONDITION_REQUIRED; 933 break; 934 case 429: 935 rv = NS_ERROR_PROXY_TOO_MANY_REQUESTS; 936 break; 937 case 431: 938 rv = NS_ERROR_PROXY_REQUEST_HEADER_FIELDS_TOO_LARGE; 939 break; 940 case 451: 941 rv = NS_ERROR_PROXY_UNAVAILABLE_FOR_LEGAL_REASONS; 942 break; 943 case 501: 944 rv = NS_ERROR_PROXY_NOT_IMPLEMENTED; 945 break; 946 case 502: 947 rv = NS_ERROR_PROXY_BAD_GATEWAY; 948 break; 949 case 503: 950 // Squid returns 503 if target request fails for anything but DNS. 951 /* User sees: "Failed to Connect: 952 * Firefox can't establish a connection to the server at 953 * www.foo.com. Though the site seems valid, the browser 954 * was unable to establish a connection." 955 */ 956 rv = NS_ERROR_CONNECTION_REFUSED; 957 break; 958 // RFC 2616 uses 504 for both DNS and target timeout, so not clear what to 959 // do here: picking target timeout, as DNS covered by 400/404/500 960 case 504: 961 rv = NS_ERROR_PROXY_GATEWAY_TIMEOUT; 962 break; 963 case 505: 964 rv = NS_ERROR_PROXY_VERSION_NOT_SUPPORTED; 965 break; 966 case 506: 967 rv = NS_ERROR_PROXY_VARIANT_ALSO_NEGOTIATES; 968 break; 969 case 510: 970 rv = NS_ERROR_PROXY_NOT_EXTENDED; 971 break; 972 case 511: 973 rv = NS_ERROR_PROXY_NETWORK_AUTHENTICATION_REQUIRED; 974 break; 975 default: 976 rv = NS_ERROR_PROXY_CONNECTION_REFUSED; 977 break; 978 } 979 980 return rv; 981 } 982 983 SupportedAlpnRank IsAlpnSupported(const nsACString& aAlpn) { 984 if (nsHttpHandler::IsHttp3Enabled() && 985 gHttpHandler->IsHttp3VersionSupported(aAlpn)) { 986 return SupportedAlpnRank::HTTP_3_VER_1; 987 } 988 989 if (StaticPrefs::network_http_http2_enabled()) { 990 SpdyInformation* spdyInfo = gHttpHandler->SpdyInfo(); 991 if (aAlpn.Equals(spdyInfo->VersionString)) { 992 return SupportedAlpnRank::HTTP_2; 993 } 994 } 995 996 if (aAlpn.LowerCaseEqualsASCII("http/1.1")) { 997 return SupportedAlpnRank::HTTP_1_1; 998 } 999 1000 return SupportedAlpnRank::NOT_SUPPORTED; 1001 } 1002 1003 // NSS Errors which *may* have been triggered by the use of 0-RTT in the 1004 // presence of badly behaving middleboxes. We may re-attempt the connection 1005 // without early data. 1006 bool PossibleZeroRTTRetryError(nsresult aReason) { 1007 return (aReason == 1008 psm::GetXPCOMFromNSSError(SSL_ERROR_PROTOCOL_VERSION_ALERT)) || 1009 (aReason == psm::GetXPCOMFromNSSError(SSL_ERROR_BAD_MAC_ALERT)) || 1010 (aReason == 1011 psm::GetXPCOMFromNSSError(SSL_ERROR_HANDSHAKE_UNEXPECTED_ALERT)); 1012 } 1013 1014 nsresult MakeOriginURL(const nsACString& origin, nsCOMPtr<nsIURI>& url) { 1015 nsAutoCString scheme; 1016 nsresult rv = net_ExtractURLScheme(origin, scheme); 1017 NS_ENSURE_SUCCESS(rv, rv); 1018 return MakeOriginURL(scheme, origin, url); 1019 } 1020 1021 nsresult MakeOriginURL(const nsACString& scheme, const nsACString& origin, 1022 nsCOMPtr<nsIURI>& url) { 1023 return NS_MutateURI(new nsStandardURL::Mutator()) 1024 .Apply(&nsIStandardURLMutator::Init, nsIStandardURL::URLTYPE_AUTHORITY, 1025 scheme.EqualsLiteral("http") ? NS_HTTP_DEFAULT_PORT 1026 : NS_HTTPS_DEFAULT_PORT, 1027 origin, nullptr, nullptr, nullptr) 1028 .Finalize(url); 1029 } 1030 1031 void CreatePushHashKey(const nsCString& scheme, const nsCString& hostHeader, 1032 const mozilla::OriginAttributes& originAttributes, 1033 uint64_t serial, const nsACString& pathInfo, 1034 nsCString& outOrigin, nsCString& outKey) { 1035 nsCString fullOrigin = scheme; 1036 fullOrigin.AppendLiteral("://"); 1037 fullOrigin.Append(hostHeader); 1038 1039 nsCOMPtr<nsIURI> origin; 1040 nsresult rv = MakeOriginURL(scheme, fullOrigin, origin); 1041 1042 if (NS_SUCCEEDED(rv)) { 1043 rv = origin->GetAsciiSpec(outOrigin); 1044 outOrigin.Trim("/", false, true, false); 1045 } 1046 1047 if (NS_FAILED(rv)) { 1048 // Fallback to plain text copy - this may end up behaving poorly 1049 outOrigin = fullOrigin; 1050 } 1051 1052 outKey = outOrigin; 1053 outKey.AppendLiteral("/["); 1054 nsAutoCString suffix; 1055 originAttributes.CreateSuffix(suffix); 1056 outKey.Append(suffix); 1057 outKey.Append(']'); 1058 outKey.AppendLiteral("/[http2."); 1059 outKey.AppendInt(serial); 1060 outKey.Append(']'); 1061 outKey.Append(pathInfo); 1062 } 1063 1064 nsresult GetNSResultFromWebTransportError(uint8_t aErrorCode) { 1065 return static_cast<nsresult>((uint32_t)NS_ERROR_WEBTRANSPORT_CODE_BASE + 1066 (uint32_t)aErrorCode); 1067 } 1068 1069 uint8_t GetWebTransportErrorFromNSResult(nsresult aResult) { 1070 if (aResult < NS_ERROR_WEBTRANSPORT_CODE_BASE || 1071 aResult > NS_ERROR_WEBTRANSPORT_CODE_END) { 1072 return 0; 1073 } 1074 1075 return static_cast<uint8_t>((uint32_t)aResult - 1076 (uint32_t)NS_ERROR_WEBTRANSPORT_CODE_BASE); 1077 } 1078 1079 uint64_t WebTransportErrorToHttp3Error(uint8_t aErrorCode) { 1080 return kWebTransportErrorCodeStart + aErrorCode + aErrorCode / 0x1e; 1081 } 1082 1083 uint8_t Http3ErrorToWebTransportError(uint64_t aErrorCode) { 1084 // Ensure the code is within the valid range. 1085 if (aErrorCode < kWebTransportErrorCodeStart || 1086 aErrorCode > kWebTransportErrorCodeEnd) { 1087 return 0; 1088 } 1089 1090 uint64_t shifted = aErrorCode - kWebTransportErrorCodeStart; 1091 uint64_t result = shifted - shifted / 0x1f; 1092 1093 if (result <= std::numeric_limits<uint8_t>::max()) { 1094 return (uint8_t)result; 1095 } 1096 1097 return 0; 1098 } 1099 1100 ConnectionCloseReason ToCloseReason(nsresult aErrorCode) { 1101 if (NS_SUCCEEDED(aErrorCode)) { 1102 return ConnectionCloseReason::OK; 1103 } 1104 1105 if (aErrorCode == NS_ERROR_UNKNOWN_HOST) { 1106 return ConnectionCloseReason::DNS_ERROR; 1107 } 1108 if (aErrorCode == NS_ERROR_NET_RESET) { 1109 return ConnectionCloseReason::NET_RESET; 1110 } 1111 if (aErrorCode == NS_ERROR_CONNECTION_REFUSED) { 1112 return ConnectionCloseReason::NET_REFUSED; 1113 } 1114 if (aErrorCode == NS_ERROR_SOCKET_ADDRESS_NOT_SUPPORTED) { 1115 return ConnectionCloseReason::SOCKET_ADDRESS_NOT_SUPPORTED; 1116 } 1117 if (aErrorCode == NS_ERROR_NET_TIMEOUT) { 1118 return ConnectionCloseReason::NET_TIMEOUT; 1119 } 1120 if (aErrorCode == NS_ERROR_OUT_OF_MEMORY) { 1121 return ConnectionCloseReason::OUT_OF_MEMORY; 1122 } 1123 if (aErrorCode == NS_ERROR_SOCKET_ADDRESS_IN_USE) { 1124 return ConnectionCloseReason::SOCKET_ADDRESS_IN_USE; 1125 } 1126 if (aErrorCode == NS_BINDING_ABORTED) { 1127 return ConnectionCloseReason::BINDING_ABORTED; 1128 } 1129 if (aErrorCode == NS_BINDING_REDIRECTED) { 1130 return ConnectionCloseReason::BINDING_REDIRECTED; 1131 } 1132 if (aErrorCode == NS_ERROR_ABORT) { 1133 return ConnectionCloseReason::ERROR_ABORT; 1134 } 1135 1136 int32_t code = -1 * NS_ERROR_GET_CODE(aErrorCode); 1137 if (mozilla::psm::IsNSSErrorCode(code)) { 1138 return ConnectionCloseReason::SECURITY_ERROR; 1139 } 1140 1141 return ConnectionCloseReason::OTHER_NET_ERROR; 1142 } 1143 1144 void DisallowHTTPSRR(uint32_t& aCaps) { 1145 // NS_HTTP_DISALLOW_HTTPS_RR should take precedence than 1146 // NS_HTTP_FORCE_WAIT_HTTP_RR. 1147 aCaps = (aCaps | NS_HTTP_DISALLOW_HTTPS_RR) & ~NS_HTTP_FORCE_WAIT_HTTP_RR; 1148 } 1149 1150 // Convert HttpVersion enum to Telemetry label string 1151 nsLiteralCString HttpVersionToTelemetryLabel(HttpVersion version) { 1152 switch (version) { 1153 case HttpVersion::v0_9: 1154 case HttpVersion::v1_0: 1155 case HttpVersion::v1_1: 1156 return "http_1"_ns; 1157 case HttpVersion::v2_0: 1158 return "http_2"_ns; 1159 case HttpVersion::v3_0: 1160 return "http_3"_ns; 1161 default: 1162 break; 1163 } 1164 return "unknown"_ns; 1165 } 1166 1167 ProxyDNSStrategy GetProxyDNSStrategyHelper(const char* aType, uint32_t aFlag) { 1168 if (!aType) { 1169 return ProxyDNSStrategy::ORIGIN; 1170 } 1171 1172 if (!(aFlag & nsIProxyInfo::TRANSPARENT_PROXY_RESOLVES_HOST)) { 1173 if (aType == kProxyType_SOCKS) { 1174 return ProxyDNSStrategy::ORIGIN; 1175 } 1176 } 1177 1178 return ProxyDNSStrategy::PROXY; 1179 } 1180 1181 } // namespace net 1182 } // namespace mozilla