Annotation of libwww/Library/src/HTHost.c, revision 2.12.2.2

2.1       frystyk     1: /*                                                                    HTHost.c
                      2: **     REMOTE HOST INFORMATION
                      3: **
                      4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
2.12.2.2! frystyk     6: **     @(#) $Id: HTHost.c,v 2.12.2.1 1996/10/29 21:26:59 eric Exp $
2.1       frystyk     7: **
                      8: **     This object manages the information that we know about a remote host.
                      9: **     This can for example be what type of host it is, and what version
                     10: **     it is using. We also keep track of persistent connections
                     11: **
                     12: **     April 96  HFN   Written
                     13: */
                     14: 
                     15: /* Library include files */
                     16: #include "sysdep.h"
                     17: #include "WWWUtil.h"
2.12.2.2! frystyk    18: #include "WWWMux.h"
2.1       frystyk    19: #include "HTParse.h"
                     20: #include "HTAlert.h"
                     21: #include "HTError.h"
                     22: #include "HTNetMan.h"
                     23: #include "HTTrans.h"
2.12.2.1  eric       24: #include "HTDNS.h"
2.1       frystyk    25: #include "HTHost.h"                                     /* Implemented here */
2.12.2.1  eric       26: #include "HTHstMan.h"
                     27: 
                     28: #include "HTTPUtil.h"
                     29: #include "HTTCP.h"
                     30: #include "HTWatch.h"
2.1       frystyk    31: 
                     32: #define HOST_TIMEOUT           43200L       /* Default host timeout is 12 h */
                     33: #define TCP_TIMEOUT            3600L           /* Default TCP timeout i 1 h */
                     34: #define HASH_SIZE              67
2.12.2.1  eric       35: #define MAX_PIPES              5    /* maximum number of pipelined requests */
2.1       frystyk    36: 
2.12.2.1  eric       37: struct _HTInputStream {
                     38:     const HTInputStreamClass * isa;
2.1       frystyk    39: };
                     40: 
2.12.2.1  eric       41: PRIVATE int HostEvent(SOCKET soc, void * pVoid, HTEvent_type type);
                     42: 
                     43: /* Type definitions and global variables etc. local to this module */
2.1       frystyk    44: PRIVATE time_t HostTimeout = HOST_TIMEOUT;       /* Timeout on host entries */
                     45: PRIVATE time_t TCPTimeout = TCP_TIMEOUT;  /* Timeout on persistent channels */
                     46: 
2.8       frystyk    47: PRIVATE HTList ** HostTable = NULL;
                     48: PRIVATE HTList * PendHost = NULL;          /* List of pending host elements */
2.1       frystyk    49: 
                     50: /* ------------------------------------------------------------------------- */
                     51: 
                     52: PRIVATE void free_object (HTHost * me)
                     53: {
                     54:     if (me) {
                     55:        HT_FREE(me->hostname);
                     56:        HT_FREE(me->type);
2.12      frystyk    57:        HT_FREE(me->server);
                     58:        HT_FREE(me->user_agent);
                     59:        HT_FREE(me->range_units);
2.3       eric       60:        if (me->channel) {
2.5       eric       61:            HTChannel_delete(me->channel, HT_OK);
2.3       eric       62:            me->channel = NULL;
                     63:        }
2.8       frystyk    64:        HTList_delete(me->pipeline);
                     65:        HTList_delete(me->pending);
2.1       frystyk    66:        HT_FREE(me);
                     67:     }
                     68: }
                     69: 
                     70: PRIVATE BOOL delete_object (HTList * list, HTHost * me)
                     71: {
2.2       frystyk    72:     if (CORE_TRACE) HTTrace("Host info... object %p from list %p\n", me, list);
2.1       frystyk    73:     HTList_removeObject(list, (void *) me);
                     74:     free_object(me);
                     75:     return YES;
                     76: }
                     77: 
2.12.2.1  eric       78: PRIVATE BOOL isLastInPipe (HTHost * host, HTNet * net)
                     79: {
                     80:     return HTList_lastObject(host->pipeline) == net;
                     81: }
                     82: 
                     83: /*
                     84: **     HostEvent - host event manager - recieves events from the event 
                     85: **     manager and dispatches them to the client net objects by calling the 
                     86: **     net object's cbf.
                     87: **
                     88: */
                     89: PRIVATE int HostEvent (SOCKET soc, void * pVoid, HTEvent_type type)
                     90: {
                     91:     HTHost * host = (HTHost *)pVoid;
                     92: 
                     93:     if (type == HTEvent_READ) {
                     94:        HTNet * targetNet;
                     95: 
                     96:        /* call the first net object */
                     97:        do {
                     98:            int ret;
                     99:            targetNet = (HTNet *)HTList_firstObject(host->pipeline);
                    100:            if (targetNet) {
                    101:                HTWATCH(HTWatch_TCP, host, HTHIDE("HostEvent: READ passed to %s.\n"), 
                    102:                        HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
                    103:                if ((ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), 
                    104:                                                  targetNet->event.param, type)) != HT_OK)
                    105:                    return ret;
                    106:            }
                    107:        /* call pipelined net object to eat all the data in the channel */
                    108:        } while (host->remainingRead > 0);
                    109: 
                    110:        /* last target net should have set remainingRead to 0 */
                    111:        if (targetNet)
                    112:            return HT_OK;
                    113: 
                    114:        /* If there was notargetNet, it should be a close */
                    115:        HTWATCH(HTWatch_TCP, host, HTHIDE("HostEvent: host %s closed connection.\n"), 
                    116:                host->hostname);
                    117: 
                    118:        /* Is there garbage in the channel? Let's check: */
                    119:        {
                    120:            char buf[256];
                    121:            int ret;
                    122:            while ((ret = NETREAD(HTChannel_socket(host->channel), buf, sizeof(buf))) > 0)
                    123:                HTWATCH(HTWatch_TCP, host, HTHIDE("Host %s had %d extraneous bytes.\n"));
                    124:        }
                    125:        HTHost_clearChannel(host, HT_OK);
                    126:        return HT_OK; /* extra garbage does not constitute an application error */
                    127:        
                    128:     } else if (type == HTEvent_WRITE) {
                    129:            HTNet * targetNet = (HTNet *)HTList_lastObject(host->pipeline);
                    130:        if (targetNet) {
                    131:            HTWATCH(HTWatch_TCP, host, HTHIDE("HostEvent: WRITE passed to %s.\n"), 
                    132:                    HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
                    133:            return (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, type);
                    134:        }
                    135:        HTWATCH(HTWatch_TCP, host, HTHIDE("HostEvent: Who wants to write to %s?\n"), 
                    136:                host->hostname);
                    137:        return HT_ERROR;
                    138:        }
                    139: 
                    140:     HTWATCH(HTWatch_TCP, host, HTHIDE("Don't know how to handle OOB data from %s?\n"), 
                    141:            host->hostname);
                    142:     return HT_OK;
                    143: }
                    144: 
