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

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

Webmaster