Skip to content

Commit 981969b

Browse files
committed
http headers are case insensitive
1 parent 6313c9b commit 981969b

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

pc/net_socket.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <fcntl.h>
1010
#include <netdb.h>
1111
#include <poll.h>
12+
13+
#include <cctype>
1214
#include <iostream>
1315

1416
#define PC_EPOLL_FLAGS (EPOLLIN|EPOLLET|EPOLLRDHUP|EPOLLHUP|EPOLLERR)
@@ -985,7 +987,7 @@ ws_parser *http_server::get_ws_parser() const
985987
return wp_;
986988
}
987989

988-
bool http_server::get_header_val( const str& key, str& val) const
990+
bool http_server::get_header_val( const std::string& key, str& val ) const
989991
{
990992
for(unsigned i=0; i != hnms_.size(); ++i ) {
991993
if ( key == hnms_[i] ) {
@@ -1015,7 +1017,7 @@ bool http_server::parse( const char *ptr, size_t len, size_t& res )
10151017
if ( !next( LF, ++ptr, end ) ) return false;
10161018

10171019
// parse other header lines
1018-
bool has_len = false, has_upgrade = false;;
1020+
bool has_len = false, has_upgrade = false;
10191021
size_t clen = 0;
10201022
for(++ptr;;++ptr) {
10211023
if ( ptr <= &end[-2] && ptr[0] == CR && ptr[1] == LF ) {
@@ -1029,16 +1031,19 @@ bool http_server::parse( const char *ptr, size_t len, size_t& res )
10291031
if ( !find( CR, ptr, end ) ) return false;
10301032
assert( hdr_end >= hdr );
10311033
const size_t hlen = static_cast< size_t >( hdr_end - hdr );
1034+
std::string hdr_str{ hdr, hlen };
1035+
for ( char& c : hdr_str ) {
1036+
c = std::toupper( static_cast< unsigned char >( c ) );
1037+
}
10321038
assert( ptr >= val );
10331039
const size_t vlen = static_cast< size_t >( ptr - val );
1034-
if ( has_len || (
1035-
0 != __builtin_strncmp( "Content-Length", hdr, hlen ) &&
1036-
0 != __builtin_strncmp( "content-length", hdr, hlen ) ) ) {
1037-
hnms_.push_back( str( hdr, hlen ) );
1040+
1041+
if ( has_len || hdr_str != "CONTENT-LENGTH" ) {
1042+
hnms_.push_back( hdr_str );
10381043
hval_.push_back( str( val, vlen ) );
10391044
if ( wp_ && !has_upgrade ) {
10401045
has_upgrade =
1041-
0 == __builtin_strncmp( "Upgrade", hdr, hlen ) &&
1046+
hdr_str == "UPGRADE" &&
10421047
0 == __builtin_strncmp( "websocket", val, vlen );
10431048
}
10441049
} else {
@@ -1070,7 +1075,7 @@ bool http_server::parse( const char *ptr, size_t len, size_t& res )
10701075
void http_server::upgrade_ws()
10711076
{
10721077
str key;
1073-
if ( !get_header_val( "Sec-WebSocket-Key", key ) ) {
1078+
if ( ! get_header_val( "SEC-WEBSOCKET-KEY", key ) ) {
10741079
return;
10751080
}
10761081
const char *magic_id = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";

pc/net_socket.hpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -322,19 +322,17 @@ namespace pc
322322

323323
// access to http request components
324324
unsigned get_num_header() const;
325-
void get_header_key( unsigned i, str& ) const;
326-
void get_header_val( unsigned i, str&) const;
327-
bool get_header_val( const str&key, str&) const;
328325
void get_path( str& ) const;
329326

330327
private:
328+
bool get_header_val( const std::string& key, str& ) const;
331329

332330
typedef std::vector<str> str_vec_t;
333331

334332
void upgrade_ws();
335333

336334
str path_;
337-
str_vec_t hnms_;
335+
std::vector< std::string > hnms_;
338336
str_vec_t hval_;
339337
ws_parser *wp_;
340338
net_connect *np_;
@@ -495,19 +493,8 @@ namespace pc
495493
return hnms_.size();
496494
}
497495

498-
inline void http_server::get_header_key( unsigned i, str& val ) const
499-
{
500-
val = hnms_[i];
501-
}
502-
503-
inline void http_server::get_header_val( unsigned i, str& val ) const
504-
{
505-
val = hval_[i];
506-
}
507-
508496
inline void http_server::get_path( str& val ) const
509497
{
510498
val = path_;
511499
}
512-
513500
}

0 commit comments

Comments
 (0)