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

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

Webmaster