Skip to content

Commit

Permalink
fix: http proxy for upgrade protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Oct 11, 2023
1 parent 3e75b65 commit 9f4c75e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
4 changes: 4 additions & 0 deletions http/HttpMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ bool HttpMessage::IsKeepAlive() {
return keepalive;
}

bool HttpMessage::IsUpgrade() {
auto iter = headers.find("upgrade");
return iter != headers.end();
}

// headers
void HttpMessage::SetHeader(const char* key, const std::string& value) {
Expand Down
1 change: 1 addition & 0 deletions http/HttpMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ class HV_EXPORT HttpMessage {

bool IsChunked();
bool IsKeepAlive();
bool IsUpgrade();

// headers
void SetHeader(const char* key, const std::string& value);
Expand Down
16 changes: 10 additions & 6 deletions http/server/HttpHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,12 @@ void HttpHandler::onMessageComplete() {
addResponseHeaders();

// upgrade ? handleUpgrade : HandleHttpRequest
upgrade = 0;
auto iter_upgrade = req->headers.find("upgrade");
if (iter_upgrade != req->headers.end()) {
upgrade = 1;
handleUpgrade(iter_upgrade->second.c_str());
status_code = resp->status_code;
if (upgrade) {
auto iter_upgrade = req->headers.find("upgrade");
if (iter_upgrade != req->headers.end()) {
handleUpgrade(iter_upgrade->second.c_str());
status_code = resp->status_code;
}
} else {
status_code = HandleHttpRequest();
if (status_code != HTTP_STATUS_NEXT) {
Expand Down Expand Up @@ -342,6 +342,9 @@ void HttpHandler::handleRequestHeaders() {
// keepalive
keepalive = pReq->IsKeepAlive();

// upgrade
upgrade = pReq->IsUpgrade();

// proxy
proxy = forward_proxy = reverse_proxy = 0;
if (hv::startswith(pReq->url, "http")) {
Expand Down Expand Up @@ -1107,6 +1110,7 @@ void HttpHandler::onProxyConnect(hio_t* upstream_io) {
}

// NOTE: start recv request continue then upstream
if (handler->upgrade) hio_setcb_read(io, hio_write_upstream);
hio_read_start(io);
// NOTE: start recv response then upstream
hio_setcb_read(upstream_io, hio_write_upstream);
Expand Down

0 comments on commit 9f4c75e

Please sign in to comment.