This source file includes following definitions.
- skipWhiteSpace
- skipToken
- skipEquals
- skipValue
- isValidHTTPHeaderValue
- isValidHTTPToken
- trimInputSample
- contentDispositionType
- parseHTTPRefresh
- parseDate
- filenameFromHTTPContentDisposition
- extractMIMETypeFromMediaType
- extractCharsetFromMediaType
- findCharsetInMediaType
- parseXSSProtectionHeader
- parseContentTypeOptionsHeader
- extractReasonPhraseFromHTTPStatusLine
- parseXFrameOptionsHeader
- parseRange
- parseHTTPRequestLine
- parseHTTPHeader
- parseHTTPRequestBody
#include "config.h"
#include "platform/network/HTTPParsers.h"
#include "wtf/DateMath.h"
#include "wtf/text/CString.h"
#include "wtf/text/StringBuilder.h"
#include "wtf/text/WTFString.h"
#include "wtf/unicode/CharacterNames.h"
using namespace WTF;
namespace WebCore {
static inline bool skipWhiteSpace(const String& str, unsigned& pos, bool fromHttpEquivMeta)
{
unsigned len = str.length();
if (fromHttpEquivMeta) {
while (pos < len && str[pos] <= ' ')
++pos;
} else {
while (pos < len && (str[pos] == '\t' || str[pos] == ' '))
++pos;
}
return pos < len;
}
static inline bool skipToken(const String& str, unsigned& pos, const char* token)
{
unsigned len = str.length();
unsigned current = pos;
while (current < len && *token) {
if (toASCIILower(str[current]) != *token++)
return false;
++current;
}
if (*token)
return false;
pos = current;
return true;
}
static inline bool skipEquals(const String& str, unsigned &pos)
{
return skipWhiteSpace(str, pos, false) && str[pos++] == '=' && skipWhiteSpace(str, pos, false);
}
static inline bool skipValue(const String& str, unsigned& pos)
{
unsigned start = pos;
unsigned len = str.length();
while (pos < len) {
if (str[pos] == ' ' || str[pos] == '\t' || str[pos] == ';')
break;
++pos;
}
return pos != start;
}
bool isValidHTTPHeaderValue(const String& name)
{
return name.containsOnlyLatin1() && !name.contains('\r') && !name.contains('\n');
}
bool isValidHTTPToken(const String& characters)
{
if (characters.isEmpty())
return false;
for (unsigned i = 0; i < characters.length(); ++i) {
UChar c = characters[i];
if (c <= 0x20 || c >= 0x7F
|| c == '(' || c == ')' || c == '<' || c == '>' || c == '@'
|| c == ',' || c == ';' || c == ':' || c == '\\' || c == '"'
|| c == '/' || c == '[' || c == ']' || c == '?' || c == '='
|| c == '{' || c == '}')
return false;
}
return true;
}
static const size_t maxInputSampleSize = 128;
static String trimInputSample(const char* p, size_t length)
{
if (length > maxInputSampleSize)
return String(p, maxInputSampleSize) + horizontalEllipsis;
return String(p, length);
}
ContentDispositionType contentDispositionType(const String& contentDisposition)
{
if (contentDisposition.isEmpty())
return ContentDispositionNone;
Vector<String> parameters;
contentDisposition.split(';', parameters);
if (parameters.isEmpty())
return ContentDispositionNone;
String dispositionType = parameters[0];
dispositionType.stripWhiteSpace();
if (equalIgnoringCase(dispositionType, "inline"))
return ContentDispositionInline;
if (!isValidHTTPToken(dispositionType))
return ContentDispositionNone;
return ContentDispositionAttachment;
}
bool parseHTTPRefresh(const String& refresh, bool fromHttpEquivMeta, double& delay, String& url)
{
unsigned len = refresh.length();
unsigned pos = 0;
if (!skipWhiteSpace(refresh, pos, fromHttpEquivMeta))
return false;
while (pos != len && refresh[pos] != ',' && refresh[pos] != ';')
++pos;
if (pos == len) {
url = String();
bool ok;
delay = refresh.stripWhiteSpace().toDouble(&ok);
return ok;
} else {
bool ok;
delay = refresh.left(pos).stripWhiteSpace().toDouble(&ok);
if (!ok)
return false;
++pos;
skipWhiteSpace(refresh, pos, fromHttpEquivMeta);
unsigned urlStartPos = pos;
if (refresh.find("url", urlStartPos, false) == urlStartPos) {
urlStartPos += 3;
skipWhiteSpace(refresh, urlStartPos, fromHttpEquivMeta);
if (refresh[urlStartPos] == '=') {
++urlStartPos;
skipWhiteSpace(refresh, urlStartPos, fromHttpEquivMeta);
} else {
urlStartPos = pos;
}
}
unsigned urlEndPos = len;
if (refresh[urlStartPos] == '"' || refresh[urlStartPos] == '\'') {
UChar quotationMark = refresh[urlStartPos];
urlStartPos++;
while (urlEndPos > urlStartPos) {
urlEndPos--;
if (refresh[urlEndPos] == quotationMark)
break;
}
if (urlEndPos == urlStartPos)
urlEndPos = len;
}
url = refresh.substring(urlStartPos, urlEndPos - urlStartPos).stripWhiteSpace();
return true;
}
}
double parseDate(const String& value)
{
return parseDateFromNullTerminatedCharacters(value.utf8().data());
}
String filenameFromHTTPContentDisposition(const String& value)
{
Vector<String> keyValuePairs;
value.split(';', keyValuePairs);
unsigned length = keyValuePairs.size();
for (unsigned i = 0; i < length; i++) {
size_t valueStartPos = keyValuePairs[i].find('=');
if (valueStartPos == kNotFound)
continue;
String key = keyValuePairs[i].left(valueStartPos).stripWhiteSpace();
if (key.isEmpty() || key != "filename")
continue;
String value = keyValuePairs[i].substring(valueStartPos + 1).stripWhiteSpace();
if (value[0] == '\"')
value = value.substring(1, value.length() - 2);
return value;
}
return String();
}
AtomicString extractMIMETypeFromMediaType(const AtomicString& mediaType)
{
StringBuilder mimeType;
unsigned length = mediaType.length();
mimeType.reserveCapacity(length);
for (unsigned i = 0; i < length; i++) {
UChar c = mediaType[i];
if (c == ';')
break;
if (c == ',')
break;
if (isSpaceOrNewline(c))
continue;
mimeType.append(c);
}
if (mimeType.length() == length)
return mediaType;
return mimeType.toAtomicString();
}
String extractCharsetFromMediaType(const String& mediaType)
{
unsigned pos, len;
findCharsetInMediaType(mediaType, pos, len);
return mediaType.substring(pos, len);
}
void findCharsetInMediaType(const String& mediaType, unsigned& charsetPos, unsigned& charsetLen, unsigned start)
{
charsetPos = start;
charsetLen = 0;
size_t pos = start;
unsigned length = mediaType.length();
while (pos < length) {
pos = mediaType.find("charset", pos, false);
if (pos == kNotFound || !pos) {
charsetLen = 0;
return;
}
if (mediaType[pos-1] > ' ' && mediaType[pos-1] != ';') {
pos += 7;
continue;
}
pos += 7;
while (pos != length && mediaType[pos] <= ' ')
++pos;
if (mediaType[pos++] != '=')
continue;
while (pos != length && (mediaType[pos] <= ' ' || mediaType[pos] == '"' || mediaType[pos] == '\''))
++pos;
unsigned endpos = pos;
while (pos != length && mediaType[endpos] > ' ' && mediaType[endpos] != '"' && mediaType[endpos] != '\'' && mediaType[endpos] != ';')
++endpos;
charsetPos = pos;
charsetLen = endpos - pos;
return;
}
}
ReflectedXSSDisposition parseXSSProtectionHeader(const String& header, String& failureReason, unsigned& failurePosition, String& reportURL)
{
DEFINE_STATIC_LOCAL(String, failureReasonInvalidToggle, ("expected 0 or 1"));
DEFINE_STATIC_LOCAL(String, failureReasonInvalidSeparator, ("expected semicolon"));
DEFINE_STATIC_LOCAL(String, failureReasonInvalidEquals, ("expected equals sign"));
DEFINE_STATIC_LOCAL(String, failureReasonInvalidMode, ("invalid mode directive"));
DEFINE_STATIC_LOCAL(String, failureReasonInvalidReport, ("invalid report directive"));
DEFINE_STATIC_LOCAL(String, failureReasonDuplicateMode, ("duplicate mode directive"));
DEFINE_STATIC_LOCAL(String, failureReasonDuplicateReport, ("duplicate report directive"));
DEFINE_STATIC_LOCAL(String, failureReasonInvalidDirective, ("unrecognized directive"));
unsigned pos = 0;
if (!skipWhiteSpace(header, pos, false))
return ReflectedXSSUnset;
if (header[pos] == '0')
return AllowReflectedXSS;
if (header[pos++] != '1') {
failureReason = failureReasonInvalidToggle;
return ReflectedXSSInvalid;
}
ReflectedXSSDisposition result = FilterReflectedXSS;
bool modeDirectiveSeen = false;
bool reportDirectiveSeen = false;
while (1) {
if (!skipWhiteSpace(header, pos, false))
return result;
if (header[pos++] != ';') {
failureReason = failureReasonInvalidSeparator;
failurePosition = pos;
return ReflectedXSSInvalid;
}
if (!skipWhiteSpace(header, pos, false))
return result;
if (skipToken(header, pos, "mode")) {
if (modeDirectiveSeen) {
failureReason = failureReasonDuplicateMode;
failurePosition = pos;
return ReflectedXSSInvalid;
}
modeDirectiveSeen = true;
if (!skipEquals(header, pos)) {
failureReason = failureReasonInvalidEquals;
failurePosition = pos;
return ReflectedXSSInvalid;
}
if (!skipToken(header, pos, "block")) {
failureReason = failureReasonInvalidMode;
failurePosition = pos;
return ReflectedXSSInvalid;
}
result = BlockReflectedXSS;
} else if (skipToken(header, pos, "report")) {
if (reportDirectiveSeen) {
failureReason = failureReasonDuplicateReport;
failurePosition = pos;
return ReflectedXSSInvalid;
}
reportDirectiveSeen = true;
if (!skipEquals(header, pos)) {
failureReason = failureReasonInvalidEquals;
failurePosition = pos;
return ReflectedXSSInvalid;
}
size_t startPos = pos;
if (!skipValue(header, pos)) {
failureReason = failureReasonInvalidReport;
failurePosition = pos;
return ReflectedXSSInvalid;
}
reportURL = header.substring(startPos, pos - startPos);
failurePosition = startPos;
} else {
failureReason = failureReasonInvalidDirective;
failurePosition = pos;
return ReflectedXSSInvalid;
}
}
}
ContentTypeOptionsDisposition parseContentTypeOptionsHeader(const String& header)
{
if (header.stripWhiteSpace().lower() == "nosniff")
return ContentTypeOptionsNosniff;
return ContentTypeOptionsNone;
}
String extractReasonPhraseFromHTTPStatusLine(const String& statusLine)
{
size_t spacePos = statusLine.find(' ');
spacePos = statusLine.find(' ', spacePos + 1);
return statusLine.substring(spacePos + 1);
}
XFrameOptionsDisposition parseXFrameOptionsHeader(const String& header)
{
XFrameOptionsDisposition result = XFrameOptionsNone;
if (header.isEmpty())
return result;
Vector<String> headers;
header.split(',', headers);
for (size_t i = 0; i < headers.size(); i++) {
String currentHeader = headers[i].stripWhiteSpace();
XFrameOptionsDisposition currentValue = XFrameOptionsNone;
if (equalIgnoringCase(currentHeader, "deny"))
currentValue = XFrameOptionsDeny;
else if (equalIgnoringCase(currentHeader, "sameorigin"))
currentValue = XFrameOptionsSameOrigin;
else if (equalIgnoringCase(currentHeader, "allowall"))
currentValue = XFrameOptionsAllowAll;
else
currentValue = XFrameOptionsInvalid;
if (result == XFrameOptionsNone)
result = currentValue;
else if (result != currentValue)
return XFrameOptionsConflict;
}
return result;
}
bool parseRange(const String& range, long long& rangeOffset, long long& rangeEnd, long long& rangeSuffixLength)
{
rangeOffset = rangeEnd = rangeSuffixLength = -1;
static const char bytesStart[] = "bytes=";
if (!range.startsWith(bytesStart, false))
return false;
String byteRange = range.substring(sizeof(bytesStart) - 1);
int index = byteRange.find('-');
if (index == -1)
return false;
if (!index) {
String suffixLengthString = byteRange.substring(index + 1).stripWhiteSpace();
bool ok;
long long value = suffixLengthString.toInt64Strict(&ok);
if (ok)
rangeSuffixLength = value;
return true;
}
String firstBytePosStr = byteRange.left(index).stripWhiteSpace();
bool ok;
long long firstBytePos = firstBytePosStr.toInt64Strict(&ok);
if (!ok)
return false;
String lastBytePosStr = byteRange.substring(index + 1).stripWhiteSpace();
long long lastBytePos = -1;
if (!lastBytePosStr.isEmpty()) {
lastBytePos = lastBytePosStr.toInt64Strict(&ok);
if (!ok)
return false;
}
if (firstBytePos < 0 || !(lastBytePos == -1 || lastBytePos >= firstBytePos))
return false;
rangeOffset = firstBytePos;
rangeEnd = lastBytePos;
return true;
}
size_t parseHTTPRequestLine(const char* data, size_t length, String& failureReason, String& method, String& url, HTTPVersion& httpVersion)
{
method = String();
url = String();
httpVersion = Unknown;
const char* space1 = 0;
const char* space2 = 0;
const char* p;
size_t consumedLength;
for (p = data, consumedLength = 0; consumedLength < length; p++, consumedLength++) {
if (*p == ' ') {
if (!space1)
space1 = p;
else if (!space2)
space2 = p;
} else if (*p == '\n') {
break;
}
}
if (consumedLength == length) {
failureReason = "Incomplete Request Line";
return 0;
}
if (!space1 || !space2) {
failureReason = "Request Line does not appear to contain: <Method> <Url> <HTTPVersion>.";
return 0;
}
const char* end = p + 1;
if (*(end - 2) != '\r') {
failureReason = "Request line does not end with CRLF";
return 0;
}
method = String(data, space1 - data);
url = String(space1 + 1, space2 - space1 - 1);
String httpVersionString(space2 + 1, end - space2 - 3);
if (httpVersionString.length() != 8 || !httpVersionString.startsWith("HTTP/1."))
httpVersion = Unknown;
else if (httpVersionString[7] == '0')
httpVersion = HTTP_1_0;
else if (httpVersionString[7] == '1')
httpVersion = HTTP_1_1;
else
httpVersion = Unknown;
return end - data;
}
size_t parseHTTPHeader(const char* start, size_t length, String& failureReason, AtomicString& nameStr, AtomicString& valueStr)
{
const char* p = start;
const char* end = start + length;
Vector<char> name;
Vector<char> value;
nameStr = nullAtom;
valueStr = nullAtom;
for (; p < end; p++) {
switch (*p) {
case '\r':
if (name.isEmpty()) {
if (p + 1 < end && *(p + 1) == '\n')
return (p + 2) - start;
failureReason = "CR doesn't follow LF at " + trimInputSample(p, end - p);
return 0;
}
failureReason = "Unexpected CR in name at " + trimInputSample(name.data(), name.size());
return 0;
case '\n':
failureReason = "Unexpected LF in name at " + trimInputSample(name.data(), name.size());
return 0;
case ':':
break;
default:
name.append(*p);
continue;
}
if (*p == ':') {
++p;
break;
}
}
for (; p < end && *p == 0x20; p++) { }
for (; p < end; p++) {
switch (*p) {
case '\r':
break;
case '\n':
failureReason = "Unexpected LF in value at " + trimInputSample(value.data(), value.size());
return 0;
default:
value.append(*p);
}
if (*p == '\r') {
++p;
break;
}
}
if (p >= end || *p != '\n') {
failureReason = "CR doesn't follow LF after value at " + trimInputSample(p, end - p);
return 0;
}
nameStr = AtomicString::fromUTF8(name.data(), name.size());
valueStr = AtomicString::fromUTF8(value.data(), value.size());
if (nameStr.isNull()) {
failureReason = "Invalid UTF-8 sequence in header name";
return 0;
}
if (valueStr.isNull()) {
failureReason = "Invalid UTF-8 sequence in header value";
return 0;
}
return p - start;
}
size_t parseHTTPRequestBody(const char* data, size_t length, Vector<unsigned char>& body)
{
body.clear();
body.append(data, length);
return length;
}
}