2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:set ts=4 sw=4 sts=4 ci et: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-22 09:01:06 -07:00
|
|
|
// HttpLog.h should generally be included first
|
|
|
|
#include "HttpLog.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsHttpHeaderArray.h"
|
|
|
|
#include "nsHttp.h"
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpHeaderArray <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
nsresult
|
|
|
|
nsHttpHeaderArray::SetHeader(nsHttpAtom header,
|
|
|
|
const nsACString &value,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool merge)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
nsEntry *entry = nullptr;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t index;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
index = LookupEntry(header, &entry);
|
|
|
|
|
|
|
|
// If an empty value is passed in, then delete the header entry...
|
|
|
|
// unless we are merging, in which case this function becomes a NOP.
|
|
|
|
if (value.IsEmpty()) {
|
|
|
|
if (!merge && entry)
|
|
|
|
mHeaders.RemoveElementAt(index);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry) {
|
2011-07-04 23:12:32 -07:00
|
|
|
entry = mHeaders.AppendElement(); // new nsEntry()
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!entry)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
entry->header = header;
|
|
|
|
entry->value = value;
|
2011-07-04 23:12:32 -07:00
|
|
|
} else if (merge && !IsSingletonHeader(header)) {
|
|
|
|
MergeHeader(header, entry, value);
|
|
|
|
} else {
|
|
|
|
// Replace the existing string with the new value
|
|
|
|
entry->value = value;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-07-04 23:12:32 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpHeaderArray::SetHeaderFromNet(nsHttpAtom header, const nsACString &value)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
nsEntry *entry = nullptr;
|
2011-07-04 23:12:32 -07:00
|
|
|
|
2012-08-25 11:19:00 -07:00
|
|
|
LookupEntry(header, &entry);
|
2011-07-04 23:12:32 -07:00
|
|
|
|
|
|
|
if (!entry) {
|
2012-01-20 09:33:01 -08:00
|
|
|
if (value.IsEmpty()) {
|
2012-02-09 11:47:30 -08:00
|
|
|
if (!TrackEmptyHeader(header)) {
|
|
|
|
LOG(("Ignoring Empty Header: %s\n", header.get()));
|
|
|
|
return NS_OK; // ignore empty headers by default
|
|
|
|
}
|
2012-01-20 09:33:01 -08:00
|
|
|
}
|
2011-07-04 23:12:32 -07:00
|
|
|
entry = mHeaders.AppendElement(); //new nsEntry(header, value);
|
|
|
|
if (!entry)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
entry->header = header;
|
2007-03-22 10:30:00 -07:00
|
|
|
entry->value = value;
|
2011-07-04 23:12:32 -07:00
|
|
|
} else if (!IsSingletonHeader(header)) {
|
|
|
|
MergeHeader(header, entry, value);
|
|
|
|
} else {
|
|
|
|
// Multiple instances of non-mergeable header received from network
|
|
|
|
// - ignore if same value
|
|
|
|
if (!entry->value.Equals(value)) {
|
|
|
|
if (IsSuspectDuplicateHeader(header)) {
|
|
|
|
// reply may be corrupt/hacked (ex: CLRF injection attacks)
|
|
|
|
return NS_ERROR_CORRUPTED_CONTENT;
|
|
|
|
} // else silently drop value: keep value from 1st header seen
|
2012-02-09 11:47:30 -08:00
|
|
|
LOG(("Header %s silently dropped as non mergeable header\n",
|
|
|
|
header.get()));
|
2011-07-04 23:12:32 -07:00
|
|
|
}
|
|
|
|
}
|
2011-05-31 16:51:51 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpHeaderArray::ClearHeader(nsHttpAtom header)
|
|
|
|
{
|
|
|
|
mHeaders.RemoveElement(header, nsEntry::MatchHeader());
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2012-04-05 01:21:02 -07:00
|
|
|
nsHttpHeaderArray::PeekHeader(nsHttpAtom header) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
const nsEntry *entry = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
LookupEntry(header, &entry);
|
2012-07-30 07:20:58 -07:00
|
|
|
return entry ? entry->value.get() : nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-04-05 01:21:02 -07:00
|
|
|
nsHttpHeaderArray::GetHeader(nsHttpAtom header, nsACString &result) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
const nsEntry *entry = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
LookupEntry(header, &entry);
|
|
|
|
if (!entry)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
result = entry->value;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpHeaderArray::VisitHeaders(nsIHttpHeaderVisitor *visitor)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(visitor);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i, count = mHeaders.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
const nsEntry &entry = mHeaders[i];
|
|
|
|
if (NS_FAILED(visitor->VisitHeader(nsDependentCString(entry.header),
|
|
|
|
entry.value)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-31 16:51:51 -07:00
|
|
|
nsresult
|
2010-05-01 17:37:00 -07:00
|
|
|
nsHttpHeaderArray::ParseHeaderLine(const char *line,
|
|
|
|
nsHttpAtom *hdr,
|
|
|
|
char **val)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// BNF from section 4.2 of RFC 2616:
|
|
|
|
//
|
|
|
|
// message-header = field-name ":" [ field-value ]
|
|
|
|
// field-name = token
|
|
|
|
// field-value = *( field-content | LWS )
|
|
|
|
// field-content = <the OCTETs making up the field-value
|
|
|
|
// and consisting of either *TEXT or combinations
|
|
|
|
// of token, separators, and quoted-string>
|
|
|
|
//
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// We skip over mal-formed headers in the hope that we'll still be able to
|
|
|
|
// do something useful with the response.
|
|
|
|
|
|
|
|
char *p = (char *) strchr(line, ':');
|
|
|
|
if (!p) {
|
|
|
|
LOG(("malformed header [%s]: no colon\n", line));
|
2011-05-31 16:51:51 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we have a valid token for the field-name
|
|
|
|
if (!nsHttp::IsValidToken(line, p)) {
|
|
|
|
LOG(("malformed header [%s]: field-name not a token\n", line));
|
2011-05-31 16:51:51 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-05-16 06:30:42 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
*p = 0; // null terminate field-name
|
|
|
|
|
|
|
|
nsHttpAtom atom = nsHttp::ResolveAtom(line);
|
|
|
|
if (!atom) {
|
|
|
|
LOG(("failed to resolve atom [%s]\n", line));
|
2011-05-31 16:51:51 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// skip over whitespace
|
|
|
|
p = net_FindCharNotInSet(++p, HTTP_LWS);
|
|
|
|
|
|
|
|
// trim trailing whitespace - bug 86608
|
|
|
|
char *p2 = net_RFindCharNotInSet(p, HTTP_LWS);
|
|
|
|
|
|
|
|
*++p2 = 0; // null terminate header value; if all chars starting at |p|
|
|
|
|
// consisted of LWS, then p2 would have pointed at |p-1|, so
|
|
|
|
// the prefix increment is always valid.
|
|
|
|
|
|
|
|
// assign return values
|
|
|
|
if (hdr) *hdr = atom;
|
|
|
|
if (val) *val = p;
|
|
|
|
|
|
|
|
// assign response header
|
2011-07-04 23:12:32 -07:00
|
|
|
return SetHeaderFromNet(atom, nsDependentCString(p, p2 - p));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsHttpHeaderArray::Flatten(nsACString &buf, bool pruneProxyHeaders)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i, count = mHeaders.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
const nsEntry &entry = mHeaders[i];
|
|
|
|
// prune proxy headers if requested
|
2013-05-16 06:30:42 -07:00
|
|
|
if (pruneProxyHeaders && ((entry.header == nsHttp::Proxy_Authorization) ||
|
2007-03-22 10:30:00 -07:00
|
|
|
(entry.header == nsHttp::Proxy_Connection)))
|
|
|
|
continue;
|
|
|
|
buf.Append(entry.header);
|
|
|
|
buf.AppendLiteral(": ");
|
|
|
|
buf.Append(entry.value);
|
|
|
|
buf.AppendLiteral("\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHttpHeaderArray::PeekHeaderAt(uint32_t index, nsHttpAtom &header) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
const nsEntry &entry = mHeaders[index];
|
|
|
|
|
|
|
|
header = entry.header;
|
|
|
|
return entry.value.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpHeaderArray::Clear()
|
|
|
|
{
|
|
|
|
mHeaders.Clear();
|
|
|
|
}
|