-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_connect.c
258 lines (207 loc) · 4.67 KB
/
http_connect.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<netdb.h>
#include<ctype.h>
#include<unistd.h>
#include<errno.h>
#include"cgiaudit.h"
void ca_putstat(const char*);
/*
* Connect to an HTTP server and fill in the info structure after parsing the
* header while handling redirects.
*/
/* static char*url;
static void set_new_url(char*newurl)
{
url=malloc(strlen(newurl)+1);
if(!url)
vexit("malloc");
strcpy(url,newurl);
}
char*get_new_url(void)
{
return url;
} */
FILE*http_connect(URLINFO*url,HTTPSRVINFO*httpsrvinfo)
{
int sock,ehr=0;
struct hostent*he;
struct sockaddr_in sain;
static char*hexbuf=NULL;
static int first_time=1;
char*qryptr,buf[65536];
register char*p,*p2;
FILE*so;
if(!url||!url->host)
{
putchar('\n');
ca_putstat("Invalid URL (did you include \"http://\"?)\n");
return NULL;
}
he=gethostbyname(options.p?options.p:url->host);
if(!he)
{
printf("\n[%d.%d] Unable to resolve hostname: %s\n",i,n,url->host);
return NULL;
}
if(first_time&&options.s)
{
int x;
x=atoi(options.s);
setmask(x,(struct in_addr*)he->h_addr);
first_time=!first_time;
}
sock=socket(PF_INET,SOCK_STREAM,0);
if(sock<0)
vexit("socket");
sain.sin_family=AF_INET;
sain.sin_port=htons(options.t?options.t:url->port);
sain.sin_addr=*((struct in_addr*)he->h_addr);
if(connect(sock,(struct sockaddr*)&sain,sizeof(sain))<0)
vexit("connect");
/* Both CR and LF are needed by the HTTP protocol. */
if(options.x&&url->query)
{
if(!hexbuf)
{
hexbuf=malloc(HEX_BUF_SIZE);
if(!hexbuf)
vexit("malloc");
}
hexencode(url->query,hexbuf,HEX_BUF_SIZE);
qryptr=hexbuf;
}
else
{
qryptr=url->query;
}
/* word to optyx and iis-zang2.c proxy technique */
if(options.p)
{
sprintf(buf,"GET http://%s:%d/%s%s%s HTTP/1.0\r\n",url->host,url->port,url->path[0]=='/'?url->path+1:url->path,qryptr?"?":"",qryptr?qryptr:"");
}
else
{
sprintf(buf,"GET %s%s%s HTTP/1.0\r\n",url->path,qryptr?"?":"",qryptr?qryptr:"");
}
/* I'm not going to rely on the return value of sprintf() */
p=buf;
p+=strlen(p);
if(options.r)
{
printf("[%d.%d] --+-- BEGIN HTTP REQUEST --+--\n",i,n);
}
strcpy(p,"Host: ");
p+=strlen(p);
strcpy(p,url->host);
p+=strlen(p);
strcpy(p,"\r\n");
p+=strlen(p);
strcpy(p,"User-Agent: ");
p+=strlen(p);
strcpy(p,HTTP_USER_AGENT);
p+=strlen(p);
if(options.r)
{
puts(buf);
printf("[%d.%d] --+-- END HTTP REQUEST --+--\n",i,n);
}
strcpy(p,"\r\n");
p+=strlen(p);
write(sock,buf,p-buf);
write(sock,"\r\n",2);
so=fdopen(sock,"r");
if(!so)
vexit("fdopen");
if(options.r)
printf("[%d.%d] --+-- BEGIN HTTP REPLY --+--\n",i,n);
while(fgets(buf,sizeof(buf),so))
{
for(p=buf;*p&&isspace(*p);p++);
/*
* If the line is blank, it marks the end of the HTTP header.
* Let the calling function handle the document that follows.
*/
if(!*p)
break;
if(options.r)
fputs(buf,stdout);
if(!strncasecmp(p,"HTTP/",5))
{
p+=6;
for(p2=p;*p2&&!isspace(*p2);p2++);
*p2=0;
httpsrvinfo->ver=malloc(p2-p+1);
if(!httpsrvinfo->ver)
vexit("malloc");
strcpy(httpsrvinfo->ver,p);
p2++;
strncpy(httpsrvinfo->code,p2,3);
*(httpsrvinfo->code+3)=0;
p2+=4;
for(p=p2;*p2;p2++)switch(*p2){case '\r':case'\n':*p2=0;}
httpsrvinfo->phrase=malloc(strlen(p)+1);
strcpy(httpsrvinfo->phrase,p);
}
if(!strncasecmp(p,"Server:",7))
{
p+=8;
while(isspace(*p))
p++;
for(p2=p;*p2&&*p2!='\r'&&*p2!='\n';p2++);
*p2=0;
httpsrvinfo->server=malloc(p2-p+1);
strcpy(httpsrvinfo->server,p);
}
if(!strncasecmp(p,"Content-length:",15))
{
p+=15;
while(isspace(*p))
p++;
for(p2=p;*p2&&!isspace(*p2);p2++);
*p2=0;
httpsrvinfo->content_length=strtol(p,(char**)NULL,10);
if(errno==ERANGE)
vexit("strtol");
if(httpsrvinfo->content_length<0)
{
fprintf(stderr,"ERROR: HTTP Content-Length is negative!\n");
exit(EXIT_FAILURE);
}
}
if(!strncasecmp(p,"Location:",9))
{
URLINFO*urlarg;
p+=9;
while(isspace(*p))
p++;
for(p2=p;*p2&&!isspace(*p2);p2++);
*p2=0;
/* First level of verbosity */
fprintf(stderr,"[%d.%d] Server redirect: %s\n",i,n,p);
urlarg=parse_url(p);
if(!urlarg||!urlarg->host)
{
fprintf(stderr,"[%d.%d] ERROR: Cannot parse URL from Location field in HTTP header: %s\n",i,n,*p?p:"null");
exit(EXIT_FAILURE);
}
url->host=urlarg->host;
/* set_new_url(p); */
if(options.r)
{
printf("[%d.%d] --+-- END HTTP REPLY --+--\n",i,n);
ehr=1;
}
return http_connect(urlarg,httpsrvinfo);
}
}
if(options.r&&!ehr)
{
printf("[%d.%d] --+-- END HTTP REPLY --+--\n",i,n);
}
return so;
}