Files
palemoon27/netwerk/protocol/http/nsHttp.cpp
T
roytam1 fe3519e975 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1257611 - Fix wrong CondVar::Wait() and Monitor::Wait() usage in netwerk/cache2, r=honzab (54f1a79f43)
- Bug 1239687 - Leak invalid/doomed file handles immediately after HTTP cache shutdown. r=michal (a20bcf662b)
- Bug 996836 - Merge CLOSE and WRITE priority in cache2 I/O thread. r=michal (8b0af50593)
- Bug 1265243 - Do not initialize CacheObserver in the child process, r=honzab (87f69529d2)
- Bug 1247432 - Don't do any unnecessary I/O in cache2 after shutdown. r=michal (7c5a2b89ac)
- Bug 1220272 - Fix signalling in HTTP cache test code suspender. r=michal (293a16731d)
- Bug 1160368 - Part 2: Clean up some cruft in nsCookie.h from PRBool/bool conversions. r=jdm (aa873f2dcc)
- Bug 1160368 - Part 5: Clean up some confusing browser_storage_listings.js checks. r=jdm (b27f5930a9)
- Bug 866380 - Null check for amlogic libc implementation. r=valentin (c3a487af9c)
- Bug 1257320 - Disable C4577 to unblock compilation on VS2015; r=mayhemer (2f800ca85c)
- Bug 1229726 - fix the data copy from sockaddr to NetAddr on OSX/FreeBSD. r=mcmanus. (fde11e004e)
- Bug 1241368 - provide JS implemented MDNS service as a fallback. r=nwgh,nalexander. (afe1445eb8)
- Bug 571126: Remove NECKO_SEPARATE_STACKS support, r=jduell (fb5b87bb3a)
- Bug 1057689 - Add xpcshell test checking correct notifications and app-offline state r=jduell (89946b44ba)
- Bug 1259459 - h2 0 length options puts end-stream on headers r=hurley (618480a609)
- Bug 1234251 - Remove HttpChannelChild::mSynthesizedResponseHead; r=jdm (20981affea)
- Bug 904559 - Veto redirect when target channel doesn't implement nsIParentRedirectingChannel. r=jduell (b3da2fae91)
- Bug 1261784 Make sure InterceptedChannel body streams are always closed. r=jdm (cd50b1a52b)
- Bug 1224508 - Use URI path without reference as the callback key. r=valentin. (b8a953f23a)
- Bug 1226760 - ViewSource doesn't work for packaged app resources r=mcmanus (ec9b0297de)
- Bug 1254061 - Rewrite nsHttp::ParseInt64 using strtoll r=mcmanus (2125b8ae6f)
- Bug 1241565 - nsIHttpChannelInternal should be a builtinclass, r=honzab.moz (d24da6a95a)
- Bug 1252386 - Removed debugging printf,r=me (c5d89f353c)
- bug 277813 - autogenerated expires needs max r=mayhemer (6aa7c255a5)
- Bug 1225384 - Change how the default resource "host names" are handled. r=michal (868732baab)
- Bug 719905: Fix resolution of resource: URLs containing : and / characters. r=valentin (f60f4baacf)
- Bug 1195173 - Convert ExtensionProtocolHandler to use channel->Open2() (r=maglione) (063f5d5d10)
- Bug 1226909 followup to fix bustage. Make sure that the channel returned by NS_NewChannel doesn't have a loadinfo that indicates that the channel has already been opened (42ebe0f44e)
- Bug 241698 - Fixed init and use of nsDirIndex::mLastModified (-1LL) + built in nullptr checks where they were missing. r=dragana (e9c2277a3d)
- Bug 1261318 - make sure brotli context is created in onstoprequest r=bagder (6646fed64d)
- Bug 1212223 - Update |bufLen| as well when we probed the multipart preamble. r=valentin. (ec878c5b0f)
- Bug 1259561: Increase CRAZY_COORD (threshold for debug build layout warnings) by an order of magnitude. r=mats (c05c16dd85)
- Bug 1261698. Make ReparentFrameViewTo return void because it always returns NS_OK. r=mats (e806d6abcb)
- Bug 1261698. Remove comment that is not relevant in nsContainerFrame.cpp. (f570189d15)
- Bug 1261698. Don't descend into child frames looking for views in ReparentFrameViewTo if the frame doesn't have the NS_FRAME_HAS_CHILD_WITH_VIEW bit set. r=mats (0d42befd46)
- Bug 1265154 - Fix compile error in MSVC 2013 caused by ArrayLength; r=heycam (1c962f2840)
- Bug 1260351 - Image: Enable ConvolveVertically/Horizontally in LS3 MMI. r=tnikkel (576b6bbdb3)
- Bug 1209780 (Part 1) - Mark DrawResult MOZ_MUST_USE. r=tn (e6c113bef2)
- Bug 1253753 - Remove unnecessary switch fallthrough to avoid -Wimplicit-fallthrough warning. r=karlt (96bd93fc5c)
- bug 1260178 null check pattern from -unico-border-gradient r=acomminos (f63c9c7ffb)
- Bug 1247796. Use keyboardFocusIndicatorColor for ActiveBorder system color keyword. r=mstange (7ed133de97)
- Bug 1248675 - Update the cached mBounds in nsChildView when its backing scale factor (display DPI) changes. r=mstange (8197274118)
- Bug 1256576 - Make sure texture is (re)initialized if the size changed. r=snorp (2c56790ca9)
- Bug 1242449 - Fix confusion among CSS, desktop and device pixel units in nsXULWindow position/size and window staggering so as to work consistently across mixed resolution displays. r=emk (f73d2fd41d)
- Bug 1255645 - Ensure nsXULWindow constrains the window to the bounds of its screen after applying intrinsic sizing (if appropriate), by re-doing positioning after the window has been sized properly. r=emk (e87e0cea81)
- Bug 1259492 - Ensure window position is constrained to the screen after it has been sized properly in nsXULWindow::OnChromeLoaded. r=emk (7cf599b39b)
- Bug 1832708 - Disable std::__throw_* wrapping on libc++ >= 14.0. r=firefox-build-system-reviewers,andi (29e0cc9319)
2024-08-09 14:38:26 +08:00

