1 |
/* wptHTTP.cpp - Generic HTTP support |
/* wptHTTP.cpp - Generic HTTP support |
2 |
* Copyright (C) 2004, 2005, 2006 Timo Schulz |
* Copyright (C) 2004, 2005, 2006, 2007 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
* General Public License for more details. |
* General Public License for more details. |
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
15 |
*/ |
*/ |
16 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
#include <config.h> |
54 |
wHTTP::wHTTP (const char *_url) |
wHTTP::wHTTP (const char *_url) |
55 |
{ |
{ |
56 |
reset (); |
reset (); |
57 |
extractHostInfo (_url, &this->host, &this->url); |
open (_url); |
58 |
/* XXX: if the connection fails, return the error code to the user. */ |
} |
59 |
|
|
60 |
|
|
61 |
|
/* Open a connection to the given URL. */ |
62 |
|
int |
63 |
|
wHTTP::open (const char *_url) |
64 |
|
{ |
65 |
|
/* In case the function is used more than once, we |
66 |
|
need to release the old structures first. */ |
67 |
|
releaseHeaders (); |
68 |
|
|
69 |
|
safe_free (this->host); |
70 |
|
safe_free (this->url); |
71 |
|
|
72 |
|
if (extractHostInfo (_url, &this->host, &this->url)) |
73 |
|
return -1; |
74 |
|
|
75 |
if (!sendRequest (this->host, this->port, this->url)) |
if (!sendRequest (this->host, this->port, this->url)) |
76 |
parseResponse (&statcode); |
return parseResponse (&statcode); |
77 |
|
|
78 |
|
return 0; |
79 |
} |
} |
80 |
|
|
81 |
|
|
98 |
int |
int |
99 |
wHTTP::head (const char *_url) |
wHTTP::head (const char *_url) |
100 |
{ |
{ |
101 |
|
int err; |
102 |
|
|
103 |
safe_free (host); |
safe_free (host); |
104 |
safe_free (url); |
safe_free (url); |
105 |
|
|
106 |
extractHostInfo (_url, &this->host, &this->url); |
err = extractHostInfo (_url, &this->host, &this->url); |
107 |
|
if (err) |
108 |
|
return err; |
109 |
|
|
110 |
method = HTTP_HEAD; |
method = HTTP_HEAD; |
111 |
|
if (!sendRequest (this->host, this->port, this->url)) |
|
if (!sendRequest (host, port, url)) |
|
112 |
parseResponse (&statcode); |
parseResponse (&statcode); |
113 |
|
|
114 |
return 0; |
return 0; |
119 |
int |
int |
120 |
wHTTP::get (const char *_url) |
wHTTP::get (const char *_url) |
121 |
{ |
{ |
122 |
safe_free (host); |
int err; |
123 |
safe_free (url); |
|
124 |
|
safe_free (this->host); |
125 |
|
safe_free (this->url); |
126 |
|
|
127 |
extractHostInfo (_url, &this->host, &this->url); |
err = extractHostInfo (_url, &this->host, &this->url); |
128 |
|
if (err) |
129 |
|
return err; |
130 |
|
|
131 |
method = HTTP_GET; |
method = HTTP_GET; |
132 |
if (!sendRequest (this->host, this->port, this->url)) |
if (!sendRequest (this->host, this->port, this->url)) |
332 |
@host and also return the resource part of the url in @new_url. */ |
@host and also return the resource part of the url in @new_url. */ |
333 |
int |
int |
334 |
wHTTP::extractHostInfo (const char *_url, char **_host, char **new_url) |
wHTTP::extractHostInfo (const char *_url, char **_host, char **new_url) |
335 |
{ |
{ |
336 |
|
char tmpbuf[2*MAX_PATH+1]; |
337 |
char *p; |
char *p; |
|
char tmpbuf[512]; |
|
338 |
|
|
339 |
*_host = NULL; |
*_host = NULL; |
340 |
*new_url = NULL; |
*new_url = NULL; |
341 |
|
|
342 |
/* XXX: do not use static buffers. */ |
/* XXX: do not use static buffers. */ |
343 |
memset (tmpbuf, 0, sizeof (tmpbuf)); |
memset (tmpbuf, 0, sizeof (tmpbuf)); |
344 |
strncpy (tmpbuf, _url, sizeof (tmpbuf)-1); |
strncpy (tmpbuf, _url, DIM (tmpbuf)-1); |
345 |
|
|
346 |
p = "http://"; |
p = "http://"; |
347 |
if (strlen (_url) < 10 || strncmp (_url, p, 7)) |
if (strlen (_url) < 10 || strncmp (_url, p, 7)) |
350 |
p = strtok (tmpbuf+7, "/"); |
p = strtok (tmpbuf+7, "/"); |
351 |
if (!p) |
if (!p) |
352 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
353 |
*_host = strdup (p); |
*_host = strdup (p); |
354 |
p = strchr (_url, '/'); |
p = strchr (_url, '/'); |
355 |
if (!p) { |
if (!p) /* document were given so we assume the root '/'. */ |
356 |
safe_free (*_host); |
*new_url = strdup ("/"); |
357 |
return WPTERR_GENERAL; |
else |
358 |
} |
*new_url = strdup (p); |
|
*new_url = strdup (p); |
|
359 |
return 0; |
return 0; |
360 |
} |
} |
361 |
|
|
455 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
456 |
|
|
457 |
do { |
do { |
458 |
rc = readLine (buf, 299, 1, &nn, NULL); |
rc = readLine (buf, DIM (buf)-1, 1, &nn, NULL); |
459 |
if (rc) |
if (rc) |
460 |
return rc; |
return rc; |
461 |
if (nn == 2) |
if (nn == 2) |
504 |
} |
} |
505 |
|
|
506 |
do { |
do { |
507 |
rc = readLine (buf, sizeof (buf)-1, 1, &n, &eof); |
rc = readLine (buf, DIM (buf)-1, 1, &n, &eof); |
508 |
if (rc) |
if (rc) |
509 |
return rc; |
return rc; |
510 |
if (n > 0) |
if (n > 0) |
557 |
resp_headers = n; |
resp_headers = n; |
558 |
} |
} |
559 |
resp_headers = NULL; |
resp_headers = NULL; |
560 |
rc = parseHeaders (&resp_headers); |
return parseHeaders (&resp_headers); |
|
if (rc) |
|
|
return rc; |
|
|
return 0; |
|
561 |
} |
} |
562 |
|
|
563 |
|
|
564 |
/* Destroy HTTP object. */ |
/* Release all used structures. */ |
565 |
wHTTP::~wHTTP (void) |
void |
566 |
|
wHTTP::releaseHeaders (void) |
567 |
{ |
{ |
568 |
http_head_t h; |
http_head_t h; |
569 |
|
|
570 |
while (resp_headers) { |
while (resp_headers != NULL) { |
571 |
h = resp_headers->next; |
h = resp_headers->next; |
572 |
free (resp_headers); |
free (resp_headers); |
573 |
resp_headers = h; |
resp_headers = h; |
574 |
} |
} |
575 |
while (req_headers) { |
resp_headers = NULL; |
576 |
|
while (req_headers != NULL) { |
577 |
h = req_headers->next; |
h = req_headers->next; |
578 |
free (req_headers); |
free (req_headers); |
579 |
req_headers = h; |
req_headers = h; |
580 |
} |
} |
581 |
safe_free (url); |
req_headers = NULL; |
582 |
|
} |
583 |
|
|
584 |
|
|
585 |
|
/* Destroy HTTP object. */ |
586 |
|
wHTTP::~wHTTP (void) |
587 |
|
{ |
588 |
|
releaseHeaders(); |
589 |
safe_free (host); |
safe_free (host); |
590 |
|
safe_free (url); |
591 |
if (fd != 0) { |
if (fd != 0) { |
592 |
closesocket (fd); |
closesocket (fd); |
593 |
fd = 0; |
fd = 0; |