2.1       frystyk   145: /*
                    146: **     Search the host info cache for a host object or create a new one
                    147: **     and add it. Examples of host names are
                    148: **
                    149: **             www.w3.org
                    150: **             www.foo.com:8000
                    151: **             18.52.0.18
                    152: **
                    153: **     Returns Host object or NULL if error. You may get back an already
                    154: **     existing host object - you're not guaranteed a new one each time.
                    155: */
2.12.2.1  eric      156: 
2.1       frystyk   157: PUBLIC HTHost * HTHost_new (char * host)
                    158: {
                    159:     HTList * list = NULL;                          /* Current list in cache */
                    160:     HTHost * pres = NULL;
                    161:     if (!host) {
2.2       frystyk   162:        if (CORE_TRACE) HTTrace("Host info... Bad argument\n");
2.1       frystyk   163:        return NULL;
                    164:     }
                    165:     
                    166:     /* Find a hash for this host */
                    167:     {
                    168:        int hash = 0;
                    169:        char *ptr;
                    170:        for (ptr=host; *ptr; ptr++)
                    171:            hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HASH_SIZE);
                    172:        if (!HostTable) {
                    173:            if ((HostTable = (HTList **) HT_CALLOC(HASH_SIZE,
                    174:                                                   sizeof(HTList *))) == NULL)
                    175:                HT_OUTOFMEM("HTHost_find");
                    176:        }
                    177:        if (!HostTable[hash]) HostTable[hash] = HTList_new();
                    178:        list = HostTable[hash];
                    179:     }
                    180: 
                    181:     /* Search the cache */
                    182:     {
                    183:        HTList * cur = list;
                    184:        while ((pres = (HTHost *) HTList_nextObject(cur))) {
                    185:            if (!strcmp(pres->hostname, host)) {
2.8       frystyk   186:                if (HTHost_isIdle(pres) && time(NULL)>pres->ntime+HostTimeout){
2.2       frystyk   187:                    if (CORE_TRACE)
2.1       frystyk   188:                        HTTrace("Host info... Collecting host info %p\n",pres);
                    189:                    delete_object(list, pres);
                    190:                    pres = NULL;
                    191:                }
                    192:                break;
                    193:            }
                    194:        }
                    195:     }
                    196: 
2.8       frystyk   197:     /* If not found then create new Host object, else use existing one */
2.1       frystyk   198:     if (pres) {
                    199:        if (pres->channel) {
                    200:            if (pres->expires < time(NULL)) {      /* Cached channel is cold */
2.2       frystyk   201:                if (CORE_TRACE)
2.1       frystyk   202:                    HTTrace("Host info... Persistent channel %p gotten cold\n",
                    203:                            pres->channel);
2.5       eric      204:                HTChannel_delete(pres->channel, HT_OK);
2.1       frystyk   205:                pres->channel = NULL;
                    206:            } else {
2.2       frystyk   207:                if (CORE_TRACE)
2.1       frystyk   208:                    HTTrace("Host info... REUSING CHANNEL %p\n",pres->channel);
                    209:            }
                    210:        }
                    211:     } else {
2.12.2.1  eric      212:        /* temporary event structure to COPY into host structure */
                    213:        HTEvent newEvent = {HT_PRIORITY_MAX, HostEvent, pres, NULL};
                    214: 
2.1       frystyk   215:        if ((pres = (HTHost *) HT_CALLOC(1, sizeof(HTHost))) == NULL)
                    216:            HT_OUTOFMEM("HTHost_add");
                    217:        StrAllocCopy(pres->hostname, host);
                    218:        pres->ntime = time(NULL);
2.8       frystyk   219:        pres->mode = HT_TP_SINGLE;
2.12.2.1  eric      220:        newEvent.param = pres;
                    221:        pres->events[HTEvent_INDEX(HTEvent_READ)] = newEvent;
                    222:        pres->events[HTEvent_INDEX(HTEvent_WRITE)] = newEvent;
2.2       frystyk   223:        if (CORE_TRACE) 
2.1       frystyk   224:            HTTrace("Host info... added `%s\' to list %p\n", host, list);
                    225:        HTList_addObject(list, (void *) pres);
                    226:     }
                    227:     return pres;
2.9       frystyk   228: }
                    229: 
