-
Notifications
You must be signed in to change notification settings - Fork 3
/
http.c
142 lines (132 loc) · 3.53 KB
/
http.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
/*
* Http put/get/.. standalone program using Http put 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.c,v 1.4 1998/09/23 06:11:55 dl Exp $
*
* $Log: http.c,v $
* Revision 1.4 1998/09/23 06:11:55 dl
* one more lint
*
* Revision 1.3 1998/09/23 06:03:45 dl
* proxy support (old change which was not checked in back in 96)
* contact, etc.. infos update
*
* Revision 1.2 1996/04/18 13:52:14 dl
* strings.h->string.h
*
* Revision 1.1 1996/04/18 12:17:25 dl
* Initial revision
*
*/
static char *rcsid = "$Id: http.c,v 1.4 1998/09/23 06:11:55 dl Exp $";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include "http_lib.h"
int main(argc, argv) int argc;
char **argv;
{
int ret, lg, blocksize, r, i;
char typebuf[70];
char *data = NULL, *filename = NULL, *proxy = NULL;
enum { ERR, DOPUT, DOGET, DODEL, DOHEA } todo = ERR;
if (argc != 3) {
fprintf(stderr, "usage: http <cmd> <url>\n\tby <[email protected]>\n");
return 1;
}
i = 1;
if (!strcasecmp(argv[i], "put")) {
todo = DOPUT;
} else if (!strcasecmp(argv[i], "get")) {
todo = DOGET;
} else if (!strcasecmp(argv[i], "delete")) {
todo = DODEL;
} else if (!strcasecmp(argv[i], "head")) {
todo = DOHEA;
}
if (todo == ERR) {
fprintf(stderr,
"Invalid <cmd> '%s',\nmust be 'put', 'get', 'delete', or 'head'\n",
argv[i]);
return 2;
}
i++;
if ((proxy = getenv("http_proxy"))) {
ret = http_parse_url(proxy, &filename);
if (ret < 0)
return ret;
http_proxy_server = http_server;
http_server = NULL;
http_proxy_port = http_port;
}
ret = http_parse_url(argv[i], &filename);
if (ret < 0) {
if (proxy)
free(http_proxy_server);
return ret;
}
switch (todo) {
/* *** PUT *** */
case DOPUT:
fprintf(stderr, "reading stdin...\n");
/* read stdin into memory */
blocksize = 16384;
lg = 0;
if (!(data = malloc(blocksize))) {
return 3;
}
while (1) {
r = read(0, data + lg, blocksize - lg);
if (r <= 0)
break;
lg += r;
if ((3 * lg / 2) > blocksize) {
blocksize *= 4;
fprintf(stderr,
"read to date: %9d bytes, reallocating buffer to %9d\n", lg,
blocksize);
if (!(data = realloc(data, blocksize))) {
return 4;
}
}
}
fprintf(stderr, "read %d bytes\n", lg);
ret = http_put(filename, data, lg, 0, NULL);
fprintf(stderr, "res=%d\n", ret);
break;
/* *** GET *** */
case DOGET:
ret = http_get(filename, &data, &lg, typebuf);
fprintf(stderr, "res=%d,type='%s',lg=%d\n", ret, typebuf, lg);
fwrite(data, lg, 1, stdout);
break;
/* *** HEAD *** */
case DOHEA:
ret = http_head(filename, &lg, typebuf);
fprintf(stderr, "res=%d,type='%s',lg=%d\n", ret, typebuf, lg);
break;
/* *** DELETE *** */
case DODEL:
ret = http_delete(filename);
fprintf(stderr, "res=%d\n", ret);
break;
/* impossible... */
default:
fprintf(stderr, "impossible todo value=%d\n", todo);
return 5;
}
if (data)
free(data);
free(filename);
free(http_server);
if (proxy)
free(http_proxy_server);
return ((ret == 201) || (ret == 200)) ? 0 : ret;
}