Annotation of XML/nanohttp.c, revision 1.33

1.1       daniel      1: /*
1.5       daniel      2:  * nanohttp.c: minimalist HTTP GET implementation to fetch external subsets.
                      3:  *             focuses on size, streamability, reentrancy and portability
                      4:  *
                      5:  * This is clearly not a general purpose HTTP implementation
                      6:  * If you look for one, check:
                      7:  *         http://www.w3.org/Library/
1.1       daniel      8:  *
                      9:  * See Copyright for the status of this software.
                     10:  *
                     11:  * Daniel.Veillard@w3.org
                     12:  */
                     13:  
1.5       daniel     14: /* TODO add compression support, Send the Accept- , and decompress on the
                     15:         fly with ZLIB if found at compile-time */
                     16: 
1.9       daniel     17: #ifdef WIN32
1.11      daniel     18: #define INCLUDE_WINSOCK
1.9       daniel     19: #include "win32config.h"
                     20: #else
1.4       daniel     21: #include "config.h"
                     22: #endif
1.9       daniel     23: 
1.25      veillard   24: #include <libxml/xmlversion.h>
1.4       daniel     25: 
1.17      daniel     26: #ifdef LIBXML_HTTP_ENABLED
1.1       daniel     27: #include <stdio.h>
                     28: #include <string.h>
1.4       daniel     29: 
                     30: #ifdef HAVE_STDLIB_H
1.1       daniel     31: #include <stdlib.h>
1.4       daniel     32: #endif
                     33: #ifdef HAVE_UNISTD_H
1.1       daniel     34: #include <unistd.h>
1.4       daniel     35: #endif
                     36: #ifdef HAVE_SYS_SOCKET_H
1.1       daniel     37: #include <sys/socket.h>
1.4       daniel     38: #endif
                     39: #ifdef HAVE_NETINET_IN_H
1.1       daniel     40: #include <netinet/in.h>
1.4       daniel     41: #endif
                     42: #ifdef HAVE_ARPA_INET_H
1.1       daniel     43: #include <arpa/inet.h>
1.4       daniel     44: #endif
                     45: #ifdef HAVE_NETDB_H
1.1       daniel     46: #include <netdb.h>
1.4       daniel     47: #endif
                     48: #ifdef HAVE_FCNTL_H
1.1       daniel     49: #include <fcntl.h> 
1.4       daniel     50: #endif
                     51: #ifdef HAVE_ERRNO_H
1.1       daniel     52: #include <errno.h>
1.4       daniel     53: #endif
                     54: #ifdef HAVE_SYS_TIME_H
1.1       daniel     55: #include <sys/time.h>
1.4       daniel     56: #endif
                     57: #ifdef HAVE_SYS_SELECT_H
1.1       daniel     58: #include <sys/select.h>
1.4       daniel     59: #endif
1.15      daniel     60: #ifdef HAVE_STRINGS_H
                     61: #include <strings.h>
                     62: #endif
1.1       daniel     63: 
1.17      daniel     64: #include <libxml/xmlmemory.h>
1.23      veillard   65: #include <libxml/parser.h> /* for xmlStr(n)casecmp() */
1.17      daniel     66: #include <libxml/nanohttp.h>
1.7       daniel     67: 
1.30      veillard   68: /**
                     69:  * A couple portability macros
                     70:  */
                     71: #ifndef _WINSOCKAPI_
                     72: #define closesocket(s) close(s)
                     73: #define SOCKET int
                     74: #endif
                     75: 
1.5       daniel     76: #ifdef STANDALONE
                     77: #define DEBUG_HTTP
1.23      veillard   78: #define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
                     79: #define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
1.5       daniel     80: #endif
                     81: 
1.1       daniel     82: #define XML_NANO_HTTP_MAX_REDIR        10
                     83: 
                     84: #define XML_NANO_HTTP_CHUNK    4096
                     85: 
                     86: #define XML_NANO_HTTP_CLOSED   0
                     87: #define XML_NANO_HTTP_WRITE    1
                     88: #define XML_NANO_HTTP_READ     2
                     89: #define XML_NANO_HTTP_NONE     4
                     90: 
                     91: typedef struct xmlNanoHTTPCtxt {
                     92:     char *protocol;    /* the protocol name */
                     93:     char *hostname;    /* the host name */
                     94:     int port;          /* the port */
                     95:     char *path;                /* the path within the URL */
1.30      veillard   96:     SOCKET fd;         /* the file descriptor for the socket */
1.1       daniel     97:     int state;         /* WRITE / READ / CLOSED */
                     98:     char *out;         /* buffer sent (zero terminated) */
                     99:     char *outptr;      /* index within the buffer sent */
                    100:     char *in;          /* the receiving buffer */
                    101:     char *content;     /* the start of the content */
                    102:     char *inptr;       /* the next byte to read from network */
                    103:     char *inrptr;      /* the next byte to give back to the client */
                    104:     int inlen;         /* len of the input buffer */
                    105:     int last;          /* return code for last operation */
                    106:     int returnValue;   /* the protocol return value */
                    107:     char *contentType; /* the MIME type for the input */
                    108:     char *location;    /* the new URL in case of redirect */
                    109: } xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
                    110: 
