-
Notifications
You must be signed in to change notification settings - Fork 3
/
http_lib.c
490 lines (448 loc) · 13.5 KB
/
http_lib.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
* Http put/get mini lib
* written by L. Demailly
* (c) 1998 Laurent Demailly - http://www.demailly.com/~dl/
* (c) 1996 Observatoire de Paris - Meudon - France
* see LICENSE for terms, conditions and DISCLAIMER OF ALL WARRANTIES
*
* $Id: http_lib.c,v 3.5 1998/09/23 06:19:15 dl Exp $
*
* Description : Use http protocol, connects to server to echange data
*
* $Log: http_lib.c,v $
* Revision 3.5 1998/09/23 06:19:15 dl
* portability and http 1.x (1.1 and later) compatibility
*
* Revision 3.4 1998/09/23 05:44:27 dl
* added support for HTTP/1.x answers
*
* Revision 3.3 1996/04/25 19:07:22 dl
* using intermediate variable for htons (port) so it does not yell
* on freebsd (thx pp for report)
*
* Revision 3.2 1996/04/24 13:56:08 dl
* added proxy support through http_proxy_server & http_proxy_port
* some httpd *needs* cr+lf so provide them
* simplification + cleanup
*
* Revision 3.1 1996/04/18 13:53:13 dl
* http-tiny release 1.0
*
*
*/
static char *rcsid = "$Id: http_lib.c,v 3.5 1998/09/23 06:19:15 dl Exp $";
#define VERBOSE
/* http_lib - Http data exchanges mini library.
*/
#ifndef OSK
/* unix */
#include <arpa/inet.h>
#include <ctype.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
static int http_read_line(int fd, char *buffer, int max);
static int http_read_buffer(int fd, char *buffer, int max);
#else
/* OS/9 includes */
#include <INET/in.h>
#include <INET/netdb.h>
#include <INET/pwd.h>
#include <INET/socket.h>
#include <machine/reg.h>
#include <modes.h>
#include <types.h>
extern char *malloc();
#endif /* OS9/Unix */
#include <stdio.h>
#include "http_lib.h"
#define SERVER_DEFAULT "adonis"
/* pointer to a mallocated string containing server name or NULL */
char *http_server = NULL;
/* server port number */
int http_port = 5757;
/* pointer to proxy server name or NULL */
char *http_proxy_server = NULL;
/* proxy server port number or 0 */
int http_proxy_port = 0;
/* user agent id string */
static char *http_user_agent = "adlib/3 ($Date: 1998/09/23 06:19:15 $)";
/*
* read a line from file descriptor
* returns the number of bytes read. negative if a read error occured
* before the end of line or the max.
* cariage returns (CR) are ignored.
*/
static int http_read_line(fd, buffer,
max) int fd; /* file descriptor to read from */
char *buffer; /* placeholder for data */
int max; /* max number of bytes to read */
{ /* not efficient on long lines (multiple unbuffered 1 char reads) */
int n = 0;
while (n < max) {
if (read(fd, buffer, 1) != 1) {
n = -n;
break;
}
n++;
if (*buffer == '\015')
continue; /* ignore CR */
if (*buffer == '\012')
break; /* LF is the separator */
buffer++;
}
*buffer = 0;
return n;
}
/*
* read data from file descriptor
* retries reading until the number of bytes requested is read.
* returns the number of bytes read. negative if a read error (EOF) occured
* before the requested length.
*/
static int http_read_buffer(fd, buffer,
length) int fd; /* file descriptor to read from */
char *buffer; /* placeholder for data */
int length; /* number of bytes to read */
{
int n, r;
for (n = 0; n < length; n += r) {
r = read(fd, buffer, length - n);
if (r <= 0)
return -n;
buffer += r;
}
return n;
}
typedef enum {
CLOSE, /* Close the socket after the query (for put) */
KEEP_OPEN /* Keep it open */
} querymode;
#ifndef OSK
static http_retcode http_query(char *command, char *url,
char *additional_header, querymode mode,
char *data, int length, int *pfd);
#endif
/* beware that filename+type+rest of header must not exceed MAXBUF */
/* so we limit filename to 256 and type to 64 chars in put & get */
#define MAXBUF 512
/*
* Pseudo general http query
*
* send a command and additional headers to the http server.
* optionally through the proxy (if http_proxy_server and http_proxy_port are
* set).
*
* Limitations: the url is truncated to first 256 chars and
* the server name to 128 in case of proxy request.
*/
static http_retcode http_query(command, url, additional_header, mode, data,
length, pfd) char *command; /* command to send */
char *url; /* url / filename queried */
char *additional_header; /* additional header */
querymode mode; /* type of query */
char *data; /* Data to send after header. If NULL, not data is sent */
int length; /* size of data */
int *pfd; /* pointer to variable where to set file descriptor value */
{
int s;
struct hostent *hp;
struct sockaddr_in server;
char header[MAXBUF];
int hlg;
http_retcode ret;
int proxy = (http_proxy_server != NULL && http_proxy_port != 0);
int port = proxy ? http_proxy_port : http_port;
if (pfd)
*pfd = -1;
/* get host info by name :*/
if ((hp = gethostbyname(
proxy ? http_proxy_server
: (http_server ? http_server : SERVER_DEFAULT)))) {
memset((char *)&server, 0, sizeof(server));
memmove((char *)&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = (unsigned short)htons(port);
} else
return ERRHOST;
/* create socket */
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return ERRSOCK;
setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, 0, 0);
/* connect to server */
if (connect(s, &server, sizeof(server)) < 0)
ret = ERRCONN;
else {
if (pfd)
*pfd = s;
/* create header */
if (proxy) {
sprintf(header,
"%s http://%.128s:%d/%.256s HTTP/1.0\015\012User-Agent: "
"%s\015\012%s\015\012",
command, http_server, http_port, url, http_user_agent,
additional_header);
} else {
sprintf(header,
"%s /%.256s HTTP/1.0\015\012User-Agent: %s\015\012%s\015\012",
command, url, http_user_agent, additional_header);
}
hlg = strlen(header);
/* send header */
if (write(s, header, hlg) != hlg)
ret = ERRWRHD;
/* send data */
else if (length && data && (write(s, data, length) != length))
ret = ERRWRDT;
else {
/* read result & check */
ret = http_read_line(s, header, MAXBUF - 1);
#ifdef VERBOSE
fputs(header, stderr);
putc('\n', stderr);
#endif
if (ret <= 0)
ret = ERRRDHD;
else if (sscanf(header, "HTTP/1.%*d %03d", (int *)&ret) != 1)
ret = ERRPAHD;
else if (mode == KEEP_OPEN)
return ret;
}
}
/* close socket */
close(s);
return ret;
}
/*
* Put data on the server
*
* This function sends data to the http data server.
* The data will be stored under the ressource name filename.
* returns a negative error code or a positive code from the server
*
* limitations: filename is truncated to first 256 characters
* and type to 64.
*/
http_retcode http_put(
filename, data, length, overwrite,
type) char *filename; /* name of the ressource to create */
char *data; /* pointer to the data to send */
int length; /* length of the data to send */
int overwrite; /* flag to request to overwrite the ressource if it
was already existing */
char *type; /* type of the data, if NULL default type is used */
{
char header[MAXBUF];
if (type)
sprintf(header, "Content-length: %d\015\012Content-type: %.64s\015\012%s",
length, type, overwrite ? "Control: overwrite=1\015\012" : "");
else
sprintf(header, "Content-length: %d\015\012%s", length,
overwrite ? "Control: overwrite=1\015\012" : "");
return http_query("PUT", filename, header, CLOSE, data, length, NULL);
}
/*
* Get data from the server
*
* This function gets data from the http data server.
* The data is read from the ressource named filename.
* Address of new new allocated memory block is filled in pdata
* whose length is returned via plength.
*
* returns a negative error code or a positive code from the server
*
*
* limitations: filename is truncated to first 256 characters
*/
http_retcode http_get(
filename, pdata, plength,
typebuf) char *filename; /* name of the ressource to read */
char **pdata; /* address of a pointer variable which will be set
to point toward allocated memory containing read data.*/
int *plength; /* address of integer variable which will be set to
length of the read data */
char *typebuf; /* allocated buffer where the read data type is returned.
If NULL, the type is not returned */
{
http_retcode ret;
char header[MAXBUF];
char *pc;
int fd;
int n, length = -1;
if (!pdata)
return ERRNULL;
else
*pdata = NULL;
if (plength)
*plength = 0;
if (typebuf)
*typebuf = '\0';
ret = http_query("GET", filename, "", KEEP_OPEN, NULL, 0, &fd);
if (ret == 200) {
while (1) {
n = http_read_line(fd, header, MAXBUF - 1);
#ifdef VERBOSE
fputs(header, stderr);
putc('\n', stderr);
#endif
if (n <= 0) {
close(fd);
return ERRRDHD;
}
/* empty line ? (=> end of header) */
if (n > 0 && (*header) == '\0')
break;
/* try to parse some keywords : */
/* convert to lower case 'till a : is found or end of string */
for (pc = header; (*pc != ':' && *pc); pc++)
*pc = tolower(*pc);
sscanf(header, "content-length: %d", &length);
if (typebuf)
sscanf(header, "content-type: %s", typebuf);
}
if (length < 0) {
close(fd);
return ERRNOLG;
}
if (plength)
*plength = length;
if (!(*pdata = malloc(length))) {
close(fd);
return ERRMEM;
}
n = http_read_buffer(fd, *pdata, length);
close(fd);
if (n != length)
ret = ERRRDDT;
} else if (ret >= 0)
close(fd);
return ret;
}
/*
* Request the header
*
* This function outputs the header of thehttp data server.
* The header is from the ressource named filename.
* The length and type of data is eventually returned (like for http_get(3))
*
* returns a negative error code or a positive code from the server
*
* limitations: filename is truncated to first 256 characters
*/
http_retcode http_head(
filename, plength,
typebuf) char *filename; /* name of the ressource to read */
int *plength; /* address of integer variable which will be set to
length of the data */
char *typebuf; /* allocated buffer where the data type is returned.
If NULL, the type is not returned */
{
/* mostly copied from http_get : */
http_retcode ret;
char header[MAXBUF];
char *pc;
int fd;
int n, length = -1;
if (plength)
*plength = 0;
if (typebuf)
*typebuf = '\0';
ret = http_query("HEAD", filename, "", KEEP_OPEN, NULL, 0, &fd);
if (ret == 200) {
while (1) {
n = http_read_line(fd, header, MAXBUF - 1);
#ifdef VERBOSE
fputs(header, stderr);
putc('\n', stderr);
#endif
if (n <= 0) {
close(fd);
return ERRRDHD;
}
/* empty line ? (=> end of header) */
if (n > 0 && (*header) == '\0')
break;
/* try to parse some keywords : */
/* convert to lower case 'till a : is found or end of string */
for (pc = header; (*pc != ':' && *pc); pc++)
*pc = tolower(*pc);
sscanf(header, "content-length: %d", &length);
if (typebuf)
sscanf(header, "content-type: %s", typebuf);
}
if (plength)
*plength = length;
close(fd);
} else if (ret >= 0)
close(fd);
return ret;
}
/*
* Delete data on the server
*
* This function request a DELETE on the http data server.
*
* returns a negative error code or a positive code from the server
*
* limitations: filename is truncated to first 256 characters
*/
http_retcode http_delete(
filename) char *filename; /* name of the ressource to create */
{ return http_query("DELETE", filename, "", CLOSE, NULL, 0, NULL); }
/* parses an url : setting the http_server and http_port global variables
* and returning the filename to pass to http_get/put/...
* returns a negative error code or 0 if sucessfully parsed.
*/
http_retcode http_parse_url(url, pfilename)
/* writeable copy of an url */
char *url;
/* address of a pointer that will be filled with allocated filename
* the pointer must be equal to NULL before calling or it will be
* automatically freed (free(3))
*/
char **pfilename;
{
char *pc, c;
http_port = 80;
if (http_server) {
free(http_server);
http_server = NULL;
}
if (*pfilename) {
free(*pfilename);
*pfilename = NULL;
}
if (strncasecmp("http://", url, 7)) {
#ifdef VERBOSE
fprintf(stderr, "invalid url (must start with 'http://')\n");
#endif
return ERRURLH;
}
url += 7;
for (pc = url, c = *pc; (c && c != ':' && c != '/');)
c = *pc++;
*(pc - 1) = 0;
if (c == ':') {
if (sscanf(pc, "%d", &http_port) != 1) {
#ifdef VERBOSE
fprintf(stderr, "invalid port in url\n");
#endif
return ERRURLP;
}
for (pc++; (*pc && *pc != '/'); pc++)
;
if (*pc)
pc++;
}
http_server = strdup(url);
*pfilename = strdup(c ? pc : "");
#ifdef VERBOSE
fprintf(stderr, "host=(%s), port=%d, filename=(%s)\n", http_server, http_port,
*pfilename);
#endif
return OK0;
}