-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.c
196 lines (172 loc) · 5.53 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
#include "http.h"
#include "netq.h"
char* fetch(const char* url) {
printf("%s\n--------------------\n", url); fflush(stdout);
// open a socket to url
int status, s;
struct addrinfo params;
struct addrinfo *results, *p;
memset(¶ms, 0, sizeof params);
params.ai_family = AF_UNSPEC;
params.ai_socktype = SOCK_STREAM;
params.ai_flags = AI_CANONNAME;
printf("looking up... "); fflush(stdout);
if ((status = getaddrinfo(url, "http", ¶ms, &results)) != 0) {
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); fflush(stdout);
exit(1);
}
printf("complete.\n"); fflush(stdout);
//*
char ip4[INET_ADDRSTRLEN];
for (p = results; p != NULL; p = p->ai_next) {
printf("canonname: '%s'\n", p->ai_canonname); fflush(stdout);
printf("socktype: '%s'\n", (p->ai_socktype == SOCK_STREAM) ? "SOCK_STREAM" : "SOCK_DGRAM"); fflush(stdout);
printf("ai_family: '%s'\n", (p->ai_family == AF_INET) ? "AF_INET" : "AF_INET6"); fflush(stdout);
inet_ntop(AF_INET, &(((struct sockaddr_in*)(p->ai_addr))->sin_addr.s_addr), ip4, INET_ADDRSTRLEN);
printf("ip: '%s'\n\n", ip4); fflush(stdout);
}
// */
//*
for (p = results; p != NULL; p = p->ai_next) {
if ((s = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) {
perror("socket");
if (p->ai_next == NULL) {
exit(1);
}
}
break;
}
// */ s = 1; s = s;
if (connect(s, p->ai_addr, p->ai_addrlen) < 0) {
perror("connect");
exit(1);
}
printf("sending... "); fflush(stdout);
http_send_req(s, url);
printf("sent.\n"); fflush(stdout);
printf("got:\n===========================\n"); fflush(stdout);
char* buf = malloc(HTTP_BUFSIZE);
struct nq* q = nq_init(s, 8192);
int sz;
while ((sz = nq_pop_until(q, buf, HTTP_BUFSIZE-1, '\n')) > 0) {
buf[sz] = 0;
print_escape(buf);
}
free(buf);
nq_free(q);
//char* rcv;
//http_decode(s, &rcv);
//printf("received? nahhhh...\n"); fflush(stdout);
//printf("\n%s\n\n", rcv); fflush(stdout);
printf("\n");
//free(rcv);
freeaddrinfo(results);
close(s);
return NULL;
}
/*
===================== Sample Data: ===============
HTTP/1.1 200 OK\r\n
Server: nginx/1.10.3 (Ubuntu)\r\n
Date: Fri, 06 Apr 2018 08:56:17 GMT\r\n
Content-Type: text/html\r\n
Transfer-Encoding: chunked\r\n
Connection: close\r\n
Vary: Accept-Encoding\r\n
Expires: Fri, 06 Apr 2018 08:56:27 GMT\r\n
Cache-Control: max-age=10\r\n
X-Proxy-Cache: EXPIRED\r\n
X-Proxy-Cache: EXPIRED\r\n
Access-Control-Allow-Origin: *\r\n
\r\n
ebd\r\n
==================================================
// */
void http_strip_response_header(int s) {
char* buf = malloc(HTTP_BUFSIZE);
struct nq* q = nq_init(s, 8192);
int sz;
while ((sz = nq_pop_until(q, buf, HTTP_BUFSIZE, '\n')) > 2);
free(buf);
nq_free(q);
}
/*
int sz;
char *data;
struct http_chunk *next;
// */
void http_decode(int s, char** dst) {
http_strip_response_header(s);
char* buf = malloc(HTTP_BUFSIZE);
struct nq* q = nq_init(s, 8192);
int sz;
struct http_chunk *p, *head = malloc(sizeof(struct http_chunk));
memset(head, 0, sizeof(struct http_chunk));
for (p = head; p != NULL; p = p->next) {
// read next chunk size
sz = nq_pop_until(q, buf, HTTP_BUFSIZE, '\n');
buf[sz] = 0;
if (sscanf(buf, "%x", &(p->sz)) < 1) {
fprintf(stderr, "ERROR: did not receive chunk size. ("); fflush(stdout);
print_escape(buf);
fprintf(stderr, ")\n"); fflush(stdout);
exit(1);
}
printf("size of next chunk: %d\n", p->sz); fflush(stdout);
if (p->sz == 0) continue;
// setup current chunk data
p->data = malloc(p->sz);
// and next chunk
p->next = malloc(sizeof(struct http_chunk));
memset(p->next, 0, sizeof(struct http_chunk));
// read data
sz = nq_pop(q, p->data, p->sz);
if (sz < p->sz) {
fprintf(stderr, "ERROR: did not receive advertised chunk size.\n"); fflush(stdout);
exit(1);
}
}
*dst = malloc(1024);
}
void http_send_req(int s, const char* host) {
const char* http_get = "GET / HTTP/1.1\r\n";
const char* http_host = "Host: ";
const char* http_connection = "Connection: close\r\n";
send(s, http_get, strlen(http_get), 0);
send(s, http_host, strlen(http_host), 0);
send(s, host, strlen(host), 0);
send(s, "\r\n", strlen("\r\n"), 0);
send(s, http_connection, strlen(http_connection), 0);
send(s, "\r\n", strlen("\r\n"), 0);
}
void print_escape(const char* s) {
int sz = strlen(s);
int i;
for (i = 0; i < sz; ++i) {
switch(s[i]) {
case '\a':
printf("\\a"); fflush(stdout);
break;
case '\b':
printf("\\b"); fflush(stdout);
break;
case '\f':
printf("\\f"); fflush(stdout);
break;
case '\n':
printf("\\n"); fflush(stdout);
break;
case '\r':
printf("\\r"); fflush(stdout);
break;
case '\t':
printf("\\t"); fflush(stdout);
break;
case '\v':
printf("\\v"); fflush(stdout);
break;
default:
printf("%c", s[i]); fflush(stdout);
}
}
}