nanohttp.c revision 750a460541832b63ec5de36374a749782ea2c9bc
/*
* nanohttp.c: minimalist HTTP GET implementation to fetch external subsets.
* focuses on size, streamability, reentrancy and portability
*
* This is clearly not a general purpose HTTP implementation
* If you look for one, check:
*
* See Copyright for the status of this software.
*
* daniel@veillard.com
*/
#define NEED_SOCKETS
#define IN_LIBXML
#include "libxml.h"
#ifdef LIBXML_HTTP_ENABLED
#include <string.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#endif
#ifdef HAVE_SYS_SOCKET_H
#endif
#ifdef HAVE_NETINET_IN_H
#endif
#ifdef HAVE_ARPA_INET_H
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_RESOLV_H
#ifdef HAVE_ARPA_NAMESER_H
#endif
#include <resolv.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_SYS_TIME_H
#endif
#ifdef HAVE_SYS_SELECT_H
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef SUPPORT_IP6
#include <resolv.h>
#endif
#ifdef HAVE_ZLIB_H
#include <zlib.h>
#endif
#ifdef VMS
#include <stropts>
#define XML_SOCKLEN_T unsigned int
#define SOCKET int
#endif
#if defined(__MINGW32__) || defined(_WIN32_WCE)
#define _WINSOCKAPI_
#include <wsockcompat.h>
#include <winsock2.h>
#define XML_SOCKLEN_T unsigned int
#endif
#include <libxml/xmlerror.h>
#include <libxml/xmlmemory.h>
#include <libxml/nanohttp.h>
/**
* A couple portability macros
*/
#ifndef _WINSOCKAPI_
#ifndef __BEOS__
#define closesocket(s) close(s)
#endif
#define SOCKET int
#endif
#ifdef __BEOS__
#ifndef PF_INET
#endif
#endif
#ifndef XML_SOCKLEN_T
#define XML_SOCKLEN_T unsigned int
#endif
#ifndef SOCKET
#define SOCKET int
#endif
#ifdef STANDALONE
#define DEBUG_HTTP
#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
#endif
#define XML_NANO_HTTP_MAX_REDIR 10
#define XML_NANO_HTTP_CHUNK 4096
#define XML_NANO_HTTP_CLOSED 0
#define XML_NANO_HTTP_WRITE 1
#define XML_NANO_HTTP_READ 2
#define XML_NANO_HTTP_NONE 4
typedef struct xmlNanoHTTPCtxt {
char *protocol; /* the protocol name */
char *hostname; /* the host name */
int port; /* the port */
char *path; /* the path within the URL */
char *query; /* the query string */
int state; /* WRITE / READ / CLOSED */
char *out; /* buffer sent (zero terminated) */
char *outptr; /* index within the buffer sent */
char *in; /* the receiving buffer */
char *content; /* the start of the content */
char *inptr; /* the next byte to read from network */
char *inrptr; /* the next byte to give back to the client */
int inlen; /* len of the input buffer */
int last; /* return code for last operation */
int returnValue; /* the protocol return value */
int ContentLength; /* specified content length from HTTP header */
char *contentType; /* the MIME type for the input */
char *location; /* the new URL in case of redirect */
char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
char *encoding; /* encoding extracted from the contentType */
char *mimeType; /* Mime-Type extracted from the contentType */
#ifdef HAVE_ZLIB_H
int usesGzip; /* "Content-Encoding: gzip" was detected */
#endif
static int initialized = 0;
static int proxyPort; /* the proxy port if any */
/**
* xmlHTTPErrMemory:
* @extra: extra informations
*
* Handle an out of memory condition
*/
static void
xmlHTTPErrMemory(const char *extra)
{
}
/**
* A portability function
*/
static int socket_errno(void) {
#ifdef _WINSOCKAPI_
return(WSAGetLastError());
#else
return(errno);
#endif
}
#ifdef SUPPORT_IP6
static
int have_ipv6(void) {
int s;
if (s != -1) {
close (s);
return (1);
}
return (0);
}
#endif
/**
* xmlNanoHTTPInit:
*
* Initialize the HTTP protocol layer.
* Currently it just checks for proxy informations
*/
void
xmlNanoHTTPInit(void) {
const char *env;
#ifdef _WINSOCKAPI_
#endif
if (initialized)
return;
#ifdef _WINSOCKAPI_
return;
#endif
proxyPort = 80;
goto done;
goto done;
}
goto done;
}
}
done:
initialized = 1;
}
/**
* xmlNanoHTTPCleanup:
*
* Cleanup the HTTP protocol layer.
*/
void
xmlNanoHTTPCleanup(void) {
}
#ifdef _WINSOCKAPI_
if (initialized)
WSACleanup();
#endif
initialized = 0;
return;
}
/**
* xmlNanoHTTPScanURL:
* @ctxt: an HTTP context
* @URL: The URL used to initialize the context
*
* (Re)Initialize an HTTP context by parsing the URL and finding
* the protocol host port and path it indicates.
*/
static void
/*
* Clear any existing data from the context
*/
}
}
}
}
return;
return;
}
else
}
/**
* xmlNanoHTTPScanProxy:
* @URL: The proxy URL used to initialize the proxy context
*
* (Re)Initialize the HTTP Proxy context by parsing the URL and finding
* the protocol host port it indicates.
* Should be like http://myproxy/ or http://myproxy:3128/
* A NULL URL cleans up proxy informations.
*/
void
xmlNanoHTTPScanProxy(const char *URL) {
}
proxyPort = 0;
#ifdef DEBUG_HTTP
"Removing HTTP proxy info\n");
else
"Using HTTP proxy %s\n", URL);
#endif
return;
}
}
/**
* xmlNanoHTTPNewCtxt:
* @URL: The URL used to initialize the context
*
* Allocate and initialize a new HTTP context.
*
* Returns an HTTP context or NULL in case of error.
*/
static xmlNanoHTTPCtxtPtr
xmlNanoHTTPNewCtxt(const char *URL) {
xmlHTTPErrMemory("allocating context");
return(NULL);
}
ret->returnValue = 0;
return(ret);
}
/**
* xmlNanoHTTPFreeCtxt:
* @ctxt: an HTTP context
*
* Frees the context after closing the connection.
*/
static void
#ifdef HAVE_ZLIB_H
}
#endif
}
/**
* xmlNanoHTTPSend:
* @ctxt: an HTTP context
*
* Send the input needed to initiate the processing on the server side
* Returns number of bytes sent or -1 on error.
*/
static int
int total_sent = 0;
while (total_sent < outlen) {
outlen - total_sent, 0);
if (nsent>0)
total_sent += nsent;
else if ( ( nsent == -1 ) &&
( socket_errno( ) != EAGAIN ) &&
#endif
( socket_errno( ) != EWOULDBLOCK ) ) {
if ( total_sent == 0 )
total_sent = -1;
break;
}
else {
/*
** No data sent
** Since non-blocking sockets are used, wait for
** socket to be writable or default timeout prior
** to retrying.
*/
#ifdef _MSC_VER
#endif
#ifdef _MSC_VER
#endif
}
}
}
return total_sent;
}
/**
* xmlNanoHTTPRecv:
* @ctxt: an HTTP context
*
* Read information coming from the HTTP connection.
* This is a blocking call (but it blocks in select(), not read()).
*
* Returns the number of byte read or -1 in case of error.
*/
static int
xmlHTTPErrMemory("allocating input");
return(-1);
}
}
}
xmlHTTPErrMemory("allocating input buffer");
return(-1);
}
}
}
return(0);
}
switch (socket_errno()) {
case EINPROGRESS:
case EWOULDBLOCK:
case EAGAIN:
#endif
break;
case ECONNRESET:
case ESHUTDOWN:
return ( 0 );
default:
return(-1);
}
}
#ifdef _MSC_VER
#endif
#ifdef _MSC_VER
#endif
#if defined(EINTR)
#endif
)
return(0);
}
return(0);
}
/**
* xmlNanoHTTPReadLine:
* @ctxt: an HTTP context
*
* Read one line in the HTTP server output, usually for extracting
* the HTTP protocol informations from the answer header.
*
* Returns a newly allocated string with a copy of the line, or NULL
* which indicate the end of the input.
*/
static char *
char buf[4096];
int rc;
return(NULL);
else
*bp = 0;
return(xmlMemStrdup(buf));
}
else if ( rc == -1 ) {
return ( NULL );
}
}
if (*bp == '\n') {
*bp = 0;
return(xmlMemStrdup(buf));
}
if (*bp != '\r')
bp++;
}
buf[4095] = 0;
return(xmlMemStrdup(buf));
}
/**
* xmlNanoHTTPScanAnswer:
* @ctxt: an HTTP context
* @line: an HTTP header line
*
* Try to extract useful informations from the server answer.
* We currently parse and process:
* - The HTTP revision/ return code
* - The Content-Type, Mime-Type and charset used
* - The Location for redirect processing.
*
* Returns -1 in case of failure, the file descriptor number otherwise
*/
static void
int version = 0;
int ret = 0;
cur += 5;
version *= 10;
cur++;
}
if (*cur == '.') {
cur++;
version *= 10;
cur++;
}
cur++;
} else
version *= 10;
ret *= 10;
cur++;
}
cur += 13;
last++;
charset += 8;
last++;
}
cur += 12;
last++;
charset += 8;
last++;
}
cur += 9;
if (*cur == '/') {
} else {
}
cur += 17;
cur += 19;
#ifdef HAVE_ZLIB_H
cur += 17;
}
}
#endif
cur += 15;
}
}
/**
* xmlNanoHTTPConnectAttempt:
* @addr: a socket address structure
*
* Attempt a connection to the given IP:port endpoint. It forces
* non-blocking semantic on the socket, and allow 60 seconds for
* the host to answer.
*
* Returns -1 in case of failure, the file descriptor number otherwise
*/
static int
{
#ifdef _WINSOCKAPI_
#endif
int status;
int addrlen;
SOCKET s;
#ifdef SUPPORT_IP6
addrlen = sizeof (struct sockaddr_in6);
}
else
#endif
{
addrlen = sizeof (struct sockaddr_in);
}
if (s==-1) {
#ifdef DEBUG_HTTP
perror("socket");
#endif
return(-1);
}
#ifdef _WINSOCKAPI_
{
}
#else /* _WINSOCKAPI_ */
#if defined(VMS)
{
int enable = 1;
}
#else /* VMS */
#if defined(__BEOS__)
{
bool noblock = true;
}
#else /* __BEOS__ */
#ifdef O_NONBLOCK
status |= O_NONBLOCK;
#else /* O_NONBLOCK */
#ifdef F_NDELAY
#endif /* F_NDELAY */
#endif /* !O_NONBLOCK */
}
if (status < 0) {
#ifdef DEBUG_HTTP
perror("nonblocking");
#endif
closesocket(s);
return(-1);
}
#endif /* !__BEOS__ */
#endif /* !VMS */
#endif /* !_WINSOCKAPI_ */
switch (socket_errno()) {
case EINPROGRESS:
case EWOULDBLOCK:
break;
default:
closesocket(s);
return(-1);
}
}
#ifdef _MSC_VER
#endif
#ifdef _WINSOCKAPI_
#else
#endif
#ifdef _MSC_VER
#endif
{
case 0:
/* Time out */
closesocket(s);
return(-1);
case -1:
/* Ermm.. ?? */
closesocket(s);
return(-1);
}
#ifdef _WINSOCKAPI_
#endif
) {
#ifdef SO_ERROR
/* Solaris error code */
return (-1);
}
#endif
if ( status ) {
closesocket(s);
return (-1);
}
} else {
/* pbm */
closesocket(s);
return (-1);
}
return(s);
}
/**
* xmlNanoHTTPConnectHost:
* @host: the host name
* @port: the port number
*
* Attempt a connection to the given host:port endpoint. It tries
* the multiple IP provided by the DNS if available.
*
* Returns -1 in case of failure, the file descriptor number otherwise
*/
static int
{
struct hostent *h;
struct sockaddr_in sockin;
#ifdef SUPPORT_IP6
struct sockaddr_in6 sockin6;
#endif
int i;
int s;
#ifdef SUPPORT_IP6
#endif
if (have_ipv6 ())
{
res_init();
}
#endif
if (have_ipv6 ())
#endif
{
int status;
if (status) {
return (-1);
}
return (-1);
}
#ifdef SUPPORT_IP6
return (-1);
}
#endif
} else
continue; /* for */
s = xmlNanoHTTPConnectAttempt (addr);
if (s != -1) {
return (s);
}
}
if (result)
}
#endif
else
#endif
#if !defined(HAVE_GETADDRINFO) || !defined(_WIN32)
{
h = gethostbyname (host);
if (h == NULL) {
/*
* Okay, I got fed up by the non-portability of this error message
* extraction code. it work on Linux, if it work on your platform
* and one want to enable it, send me the defined(foobar) needed
*/
const char *h_err_txt = "";
switch (h_errno) {
case HOST_NOT_FOUND:
h_err_txt = "Authoritive host not found";
break;
case TRY_AGAIN:
"Non-authoritive host not found or server failure.";
break;
case NO_RECOVERY:
"Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
break;
case NO_ADDRESS:
"Valid name, no data record of requested type.";
break;
default:
h_err_txt = "No error text defined.";
break;
}
#else
#endif
return (-1);
}
for (i = 0; h->h_addr_list[i]; i++) {
if (h->h_addrtype == AF_INET) {
/* A records (IPv4) */
return (-1);
}
#ifdef SUPPORT_IP6
/* AAAA records (IPv6) */
return (-1);
}
#endif
} else
break; /* for */
s = xmlNanoHTTPConnectAttempt (addr);
if (s != -1)
return (s);
}
}
#endif
#ifdef DEBUG_HTTP
"xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
host);
#endif
return (-1);
}
/**
* xmlNanoHTTPOpen:
* @URL: The URL to load
* @contentType: if available the Content-Type information will be
* returned at that location
*
* This function try to open a connection to the indicated resource
* via HTTP GET.
*
* Returns NULL in case of failure, otherwise a request handler.
* The contentType, if provided must be freed by the caller
*/
void*
}
/**
* xmlNanoHTTPOpenRedir:
* @URL: The URL to load
* @contentType: if available the Content-Type information will be
* returned at that location
* @redir: if available the redirected URL will be returned
*
* This function try to open a connection to the indicated resource
* via HTTP GET.
*
* Returns NULL in case of failure, otherwise a request handler.
* The contentType, if provided must be freed by the caller
*/
void*
}
/**
* xmlNanoHTTPRead:
* @ctx: the HTTP context
* @dest: a buffer
* @len: the buffer length
*
* This function tries to read @len bytes from the existing HTTP connection
* and saves them in @dest. This is a blocking call.
*
* Returns the number of byte read. 0 is an indication of an end of connection.
* -1 indicates a parameter error.
*/
int
#ifdef HAVE_ZLIB_H
int bytes_read = 0;
int orig_avail_in;
int z_ret;
#endif
if (len <= 0) return(0);
#ifdef HAVE_ZLIB_H
}
}
#endif
if (xmlNanoHTTPRecv(ctxt) <= 0) break;
}
return(len);
}
/**
* xmlNanoHTTPClose:
* @ctx: the HTTP context
*
* This function closes an HTTP context, it ends up the connection and
* free all data related to it.
*/
void
xmlNanoHTTPClose(void *ctx) {
}
/**
* xmlNanoHTTPMethodRedir:
* @URL: The URL to load
* @method: the HTTP method to use
* @input: the input string if any
* @contentType: the Content-Type information IN and OUT
* @redir: the redirected URL OUT
* @headers: the extra headers
* @ilen: input length
*
* This function try to open a connection to the indicated resource
* via HTTP using the given @method, adding the given extra headers
* and the input buffer for the request content.
*
* Returns NULL in case of failure, otherwise a request handler.
* The contentType, or redir, if provided must be freed by the caller
*/
void*
char **contentType, char **redir,
char *bp, *p;
int head;
int nbRedirects = 0;
#ifdef DEBUG_HTTP
int xmt_bytes;
#endif
else {
}
return ( NULL );
}
return(NULL);
}
"Failed to identify host in URI");
return(NULL);
}
if (proxy) {
}
else {
}
if (ret < 0) {
return(NULL);
}
ilen = 0;
else
blen += 36;
if (contentType && *contentType)
/* reserve for string plus 'Content-Type: \r\n" */
/* 1 for '?' */
#ifdef HAVE_ZLIB_H
/* reserve for possible 'Accept-Encoding: gzip' string */
blen += 23;
#endif
/* reserve space for ':xxxxx', incl. potential proxy */
if (proxy)
blen += 12;
else
blen += 6;
}
xmlHTTPErrMemory("allocating header buffer");
return ( NULL );
}
p = bp;
if (proxy) {
}
else
}
else
} else {
}
#ifdef HAVE_ZLIB_H
#endif
else
#ifdef DEBUG_HTTP
"ERROR: overflowed buffer by %d bytes\n", -blen);
#endif
#ifdef DEBUG_HTTP
"xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
"bytes of HTTP headers sent to host",
#else
#endif
#ifdef DEBUG_HTTP
"xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
"bytes of HTTP content sent to host",
#else
#endif
}
head = 1;
if (head && (*p == 0)) {
head = 0;
xmlFree(p);
break;
}
#ifdef DEBUG_HTTP
#endif
xmlFree(p);
}
#ifdef DEBUG_HTTP
#endif
while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
nbRedirects++;
goto retry;
}
#ifdef DEBUG_HTTP
"xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
#endif
return(NULL);
}
if (contentType != NULL) {
else
*contentType = NULL;
}
} else {
}
#ifdef DEBUG_HTTP
"\nCode %d, content-type '%s'\n\n",
else
"\nCode %d, no content-type\n\n",
ctxt->returnValue);
#endif
return((void *) ctxt);
}
/**
* xmlNanoHTTPMethod:
* @URL: The URL to load
* @method: the HTTP method to use
* @input: the input string if any
* @contentType: the Content-Type information IN and OUT
* @headers: the extra headers
* @ilen: input length
*
* This function try to open a connection to the indicated resource
* via HTTP using the given @method, adding the given extra headers
* and the input buffer for the request content.
*
* Returns NULL in case of failure, otherwise a request handler.
* The contentType, if provided must be freed by the caller
*/
void*
}
/**
* xmlNanoHTTPFetch:
* @URL: The URL to load
* @filename: the filename where the content should be saved
* @contentType: if available the Content-Type information will be
* returned at that location
*
* This function try to fetch the indicated resource via HTTP GET
* and save it's content in the file.
*
* Returns -1 in case of failure, 0 incase of success. The contentType,
* if provided must be freed by the caller
*/
int
int fd;
int len;
fd = 0;
else {
if (fd < 0) {
*contentType = NULL;
}
return(-1);
}
}
if ( len > 0 ) {
}
return(0);
}
#ifdef LIBXML_OUTPUT_ENABLED
/**
* xmlNanoHTTPSave:
* @ctxt: the HTTP context
* @filename: the filename where the content should be saved
*
* This function saves the output of the HTTP transaction to a file
* It closes and free the context at the end
*
* Returns -1 in case of failure, 0 incase of success.
*/
int
int fd;
int len;
fd = 0;
else {
if (fd < 0) {
return(-1);
}
}
if ( len > 0 ) {
}
return(0);
}
#endif /* LIBXML_OUTPUT_ENABLED */
/**
* xmlNanoHTTPReturnCode:
* @ctx: the HTTP context
*
* Get the latest HTTP return code received
*
* Returns the HTTP return code for the request.
*/
int
xmlNanoHTTPReturnCode(void *ctx) {
return(ctxt->returnValue);
}
/**
* xmlNanoHTTPAuthHeader:
* @ctx: the HTTP context
*
* Get the authentication header of an HTTP context
*
* Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
* header.
*/
const char *
xmlNanoHTTPAuthHeader(void *ctx) {
return(ctxt->authHeader);
}
/**
* xmlNanoHTTPContentLength:
* @ctx: the HTTP context
*
* Provides the specified content length from the HTTP header.
*
* Return the specified content length from the HTTP header. Note that
* a value of -1 indicates that the content length element was not included in
* the response header.
*/
int
xmlNanoHTTPContentLength( void * ctx ) {
}
/**
* xmlNanoHTTPRedir:
* @ctx: the HTTP context
*
* Provides the specified redirection URL if available from the HTTP header.
*
* Return the specified redirection URL or NULL if not redirected.
*/
const char *
xmlNanoHTTPRedir( void * ctx ) {
}
/**
* xmlNanoHTTPEncoding:
* @ctx: the HTTP context
*
* Provides the specified encoding if specified in the HTTP headers.
*
* Return the specified encoding or NULL if not available
*/
const char *
xmlNanoHTTPEncoding( void * ctx ) {
}
/**
* xmlNanoHTTPMimeType:
* @ctx: the HTTP context
*
* Provides the specified Mime-Type if specified in the HTTP headers.
*
* Return the specified Mime-Type or NULL if not available
*/
const char *
xmlNanoHTTPMimeType( void * ctx ) {
}
/**
* xmlNanoHTTPFetchContent:
* @ctx: the HTTP context
* @ptr: pointer to set to the content buffer.
* @len: integer pointer to hold the length of the content
*
* 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
* occurred.
*/
static int
int rc = 0;
int cur_lgth;
int rcvd_lgth;
int dummy_int;
/* Dummy up return input parameters if not provided */
/* But can't work without the context pointer */
*len = 0;
return ( -1 );
}
break;
}
rc = -1;
else if ( rcvd_lgth == 0 )
rc = -1;
return ( rc );
}
#ifdef STANDALONE
char *contentType = NULL;
else
} else {
"%s: minimal HTTP GET implementation\n", argv[0]);
"\tusage %s [ URL [ filename ] ]\n", argv[0]);
}
return(0);
}
#endif /* STANDALONE */
#else /* !LIBXML_HTTP_ENABLED */
#ifdef STANDALONE
#include <stdio.h>
"%s : HTTP support not compiled in\n", argv[0]);
return(0);
}
#endif /* STANDALONE */
#endif /* LIBXML_HTTP_ENABLED */
#define bottom_nanohttp
#include "elfgcchack.h"