2.12.2.1  eric      230: PUBLIC HTHost * HTHost_newWParse (HTRequest * request, char * url, u_short default_port)
                    231: {
                    232:              char * port;
                    233:              char * fullhost = NULL;
                    234:              char * parsedHost = NULL;
                    235:              SockA * sin;
                    236:              HTHost * me;
                    237:              char * proxy = HTRequest_proxy(request);
                    238: 
                    239:              fullhost = HTParse(proxy ? proxy : url, "", PARSE_HOST);
                    240: 
                    241:              /* If there's an @ then use the stuff after it as a hostname */
                    242:              if (fullhost) {
                    243:                  char * at_sign;
                    244:                  if ((at_sign = strchr(fullhost, '@')) != NULL)
                    245:                      parsedHost = at_sign+1;
                    246:                  else
                    247:                      parsedHost = fullhost;
                    248:              }
                    249:              if (!parsedHost || !*parsedHost) {
                    250:                  HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_HOST,
                    251:                                     NULL, 0, "HTDoConnect");
                    252:                  HT_FREE(fullhost);
                    253:                  return NULL;
                    254:              }
                    255:              port = strchr(parsedHost, ':');
                    256:              if (PROT_TRACE)
                    257:                  HTTrace("HTDoConnect. Looking up `%s\'\n", parsedHost);
                    258:              if (port) {
                    259:                  *port++ = '\0';
                    260:                  if (!*port || !isdigit(*port))
                    261:                      port = 0;
                    262:              }
                    263:              /* Find information about this host */
                    264:              if ((me = HTHost_new(parsedHost)) == NULL) {
                    265:                  if (PROT_TRACE)HTTrace("HTDoConnect. Can't get host info\n");
                    266:                  me->tcpstate = TCP_ERROR;
                    267:                  return NULL;
                    268:              }
                    269:              HT_FREE(fullhost);        /* parsedHost points into fullhost */
                    270:              sin = &me->sock_addr;
                    271:              memset((void *) sin, '\0', sizeof(SockA));
                    272: 
                    273: #ifdef DECNET
                    274:              sin->sdn_family = AF_DECnet;
                    275:              net->sock_addr.sdn_objnum = port ? (unsigned char)(strtol(port, (char **) 0, 10)) : DNP_OBJ;
                    276: #else  /* Internet */
                    277:              sin->sin_family = AF_INET;
                    278:              sin->sin_port = htons(port ? atol(port) : default_port);
                    279: #endif
                    280:              return me;
                    281: }
                    282: 
2.9       frystyk   283: /*
                    284: **     Search the host info cache for a host object. Examples of host names:
                    285: **
                    286: **             www.w3.org
                    287: **             www.foo.com:8000
                    288: **             18.52.0.18
                    289: **
                    290: **     Returns Host object or NULL if not found.
                    291: */
                    292: PUBLIC HTHost * HTHost_find (char * host)
                    293: {
                    294:     HTList * list = NULL;                          /* Current list in cache */
                    295:     HTHost * pres = NULL;
                    296:     if (CORE_TRACE)
                    297:        HTTrace("Host info... Looking for `%s\'\n", host ? host : "<null>");
                    298: 
                    299:     /* Find a hash for this host */
                    300:     if (host && HostTable) {
                    301:        int hash = 0;
                    302:        char *ptr;
                    303:        for (ptr=host; *ptr; ptr++)
                    304:            hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HASH_SIZE);
                    305:        if (!HostTable[hash]) return NULL;
                    306:        list = HostTable[hash];
                    307: 
                    308:        /* Search the cache */
                    309:        {
                    310:            HTList * cur = list;
                    311:            while ((pres = (HTHost *) HTList_nextObject(cur))) {
                    312:                if (!strcmp(pres->hostname, host)) {
                    313:                    if (time(NULL) > pres->ntime + HostTimeout) {
                    314:                        if (CORE_TRACE)
                    315:                            HTTrace("Host info... Collecting host %p\n", pres);
                    316:                        delete_object(list, pres);
                    317:                        pres = NULL;
                    318:                    } else {
                    319:                        if (CORE_TRACE)
                    320:                            HTTrace("Host info... Found `%s\'\n", host);
                    321:                    }
                    322:                    return pres;
                    323:                }
                    324:            }
                    325:        }
                    326:     }
                    327:     return NULL;
