Skip to content

Commit

Permalink
Bug Fix: HTTP Headers Comparison Should Ignore Case
Browse files Browse the repository at this point in the history
In issue #98, it was brought to my attention that according to RFC7540
(HTTP/2), header comparisons must be case-insensitive. However, this was
not happening in `wsServer`, which caused failures in certain scenarios,
such as when used with HAProxy, which sends headers in lowercase.

This commit addresses the issue by implementing a case-insensitive
comparison when searching for the string 'Sec-WebSocket-Key'.
  • Loading branch information
Theldus committed Sep 13, 2024
1 parent ea8a029 commit 181ff74
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/handshake.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2020 Davidson Francis <[email protected]>
* Copyright (C) 2016-2024 Davidson Francis <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -71,6 +71,25 @@ int get_handshake_accept(char *wsKey, unsigned char **dest)
return (0);
}

/**
* @brief Finds the ocorrence of @p needle in @p haystack, case
* insensitive.
*
* @param haystack Target string to be searched.
* @param needle Substring to search for.
*
* @returns If found, returns a pointer at the beginning of the
* found substring. Otherwise, returns NULL.
*/
static char *strstricase(const char *haystack, const char *needle)
{
size_t length;
for (length = strlen(needle); *haystack; haystack++)
if (!strncasecmp(haystack, needle, length))
return (char*)haystack;
return (NULL);
}

/**
* @brief Gets the complete response to accomplish a succesfully
* handshake.
Expand All @@ -95,7 +114,7 @@ int get_handshake_response(char *hsrequest, char **hsresponse)
for (s = strtok_r(hsrequest, "\r\n", &saveptr); s != NULL;
s = strtok_r(NULL, "\r\n", &saveptr))
{
if (strstr(s, WS_HS_REQ) != NULL)
if (strstricase(s, WS_HS_REQ) != NULL)
break;
}

Expand Down

0 comments on commit 181ff74

Please sign in to comment.