WebServer Headers for Request are treated case-insensitive

To achieve this, Request Headers are stored in both "original" and "lowercase".
In this way we don't mangle with the request object we have received, while
still be able to handle headers when NOT in the classic "Camel-Case" format.

Fixes #11421.
1.x
Ivan De Marino 2013-06-21 01:41:20 +01:00
parent edf2d90a11
commit c466d8aeef
3 changed files with 18 additions and 12 deletions

View File

@ -37,6 +37,9 @@
#define PHANTOMJS_VERSION_PATCH 0
#define PHANTOMJS_VERSION_STRING "1.10.0 (development)"
#define HTTP_HEADER_CONTENT_LENGTH "content-length"
#define HTTP_HEADER_CONTENT_TYPE "content-type"
#define COFFEE_SCRIPT_EXTENSION ".coffee"
#define JS_ELEMENT_CLICK "(function (el) { " \

View File

@ -33,6 +33,7 @@
#include "encoding.h"
#include "mongoose/mongoose.h"
#include "consts.h"
#include <QByteArray>
#include <QHostAddress>
@ -195,18 +196,20 @@ bool WebServer::handleRequest(mg_event event, mg_connection *conn, const mg_requ
#endif
QVariantMap headersObject;
QMap<QString, QString> ciHeadersObject; //< FIXME: "case-insensitive" Headers. This shows how desperately we need a better HTTP Server
for (int i = 0; i < request->num_headers; ++i) {
QString key = QString::fromLocal8Bit(request->http_headers[i].name);
QString value = QString::fromLocal8Bit(request->http_headers[i].value);
qDebug() << "HTTP Request - Receiving Header" << key << "=" << value;
headersObject[key] = value;
ciHeadersObject[key.toLower()] = value;
}
requestObject["headers"] = headersObject;
// Read request body ONLY for POST and PUT, and ONLY if the "Content-Length" is provided
if ((requestObject["method"] == "POST" || requestObject["method"] == "PUT") && headersObject.contains("Content-Length")) {
if ((requestObject["method"] == "POST" || requestObject["method"] == "PUT") && ciHeadersObject.contains(HTTP_HEADER_CONTENT_LENGTH)) {
bool contentLengthKnown = false;
uint contentLength = headersObject["Content-Length"].toUInt(&contentLengthKnown);
uint contentLength = ciHeadersObject[HTTP_HEADER_CONTENT_LENGTH].toUInt(&contentLengthKnown);
qDebug() << "HTTP Request - Method POST/PUT";
@ -220,7 +223,7 @@ bool WebServer::handleRequest(mg_event event, mg_connection *conn, const mg_requ
qDebug() << "HTTP Request - Content Body:" << qPrintable(data);
// Check if the 'Content-Type' requires decoding
if (headersObject["Content-Type"] == "application/x-www-form-urlencoded") {
if (ciHeadersObject[HTTP_HEADER_CONTENT_TYPE] == "application/x-www-form-urlencoded") {
requestObject["post"] = UrlEncodedParser::parse(QByteArray(data, read));
requestObject["postRaw"] = QString::fromUtf8(data, read);
} else {

View File

@ -1211,9 +1211,9 @@ describe("WebPage object", function() {
expect(status).toEqual('success');
});
});
waits(5000);
runs(function() {
page.close();
expect(handled).toBeTruthy();
@ -1277,7 +1277,7 @@ describe("WebPage object", function() {
expect(handled).toBe(true);
});
});
it('should fire `onResourceReceived` callback when the resource error occured', function() {
var page = require('webpage').create();
var server = require('webserver').create();
@ -2113,7 +2113,7 @@ describe("WebPage network request headers handling", function() {
};
runs(function() {
page.open("http://localhost:12345", function() {
page.open("http://localhost:12345", function(status) {
expect(status).toEqual("success");
});
});
@ -2130,7 +2130,7 @@ describe("WebPage network request headers handling", function() {
it("should remove HTTP header from a network request", function() {
var page = require("webpage").create();
page.customHeaders = {"CustomHeader": "CustomValue"};
var server = require("webserver").create();
var handled = false;
@ -2146,7 +2146,7 @@ describe("WebPage network request headers handling", function() {
};
runs(function() {
page.open("http://localhost:12345", function() {
page.open("http://localhost:12345", function(status) {
expect(status).toEqual("success");
});
});
@ -2163,12 +2163,12 @@ describe("WebPage network request headers handling", function() {
it("should set HTTP header value for a network request", function() {
var page = require("webpage").create();
page.customHeaders = {"CustomHeader": "CustomValue"};
var server = require("webserver").create();
var handled = false;
server.listen(12345, function(request) {
if (request.headers["CustomHeader"] &&
if (request.headers["CustomHeader"] &&
request.headers["CustomHeader"] === "ChangedCustomValue") {
handled = true;
}
@ -2180,7 +2180,7 @@ describe("WebPage network request headers handling", function() {
};
runs(function() {
page.open("http://localhost:12345", function() {
page.open("http://localhost:12345", function(status) {
expect(status).toEqual("success");
});
});