|
2 | 2 |
|
3 | 3 | int uv_main(int argc, char **argv) { |
4 | 4 | yield(); |
5 | | - if (argc <= 1) { |
6 | | - cerr("Usage: fetch url"CLR_LN); |
7 | | - return 1; |
8 | | - } |
9 | | - |
10 | | - string data = nullptr; |
11 | | - int chunks = 0; |
12 | | - url_t *url = parse_url(argv[1]); |
13 | | - if (!is_empty(url)) { |
14 | | - dnsinfo_t *dns = get_addrinfo(url->host, url->scheme, 3, |
15 | | - kv(ai_flags, AF_UNSPEC), |
16 | | - kv(ai_socktype, SOCK_STREAM), |
17 | | - kv(ai_protocol, IPPROTO_TCP) |
18 | | - ); |
| 5 | + cli_message_set("\turl - website\n", false); |
| 6 | + if (is_cli_getopt(nullptr, true)) { |
| 7 | + string data = nullptr; |
| 8 | + int chunks = 0; |
| 9 | + url_t *url = parse_url(cli_getopt()); |
| 10 | + if (!is_empty(url)) { |
| 11 | + dnsinfo_t *dns = get_addrinfo(url->host, url->scheme, 3, |
| 12 | + kv(ai_flags, AF_UNSPEC), |
| 13 | + kv(ai_socktype, SOCK_STREAM), |
| 14 | + kv(ai_protocol, IPPROTO_TCP) |
| 15 | + ); |
19 | 16 |
|
20 | | - use_ca_certificate("cert.pem"); |
21 | | - uv_stream_t *client = stream_secure(addrinfo_ip(dns), url->host, url->port); |
22 | | - if (!is_empty(client) && stream_write(client, "GET /"CRLF)) { |
23 | | - cout(CLR_LN); |
24 | | - while (stream_peek(client) != UV_EOF) { |
25 | | - if (!is_empty(data = stream_read(client))) |
26 | | - cout("%s", data); |
27 | | - else |
28 | | - break; |
| 17 | + use_ca_certificate("cert.pem"); |
| 18 | + uv_stream_t *client = stream_secure(addrinfo_ip(dns), url->host, url->port); |
| 19 | + if (!is_empty(client) && stream_write(client, "GET /"CRLF)) { |
| 20 | + cout(CLR_LN); |
| 21 | + while (stream_peek(client) != UV_EOF) { |
| 22 | + if (!is_empty(data = stream_read(client))) |
| 23 | + cout(data); |
| 24 | + else |
| 25 | + break; |
29 | 26 |
|
30 | | - chunks++; |
| 27 | + chunks++; |
| 28 | + } |
31 | 29 | } |
| 30 | + |
| 31 | + cout("\n\nReceived: %d chunks.\n", chunks); |
| 32 | + } else { |
| 33 | + return is_cli_getopt("help", false); |
32 | 34 | } |
33 | 35 | } |
34 | 36 |
|
35 | | - cout("\n\nReceived: %d chunks.\n", chunks); |
36 | 37 | return coro_err_code(); |
37 | 38 | } |
0 commit comments