/[winpt]/trunk/Src/wptHTTP.cpp
ViewVC logotype

Diff of /trunk/Src/wptHTTP.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 259 by twoaday, Sun May 14 18:40:36 2006 UTC revision 260 by twoaday, Wed Aug 16 10:01:30 2006 UTC
# Line 33  Line 33 
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)
# Line 85  wHTTP::reset (void) Line 84  wHTTP::reset (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;
# Line 102  wHTTP::head (const char *_url) Line 101  wHTTP::head (const char *_url)
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    
# Line 278  wHTTP::readLine (char *buf, unsigned int Line 277  wHTTP::readLine (char *buf, unsigned int
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)
# Line 330  wHTTP::extractHostInfo (const char *_url Line 331  wHTTP::extractHostInfo (const char *_url
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);
# Line 348  wHTTP::addRequestHeader (const char *nam Line 349  wHTTP::addRequestHeader (const char *nam
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  }  }
# Line 416  wHTTP::sendRequest (const char *_host, i Line 417  wHTTP::sendRequest (const char *_host, i
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    
# Line 469  wHTTP::readData (FILE *out) Line 470  wHTTP::readData (FILE *out)
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;
# Line 480  wHTTP::readData (FILE *out) Line 483  wHTTP::readData (FILE *out)
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)
# Line 555  wHTTP::~wHTTP (void) Line 558  wHTTP::~wHTTP (void)
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;
# Line 603  wHTTP::write (const void *buf, unsigned Line 606  wHTTP::write (const void *buf, unsigned
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;

Legend:
Removed from v.259  
changed lines
  Added in v.260

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26