This repository has been archived by the owner on Jul 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.c
executable file
·1460 lines (1150 loc) · 31 KB
/
proxy.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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* proxy.c - HTTP Proxy
*
* This program depends on ISO 9899:1999. If using gcc, compile with -std=c99
*
* TEAM MEMBERS:
* Gabríel Arthúr Pétursson <[email protected]>
* Gunnar Guðvarðarson <[email protected]>
* Kristján Árni Gerhardsson <[email protected]>
*
* When the proxy receives a new connection, it accepts it and spawns a new
* thread to process it.
*
* The thread then waits until a whole HTTP header has been received. It is
* then processed and forwarded to the server (unless it's a CONNECT request,
* they are treated differently).
*
* The client and server sockets are monitored for data. Once data arrives,
* it is stored in one of the appropriate buffers and later processed.
*
* The buffers are ring (circular) buffers. Data can be moved between them.
* Data can also be inserted directly into them, such as when sending custom
* HTTP header entities.
*
* This proxy fully supports IPv6.
*/
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define BUFFER_SIZE (16384)
struct string
{
size_t len;
const char *str;
};
struct mutable_string
{
size_t len;
char *str;
};
/* a ring buffer */
struct proxy_buffer
{
size_t size;
size_t pos;
char buf[BUFFER_SIZE];
};
struct proxy_connection
{
struct proxy_log *log;
/* network addresses */
struct sockaddr_storage client_addr;
struct sockaddr_storage server_addr;
socklen_t client_len;
socklen_t server_len;
/* file descriptors */
int client_fd;
int server_fd;
/* proxy state */
uint64_t client_rx;
bool client_connected;
bool server_connected;
bool server_expect_header; /* is a header expected? */
/* client buffers */
struct proxy_buffer crecv_buffer;
struct proxy_buffer csend_buffer;
/* server buffers */
struct proxy_buffer srecv_buffer;
struct proxy_buffer ssend_buffer;
};
struct proxy_log
{
pthread_mutex_t mutex;
FILE *file;
};
struct proxy
{
struct proxy_log log;
pthread_attr_t attr;
int socket;
};
enum http_method
{
OPTIONS,
GET,
HEAD,
POST,
PUT,
DELETE,
TRACE,
CONNECT,
PATCH,
};
static const char *str_http_method[] = {
"OPTIONS",
"GET",
"HEAD",
"POST",
"PUT",
"DELETE",
"TRACE",
"CONNECT",
"PATCH",
};
/* string functions */
static int proxy_string_compare (const struct string pstr, const char *cstr);
/* log functions */
static void proxy_log_get_time (char *strtime, size_t length);
static void proxy_log_get_address (const struct sockaddr *sa, socklen_t salen, char *addr, size_t addrlen);
static void proxy_log_init (struct proxy_log *log);
static void proxy_log_free (struct proxy_log *log);
static void proxy_log_add (struct proxy_log *log, const struct sockaddr *sa, socklen_t salen, struct string url, uint64_t size);
/* url functions */
static bool proxy_url_parse (const struct string url, struct string *restrict host, struct string *restrict path, struct string *restrict port);
static bool proxy_url_parse_plain (const struct string url, struct string *restrict host, struct string *restrict port);
/* buffer functions */
static bool proxy_buffer_add (struct proxy_buffer *buffer, const struct string data);
static bool proxy_buffer_add_cstr (struct proxy_buffer *buffer, const char *str);
static size_t proxy_buffer_available (const struct proxy_buffer *buffer);
static bool proxy_buffer_commit (struct proxy_buffer *buffer, size_t bytes);
static size_t proxy_buffer_move (struct proxy_buffer *dest, struct proxy_buffer *src, size_t bytes);
static struct string proxy_buffer_read_consecutive (const struct proxy_buffer *buffer);
static bool proxy_buffer_remove (struct proxy_buffer *buffer, size_t bytes);
static void proxy_buffer_shift (struct proxy_buffer *buffer);
static size_t proxy_buffer_size (struct proxy_buffer *buffer);
static struct mutable_string proxy_buffer_write_consecutive (struct proxy_buffer *buffer);
/* header functions */
static size_t proxy_header_complete (struct proxy_buffer *buffer);
static bool proxy_header_line (struct string *header, struct string *line);
static bool proxy_header_parse_head (const struct string line, enum http_method *method, struct string *location, struct string *version);
static bool proxy_header_parse_line (const struct string line, struct string *key, struct string *value);
/* client functions */
static int proxy_client_connect (const struct string node, const struct string service, struct sockaddr_storage *addr, socklen_t *addrlen);
static bool proxy_client_process (struct proxy_connection *conn);
static void *proxy_client_work (void *ud);
/* proxy functions */
static int proxy_init (struct proxy *proxy, const char *listen_address, const char *port);
static void proxy_free (struct proxy *proxy);
static void proxy_accept (struct proxy *proxy);
/* main function */
int main (int argc, char **argv);
/* Compares a Proxy string with a C string in a case-insensitive manner */
static int
proxy_string_compare (const struct string pstr, const char *cstr)
{
size_t clen;
int cmp;
assert (pstr.str);
assert (cstr);
clen = strlen (cstr);
cmp = strncasecmp (pstr.str, cstr, MIN (pstr.len, clen));
if (cmp != 0)
return cmp;
if (pstr.len < clen)
return -1;
if (pstr.len > clen)
return 1;
return 0;
}
/*
* Gets and formats the current time
*
* If the current time cannot be represented, or 'length' is of insufficient
* size, it is filled with the string "(NULL)".
*/
static void
proxy_log_get_time (char *strtime, size_t length)
{
time_t t;
struct tm tm;
assert (strtime);
assert (length > 0);
t = time (NULL);
if (gmtime_r (&t, &tm) == NULL)
{
strncpy (strtime, "(NULL)", length);
strtime[length - 1] = 0;
}
else
{
if (strftime (strtime, length, "%FT%TZ", &tm) == 0)
{
strncpy (strtime, "(NULL)", length);
strtime[length - 1] = 0;
}
}
}
/*
* Formats the IP address 'sa' into human-friendly form
*
* If the address cannot be formatted, or 'addrlen' is of insufficient size,
* it is filled with the string "(NULL)".
*/
static void
proxy_log_get_address (const struct sockaddr *sa, socklen_t salen, char *addr, size_t addrlen)
{
int status;
assert (sa);
assert (salen > 0);
assert (addr);
assert (addrlen > 0);
status = getnameinfo (sa, salen, addr, addrlen, NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV);
if (status != 0)
{
fprintf (stderr, "getnameinfo: %s\n", gai_strerror(status));
strncpy (addr, "(NULL)", addrlen);
addr[addrlen - 1] = 0;
}
}
/* Initializes the log struct */
static void
proxy_log_init (struct proxy_log *log)
{
assert (log);
pthread_mutex_init (&log->mutex, NULL);
log->file = fopen ("proxy.log", "ab");
if (log->file == NULL)
{
perror ("fopen");
log->file = stdout;
}
}
/* Destroy the log struct */
static void
proxy_log_free (struct proxy_log *log)
{
assert (log);
pthread_mutex_destroy (&log->mutex);
if (fclose (log->file) != 0)
{
perror ("fclose");
}
}
/*
* Adds a request to the log
*
* The inputs are the socket address of the requesting client
* (sockaddr), the URI from the request (uri), and the size of the response
* from the server in bytes (size).
*/
static void
proxy_log_add (struct proxy_log *log, const struct sockaddr *sa, socklen_t salen, struct string url, uint64_t size)
{
char strtime[32];
char address[INET6_ADDRSTRLEN];
assert (log);
assert (sa);
assert (salen > 0);
assert (url.str);
proxy_log_get_time (strtime, sizeof (strtime));
proxy_log_get_address (sa, salen, address, sizeof (address));
pthread_mutex_lock (&log->mutex);
fprintf (log->file, "%s: [%s] %.*s %" PRIu64 "\n", strtime, address, (int) url.len, url.str, size);
fflush (log->file);
pthread_mutex_unlock (&log->mutex);
}
/*
* A simplified URL parser. Gives host, path and port.
*/
static bool
proxy_url_parse (
const struct string url,
struct string *restrict host,
struct string *restrict path,
struct string *restrict port)
{
const char *slash;
const char *colon;
assert (url.str);
assert (host);
assert (path);
assert (port);
/* prefix + leading slash */
if (url.len < 7 + 1)
return false;
if (memcmp (url.str, "http://", 7) != 0)
return false;
/* find the leading slash */
slash = memchr (&url.str[7], '/', url.len - 7);
if (slash == NULL)
return false;
/* find the last colon in the host */
colon = memrchr (&url.str[7], ':', slash - &url.str[7]);
/* no colon means we do not need to do further processing */
if (colon == NULL)
{
host->len = slash - &url.str[7];
host->str = &url.str[7];
port->len = 2;
port->str = "80";
}
else
{
/* do we have an IPv6 address? */
if (url.str[7] == '[')
{
/* does the colon immediately follow the IPv6 address? */
if (colon[-1] == ']')
{
host->len = colon - &url.str[7] - 2;
host->str = &url.str[8];
port->len = slash - colon - 1;
port->str = colon + 1;
}
else
{
/* matching closing bracket */
if (slash[-1] != ']')
return false;
host->len = slash - &url.str[7] - 2;
host->str = &url.str[8];
port->len = 2;
port->str = "80";
}
}
else
{
host->len = colon - &url.str[7];
host->str = &url.str[7];
port->len = slash - colon - 1;
port->str = colon + 1;
}
}
path->len = url.len - (slash - url.str);
path->str = slash;
return true;
}
/* A more simplistic URL parser. Parses simple host:port strings. */
static bool
proxy_url_parse_plain (
const struct string url,
struct string *host,
struct string *port)
{
const char *colon;
assert (url.str);
assert (host);
assert (port);
/* find the colon */
colon = memchr (url.str, ':', url.len);
if (colon == NULL)
return false;
host->len = colon - url.str;
host->str = url.str;
port->len = &url.str[url.len] - colon - 1;
port->str = colon + 1;
if (host->len == 0 || port->len == 0)
return false;
/* do we have an IPv6 address? */
if (host->str[0] == '[')
{
/* matching closing bracket */
if (host->str[host->len - 1] != ']')
return false;
host->len -= 2;
host->str += 1;
}
return true;
}
/* Append data to the buffer */
static bool
proxy_buffer_add (struct proxy_buffer *buffer, const struct string data)
{
size_t length;
assert (buffer);
assert (data.str);
/* do we have sufficient storage space? */
if (data.len + buffer->size > sizeof (buffer->buf))
return false;
length = MIN (data.len, sizeof (buffer->buf) - buffer->pos);
memcpy (&buffer->buf[buffer->pos], data.str, length);
memcpy (buffer->buf, &data.str[length], data.len - length);
buffer->pos += data.len;
buffer->pos %= sizeof (buffer->buf);
buffer->size += data.len;
return true;
}
/* Append C string to the buffer */
static bool
proxy_buffer_add_cstr (struct proxy_buffer *buffer, const char *str)
{
size_t length;
assert (str);
length = strlen (str);
return proxy_buffer_add (buffer, (struct string) { length, str });
}
/* Number of bytes available for addition to the buffer */
static size_t
proxy_buffer_available (const struct proxy_buffer *buffer)
{
assert (buffer);
return sizeof (buffer->buf) - buffer->size;
}
/*
* Adds 'bytes' bytes to the size of buffer, assuming the data is already there
*
* Generally used in combination with 'proxy_buffer_write_consecutive'.
*/
static bool
proxy_buffer_commit (struct proxy_buffer *buffer, size_t bytes)
{
assert (buffer);
if (buffer->size + bytes > sizeof (buffer->buf))
return false;
buffer->size += bytes;
buffer->pos += bytes;
buffer->pos %= sizeof (buffer->buf);
return true;
}
/*
* Moves 'bytes' bytes from source to destination buffer.
*
* If less than 'bytes' bytes are available in the source, or less than 'bytes'
* bytes fit in the destination, the number of remaining bytes is returned
* after the copy.
*/
static size_t
proxy_buffer_move (struct proxy_buffer *dest, struct proxy_buffer *src, size_t bytes)
{
size_t length;
struct string src_data;
struct mutable_string dest_data;
assert (dest);
assert (src);
while (true)
{
src_data = proxy_buffer_read_consecutive (src);
dest_data = proxy_buffer_write_consecutive (dest);
length = MIN (bytes, MIN (dest_data.len, src_data.len));
if (length == 0)
break;
memcpy (dest_data.str, src_data.str, length);
proxy_buffer_remove (src, length);
proxy_buffer_commit (dest, length);
bytes -= length;
}
return bytes;
}
/*
* Returns the entire buffer, but stops early if it's not consecutive
*
* The contents of the returned string are invalidated as soon as any other
* buffer operations are performed on the buffer.
*
* Generally used in combination with 'proxy_buffer_remove'.
*/
static struct string
proxy_buffer_read_consecutive (const struct proxy_buffer *buffer)
{
assert (buffer);
size_t begin = (buffer->pos + sizeof (buffer->buf) - buffer->size) % sizeof (buffer->buf);
return (struct string) {
MIN (buffer->size, sizeof (buffer->buf) - begin),
&buffer->buf[begin]
};
}
/* Remove bytes from the beginning of the buffer */
static bool
proxy_buffer_remove (struct proxy_buffer *buffer, size_t bytes)
{
assert (buffer);
if (bytes > buffer->size)
return false;
buffer->size -= bytes;
return true;
}
/* Shift the buffer so its first byte begins physically first in memory */
static void
proxy_buffer_shift (struct proxy_buffer *buffer)
{
size_t begin;
size_t length;
char aux[sizeof (buffer->buf)];
assert (buffer);
/* already shifted */
if (buffer->pos % sizeof (buffer->buf) == buffer->size)
return;
begin = (buffer->pos + sizeof (buffer->buf) - buffer->size) % sizeof (buffer->buf);
length = MIN (buffer->size, sizeof (buffer->buf) - begin);
memcpy (aux, &buffer->buf[begin], length);
memcpy (&aux[length], buffer->buf, buffer->size - length);
memcpy (buffer->buf, aux, buffer->size);
buffer->pos = buffer->size;
buffer->pos %= sizeof (buffer->buf);
}
/* Number of bytes in the buffer */
static size_t
proxy_buffer_size (struct proxy_buffer *buffer)
{
assert (buffer);
return buffer->size;
}
/*
* Returns a memory region located past the end of the current position in the
* buffer, suitable for appending data.
*
* When finished writing, call 'proxy_buffer_commit' with the number of bytes
* written.
*/
static struct mutable_string
proxy_buffer_write_consecutive (struct proxy_buffer *buffer)
{
assert (buffer);
return (struct mutable_string) {
MIN (sizeof (buffer->buf) - buffer->size , sizeof (buffer->buf) - buffer->pos),
&buffer->buf[buffer->pos],
};
}
/* Is the HTTP header complete, and if so, what is its size? */
static size_t
proxy_header_complete (struct proxy_buffer *buffer)
{
struct string data;
assert (buffer);
proxy_buffer_shift (buffer);
data = proxy_buffer_read_consecutive (buffer);
char *end = memmem (data.str, data.len, "\r\n\r\n", 4);
/* the header is incomplete */
if (end == NULL)
return 0;
return end + 4 - data.str;
}
/* Extracts a single line from the header and advances the header pointer */
static bool
proxy_header_line (struct string *header, struct string *line)
{
assert (header);
assert (header->str);
assert (line);
const char *end = memmem (header->str, header->len, "\r\n", 2);
if (end == NULL)
return false;
line->len = end - header->str + 2;
line->str = header->str;
header->str += line->len;
header->len -= line->len;
return true;
}
/* Parses the initial HTTP head entry */
static bool
proxy_header_parse_head (
const struct string line,
enum http_method *method,
struct string *location,
struct string *version)
{
const char *space;
assert (line.str);
assert (method);
assert (location);
assert (version);
for (size_t i = 0; i < sizeof (str_http_method) / sizeof (*str_http_method); ++i)
{
size_t len = strlen (str_http_method[i]);
/* terminating \r\n and a space */
if (line.len <= len + 3)
continue;
/* have we found the correct method? */
if (memcmp (str_http_method[i], line.str, len) == 0)
{
/* the following byte should be a space */
if (line.str[len] != ' ')
return false;
/* find the next space */
space = memchr (&line.str[len + 1], ' ', line.len - len - 1);
if (space == NULL)
return false;
*method = i;
location->len = space - &line.str[len + 1];
location->str = &line.str[len + 1];
version->len = &line.str[line.len - 2] - (space + 1);
version->str = space + 1;
return true;
}
}
return false;
}
/*
* Parses a header line to get the key and value of that line
*/
static bool
proxy_header_parse_line (
const struct string line,
struct string *key,
struct string *value)
{
char *colon;
assert (line.str);
assert (key);
assert (value);
/* key starts at start of line.str but ends at ':' */
colon = memchr (line.str, ':', line.len);
if (colon == NULL)
return false; /* no ':', header line is invalid */
key->len = colon - line.str;
key->str = line.str;
if (colon[1] != ' ')
return false;
/* value starts 1 characters after the ':' and ends before the '\r' */
value->len = &line.str[line.len] - colon - 4;
value->str = colon + 2;
return true;
}
/*
* Resolve and connect to the given node and service
*/
static int
proxy_client_connect (
const struct string node,
const struct string service,
struct sockaddr_storage *addr,
socklen_t *addrlen)
{
struct addrinfo hints;
struct addrinfo *res = NULL;
struct addrinfo *aip = NULL;
int sock = -1;
int gai;
char str_node[node.len + 1];
char str_service[service.len + 1];
assert (node.str);
assert (service.str);
assert (addr);
assert (addrlen);
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
memcpy (str_node, node.str, node.len);
memcpy (str_service, service.str, service.len);
str_node[node.len] = 0;
str_service[service.len] = 0;
gai = getaddrinfo (str_node, str_service, &hints, &res);
if (gai != 0)
{
fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (gai));
fprintf (stderr, "name: %zu '%s'\n", node.len, str_node);
fprintf (stderr, "service: %zu '%s'\n", service.len, str_service);
return -1;
}
/* try all addresses until one succeeds */
for (aip = res; aip; aip = aip->ai_next)
{
sock = socket (aip->ai_family, aip->ai_socktype, aip->ai_protocol);
if (sock == -1)
{
/* family and protocol not supported */
if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
continue;
perror ("socket");
break;
}
else
{
if (connect (sock, aip->ai_addr, aip->ai_addrlen) == 0)
{
memcpy (addr, aip->ai_addr, aip->ai_addrlen);
*addrlen = aip->ai_addrlen;
break;
}
perror ("connect");
if (close (sock) == -1)
{
perror ("close");
}
sock = -1;
break;
}
}
freeaddrinfo (res);
return sock;
}
/*
* Examines the buffers and performs the appropriate computation on them,
* such as passing data along.
*/
static bool
proxy_client_process (struct proxy_connection *conn)
{
size_t header_size;
assert (conn);
/* client to server */
proxy_buffer_move (&conn->ssend_buffer, &conn->crecv_buffer, BUFFER_SIZE);
if (conn->server_expect_header)
{
header_size = proxy_header_complete (&conn->srecv_buffer);
/* header is incomplete */
if (header_size == 0)
return proxy_buffer_available (&conn->srecv_buffer) > 0;
/* pass the headers on, mostly unmodified */
if (proxy_buffer_move (&conn->csend_buffer, &conn->srecv_buffer, header_size - 2) != 0)
return false;
/* we do not support keep-alives */
if ( ! proxy_buffer_add_cstr (&conn->csend_buffer, "Proxy-Connection: close\r\n"))
return false;
conn->server_expect_header = false;
}
else
{
/* server to client */
proxy_buffer_move (&conn->csend_buffer, &conn->srecv_buffer, BUFFER_SIZE);
}
return true;
}
/* Working thread */
static void *
proxy_client_work (void *ud)
{
/* connection */
struct proxy_connection *conn = ud;
/* header parsing */
size_t header_size;
struct string header;
struct string line;
/* header information */
enum http_method method;
struct string location;
struct string version;
struct string host;
struct string path;
struct string port;
char *mem = NULL;
struct string key;
struct string value;
/* multiplexing */
fd_set rset;
fd_set wset;
assert (ud);
/* receive until we have the header */
while (proxy_header_complete (&conn->crecv_buffer) == 0 &&
proxy_buffer_available (&conn->crecv_buffer) > 0)
{
struct mutable_string data = proxy_buffer_write_consecutive (&conn->crecv_buffer);
ssize_t bytes_read = recv (conn->client_fd, data.str, data.len, 0);
if (bytes_read == -1)
{
perror ("recv");
goto error;
}
/* client disconnected */
if (bytes_read == 0)
goto error;
proxy_buffer_commit (&conn->crecv_buffer, bytes_read);
}
header_size = proxy_header_complete (&conn->crecv_buffer);
/* the header was not found */
if (header_size == 0)
goto error;
header = proxy_buffer_read_consecutive (&conn->crecv_buffer);
header.len = header_size;
if ( ! proxy_header_line (&header, &line))
goto error;
if ( ! proxy_header_parse_head (line, &method, &location, &version))
goto error;
/* we only support HTTP/1.1 clients */
if (proxy_string_compare (version, "HTTP/1.1") != 0)
goto error;
/* CONNECT requests are essentially plain TCP tunnels */
if (method == CONNECT)
{
if ( ! proxy_url_parse_plain (location, &host, &port))
goto error;
conn->server_expect_header = false;
}
else
{
if ( ! proxy_url_parse (location, &host, &path, &port))
goto error;
/* send the request */
proxy_buffer_add_cstr (&conn->ssend_buffer, str_http_method[method]);
proxy_buffer_add_cstr (&conn->ssend_buffer, " ");
proxy_buffer_add (&conn->ssend_buffer, path);
proxy_buffer_add_cstr (&conn->ssend_buffer, " ");
proxy_buffer_add (&conn->ssend_buffer, version);
proxy_buffer_add_cstr (&conn->ssend_buffer, "\r\n");
/* we do not want a keep-alive session with the server */
proxy_buffer_add_cstr (&conn->ssend_buffer, "Connection: close\r\n");
while (true)
{
if ( ! proxy_header_line (&header, &line))
goto error;