1.12      daniel    111: static int initialized = 0;
1.28      veillard  112: static char *proxy = NULL;      /* the proxy name if any */
1.12      daniel    113: static int proxyPort;  /* the proxy port if any */
1.28      veillard  114: static unsigned int timeout = 60;/* the select() timeout in seconds */
1.12      daniel    115: 
                    116: /**
1.30      veillard  117:  * A portability function
1.26      veillard  118:  */
                    119: int socket_errno(void) {
                    120: #ifdef _WINSOCKAPI_
                    121:     return(WSAGetLastError());
                    122: #else
                    123:     return(errno);
                    124: #endif
                    125: }
                    126: 
                    127: /**
1.12      daniel    128:  * xmlNanoHTTPInit:
                    129:  *
                    130:  * Initialize the HTTP protocol layer.
                    131:  * Currently it just checks for proxy informations
                    132:  */
                    133: 
                    134: void
                    135: xmlNanoHTTPInit(void) {
                    136:     const char *env;
1.30      veillard  137: #ifdef _WINSOCKAPI_
                    138:     WSADATA wsaData;    
                    139: #endif
1.12      daniel    140: 
                    141:     if (initialized)
                    142:        return;
                    143: 
1.26      veillard  144: #ifdef _WINSOCKAPI_
1.30      veillard  145:     if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
                    146:        return;
1.26      veillard  147: #endif
                    148: 
1.13      daniel    149:     if (proxy == NULL) {
                    150:        proxyPort = 80;
                    151:        env = getenv("no_proxy");
                    152:        if (env != NULL)
                    153:            goto done;
                    154:        env = getenv("http_proxy");
                    155:        if (env != NULL) {
                    156:            xmlNanoHTTPScanProxy(env);
                    157:            goto done;
                    158:        }
                    159:        env = getenv("HTTP_PROXY");
                    160:        if (env != NULL) {
                    161:            xmlNanoHTTPScanProxy(env);
                    162:            goto done;
                    163:        }
1.12      daniel    164:     }
1.13      daniel    165: done:
1.12      daniel    166:     initialized = 1;
                    167: }
                    168: 
                    169: /**
                    170:  * xmlNanoHTTPClenup:
                    171:  *
                    172:  * Cleanup the HTTP protocol layer.
                    173:  */
                    174: 
                    175: void
                    176: xmlNanoHTTPCleanup(void) {
                    177:     if (proxy != NULL)
                    178:        xmlFree(proxy);
1.26      veillard  179: #ifdef _WINSOCKAPI_
1.30      veillard  180:     if (initialized)
                    181:        WSACleanup();
1.26      veillard  182: #endif
1.30      veillard  183:     initialized = 0;
1.12      daniel    184:     return;
                    185: }
                    186: 
1.5       daniel    187: /**
1.28      veillard  188:  * xmlNanoHTTPTimeout:
                    189:  * @delay:  the delay in seconds
                    190:  *
                    191:  * Set the HTTP timeout, (default is 60secs).  0 means immediate
                    192:  * return, while -1 infinite.
                    193:  */
                    194: 
                    195: void
                    196: xmlNanoHTTPTimeout(int delay) {
                    197:     timeout = (unsigned int) delay;
                    198: }
                    199: 
                    200: /**
1.5       daniel    201:  * xmlNanoHTTPScanURL:
                    202:  * @ctxt:  an HTTP context
                    203:  * @URL:  The URL used to initialize the context
                    204:  *
                    205:  * (Re)Initialize an HTTP context by parsing the URL and finding
                    206:  * the protocol host port and path it indicates.
                    207:  */
                    208: 
                    209: static void
                    210: xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
1.1       daniel    211:     const char *cur = URL;
                    212:     char buf[4096];
                    213:     int index = 0;
                    214:     int port = 0;
                    215: 
                    216:     if (ctxt->protocol != NULL) { 
1.7       daniel    217:         xmlFree(ctxt->protocol);
1.1       daniel    218:        ctxt->protocol = NULL;
                    219:     }
                    220:     if (ctxt->hostname != NULL) { 
1.7       daniel    221:         xmlFree(ctxt->hostname);
1.1       daniel    222:        ctxt->hostname = NULL;
                    223:     }
                    224:     if (ctxt->path != NULL) { 
1.7       daniel    225:         xmlFree(ctxt->path);
1.1       daniel    226:        ctxt->path = NULL;
                    227:     }
1.12      daniel    228:     if (URL == NULL) return;
1.1       daniel    229:     buf[index] = 0;
                    230:     while (*cur != 0) {
                    231:         if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
                    232:            buf[index] = 0;
1.7       daniel    233:            ctxt->protocol = xmlMemStrdup(buf);
1.1       daniel    234:            index = 0;
                    235:             cur += 3;
                    236:            break;
                    237:        }
                    238:        buf[index++] = *cur++;
                    239:     }
                    240:     if (*cur == 0) return;
                    241: 
                    242:     buf[index] = 0;
                    243:     while (1) {
                    244:         if (cur[0] == ':') {
                    245:            buf[index] = 0;
1.7       daniel    246:            ctxt->hostname = xmlMemStrdup(buf);
1.1       daniel    247:            index = 0;
                    248:            cur += 1;
                    249:            while ((*cur >= '0') && (*cur <= '9')) {
                    250:                port *= 10;
                    251:                port += *cur - '0';
                    252:                cur++;
                    253:            }
                    254:            if (port != 0) ctxt->port = port;
                    255:            while ((cur[0] != '/') && (*cur != 0)) 
                    256:                cur++;
                    257:            break;
                    258:        }
                    259:         if ((*cur == '/') || (*cur == 0)) {
                    260:            buf[index] = 0;
1.7       daniel    261:            ctxt->hostname = xmlMemStrdup(buf);
1.1       daniel    262:            index = 0;
                    263:            break;
                    264:        }
                    265:        buf[index++] = *cur++;
                    266:     }
                    267:     if (*cur == 0) 
1.7       daniel    268:         ctxt->path = xmlMemStrdup("/");
1.5       daniel    269:     else {
1.14      daniel    270:         index = 0;
1.5       daniel    271:         buf[index] = 0;
1.14      daniel    272:        while (*cur != 0)
1.5       daniel    273:            buf[index++] = *cur++;
                    274:        buf[index] = 0;
1.7       daniel    275:        ctxt->path = xmlMemStrdup(buf);
1.5       daniel    276:     }  
1.1       daniel    277: }
                    278: 
