33 |
#include "wptTypes.h" |
#include "wptTypes.h" |
34 |
#include "wptErrors.h" |
#include "wptErrors.h" |
35 |
|
|
|
#define free_if_malloc(buf) if ((buf)) { free ((buf)); (buf) = NULL; } |
|
36 |
|
|
37 |
/* Empty constructur to allow advanced requests. */ |
/* Empty constructur to allow advanced requests. */ |
38 |
wHTTP::wHTTP (void) |
wHTTP::wHTTP (void) |
84 |
int |
int |
85 |
wHTTP::head (const char *_url) |
wHTTP::head (const char *_url) |
86 |
{ |
{ |
87 |
free_if_malloc (host); |
safe_free (host); |
88 |
free_if_malloc (url); |
safe_free (url); |
89 |
|
|
90 |
extractHostInfo (_url, &this->host, &this->url); |
extractHostInfo (_url, &this->host, &this->url); |
91 |
method = HTTP_HEAD; |
method = HTTP_HEAD; |
101 |
int |
int |
102 |
wHTTP::get (const char *_url) |
wHTTP::get (const char *_url) |
103 |
{ |
{ |
104 |
free_if_malloc (host); |
safe_free (host); |
105 |
free_if_alloc (url); |
safe_free (url); |
106 |
|
|
107 |
extractHostInfo (_url, &this->host, &this->url); |
extractHostInfo (_url, &this->host, &this->url); |
108 |
|
|
277 |
break; |
break; |
278 |
} |
} |
279 |
n = recv (fd, &c, 1, 0); |
n = recv (fd, &c, 1, 0); |
280 |
if (n == -1) |
if (n == -1) { |
281 |
|
this->error = (int)WSAGetLastError (); |
282 |
break; |
break; |
283 |
|
} |
284 |
if (n == 0 || nbuf == 0 || c == '\n') { |
if (n == 0 || nbuf == 0 || c == '\n') { |
285 |
if (n == 0) { |
if (n == 0) { |
286 |
if (eof) |
if (eof) |
331 |
*_host = strdup (p); |
*_host = strdup (p); |
332 |
p = strchr (_url, '/'); |
p = strchr (_url, '/'); |
333 |
if (!p) { |
if (!p) { |
334 |
free_if_malloc (*_host); |
safe_free (*_host); |
335 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
336 |
} |
} |
337 |
*new_url = strdup (p); |
*new_url = strdup (p); |
349 |
p = (char*) malloc (strlen (name)+strlen (val)+strlen (fmt)+4); |
p = (char*) malloc (strlen (name)+strlen (val)+strlen (fmt)+4); |
350 |
sprintf (p, fmt, name, val); |
sprintf (p, fmt, name, val); |
351 |
addHeader (&req_headers, p); |
addHeader (&req_headers, p); |
352 |
free_if_malloc (p); |
safe_free (p); |
353 |
|
|
354 |
return 0; |
return 0; |
355 |
} |
} |
417 |
} |
} |
418 |
strcat (p, "\r\n"); |
strcat (p, "\r\n"); |
419 |
n = send (fd, p, strlen (p), 0); |
n = send (fd, p, strlen (p), 0); |
420 |
free_if_malloc (p); |
safe_free (p); |
421 |
return n > 0? 0 : WPTERR_GENERAL; |
return n > 0? 0 : WPTERR_GENERAL; |
422 |
} |
} |
423 |
|
|
470 |
if (strnicmp (val, "text", 4)) { /* binary */ |
if (strnicmp (val, "text", 4)) { /* binary */ |
471 |
do { |
do { |
472 |
n = recv (fd, buf, nlen > 1024? 1024 : nlen, 0); |
n = recv (fd, buf, nlen > 1024? 1024 : nlen, 0); |
473 |
if (n == -1) |
if (n == -1) { |
474 |
|
this->error = (int)WSAGetLastError (); |
475 |
return SOCKET_ERROR; |
return SOCKET_ERROR; |
476 |
|
} |
477 |
if (n == 0) |
if (n == 0) |
478 |
break; |
break; |
479 |
nlen -= n; |
nlen -= n; |
483 |
} |
} |
484 |
|
|
485 |
do { |
do { |
486 |
rc = readLine (buf, 1024, 1, &n, &eof); |
rc = readLine (buf, sizeof (buf)-1, 1, &n, &eof); |
487 |
if (rc) |
if (rc) |
488 |
return rc; |
return rc; |
489 |
if (n > 0) |
if (n > 0) |
558 |
free (req_headers); |
free (req_headers); |
559 |
req_headers = h; |
req_headers = h; |
560 |
} |
} |
561 |
free_if_malloc (url); |
safe_free (url); |
562 |
free_if_malloc (host); |
safe_free (host); |
563 |
if (fd != 0) { |
if (fd != 0) { |
564 |
closesocket (fd); |
closesocket (fd); |
565 |
fd = 0; |
fd = 0; |
606 |
} |
} |
607 |
|
|
608 |
|
|
609 |
/* Return the winsock specific error code. */ |
/* Return the Winsock specific error code. */ |
610 |
int wHTTP::getErrorCode (void) |
int wHTTP::getErrorCode (void) |
611 |
{ |
{ |
612 |
return error; |
return error; |