2.1       frystyk   328: }
                    329: 
                    330: /*
2.8       frystyk   331: **     Get and set the hostname of the remote host
                    332: */
                    333: PUBLIC char * HTHost_name (HTHost * host)
                    334: {
                    335:      return host ? host->hostname : NULL;
                    336: }
                    337: 
                    338: /*
2.1       frystyk   339: **     Get and set the type class of the remote host
                    340: */
                    341: PUBLIC char * HTHost_class (HTHost * host)
                    342: {
                    343:      return host ? host->type : NULL;
                    344: }
                    345: 
                    346: PUBLIC void HTHost_setClass (HTHost * host, char * s_class)
                    347: {
                    348:     if (host && s_class) StrAllocCopy(host->type, s_class);
                    349: }
                    350: 
                    351: /*
                    352: **     Get and set the version of the remote host
                    353: */
                    354: PUBLIC int HTHost_version (HTHost *host)
                    355: {
                    356:      return host ? host->version : 0;
                    357: }
                    358: 
                    359: PUBLIC void HTHost_setVersion (HTHost * host, int version)
                    360: {
                    361:     if (host) host->version = version;
                    362: }
                    363: 
                    364: /*
                    365: **     Get and set the cache timeout for persistent entries.
                    366: **     The default value is TCP_TIMEOUT
                    367: */
                    368: PUBLIC void HTHost_setPersistTimeout (time_t timeout)
                    369: {
                    370:     TCPTimeout = timeout;
                    371: }
                    372: 
                    373: PUBLIC time_t HTHost_persistTimeout (time_t timeout)
                    374: {
                    375:     return TCPTimeout;
                    376: }
                    377: 
                    378: /*     Persistent Connection Expiration
                    379: **     --------------------------------
                    380: **     Should normally not be used. If, then use calendar time.
                    381: */
                    382: PUBLIC void HTHost_setPersistExpires (HTHost * host, time_t expires)
                    383: {
                    384:     if (host) host->expires = expires;
                    385: }
                    386: 
                    387: PUBLIC time_t HTHost_persistExpires (HTHost * host)
                    388: {
                    389:     return host ? host->expires : -1;
                    390: }
                    391: 
                    392: /*
2.6       frystyk   393: **     Public methods for this host
                    394: */
                    395: PUBLIC HTMethod HTHost_publicMethods (HTHost * me)
                    396: {
                    397:     return me ? me->methods : METHOD_INVALID;
                    398: }
                    399: 
                    400: PUBLIC void HTHost_setPublicMethods (HTHost * me, HTMethod methodset)
                    401: {
                    402:     if (me) me->methods = methodset;
                    403: }
                    404: 
                    405: PUBLIC void HTHost_appendPublicMethods (HTHost * me, HTMethod methodset)
                    406: {
                    407:     if (me) me->methods |= methodset;
                    408: }
                    409: 
                    410: /*
                    411: **     Get and set the server name of the remote host
                    412: */
                    413: PUBLIC char * HTHost_server (HTHost * host)
                    414: {
                    415:      return host ? host->server : NULL;
                    416: }
                    417: 
                    418: PUBLIC BOOL HTHost_setServer (HTHost * host, const char * server)
                    419: {
                    420:     if (host && server) {
                    421:        StrAllocCopy(host->server, server);
                    422:        return YES;
                    423:     }
                    424:     return NO;
                    425: }
                    426: 
                    427: /*
                    428: **     Get and set the userAgent name of the remote host
                    429: */
                    430: PUBLIC char * HTHost_userAgent (HTHost * host)
                    431: {
                    432:      return host ? host->user_agent : NULL;
                    433: }
                    434: 
                    435: PUBLIC BOOL HTHost_setUserAgent (HTHost * host, const char * userAgent)
                    436: {
                    437:     if (host && userAgent) {
                    438:        StrAllocCopy(host->user_agent, userAgent);
                    439:        return YES;
2.12      frystyk   440:     }
                    441:     return NO;
                    442: }
                    443: 
                    444: /*
                    445: **     Get and set acceptable range units
                    446: */
                    447: PUBLIC char * HTHost_rangeUnits (HTHost * host)
                    448: {
                    449:      return host ? host->range_units : NULL;
                    450: }
                    451: 
                    452: PUBLIC BOOL HTHost_setRangeUnits (HTHost * host, const char * units)
                    453: {
                    454:     if (host && units) {
                    455:        StrAllocCopy(host->range_units, units);
                    456:        return YES;
                    457:     }
                    458:     return NO;
                    459: }
                    460: 
                    461: /*
                    462: **     Checks whether a specific range unit is OK. We always say
                    463: **     YES except if we have a specific statement from the server that
                    464: **     it doesn't understand byte ranges - that is - it has sent "none"
                    465: **     in a "Accept-Range" response header
                    466: */
                    467: PUBLIC BOOL HTHost_isRangeUnitAcceptable (HTHost * host, const char * unit)
                    468: {
                    469:     if (host && unit) {
                    470: #if 0
                    471:        if (host->range_units) {
                    472:            char * start = strcasestr(host->range_units, "none");
                    473: 
                    474:            /*
                    475:            **  Check that "none" is infact a token. It could be part of some
                    476:            **  other valid string, so we'd better check for it.
                    477:            */
                    478:            if (start) {
                    479:                
                    480:                
                    481:            }
                    482:            return NO;
                    483:        }
                    484: #endif
                    485:        return strcasecomp(unit, "bytes") ? NO : YES;
2.6       frystyk   486:     }
                    487:     return NO;
                    488: }
                    489: 
2.1       frystyk   490: /*     HTHost_catchClose
                    491: **     -----------------
                    492: **     This function is registered when the socket is idle so that we get
                    493: **     a notification if the socket closes at the other end. At this point
                    494: **     we can't use the request object as it might have been freed a long
                    495: **     time ago.
                    496: */
2.12.2.1  eric      497: PUBLIC int HTHost_catchClose (SOCKET soc, void * context, HTEvent_type type)
2.1       frystyk   498: {
2.12.2.1  eric      499:     HTNet * net = (HTNet *)context;
                    500:     HTHost * host = net->host;
2.2       frystyk   501:     if (CORE_TRACE)
2.12.2.1  eric      502:        HTTrace("Catch Close. called with socket %d with type %x\n",
                    503:                soc, type);
                    504:     if (type == HTEvent_READ) {
2.1       frystyk   505:        HTChannel * ch = HTChannel_find(soc);     /* Find associated channel */
2.8       frystyk   506:        HTHost * host = HTChannel_host(ch);           /* and associated host */
2.1       frystyk   507:        if (ch && host) {           
2.2       frystyk   508:            if (CORE_TRACE) HTTrace("Catch Close. CLOSING socket %d\n", soc);
2.8       frystyk   509:            HTHost_clearChannel(host, HT_OK);
2.1       frystyk   510:        } else {
2.2       frystyk   511:            if (CORE_TRACE) HTTrace("Catch Close. socket %d NOT FOUND!\n",soc);
2.1       frystyk   512:        }
                    513:     }
2.12.2.1  eric      514:     HTHost_unregister(host, net, HTEvent_CLOSE);
2.1       frystyk   515:     return HT_OK;
                    516: }
                    517: 
                    518: /*
                    519: **     As soon as we know that this host accepts persistent connections,
                    520: **     we associated the channel with the host. 
                    521: **     We don't want more than MaxSockets-2 connections to be persistent in
                    522: **     order to avoid deadlock.
                    523: */
2.12.2.1  eric      524: PUBLIC BOOL HTHost_setPersistent (HTHost *             host,
                    525:                                  BOOL                  persistent,
                    526:                                  HTTransportMode       mode)
