-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhttp.c
176 lines (157 loc) · 3.63 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
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
/*
* Http put/get/post standalone program using Http put mini lib
* written by L. Demailly
* (c) 2013 Anibal Limon - [email protected]
* (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.5.x 2013/08/07 08:30:42 -0500
* Removed no used code for OS9, and code functions to access global
* variables now static instead extern.
* Author: alimon <[email protected]>
*
* Revisionq 1.5.x 2013/08/07 00:20:42 -0500
* Added support for POST
* Author: alimon <[email protected]>
*
* 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
*
*/
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "http_lib.h"
int main(int argc,char* argv[])
{
int ret,lg,blocksize,r,i;
char typebuf[70];
char *data=NULL,*filename=NULL,*proxy=NULL;
int data_len = 0;
char *type = NULL;
enum {
ERR,
DOPUT,
DOGET,
DODEL,
DOHEA,
DOPOST
} 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;
} else if (!strcasecmp(argv[i],"post")) {
todo=DOPOST;
}
if (todo==ERR) {
fprintf(stderr,
"Invalid <cmd> '%s',\nmust be "
"'put', 'get', 'post', 'delete', or 'head'\n",
argv[i]
);
return 2;
}
i++;
if ((proxy=getenv("http_proxy"))) {
ret=http_proxy_url(proxy);
if (ret<0) {
return ret;
}
}
ret=http_parse_url(argv[i],&filename);
if (ret<0) {
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);
fprintf(stderr, "%s\n", data);
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;
case DOPOST:
ret = http_post(filename, "your_name=1", 11, NULL, &data, &data_len, &type);
fprintf(stderr,"res=%d\n",ret);
fprintf(stderr,"%s\n", type);
fprintf(stderr,"data: %s\n", data);
break;
/* impossible... */
default:
fprintf(stderr,"impossible todo value=%d\n",todo);
return 5;
}
if (type) {
free(type);
}
if (data) {
free(data);
}
free(filename);
return ( (ret==201) || (ret==200) ) ? 0 : ret;
}