Skip to content

Commit 7562c07

Browse files
committed
use CONNECT method if request contains upgrade header
this should allow using websockets or other upgraded connections on a standard (non-encrypted) proxified HTTP connection. testcase: $ telnet localhost 8888 GET / HTTP/1.1 Host: echo.websocket.org:80 Upgrade: websocket Connection: Upgrade Origin: http://echo.websocket.org Sec-WebSocket-Key: 5KNqfsPZ9m/BbeRlVpf7MQ== Sec-WebSocket-Version: 13
1 parent 04f68e2 commit 7562c07

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/reqs.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
* connections. The request line is allocated from the heap, but it must
8888
* be freed in another function.
8989
*/
90-
static int read_request_line (struct conn_s *connptr)
90+
static int read_request_line (struct conn_s *connptr, char** lines, size_t* lines_len)
9191
{
9292
ssize_t len;
9393

@@ -101,6 +101,12 @@ static int read_request_line (struct conn_s *connptr)
101101
return -1;
102102
}
103103

104+
*lines = saferealloc(*lines, *lines_len + len + 1);
105+
if(*lines) {
106+
strcpy(*lines + *lines_len, connptr->request_line);
107+
*lines_len += len;
108+
}
109+
104110
/*
105111
* Strip the new line and carriage return from the string.
106112
*/
@@ -626,7 +632,7 @@ add_header_to_connection (hashmap_t hashofheaders, char *header, size_t len)
626632
/*
627633
* Read all the headers from the stream
628634
*/
629-
static int get_all_headers (int fd, hashmap_t hashofheaders)
635+
static int get_all_headers (int fd, hashmap_t hashofheaders, char** lines, size_t* lines_len)
630636
{
631637
char *line = NULL;
632638
char *header = NULL;
@@ -646,6 +652,14 @@ static int get_all_headers (int fd, hashmap_t hashofheaders)
646652
return -1;
647653
}
648654

655+
if(lines) {
656+
*lines = saferealloc(*lines, *lines_len + linelen + 1);
657+
if(*lines) {
658+
strcpy(*lines + *lines_len, line);
659+
*lines_len += linelen;
660+
}
661+
}
662+
649663
/*
650664
* If we received a CR LF or a non-continuation line, then add
651665
* the accumulated header field, if any, to the hashmap, and
@@ -1011,7 +1025,7 @@ static int process_server_headers (struct conn_s *connptr)
10111025
/*
10121026
* Get all the headers from the remote server in a big hash
10131027
*/
1014-
if (get_all_headers (connptr->server_fd, hashofheaders) < 0) {
1028+
if (get_all_headers (connptr->server_fd, hashofheaders, NULL, NULL) < 0) {
10151029
log_message (LOG_WARNING,
10161030
"Could not retrieve all the headers from the remote server.");
10171031
hashmap_delete (hashofheaders);
@@ -1543,6 +1557,8 @@ void handle_connection (int fd)
15431557
char sock_ipaddr[IP_LENGTH];
15441558
char peer_ipaddr[IP_LENGTH];
15451559
char peer_string[HOSTNAME_LENGTH];
1560+
char *lines = NULL;
1561+
size_t lines_len = 0;
15461562

15471563
getpeer_information (fd, peer_ipaddr, peer_string);
15481564

@@ -1571,7 +1587,7 @@ void handle_connection (int fd)
15711587
goto fail;
15721588
}
15731589

1574-
if (read_request_line (connptr) < 0) {
1590+
if (read_request_line (connptr, &lines, &lines_len) < 0) {
15751591
update_stats (STAT_BADCONN);
15761592
indicate_http_error (connptr, 408, "Timeout",
15771593
"detail",
@@ -1597,7 +1613,7 @@ void handle_connection (int fd)
15971613
/*
15981614
* Get all the headers from the client in a big hash.
15991615
*/
1600-
if (get_all_headers (connptr->client_fd, hashofheaders) < 0) {
1616+
if (get_all_headers (connptr->client_fd, hashofheaders, &lines, &lines_len) < 0) {
16011617
log_message (LOG_WARNING,
16021618
"Could not retrieve all the headers from the client");
16031619
indicate_http_error (connptr, 400, "Bad Request",
@@ -1683,6 +1699,11 @@ void handle_connection (int fd)
16831699
"file descriptor %d.", request->host,
16841700
connptr->server_fd);
16851701

1702+
if(hashmap_search(hashofheaders, "upgrade") > 0) {
1703+
connptr->connect_method = TRUE;
1704+
safe_write (connptr->server_fd, lines, lines_len);
1705+
}
1706+
16861707
if (!connptr->connect_method)
16871708
establish_http_connection (connptr, request);
16881709
}
@@ -1740,6 +1761,7 @@ void handle_connection (int fd)
17401761
}
17411762

17421763
done:
1764+
safefree(lines);
17431765
free_request_struct (request);
17441766
hashmap_delete (hashofheaders);
17451767
destroy_conn (connptr);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy