Skip to content

Commit f443b9a

Browse files
Merge pull request #470 from dylan-mccormick/sse-method-fix
Add HTTP method constraint for categorizing SSE requests
2 parents 4af7d00 + 7e06250 commit f443b9a

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/WebRequest.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,13 @@ bool AsyncWebServerRequest::_parseReqHeader() {
659659
}
660660
} else if (name.equalsIgnoreCase(T_UPGRADE) && value.equalsIgnoreCase(T_WS)) {
661661
// WebSocket request can be uniquely identified by header: [Upgrade: websocket]
662-
_reqconntype = RCT_WS;
662+
// Per RFC 6455 §4.1 the handshake is a GET. Only classify when the
663+
// connection is still a plain HTTP connection so a previously detected
664+
// SSE request (or any other classified type) cannot be clobbered by
665+
// header ordering.
666+
if (_method == AsyncWebRequestMethod::HTTP_GET && (_reqconntype == RCT_DEFAULT || _reqconntype == RCT_HTTP)) {
667+
_reqconntype = RCT_WS;
668+
}
663669
} else if (name.equalsIgnoreCase(T_ACCEPT)) {
664670
String lowcase(value);
665671
lowcase.toLowerCase();
@@ -668,7 +674,11 @@ bool AsyncWebServerRequest::_parseReqHeader() {
668674
#else
669675
const char *substr = std::strstr(lowcase.c_str(), String(T_text_event_stream).c_str());
670676
#endif
671-
if (substr != NULL) {
677+
// Server-Sent Events (HTML §9.2) are GET-only connections negotiated via
678+
// Accept: text/event-stream. Only classify when the connection is still
679+
// a plain HTTP connection so a previously detected WebSocket upgrade
680+
// cannot be clobbered by header ordering.
681+
if (substr != NULL && _method == AsyncWebRequestMethod::HTTP_GET && (_reqconntype == RCT_DEFAULT || _reqconntype == RCT_HTTP)) {
672682
// WebEvent request can be uniquely identified by header: [Accept: text/event-stream]
673683
_reqconntype = RCT_EVENT;
674684
}

0 commit comments

Comments
 (0)