-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtcli.c
More file actions
362 lines (316 loc) · 8.59 KB
/
htcli.c
File metadata and controls
362 lines (316 loc) · 8.59 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
struct http_t {
char* host;
short port;
char* uri; /* uri doesn't have leading slash. */
int sock;
FILE* sockf;
/* It's a pointer to internal buffer for
memory managment simplicity */
char* buf;
};
#define HTTP_PORT 80
#define DEF_SCHEME "http://"
#define MAXSTACKBUF 2048
#define MAX_HEADERS_LINES 2048
#define MAX_FILE_SIZE (1024*1024*1024)
#define TIMEOUT 6
#define NETREADBUF 65536
struct http_t*
ht_init_handle(void)
{
struct http_t* ptr=(struct http_t*)malloc(sizeof(struct http_t));
if (ptr) {
memset(ptr,0,sizeof(struct http_t));
ptr->sock = -1;
}
return ptr;
}
void
ht_free_handle(struct http_t* h)
{
if (h) {
free(h->buf);
if (h->sockf)
fclose(h->sockf);
else if (h->sock>=0)
close(h->sock);
free(h);
}
}
int
ht_parse_url(struct http_t* handle,char* t)
{
/* Only allowed urls with "http://" scheme prefix. */
if (strncasecmp(DEF_SCHEME,t,sizeof(DEF_SCHEME)-1)==0) {
free(handle->buf);
handle->buf=handle->host=strdup(t+sizeof(DEF_SCHEME)-1);
if (!handle->buf) {
fprintf(stderr,"Unable to allocate memory for host structure.\n");
return -1;
}
if (strchr(handle->host,'@')) {
fprintf(stderr,"Authentication url section is not supported.\n");
return -1;
}
/* path is located after the first '/' */
if ((handle->uri=strchr(handle->host,'/'))!=NULL) {
*(handle->uri)='\0';
handle->uri +=1;
} else {
/* Make absent uri path point to empty string not to NULL */
handle->uri = handle->host + strlen(handle->host);
}
int p=HTTP_PORT;
char* ports=strchr(handle->host,':');
if (ports!=NULL) {
*ports='\0';
/* Skip convertation and syntax errors here */
p = atoi(ports+1);
/* Simple check for IP port range */
if (p<0 && p>0xffff) {
fprintf(stderr,"Invalid port range, assuming default is 80.\n");
p=HTTP_PORT;
}
}
handle->port = p;
/* minimum check - don't allow empty hosts */
if (*(handle->host)!='\0')
return 0;
}
fprintf(stderr,"Invalid url.\n");
return -1;
}
/* Poor man timeouts. Very ugly timeout handling is here
Real system should use timeout handling based on
nonblocking io and poll/epoll/select. */
static void
sig_alrm(int signo)
{
/* Do nothing. Networking syscalls should
abort with "Interruted system call". */
return;
}
static void
poor_timer_on(int nsecs)
{
/* Unrelaible signal syscall - one should use sigaction instead */
signal(SIGALRM,sig_alrm);
alarm(nsecs);
}
static void
poor_timer_off(void)
{
alarm(0);
}
int
ht_connect(struct http_t* h)
{
if (!h)
return -1;
struct addrinfo hints, *records;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_INET;
hints.ai_socktype = SOCK_STREAM;
int error;
if ((error=getaddrinfo(h->host,NULL, &hints, &records))!=0) {
fprintf(stderr,"Unable to resolv host: %s.\n", gai_strerror(error));
return -1;
}
uint16_t port = htons(h->port);
int sock;
struct addrinfo* r;
for (r = records; r; r = r->ai_next) {
// Only IPv4 socket here.
sock = socket(PF_INET,SOCK_STREAM, 0);
if (sock < 0) {
fprintf(stderr,"Unable to create socket endpoint.\n");
break;
}
struct sockaddr_in* iaddr=(struct sockaddr_in*)r->ai_addr;
iaddr->sin_port = port;
poor_timer_on(TIMEOUT);
if (connect(sock, r->ai_addr, r->ai_addrlen) < 0) {
fprintf(stderr,"Unable to connect %s '%s'.\n",
h->host,strerror(errno));
close(sock);
sock=-1;
continue;
} else
break;
}
poor_timer_off();
freeaddrinfo(records);
if (sock < 0) {
return -1;
}
h->sock=sock;
return 0;
}
/* It's important to send HTTP 1.0 requests since for 1.1 and up
we need to implement chuncked http read. */
static const char request_template[] = "GET /%s HTTP/1.0\r\n"
"User-Agent: : Mozilla/1.0 (linux)\r\n"
"Accept: */*\r\n"
"Accept-Encoding: identity\r\n"
"Host: %s\r\n"
"Connection: close\r\n"
"\r\n";
int
ht_send_request(struct http_t* h)
{
char buf[MAXSTACKBUF];
int need_write=snprintf(buf,sizeof(buf),request_template,
h->uri,h->host);
/* check for buffer overflow with url string */
if (need_write>sizeof(buf)) {
fprintf(stderr,"Url is to large.\n");
return -1;
}
int written=0;
while(need_write>0) {
poor_timer_on(TIMEOUT);
int n=write(h->sock,buf+written,need_write);
/* Don't need timer_off() here */
if (n<0) {
fprintf(stderr,"Unable to write to remote server: %s.\n",
strerror(errno));
return -1;
}
need_write-=n;
written +=n;
}
return 0;
}
int
ht_read_response_hdr(struct http_t* h)
{
if ((h->sockf=fdopen(h->sock,"r"))==NULL) {
fprintf(stderr,"unable to fdopen socket.\n");
return -1;
}
/* We are not going to accept HTTP lines more then MAXSTACKBUF. */
char buf[MAXSTACKBUF],*line;
poor_timer_on(TIMEOUT);
if ((line=fgets(buf,sizeof(buf),h->sockf))==NULL) {
fprintf(stderr,"Coudn't get status response from server.\n");
return -1;
}
int status=-1;
if (sscanf(line,"HTTP/%*d.%*d%*[ \t]%d",&status)!=1 || status!=200) {
fprintf(stderr,"Invalid status line response. HTTP status=%d\n",
status);
return -1;
}
/* now rewind headers to the http body */
int max_lines=MAX_HEADERS_LINES;
while(1) {
poor_timer_on(TIMEOUT); /* Don't need timer_off below */
if ((line=fgets(buf,sizeof(buf),h->sockf))==NULL) {
fprintf(stderr,"Premature eof in server response.\n");
return -1;
}
/* some sanity check for too big headers*/
if (--max_lines==0) {
fprintf(stderr,"Http header contains to much lines.\n");
return -1;
}
/* it's end of headers mark */
if (strcmp(line,"\r\n")==0 || strcmp(line,"\n")==0)
break;
}
return 0;
}
int
ht_write_file(struct http_t* h, const char* filename)
{
FILE* outf=fopen(filename,"w+");
if (!outf) {
fprintf(stderr,"Unable to open output file.\n");
return -1;
}
int file_limit=MAX_FILE_SIZE;
char* buf=(char*)malloc(NETREADBUF);
if (!buf) {
fprintf(stderr,"Unable to allocate buffer for network read.\n");
return -1;
}
while(1) {
poor_timer_on(TIMEOUT);
int n = fread(buf,1,NETREADBUF,h->sockf);
poor_timer_off();
/* reading zero bytes means server've closed
connection let's ignore errors */
if (n==0)
break;
file_limit -=n;
/* Preventing file bombing. */
if (file_limit<=0) {
fprintf(stderr,"File is too large. Stop.\n");
break;
}
int wn = fwrite(buf,1,n,outf);
if (wn!=n) { /* It's a error on the file system */
fprintf(stderr,"Error happened while writing file.\n");
break;
}
}
free(buf);
fclose(outf);
return 0;
}
int
main(int ac, char* av[])
{
if (ac<2) {
fprintf(stderr,"Invalid usage: %s url [filename]\n",av[0]);
exit(-1);
}
char* filename = "/dev/fd/1";
if (ac==3)
filename=av[2];
int exit_code=0;
struct http_t* hdl = ht_init_handle();
if (!hdl || ht_parse_url(hdl,av[1])!=0 ||ht_connect(hdl)!=0 ||
ht_send_request(hdl)!=0 || ht_read_response_hdr(hdl)!=0 ||
ht_write_file(hdl,filename)!=0) {
fprintf(stderr,"Unable to retrive and save file.\n");
exit_code=-1;
}
ht_free_handle(hdl);
exit(exit_code);
}
#if 0
int
main(int ac, char* av)
{
struct http_t* hdl = ht_init_handle();
int r = ht_parse_url(hdl,"http://");
printf("ret %d, host '%s', port %hd, uri '%s'\n", r, hdl->host,hdl->port,hdl->uri);
r = ht_parse_url(hdl,"httpx://");
printf("ret %d, host '%s', port %hd, uri '%s'\n", r, hdl->host,hdl->port,hdl->uri);
r = ht_parse_url(hdl,"");
printf("ret %d, host '%s', port %hd, uri '%s'\n", r, hdl->host,hdl->port,hdl->uri);
r = ht_parse_url(hdl,"http://mail.ru");
printf("ret %d, host '%s', port %hd, uri '%s'\n", r, hdl->host,hdl->port,hdl->uri);
r = ht_parse_url(hdl,"http://mail.ru:80/");
printf("ret %d, host '%s', port %hd, uri '%s'\n", r, hdl->host,hdl->port,hdl->uri);
r = ht_parse_url(hdl,"http://mail.ru/:80/");
printf("ret %d, host '%s', port %hd, uri '%s'\n", r, hdl->host,hdl->port,hdl->uri);
r = ht_parse_url(hdl,"http://:");
printf("ret %d, host '%s', port %hd, uri '%s'\n", r, hdl->host,hdl->port,hdl->uri);
r = ht_parse_url(hdl,"http://mail.ru:");
printf("ret %d, host '%s', port %hd, uri '%s'\n", r, hdl->host,hdl->port,hdl->uri);
r = ht_parse_url(hdl,"http://mvideo.ru:path");
printf("ret %d, host '%s', port %hd, uri '%s'\n", r, hdl->host,hdl->port,hdl->uri);
}
#endif