Skip to content
Permalink
Browse files

Debugger: Fix connection in Firefox.

Firefox now requires HTTP/1.1 or higher, which is reasonable since 1.0 is
ancient.  The Upgrade response and WebSocket protocol handling are all
HTTP/1.1 safe, so let's just support both versions.
  • Loading branch information...
unknownbrackets committed Jun 23, 2019
1 parent 22b7349 commit 65ac59021c168c0a564e85c82462686f24a908ed
Showing with 15 additions and 15 deletions.
  1. +6 −6 Core/WebServer.cpp
  2. +4 −4 ext/native/net/http_server.cpp
  3. +1 −1 ext/native/net/http_server.h
  4. +4 −4 ext/native/net/websocket_server.cpp
@@ -146,24 +146,24 @@ static void RegisterDiscHandlers(http::Server *http, std::unordered_map<std::str

std::string range;
if (request.Method() == http::RequestHeader::HEAD) {
request.WriteHttpResponseHeader(200, sz, "application/octet-stream", "Accept-Ranges: bytes\r\n");
request.WriteHttpResponseHeader("1.0", 200, sz, "application/octet-stream", "Accept-Ranges: bytes\r\n");
} else if (request.GetHeader("range", &range)) {
s64 begin = 0, last = 0;
if (sscanf(range.c_str(), "bytes=%lld-%lld", &begin, &last) != 2) {
request.WriteHttpResponseHeader(400, -1, "text/plain");
request.WriteHttpResponseHeader("1.0", 400, -1, "text/plain");
request.Out()->Push("Could not understand range request.");
return;
}

if (begin < 0 || begin > last || last >= sz) {
request.WriteHttpResponseHeader(416, -1, "text/plain");
request.WriteHttpResponseHeader("1.0", 416, -1, "text/plain");
request.Out()->Push("Range goes outside of file.");
return;
}

FILE *fp = File::OpenCFile(filename, "rb");
if (!fp || fseek(fp, begin, SEEK_SET) != 0) {
request.WriteHttpResponseHeader(500, -1, "text/plain");
request.WriteHttpResponseHeader("1.0", 500, -1, "text/plain");
request.Out()->Push("File access failed.");
if (fp) {
fclose(fp);
@@ -174,7 +174,7 @@ static void RegisterDiscHandlers(http::Server *http, std::unordered_map<std::str
s64 len = last - begin + 1;
char contentRange[1024];
sprintf(contentRange, "Content-Range: bytes %lld-%lld/%lld\r\n", begin, last, sz);
request.WriteHttpResponseHeader(206, len, "application/octet-stream", contentRange);
request.WriteHttpResponseHeader("1.0", 206, len, "application/octet-stream", contentRange);

const size_t CHUNK_SIZE = 16 * 1024;
char *buf = new char[CHUNK_SIZE];
@@ -188,7 +188,7 @@ static void RegisterDiscHandlers(http::Server *http, std::unordered_map<std::str
delete [] buf;
request.Out()->Flush();
} else {
request.WriteHttpResponseHeader(418, -1, "text/plain");
request.WriteHttpResponseHeader("1.0", 418, -1, "text/plain");
request.Out()->Push("This server only supports range requests.");
}
};
@@ -70,7 +70,7 @@ Request::~Request() {
delete out_;
}

void Request::WriteHttpResponseHeader(int status, int64_t size, const char *mimeType, const char *otherHeaders) const {
void Request::WriteHttpResponseHeader(const char *ver, int status, int64_t size, const char *mimeType, const char *otherHeaders) const {
const char *statusStr;
switch (status) {
case 200: statusStr = "OK"; break;
@@ -92,7 +92,7 @@ void Request::WriteHttpResponseHeader(int status, int64_t size, const char *mime
}

net::OutputSink *buffer = Out();
buffer->Printf("HTTP/1.0 %03d %s\r\n", status, statusStr);
buffer->Printf("HTTP/%s %03d %s\r\n", ver, status, statusStr);
buffer->Push("Server: PPSSPPServer v0.1\r\n");
if (!mimeType || strcmp(mimeType, "websocket") != 0) {
buffer->Printf("Content-Type: %s\r\n", mimeType ? mimeType : DEFAULT_MIME_TYPE);
@@ -313,12 +313,12 @@ void Server::HandleRequestDefault(const Request &request) {
void Server::Handle404(const Request &request) {
ILOG("No handler for '%s', falling back to 404.", request.resource());
const char *payload = "<html><body>404 not found</body></html>\r\n";
request.WriteHttpResponseHeader(404, (int)strlen(payload));
request.WriteHttpResponseHeader("1.0", 404, (int)strlen(payload));
request.Out()->Push(payload);
}

void Server::HandleListing(const Request &request) {
request.WriteHttpResponseHeader(200, -1, "text/plain");
request.WriteHttpResponseHeader("1.0", 200, -1, "text/plain");
for (auto iter = handlers_.begin(); iter != handlers_.end(); ++iter) {
request.Out()->Printf("%s\n", iter->first.c_str());
}
@@ -50,7 +50,7 @@ class Request {
bool IsOK() const { return fd_ > 0; }

// If size is negative, no Content-Length: line is written.
void WriteHttpResponseHeader(int status, int64_t size = -1, const char *mimeType = nullptr, const char *otherHeaders = nullptr) const;
void WriteHttpResponseHeader(const char *ver, int status, int64_t size = -1, const char *mimeType = nullptr, const char *otherHeaders = nullptr) const;

private:
net::InputSink *in_;
@@ -90,12 +90,12 @@ WebSocketServer *WebSocketServer::CreateAsUpgrade(const http::Request &request,
};

if (!requireHeader("upgrade", "websocket") || !requireHeaderContains("connection", "upgrade")) {
request.WriteHttpResponseHeader(400, -1, "text/plain");
request.WriteHttpResponseHeader("1.1", 400, -1, "text/plain");
request.Out()->Push("Must send a websocket request.");
return nullptr;
}
if (!requireHeader("sec-websocket-version", "13")) {
request.WriteHttpResponseHeader(400, -1, "text/plain", "Sec-WebSocket-Version: 13\r\n");
request.WriteHttpResponseHeader("1.1", 400, -1, "text/plain", "Sec-WebSocket-Version: 13\r\n");
request.Out()->Push("Unsupported version.");
return nullptr;
}
@@ -110,7 +110,7 @@ WebSocketServer *WebSocketServer::CreateAsUpgrade(const http::Request &request,

std::string key;
if (!request.GetHeader("sec-websocket-key", &key)) {
request.WriteHttpResponseHeader(400, -1, "text/plain");
request.WriteHttpResponseHeader("1.1", 400, -1, "text/plain");
request.Out()->Push("Cannot accept without key.");
return nullptr;
}
@@ -123,7 +123,7 @@ WebSocketServer *WebSocketServer::CreateAsUpgrade(const http::Request &request,
std::string otherHeaders = StringFromFormat("Upgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: %s\r\n%s", acceptKey.c_str(), obtainedProtocolHeader.c_str());

// Okay, we're good to go then.
request.WriteHttpResponseHeader(101, -1, "websocket", otherHeaders.c_str());
request.WriteHttpResponseHeader("1.1", 101, -1, "websocket", otherHeaders.c_str());
request.WritePartial();

return new WebSocketServer(request.fd(), request.In(), request.Out());

0 comments on commit 65ac590

Please sign in to comment.
You can’t perform that action at this time.