This repository has been archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathhttp.c
804 lines (645 loc) · 25.2 KB
/
http.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
#define _GNU_SOURCE 1
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <fnmatch.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "core.h"
#include "utils.h"
#include "socket.h"
#include "http.h"
#include "ssl.h"
#include "socks.h"
#include "main.h"
#include "minica.h"
/**
*
*/
void generic_http_error_page(sock_t sock, char* msg)
{
const char* html_header = "<html>"
"<head><title>"PROGNAME": ERROR!</title></head>"
"<body><h1>Error</h1>"
PROGNAME" encountered an error when loading your page<br><br>"
"The following message was returned:<br><div style=\"border: solid black 1px;font-family: monospace;\"><br>";
const char* html_footer = "</div></body></html>";
if (write(sock, html_header, strlen(html_header)) < 0) {
xlog(LOG_ERROR, "Failed to write error HTML header: %s\n", strerror(errno));
return;
}
if(write(sock, msg, strlen(msg)) < 0){
xlog(LOG_ERROR, "Failed to write error HTML page: %s\n", strerror(errno));
return;
}
if(write(sock, html_footer, strlen(html_footer)) < 0){
xlog(LOG_ERROR, "Failed to write error HTML footer: %s\n", strerror(errno));
return;
}
return;
}
/**
*
*/
static char* get_request_full_uri(request_t* req)
{
char* uri;
http_infos_t* http_infos = &req->http_infos;
size_t len;
if (!req || !http_infos)
return NULL;
len = sizeof(HTTPS_PROTO_STRING) + strlen(http_infos->hostname) + sizeof(":") + sizeof("65535");
len+= strlen(http_infos->path);
uri = (char*)proxenet_xmalloc(len+1);
proxenet_xsnprintf(uri, len, "%s%s:%d%s",
req->is_ssl?HTTPS_PROTO_STRING:HTTP_PROTO_STRING,
http_infos->hostname,
http_infos->port,
http_infos->path);
return uri;
}
/**
* This function defines the request type with the hostname and port based on the URI gathered
* from the buffer.
*
* This is for example used for
* CONNECT IP:PORT HTTP/1.0
* types of requests.
*
* If :PORT is not found, then req.http_infos.port is left untouched (must be defined priorly).
*
* @return 0 on success, -1 if error
*/
static int get_hostname_from_uri(request_t* req, int offset)
{
char *ptr, *buf;
buf = req->data + offset;
/* isolate the whole block 'IP:PORT' */
ptr = strchr(buf, ' ');
if (!ptr){
xlog(LOG_ERROR, "%s\n", "Invalid URI block");
return -1;
}
*ptr = '\0';
buf = proxenet_xstrdup2(buf);
/* host and port */
ptr = strchr(buf, ':');
if (ptr){
/* explicit port */
req->http_infos.port = (unsigned short)atoi(ptr+1);
*ptr = '\0';
}
req->http_infos.hostname = proxenet_xstrdup2(buf);
proxenet_xfree(buf);
return 0;
}
/**
* Look up for a header in a request. If found, this function will allocate a buffer containing
* the header value (after the ': ') until a line RET (CR/LF) is found.
* If not found, it returns NULL.
*
* @note if found, the allocated buffer *MUST* be free-ed by caller.
* @return a pointer to a malloc-ed buffer with the header value, or NULL if not found.
*/
static char* get_header_by_name(char* request, const char* header_name)
{
char *ptr, *ptr2, c, *header_value;
/* get header */
ptr = strstr(request, header_name);
if(!ptr){
return NULL;
}
/* move to start of hostname */
ptr += strlen(header_name);
ptr2 = ptr;
/* get the end of header line */
for(; *ptr2 && *ptr2!='\r' && *ptr2!='\n'; ptr2++);
c = *ptr2;
*ptr2 = '\0';
/* copy the value */
header_value = proxenet_xstrdup2(ptr);
*ptr2 = c;
proxenet_strip(header_value);
return header_value;
}
/**
*
*/
static int get_hostname_from_header(request_t *req)
{
char *ptr, *header;
header = get_header_by_name(req->data, "Host:");
if (!header){
return -1;
}
/* if port number, copy it */
ptr = strchr(header, ':');
if (ptr){
*ptr = '\0';
req->http_infos.hostname = proxenet_xstrdup2(header);
req->http_infos.port = (unsigned short)atoi(ptr+1);
} else {
req->http_infos.hostname = proxenet_xstrdup2(header);
}
proxenet_xfree(header);
return 0;
}
/**
* Identify the protocol to use from the request header
* @return a proto_t value if defined/found, -1 otherwise
*/
static int get_http_protocol(request_t *req)
{
char *ptr, *ptr2;
ptr2 = strstr(req->data, CRLF);
if(!ptr2)
goto invalid_http_protocol;
/* Move to beginning of URL */
for(ptr=req->data; ptr && *ptr!=' ' && *ptr!='\x00' && ptr < ptr2; ptr++);
if(!ptr || *ptr!=' ')
goto invalid_http_protocol;
++ptr;
if(*ptr=='/'){
/* this indicates that the request is well formed already */
/* this case would happen only when using transparent mode */
return TRANSPARENT;
}
if( strncmp(ptr, HTTP_PROTO_STRING, sizeof(HTTP_PROTO_STRING)-1)==0 ){
/* Check if the requested URL starts with 'http://' */
req->http_infos.proto_type = HTTP;
return HTTP;
} else if( strncmp(ptr, HTTPS_PROTO_STRING, sizeof(HTTPS_PROTO_STRING)-1)==0 ){
/* Check if the requested URL starts with 'https://' */
req->http_infos.proto_type = HTTPS;
return HTTPS;
} else if( strncmp(ptr, WS_PROTO_STRING, sizeof(WS_PROTO_STRING)-1)==0 ){
/* Check if the requested URL starts with 'WS://' */
req->http_infos.proto_type = WS;
return WS;
}
invalid_http_protocol:
xlog(LOG_ERROR, "%s\n", "Request is not a valid HTTP(S)/WS(S) request");
if (cfg->verbose > 1)
xlog(LOG_ERROR, "The invalid request is:\n%s\n", req);
return -1;
}
/**
* Modifies the HTTP request to strip out the protocol and hostname
* If successful, the request will be modified on output of this function as a
* valid HTTP request :
*
* METHOD /PATH HTTP/1.1[...]
*
* @return 0 if no error was encountered, -1 otherwise.
*/
int format_http_request(request_t* req)
{
size_t request_len = req->size;
size_t new_request_len = 0;
char **request = &req->data;
char *old_ptr, *new_ptr, *ptr;
unsigned int i;
int offlen;
switch(get_http_protocol(req)){
case HTTP:
/* check that !NULL is done by get_http_protocol */
ptr = strstr(*request, HTTP_PROTO_STRING);
/* -1 because of \x00 added by sizeof */
offlen = sizeof(HTTP_PROTO_STRING)-1;
break;
case HTTPS:
ptr = strstr(*request, HTTPS_PROTO_STRING);
offlen = sizeof(HTTPS_PROTO_STRING)-1;
break;
case WS:
ptr = strstr(*request, WS_PROTO_STRING);
offlen = sizeof(WS_PROTO_STRING)-1;
break;
case TRANSPARENT:
return 0;
default:
return -1;
}
old_ptr = ptr;
new_ptr = strchr(old_ptr + offlen, '/');
if (!new_ptr) {
xlog(LOG_ERROR, "%s\n", "Cannot find path (must not be implicit)");
return -1;
}
new_request_len = request_len - (new_ptr-old_ptr);
#ifdef DEBUG
xlog(LOG_DEBUG, "Formatting HTTP request (%dB->%dB)\n", request_len, new_request_len);
#endif
for (i=0; i<new_request_len - (old_ptr-*request);i++) {
*(old_ptr+i) = *(new_ptr+i);
}
req->data = proxenet_xrealloc(*request, new_request_len);
req->size = new_request_len;
return 0;
}
/**
* This function updates all the fields of the current request_t with the new values found in the
* request. Since those values will be useful many times, they are strdup-ed in a specific structure
* (http_infos_t). Those values *must* be free-ed later on.
*
* @return 0 if successful, -1 if any error occurs.
*/
int parse_http_request(request_t *req)
{
char *ptr, *buf, c;
int offset;
buf = req->data;
/* method */
ptr = strchr(buf, ' ');
if (!ptr){
xlog(LOG_ERROR, "%s\n", "Cannot find HTTP method in request");
if (cfg->verbose)
xlog(LOG_ERROR, "Buffer sent:\n%s\n", buf);
return -1;
}
c = *ptr;
*ptr = '\0';
req->http_infos.method = proxenet_xstrdup2(buf);
if (!req->http_infos.method){
xlog(LOG_ERROR, "%s\n", "strdup(method) failed, cannot pursue...");
return -1;
}
*ptr = c;
req->http_infos.proto_type = HTTP;
if (!strcmp(req->http_infos.method, "CONNECT")){
/*
* We can receive a CONNECT if
* - the connection uses HTTPS
* - the client tries to upgrade to WebSocket/Secure WebSocket
*/
char *upgrade_header = get_header_by_name(buf, "Upgrade:");
if (upgrade_header){
if (strcmp(upgrade_header, "WebSocket")==0){
xlog(LOG_INFO, "%s\n", "Upgrading to WebSocket");
req->http_infos.proto_type = WS;
req->is_ssl = false;
req->http_infos.proto = WS_STRING;
req->http_infos.port = HTTP_DEFAULT_PORT;
req->http_infos.proto_type = WS;
}
proxenet_xfree(upgrade_header);
} else {
req->is_ssl = true;
req->http_infos.proto = HTTPS_STRING;
req->http_infos.port = HTTPS_DEFAULT_PORT;
req->http_infos.proto_type = HTTPS;
}
offset = ptr - buf + 1;
if( get_hostname_from_uri(req, offset) < 0 ){
xlog(LOG_ERROR, "%s\n", "Failed to get hostname (URI)");
goto failed_hostname;
}
req->http_infos.path = proxenet_xstrdup2("/");
req->http_infos.version = proxenet_xstrdup2("HTTP/1.0");
req->http_infos.uri = get_request_full_uri(req);
if(!req->http_infos.uri){
xlog(LOG_ERROR, "%s\n", "get_request_full_uri() failed");
goto failed_uri;
}
return 0;
}
/* hostname and port */
if (req->is_ssl){
req->http_infos.port = HTTPS_DEFAULT_PORT;
req->http_infos.proto = HTTPS_STRING;
} else {
req->http_infos.port = HTTP_DEFAULT_PORT;
req->http_infos.proto = HTTP_STRING;
}
if( get_hostname_from_header(req) < 0 ){
xlog(LOG_ERROR, "%s\n", "Failed to get hostname (Host header)");
goto failed_hostname;
}
/* path */
buf = ptr+1;
if (!strncmp(buf, HTTP_PROTO_STRING, strlen(HTTP_PROTO_STRING))){
buf = strchr(buf + 8, '/');
}
ptr = strchr(buf, ' ');
if (!ptr){
xlog(LOG_ERROR, "%s\n", "Cannot find HTTP path in request");
goto failed_path;
}
c = *ptr;
*ptr = '\0';
req->http_infos.path = proxenet_xstrdup2(buf);
if (!req->http_infos.path){
xlog(LOG_ERROR, "%s\n", "strdup(path) failed, cannot pursue...");
goto failed_path;
}
*ptr = c;
buf = ptr+1;
/* version */
ptr = strchr(req->data, '\r');
if (!ptr){
xlog(LOG_ERROR, "%s\n", "Cannot find HTTP version");
goto failed_version;
}
c = *ptr;
*ptr = '\0';
req->http_infos.version = proxenet_xstrdup2(buf);
if (!req->http_infos.version){
xlog(LOG_ERROR, "%s\n", "strdup(version) failed, cannot pursue...");
goto failed_version;
}
*ptr = c;
/* refresh uri */
req->http_infos.uri = get_request_full_uri(req);
if(!req->http_infos.uri){
xlog(LOG_ERROR, "%s\n", "get_request_full_uri() failed");
goto failed_uri;
}
if (cfg->verbose) {
xlog(LOG_INFO, "New request %d to '%s'\n",
req->id, req->http_infos.uri);
if (cfg->verbose > 1) {
xlog(LOG_INFO,
"Request HTTP information:\n"
"method=%s\n"
"proto=%s\n"
"hostname=%s\n"
"port=%d\n"
"path=%s\n"
"version=%s\n"
,
req->http_infos.method,
req->http_infos.proto,
req->http_infos.hostname,
req->http_infos.port,
req->http_infos.path,
req->http_infos.version);
}
}
return 0;
failed_uri:
proxenet_xfree(req->http_infos.version);
failed_version:
proxenet_xfree(req->http_infos.path);
failed_path:
proxenet_xfree(req->http_infos.hostname);
failed_hostname:
proxenet_xfree(req->http_infos.method);
return -1;
}
/**
* Free all the heap allocated blocks on http_infos (allocated by parse_http_request()).
*/
void free_http_infos(http_infos_t *hi)
{
proxenet_xclean(hi->method, strlen(hi->method));
proxenet_xclean(hi->hostname, strlen(hi->hostname));
proxenet_xclean(hi->path, strlen(hi->path));
proxenet_xclean(hi->version, strlen(hi->version));
proxenet_xclean(hi->uri, strlen(hi->uri));
hi->proto = NULL;
hi->port = 0;
return;
}
/**
*
*/
int create_https_socket(request_t *req, sock_t *cli_sock, sock_t *srv_sock, ssl_context_t* ctx, bool use_proxy)
{
char *connect_buf = NULL;
http_infos_t* http_infos = &req->http_infos;
int retcode = -1;
bool use_http_proxy = use_proxy && cfg->is_socks_proxy==false;
/* disable all interception if ssl intercept was explicitely disabled by config */
if (cfg->ssl_intercept == false)
req->do_intercept = false;
/* if an HTTP proxy is used, expect CONNECT request */
if (use_http_proxy) {
/* 0. set up proxy->proxy ssl session (i.e. forward CONNECT request) */
retcode = proxenet_write(*cli_sock, req->data, req->size);
if (retcode < 0) {
xlog(LOG_ERROR, "%s failed to CONNECT to proxy\n", PROGNAME);
return -1;
}
/* read response */
retcode = proxenet_read_all(*cli_sock, &connect_buf, NULL);
if (retcode < 0) {
xlog(LOG_ERROR, "%s failed to read from proxy (retcode=%#x)\n", PROGNAME);
if (retcode==-ENODATA && cfg->verbose){
xlog(LOG_ERROR, "%s\n", "Data expected but none received");
}
return -1;
}
/* expect HTTP 200 */
if ( (strncmp(connect_buf, "HTTP/1.0 200", 12) != 0)
&& (strncmp(connect_buf, "HTTP/1.1 200", 12) != 0)) {
xlog(LOG_ERROR, "%s->proxy: bad HTTP version\n", PROGNAME);
proxenet_xfree(connect_buf);
if (cfg->verbose)
xlog(LOG_ERROR, "Received %s\n", connect_buf);
return -1;
}
proxenet_xfree(connect_buf);
}
if (req->do_intercept){
/* 1. set up proxy->server ssl session with hostname */
if(proxenet_ssl_init_client_context(&(ctx->client), http_infos->hostname) < 0) {
return -1;
}
proxenet_ssl_wrap_socket(&(ctx->client.context), cli_sock);
retcode = proxenet_ssl_handshake(&(ctx->client.context));
if (retcode < 0) {
xlog(LOG_ERROR, "handshake %s->server failed [code: %#x]\n", PROGNAME, retcode);
xlog(LOG_ERROR, "Client SSL handshake failed for '%s:%d'.\n",
http_infos->hostname, http_infos->port, retcode);
return -1;
}
#ifdef DEBUG
xlog(LOG_DEBUG, "SSL handshake with %s done, cli_sock=%d\n",
use_http_proxy?"proxy":"server", *cli_sock);
#endif
}
if (proxenet_write(*srv_sock, "HTTP/1.0 200 Connection established\r\n\r\n", 39) < 0){
return -1;
}
if (req->do_intercept) {
/* 2. set up proxy->browser ssl session with hostname */
if(proxenet_ssl_init_server_context(&(ctx->server), http_infos->hostname) < 0) {
return -1;
}
proxenet_ssl_wrap_socket(&(ctx->server.context), srv_sock);
retcode = proxenet_ssl_handshake(&(ctx->server.context));
if (retcode < 0) {
xlog(LOG_ERROR, "handshake %s->client failed [code: %#x]\n", PROGNAME, retcode);
xlog(LOG_ERROR, "Server SSL handshake failed for '%s:%d'.\n",
http_infos->hostname, http_infos->port, retcode);
return -1;
}
#ifdef DEBUG
xlog(LOG_DEBUG, "SSL handshake with client done, srv_sock=%d\n", *srv_sock);
#endif
}
return 0;
}
/**
* This function aimes to establish whether a request should be intercepted or not.
* A request should be intercepted if
* - Interception is set to INTERCEPT_ONLY and the hostname regex matches the expression
* from configuration
* - Interception is set to INTERCEPT_EXCEPT and the hostname regex does not match the
* expression from configuration.
*
* @return true if the request must be intercepted, false otherwise
*/
static bool apply_intercept_rules(http_infos_t* hi)
{
if (cfg->intercept_mode==INTERCEPT_ONLY &&
fnmatch(cfg->intercept_pattern, hi->hostname, FNM_CASEFOLD)==0)
/* INTERCEPT_ONLY + match */
return true;
if (cfg->intercept_mode==INTERCEPT_EXCEPT &&
fnmatch(cfg->intercept_pattern, hi->hostname, FNM_CASEFOLD)==FNM_NOMATCH)
/* INTERCEPT_EXCEPT + nomatch */
return true;
/* All other cases */
return false;
}
/**
* Establish a connection from proxenet -> server.
* If proxy forwarding configured, then this function performs the negociation with the other proxy.
* If the host applies for SSL intercept rules, this function also handles the SSL handshake.
*
* @return 0 if successful, -1 otherwise
*/
int create_http_socket(request_t* req, sock_t* server_sock, sock_t* client_sock, ssl_context_t* ssl_ctx)
{
int retcode;
char *host, *port;
char sport[7] = {0, };
http_infos_t* http_infos = &req->http_infos;
bool use_proxy = (cfg->proxy.host != NULL);
bool use_socks_proxy = use_proxy && (cfg->is_socks_proxy==true);
bool use_http_proxy = use_proxy && (cfg->is_socks_proxy==false);
char errmsg[512]={0,};
if (parse_http_request(req) < 0){
xlog(LOG_ERROR, "%s\n", "Failed to extract valid parameters from URL.");
return -1;
}
ssl_ctx->use_ssl = req->is_ssl;
proxenet_xsnprintf(sport, sizeof(sport), "%hu", http_infos->port);
/* do we forward to another proxy ? */
if (use_proxy) {
host = cfg->proxy.host;
port = cfg->proxy.port;
} else {
host = http_infos->hostname;
port = sport;
}
#ifdef DEBUG
xlog(LOG_DEBUG, "Relay request %s to '%s:%s (type=%d)'\n",
use_http_proxy ? "via HTTP proxy" : "direct",
host, port, http_infos->proto_type);
#endif
retcode = proxenet_open_socket(host, port);
if (retcode < 0) {
proxenet_xsnprintf(errmsg, sizeof(errmsg), "Cannot connect to %s:%s<br><br>Reason: %s",
host, port, errno?strerror(errno):"<i>proxenet_open_socket()</i> failed");
generic_http_error_page(*server_sock, errmsg);
return -1;
}
if (cfg->verbose > 2)
xlog(LOG_INFO, "Socket to %s '%s:%s': fd=%d\n",
use_http_proxy?"HTTP proxy":(use_socks_proxy?"SOCKS4 proxy":"server"),
host, port, retcode);
*client_sock = retcode;
req->do_intercept = apply_intercept_rules(http_infos);
if (cfg->verbose > 1) {
xlog(LOG_INFO, "Server '%s' %s match filter '%s' with pattern '%s'\n",
http_infos->hostname,
req->do_intercept ? "do" : "do not",
cfg->intercept_mode==INTERCEPT_ONLY?"INTERCEPT_ONLY":"INTERCEPT_EXCEPT",
cfg->intercept_pattern);
}
if(use_socks_proxy){
char*rhost = http_infos->hostname;
int rport = http_infos->port;
retcode = proxenet_socks_connect(*client_sock, rhost, rport, true);
if( retcode<0 ){
proxenet_xsnprintf(errmsg, sizeof(errmsg), "Failed to open SOCKS4 tunnel to %s:%s.\n",
host, port);
generic_http_error_page(*server_sock, errmsg);
xlog(LOG_ERROR, "%s", errmsg);
return -1;
}
}
/* set up specific sockets */
switch (http_infos->proto_type){
case HTTPS:
if(cfg->verbose > 2)
xlog(LOG_INFO, "Creating a new HTTPS socket: %d/%d proxy=%s\n", client_sock, server_sock, use_socks_proxy?"true":"false");
return create_https_socket(req, client_sock, server_sock, ssl_ctx, use_socks_proxy);
default:
break;
}
return 0;
}
/**
* Add old IE support (Compatibility View) for POST requests by forcing a 2nd read on the
* server socket, to make IE send the POST body.
* Tests revealed that this mode does not change the behaviour for recent browser. Therefore,
* it can be used all the time. Option -i allows to disable it.
*
* @return 0 if successful, -1 otherwise
*/
int ie_compat_read_post_body(sock_t sock, request_t* req, proxenet_ssl_context_t* sslctx)
{
int nb;
size_t old_len, body_len;
char *body, *clen;
/* to speed-up, disregard requests without body */
if (strcmp(req->http_infos.method, "POST")!=0 && \
strcmp(req->http_infos.method, "PUT")!=0)
return 0;
/* read Content-Length header */
clen = get_header_by_name(req->data, "Content-Length: ");
if (!clen){
xlog(LOG_ERROR, "%s\n", "Extending IE POST: No Content-Length");
return -1;
}
/* if Content-Length is zero */
body_len = (size_t)atoi(clen);
proxenet_xfree(clen);
if (body_len==0){
return 0;
}
/* if everything has already been sent (i.e. end of already received buffer is not CRLF*2) */
if (req->data[ req->size-4 ] != '\r')
return 0;
if (req->data[ req->size-3 ] != '\n')
return 0;
if (req->data[ req->size-2 ] != '\r')
return 0;
if (req->data[ req->size-1 ] != '\n')
return 0;
/* read data (if any) */
nb = proxenet_read_all(sock, &body, sslctx);
if (nb<0){
xlog(LOG_ERROR, "%s\n", "Extending IE POST: failed to read");
return -1;
}
/* and extend the request buffer */
if (nb>0){
old_len = req->size;
req->size = old_len + (size_t)nb;
req->data = proxenet_xrealloc(req->data, req->size);
memcpy(req->data + old_len, body, (size_t)nb);
}
proxenet_xfree(body);
#ifdef DEBUG
xlog(LOG_DEBUG, "Extending request for IE: new_size=%d (content-length=%d)\n",
req->size, body_len);
#endif
return 0;
}