-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
http.c
120 lines (97 loc) · 2.11 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
/* Released under AGPL v3 with exception for the OpenSSL library. See license.txt */
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef NO_SSL
#include <openssl/ssl.h>
#include "mssl.h"
#endif
#include "gen.h"
#include "http.h"
#include "io.h"
#include "utils.h"
int get_HTTP_headers(int socket_h, SSL *ssl_h, char **headers, int *overflow, double timeout)
{
char *term = NULL;
int len_in=0, len=4096;
char *buffer = (char *)malloc(len + 1);
int rc = RC_OK;
*headers = NULL;
memset(buffer, 0x00, len);
for(;;)
{
int rrc = -1;
int now_n = len - len_in;
#ifndef NO_SSL
if (ssl_h)
rrc = SSL_read(ssl_h, &buffer[len_in], now_n);
else
#endif
rrc = read_to(socket_h, &buffer[len_in], now_n, timeout);
if (rrc == 0 || rrc == RC_SHORTREAD) /* socket closed before request was read? */
{
rc = RC_SHORTREAD;
break;
}
else if (rrc < 0)
{
free(buffer);
return rrc;
}
len_in += rrc;
assert(len_in >= 0);
assert(len_in <= len);
buffer[len_in] = 0x00;
if (strstr(buffer, "\r\n\r\n") != NULL)
break;
if (len_in >= len)
{
len <<= 1;
buffer = (char *)realloc(buffer, len + 1);
}
}
*headers = buffer;
term = strstr(buffer, "\r\n\r\n");
if (term)
*overflow = len_in - (term - buffer + 4);
else
*overflow = 0;
return rc;
}
int dumb_get_HTTP_headers(int socket_h, char **headers, double timeout)
{
int len_in=0, len=4096;
char *buffer = (char *)malloc(len);
int rc = RC_OK;
*headers = NULL;
for(;;)
{
int rrc = read_to(socket_h, &buffer[len_in], 1, timeout);
if (rrc == 0 || rrc == RC_SHORTREAD) /* socket closed before request was read? */
{
rc = RC_SHORTREAD;
break;
}
else if (rrc == RC_TIMEOUT) /* timeout */
{
free(buffer);
return RC_TIMEOUT;
}
len_in += rrc;
buffer[len_in] = 0x00;
if (memcmp(&buffer[len_in - 4], "\r\n\r\n", 4) == 0)
break;
if (memcmp(&buffer[len_in - 2], "\n\n", 2) == 0) /* broken proxies */
break;
if (len_in == (len - 1))
{
len <<= 1;
buffer = (char *)realloc(buffer, len);
}
}
*headers = buffer;
return rc;
}