Debian Codegraph @

Contrib/NSISdl/httpget.cpp

raw
1 /*
2 ** JNetLib
3 ** Copyright (C) 2000-2001 Nullsoft, Inc.
4 ** Author: Justin Frankel
5 ** File: httpget.cpp - JNL HTTP GET implementation
6 ** License: see License.txt
7 **
8 ** Unicode support by Jim Park -- 08/24/2007
9 */
10
11 #include "netinc.h"
12 #include "util.h"
13 #include "httpget.h"
14
15 static const char g_errstr_oom[] = "error allocating memory";
16
17 template<class T> static int classify_scheme_char(T c)
18 {
19 if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')) return 0;
20 return c == '+' || c == '-' || c == '.' ? 1 : -1;
21 }
22
23 template<class T> static unsigned int get_scheme_length(T*s)
24 {
25 if (classify_scheme_char(s[0]) != 0) return 0; // The first character MUST be a-z||A-Z. (RFC 3986 3.1)
26 for (unsigned int i=0;;)
27 {
28 if (s[++i] == ':') return ++i; // Length including ':'
29 if (classify_scheme_char(s[i]) < 0) return 0;
30 }
31 }
32
33 enum { GLT_ABS=0, GLT_NETREL, GLT_ROOTREL, GLT_PATHREL };
34 template<class T> static unsigned int get_location_type(T*s)
35 {
36 unsigned int len=get_scheme_length(s);
37 if (len) return GLT_ABS; // "xyz:..."
38 s+=len;
39 // Note: The authority section is not skipped here because it is assumed to not exist without a scheme!
40 if (s[0] != '/') return GLT_PATHREL; // "xyz..." or "./xyz..." (or "" but don't rely on this)
41 if (s[1] == '/') return GLT_NETREL; // "//..." (relative "network-path reference")
42 return GLT_ROOTREL; // "/" or "/xyz..."
43 }
44
45 JNL_HTTPGet::JNL_HTTPGet(JNL_AsyncDNS *dns, int recvbufsize, char *proxy)
46 {
47 m_recvbufsize=recvbufsize;
48 m_dns=dns;
49 m_con=NULL;
50 m_http_proxylpinfo=0;
51 m_http_proxyhost=0;
52 m_http_proxyport=0;
53 if (proxy && *proxy)
54 {
55 char *p=(char*)malloc(strlen(proxy)+1);
56 if (p)
57 {
58 char *r=NULL;
59 strcpy(p,proxy);
60 do_parse_url(p,&m_http_proxyhost,&m_http_proxyport,&r,&m_http_proxylpinfo);
61 free(r);
62 free(p);
63 }
64 }
65 m_sendheaders=NULL;
66 reinit();
67 }
68
69 void JNL_HTTPGet::reinit()
70 {
71 m_errstr=0;
72 m_recvheaders=NULL;
73 m_recvheaders_size=0;
74 m_http_state=0;
75 m_http_port=0;
76 m_http_url=0;
77 m_reply=0;
78 m_http_host=m_http_lpinfo=m_http_request=NULL;
79 }
80
81 void JNL_HTTPGet::deinit()
82 {
83 delete m_con;
84 free(m_recvheaders);
85
86 free(m_http_url);
87 free(m_http_host);
88 free(m_http_lpinfo);
89 free(m_http_request);
90 free(m_errstr);
91 free(m_reply);
92 reinit();
93 }
94
95 JNL_HTTPGet::~JNL_HTTPGet()
96 {
97 deinit();
98 free(m_sendheaders);
99 free(m_http_proxylpinfo);
100 free(m_http_proxyhost);
101
102 }
103
104
105 void JNL_HTTPGet::addheader(const char *header)
106 {
107 //if (strstr(header,"\r") || strstr(header,"\n")) return;
108 if (!m_sendheaders)
109 {
110 m_sendheaders=(char*)malloc(strlen(header)+3);
111 if (m_sendheaders)
112 {
113 strcpy(m_sendheaders,header);
114 strcat(m_sendheaders,"\r\n");
115 }
116 }
117 else
118 {
119 char *t=(char*)malloc(strlen(header)+strlen(m_sendheaders)+1+2);
120 if (t)
121 {
122 strcpy(t,m_sendheaders);
123 strcat(t,header);
124 strcat(t,"\r\n");
125 free(m_sendheaders);
126 m_sendheaders=t;
127 }
128 }
129 }
130
131 void JNL_HTTPGet::do_encode_mimestr(char *in, char *out)
132 {
133 char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
134 int shift = 0;
135 int accum = 0;
136
137 while (*in)
138 {
139 if (*in)
140 {
141 accum <<= 8;
142 shift += 8;
143 accum |= *in++;
144 }
145 while ( shift >= 6 )
146 {
147 shift -= 6;
148 *out++ = alphabet[(accum >> shift) & 0x3F];
149 }
150 }
151 if (shift == 4)
152 {
153 *out++ = alphabet[(accum & 0xF)<<2];
154 *out++='=';
155 }
156 else if (shift == 2)
157 {
158 *out++ = alphabet[(accum & 0x3)<<4];
159 *out++='=';
160 *out++='=';
161 }
162
163 *out++=0;
164 }
165
166
167 void JNL_HTTPGet::connect(const char *url)
168 {
169 deinit();
170 m_http_url=(char*)malloc(strlen(url)+1);
171 strcpy(m_http_url,url);
172 do_parse_url(m_http_url,&m_http_host,&m_http_port,&m_http_request, &m_http_lpinfo);
173 strcpy(m_http_url,url);
174 if (!m_http_host || !m_http_host[0] || !m_http_port)
175 {
176 m_http_state=-1;
177 seterrstr("invalid URL");
178 return;
179 }
180
181 int sendbufferlen=0;
182
183 if (!m_http_proxyhost || !m_http_proxyhost[0])
184 {
185 sendbufferlen += 4 /* GET */ + strlen(m_http_request) + 9 /* HTTP/1.0 */ + 2;
186 }
187 else
188 {
189 sendbufferlen += 4 /* GET */ + strlen(m_http_url) + 9 /* HTTP/1.0 */ + 2;
190 if (m_http_proxylpinfo&&m_http_proxylpinfo[0])
191 {
192 sendbufferlen+=58+strlen(m_http_proxylpinfo)*2; // being safe here
193 }
194 }
195 sendbufferlen += 5 /* Host: */ + strlen(m_http_host) + 2;
196
197 if (m_http_lpinfo&&m_http_lpinfo[0])
198 {
199 sendbufferlen+=46+strlen(m_http_lpinfo)*2; // being safe here
200 }
201
202 if (m_sendheaders) sendbufferlen+=strlen(m_sendheaders);
203
204 char *str=(char*)malloc(sendbufferlen+1024);
205 if (!str)
206 {
207 seterrstr(g_errstr_oom);
208 m_http_state=-1;
209 }
210
211 if (!m_http_proxyhost || !m_http_proxyhost[0])
212 {
213 wsprintfA(str,"GET %s HTTP/1.0\r\n",m_http_request);
214 }
215 else
216 {
217 wsprintfA(str,"GET %s HTTP/1.0\r\n",m_http_url);
218 }
219
220 wsprintfA(str+strlen(str),"Host: %s\r\n",m_http_host);
221
222 if (m_http_lpinfo&&m_http_lpinfo[0])
223 {
224 strcat(str,"Authorization: Basic ");
225 do_encode_mimestr(m_http_lpinfo,str+strlen(str));
226 strcat(str,"\r\n");
227 }
228 if (m_http_proxylpinfo&&m_http_proxylpinfo[0])
229 {
230 strcat(str,"Proxy-Authorization: Basic ");
231 do_encode_mimestr(m_http_proxylpinfo,str+strlen(str));
232 strcat(str,"\r\n");
233 }
234
235 if (m_sendheaders) strcat(str,m_sendheaders);
236 strcat(str,"\r\n");
237
238 int a=m_recvbufsize;
239 if (a < 4096) a=4096;
240 m_con=new JNL_Connection(m_dns,strlen(str)+4,a);
241 if (m_con)
242 {
243 if (!m_http_proxyhost || !m_http_proxyhost[0])
244 {
245 m_con->connect(m_http_host,m_http_port);
246 }
247 else
248 {
249 m_con->connect(m_http_proxyhost,m_http_proxyport);
250 }
251 m_con->send_string(str);
252 }
253 else
254 {
255 m_http_state=-1;
256 seterrstr("could not create connection object");
257 }
258 free(str);
259
260 }
261
262 static int my_strnicmp(char *b1, const char *b2, int l)
263 {
264 while (l-- && *b1 && *b2)
265 {
266 char bb1=*b1++;
267 char bb2=*b2++;
268 if (bb1>='a' && bb1 <= 'z') bb1+='A'-'a';
269 if (bb2>='a' && bb2 <= 'z') bb2+='A'-'a';
270 if (bb1 != bb2) return bb1-bb2;
271 }
272 return 0;
273 }
274
275 char *_strstr(const char *i, const char *s)
276 {
277 if (strlen(i)>=strlen(s)) while (i[strlen(s)-1])
278 {
279 int l=strlen(s)+1;
280 const char *ii=i;
281 const char *is=s;
282 while (--l>0)
283 {
284 if (*ii != *is) break;
285 ii++;
286 is++;
287 }
288 if (l==0) return const_cast<char*>(i);
289 i++;
290 }
291 return NULL;
292 }
293
294 #define strstr _strstr
295
296 void JNL_HTTPGet::do_parse_url(char *url, char **host, int *port, char **req, char **lp)
297 {
298 char *p,*np;
299 free(*host); *host=0;
300 free(*req); *req=0;
301 free(*lp); *lp=0;
302
303 if (strstr(url,"://")) np=p=strstr(url,"://")+3;
304 else np=p=url;
305 while (*np != '/' && *np) np++;
306 if (*np)
307 {
308 *req=(char*)malloc(strlen(np)+1);
309 if (*req) strcpy(*req,np);
310 *np++=0;
311 }
312 else
313 {
314 *req=(char*)malloc(2);
315 if (*req) strcpy(*req,"/");
316 }
317
318 np=p;
319 while (*np != '@' && *np) np++;
320 if (*np)
321 {
322 *np++=0;
323 *lp=(char*)malloc(strlen(p)+1);
324 if (*lp) strcpy(*lp,p);
325 p=np;
326 }
327 else
328 {
329 *lp=(char*)malloc(1);
330 if (*lp) strcpy(*lp,"");
331 }
332 np=p;
333 while (*np != ':' && *np) np++;
334 if (*np)
335 {
336 *np++=0;
337 *port=my_atoi(np);
338 } else *port=80;
339 *host=(char*)malloc(strlen(p)+1);
340 if (*host) strcpy(*host,p);
341 }
342
343
344 const char *JNL_HTTPGet::getallheaders()
345 { // double null terminated, null delimited list
346 if (m_recvheaders) return m_recvheaders;
347 else return "\0\0";
348 }
349
350 const char *JNL_HTTPGet::getheader(const char *headername)
351 {
352 char *ret=NULL;
353 if (strlen(headername)<1||!m_recvheaders) return NULL;
354 char *p=m_recvheaders;
355 while (*p)
356 {
357 if (!my_strnicmp(p,headername,strlen(headername)))
358 {
359 ret=p+strlen(headername);
360 while (*ret == ' ') ret++;
361 break;
362 }
363 p+=strlen(p)+1;
364 }
365 return ret;
366 }
367
368 int JNL_HTTPGet::run()
369 {
370 int cnt=0;
371 if (m_http_state==-1||!m_con) return -1; // error
372
373
374 run_again:
375 static char main_buf[4096];
376 char *buf = main_buf;
377 m_con->run();
378
379 if (m_con->get_state()==JNL_Connection::STATE_ERROR)
380 {
381 seterrstr(m_con->get_errstr());
382 return -1;
383 }
384 if (m_con->get_state()==JNL_Connection::STATE_CLOSED) return 1;
385
386 if (m_http_state==0) // connected, waiting for reply
387 {
388 if (m_con->recv_lines_available()>0)
389 {
390 m_con->recv_line(buf,4095);
391 buf[4095]=0;
392 m_reply=(char*)malloc(strlen(buf)+1);
393 if (!m_reply)
394 {
395 seterrstr(g_errstr_oom);
396 return m_http_state=-1;
397 }
398 strcpy(m_reply,buf);
399
400 if (strstr(buf,"200")) m_http_state=2; // proceed to read headers normally
401 else if (strstr(buf,"301") || strstr(buf,"302"))
402 {
403 m_http_state=1; // redirect city
404 }
405 else
406 {
407 seterrstr(buf);
408 m_http_state=-1;
409 return -1;
410 }
411 cnt=0;
412 }
413 else if (!cnt++) goto run_again;
414 }
415 if (m_http_state == 1) // redirect
416 {
417 while (m_con->recv_lines_available() > 0)
418 {
419 m_con->recv_line(buf,4096);
420 if (!buf[0])
421 {
422 m_http_state=-1;
423 return -1;
424 }
425 if (!my_strnicmp(buf,"Location:",9))
426 {
427 char *p=buf+9; while (*p== ' ') p++;
428 if (*p)
429 {
430 const char *orgloc=m_http_url, *e;
431 char *newloc=p, *newbuf=0;
432 if (my_strnicmp(newloc, "http://",7))
433 {
434 unsigned int lenorg=0, sep=0, lennew=0;
435 unsigned int newloctype=get_location_type(newloc);
436
437 for (e=orgloc, lenorg=7; e[lenorg]; ++lenorg) // This assumes the original URL starts with "http://"
438 {
439 if (e[lenorg] == '/' || e[lenorg] == '?' || e[lenorg] == '#') // Is it the end of the authority section?
440 break;
441 }
442
443 if (newloctype == GLT_ABS)
444 {
445 // A scheme we don't understand, let connect deal with it.
446 }
447 else
448 {
449 lennew=strlen(newloc);
450 if (newloctype == GLT_NETREL)
451 {
452 lenorg=sizeof("http:")-1; // The new location is "//...", we must prepend the original scheme.
453 }
454 else if (newloctype != GLT_ROOTREL)
455 {
456 ++sep; // We need a separator between the original path and the new path.
457 unsigned int lastsep=lenorg;
458 for (e=orgloc; e[lastsep]; ++lastsep)
459 {
460 if (e[lastsep] == '/')
461 lenorg = lastsep;
462 if (e[lastsep] == '?' || e[lastsep] == '#') // Is it the end of the path section?
463 break;
464 }
465 }
466
467 p=malloc(lenorg+sep+lennew+1);
468 if (!p)
469 {
470 seterrstr(g_errstr_oom);
471 return m_http_state=-1;
472 }
473 strncpy(p,orgloc,++lenorg), p[lenorg]='\0';
474 if (sep) strcat(p, "/");
475 strcat(p,newloc);
476 newbuf=newloc=p;
477 }
478 }
479
480 connect(newloc);
481 free(newbuf);
482 return 0;
483 }
484 }
485 }
486 }
487 if (m_http_state==2)
488 {
489 if (!cnt++ && m_con->recv_lines_available() < 1) goto run_again;
490 while (m_con->recv_lines_available() > 0)
491 {
492 m_con->recv_line(buf,4096);
493 if (!buf[0]) { m_http_state=3; break; }
494
495 char *h = buf;
496
497 // workaround for bug #1445735
498 //
499 // some proxies, like WinProxy, prefix headers with tabs
500 // or spaces. to make sure headers are detected properly,
501 // this removes up to 128 useless white spaces.
502
503 while ((h - buf < 128) && (*h == ' ' || *h == '\t')) h++;
504
505 if (!m_recvheaders)
506 {
507 m_recvheaders_size=strlen(h)+1;
508 m_recvheaders=(char*)malloc(m_recvheaders_size+1);
509 if (m_recvheaders)
510 {
511 strcpy(m_recvheaders,h);
512 m_recvheaders[m_recvheaders_size]=0;
513 }
514 }
515 else
516 {
517 int oldsize=m_recvheaders_size;
518 m_recvheaders_size+=strlen(h)+1;
519 char *n=(char*)malloc(m_recvheaders_size+1);
520 if (n)
521 {
522 memcpy(n,m_recvheaders,oldsize);
523 strcpy(n+oldsize,h);
524 n[m_recvheaders_size]=0;
525 free(m_recvheaders);
526 m_recvheaders=n;
527 }
528 }
529 }
530 }
531 if (m_http_state==3)
532 {
533 }
534 return 0;
535 }
536
537 int JNL_HTTPGet::get_status() // returns 0 if connecting, 1 if reading headers,
538 // 2 if reading content, -1 if error.
539 {
540 if (m_http_state < 0) return -1;
541 if (m_http_state < 2) return 0;
542 if (m_http_state == 2) return 1;
543 if (m_http_state == 3) return 2;
544 return -1;
545 }
546
547 int JNL_HTTPGet::getreplycode()// returns 0 if none yet, otherwise returns http reply code.
548 {
549 if (!m_reply) return 0;
550 char *p=m_reply;
551 while (*p && *p != ' ') p++; // skip over HTTP/x.x
552 if (!*p) return 0;
553 return my_atoi(++p);
554 }
555
556 int JNL_HTTPGet::bytes_available()
557 {
558 if (m_con && m_http_state==3) return m_con->recv_bytes_available();
559 return 0;
560 }
561 int JNL_HTTPGet::get_bytes(char *buf, int len)
562 {
563 if (m_con && m_http_state==3) return m_con->recv_bytes(buf,len);
564 return 0;
565 }
566 int JNL_HTTPGet::peek_bytes(char *buf, int len)
567 {
568 if (m_con && m_http_state==3) return m_con->peek_bytes(buf,len);
569 return 0;
570 }
571
572 __int64 JNL_HTTPGet::content_length()
573 {
574 const char *p=getheader("content-length:");
575 if (!p) return 0;
576 __int64 cl = myatoi64(p);
577 if (cl > 0) return cl;
578
579 // workaround for bug #1744091
580 // some buggy apache servers return negative values for sizes
581 // over 2gb - fix it for them
582 if (cl < 0)
583 return (__int64)((unsigned int)(cl));
584
585 return 0;
586 }