2.1       frystyk   527: {
2.12.2.1  eric      528:     if (!host) return NO;
                    529: 
                    530:     if (!persistent) {
                    531:        /*
                    532:        **  We use the HT_IGNORE status code as we don't want to free
                    533:        **  the stream at this point in time. The situation we want to
                    534:        **  avoid is that we free the channel from within the stream pipe.
                    535:        **  This will lead to an infinite look having the stream freing
                    536:        **  itself.
                    537:        */
                    538:        return HTHost_clearChannel(host, HT_IGNORE);
                    539:     }
                    540: 
                    541:     if (host->persistent) {
2.2       frystyk   542:        if (CORE_TRACE) HTTrace("Host info... %p already persistent\n", host);
                    543:        return YES;
2.12.2.1  eric      544:     }
                    545: 
                    546:     {
                    547:        SOCKET sockfd = HTChannel_socket(host->channel);
2.8       frystyk   548:        if (sockfd != INVSOC && HTNet_availablePersistentSockets() > 0) {
2.12.2.1  eric      549:            host->persistent = YES;
2.8       frystyk   550:            host->mode = mode;
2.1       frystyk   551:            host->expires = time(NULL) + TCPTimeout;      /* Default timeout */
2.12.2.1  eric      552:            HTChannel_setHost(host->channel, host);
2.8       frystyk   553:            HTNet_increasePersistentSocket();
2.2       frystyk   554:            if (CORE_TRACE)
2.1       frystyk   555:                HTTrace("Host info... added host %p as persistent\n", host);
                    556:            return YES;
                    557:        } else {
2.2       frystyk   558:            if (CORE_TRACE)
                    559:                HTTrace("Host info... no room for persistent socket %d\n",
2.7       frystyk   560:                        sockfd);
2.1       frystyk   561:        }
                    562:     }
                    563:     return NO;
                    564: }
                    565: 
                    566: /*
2.12.2.1  eric      567: **     Check whether we have a persistent channel or not
                    568: */
                    569: PUBLIC BOOL HTHost_isPersistent (HTHost * host)
                    570: {
                    571:     return host && host->persistent;
                    572: }
                    573: 
                    574: /*
2.1       frystyk   575: **     Find persistent channel associated with this host.
                    576: */
                    577: PUBLIC HTChannel * HTHost_channel (HTHost * host)
                    578: {
                    579:     return host ? host->channel : NULL;
                    580: }
                    581: 
                    582: /*
                    583: **     Clear the persistent entry by deleting the channel object. Note that
                    584: **     the channel object is only deleted if it's not used anymore.
                    585: */
2.8       frystyk   586: PUBLIC BOOL HTHost_clearChannel (HTHost * host, int status)
2.1       frystyk   587: {
                    588:     if (host && host->channel) {
2.8       frystyk   589:        HTChannel_setHost(host->channel, NULL);
2.10      frystyk   590:        
                    591:        /*
                    592:        **  We don't want to recursively delete ourselves so if we are
                    593:        **  called from within the stream pipe then don't delete the channel
                    594:        **  at this point
                    595:        */
2.8       frystyk   596:        HTChannel_delete(host->channel, status);
2.1       frystyk   597:        host->expires = 0;
                    598:        host->channel = NULL;
2.8       frystyk   599:        HTNet_decreasePersistentSocket();
2.2       frystyk   600:        if (CORE_TRACE)
                    601:            HTTrace("Host info... removed host %p as persistent\n", host);
2.1       frystyk   602:        return YES;
                    603:     }
                    604:     return NO;
                    605: }
                    606: 
                    607: /*
2.8       frystyk   608: **     Handle the connection mode. The mode may change mode in the 
                    609: **     middle of a connection.
                    610: */
                    611: PUBLIC HTTransportMode HTHost_mode (HTHost * host, BOOL * active)
                    612: {
                    613:     return host ? host->mode : HT_TP_SINGLE;
                    614: }
                    615: 
                    616: /*
                    617: **     If the new mode is lower than the old mode then adjust the pipeline
                    618: **     accordingly. That is, if we are going into single mode then move
                    619: **     all entries in the pipeline and move the rest to the pending
                    620: **     queue. They will get launched at a later point in time.
                    621: */
                    622: PUBLIC BOOL HTHost_setMode (HTHost * host, HTTransportMode mode)
                    623: {
                    624:     if (host) {
                    625:        /*
                    626:        **  Check the new mode and see if we must adjust the queues.
                    627:        */
                    628:        if (mode == HT_TP_SINGLE && host->mode > mode) {
                    629:            int piped = HTList_count(host->pipeline);
                    630:            if (piped > 0) {
                    631:                int cnt;
                    632:                if (CORE_TRACE)
                    633:                    HTTrace("Host info... Moving %d Net objects from pipe line to pending queue\n", piped);
                    634:                if (!host->pending) host->pending = HTList_new();
                    635:                for (cnt=0; cnt<piped; cnt++) {
                    636:                    HTNet * net = HTList_removeFirstObject(host->pipeline);
                    637:                    HTList_appendObject(host->pending, net);
                    638:                }
                    639:            }
                    640:        }
                    641:        host->mode = mode;
                    642:        return YES;
                    643:     }
                    644:     return NO;
                    645: }
                    646: 
                    647: /*
                    648: **     Check whether a host is idle meaning if it is ready for a new
                    649: **     request which depends on the mode of the host. If the host is 
                    650: **     idle, i.e. ready for use then return YES else NO. If the host supports
                    651: **     persistent connections then still only return idle if no requests are
                    652: **     ongoing. 
                    653: */
                    654: PUBLIC BOOL HTHost_isIdle (HTHost * host)
                    655: {
                    656:     return (host && HTList_count(host->pipeline) <= 0);
                    657: }
                    658: 
2.12.2.1  eric      659: PRIVATE BOOL _roomInPipe (HTHost * host)
                    660: {
                    661:     int count;
                    662:     if (!host) return NO;
                    663:     count = HTList_count(host->pipeline);
                    664:     if (host->version == HTTP_11)
                    665:        return (count < MAX_PIPES);
                    666:     return (count <= 0);
                    667: }
                    668: 
