summaryrefslogtreecommitdiff
path: root/nanohttp.c
diff options
context:
space:
mode:
Diffstat (limited to 'nanohttp.c')
-rw-r--r--nanohttp.c81
1 files changed, 46 insertions, 35 deletions
diff --git a/nanohttp.c b/nanohttp.c
index 2437fed..e109ad7 100644
--- a/nanohttp.c
+++ b/nanohttp.c
@@ -10,7 +10,7 @@
*
* daniel@veillard.com
*/
-
+
#define NEED_SOCKETS
#define IN_LIBXML
#include "libxml.h"
@@ -46,7 +46,7 @@
#include <resolv.h>
#endif
#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
+#include <fcntl.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
@@ -213,7 +213,7 @@ void
xmlNanoHTTPInit(void) {
const char *env;
#ifdef _WINSOCKAPI_
- WSADATA wsaData;
+ WSADATA wsaData;
#endif
if (initialized)
@@ -276,22 +276,24 @@ xmlNanoHTTPCleanup(void) {
static void
xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
xmlURIPtr uri;
+ int len;
+
/*
* Clear any existing data from the context
*/
- if (ctxt->protocol != NULL) {
+ if (ctxt->protocol != NULL) {
xmlFree(ctxt->protocol);
ctxt->protocol = NULL;
}
- if (ctxt->hostname != NULL) {
+ if (ctxt->hostname != NULL) {
xmlFree(ctxt->hostname);
ctxt->hostname = NULL;
}
- if (ctxt->path != NULL) {
+ if (ctxt->path != NULL) {
xmlFree(ctxt->path);
ctxt->path = NULL;
}
- if (ctxt->query != NULL) {
+ if (ctxt->query != NULL) {
xmlFree(ctxt->query);
ctxt->query = NULL;
}
@@ -305,9 +307,17 @@ xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
xmlFreeURI(uri);
return;
}
-
+
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)
ctxt->path = xmlMemStrdup(uri->path);
else
@@ -334,7 +344,7 @@ void
xmlNanoHTTPScanProxy(const char *URL) {
xmlURIPtr uri;
- if (proxy != NULL) {
+ if (proxy != NULL) {
xmlFree(proxy);
proxy = NULL;
}
@@ -358,7 +368,7 @@ xmlNanoHTTPScanProxy(const char *URL) {
xmlFreeURI(uri);
return;
}
-
+
proxy = xmlMemStrdup(uri->server);
if (uri->port != 0)
proxyPort = uri->port;
@@ -451,7 +461,7 @@ xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char *xmt_ptr, int outlen)
if ((ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL)) {
while (total_sent < outlen) {
- int nsent = send(ctxt->fd, xmt_ptr + total_sent,
+ int nsent = send(ctxt->fd, SEND_ARG2_CAST (xmt_ptr + total_sent),
outlen - total_sent, 0);
if (nsent > 0)
@@ -644,7 +654,7 @@ xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
char buf[4096];
char *bp = buf;
int rc;
-
+
while (bp - buf < 4095) {
if (ctxt->inrptr == ctxt->inptr) {
if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
@@ -781,9 +791,9 @@ xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
xmlFree(ctxt->location);
if (*cur == '/') {
xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
- xmlChar *tmp_loc =
+ xmlChar *tmp_loc =
xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
- ctxt->location =
+ ctxt->location =
(char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
} else {
ctxt->location = xmlMemStrdup(cur);
@@ -993,6 +1003,7 @@ xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
0) {
/* Solaris error code */
__xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
+ closesocket(s);
return INVALID_SOCKET;
}
#endif
@@ -1111,7 +1122,7 @@ xmlNanoHTTPConnectHost(const char *host, int port)
#endif
#if !defined(HAVE_GETADDRINFO) || !defined(_WIN32)
{
- h = gethostbyname (host);
+ h = gethostbyname (GETHOSTBYNAME_ARG_CAST host);
if (h == NULL) {
/*
@@ -1267,7 +1278,7 @@ xmlNanoHTTPRead(void *ctx, void *dest, int len) {
#ifdef HAVE_ZLIB_H
if (ctxt->usesGzip == 1) {
if (ctxt->strm == NULL) return(0);
-
+
ctxt->strm->next_out = dest;
ctxt->strm->avail_out = len;
ctxt->strm->avail_in = ctxt->inptr - ctxt->inrptr;
@@ -1346,23 +1357,23 @@ xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
#ifdef DEBUG_HTTP
int xmt_bytes;
#endif
-
+
if (URL == NULL) return(NULL);
if (method == NULL) method = "GET";
xmlNanoHTTPInit();
retry:
- if (redirURL == NULL)
+ if (redirURL == NULL) {
ctxt = xmlNanoHTTPNewCtxt(URL);
- else {
+ if (ctxt == NULL)
+ return(NULL);
+ } else {
ctxt = xmlNanoHTTPNewCtxt(redirURL);
+ if (ctxt == NULL)
+ return(NULL);
ctxt->location = xmlMemStrdup(redirURL);
}
- if ( ctxt == NULL ) {
- return ( NULL );
- }
-
if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
__xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
xmlNanoHTTPFreeCtxt(ctxt);
@@ -1427,13 +1438,13 @@ retry:
if (proxy) {
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,
- ctxt->port, ctxt->path );
+ ctxt->port, ctxt->path );
}
- else
+ else
p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
- ctxt->hostname, ctxt->path);
+ ctxt->hostname, ctxt->path);
}
else
p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
@@ -1442,7 +1453,7 @@ retry:
p += snprintf( p, blen - (p - bp), "?%s", ctxt->query);
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);
} else {
p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s:%d\r\n",
@@ -1453,7 +1464,7 @@ retry:
p += snprintf(p, blen - (p - bp), "Accept-Encoding: gzip\r\n");
#endif
- if (contentType != NULL && *contentType)
+ if (contentType != NULL && *contentType)
p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
if (headers != NULL)
@@ -1492,7 +1503,7 @@ retry:
if ( xmt_bytes != ilen )
xmlGenericError( xmlGenericErrorContext,
- "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
+ "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
xmt_bytes, ilen,
"bytes of HTTP content sent to host",
ctxt->hostname );
@@ -1620,7 +1631,7 @@ xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
ctxt = xmlNanoHTTPOpen(URL, contentType);
if (ctxt == NULL) return(-1);
- if (!strcmp(filename, "-"))
+ if (!strcmp(filename, "-"))
fd = 0;
else {
fd = open(filename, O_CREAT | O_WRONLY, 00644);
@@ -1666,7 +1677,7 @@ xmlNanoHTTPSave(void *ctxt, const char *filename) {
if ((ctxt == NULL) || (filename == NULL)) return(-1);
- if (!strcmp(filename, "-"))
+ if (!strcmp(filename, "-"))
fd = 0;
else {
fd = open(filename, O_CREAT | O_WRONLY, 0666);
@@ -1795,7 +1806,7 @@ xmlNanoHTTPMimeType( void * ctx ) {
* Check if all the content was read
*
* 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.
*/
static int
@@ -1849,7 +1860,7 @@ int main(int argc, char **argv) {
char *contentType = NULL;
if (argv[1] != NULL) {
- if (argv[2] != NULL)
+ if (argv[2] != NULL)
xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
else
xmlNanoHTTPFetch(argv[1], "-", &contentType);