Diff for /embedaddon/libxml2/nanohttp.c between versions 1.1.1.2 and 1.1.1.3

version 1.1.1.2, 2013/07/22 01:22:19 version 1.1.1.3, 2014/06/15 19:53:29
Line 10 Line 10
  *   *
  * daniel@veillard.com   * daniel@veillard.com
  */   */
 
 #define NEED_SOCKETS  #define NEED_SOCKETS
 #define IN_LIBXML  #define IN_LIBXML
 #include "libxml.h"  #include "libxml.h"
Line 46 Line 46
 #include <resolv.h>  #include <resolv.h>
 #endif  #endif
 #ifdef HAVE_FCNTL_H  #ifdef HAVE_FCNTL_H
#include <fcntl.h> #include <fcntl.h>
 #endif  #endif
 #ifdef HAVE_ERRNO_H  #ifdef HAVE_ERRNO_H
 #include <errno.h>  #include <errno.h>
Line 213  void Line 213  void
 xmlNanoHTTPInit(void) {  xmlNanoHTTPInit(void) {
     const char *env;      const char *env;
 #ifdef _WINSOCKAPI_  #ifdef _WINSOCKAPI_
    WSADATA wsaData;        WSADATA wsaData;
 #endif  #endif
   
     if (initialized)      if (initialized)
Line 276  xmlNanoHTTPCleanup(void) { Line 276  xmlNanoHTTPCleanup(void) {
 static void  static void
 xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {  xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
     xmlURIPtr uri;      xmlURIPtr uri;
       int len;
   
     /*      /*
      * Clear any existing data from the context       * Clear any existing data from the context
      */       */
    if (ctxt->protocol != NULL) {     if (ctxt->protocol != NULL) {
         xmlFree(ctxt->protocol);          xmlFree(ctxt->protocol);
         ctxt->protocol = NULL;          ctxt->protocol = NULL;
     }      }
    if (ctxt->hostname != NULL) {     if (ctxt->hostname != NULL) {
         xmlFree(ctxt->hostname);          xmlFree(ctxt->hostname);
         ctxt->hostname = NULL;          ctxt->hostname = NULL;
     }      }
    if (ctxt->path != NULL) {     if (ctxt->path != NULL) {
         xmlFree(ctxt->path);          xmlFree(ctxt->path);
         ctxt->path = NULL;          ctxt->path = NULL;
     }      }
    if (ctxt->query != NULL) {     if (ctxt->query != NULL) {
         xmlFree(ctxt->query);          xmlFree(ctxt->query);
         ctxt->query = NULL;          ctxt->query = NULL;
     }      }
Line 305  xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char Line 307  xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char
         xmlFreeURI(uri);          xmlFreeURI(uri);
         return;          return;
     }      }
    
     ctxt->protocol = xmlMemStrdup(uri->scheme);      ctxt->protocol = xmlMemStrdup(uri->scheme);
    ctxt->hostname = xmlMemStrdup(uri->server);    /* special case of IPv6 addresses, the [] need to be removed */
     if ((uri->server != NULL) && (*uri->server == '[')) {
         len = strlen(uri->server);
         if ((len > 2) && (uri->server[len - 1] == ']')) {
             ctxt->hostname = (char *) xmlCharStrndup(uri->server + 1, len -2);
         } else
             ctxt->hostname = xmlMemStrdup(uri->server);
     } else
         ctxt->hostname = xmlMemStrdup(uri->server);
     if (uri->path != NULL)      if (uri->path != NULL)
         ctxt->path = xmlMemStrdup(uri->path);          ctxt->path = xmlMemStrdup(uri->path);
     else      else
Line 334  void Line 344  void
 xmlNanoHTTPScanProxy(const char *URL) {  xmlNanoHTTPScanProxy(const char *URL) {
     xmlURIPtr uri;      xmlURIPtr uri;
   
    if (proxy != NULL) {     if (proxy != NULL) {
         xmlFree(proxy);          xmlFree(proxy);
         proxy = NULL;          proxy = NULL;
     }      }
Line 358  xmlNanoHTTPScanProxy(const char *URL) { Line 368  xmlNanoHTTPScanProxy(const char *URL) {
             xmlFreeURI(uri);              xmlFreeURI(uri);
         return;          return;
     }      }
    
     proxy = xmlMemStrdup(uri->server);      proxy = xmlMemStrdup(uri->server);
     if (uri->port != 0)      if (uri->port != 0)
         proxyPort = uri->port;          proxyPort = uri->port;
Line 644  xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) { Line 654  xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
     char buf[4096];      char buf[4096];
     char *bp = buf;      char *bp = buf;
     int rc;      int rc;
    
     while (bp - buf < 4095) {      while (bp - buf < 4095) {
         if (ctxt->inrptr == ctxt->inptr) {          if (ctxt->inrptr == ctxt->inptr) {
             if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {              if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Line 781  xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const c Line 791  xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const c
             xmlFree(ctxt->location);              xmlFree(ctxt->location);
         if (*cur == '/') {          if (*cur == '/') {
             xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");              xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
            xmlChar *tmp_loc =             xmlChar *tmp_loc =
                 xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);                  xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
            ctxt->location =             ctxt->location =
                 (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);                  (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
         } else {          } else {
             ctxt->location = xmlMemStrdup(cur);              ctxt->location = xmlMemStrdup(cur);
Line 1267  xmlNanoHTTPRead(void *ctx, void *dest, int len) { Line 1277  xmlNanoHTTPRead(void *ctx, void *dest, int len) {
 #ifdef HAVE_ZLIB_H  #ifdef HAVE_ZLIB_H
     if (ctxt->usesGzip == 1) {      if (ctxt->usesGzip == 1) {
         if (ctxt->strm == NULL) return(0);          if (ctxt->strm == NULL) return(0);
 
         ctxt->strm->next_out = dest;          ctxt->strm->next_out = dest;
         ctxt->strm->avail_out = len;          ctxt->strm->avail_out = len;
         ctxt->strm->avail_in = ctxt->inptr - ctxt->inrptr;          ctxt->strm->avail_in = ctxt->inptr - ctxt->inrptr;
Line 1346  xmlNanoHTTPMethodRedir(const char *URL, const char *me Line 1356  xmlNanoHTTPMethodRedir(const char *URL, const char *me
 #ifdef DEBUG_HTTP  #ifdef DEBUG_HTTP
     int xmt_bytes;      int xmt_bytes;
 #endif  #endif
    
     if (URL == NULL) return(NULL);      if (URL == NULL) return(NULL);
     if (method == NULL) method = "GET";      if (method == NULL) method = "GET";
     xmlNanoHTTPInit();      xmlNanoHTTPInit();
Line 1427  retry: Line 1437  retry:
   
     if (proxy) {      if (proxy) {
         if (ctxt->port != 80) {          if (ctxt->port != 80) {
            p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",             p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
                         method, ctxt->hostname,                          method, ctxt->hostname,
                        ctxt->port, ctxt->path );                        ctxt->port, ctxt->path );
         }          }
        else         else
             p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,              p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
                        ctxt->hostname, ctxt->path);                        ctxt->hostname, ctxt->path);
     }      }
     else      else
         p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);          p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Line 1442  retry: Line 1452  retry:
         p += snprintf( p, blen - (p - bp), "?%s", ctxt->query);          p += snprintf( p, blen - (p - bp), "?%s", ctxt->query);
   
     if (ctxt->port == 80) {      if (ctxt->port == 80) {
        p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",         p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
                     ctxt->hostname);                      ctxt->hostname);
     } else {      } else {
         p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s:%d\r\n",          p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s:%d\r\n",
Line 1453  retry: Line 1463  retry:
     p += snprintf(p, blen - (p - bp), "Accept-Encoding: gzip\r\n");      p += snprintf(p, blen - (p - bp), "Accept-Encoding: gzip\r\n");
 #endif  #endif
   
    if (contentType != NULL && *contentType)     if (contentType != NULL && *contentType)
         p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);          p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
   
     if (headers != NULL)      if (headers != NULL)
Line 1492  retry: Line 1502  retry:
   
         if ( xmt_bytes != ilen )          if ( xmt_bytes != ilen )
             xmlGenericError( xmlGenericErrorContext,              xmlGenericError( xmlGenericErrorContext,
                        "xmlNanoHTTPMethodRedir:  Only %d of %d %s %s\n",                        "xmlNanoHTTPMethodRedir:  Only %d of %d %s %s\n",
                         xmt_bytes, ilen,                          xmt_bytes, ilen,
                         "bytes of HTTP content sent to host",                          "bytes of HTTP content sent to host",
                         ctxt->hostname );                          ctxt->hostname );
Line 1620  xmlNanoHTTPFetch(const char *URL, const char *filename Line 1630  xmlNanoHTTPFetch(const char *URL, const char *filename
     ctxt = xmlNanoHTTPOpen(URL, contentType);      ctxt = xmlNanoHTTPOpen(URL, contentType);
     if (ctxt == NULL) return(-1);      if (ctxt == NULL) return(-1);
   
    if (!strcmp(filename, "-"))     if (!strcmp(filename, "-"))
         fd = 0;          fd = 0;
     else {      else {
         fd = open(filename, O_CREAT | O_WRONLY, 00644);          fd = open(filename, O_CREAT | O_WRONLY, 00644);
Line 1666  xmlNanoHTTPSave(void *ctxt, const char *filename) { Line 1676  xmlNanoHTTPSave(void *ctxt, const char *filename) {
   
     if ((ctxt == NULL) || (filename == NULL)) return(-1);      if ((ctxt == NULL) || (filename == NULL)) return(-1);
   
    if (!strcmp(filename, "-"))     if (!strcmp(filename, "-"))
         fd = 0;          fd = 0;
     else {      else {
         fd = open(filename, O_CREAT | O_WRONLY, 0666);          fd = open(filename, O_CREAT | O_WRONLY, 0666);
Line 1795  xmlNanoHTTPMimeType( void * ctx ) { Line 1805  xmlNanoHTTPMimeType( void * ctx ) {
  * Check if all the content was read   * Check if all the content was read
  *   *
  * Returns 0 if all the content was read and available, returns   * Returns 0 if all the content was read and available, returns
 * -1 if received content length was less than specified or an error  * -1 if received content length was less than specified or an error
  * occurred.   * occurred.
  */   */
 static int  static int
Line 1849  int main(int argc, char **argv) { Line 1859  int main(int argc, char **argv) {
     char *contentType = NULL;      char *contentType = NULL;
   
     if (argv[1] != NULL) {      if (argv[1] != NULL) {
        if (argv[2] != NULL)         if (argv[2] != NULL)
             xmlNanoHTTPFetch(argv[1], argv[2], &contentType);              xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
         else          else
             xmlNanoHTTPFetch(argv[1], "-", &contentType);              xmlNanoHTTPFetch(argv[1], "-", &contentType);

Removed from v.1.1.1.2  
changed lines
  Added in v.1.1.1.3


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>