-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttpc.c
189 lines (157 loc) · 4.43 KB
/
httpc.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
/*
* Copyright (c) 2014-2021 Jan Klemkow <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "http_parser.h"
#include "dprintf.c"
#define READ_FD 6
#define WRITE_FD 7
static void
usage(void)
{
fprintf(stderr, "httpc [-h] [-H HOST] [-o file] [URI]\n");
exit(EXIT_FAILURE);
}
void
read_header(struct http_response *head, FILE *fh)
{
char buf[BUFSIZ];
memset(head, 0, sizeof *head);
if (http_read_line_fh(fh , buf, sizeof buf) == -1)
errx(EXIT_FAILURE, "http_read_line failed");
head->code = http_parse_code(buf, sizeof buf);
if (head->code == -1)
errx(EXIT_FAILURE, "unable to parse HTTP RETURN CODE");
if (head->code != 200)
errx(EXIT_FAILURE, "http_parse_code...%d %s\n", head->code,
http_reason_phrase(head->code));
/* read response header */
do {
if (http_read_line_fh(fh, buf, sizeof buf) == -1)
errx(EXIT_FAILURE, "http_read_line_fh failed");
if (http_parse_line(head, buf) == -1)
errx(EXIT_FAILURE, "http_parse_line failed");
} while (strcmp(buf, "\r\n") != 0);
}
void
read_content(size_t content_length, FILE *in, FILE *out)
{
char buf[BUFSIZ];
size_t size = sizeof buf;
/* handle content */
for (;content_length > 0; content_length -= size) {
if (content_length < size)
size = content_length;
if (fread(buf, size , 1, in) == 0)
err(EXIT_FAILURE, "fread");
if (fwrite(buf, size, 1, out) == 0)
err(EXIT_FAILURE, "fwrite");
}
}
void
read_content_chunked(struct http_response *head, FILE *in, FILE *out)
{
size_t content_length = 0;
char buf[BUFSIZ];
int old_errno = 0;
do {
/* read: chunk-size [chunk-ext] CRLF */
if (http_read_line_fh(in, buf, sizeof buf) == -1)
errx(EXIT_FAILURE, "http_read_line failed");
/* parse: chunk-size */
old_errno = errno; errno = 0;
content_length = strtol(buf, NULL, 16);
if (errno != 0)
errx(EXIT_FAILURE, "unreadable chunk size");
errno = old_errno;
if (content_length > 0) {
/* read: chunk-data */
read_content(content_length, in, out);
/* read: CRLF */
if (http_read_line_fh(in , buf, sizeof buf) == -1)
errx(EXIT_FAILURE, "http_read_line failed");
} else {
/* read: trailer-part */
read_header(head, in);
}
} while (content_length > 0);
}
int
main(int argc, char *argv[])
{
int ch;
int verbosity = 0;
char *host = getenv("TCPREMOTEHOST");
char *file = NULL;
char *uri = "/";
struct http_response head;
FILE *fh = NULL;
FILE *out = stdout;
if (setvbuf(stdout, NULL, _IONBF, 0) != 0)
err(EXIT_FAILURE, "setvbuf");
while ((ch = getopt(argc, argv, "H:o:gvh")) != -1) {
switch (ch) {
case 'H':
if ((host = strdup(optarg)) == NULL) goto err;
break;
case 'o':
if ((file = strdup(optarg)) == NULL) goto err;
break;
case 'v':
verbosity++;
break;
case 'h':
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc > 0)
uri = argv[0];
/* write HTTP request header */
dprintf(WRITE_FD, "GET %s HTTP/1.1\r\n", uri);
if (host != NULL)
dprintf(WRITE_FD, "Host: %s\r\n", host);
dprintf(WRITE_FD, "Accept-Encoding: gzip\r\n");
dprintf(WRITE_FD, "Connection: close\r\n");
dprintf(WRITE_FD, "\r\n");
if ((fh = fdopen(READ_FD, "r")) == NULL)
err(EXIT_FAILURE, "fdopen");
if (file == NULL)
file = basename(uri);
read_header(&head, fh);
if (head.content_encoding == HTTP_CONT_ENC_GZIP) {
if ((out = popen("exec gunzip", "w")) == NULL)
err(EXIT_FAILURE, "popen");
}
if (head.transfer_encoding == HTTP_TRANS_ENC_CHUNKED)
read_content_chunked(&head, fh, out);
else
read_content(head.content_length, fh, out);
return EXIT_SUCCESS;
err:
perror("err: httpc:");
return EXIT_FAILURE;
}