1.5       daniel    279: /**
1.12      daniel    280:  * xmlNanoHTTPScanProxy:
                    281:  * @URL:  The proxy URL used to initialize the proxy context
                    282:  *
                    283:  * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
                    284:  * the protocol host port it indicates.
                    285:  * Should be like http://myproxy/ or http://myproxy:3128/
                    286:  * A NULL URL cleans up proxy informations.
                    287:  */
                    288: 
                    289: void
                    290: xmlNanoHTTPScanProxy(const char *URL) {
                    291:     const char *cur = URL;
                    292:     char buf[4096];
                    293:     int index = 0;
                    294:     int port = 0;
                    295: 
                    296:     if (proxy != NULL) { 
                    297:         xmlFree(proxy);
                    298:        proxy = NULL;
                    299:     }
                    300:     if (proxyPort != 0) { 
                    301:        proxyPort = 0;
                    302:     }
                    303: #ifdef DEBUG_HTTP
                    304:     if (URL == NULL)
                    305:        printf("Removing HTTP proxy info\n");
                    306:     else
                    307:        printf("Using HTTP proxy %s\n", URL);
                    308: #endif
                    309:     if (URL == NULL) return;
                    310:     buf[index] = 0;
                    311:     while (*cur != 0) {
                    312:         if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
                    313:            buf[index] = 0;
                    314:            index = 0;
                    315:             cur += 3;
                    316:            break;
                    317:        }
                    318:        buf[index++] = *cur++;
                    319:     }
                    320:     if (*cur == 0) return;
                    321: 
                    322:     buf[index] = 0;
                    323:     while (1) {
                    324:         if (cur[0] == ':') {
                    325:            buf[index] = 0;
                    326:            proxy = xmlMemStrdup(buf);
                    327:            index = 0;
                    328:            cur += 1;
                    329:            while ((*cur >= '0') && (*cur <= '9')) {
                    330:                port *= 10;
                    331:                port += *cur - '0';
                    332:                cur++;
                    333:            }
                    334:            if (port != 0) proxyPort = port;
                    335:            while ((cur[0] != '/') && (*cur != 0)) 
                    336:                cur++;
                    337:            break;
                    338:        }
                    339:         if ((*cur == '/') || (*cur == 0)) {
                    340:            buf[index] = 0;
                    341:            proxy = xmlMemStrdup(buf);
                    342:            index = 0;
                    343:            break;
                    344:        }
                    345:        buf[index++] = *cur++;
                    346:     }
                    347: }
                    348: 
                    349: /**
1.5       daniel    350:  * xmlNanoHTTPNewCtxt:
                    351:  * @URL:  The URL used to initialize the context
                    352:  *
                    353:  * Allocate and initialize a new HTTP context.
                    354:  *
                    355:  * Returns an HTTP context or NULL in case of error.
                    356:  */
                    357: 
                    358: static xmlNanoHTTPCtxtPtr
                    359: xmlNanoHTTPNewCtxt(const char *URL) {
1.1       daniel    360:     xmlNanoHTTPCtxtPtr ret;
                    361: 
1.7       daniel    362:     ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
1.1       daniel    363:     if (ret == NULL) return(NULL);
                    364: 
                    365:     memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
                    366:     ret->port = 80;
                    367:     ret->returnValue = 0;
                    368: 
                    369:     xmlNanoHTTPScanURL(ret, URL);
                    370: 
                    371:     return(ret);
                    372: }
                    373: 
1.5       daniel    374: /**
                    375:  * xmlNanoHTTPFreeCtxt:
                    376:  * @ctxt:  an HTTP context
                    377:  *
                    378:  * Frees the context after closing the connection.
                    379:  */
                    380: 
                    381: static void
                    382: xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
                    383:     if (ctxt == NULL) return;
1.7       daniel    384:     if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
                    385:     if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
                    386:     if (ctxt->path != NULL) xmlFree(ctxt->path);
                    387:     if (ctxt->out != NULL) xmlFree(ctxt->out);
                    388:     if (ctxt->in != NULL) xmlFree(ctxt->in);
                    389:     if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
                    390:     if (ctxt->location != NULL) xmlFree(ctxt->location);
1.1       daniel    391:     ctxt->state = XML_NANO_HTTP_NONE;
1.26      veillard  392:     if (ctxt->fd >= 0) closesocket(ctxt->fd);
1.1       daniel    393:     ctxt->fd = -1;
1.7       daniel    394:     xmlFree(ctxt);
1.1       daniel    395: }
                    396: 
1.5       daniel    397: /**
                    398:  * xmlNanoHTTPSend:
                    399:  * @ctxt:  an HTTP context
                    400:  *
                    401:  * Send the input needed to initiate the processing on the server side
                    402:  */
                    403: 
                    404: static void
                    405: xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt) {
1.31      veillard  406:     if (ctxt->state & XML_NANO_HTTP_WRITE) {
                    407:         int total_sent = 0;
                    408:         while (total_sent <strlen(ctxt->outptr)) {
                    409:             int nsent = send(ctxt->fd, ctxt->outptr+total_sent,
                    410:                              strlen(ctxt->outptr)-total_sent, 0);
                    411:             if (nsent>0)
                    412:                 total_sent += nsent;
                    413: }
                    414: 
                    415:         ctxt->last = total_sent;
                    416:     }
1.1       daniel    417: }
                    418: 
1.5       daniel    419: /**
                    420:  * xmlNanoHTTPRecv:
                    421:  * @ctxt:  an HTTP context
                    422:  *
                    423:  * Read information coming from the HTTP connection.
                    424:  * This is a blocking call (but it blocks in select(), not read()).
                    425:  *
                    426:  * Returns the number of byte read or -1 in case of error.
                    427:  */
                    428: 
                    429: static int
                    430: xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
