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

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

Webmaster