484 lines
12 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim:set ts=4 sw=4 sts=4 et cin: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// HttpLog.h should generally be included first
#include "HttpLog.h"
#include "nsHttp.h"
#include "PLDHashTable.h"
#include "mozilla/Mutex.h"
#include "mozilla/HashFunctions.h"
#include "nsCRT.h"
#include <errno.h>
namespace mozilla {
namespace net {
// define storage for all atoms
#define HTTP_ATOM(_name, _value) nsHttpAtom nsHttp::_name = { _value };
#include "nsHttpAtomList.h"
#undef HTTP_ATOM
// find out how many atoms we have
#define HTTP_ATOM(_name, _value) Unused_ ## _name,
enum {
#include "nsHttpAtomList.h"
NUM_HTTP_ATOMS
};
#undef HTTP_ATOM
// we keep a linked list of atoms allocated on the heap for easy clean up when
// the atom table is destroyed. The structure and value string are allocated
// as one contiguous block.
struct HttpHeapAtom {
struct HttpHeapAtom *next;
char value[1];
};
static PLDHashTable *sAtomTable;
static struct HttpHeapAtom *sHeapAtoms = nullptr;
static Mutex *sLock = nullptr;
HttpHeapAtom *
NewHeapAtom(const char *value) {
int len = strlen(value);
HttpHeapAtom *a =
reinterpret_cast<HttpHeapAtom *>(moz_xmalloc(sizeof(*a) + len));
if (!a)
return nullptr;
memcpy(a->value, value, len + 1);
// add this heap atom to the list of all heap atoms
a->next = sHeapAtoms;
sHeapAtoms = a;
return a;
}
// Hash string ignore case, based on PL_HashString
static PLDHashNumber
StringHash(const void *key)
{
PLDHashNumber h = 0;
for (const char *s = reinterpret_cast<const char*>(key); *s; ++s)
h = AddToHash(h, nsCRT::ToLower(*s));
return h;
}
static bool
StringCompare(const PLDHashEntryHdr *entry, const void *testKey)
{
const void *entryKey =
reinterpret_cast<const PLDHashEntryStub *>(entry)->key;
return PL_strcasecmp(reinterpret_cast<const char *>(entryKey),
reinterpret_cast<const char *>(testKey)) == 0;
}
static const PLDHashTableOps ops = {
StringHash,
StringCompare,
PLDHashTable::MoveEntryStub,
PLDHashTable::ClearEntryStub,
nullptr
};
// We put the atoms in a hash table for speedy lookup.. see ResolveAtom.
nsresult
nsHttp::CreateAtomTable()
{
MOZ_ASSERT(!sAtomTable, "atom table already initialized");
if (!sLock) {
sLock = new Mutex("nsHttp.sLock");
}
// The initial length for this table is a value greater than the number of
// known atoms (NUM_HTTP_ATOMS) because we expect to encounter a few random
// headers right off the bat.
sAtomTable = new PLDHashTable(&ops, sizeof(PLDHashEntryStub),
NUM_HTTP_ATOMS + 10);
// fill the table with our known atoms
const char *const atoms[] = {
#define HTTP_ATOM(_name, _value) nsHttp::_name._val,
#include "nsHttpAtomList.h"
#undef HTTP_ATOM
nullptr
};
for (int i = 0; atoms[i]; ++i) {
auto stub = static_cast<PLDHashEntryStub*>
(sAtomTable->Add(atoms[i], fallible));
if (!stub)
return NS_ERROR_OUT_OF_MEMORY;
MOZ_ASSERT(!stub->key, "duplicate static atom");
stub->key = atoms[i];
}
return NS_OK;
}
void
nsHttp::DestroyAtomTable()
{
delete sAtomTable;
sAtomTable = nullptr;
while (sHeapAtoms) {
HttpHeapAtom *next = sHeapAtoms->next;
free(sHeapAtoms);
sHeapAtoms = next;
}
delete sLock;
sLock = nullptr;
}
Mutex *
nsHttp::GetLock()
{
return sLock;
}
// this function may be called from multiple threads
nsHttpAtom
nsHttp::ResolveAtom(const char *str)
{
nsHttpAtom atom = { nullptr };
if (!str || !sAtomTable)
return atom;
MutexAutoLock lock(*sLock);
auto stub = static_cast<PLDHashEntryStub*>(sAtomTable->Add(str, fallible));
if (!stub)
return atom; // out of memory
if (stub->key) {
atom._val = reinterpret_cast<const char *>(stub->key);
return atom;
}
// if the atom could not be found in the atom table, then we'll go
// and allocate a new atom on the heap.
HttpHeapAtom *heapAtom = NewHeapAtom(str);
if (!heapAtom)
return atom; // out of memory
stub->key = atom._val = heapAtom->value;
return atom;
}
//
// From section 2.2 of RFC 2616, a token is defined as:
//
// token = 1*<any CHAR except CTLs or separators>
// CHAR = <any US-ASCII character (octets 0 - 127)>
// separators = "(" | ")" | "<" | ">" | "@"
// | "," | ";" | ":" | "\" | <">
// | "/" | "[" | "]" | "?" | "="
// | "{" | "}" | SP | HT
// CTL = <any US-ASCII control character
// (octets 0 - 31) and DEL (127)>
// SP = <US-ASCII SP, space (32)>
// HT = <US-ASCII HT, horizontal-tab (9)>
//
static const char kValidTokenMap[128] = {
0, 0, 0, 0, 0, 0, 0, 0, // 0
0, 0, 0, 0, 0, 0, 0, 0, // 8
0, 0, 0, 0, 0, 0, 0, 0, // 16
0, 0, 0, 0, 0, 0, 0, 0, // 24
0, 1, 0, 1, 1, 1, 1, 1, // 32
0, 0, 1, 1, 0, 1, 1, 0, // 40
1, 1, 1, 1, 1, 1, 1, 1, // 48
1, 1, 0, 0, 0, 0, 0, 0, // 56
0, 1, 1, 1, 1, 1, 1, 1, // 64
1, 1, 1, 1, 1, 1, 1, 1, // 72
1, 1, 1, 1, 1, 1, 1, 1, // 80
1, 1, 1, 0, 0, 0, 1, 1, // 88
1, 1, 1, 1, 1, 1, 1, 1, // 96
1, 1, 1, 1, 1, 1, 1, 1, // 104
1, 1, 1, 1, 1, 1, 1, 1, // 112
1, 1, 1, 0, 1, 0, 1, 0 // 120
};
bool
nsHttp::IsValidToken(const char *start, const char *end)
{
if (start == end)
return false;
for (; start != end; ++start) {
const unsigned char idx = *start;
if (idx > 127 || !kValidTokenMap[idx])
return false;
}
return true;
}
const char*
nsHttp::GetProtocolVersion(uint32_t pv)
{
switch (pv) {
case SPDY_VERSION_31:
return "spdy/3.1";
case HTTP_VERSION_2:
case NS_HTTP_VERSION_2_0:
return "h2";
case NS_HTTP_VERSION_1_0:
return "http/1.0";
case NS_HTTP_VERSION_1_1:
return "http/1.1";
default:
NS_WARNING(nsPrintfCString("Unkown protocol version: 0x%X. "
"Please file a bug", pv).get());
return "http/1.1";
}
}
// static
bool
nsHttp::IsReasonableHeaderValue(const nsACString &s)
{
// Header values MUST NOT contain line-breaks. RFC 2616 technically
// permits CTL characters, including CR and LF, in header values provided
// they are quoted. However, this can lead to problems if servers do not
// interpret quoted strings properly. Disallowing CR and LF here seems
// reasonable and keeps things simple. We also disallow a null byte.
const nsACString::char_type* end = s.EndReading();
for (const nsACString::char_type* i = s.BeginReading(); i != end; ++i) {
if (*i == '\r' || *i == '\n' || *i == '\0') {
return false;
}
}
return true;
}
const char *
nsHttp::FindToken(const char *input, const char *token, const char *seps)
{
if (!input)
return nullptr;
int inputLen = strlen(input);
int tokenLen = strlen(token);
if (inputLen < tokenLen)
return nullptr;
const char *inputTop = input;
const char *inputEnd = input + inputLen - tokenLen;
for (; input <= inputEnd; ++input) {
if (PL_strncasecmp(input, token, tokenLen) == 0) {
if (input > inputTop && !strchr(seps, *(input - 1)))
continue;
if (input < inputEnd && !strchr(seps, *(input + tokenLen)))
continue;
return input;
}
}
return nullptr;
}
bool
nsHttp::ParseInt64(const char *input, const char **next, int64_t *r)
{
MOZ_ASSERT(input);
MOZ_ASSERT(r);
char *end = nullptr;
errno = 0; // Clear errno to make sure its value is set by strtoll
int64_t value = strtoll(input, &end, /* base */ 10);
// Fail if: - the parsed number overflows.
// - the end points to the start of the input string.
// - we parsed a negative value. Consumers don't expect that.
if (errno != 0 || end == input || value < 0) {
LOG(("nsHttp::ParseInt64 value=%ld errno=%d", value, errno));
return false;
}
if (next) {
*next = end;
}
*r = value;
return true;
}
bool
nsHttp::IsPermanentRedirect(uint32_t httpStatus)
{
return httpStatus == 301 || httpStatus == 308;
}
template<typename T> void
localEnsureBuffer(UniquePtr<T[]> &buf, uint32_t newSize,
uint32_t preserve, uint32_t &objSize)
{
if (objSize >= newSize)
return;
// Leave a little slop on the new allocation - add 2KB to
// what we need and then round the result up to a 4KB (page)
// boundary.
objSize = (newSize + 2048 + 4095) & ~4095;
static_assert(sizeof(T) == 1, "sizeof(T) must be 1");
auto tmp = MakeUnique<T[]>(objSize);
if (preserve) {
memcpy(tmp.get(), buf.get(), preserve);
}
buf = Move(tmp);
}
void EnsureBuffer(UniquePtr<char[]> &buf, uint32_t newSize,
uint32_t preserve, uint32_t &objSize)
{
localEnsureBuffer<char> (buf, newSize, preserve, objSize);
}
void EnsureBuffer(UniquePtr<uint8_t[]> &buf, uint32_t newSize,
uint32_t preserve, uint32_t &objSize)
{
localEnsureBuffer<uint8_t> (buf, newSize, preserve, objSize);
}
///
void
ParsedHeaderValueList::Tokenize(char *input, uint32_t inputLen, char **token,
uint32_t *tokenLen, bool *foundEquals, char **next)
{
if (foundEquals) {
*foundEquals = false;
}
if (next) {
*next = nullptr;
}
if (inputLen < 1 || !input || !token) {
return;
}
bool foundFirst = false;
bool inQuote = false;
bool foundToken = false;
*token = input;
*tokenLen = inputLen;
for (uint32_t index = 0; !foundToken && index < inputLen; ++index) {
// strip leading cruft
if (!foundFirst &&
(input[index] == ' ' || input[index] == '"' || input[index] == '\t')) {
(*token)++;
} else {
foundFirst = true;
}
if (input[index] == '"') {
inQuote = !inQuote;
continue;
}
if (inQuote) {
continue;
}
if (input[index] == '=' || input[index] == ';') {
*tokenLen = (input + index) - *token;
if (next && ((index + 1) < inputLen)) {
*next = input + index + 1;
}
foundToken = true;
if (foundEquals && input[index] == '=') {
*foundEquals = true;
}
break;
}
}
if (!foundToken) {
*tokenLen = (input + inputLen) - *token;
}
// strip trailing cruft
for (char *index = *token + *tokenLen - 1; index >= *token; --index) {
if (*index != ' ' && *index != '\t' && *index != '"') {
break;
}
--(*tokenLen);
if (*index == '"') {
break;
}
}
}
ParsedHeaderValueList::ParsedHeaderValueList(char *t, uint32_t len)
{
char *name = nullptr;
uint32_t nameLen = 0;
char *value = nullptr;
uint32_t valueLen = 0;
char *next = nullptr;
bool foundEquals;
while (t) {
Tokenize(t, len, &name, &nameLen, &foundEquals, &next);
if (next) {
len -= next - t;
}
t = next;
if (foundEquals && t) {
Tokenize(t, len, &value, &valueLen, nullptr, &next);
if (next) {
len -= next - t;
}
t = next;
}
mValues.AppendElement(ParsedHeaderPair(name, nameLen, value, valueLen));
value = name = nullptr;
valueLen = nameLen = 0;
next = nullptr;
}
}
ParsedHeaderValueListList::ParsedHeaderValueListList(const nsCString &fullHeader)
: mFull(fullHeader)
{
char *t = mFull.BeginWriting();
uint32_t len = mFull.Length();
char *last = t;
bool inQuote = false;
for (uint32_t index = 0; index < len; ++index) {
if (t[index] == '"') {
inQuote = !inQuote;
continue;
}
if (inQuote) {
continue;
}
if (t[index] == ',') {
mValues.AppendElement(ParsedHeaderValueList(last, (t + index) - last));
last = t + index + 1;
}
}
if (!inQuote) {
mValues.AppendElement(ParsedHeaderValueList(last, (t + len) - last));
}
}
} // namespace net
} // namespace mozilla