1.1       daniel    431:     fd_set rfd;
                    432:     struct timeval tv;
                    433: 
                    434: 
                    435:     while (ctxt->state & XML_NANO_HTTP_READ) {
                    436:        if (ctxt->in == NULL) {
1.7       daniel    437:            ctxt->in = (char *) xmlMalloc(65000 * sizeof(char));
1.1       daniel    438:            if (ctxt->in == NULL) {
                    439:                ctxt->last = -1;
                    440:                return(-1);
                    441:            }
                    442:            ctxt->inlen = 65000;
                    443:            ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
                    444:        }
                    445:        if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
                    446:            int delta = ctxt->inrptr - ctxt->in;
                    447:            int len = ctxt->inptr - ctxt->inrptr;
                    448:            
                    449:            memmove(ctxt->in, ctxt->inrptr, len);
                    450:            ctxt->inrptr -= delta;
                    451:            ctxt->content -= delta;
                    452:            ctxt->inptr -= delta;
                    453:        }
                    454:         if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
                    455:            int d_inptr = ctxt->inptr - ctxt->in;
                    456:            int d_content = ctxt->content - ctxt->in;
                    457:            int d_inrptr = ctxt->inrptr - ctxt->in;
                    458: 
                    459:            ctxt->inlen *= 2;
1.7       daniel    460:             ctxt->in = (char *) xmlRealloc(ctxt->in, ctxt->inlen);
1.1       daniel    461:            if (ctxt->in == NULL) {
                    462:                ctxt->last = -1;
                    463:                return(-1);
                    464:            }
                    465:             ctxt->inptr = ctxt->in + d_inptr;
                    466:             ctxt->content = ctxt->in + d_content;
                    467:             ctxt->inrptr = ctxt->in + d_inrptr;
                    468:        }
1.26      veillard  469:        ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
1.1       daniel    470:        if (ctxt->last > 0) {
                    471:            ctxt->inptr += ctxt->last;
                    472:            return(ctxt->last);
                    473:        }
                    474:        if (ctxt->last == 0) {
                    475:            return(0);
                    476:        }
1.26      veillard  477:        if (ctxt->last == -1) {
                    478:            switch (socket_errno()) {
                    479:                case EINPROGRESS:
                    480:                case EWOULDBLOCK:
                    481: #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
                    482:                case EAGAIN:
                    483: #endif
                    484:                    break;
                    485:                default:
                    486:                    return(0);
                    487:            }
1.1       daniel    488:        }
1.26      veillard  489: 
1.28      veillard  490:        tv.tv_sec = timeout;
1.26      veillard  491:        tv.tv_usec = 0;
1.1       daniel    492:        FD_ZERO(&rfd);
                    493:        FD_SET(ctxt->fd, &rfd);
                    494:        
1.26      veillard  495:        if (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
1.5       daniel    496:                return(0);
1.1       daniel    497:     }
                    498:     return(0);
                    499: }
                    500: 
1.5       daniel    501: /**
                    502:  * xmlNanoHTTPReadLine:
                    503:  * @ctxt:  an HTTP context
                    504:  *
                    505:  * Read one line in the HTTP server output, usually for extracting
                    506:  * the HTTP protocol informations from the answer header.
                    507:  *
                    508:  * Returns a newly allocated string with a copy of the line, or NULL
                    509:  *         which indicate the end of the input.
                    510:  */
                    511: 
                    512: static char *
                    513: xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
                    514:     char buf[4096];
1.26      veillard  515:     char *bp = buf;
1.1       daniel    516:     
1.26      veillard  517:     while (bp - buf < 4095) {
                    518:        if (ctxt->inrptr == ctxt->inptr) {
1.1       daniel    519:            if (xmlNanoHTTPRecv(ctxt) == 0) {
                    520:                if (bp == buf)
1.5       daniel    521:                    return(NULL);
1.1       daniel    522:                else
                    523:                    *bp = 0;
1.7       daniel    524:                return(xmlMemStrdup(buf));
1.1       daniel    525:            }
                    526:        }
                    527:        *bp = *ctxt->inrptr++;
1.26      veillard  528:        if (*bp == '\n') {
1.1       daniel    529:            *bp = 0;
1.7       daniel    530:            return(xmlMemStrdup(buf));
1.1       daniel    531:        }
1.26      veillard  532:        if (*bp != '\r')
1.1       daniel    533:            bp++;
                    534:     }
                    535:     buf[4095] = 0;
1.7       daniel    536:     return(xmlMemStrdup(buf));
1.1       daniel    537: }
                    538: 
1.5       daniel    539: 
                    540: /**
                    541:  * xmlNanoHTTPScanAnswer:
                    542:  * @ctxt:  an HTTP context
                    543:  * @line:  an HTTP header line
                    544:  *
                    545:  * Try to extract useful informations from the server answer.
                    546:  * We currently parse and process:
                    547:  *  - The HTTP revision/ return code
                    548:  *  - The Content-Type
                    549:  *  - The Location for redirrect processing.
                    550:  *
                    551:  * Returns -1 in case of failure, the file descriptor number otherwise
                    552:  */
                    553: 
                    554: static void
                    555: xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
1.1       daniel    556:     const char *cur = line;
                    557: 
                    558:     if (line == NULL) return;
                    559: 
                    560:     if (!strncmp(line, "HTTP/", 5)) {
                    561:         int version = 0;
                    562:        int ret = 0;
                    563: 
                    564:        cur += 5;
                    565:        while ((*cur >= '0') && (*cur <= '9')) {
                    566:            version *= 10;
                    567:            version += *cur - '0';
                    568:            cur++;
                    569:        }
                    570:        if (*cur == '.') {
                    571:            cur++;
                    572:            if ((*cur >= '0') && (*cur <= '9')) {
                    573:                version *= 10;
                    574:                version += *cur - '0';
                    575:                cur++;
                    576:            }
                    577:            while ((*cur >= '0') && (*cur <= '9'))
                    578:                cur++;
                    579:        } else
                    580:            version *= 10;
                    581:        if ((*cur != ' ') && (*cur != '\t')) return;
                    582:        while ((*cur == ' ') || (*cur == '\t')) cur++;
                    583:        if ((*cur < '0') || (*cur > '9')) return;
                    584:        while ((*cur >= '0') && (*cur <= '9')) {
                    585:            ret *= 10;
                    586:            ret += *cur - '0';
                    587:            cur++;
                    588:        }
                    589:        if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
                    590:        ctxt->returnValue = ret;
1.23      veillard  591:     } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
1.1       daniel    592:         cur += 13;
                    593:        while ((*cur == ' ') || (*cur == '\t')) cur++;
                    594:        if (ctxt->contentType != NULL)
