forked from OpenIPC/ipctool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.c
308 lines (278 loc) · 9.18 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
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <poll.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "dns.h"
#include "http.h"
static int get_http_respcode(const char *inpbuf) {
char proto[32], descr[32];
int code;
if (sscanf(inpbuf, "%31s %d %31s", proto, &code, descr) < 2)
return -1;
return code;
}
static void get_http_date(const char *inpbuf, const char *end, char *buf) {
while (end - inpbuf >= 15) {
if (!strncmp(inpbuf, "Last-Modified: ", 15)) {
const char *ptr = inpbuf + 15;
for (int i = 0; i < DATE_BUF_LEN; i++) {
if (ptr[i] == '\n')
break;
buf[i] = ptr[i];
}
}
for (;;) {
if (inpbuf == end)
return;
inpbuf++;
if (*(inpbuf - 1) == '\n')
break;
}
}
}
static int get_http_payload_len(const char *inpbuf, const char *end) {
while (end - inpbuf >= 16) {
if (!strncmp(inpbuf, "Content-Length: ", 16))
return strtol(inpbuf + 16, NULL, 10);
for (;;) {
if (inpbuf == end)
return 0;
inpbuf++;
if (*(inpbuf - 1) == '\n')
break;
}
}
return 0;
}
int connect_with_timeout(int sockfd, const struct sockaddr *addr,
socklen_t addrlen, unsigned int timeout_ms) {
int rc = 0;
// Set O_NONBLOCK
int sockfd_flags_before;
if ((sockfd_flags_before = fcntl(sockfd, F_GETFL, 0) < 0))
return -1;
if (fcntl(sockfd, F_SETFL, sockfd_flags_before | O_NONBLOCK) < 0)
return -1;
// Start connecting (asynchronously)
do {
if (connect(sockfd, addr, addrlen) < 0) {
// Did connect return an error? If so, we'll fail.
if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS)) {
rc = -1;
}
// Otherwise, we'll wait for it to complete.
else {
// Set a deadline timestamp 'timeout' ms from now (needed b/c
// poll can be interrupted)
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
rc = -1;
break;
}
struct timespec deadline = {.tv_sec = now.tv_sec,
.tv_nsec = now.tv_nsec +
timeout_ms * 1000000l};
// Wait for the connection to complete.
do {
// Calculate how long until the deadline
if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
rc = -1;
break;
}
int ms_until_deadline =
(int)((deadline.tv_sec - now.tv_sec) * 1000l +
(deadline.tv_nsec - now.tv_nsec) / 1000000l);
if (ms_until_deadline < 0) {
rc = 0;
break;
}
// Wait for connect to complete (or for the timeout
// deadline)
struct pollfd pfds[] = {{.fd = sockfd, .events = POLLOUT}};
rc = poll(pfds, 1, ms_until_deadline);
// If poll 'succeeded', make sure it *really* succeeded
if (rc > 0) {
int error = 0;
socklen_t len = sizeof(error);
int retval = getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
&error, &len);
if (retval == 0)
errno = error;
if (error != 0)
rc = -1;
}
}
// If poll was interrupted, try again.
while (rc == -1 && errno == EINTR);
// Did poll timeout? If so, fail.
if (rc == 0) {
errno = ETIMEDOUT;
rc = -1;
}
}
}
} while (0);
// Restore original O_NONBLOCK state
if (fcntl(sockfd, F_SETFL, sockfd_flags_before) < 0)
return -1;
// Success
return rc;
}
#define CONNECT_TIMEOUT 3000 // milliseconds
static int common_connect(const char *hostname, const char *uri, nservers_t *ns,
int *s) {
int ret = ERR_GENERAL;
(void)uri;
a_records_t srv;
if (!resolv_name(ns, hostname, &srv)) {
return ERR_GETADDRINFO;
}
*s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
for (size_t i = 0; i < srv.len; i++) {
memcpy(&addr.sin_addr, &srv.ipv4_addr[i], sizeof(uint32_t));
#ifndef NDEBUG
char buf[256];
inet_ntop(AF_INET, &addr.sin_addr, buf, sizeof(buf));
fprintf(stdout, "Connecting to %s...\n", buf);
#endif
if (connect_with_timeout(*s, (struct sockaddr *)&addr, sizeof(addr),
CONNECT_TIMEOUT) != 1) {
ret = ERR_GENERAL;
break;
}
close(*s);
*s = socket(AF_INET, SOCK_STREAM, 0);
ret = ERR_CONNECT;
}
return ret;
}
#define MAKE_ERROR(err) (char *)(-err)
#define SNPRINTF(...) \
ptr += snprintf(ptr, sizeof(buf) - (ptr - buf), __VA_ARGS__)
char *download(char *hostname, const char *uri, const char *useragent,
nservers_t *ns, size_t *len, char *date, bool progress) {
int s, ret;
if ((ret = common_connect(hostname, uri, ns, &s) != ERR_GENERAL)) {
return MAKE_ERROR(ret);
}
char buf[4096] = "GET /";
char *ptr = buf + 5;
if (uri) {
SNPRINTF("%s", uri);
}
SNPRINTF(" HTTP/1.0\r\nHost: %s\r\n", hostname);
if (useragent)
SNPRINTF("User-Agent: %s\r\n", useragent);
SNPRINTF("\r\n");
int tosend = ptr - buf;
int nsent = send(s, buf, tosend, 0);
if (nsent != tosend)
return MAKE_ERROR(ERR_SEND);
int header = 1;
int nrecvd;
char *binbuf = NULL, *sptr;
int percent = 0;
while ((nrecvd = recv(s, buf, sizeof(buf), 0))) {
char *ptr = buf;
if (header) {
ptr = strstr(buf, "\r\n\r\n");
if (!ptr)
continue;
int rcode = get_http_respcode(buf);
if (rcode / 100 != 2) {
close(s);
return MAKE_ERROR(rcode / 100 * 10 + rcode % 10);
}
*len = get_http_payload_len(buf, ptr);
if (!*len) {
fprintf(stderr, "No length found, aborting...\n");
close(s);
return MAKE_ERROR(ERR_HTTP);
}
get_http_date(buf, ptr, date);
binbuf = malloc(*len);
if (!binbuf) {
close(s);
return MAKE_ERROR(ERR_MALLOC);
}
sptr = binbuf;
header = 0;
ptr += 4;
nrecvd -= ptr - buf;
}
if (!header) {
if (progress) {
int np = 100 * (sptr - binbuf) / *len;
if (np != percent) {
printf("Downloading %d%%\r", np);
fflush(stdout);
percent = np;
}
}
// TODO: avoid copying?
memcpy(sptr, ptr, nrecvd);
sptr += nrecvd;
}
}
return binbuf;
}
int upload(const char *hostname, const char *uri, nservers_t *ns,
span_t blocks[MAX_MTDBLOCKS + 1], size_t blocks_num) {
int s, ret;
if ((ret = common_connect(hostname, uri, ns, &s) != ERR_GENERAL)) {
return ret;
}
size_t len = 0;
for (size_t i = 0; i < blocks_num; i++) {
len += blocks[i].len;
// add len header
if (i)
len += sizeof(uint32_t);
}
char buf[4096] = "PUT /";
if (uri) {
strncat(buf, uri, sizeof(buf) - strlen(buf) - 1);
}
strncat(buf, " HTTP/1.0\r\nHost: ", sizeof(buf) - strlen(buf) - 1);
strncat(buf, hostname, sizeof(buf) - strlen(buf) - 1);
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,
"\r\n"
"Content-type: application/octet-stream\r\n"
"Connection: close\r\n"
"Content-Length: %zu\r\n"
"\r\n",
len);
int tosent = strlen(buf);
int nsent = send(s, buf, tosent, 0);
if (nsent != tosent)
return ERR_SEND;
for (size_t i = 0; i < blocks_num; i++) {
if (i) {
uint32_t len_header = blocks[i].len;
write(s, &len_header, sizeof(len_header));
}
int nbytes = write(s, blocks[i].data, blocks[i].len);
if (nbytes == -1)
break;
#if 0
printf("[%d] sent %d bytes\n", i, nbytes);
#endif
}
return 0;
}