|
@@ -0,0 +1,823 @@
|
|
1
|
+/**
|
|
2
|
+ * @PROJECT WebGet Downloader
|
|
3
|
+ * @COPYRIGHT See COPYING in the top level directory
|
|
4
|
+ * @FILE libweb.c
|
|
5
|
+ * @PURPOSE Common Web Library
|
|
6
|
+ * @DEVELOPERS Al Globus <globus@nas.nasa.gov>
|
|
7
|
+ * Rafal Kupiec <belliash@asiotec.eu.org>
|
|
8
|
+ * Jef Poskanzer <jef@mail.acme.com>
|
|
9
|
+ */
|
|
10
|
+
|
|
11
|
+#include <unistd.h>
|
|
12
|
+#include <stdlib.h>
|
|
13
|
+#include <stdio.h>
|
|
14
|
+#include <string.h>
|
|
15
|
+#include <ctype.h>
|
|
16
|
+#include <signal.h>
|
|
17
|
+#include <fcntl.h>
|
|
18
|
+#include <sys/types.h>
|
|
19
|
+#include <sys/stat.h>
|
|
20
|
+#include <sys/socket.h>
|
|
21
|
+#include <netinet/in.h>
|
|
22
|
+#include <netdb.h>
|
|
23
|
+
|
|
24
|
+#include <cyassl/openssl/ssl.h>
|
|
25
|
+#include <cyassl/openssl/err.h>
|
|
26
|
+#include <cyassl/error.h>
|
|
27
|
+
|
|
28
|
+#ifdef CROSS_BUILD
|
|
29
|
+ #include <cyassl/cyassl_error.h>
|
|
30
|
+#endif
|
|
31
|
+
|
|
32
|
+#include "libweb.h"
|
|
33
|
+
|
|
34
|
+int b64_encode(unsigned char* ptr, int len, char* space, int size) {
|
|
35
|
+ int ptr_idx, space_idx, phase;
|
|
36
|
+ char c;
|
|
37
|
+
|
|
38
|
+ space_idx = 0;
|
|
39
|
+ phase = 0;
|
|
40
|
+ for(ptr_idx = 0; ptr_idx < len; ++ptr_idx) {
|
|
41
|
+ switch(phase) {
|
|
42
|
+ case 0:
|
|
43
|
+ c = b64_encode_table[ptr[ptr_idx] >> 2];
|
|
44
|
+ if(space_idx < size) {
|
|
45
|
+ space[space_idx++] = c;
|
|
46
|
+ }
|
|
47
|
+ c = b64_encode_table[(ptr[ptr_idx] & 0x3) << 4];
|
|
48
|
+ if(space_idx < size) {
|
|
49
|
+ space[space_idx++] = c;
|
|
50
|
+ }
|
|
51
|
+ ++phase;
|
|
52
|
+ break;
|
|
53
|
+ case 1:
|
|
54
|
+ space[space_idx - 1] = b64_encode_table[b64_decode_table[(int) ((unsigned char) space[space_idx - 1])] | (ptr[ptr_idx] >> 4)];
|
|
55
|
+ c = b64_encode_table[(ptr[ptr_idx] & 0xf) << 2];
|
|
56
|
+ if(space_idx < size) {
|
|
57
|
+ space[space_idx++] = c;
|
|
58
|
+ }
|
|
59
|
+ ++phase;
|
|
60
|
+ break;
|
|
61
|
+ case 2:
|
|
62
|
+ space[space_idx - 1] = b64_encode_table[b64_decode_table[(int) ((unsigned char) space[space_idx - 1])] | (ptr[ptr_idx] >> 6)];
|
|
63
|
+ c = b64_encode_table[ptr[ptr_idx] & 0x3f];
|
|
64
|
+ if(space_idx < size) {
|
|
65
|
+ space[space_idx++] = c;
|
|
66
|
+ }
|
|
67
|
+ phase = 0;
|
|
68
|
+ break;
|
|
69
|
+ }
|
|
70
|
+ }
|
|
71
|
+ while(phase++ < 3) {
|
|
72
|
+ if(space_idx < size) {
|
|
73
|
+ space[space_idx++] = '=';
|
|
74
|
+ }
|
|
75
|
+ }
|
|
76
|
+ return space_idx;
|
|
77
|
+}
|
|
78
|
+
|
|
79
|
+void check(void* ptr) {
|
|
80
|
+ if(ptr == (void*) 0) {
|
|
81
|
+ (void) fprintf(stderr, "%s: out of memory\n", argv0);
|
|
82
|
+ exit(1);
|
|
83
|
+ }
|
|
84
|
+}
|
|
85
|
+
|
|
86
|
+off_t file_bytes(const char* filename) {
|
|
87
|
+ struct stat sb;
|
|
88
|
+
|
|
89
|
+ if(stat(filename, &sb) < 0) {
|
|
90
|
+ perror(filename);
|
|
91
|
+ exit(1);
|
|
92
|
+ }
|
|
93
|
+ return sb.st_size;
|
|
94
|
+}
|
|
95
|
+
|
|
96
|
+int file_copy(const char* filename, char* buf) {
|
|
97
|
+ int fd;
|
|
98
|
+ struct stat sb;
|
|
99
|
+ off_t bytes;
|
|
100
|
+
|
|
101
|
+ fd = open(filename, O_RDONLY);
|
|
102
|
+ if(fd == -1) {
|
|
103
|
+ perror(filename);
|
|
104
|
+ exit(-1);
|
|
105
|
+ }
|
|
106
|
+ if(fstat(fd, &sb) != 0) {
|
|
107
|
+ perror(filename);
|
|
108
|
+ exit(-1);
|
|
109
|
+ }
|
|
110
|
+ bytes = sb.st_size;
|
|
111
|
+ if(read(fd, buf, bytes) != bytes) {
|
|
112
|
+ perror(filename);
|
|
113
|
+ exit(-1);
|
|
114
|
+ }
|
|
115
|
+ (void) close(fd);
|
|
116
|
+ return bytes;
|
|
117
|
+}
|
|
118
|
+
|
|
119
|
+int getURL(char* url, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value) {
|
|
120
|
+ char* s;
|
|
121
|
+ int protocol;
|
|
122
|
+ char host[2000];
|
|
123
|
+ int host_len;
|
|
124
|
+ unsigned short port;
|
|
125
|
+ char* file = (char*) 0;
|
|
126
|
+ char* http = "http://";
|
|
127
|
+ int http_len = strlen( http );
|
|
128
|
+ char* https = "https://";
|
|
129
|
+ int https_len = strlen( https );
|
|
130
|
+ int proto_len;
|
|
131
|
+
|
|
132
|
+ if(url == (char*) 0) {
|
|
133
|
+ (void) fprintf(stderr, "%s: null URL\n", argv0);
|
|
134
|
+ exit(1);
|
|
135
|
+ }
|
|
136
|
+ if(strncmp(http, url, http_len) == 0) {
|
|
137
|
+ proto_len = http_len;
|
|
138
|
+ protocol = PROTO_HTTP;
|
|
139
|
+ } else if(strncmp(https, url, https_len) == 0) {
|
|
140
|
+ proto_len = https_len;
|
|
141
|
+ protocol = PROTO_HTTPS;
|
|
142
|
+ } else {
|
|
143
|
+ (void) fprintf(stderr, "%s: non-http URL\n", argv0);
|
|
144
|
+ exit(1);
|
|
145
|
+ }
|
|
146
|
+
|
|
147
|
+ for(s = url + proto_len; *s != '\0' && *s != ':' && *s != '/'; ++s);
|
|
148
|
+ host_len = s - url;
|
|
149
|
+ host_len -= proto_len;
|
|
150
|
+ strncpy(host, url + proto_len, host_len);
|
|
151
|
+ host[host_len] = '\0';
|
|
152
|
+ if(*s == ':') {
|
|
153
|
+ port = (unsigned short) atoi(++s);
|
|
154
|
+ while(*s != '\0' && *s != '/')
|
|
155
|
+ ++s;
|
|
156
|
+ } else {
|
|
157
|
+ if(protocol == PROTO_HTTPS) {
|
|
158
|
+ port = 443;
|
|
159
|
+ } else {
|
|
160
|
+ port = 80;
|
|
161
|
+ }
|
|
162
|
+ }
|
|
163
|
+ if(*s == '\0') {
|
|
164
|
+ file = "/";
|
|
165
|
+ } else {
|
|
166
|
+ file = s;
|
|
167
|
+ }
|
|
168
|
+ return getURLbyParts(protocol, host, port, file, referer, user_agent, auth_token, ncookies, cookies, header_name, header_value);
|
|
169
|
+}
|
|
170
|
+
|
|
171
|
+int getURLbyParts(int protocol, char* host, unsigned short port, char* file, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value) {
|
|
172
|
+ int sockfd;
|
|
173
|
+ SSL_CTX* ssl_ctx = (SSL_CTX*) 0;
|
|
174
|
+ SSL* ssl = (SSL*) 0;
|
|
175
|
+ char buf[20000];
|
|
176
|
+ int i, bytes, b, header_state, status;
|
|
177
|
+
|
|
178
|
+ (void) alarm(timeout);
|
|
179
|
+ sockfd = open_client_socket(host, port);
|
|
180
|
+ if(protocol == PROTO_HTTPS) {
|
|
181
|
+ int r;
|
|
182
|
+ ssl_ctx = SSL_CTX_new(TLSv1_client_method());
|
|
183
|
+ SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, 0);
|
|
184
|
+ ssl = SSL_new(ssl_ctx);
|
|
185
|
+ SSL_set_fd(ssl, sockfd);
|
|
186
|
+ r = SSL_connect(ssl);
|
|
187
|
+ if(r <= 0) {
|
|
188
|
+ (void) fprintf(stderr, "%s: %s - SSL connection failed - %d\n", argv0, url, r);
|
|
189
|
+ exit(1);
|
|
190
|
+ }
|
|
191
|
+ }
|
|
192
|
+ (void) alarm(timeout);
|
|
193
|
+ bytes = snprintf(buf, sizeof(buf), "GET %s HTTP/1.0\r\n", file);
|
|
194
|
+ bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Host: %s\r\n", host);
|
|
195
|
+ if(referer != (char*) 0) {
|
|
196
|
+ bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Referer: %s\r\n", referer);
|
|
197
|
+ }
|
|
198
|
+ bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "User-Agent: %s\r\n", user_agent);
|
|
199
|
+ bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Accept: */*\r\n");
|
|
200
|
+ bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Accept-Language: en\r\n");
|
|
201
|
+ bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Accept-Charset: iso-8859-1,*,utf-8\r\n");
|
|
202
|
+ if(auth_token != (char*) 0) {
|
|
203
|
+ char token_buf[1000];
|
|
204
|
+ token_buf[b64_encode((unsigned char*) auth_token, strlen( auth_token ), token_buf, sizeof(token_buf))] = '\0';
|
|
205
|
+ bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Authorization: Basic %s\r\n", token_buf);
|
|
206
|
+ }
|
|
207
|
+ for(i = 0; i < ncookies; ++i) {
|
|
208
|
+ bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Cookie: %s\r\n", cookies[i]);
|
|
209
|
+ }
|
|
210
|
+ if(header_name != (char*) 0) {
|
|
211
|
+ bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "%s: %s\r\n", header_name, header_value);
|
|
212
|
+ }
|
|
213
|
+ bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "\r\n");
|
|
214
|
+ if (protocol == PROTO_HTTPS)
|
|
215
|
+ (void) SSL_write(ssl, buf, bytes);
|
|
216
|
+ else
|
|
217
|
+ (void) write(sockfd, buf, bytes);
|
|
218
|
+ (void) alarm(timeout);
|
|
219
|
+ header_state = HDST_LINE1_PROTOCOL;
|
|
220
|
+ status = 0;
|
|
221
|
+ for(;;) {
|
|
222
|
+ if(protocol == PROTO_HTTPS) {
|
|
223
|
+ bytes = SSL_read(ssl, buf, sizeof(buf));
|
|
224
|
+ } else {
|
|
225
|
+ bytes = read(sockfd, buf, sizeof(buf));
|
|
226
|
+ }
|
|
227
|
+ if(bytes <= 0) {
|
|
228
|
+ break;
|
|
229
|
+ }
|
|
230
|
+ for(b = 0; b < bytes; ++b) {
|
|
231
|
+ if(verbose) {
|
|
232
|
+ (void) write(1, &buf[b], 1);
|
|
233
|
+ }
|
|
234
|
+ switch(header_state) {
|
|
235
|
+ case HDST_LINE1_PROTOCOL:
|
|
236
|
+ switch(buf[b]) {
|
|
237
|
+ case ' ': case '\t':
|
|
238
|
+ header_state = HDST_LINE1_WHITESPACE;
|
|
239
|
+ break;
|
|
240
|
+ case '\n':
|
|
241
|
+ header_state = HDST_LF;
|
|
242
|
+ break;
|
|
243
|
+ case '\r':
|
|
244
|
+ header_state = HDST_CR;
|
|
245
|
+ break;
|
|
246
|
+ }
|
|
247
|
+ break;
|
|
248
|
+ case HDST_LINE1_WHITESPACE:
|
|
249
|
+ switch(buf[b]) {
|
|
250
|
+ case '0':
|
|
251
|
+ case '1':
|
|
252
|
+ case '2':
|
|
253
|
+ case '3':
|
|
254
|
+ case '4':
|
|
255
|
+ case '5':
|
|
256
|
+ case '6':
|
|
257
|
+ case '7':
|
|
258
|
+ case '8':
|
|
259
|
+ case '9':
|
|
260
|
+ status = buf[b] - '0';
|
|
261
|
+ header_state = HDST_LINE1_STATUS;
|
|
262
|
+ break;
|
|
263
|
+ case '\n':
|
|
264
|
+ header_state = HDST_LF;
|
|
265
|
+ break;
|
|
266
|
+ case '\r':
|
|
267
|
+ header_state = HDST_CR;
|
|
268
|
+ break;
|
|
269
|
+ default:
|
|
270
|
+ header_state = HDST_TEXT;
|
|
271
|
+ break;
|
|
272
|
+ }
|
|
273
|
+ break;
|
|
274
|
+ case HDST_LINE1_STATUS:
|
|
275
|
+ switch(buf[b]) {
|
|
276
|
+ case '0':
|
|
277
|
+ case '1':
|
|
278
|
+ case '2':
|
|
279
|
+ case '3':
|
|
280
|
+ case '4':
|
|
281
|
+ case '5':
|
|
282
|
+ case '6':
|
|
283
|
+ case '7':
|
|
284
|
+ case '8':
|
|
285
|
+ case '9':
|
|
286
|
+ status = status * 10 + buf[b] - '0';
|
|
287
|
+ break;
|
|
288
|
+ case '\n':
|
|
289
|
+ header_state = HDST_LF;
|
|
290
|
+ break;
|
|
291
|
+ case '\r':
|
|
292
|
+ header_state = HDST_CR;
|
|
293
|
+ break;
|
|
294
|
+ default:
|
|
295
|
+ header_state = HDST_TEXT;
|
|
296
|
+ break;
|
|
297
|
+ }
|
|
298
|
+ break;
|
|
299
|
+ case HDST_BOL:
|
|
300
|
+ switch(buf[b]) {
|
|
301
|
+ case '\n':
|
|
302
|
+ header_state = HDST_LF;
|
|
303
|
+ break;
|
|
304
|
+ case '\r':
|
|
305
|
+ header_state = HDST_CR;
|
|
306
|
+ break;
|
|
307
|
+ default:
|
|
308
|
+ header_state = HDST_TEXT;
|
|
309
|
+ break;
|
|
310
|
+ }
|
|
311
|
+ break;
|
|
312
|
+ case HDST_TEXT:
|
|
313
|
+ switch(buf[b]) {
|
|
314
|
+ case '\n':
|
|
315
|
+ header_state = HDST_LF;
|
|
316
|
+ break;
|
|
317
|
+ case '\r':
|
|
318
|
+ header_state = HDST_CR;
|
|
319
|
+ break;
|
|
320
|
+ }
|
|
321
|
+ break;
|
|
322
|
+ case HDST_LF:
|
|
323
|
+ switch(buf[b]) {
|
|
324
|
+ case '\n':
|
|
325
|
+ goto end_of_headers;
|
|
326
|
+ case '\r':
|
|
327
|
+ header_state = HDST_CR;
|
|
328
|
+ break;
|
|
329
|
+ default:
|
|
330
|
+ header_state = HDST_TEXT;
|
|
331
|
+ break;
|
|
332
|
+ }
|
|
333
|
+ break;
|
|
334
|
+ case HDST_CR:
|
|
335
|
+ switch(buf[b]) {
|
|
336
|
+ case '\n':
|
|
337
|
+ header_state = HDST_CRLF;
|
|
338
|
+ break;
|
|
339
|
+ case '\r':
|
|
340
|
+ goto end_of_headers;
|
|
341
|
+ default:
|
|
342
|
+ header_state = HDST_TEXT;
|
|
343
|
+ break;
|
|
344
|
+ }
|
|
345
|
+ break;
|
|
346
|
+ case HDST_CRLF:
|
|
347
|
+ switch(buf[b]) {
|
|
348
|
+ case '\n':
|
|
349
|
+ goto end_of_headers;
|
|
350
|
+ case '\r':
|
|
351
|
+ header_state = HDST_CRLFCR;
|
|
352
|
+ break;
|
|
353
|
+ default:
|
|
354
|
+ header_state = HDST_TEXT;
|
|
355
|
+ break;
|
|
356
|
+ }
|
|
357
|
+ break;
|
|
358
|
+ case HDST_CRLFCR:
|
|
359
|
+ switch(buf[b]) {
|
|
360
|
+ case '\n':
|
|
361
|
+ case '\r':
|
|
362
|
+ goto end_of_headers;
|
|
363
|
+ default:
|
|
364
|
+ header_state = HDST_TEXT;
|
|
365
|
+ break;
|
|
366
|
+ }
|
|
367
|
+ break;
|
|
368
|
+ }
|
|
369
|
+ }
|
|
370
|
+ }
|
|
371
|
+end_of_headers:
|
|
372
|
+ if(bytes > 0) {
|
|
373
|
+ ++b;
|
|
374
|
+ (void) write(1, &buf[b], bytes - b);
|
|
375
|
+ }
|
|
376
|
+ for(;;) {
|
|
377
|
+ (void) alarm(timeout);
|
|
378
|
+ if(protocol == PROTO_HTTPS) {
|
|
379
|
+ bytes = SSL_read(ssl, buf, sizeof(buf));
|
|
380
|
+ } else {
|
|
381
|
+ bytes = read(sockfd, buf, sizeof(buf));
|
|
382
|
+ }
|
|
383
|
+ if(bytes == 0) {
|
|
384
|
+ break;
|
|
385
|
+ }
|
|
386
|
+ if(bytes < 0) {
|
|
387
|
+ show_error("read");
|
|
388
|
+ }
|
|
389
|
+ (void) write(1, buf, bytes);
|
|
390
|
+ }
|
|
391
|
+ if(protocol == PROTO_HTTPS) {
|
|
392
|
+ SSL_free(ssl);
|
|
393
|
+ SSL_CTX_free(ssl_ctx);
|
|
394
|
+ }
|
|
395
|
+ (void) close(sockfd);
|
|
396
|
+ return status;
|
|
397
|
+}
|
|
398
|
+
|
|
399
|
+void* malloc_check(size_t size) {
|
|
400
|
+ void* ptr = malloc(size);
|
|
401
|
+ check(ptr);
|
|
402
|
+ return ptr;
|
|
403
|
+}
|
|
404
|
+
|
|
405
|
+int open_client_socket(char* hostname, unsigned short port) {
|
|
406
|
+ struct addrinfo hints;
|
|
407
|
+ char portstr[10];
|
|
408
|
+ int gaierr;
|
|
409
|
+ struct addrinfo* ai;
|
|
410
|
+ struct addrinfo* ai2;
|
|
411
|
+ struct addrinfo* aiv4;
|
|
412
|
+ struct addrinfo* aiv6;
|
|
413
|
+ struct sockaddr_in6 sa;
|
|
414
|
+ int sa_len, sock_family, sock_type, sock_protocol;
|
|
415
|
+ int sockfd;
|
|
416
|
+
|
|
417
|
+ (void) memset((void*) &sa, 0, sizeof(sa));
|
|
418
|
+ (void) memset(&hints, 0, sizeof(hints));
|
|
419
|
+ hints.ai_family = PF_UNSPEC;
|
|
420
|
+ hints.ai_socktype = SOCK_STREAM;
|
|
421
|
+ (void) snprintf(portstr, sizeof(portstr), "%d", (int) port);
|
|
422
|
+ if((gaierr = getaddrinfo(hostname, portstr, &hints, &ai)) != 0) {
|
|
423
|
+ (void) fprintf(stderr, "%s: getaddrinfo %s - %s\n", argv0, hostname, gai_strerror(gaierr));
|
|
424
|
+ exit(1);
|
|
425
|
+ }
|
|
426
|
+ aiv4 = (struct addrinfo*) 0;
|
|
427
|
+ aiv6 = (struct addrinfo*) 0;
|
|
428
|
+ for(ai2 = ai; ai2 != (struct addrinfo*) 0; ai2 = ai2->ai_next) {
|
|
429
|
+ switch(ai2->ai_family) {
|
|
430
|
+ case AF_INET:
|
|
431
|
+ if(aiv4 == (struct addrinfo*) 0) {
|
|
432
|
+ aiv4 = ai2;
|
|
433
|
+ }
|
|
434
|
+ break;
|
|
435
|
+ case AF_INET6:
|
|
436
|
+ if(aiv6 == (struct addrinfo*) 0) {
|
|
437
|
+ aiv6 = ai2;
|
|
438
|
+ }
|
|
439
|
+ break;
|
|
440
|
+ }
|
|
441
|
+ }
|
|
442
|
+ if(aiv4 != (struct addrinfo*) 0) {
|
|
443
|
+ if(sizeof(sa) < aiv4->ai_addrlen) {
|
|
444
|
+ (void) fprintf(stderr, "%s - sockaddr too small (%lu < %lu)\n", hostname, (unsigned long) sizeof(sa), (unsigned long) aiv4->ai_addrlen);
|
|
445
|
+ exit(1);
|
|
446
|
+ }
|
|
447
|
+ sock_family = aiv4->ai_family;
|
|
448
|
+ sock_type = aiv4->ai_socktype;
|
|
449
|
+ sock_protocol = aiv4->ai_protocol;
|
|
450
|
+ sa_len = aiv4->ai_addrlen;
|
|
451
|
+ (void) memmove(&sa, aiv4->ai_addr, sa_len);
|
|
452
|
+ goto ok;
|
|
453
|
+ }
|
|
454
|
+ if(aiv6 != (struct addrinfo*) 0) {
|
|
455
|
+ if(sizeof(sa) < aiv6->ai_addrlen) {
|
|
456
|
+ (void) fprintf(stderr, "%s - sockaddr too small (%lu < %lu)\n", hostname, (unsigned long) sizeof(sa), (unsigned long) aiv6->ai_addrlen);
|
|
457
|
+ exit(1);
|
|
458
|
+ }
|
|
459
|
+ sock_family = aiv6->ai_family;
|
|
460
|
+ sock_type = aiv6->ai_socktype;
|
|
461
|
+ sock_protocol = aiv6->ai_protocol;
|
|
462
|
+ sa_len = aiv6->ai_addrlen;
|
|
463
|
+ (void) memmove(&sa, aiv6->ai_addr, sa_len);
|
|
464
|
+ goto ok;
|
|
465
|
+ }
|
|
466
|
+ (void) fprintf(stderr, "%s: no valid address found for host %s\n", argv0, hostname);
|
|
467
|
+ exit(1);
|
|
468
|
+ok:
|
|
469
|
+ freeaddrinfo(ai);
|
|
470
|
+ sockfd = socket(sock_family, sock_type, sock_protocol);
|
|
471
|
+ if(sockfd < 0) {
|
|
472
|
+ show_error("socket");
|
|
473
|
+ }
|
|
474
|
+ if(connect(sockfd, (struct sockaddr*) &sa, sa_len) < 0) {
|
|
475
|
+ show_error("connect");
|
|
476
|
+ }
|
|
477
|
+ return sockfd;
|
|
478
|
+}
|
|
479
|
+
|
|
480
|
+void postURL(char* url, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value, char** args, int argc) {
|
|
481
|
+ char* s;
|
|
482
|
+ int protocol;
|
|
483
|
+ char host[2000];
|
|
484
|
+ int host_len;
|
|
485
|
+ unsigned short port;
|
|
486
|
+ char* file = 0;
|
|
487
|
+ char* http = "http://";
|
|
488
|
+ int http_len = strlen(http);
|
|
489
|
+ char* https = "https://";
|
|
490
|
+ int https_len = strlen(https);
|
|
491
|
+ int proto_len;
|
|
492
|
+
|
|
493
|
+ if(url == (char*) 0) {
|
|
494
|
+ (void) fprintf(stderr, "%s: null URL\n", argv0);
|
|
495
|
+ exit(1);
|
|
496
|
+ }
|
|
497
|
+ if(strncmp(http, url, http_len) == 0) {
|
|
498
|
+ proto_len = http_len;
|
|
499
|
+ protocol = PROTO_HTTP;
|
|
500
|
+ } else if(strncmp(https, url, https_len) == 0) {
|
|
501
|
+ proto_len = https_len;
|
|
502
|
+ protocol = PROTO_HTTPS;
|
|
503
|
+ } else {
|
|
504
|
+ (void) fprintf(stderr, "%s: non-http URL\n", argv0);
|
|
505
|
+ exit(1);
|
|
506
|
+ }
|
|
507
|
+ for(s = url + proto_len; *s != '\0' && *s != ':' && *s != '/'; ++s);
|
|
508
|
+ host_len = s - url;
|
|
509
|
+ host_len -= proto_len;
|
|
510
|
+ strncpy(host, url + proto_len, host_len);
|
|
511
|
+ host[host_len] = '\0';
|
|
512
|
+ if(*s == ':') {
|
|
513
|
+ port = (unsigned short) atoi(++s);
|
|
514
|
+ while(*s != '\0' && *s != '/') {
|
|
515
|
+ ++s;
|
|
516
|
+ }
|
|
517
|
+ } else {
|
|
518
|
+ if(protocol == PROTO_HTTPS) {
|
|
519
|
+ port = 443;
|
|
520
|
+ } else {
|
|
521
|
+ port = 80;
|
|
522
|
+ }
|
|
523
|
+ }
|
|
524
|
+ if(*s == '\0') {
|
|
525
|
+ file = "/";
|
|
526
|
+ } else {
|
|
527
|
+ file = s;
|
|
528
|
+ }
|
|
529
|
+ postURLbyParts(protocol, host, port, file, referer, user_agent, auth_token, ncookies, cookies, header_name, header_value, args, argc);
|
|
530
|
+}
|
|
531
|
+
|
|
532
|
+void postURLbyParts(int protocol, char* host, unsigned short port, char* file, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value, char** args, int argc) {
|
|
533
|
+ int sockfd;
|
|
534
|
+ SSL_CTX* ssl_ctx = (SSL_CTX*) 0;
|
|
535
|
+ SSL* ssl = (SSL*) 0;
|
|
536
|
+ char head_buf[20000];
|
|
537
|
+ int max_arg, total_bytes;
|
|
538
|
+ int multipart, next_arg_is_file;
|
|
539
|
+ static const char* const sep = "http_post-content-separator";
|
|
540
|
+ char* data_buf;
|
|
541
|
+ char* enc_buf;
|
|
542
|
+ int head_bytes, data_bytes, i, header_state;
|
|
543
|
+ char* eq;
|
|
544
|
+
|
|
545
|
+ (void) alarm(timeout);
|
|
546
|
+ sockfd = open_client_socket(host, port);
|
|
547
|
+ if(protocol == PROTO_HTTPS) {
|
|
548
|
+ int r;
|
|
549
|
+ ssl_ctx = SSL_CTX_new(TLSv1_client_method());
|
|
550
|
+ SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, 0);
|
|
551
|
+ ssl = SSL_new(ssl_ctx);
|
|
552
|
+ SSL_set_fd(ssl, sockfd);
|
|
553
|
+ r = SSL_connect(ssl);
|
|
554
|
+ if(r <= 0) {
|
|
555
|
+ (void) fprintf(stderr, "%s: %s - SSL connection failed - %d\n", argv0, url, r);
|
|
556
|
+ exit(1);
|
|
557
|
+ }
|
|
558
|
+ }
|
|
559
|
+ multipart = 0;
|
|
560
|
+ total_bytes = max_arg = 0;
|
|
561
|
+ next_arg_is_file = 0;
|
|
562
|
+ for(i = 0; i < argc ; ++i) {
|
|
563
|
+ int l = strlen(args[i]);
|
|
564
|
+ if(strcmp(args[i], "-f") == 0) {
|
|
565
|
+ multipart = 1;
|
|
566
|
+ next_arg_is_file = 1;
|
|
567
|
+ continue;
|
|
568
|
+ }
|
|
569
|
+ total_bytes += l;
|
|
570
|
+ if(l > max_arg) {
|
|
571
|
+ max_arg = l;
|
|
572
|
+ }
|
|
573
|
+ if(next_arg_is_file) {
|
|
574
|
+ eq = strchr(args[i], '=');
|
|
575
|
+ if(eq == (char*) 0) {
|
|
576
|
+ (void) fprintf(stderr, "%s: missing filename\n", argv0);
|
|
577
|
+ exit(1);
|
|
578
|
+ } else {
|
|
579
|
+ ++eq;
|
|
580
|
+ total_bytes += file_bytes(eq);
|
|
581
|
+ }
|
|
582
|
+ next_arg_is_file = 0;
|
|
583
|
+ }
|
|
584
|
+ }
|
|
585
|
+ if(multipart) {
|
|
586
|
+ for(i = 0; i < argc ; ++i) {
|
|
587
|
+ total_bytes += strlen(sep) * 2 + 100;
|
|
588
|
+ }
|
|
589
|
+ total_bytes += strlen(sep) * 2;
|
|
590
|
+ } else {
|
|
591
|
+ total_bytes *= 4;
|
|
592
|
+ enc_buf = (char*) malloc_check(max_arg * 4);
|
|
593
|
+ }
|
|
594
|
+ data_buf = (char*) malloc_check(total_bytes);
|
|
595
|
+ if(multipart) {
|
|
596
|
+ next_arg_is_file = 0;
|
|
597
|
+ data_bytes = 0;
|
|
598
|
+ for(i = 0; i < argc; ++i) {
|
|
599
|
+ if(strcmp(args[i], "-f") == 0) {
|
|
600
|
+ next_arg_is_file = 1;
|
|
601
|
+ continue;
|
|
602
|
+ }
|
|
603
|
+ eq = strchr(args[i], '=');
|
|
604
|
+ if(eq == (char*) 0) {
|
|
605
|
+ data_bytes += sprintf(&data_buf[data_bytes], "--%s\r\nContent-Disposition: form-data\r\n\r\n%s\r\n", sep, args[i]);
|
|
606
|
+ } else {
|
|
607
|
+ *eq++ = '\0';
|
|
608
|
+ if(next_arg_is_file) {
|
|
609
|
+ data_bytes += sprintf(&data_buf[data_bytes], "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n\r\n", sep, args[i], eq);
|
|
610
|
+ data_bytes += file_copy(eq, &data_buf[data_bytes]);
|
|
611
|
+ data_bytes += sprintf(&data_buf[data_bytes], "\r\n");
|
|
612
|
+ next_arg_is_file = 0;
|
|
613
|
+ } else {
|
|
614
|
+ data_bytes += sprintf(&data_buf[data_bytes], "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n", sep, args[i], eq);
|
|
615
|
+ }
|
|
616
|
+ }
|
|
617
|
+ }
|
|
618
|
+ data_bytes += sprintf(&data_buf[data_bytes], "--%s--\r\n", sep);
|
|
619
|
+ } else {
|
|
620
|
+ data_bytes = 0;
|
|
621
|
+ for(i = 0; i < argc ; ++i) {
|
|
622
|
+ if(data_bytes > 0) {
|
|
623
|
+ data_bytes += sprintf(&data_buf[data_bytes], "&");
|
|
624
|
+ }
|
|
625
|
+ eq = strchr(args[i], '=');
|
|
626
|
+ if(eq == (char*) 0) {
|
|
627
|
+ strencode(enc_buf, args[i]);
|
|
628
|
+ data_bytes += sprintf(&data_buf[data_bytes], "%s", enc_buf);
|
|
629
|
+ } else {
|
|
630
|
+ *eq++ = '\0';
|
|
631
|
+ strencode(enc_buf, args[i]);
|
|
632
|
+ data_bytes += sprintf(&data_buf[data_bytes], "%s=", enc_buf);
|
|
633
|
+ strencode(enc_buf, eq);
|
|
634
|
+ data_bytes += sprintf(&data_buf[data_bytes], "%s", enc_buf);
|
|
635
|
+ }
|
|
636
|
+ }
|
|
637
|
+ }
|
|
638
|
+ (void) alarm(timeout);
|
|
639
|
+ head_bytes = snprintf(head_buf, sizeof(head_buf), "POST %s HTTP/1.0\r\n", file);
|
|
640
|
+ head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Host: %s\r\n", host);
|
|
641
|
+ head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Content-Length: %d\r\n", data_bytes);
|
|
642
|
+ if(referer != (char*) 0) {
|
|
643
|
+ head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Referer: %s\r\n", referer);
|
|
644
|
+ }
|
|
645
|
+ head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "User-Agent: %s\r\n", user_agent);
|
|
646
|
+ if(multipart) {
|
|
647
|
+ head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Content-type: multipart/form-data; boundary=\"%s\"\r\n", sep);
|
|
648
|
+ } else {
|
|
649
|
+ head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Content-type: application/x-www-form-urlencoded\r\n");
|
|
650
|
+ }
|
|
651
|
+ head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Accept: */*\r\n");
|
|
652
|
+ head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Accept-Language: en\r\n");
|
|
653
|
+ head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Accept-Charset: iso-8859-1,*,utf-8\r\n");
|
|
654
|
+ if(auth_token != (char*) 0) {
|
|
655
|
+ char token_buf[1000];
|
|
656
|
+ token_buf[b64_encode((unsigned char*) auth_token, strlen(auth_token), token_buf, sizeof(token_buf))] = '\0';
|
|
657
|
+ head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Authorization: Basic %s\r\n", token_buf);
|
|
658
|
+ }
|
|
659
|
+ for(i = 0; i < ncookies; ++i) {
|
|
660
|
+ head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Cookie: %s\r\n", cookies[i]);
|
|
661
|
+ }
|
|
662
|
+ if(header_name != (char*) 0) {
|
|
663
|
+ head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "%s: %s\r\n", header_name, header_value);
|
|
664
|
+ }
|
|
665
|
+ head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "\r\n");
|
|
666
|
+ if(protocol == PROTO_HTTPS) {
|
|
667
|
+ (void) SSL_write(ssl, head_buf, head_bytes);
|
|
668
|
+ } else {
|
|
669
|
+ (void) write(sockfd, head_buf, head_bytes);
|
|
670
|
+ }
|
|
671
|
+ if(protocol == PROTO_HTTPS) {
|
|
672
|
+ (void) SSL_write(ssl, data_buf, data_bytes);
|
|
673
|
+ } else {
|
|
674
|
+ (void) write(sockfd, data_buf, data_bytes);
|
|
675
|
+ }
|
|
676
|
+ (void) alarm(timeout);
|
|
677
|
+ header_state = HDST_BOL;
|
|
678
|
+ for(;;) {
|
|
679
|
+ if(protocol == PROTO_HTTPS) {
|
|
680
|
+ head_bytes = SSL_read(ssl, head_buf, sizeof(head_buf));
|
|
681
|
+ } else {
|
|
682
|
+ head_bytes = read(sockfd, head_buf, sizeof(head_buf));
|
|
683
|
+ }
|
|
684
|
+ if(head_bytes <= 0) {
|
|
685
|
+ break;
|
|
686
|
+ }
|
|
687
|
+ for(i = 0; i < head_bytes; ++i) {
|
|
688
|
+ if(verbose) {
|
|
689
|
+ (void) write(1, &head_buf[i], 1);
|
|
690
|
+ }
|
|
691
|
+ switch(header_state) {
|
|
692
|
+ case HDST_BOL:
|
|
693
|
+ switch(head_buf[i]) {
|
|
694
|
+ case '\n':
|
|
695
|
+ header_state = HDST_LF;
|
|
696
|
+ break;
|
|
697
|
+ case '\r':
|
|
698
|
+ header_state = HDST_CR;
|
|
699
|
+ break;
|
|
700
|
+ default:
|
|
701
|
+ header_state = HDST_TEXT;
|
|
702
|
+ break;
|
|
703
|
+ }
|
|
704
|
+ break;
|
|
705
|
+ case HDST_TEXT:
|
|
706
|
+ switch(head_buf[i]) {
|
|
707
|
+ case '\n':
|
|
708
|
+ header_state = HDST_LF;
|
|
709
|
+ break;
|
|
710
|
+ case '\r':
|
|
711
|
+ header_state = HDST_CR;
|
|
712
|
+ break;
|
|
713
|
+ }
|
|
714
|
+ break;
|
|
715
|
+ case HDST_LF:
|
|
716
|
+ switch(head_buf[i]) {
|
|
717
|
+ case '\n':
|
|
718
|
+ goto end_of_headers;
|
|
719
|
+ case '\r':
|
|
720
|
+ header_state = HDST_CR;
|
|
721
|
+ break;
|
|
722
|
+ default:
|
|
723
|
+ header_state = HDST_TEXT;
|
|
724
|
+ break;
|
|
725
|
+ }
|
|
726
|
+ break;
|
|
727
|
+ case HDST_CR:
|
|
728
|
+ switch(head_buf[i]) {
|
|
729
|
+ case '\n':
|
|
730
|
+ header_state = HDST_CRLF;
|
|
731
|
+ break;
|
|
732
|
+ case '\r':
|
|
733
|
+ goto end_of_headers;
|
|
734
|
+ default:
|
|
735
|
+ header_state = HDST_TEXT;
|
|
736
|
+ break;
|
|
737
|
+ }
|
|
738
|
+ break;
|
|
739
|
+ case HDST_CRLF:
|
|
740
|
+ switch(head_buf[i]) {
|
|
741
|
+ case '\n':
|
|
742
|
+ goto end_of_headers;
|
|
743
|
+ case '\r':
|
|
744
|
+ header_state = HDST_CRLFCR;
|
|
745
|
+ break;
|
|
746
|
+ default:
|
|
747
|
+ header_state = HDST_TEXT;
|
|
748
|
+ break;
|
|
749
|
+ }
|
|
750
|
+ break;
|
|
751
|
+ case HDST_CRLFCR:
|
|
752
|
+ switch(head_buf[i]) {
|
|
753
|
+ case '\n':
|
|
754
|
+ case '\r':
|
|
755
|
+ goto end_of_headers;
|
|
756
|
+ default:
|
|
757
|
+ header_state = HDST_TEXT;
|
|
758
|
+ break;
|
|
759
|
+ }
|
|
760
|
+ break;
|
|
761
|
+ }
|
|
762
|
+ }
|
|
763
|
+ }
|
|
764
|
+end_of_headers:
|
|
765
|
+ if(head_bytes > 0) {
|
|
766
|
+ ++i;
|
|
767
|
+ (void) write(1, &head_buf[i], head_bytes - i);
|
|
768
|
+ }
|
|
769
|
+ for(;;) {
|
|
770
|
+ (void) alarm(timeout);
|
|
771
|
+ if(protocol == PROTO_HTTPS) {
|
|
772
|
+ head_bytes = SSL_read(ssl, head_buf, sizeof(head_buf));
|
|
773
|
+ } else {
|
|
774
|
+ head_bytes = read(sockfd, head_buf, sizeof(head_buf));
|
|
775
|
+ }
|
|
776
|
+ if(head_bytes == 0) {
|
|
777
|
+ break;
|
|
778
|
+ }
|
|
779
|
+ if(head_bytes < 0) {
|
|
780
|
+ show_error("read");
|
|
781
|
+ }
|
|
782
|
+ (void) write(1, head_buf, head_bytes);
|
|
783
|
+ }
|
|
784
|
+ if(protocol == PROTO_HTTPS) {
|
|
785
|
+ SSL_free(ssl);
|
|
786
|
+ SSL_CTX_free(ssl_ctx);
|
|
787
|
+ }
|
|
788
|
+ (void) close(sockfd);
|
|
789
|
+}
|
|
790
|
+
|
|
791
|
+void show_error(char* cause) {
|
|
792
|
+ char buf[5000];
|
|
793
|
+ (void) sprintf(buf, "%s: %s - %s", argv0, url, cause);
|
|
794
|
+ perror(buf);
|
|
795
|
+ exit(1);
|
|
796
|
+}
|
|
797
|
+
|
|
798
|
+void sigcatch(int sig) {
|
|
799
|
+ (void) fprintf(stderr, "%s: %s - timed out\n", argv0, url);
|
|
800
|
+ exit(1);
|
|
801
|
+}
|
|
802
|
+
|
|
803
|
+void strencode(char* to, char* from) {
|
|
804
|
+ int tolen;
|
|
805
|
+
|
|
806
|
+ for(tolen = 0; *from != '\0'; ++from) {
|
|
807
|
+ if(isalnum(*from) || strchr("/_.", *from) != (char*) 0) {
|
|
808
|
+ *to = *from;
|
|
809
|
+ ++to;
|
|
810
|
+ ++tolen;
|
|
811
|
+ } else {
|
|
812
|
+ (void) sprintf(to, "%%%02x", (int) *from & 0xff);
|
|
813
|
+ to += 3;
|
|
814
|
+ tolen += 3;
|
|
815
|
+ }
|
|
816
|
+ }
|
|
817
|
+ *to = '\0';
|
|
818
|
+}
|
|
819
|
+
|
|
820
|
+void usage() {
|
|
821
|
+ (void) fprintf(stderr, "usage: %s [-c cookie] [-t timeout] [-r referer] [-u user-agent] [-a username:password] [-h header value] [-v] url\n", argv0);
|
|
822
|
+ exit(1);
|
|
823
|
+}
|