1.7       daniel    595:            xmlFree(ctxt->contentType);
                    596:        ctxt->contentType = xmlMemStrdup(cur);
1.23      veillard  597:     } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
1.1       daniel    598:         cur += 12;
                    599:        if (ctxt->contentType != NULL) return;
                    600:        while ((*cur == ' ') || (*cur == '\t')) cur++;
1.7       daniel    601:        ctxt->contentType = xmlMemStrdup(cur);
1.23      veillard  602:     } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
1.1       daniel    603:         cur += 9;
                    604:        while ((*cur == ' ') || (*cur == '\t')) cur++;
                    605:        if (ctxt->location != NULL)
1.7       daniel    606:            xmlFree(ctxt->location);
                    607:        ctxt->location = xmlMemStrdup(cur);
1.1       daniel    608:     }
                    609: }
                    610: 
1.5       daniel    611: /**
                    612:  * xmlNanoHTTPConnectAttempt:
                    613:  * @ia:  an internet adress structure
                    614:  * @port:  the port number
                    615:  *
                    616:  * Attempt a connection to the given IP:port endpoint. It forces
                    617:  * non-blocking semantic on the socket, and allow 60 seconds for
                    618:  * the host to answer.
                    619:  *
                    620:  * Returns -1 in case of failure, the file descriptor number otherwise
                    621:  */
                    622: 
                    623: static int
                    624: xmlNanoHTTPConnectAttempt(struct in_addr ia, int port)
1.1       daniel    625: {
1.30      veillard  626:     SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
1.1       daniel    627:     struct sockaddr_in sin;
                    628:     fd_set wfd;
                    629:     struct timeval tv;
1.2       daniel    630:     int status;
1.1       daniel    631:     
1.26      veillard  632:     if (s==-1) {
1.5       daniel    633: #ifdef DEBUG_HTTP
1.1       daniel    634:        perror("socket");
1.5       daniel    635: #endif
1.1       daniel    636:        return(-1);
                    637:     }
                    638:     
1.2       daniel    639: #ifdef _WINSOCKAPI_
                    640:     {
                    641:        u_long one = 1;
                    642: 
1.3       daniel    643:        status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
1.2       daniel    644:     }
                    645: #else /* _WINSOCKAPI_ */
                    646: #if defined(VMS)
                    647:     {
                    648:        int enable = 1;
1.3       daniel    649:        status = IOCTL(s, FIONBIO, &enable);
1.2       daniel    650:     }
                    651: #else /* VMS */
1.26      veillard  652:     if ((status = fcntl(s, F_GETFL, 0)) != -1) {
1.2       daniel    653: #ifdef O_NONBLOCK
                    654:        status |= O_NONBLOCK;
                    655: #else /* O_NONBLOCK */
                    656: #ifdef F_NDELAY
                    657:        status |= F_NDELAY;
                    658: #endif /* F_NDELAY */
                    659: #endif /* !O_NONBLOCK */
1.3       daniel    660:        status = fcntl(s, F_SETFL, status);
1.2       daniel    661:     }
1.26      veillard  662:     if (status < 0) {
1.5       daniel    663: #ifdef DEBUG_HTTP
1.1       daniel    664:        perror("nonblocking");
1.5       daniel    665: #endif
1.26      veillard  666:        closesocket(s);
1.1       daniel    667:        return(-1);
                    668:     }
1.2       daniel    669: #endif /* !VMS */
                    670: #endif /* !_WINSOCKAPI_ */
                    671: 
1.1       daniel    672: 
                    673:     sin.sin_family = AF_INET;  
                    674:     sin.sin_addr   = ia;
                    675:     sin.sin_port   = htons(port);
                    676:     
1.30      veillard  677:     if ((connect(s, (struct sockaddr *)&sin, sizeof(sin))==-1)) {
                    678:        switch (socket_errno()) {
                    679:            case EINPROGRESS:
                    680:            case EWOULDBLOCK:
                    681:                break;
                    682:            default:
                    683:                perror("connect");
                    684:                closesocket(s);
                    685:                return(-1);
                    686:        }
1.1       daniel    687:     }  
                    688:     
1.28      veillard  689:     tv.tv_sec = timeout;
1.1       daniel    690:     tv.tv_usec = 0;
                    691:     
                    692:     FD_ZERO(&wfd);
                    693:     FD_SET(s, &wfd);
                    694:     
                    695:     switch(select(s+1, NULL, &wfd, NULL, &tv))
                    696:     {
                    697:        case 0:
                    698:            /* Time out */
1.26      veillard  699:            closesocket(s);
1.1       daniel    700:            return(-1);
                    701:        case -1:
                    702:            /* Ermm.. ?? */
1.5       daniel    703: #ifdef DEBUG_HTTP
1.1       daniel    704:            perror("select");
1.5       daniel    705: #endif
1.26      veillard  706:            closesocket(s);
1.1       daniel    707:            return(-1);
                    708:     }
1.19      daniel    709: 
                    710:     if ( FD_ISSET(s, &wfd) ) {
1.27      veillard  711:        SOCKLEN_T len;
1.19      daniel    712:        len = sizeof(status);
1.30      veillard  713:        if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
1.19      daniel    714:            /* Solaris error code */
                    715:            return (-1);
                    716:        }
                    717:        if ( status ) {
1.26      veillard  718:            closesocket(s);
1.19      daniel    719:            errno = status;
                    720:            return (-1);
                    721:        }
                    722:     } else {
                    723:        /* pbm */
                    724:        return (-1);
                    725:     }
