// Returns the header length (including "\r\n"), or -1 if we have not received enough data yet. // If the line is malformed or the status code is not a 3-digit number, // statusCode and statusText will be set to -1 and a null string, respectively. int WebSocketHandshake::readStatusLine(const char* header, size_t headerLength, int& statusCode, String& statusText) { // Arbitrary size limit to prevent the server from sending an unbounded // amount of data with no newlines and forcing us to buffer it all. static const int maximumLength = 1024; statusCode = -1; statusText = String(); const char* space1 = 0; const char* space2 = 0; const char* p; size_t consumedLength; for (p = header, consumedLength = 0; consumedLength < headerLength; p++, consumedLength++) { if (*p == ' ') { if (!space1) space1 = p; else if (!space2) space2 = p; } else if (*p == '\0') { // The caller isn't prepared to deal with null bytes in status // line. WebSockets specification doesn't prohibit this, but HTTP // does, so we'll just treat this as an error. m_failureReason = "Status line contains embedded null"; return p + 1 - header; } else if (*p == '\n') break; } if (consumedLength == headerLength) return -1; // We have not received '\n' yet. const char* end = p + 1; int lineLength = end - header; if (lineLength > maximumLength) { m_failureReason = "Status line is too long"; return maximumLength; } // The line must end with "\r\n". if (lineLength < 2 || *(end - 2) != '\r') { m_failureReason = "Status line does not end with CRLF"; return lineLength; } if (!space1 || !space2) { m_failureReason = "No response code found: " + trimInputSample(header, lineLength - 2); return lineLength; } String statusCodeString(space1 + 1, space2 - space1 - 1); if (statusCodeString.length() != 3) // Status code must consist of three digits. return lineLength; for (int i = 0; i < 3; ++i) if (statusCodeString[i] < '0' || statusCodeString[i] > '9') { m_failureReason = "Invalid status code: " + statusCodeString; return lineLength; } bool ok = false; statusCode = statusCodeString.toInt(&ok); ASSERT(ok); statusText = String(space2 + 1, end - space2 - 3); // Exclude "\r\n". return lineLength; }
size_t parseHTTPHeader(const char* start, size_t length, String& failureReason, AtomicString& nameStr, String& valueStr) { const char* p = start; const char* end = start + length; Vector<char> name; Vector<char> value; nameStr = AtomicString(); valueStr = String(); 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 = String::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; }