2.8       frystyk   669: /*
                    670: **     Add a net object to the host object. If the host
                    671: **     is idle then add to active list (pipeline) else add
                    672: **     it to the pending list
                    673: **     Return HT_PENDING if we must pend, HT_OK, or HT_ERROR
                    674: */
                    675: PUBLIC int HTHost_addNet (HTHost * host, HTNet * net)
                    676: {
                    677:     if (host && net) {
                    678:        int status = HT_OK;
                    679: 
                    680:        /* Check to see if we can get a socket */
                    681:        if (HTNet_availableSockets() <= 0) {
                    682:            if (!PendHost) PendHost = HTList_new();
                    683:            if (CORE_TRACE)
                    684:                HTTrace("Host info... Add Host %p as pending\n", host);
                    685:            HTList_addObject(PendHost, host);
                    686:            status = HT_PENDING;
                    687:        }
                    688: 
                    689:        /* Add to either active or pending queue */
2.12.2.1  eric      690:        if (_roomInPipe(host)) {
2.8       frystyk   691:            if (CORE_TRACE) HTTrace("Host info... Add Net %p to pipeline of host %p\n", net, host);
                    692:            if (!host->pipeline) host->pipeline = HTList_new();
                    693:            HTList_addObject(host->pipeline, net);
                    694:            
                    695:            /*
                    696:            **  We have been idle and must hence unregister our catch close
                    697:            **  event handler
                    698:            */
                    699:            if (host->channel) {
2.12.2.1  eric      700:                HTHost_unregister(host, net, HTEvent_CLOSE);
2.8       frystyk   701:            }
2.12.2.1  eric      702:            /*
                    703:            ** Send out the request if we're not blocked on write
                    704:            */
                    705:            if (!(host->registeredFor & HTEvent_BITS(HTEvent_WRITE)))
                    706:                status = HTHost_launchPending(host) == TRUE ? HT_OK : HT_ERROR;
2.8       frystyk   707:        } else {
                    708:            if (CORE_TRACE) HTTrace("Host info... Add Net %p as pending\n", net);
                    709:            if (!host->pending) host->pending = HTList_new();
                    710:            HTList_addObject(host->pending, net);
                    711:            status = HT_PENDING;
                    712:        }
                    713:        return status;
                    714:     }
                    715:     return HT_ERROR;
                    716: }
                    717: 
2.12.2.1  eric      718: PUBLIC BOOL HTHost_free (HTHost * host, int status)
                    719: {
                    720:     if (host->channel == NULL) return NO;
                    721:     if (host->persistent) {
                    722:        if (CORE_TRACE)
                    723:            HTTrace("Host Object. keeping socket %d\n", HTChannel_socket(host->channel));
                    724:        HTChannel_delete(host->channel, status);
                    725:     } else {
                    726:        if (CORE_TRACE)
                    727:            HTTrace("Host Object. closing socket %d\n", HTChannel_socket(host->channel));
                    728: 
                    729:        /* 
                    730:        **  By lowering the semaphore we make sure that the channel
                    731:        **  is gonna be deleted
                    732:        */
                    733:        HTChannel_downSemaphore(host->channel);
                    734:        HTChannel_delete(host->channel, status);
                    735:        host->channel = NULL;
                    736:     }
                    737:     return YES;
                    738: }
                    739: 
