void WebInspectorServer::didReceiveUnrecognizedHTTPRequest(WebSocketServerConnection* connection, PassRefPtr<HTTPRequest> request) { // request->url() contains only the path extracted from the HTTP request line // and URL is poor at parsing incomplete URLs, so extract the interesting parts manually. String path = request->url(); size_t pathEnd = path.find('?'); if (pathEnd == notFound) pathEnd = path.find('#'); if (pathEnd != notFound) path.truncate(pathEnd); // Ask for the complete payload in memory for the sake of simplicity. A more efficient way would be // to ask for header data and then let the platform abstraction write the payload straight on the connection. Vector<char> body; String contentType; bool found = platformResourceForPath(path, body, contentType); HTTPHeaderMap headerFields; headerFields.set("Connection", "close"); headerFields.set("Content-Length", String::number(body.size())); if (found) headerFields.set("Content-Type", contentType); // Send when ready and close immediately afterwards. connection->sendHTTPResponseHeader(found ? 200 : 404, found ? "OK" : "Not Found", headerFields); connection->sendRawData(body.data(), body.size()); connection->shutdownAfterSendOrNow(); }
static HTTPHeaderMap parseRFC822HeaderFields(const char* bytes, unsigned length) { String lastHeaderKey; HTTPHeaderMap headerFields; // Loop over lines until we're past the header, or we can't find any more end-of-lines while (const char* endOfLine = findEndOfLine(bytes, length)) { const char* line = bytes; int lineLength = endOfLine - bytes; // Move bytes to the character after the terminator as returned by findEndOfLine. bytes = endOfLine + 1; if ((*endOfLine == '\r') && (*bytes == '\n')) bytes++; // Safe since findEndOfLine won't return a spanning CRLF. length -= (bytes - line); if (!lineLength) { // Blank line; we're at the end of the header break; } if (*line == ' ' || *line == '\t') { // Continuation of the previous header if (lastHeaderKey.isNull()) { // malformed header; ignore it and continue continue; } // Merge the continuation of the previous header String currentValue = headerFields.get(lastHeaderKey); String newValue(line, lineLength); headerFields.set(lastHeaderKey, currentValue + newValue); } else { // Brand new header const char* colon = line; while (*colon != ':' && colon != endOfLine) colon++; if (colon == endOfLine) { // malformed header; ignore it and continue continue; } lastHeaderKey = capitalizeRFC822HeaderFieldName(String(line, colon - line)); String value; for (colon++; colon != endOfLine; colon++) { if (*colon != ' ' && *colon != '\t') break; } if (colon == endOfLine) value = ""; else value = String(colon, endOfLine - colon); String oldValue = headerFields.get(lastHeaderKey); if (!oldValue.isNull()) value = oldValue + ", " + value; headerFields.set(lastHeaderKey, value); } } return headerFields; }
static void addToHTTPHeaderMap(const void* key, const void* value, void* context) { HTTPHeaderMap* httpHeaderMap = (HTTPHeaderMap*)context; httpHeaderMap->set((CFStringRef)key, (CFStringRef)value); }
static inline HTTPHeaderMap parseRFC822HeaderFields(const Vector<char>& buffer, unsigned length) { const char* bytes = buffer.data(); const char* eol; String lastKey; HTTPHeaderMap headerFields; // Loop ove rlines until we're past the header, or we can't find any more end-of-lines while ((eol = findEOL(bytes, length))) { const char* line = bytes; int lineLength = eol - bytes; // Move bytes to the character after the terminator as returned by findEOL. bytes = eol + 1; if ((*eol == '\r') && (*bytes == '\n')) bytes++; // Safe since findEOL won't return a spanning CRLF. length -= (bytes - line); if (lineLength == 0) // Blank line; we're at the end of the header break; else if (*line == ' ' || *line == '\t') { // Continuation of the previous header if (lastKey.isNull()) { // malformed header; ignore it and continue continue; } else { // Merge the continuation of the previous header String currentValue = headerFields.get(lastKey); String newValue(line, lineLength); headerFields.set(lastKey, currentValue + newValue); } } else { // Brand new header const char* colon; for (colon = line; *colon != ':' && colon != eol; colon++) { // empty loop } if (colon == eol) // malformed header; ignore it and continue continue; else { lastKey = capitalizeRFC822HeaderFieldName(String(line, colon - line)); String value; for (colon++; colon != eol; colon++) { if (*colon != ' ' && *colon != '\t') break; } if (colon == eol) value = ""; else value = String(colon, eol - colon); String oldValue = headerFields.get(lastKey); if (!oldValue.isNull()) { String tmp = oldValue; tmp += ", "; tmp += value; value = tmp; } headerFields.set(lastKey, value); } } } return headerFields; }