1.1       daniel    726:     
1.5       daniel    727:     return(s);
1.1       daniel    728: }
                    729:  
1.5       daniel    730: /**
                    731:  * xmlNanoHTTPConnectHost:
                    732:  * @host:  the host name
                    733:  * @port:  the port number
                    734:  *
                    735:  * Attempt a connection to the given host:port endpoint. It tries
                    736:  * the multiple IP provided by the DNS if available.
                    737:  *
                    738:  * Returns -1 in case of failure, the file descriptor number otherwise
                    739:  */
                    740: 
                    741: static int
                    742: xmlNanoHTTPConnectHost(const char *host, int port)
1.1       daniel    743: {
                    744:     struct hostent *h;
                    745:     int i;
                    746:     int s;
                    747:     
                    748:     h=gethostbyname(host);
1.26      veillard  749:     if (h==NULL)
1.1       daniel    750:     {
1.5       daniel    751: #ifdef DEBUG_HTTP
1.1       daniel    752:        fprintf(stderr,"unable to resolve '%s'.\n", host);
1.5       daniel    753: #endif
1.1       daniel    754:        return(-1);
                    755:     }
                    756:     
                    757:     for(i=0; h->h_addr_list[i]; i++)
                    758:     {
                    759:        struct in_addr ia;
                    760:        memcpy(&ia, h->h_addr_list[i],4);
                    761:        s = xmlNanoHTTPConnectAttempt(ia, port);
1.26      veillard  762:        if (s != -1)
1.5       daniel    763:            return(s);
1.1       daniel    764:     }
1.5       daniel    765: 
                    766: #ifdef DEBUG_HTTP
1.1       daniel    767:     fprintf(stderr, "unable to connect to '%s'.\n", host);
1.5       daniel    768: #endif
1.1       daniel    769:     return(-1);
                    770: }
                    771: 
                    772: 
1.5       daniel    773: /**
                    774:  * xmlNanoHTTPOpen:
                    775:  * @URL:  The URL to load
                    776:  * @contentType:  if available the Content-Type information will be
                    777:  *                returned at that location
                    778:  *
                    779:  * This function try to open a connection to the indicated resource
                    780:  * via HTTP GET.
                    781:  *
1.6       daniel    782:  * Returns NULL in case of failure, otherwise a request handler.
                    783:  *     The contentType, if provided must be freed by the caller
1.5       daniel    784:  */
1.1       daniel    785: 
1.18      daniel    786: void*
1.1       daniel    787: xmlNanoHTTPOpen(const char *URL, char **contentType) {
1.5       daniel    788:     if (contentType != NULL) *contentType = NULL;
1.33    ! veillard  789:     return xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL);
1.1       daniel    790: }
                    791: 
1.5       daniel    792: /**
                    793:  * xmlNanoHTTPRead:
                    794:  * @ctx:  the HTTP context
                    795:  * @dest:  a buffer
                    796:  * @len:  the buffer length
                    797:  *
                    798:  * This function tries to read @len bytes from the existing HTTP connection
                    799:  * and saves them in @dest. This is a blocking call.
                    800:  *
                    801:  * Returns the number of byte read. 0 is an indication of an end of connection.
                    802:  *         -1 indicates a parameter error.
                    803:  */
1.1       daniel    804: int
                    805: xmlNanoHTTPRead(void *ctx, void *dest, int len) {
                    806:     xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
                    807: 
                    808:     if (ctx == NULL) return(-1);
                    809:     if (dest == NULL) return(-1);
                    810:     if (len <= 0) return(0);
                    811: 
                    812:     while (ctxt->inptr - ctxt->inrptr < len) {
                    813:         if (xmlNanoHTTPRecv(ctxt) == 0) break;
                    814:     }
                    815:     if (ctxt->inptr - ctxt->inrptr < len)
                    816:         len = ctxt->inptr - ctxt->inrptr;
                    817:     memcpy(dest, ctxt->inrptr, len);
                    818:     ctxt->inrptr += len;
                    819:     return(len);
                    820: }
                    821: 
1.5       daniel    822: /**
                    823:  * xmlNanoHTTPClose:
                    824:  * @ctx:  the HTTP context
                    825:  *
                    826:  * This function closes an HTTP context, it ends up the connection and
                    827:  * free all data related to it.
                    828:  */
1.1       daniel    829: void
                    830: xmlNanoHTTPClose(void *ctx) {
                    831:     xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
                    832: 
                    833:     if (ctx == NULL) return;
                    834: 
                    835:     xmlNanoHTTPFreeCtxt(ctxt);
                    836: }
                    837: 
1.5       daniel    838: /**
1.6       daniel    839:  * xmlNanoHTTPMethod:
                    840:  * @URL:  The URL to load
                    841:  * @method:  the HTTP method to use
                    842:  * @input:  the input string if any
                    843:  * @contentType:  the Content-Type information IN and OUT
                    844:  * @headers:  the extra headers
                    845:  *
                    846:  * This function try to open a connection to the indicated resource
                    847:  * via HTTP using the given @method, adding the given extra headers
                    848:  * and the input buffer for the request content.
                    849:  *
                    850:  * Returns NULL in case of failure, otherwise a request handler.
                    851:  *     The contentType, if provided must be freed by the caller
                    852:  */
                    853: 