2.8       frystyk   740: PUBLIC BOOL HTHost_deleteNet (HTHost * host, HTNet * net)
                    741: {
                    742:     if (host && net) {
                    743:        if (CORE_TRACE)
                    744:            HTTrace("Host info... Remove Net %p from pipe line\n", net);
                    745:        HTList_removeObject(host->pipeline, net);
                    746:        HTList_removeObject(host->pending, net);
                    747:        return YES;
                    748:     }
                    749:     return NO;
                    750: }
                    751: 
                    752: /*
                    753: **     Handle pending host objects.
                    754: **     There are two ways we can end up with pending reqyests:
                    755: **      1) If we are out of sockets then register new host objects as pending.
                    756: **      2) If we are pending on a connection then register new net objects as
                    757: **         pending
                    758: **     This set of functions handles pending host objects and can start new
                    759: **     requests as resources get available
                    760: */
                    761: 
                    762: /*
                    763: **     Check this host object for any pending requests and return the next
                    764: **     registered Net object.
                    765: */
                    766: PUBLIC HTNet * HTHost_nextPendingNet (HTHost * host)
                    767: {
                    768:     HTNet * net = NULL;
                    769:     if (host && host->pending && host->pipeline) {
2.11      kahan     770:       /*JK 23/Sep/96 Bug correction. Associated the following lines to the
                    771:       **above if. There was a missing pair of brackets. 
                    772:       */
                    773:       if ((net = (HTNet *) HTList_removeFirstObject(host->pending)) != NULL) {
                    774:        if (PROT_TRACE)
                    775:          HTTrace("Host info... Popping %p from pending net queue\n",
                    776:                  net);
2.8       frystyk   777:        HTList_addObject(host->pipeline, net);
2.11      kahan     778:       }
2.8       frystyk   779:     }
                    780:     return net;
                    781: }
                    782: 
                    783: /*
2.12.2.1  eric      784: **     Return the current list of pending host objects waiting for a socket
2.8       frystyk   785: */
                    786: PUBLIC HTHost * HTHost_nextPendingHost (void)
                    787: {
                    788:     HTHost * host = NULL;
                    789:     if (PendHost) {
                    790:        if ((host = (HTHost *) HTList_removeFirstObject(PendHost)) != NULL)
                    791:            if (PROT_TRACE)
                    792:                HTTrace("Host info... Poping %p from pending host queue\n",
                    793:                        host);
                    794:     }
                    795:     return host;
                    796: }
                    797: 
                    798: /*
                    799: **     Start the next pending request if any. First we look for pending
                    800: **     requests for the same host and then we check for any other pending
                    801: **     hosts
                    802: */
                    803: PUBLIC BOOL HTHost_launchPending (HTHost * host)
                    804: {
                    805:     int available = HTNet_availableSockets();
                    806: 
                    807:     if (!host) {
                    808:        if (PROT_TRACE) HTTrace("Host info... Bad arguments\n");
                    809:        return NO;
                    810:     }
                    811: 
                    812:     /*
                    813:     **  Check if we do have resources available for a new request
                    814:     **  This can either be reusing an existing connection or opening a new one
                    815:     */
                    816:     if (available > 0 || host->mode >= HT_TP_PIPELINE) {
2.12.2.1  eric      817:        HTNet * last = (HTNet *) HTList_lastObject(host->pipeline);
2.8       frystyk   818: 
                    819:        /*
2.12.2.1  eric      820:        **  Can only have one doing writing at a time
                    821:        */
                    822:        if (last && last->registeredFor == HTEvent_WRITE)
                    823:            return NO;
                    824: 
                    825:        /*
                    826:        **  Check the current Host object for pending Net objects
2.8       frystyk   827:        */
                    828:        if (host) {
                    829:            HTNet * net = HTHost_nextPendingNet(host);
                    830:            if (net) return HTNet_start(net);
                    831:        }
                    832: 
                    833:        /*
                    834:        **  Check for other pending Host objects
                    835:        */
                    836:        {
                    837:            HTHost * pending = HTHost_nextPendingHost();
                    838:            if (pending) {
                    839:                HTNet * net = HTHost_nextPendingNet(pending);
                    840:                if (net) return HTNet_start(net);
                    841:            }
                    842:        }
                    843: 
2.12.2.1  eric      844: #if 0
2.8       frystyk   845:        /*
                    846:        **  If nothing pending then register our catch close event handler to
                    847:        **  have something catching the socket if the remote server closes the
                    848:        **  connection, for example due to timeout.
                    849:        */
                    850:        if (PROT_TRACE) HTTrace("Host info... Nothing pending\n");
                    851:        if (host->channel) {
2.12.2.1  eric      852:            host->events[HTEvent_INDEX(HTEvent_CLOSE)].cbf = HTHost_catchClose;
                    853:            HTHost_register(host, NULL, HTEvent_CLOSE);
2.8       frystyk   854:        }
2.12.2.1  eric      855: #endif
2.8       frystyk   856:     } else
2.12.2.1  eric      857:        if (PROT_TRACE) HTTrace("Host info... No more requests.\n");
2.8       frystyk   858:     return NO;
2.1       frystyk   859: }
2.11      kahan     860: 
2.12.2.1  eric      861: PUBLIC HTNet * HTHost_firstNet (HTHost * host)
                    862: {
                    863:     return (HTNet *) HTList_firstObject(host->pipeline);
                    864: }
                    865: 
                    866: /*
                    867: **     The host event manager keeps track of the state of it's client engines
                    868: **     (typically HTTPEvent), accepting multiple blocks on read or write from
                    869: **     multiple pipelined engines. It then registers its own engine 
                    870: **     (HostEvent) with the event manager.
                    871: */
                    872: PUBLIC int HTHost_connect (HTHost * host, HTNet * net, char * url, int port)
                    873: {
                    874:     int status;
                    875:     if (host && host->connecttime)
                    876:        return HT_OK;
                    877:     status = HTDoConnect(net, url, port);
                    878:     if (status == HT_OK)
                    879:        return HT_OK;
                    880:     if (status == HT_WOULD_BLOCK || status == HT_PENDING)
                    881:        return HT_WOULD_BLOCK;
                    882:     return HT_ERROR; /* @@@ - some more deletion and stuff here? */
                    883: }
                    884: 
                    885: /*
                    886: **     Rules: SINGLE: one element in pipe, either reading or writing
                    887: **              PIPE: n element in pipe, n-1 reading, 1 writing
                    888: */
                    889: PUBLIC int HTHost_register (HTHost * host, HTNet * net, HTEvent_type type)
                    890: {
                    891:     /* net object may already be registered */
                    892:     if (HTEvent_BITS(type) & net->registeredFor)
                    893:        return NO;
                    894:     net->registeredFor ^= HTEvent_BITS(type);
                    895: 
                    896:     /* host object may already be registered */
                    897:     if (host->registeredFor & HTEvent_BITS(type))
                    898:        return YES;
                    899:     host->registeredFor ^= HTEvent_BITS(type);
                    900: 
                    901:     return HTEvent_register(HTChannel_socket(host->channel), type, host->events+HTEvent_INDEX(type));
                    902: }
                    903: 
                    904: PUBLIC int HTHost_unregister (HTHost * host, HTNet * net, HTEvent_type type)
                    905: {
                    906:     /* net object may no be registered */
                    907:     if (!(HTEvent_BITS(type) & net->registeredFor))
                    908:        return NO;
                    909:     net->registeredFor ^= HTEvent_BITS(type);
                    910: 
                    911:     /* host object may no be registered */
                    912:     if (!(host->registeredFor & HTEvent_BITS(type)))
                    913:        return YES;
                    914:     host->registeredFor ^= HTEvent_BITS(type);
2.11      kahan     915: 
2.12.2.1  eric      916:     /* stay registered for READ to catch a socket close */
                    917:     /* WRITE and CONNECT can be unregistered, though */
                    918:     if ((type == HTEvent_WRITE && isLastInPipe(host, net)) || 
                    919:        type == HTEvent_CONNECT)
                    920:        /* if we are blocked downstream, shut down the whole pipe */
                    921:        HTEvent_unregister(HTChannel_socket(host->channel), type);
                    922:     return YES;
                    923: }
2.11      kahan     924: 
2.12.2.1  eric      925: /*
                    926: **     The reader tells HostEvent that it's stream did not finish the data
                    927: */
                    928: PUBLIC BOOL HTHost_setRemainingRead (HTHost * host, size_t remaining)
                    929: {
                    930:     if (host == NULL) return NO;
                    931:     host->remainingRead = remaining;
                    932:     return YES;
                    933: #if 0
                    934:     /* This (old) way is recursive and needs to go to the NEXT net object */
                    935:     HTNet * targetNet = (HTNet *)HTList_firstObject(host->pipeline);
                    936:     if (targetNet == NULL)
                    937:        return HT_ERROR;
                    938:     return (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, type);
                    939: #endif
                    940: }
