-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
69 lines (59 loc) · 1.81 KB
/
main.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
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "http/client.h"
#include "http/uri.h"
#include "http/types.h"
int main(int argc, char **argv) {
Uri uri = empty_uri();
HttpSession session = empty_http_session();
size_t download_size = 0;
try {
// parse args
if (argc != 2) {
fprintf(stderr, "%s HOST\n", argv[0]);
throw(1);
}
// parse uri
uri = parse_uri(argv[1], strlen(argv[1]));
if (uri.scheme == SCHEME_UNKNOWN) {
fprintf(stderr, "Invalid uri\n");
throw(2);
}
if (uri.scheme == SCHEME_HTTPS) {
fprintf(stderr, "Unsupported scheme\n");
throw(3);
}
// send request
int err_code = http_send_get(&session, uri, NULL);
if (err_code) {
fprintf(stderr, "Send get error: %d\n", err_code);
throw(3);
}
// read response
err_code = http_read_response(&session, empty_buffer());
if (err_code) {
printf("Read response error: %d\n", err_code);
throw(err_code);
}
// read response data
char buffer[1024];
while (session.read_state == READ_STATE_READ_DATA) {
ssize_t r_res = http_read_data(&session, buffer, sizeof(buffer));
if (r_res > 0) {
fwrite(buffer, (size_t)r_res, 1, stdout);
download_size += r_res;
}
}
if (!session.read_state != READ_STATE_COMPLETE) {
throw(session.err_code);
}
}
catch {
}
free_uri(uri);
free_http_session(session);
fprintf(stderr, "Complete %lld bytes, err_code=%d\n", (long long)download_size, (int)_try_error_code);
fflush(stdout);
return (int)_try_error_code;
}