1.18      daniel    854: void*
1.6       daniel    855: xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
                    856:                   char **contentType, const char *headers) {
                    857:     xmlNanoHTTPCtxtPtr ctxt;
1.33    ! veillard  858:     char *bp, *p;
        !           859:     int blen, ilen, ret;
1.6       daniel    860:     int head;
                    861:     int nbRedirects = 0;
                    862:     char *redirURL = NULL;
                    863:     
                    864:     if (URL == NULL) return(NULL);
                    865:     if (method == NULL) method = "GET";
1.33    ! veillard  866:     xmlNanoHTTPInit();
1.6       daniel    867: 
                    868: retry:
                    869:     if (redirURL == NULL)
                    870:        ctxt = xmlNanoHTTPNewCtxt(URL);
                    871:     else {
                    872:        ctxt = xmlNanoHTTPNewCtxt(redirURL);
1.7       daniel    873:        xmlFree(redirURL);
1.6       daniel    874:        redirURL = NULL;
                    875:     }
                    876: 
                    877:     if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
                    878:         xmlNanoHTTPFreeCtxt(ctxt);
1.7       daniel    879:        if (redirURL != NULL) xmlFree(redirURL);
1.6       daniel    880:         return(NULL);
                    881:     }
                    882:     if (ctxt->hostname == NULL) {
                    883:         xmlNanoHTTPFreeCtxt(ctxt);
                    884:         return(NULL);
                    885:     }
1.33    ! veillard  886:     if (proxy) {
        !           887:        blen = strlen(ctxt->hostname) * 2 + 16;
        !           888:        ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
        !           889:     }
        !           890:     else {
        !           891:        blen = strlen(ctxt->hostname);
        !           892:        ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
        !           893:     }
1.6       daniel    894:     if (ret < 0) {
                    895:         xmlNanoHTTPFreeCtxt(ctxt);
                    896:         return(NULL);
                    897:     }
                    898:     ctxt->fd = ret;
                    899: 
1.33    ! veillard  900:     if (input != NULL) {
        !           901:        ilen = strlen(input);
        !           902:        blen += ilen + 32;
        !           903:     }
        !           904:     else
        !           905:        ilen = 0;
        !           906:     if (headers != NULL)
        !           907:        blen += strlen(headers);
        !           908:     if (contentType && *contentType)
        !           909:        blen += strlen(*contentType) + 16;
        !           910:     blen += strlen(method) + strlen(ctxt->path) + 23;
        !           911:     bp = xmlMalloc(blen);
        !           912:     if (proxy) {
        !           913:        if (ctxt->port != 80) {
        !           914:            sprintf(bp, "%s http://%s:%d%s", method, ctxt->hostname,
        !           915:                 ctxt->port, ctxt->path);
1.6       daniel    916:        }
1.33    ! veillard  917:        else
        !           918:            sprintf(bp, "%s http://%s%s", method, ctxt->hostname, ctxt->path);
        !           919:     }
        !           920:     else
        !           921:        sprintf(bp, "%s %s", method, ctxt->path);
        !           922:     p = bp + strlen(bp);
        !           923:     sprintf(p, " HTTP/1.0\r\nHost: %s\r\n", ctxt->hostname);
        !           924:     p += strlen(p);
        !           925:     if (contentType != NULL && *contentType) {
        !           926:        sprintf(p, "Content-Type: %s\r\n", *contentType);
        !           927:        p += strlen(p);
        !           928:     }
        !           929:     if (headers != NULL) {
        !           930:        strcpy(p, headers);
        !           931:        p += strlen(p);
1.6       daniel    932:     }
1.33    ! veillard  933:     if (input != NULL)
        !           934:        sprintf(p, "Content-Length: %d\r\n\r\n%s", ilen, input);
        !           935:     else
        !           936:        strcpy(p, "\r\n");
1.6       daniel    937: #ifdef DEBUG_HTTP
1.33    ! veillard  938:     printf("-> %s%s", proxy? "(Proxy) " : "", bp);
        !           939:     if ((blen -= strlen(bp)+1) < 0)
        !           940:        printf("ERROR: overflowed buffer by %d bytes\n", -blen);
1.6       daniel    941: #endif
1.33    ! veillard  942:     ctxt->outptr = ctxt->out = bp;
1.6       daniel    943:     ctxt->state = XML_NANO_HTTP_WRITE;
                    944:     xmlNanoHTTPSend(ctxt);
                    945:     ctxt->state = XML_NANO_HTTP_READ;
                    946:     head = 1;
                    947: 
                    948:     while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
                    949:         if (head && (*p == 0)) {
                    950:            head = 0;
                    951:            ctxt->content = ctxt->inrptr;
1.33    ! veillard  952:            xmlFree(p);
1.6       daniel    953:            break;
                    954:        }
                    955:        xmlNanoHTTPScanAnswer(ctxt, p);
                    956: 
                    957: #ifdef DEBUG_HTTP
1.33    ! veillard  958:        printf("<- %s\n", p);
1.6       daniel    959: #endif
1.33    ! veillard  960:         xmlFree(p);
1.6       daniel    961:     }
                    962: 
                    963:     if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
                    964:         (ctxt->returnValue < 400)) {
                    965: #ifdef DEBUG_HTTP
                    966:        printf("\nRedirect to: %s\n", ctxt->location);
                    967: #endif
                    968:        while (xmlNanoHTTPRecv(ctxt)) ;
                    969:         if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
                    970:            nbRedirects++;
1.7       daniel    971:            redirURL = xmlMemStrdup(ctxt->location);
1.6       daniel    972:            xmlNanoHTTPFreeCtxt(ctxt);
                    973:            goto retry;
                    974:        }
                    975:        xmlNanoHTTPFreeCtxt(ctxt);
                    976: #ifdef DEBUG_HTTP
1.33    ! veillard  977:        printf("Too many redirects, aborting ...\n");
1.6       daniel    978: #endif
                    979:        return(NULL);
                    980: 
                    981:     }
                    982: 
1.33    ! veillard  983:     if (contentType != NULL) {
        !           984:        if (ctxt->contentType != NULL)
        !           985:            *contentType = xmlMemStrdup(ctxt->contentType);
        !           986:        else
        !           987:            *contentType = NULL;
        !           988:     }