2.11      kahan     941: 
2.12.2.1  eric      942: PUBLIC SockA * HTHost_getSockAddr (HTHost * host)
                    943: {
                    944:     if (!host) return NULL;
                    945:     return &host->sock_addr;
                    946: }
                    947: 
                    948: PUBLIC BOOL HTHost_setHome (HTHost * host, int home)
                    949: {
                    950:     if (!host) return NO;
                    951:     host->home = home;
                    952:     return YES;
                    953: }
                    954: 
                    955: PUBLIC int HTHost_home (HTHost * host)
                    956: {
                    957:     if (!host) return 0;
                    958:     return host->home;
                    959: }
                    960: 
                    961: #if 0  /* Is a macro right now */
                    962: PUBLIC BOOL HTHost_setDNS5 (HTHost * host, HTdns * dns)
                    963: {
                    964:     if (!host) return NO;
                    965:     host->dns = dns;
                    966:     return YES;
                    967: }
                    968: #endif
                    969: 
                    970: PUBLIC BOOL HTHost_setChannel (HTHost * host, HTChannel * channel)
                    971: {
                    972:     if (!host) return NO;
                    973:     host->channel = channel;
                    974:     return YES;
                    975: }
                    976: 
2.12.2.2! frystyk   977: PUBLIC BOOL HTHost_setMuxChannel (HTHost * host, HTMuxChannel * muxch)
        !           978: {
        !           979:     if (host) {
        !           980:        host->muxch = muxch;
        !           981:        return YES;
        !           982:     }
        !           983:     return NO;
        !           984: }
        !           985: 
        !           986: PUBLIC HTMuxChannel * HTHost_muxChannel (HTHost * host)
        !           987: {
        !           988:     return host ? host->muxch : NULL;
        !           989: }
        !           990: 
2.12.2.1  eric      991: PUBLIC HTNet * HTHost_getReadNet(HTHost * host)
                    992: {
2.12.2.2! frystyk   993:     if (host) {
        !           994:        return host->mode == HT_TP_INTERLEAVE ?
        !           995:            HTMuxChannel_net(host->muxch) :
        !           996:            (HTNet *) HTList_firstObject(host->pipeline);
        !           997:     }
        !           998:     return NULL;
        !           999: }
        !          1000: 
        !          1001: PUBLIC HTNet * HTHost_getWriteNet(HTHost * host)
        !          1002: {
        !          1003:     return host ? (HTNet *) HTList_lastObject(host->pipeline) : NULL;
2.12.2.1  eric     1004: }
                   1005: 
                   1006: /*
                   1007: **     Create the input stream and bind it to the channel
                   1008: **     Please read the description in the HTIOStream module for the parameters
                   1009: */
2.12.2.2! frystyk  1010: PUBLIC HTInputStream * HTHost_getInput (HTHost * host, HTTransport * tp,
        !          1011:                                        void * param, int mode)
2.12.2.1  eric     1012: {
2.12.2.2! frystyk  1013:     if (host && host->channel && tp) {
2.12.2.1  eric     1014:        HTChannel * ch = host->channel;
                   1015:        HTInputStream * input = (*tp->input_new)(host, ch, param, mode);
                   1016:        HTChannel_setInput(ch, input);
                   1017:        return input;
                   1018:     }
                   1019:     if (CORE_TRACE) HTTrace("Host Object.. Can't create input stream\n");
2.12.2.2! frystyk  1020:     return NULL;
        !          1021: }
        !          1022: 
        !          1023: PUBLIC HTOutputStream * HTHost_getOutput (HTHost * host, HTTransport * tp,
        !          1024:                                          void * param, int mode)
        !          1025: {
        !          1026:     if (host && host->channel && tp) {
        !          1027:        HTChannel * ch = host->channel;
        !          1028:        HTOutputStream * output = (*tp->output_new)(host, ch, param, mode);
        !          1029:        HTChannel_setOutput(ch, output);
        !          1030:        return output;
        !          1031:     }
        !          1032:     if (CORE_TRACE) HTTrace("Host Object.. Can't create output stream\n");
        !          1033:     return NULL;
        !          1034: }
        !          1035: 
        !          1036: PUBLIC HTOutputStream * HTHost_output (HTHost * host, HTNet * net)
        !          1037: {
        !          1038:     if (host && host->channel && net) {
        !          1039:        HTOutputStream * output = HTChannel_output(host->channel);
        !          1040: 
        !          1041:        /*
        !          1042:        **  If we are in MUX mode then create new output stream on top
        !          1043:        **  of the already existing one. Otherwise just return what we
        !          1044:        **  have.
        !          1045:        */
        !          1046:        if (host->mode == HT_TP_INTERLEAVE) {
        !          1047:            HTStream * target = HTChannel_output(host->channel);
        !          1048:            output = HTMuxWriter_new(host->channel, net, target);
        !          1049:        }
        !          1050:        return output;
        !          1051:     }
2.12.2.1  eric     1052:     return NULL;
                   1053: }
                   1054: 
                   1055: PUBLIC int HTHost_read(HTHost * host)
                   1056: {
                   1057:     HTInputStream * input = HTChannel_input(host->channel);
                   1058:     if (input == NULL) return HT_ERROR;
                   1059:     return (*input->isa->read)(input);
                   1060: }
                   1061: 
                   1062: PUBLIC BOOL HTHost_setConsumed(HTHost * host, size_t bytes)
                   1063: {
                   1064:     HTInputStream * input;
                   1065:     if (!host || !host->channel) return NO;
                   1066:     if ((input = HTChannel_input(host->channel)) == NULL)
                   1067:        return NO;
                   1068:     return (*input->isa->consumed)(input, bytes);
                   1069: }
2.1       frystyk  1070: 

Webmaster