1.6       daniel    989: 
                    990: #ifdef DEBUG_HTTP
                    991:     if (ctxt->contentType != NULL)
                    992:        printf("\nCode %d, content-type '%s'\n\n",
                    993:               ctxt->returnValue, ctxt->contentType);
                    994:     else
                    995:        printf("\nCode %d, no content-type\n\n",
                    996:               ctxt->returnValue);
                    997: #endif
                    998: 
                    999:     return((void *) ctxt);
                   1000: }
                   1001: 
                   1002: /**
1.5       daniel   1003:  * xmlNanoHTTPFetch:
                   1004:  * @URL:  The URL to load
                   1005:  * @filename:  the filename where the content should be saved
                   1006:  * @contentType:  if available the Content-Type information will be
                   1007:  *                returned at that location
                   1008:  *
                   1009:  * This function try to fetch the indicated resource via HTTP GET
                   1010:  * and save it's content in the file.
                   1011:  *
                   1012:  * Returns -1 in case of failure, 0 incase of success. The contentType,
                   1013:  *     if provided must be freed by the caller
                   1014:  */
                   1015: int
                   1016: xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
1.1       daniel   1017:     void *ctxt;
                   1018:     char buf[4096];
                   1019:     int fd;
                   1020:     int len;
                   1021:     
                   1022:     ctxt = xmlNanoHTTPOpen(URL, contentType);
                   1023:     if (ctxt == NULL) return(-1);
                   1024: 
                   1025:     if (!strcmp(filename, "-")) 
                   1026:         fd = 0;
                   1027:     else {
1.13      daniel   1028:         fd = open(filename, O_CREAT | O_WRONLY, 00644);
1.1       daniel   1029:        if (fd < 0) {
                   1030:            xmlNanoHTTPClose(ctxt);
1.5       daniel   1031:            if ((contentType != NULL) && (*contentType != NULL)) {
1.7       daniel   1032:                xmlFree(*contentType);
1.5       daniel   1033:                *contentType = NULL;
                   1034:            }
1.1       daniel   1035:            return(-1);
                   1036:        }
                   1037:     }
                   1038: 
                   1039:     while ((len = xmlNanoHTTPRead(ctxt, buf, sizeof(buf))) > 0) {
                   1040:        write(fd, buf, len);
                   1041:     }
                   1042: 
                   1043:     xmlNanoHTTPClose(ctxt);
1.13      daniel   1044:     close(fd);
1.1       daniel   1045:     return(0);
1.6       daniel   1046: }
                   1047: 
                   1048: /**
                   1049:  * xmlNanoHTTPSave:
1.8       daniel   1050:  * @ctxt:  the HTTP context
1.6       daniel   1051:  * @filename:  the filename where the content should be saved
                   1052:  *
                   1053:  * This function saves the output of the HTTP transaction to a file
                   1054:  * It closes and free the context at the end
                   1055:  *
                   1056:  * Returns -1 in case of failure, 0 incase of success.
                   1057:  */
                   1058: int
                   1059: xmlNanoHTTPSave(void *ctxt, const char *filename) {
                   1060:     char buf[4096];
                   1061:     int fd;
                   1062:     int len;
                   1063:     
                   1064:     if (ctxt == NULL) return(-1);
                   1065: 
                   1066:     if (!strcmp(filename, "-")) 
                   1067:         fd = 0;
                   1068:     else {
                   1069:         fd = open(filename, O_CREAT | O_WRONLY);
                   1070:        if (fd < 0) {
                   1071:            xmlNanoHTTPClose(ctxt);
                   1072:            return(-1);
                   1073:        }
                   1074:     }
                   1075: 
                   1076:     while ((len = xmlNanoHTTPRead(ctxt, buf, sizeof(buf))) > 0) {
                   1077:        write(fd, buf, len);
                   1078:     }
                   1079: 
                   1080:     xmlNanoHTTPClose(ctxt);
                   1081:     return(0);
                   1082: }
                   1083: 
                   1084: /**
                   1085:  * xmlNanoHTTPReturnCode:
                   1086:  * @ctx:  the HTTP context
                   1087:  *
                   1088:  * Returns the HTTP return code for the request.
                   1089:  */
                   1090: int
                   1091: xmlNanoHTTPReturnCode(void *ctx) {
                   1092:     xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
                   1093: 
                   1094:     if (ctxt == NULL) return(-1);
                   1095: 
                   1096:     return(ctxt->returnValue);
1.1       daniel   1097: }
                   1098: 
                   1099: #ifdef STANDALONE
                   1100: int main(int argc, char **argv) {
                   1101:     char *contentType = NULL;
                   1102: 
                   1103:     if (argv[1] != NULL) {
                   1104:        if (argv[2] != NULL) 
                   1105:            xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
                   1106:         else
                   1107:            xmlNanoHTTPFetch(argv[1], "-", &contentType);
1.7       daniel   1108:        if (contentType != NULL) xmlFree(contentType);
1.1       daniel   1109:     } else {
                   1110:         printf("%s: minimal HTTP GET implementation\n", argv[0]);
                   1111:         printf("\tusage %s [ URL [ filename ] ]\n", argv[0]);
                   1112:     }
1.12      daniel   1113:     xmlNanoHTTPCleanup();
                   1114:     xmlMemoryDump();
1.1       daniel   1115:     return(0);
                   1116: }
                   1117: #endif /* STANDALONE */
1.17      daniel   1118: #else /* !LIBXML_HTTP_ENABLED */
                   1119: #ifdef STANDALONE
                   1120: #include <stdio.h>
                   1121: int main(int argc, char **argv) {
                   1122:     printf("%s : HTTP support not compiled in\n", argv[0]);
                   1123:     return(0);
                   1124: }
                   1125: #endif /* STANDALONE */
                   1126: #endif /* LIBXML_HTTP_ENABLED */

Webmaster