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

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.10! frystyk     6: **     @(#) $Id: HTHost.c,v 2.12.2.9 1996/11/08 22:06:56 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.12.2.10! frystyk    62:        HTEvent_delete(me->events[0]);
        !            63:        HTEvent_delete(me->events[1]);
2.8       frystyk    64:        HTList_delete(me->pipeline);
                     65:        HTList_delete(me->pending);
2.1       frystyk    66:        HT_FREE(me);
                     67:     }
                     68: }
                     69: 
                     70: PRIVATE BOOL delete_object (HTList * list, HTHost * me)
                     71: {
2.2       frystyk    72:     if (CORE_TRACE) HTTrace("Host info... object %p from list %p\n", me, list);
2.1       frystyk    73:     HTList_removeObject(list, (void *) me);
                     74:     free_object(me);
                     75:     return YES;
                     76: }
                     77: 
2.12.2.1  eric       78: PRIVATE BOOL isLastInPipe (HTHost * host, HTNet * net)
                     79: {
                     80:     return HTList_lastObject(host->pipeline) == net;
                     81: }
                     82: 
                     83: /*
                     84: **     HostEvent - host event manager - recieves events from the event 
                     85: **     manager and dispatches them to the client net objects by calling the 
                     86: **     net object's cbf.
                     87: **
                     88: */
2.12.2.3  eric       89: PRIVATE int HostEvent (SOCKET soc, void * pVoid, HTEventType type)
2.12.2.1  eric       90: {
                     91:     HTHost * host = (HTHost *)pVoid;
                     92: 
                     93:     if (type == HTEvent_READ) {
                     94:        HTNet * targetNet;
                     95: 
                     96:        /* call the first net object */
                     97:        do {
                     98:            int ret;
                     99:            targetNet = (HTNet *)HTList_firstObject(host->pipeline);
                    100:            if (targetNet) {
2.12.2.8  eric      101:                if (CORE_TRACE)
                    102:                    HTWATCH(HTWatch_TCP, host, HTHIDE("HostEvent: READ passed to %s.\n"), 
                    103:                            HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
2.12.2.1  eric      104:                if ((ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), 
                    105:                                                  targetNet->event.param, type)) != HT_OK)
                    106:                    return ret;
                    107:            }
                    108:        /* call pipelined net object to eat all the data in the channel */
                    109:        } while (host->remainingRead > 0);
                    110: 
                    111:        /* last target net should have set remainingRead to 0 */
                    112:        if (targetNet)
                    113:            return HT_OK;
                    114: 
                    115:        /* If there was notargetNet, it should be a close */
                    116:        HTWATCH(HTWatch_TCP, host, HTHIDE("HostEvent: host %s closed connection.\n"), 
                    117:                host->hostname);
                    118: 
                    119:        /* Is there garbage in the channel? Let's check: */
                    120:        {
                    121:            char buf[256];
                    122:            int ret;
                    123:            while ((ret = NETREAD(HTChannel_socket(host->channel), buf, sizeof(buf))) > 0)
                    124:                HTWATCH(HTWatch_TCP, host, HTHIDE("Host %s had %d extraneous bytes.\n"));
                    125:        }
                    126:        HTHost_clearChannel(host, HT_OK);
                    127:        return HT_OK; /* extra garbage does not constitute an application error */
                    128:        
                    129:     } else if (type == HTEvent_WRITE) {
                    130:            HTNet * targetNet = (HTNet *)HTList_lastObject(host->pipeline);
                    131:        if (targetNet) {
2.12.2.8  eric      132:            if (CORE_TRACE)
                    133:                HTWATCH(HTWatch_TCP, host, HTHIDE("HostEvent: WRITE passed to %s.\n"), 
                    134:                        HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
2.12.2.1  eric      135:            return (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, type);
                    136:        }
                    137:        HTWATCH(HTWatch_TCP, host, HTHIDE("HostEvent: Who wants to write to %s?\n"), 
                    138:                host->hostname);
                    139:        return HT_ERROR;
                    140:        }
                    141: 
                    142:     HTWATCH(HTWatch_TCP, host, HTHIDE("Don't know how to handle OOB data from %s?\n"), 
                    143:            host->hostname);
                    144:     return HT_OK;
                    145: }
                    146: 
2.1       frystyk   147: /*
                    148: **     Search the host info cache for a host object or create a new one
                    149: **     and add it. Examples of host names are
                    150: **
                    151: **             www.w3.org
                    152: **             www.foo.com:8000
                    153: **             18.52.0.18
                    154: **
                    155: **     Returns Host object or NULL if error. You may get back an already
                    156: **     existing host object - you're not guaranteed a new one each time.
                    157: */
2.12.2.1  eric      158: 
2.1       frystyk   159: PUBLIC HTHost * HTHost_new (char * host)
                    160: {
                    161:     HTList * list = NULL;                          /* Current list in cache */
                    162:     HTHost * pres = NULL;
2.12.2.7  frystyk   163:     int hash = 0;
2.1       frystyk   164:     if (!host) {
2.2       frystyk   165:        if (CORE_TRACE) HTTrace("Host info... Bad argument\n");
2.1       frystyk   166:        return NULL;
                    167:     }
                    168:     
                    169:     /* Find a hash for this host */
                    170:     {
                    171:        char *ptr;
                    172:        for (ptr=host; *ptr; ptr++)
2.12.2.7  frystyk   173:            hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.1       frystyk   174:        if (!HostTable) {
2.12.2.7  frystyk   175:            if ((HostTable = (HTList **) HT_CALLOC(HOST_HASH_SIZE,
2.1       frystyk   176:                                                   sizeof(HTList *))) == NULL)
                    177:                HT_OUTOFMEM("HTHost_find");
                    178:        }
                    179:        if (!HostTable[hash]) HostTable[hash] = HTList_new();
                    180:        list = HostTable[hash];
                    181:     }
                    182: 
                    183:     /* Search the cache */
                    184:     {
                    185:        HTList * cur = list;
                    186:        while ((pres = (HTHost *) HTList_nextObject(cur))) {
                    187:            if (!strcmp(pres->hostname, host)) {
2.8       frystyk   188:                if (HTHost_isIdle(pres) && time(NULL)>pres->ntime+HostTimeout){
2.2       frystyk   189:                    if (CORE_TRACE)
2.1       frystyk   190:                        HTTrace("Host info... Collecting host info %p\n",pres);
                    191:                    delete_object(list, pres);
                    192:                    pres = NULL;
                    193:                }
                    194:                break;
                    195:            }
                    196:        }
                    197:     }
                    198: 
2.8       frystyk   199:     /* If not found then create new Host object, else use existing one */
2.1       frystyk   200:     if (pres) {
                    201:        if (pres->channel) {
2.12.2.6  eric      202:            if (pres->expires && pres->expires < time(NULL)) {     /* Cached channel is cold */
2.2       frystyk   203:                if (CORE_TRACE)
2.1       frystyk   204:                    HTTrace("Host info... Persistent channel %p gotten cold\n",
                    205:                            pres->channel);
2.5       eric      206:                HTChannel_delete(pres->channel, HT_OK);
2.1       frystyk   207:                pres->channel = NULL;
                    208:            } else {
2.2       frystyk   209:                if (CORE_TRACE)
2.1       frystyk   210:                    HTTrace("Host info... REUSING CHANNEL %p\n",pres->channel);
                    211:            }
                    212:        }
                    213:     } else {
                    214:        if ((pres = (HTHost *) HT_CALLOC(1, sizeof(HTHost))) == NULL)
                    215:            HT_OUTOFMEM("HTHost_add");
2.12.2.7  frystyk   216:        pres->hash = hash;
2.1       frystyk   217:        StrAllocCopy(pres->hostname, host);
                    218:        pres->ntime = time(NULL);
2.8       frystyk   219:        pres->mode = HT_TP_SINGLE;
2.12.2.10! frystyk   220:        pres->events[HTEvent_INDEX(HTEvent_READ)] = HTEvent_new(HostEvent, pres, HT_PRIORITY_MAX, -1);
        !           221:        pres->events[HTEvent_INDEX(HTEvent_WRITE)]= HTEvent_new(HostEvent, pres, HT_PRIORITY_MAX, -1);
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:              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
2.12.2.9  eric      278:              HT_FREE(fullhost);        /* parsedHost points into fullhost */
2.12.2.1  eric      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++)
2.12.2.7  frystyk   303:            hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.9       frystyk   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.3  eric      496: PUBLIC int HTHost_catchClose (SOCKET soc, void * context, HTEventType 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.12.2.7  frystyk   549:            HTHost_setMode(host, 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:        
2.12.2.6  eric      590:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
                    591:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
                    592: 
2.10      frystyk   593:        /*
                    594:        **  We don't want to recursively delete ourselves so if we are
                    595:        **  called from within the stream pipe then don't delete the channel
                    596:        **  at this point
                    597:        */
2.8       frystyk   598:        HTChannel_delete(host->channel, status);
2.1       frystyk   599:        host->expires = 0;
                    600:        host->channel = NULL;
2.8       frystyk   601:        HTNet_decreasePersistentSocket();
2.2       frystyk   602:        if (CORE_TRACE)
                    603:            HTTrace("Host info... removed host %p as persistent\n", host);
2.1       frystyk   604:        return YES;
                    605:     }
                    606:     return NO;
                    607: }
                    608: 
                    609: /*
2.8       frystyk   610: **     Handle the connection mode. The mode may change mode in the 
                    611: **     middle of a connection.
                    612: */
                    613: PUBLIC HTTransportMode HTHost_mode (HTHost * host, BOOL * active)
                    614: {
                    615:     return host ? host->mode : HT_TP_SINGLE;
                    616: }
                    617: 
                    618: /*
                    619: **     If the new mode is lower than the old mode then adjust the pipeline
                    620: **     accordingly. That is, if we are going into single mode then move
                    621: **     all entries in the pipeline and move the rest to the pending
                    622: **     queue. They will get launched at a later point in time.
                    623: */
                    624: PUBLIC BOOL HTHost_setMode (HTHost * host, HTTransportMode mode)
                    625: {
                    626:     if (host) {
                    627:        /*
                    628:        **  Check the new mode and see if we must adjust the queues.
                    629:        */
                    630:        if (mode == HT_TP_SINGLE && host->mode > mode) {
                    631:            int piped = HTList_count(host->pipeline);
                    632:            if (piped > 0) {
                    633:                int cnt;
                    634:                if (CORE_TRACE)
                    635:                    HTTrace("Host info... Moving %d Net objects from pipe line to pending queue\n", piped);
                    636:                if (!host->pending) host->pending = HTList_new();
                    637:                for (cnt=0; cnt<piped; cnt++) {
                    638:                    HTNet * net = HTList_removeFirstObject(host->pipeline);
                    639:                    HTList_appendObject(host->pending, net);
                    640:                }
                    641:            }
                    642:        }
2.12.2.7  frystyk   643:        if (PROT_TRACE)
                    644:            HTTrace("Host info... New mode is %d for host %p\n", host->mode, host);
2.8       frystyk   645:        host->mode = mode;
                    646:        return YES;
                    647:     }
                    648:     return NO;
                    649: }
                    650: 
                    651: /*
                    652: **     Check whether a host is idle meaning if it is ready for a new
                    653: **     request which depends on the mode of the host. If the host is 
                    654: **     idle, i.e. ready for use then return YES else NO. If the host supports
                    655: **     persistent connections then still only return idle if no requests are
                    656: **     ongoing. 
                    657: */
                    658: PUBLIC BOOL HTHost_isIdle (HTHost * host)
                    659: {
                    660:     return (host && HTList_count(host->pipeline) <= 0);
                    661: }
                    662: 
2.12.2.1  eric      663: PRIVATE BOOL _roomInPipe (HTHost * host)
                    664: {
                    665:     int count;
                    666:     if (!host) return NO;
                    667:     count = HTList_count(host->pipeline);
2.12.2.7  frystyk   668:     return host->mode == HT_TP_INTERLEAVE ? YES :
                    669:        (host->version == HTTP_11) ? (count < MAX_PIPES) : (count <= 0);
2.12.2.1  eric      670: }
                    671: 
2.8       frystyk   672: /*
                    673: **     Add a net object to the host object. If the host
                    674: **     is idle then add to active list (pipeline) else add
                    675: **     it to the pending list
                    676: **     Return HT_PENDING if we must pend, HT_OK, or HT_ERROR
                    677: */
                    678: PUBLIC int HTHost_addNet (HTHost * host, HTNet * net)
                    679: {
                    680:     if (host && net) {
                    681:        int status = HT_OK;
                    682: 
                    683:        /* Check to see if we can get a socket */
                    684:        if (HTNet_availableSockets() <= 0) {
                    685:            if (!PendHost) PendHost = HTList_new();
                    686:            if (CORE_TRACE)
                    687:                HTTrace("Host info... Add Host %p as pending\n", host);
                    688:            HTList_addObject(PendHost, host);
                    689:            status = HT_PENDING;
                    690:        }
                    691: 
                    692:        /* Add to either active or pending queue */
2.12.2.1  eric      693:        if (_roomInPipe(host)) {
2.8       frystyk   694:            if (CORE_TRACE) HTTrace("Host info... Add Net %p to pipeline of host %p\n", net, host);
                    695:            if (!host->pipeline) host->pipeline = HTList_new();
                    696:            HTList_addObject(host->pipeline, net);
2.12.2.8  eric      697: #if 0
2.8       frystyk   698:            /*
                    699:            **  We have been idle and must hence unregister our catch close
                    700:            **  event handler
                    701:            */
                    702:            if (host->channel) {
2.12.2.1  eric      703:                HTHost_unregister(host, net, HTEvent_CLOSE);
2.8       frystyk   704:            }
2.12.2.8  eric      705: #endif
2.12.2.1  eric      706:            /*
                    707:            ** Send out the request if we're not blocked on write
                    708:            */
                    709:            if (!(host->registeredFor & HTEvent_BITS(HTEvent_WRITE)))
                    710:                status = HTHost_launchPending(host) == TRUE ? HT_OK : HT_ERROR;
2.8       frystyk   711:        } else {
                    712:            if (CORE_TRACE) HTTrace("Host info... Add Net %p as pending\n", net);
                    713:            if (!host->pending) host->pending = HTList_new();
                    714:            HTList_addObject(host->pending, net);
                    715:            status = HT_PENDING;
                    716:        }
                    717:        return status;
                    718:     }
                    719:     return HT_ERROR;
                    720: }
                    721: 
2.12.2.1  eric      722: PUBLIC BOOL HTHost_free (HTHost * host, int status)
                    723: {
                    724:     if (host->channel == NULL) return NO;
                    725:     if (host->persistent) {
                    726:        if (CORE_TRACE)
                    727:            HTTrace("Host Object. keeping socket %d\n", HTChannel_socket(host->channel));
                    728:        HTChannel_delete(host->channel, status);
                    729:     } else {
                    730:        if (CORE_TRACE)
                    731:            HTTrace("Host Object. closing socket %d\n", HTChannel_socket(host->channel));
                    732: 
                    733:        /* 
                    734:        **  By lowering the semaphore we make sure that the channel
                    735:        **  is gonna be deleted
                    736:        */
2.12.2.3  eric      737:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
                    738:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
                    739:        host->registeredFor = 0;
2.12.2.1  eric      740:        HTChannel_downSemaphore(host->channel);
                    741:        HTChannel_delete(host->channel, status);
                    742:        host->channel = NULL;
                    743:     }
                    744:     return YES;
                    745: }
                    746: 
2.8       frystyk   747: PUBLIC BOOL HTHost_deleteNet (HTHost * host, HTNet * net)
                    748: {
                    749:     if (host && net) {
                    750:        if (CORE_TRACE)
                    751:            HTTrace("Host info... Remove Net %p from pipe line\n", net);
                    752:        HTList_removeObject(host->pipeline, net);
                    753:        HTList_removeObject(host->pending, net);
                    754:        return YES;
                    755:     }
                    756:     return NO;
                    757: }
                    758: 
                    759: /*
                    760: **     Handle pending host objects.
                    761: **     There are two ways we can end up with pending reqyests:
                    762: **      1) If we are out of sockets then register new host objects as pending.
                    763: **      2) If we are pending on a connection then register new net objects as
                    764: **         pending
                    765: **     This set of functions handles pending host objects and can start new
                    766: **     requests as resources get available
                    767: */
                    768: 
                    769: /*
                    770: **     Check this host object for any pending requests and return the next
                    771: **     registered Net object.
                    772: */
                    773: PUBLIC HTNet * HTHost_nextPendingNet (HTHost * host)
                    774: {
                    775:     HTNet * net = NULL;
                    776:     if (host && host->pending && host->pipeline) {
2.11      kahan     777:       /*JK 23/Sep/96 Bug correction. Associated the following lines to the
                    778:       **above if. There was a missing pair of brackets. 
                    779:       */
                    780:       if ((net = (HTNet *) HTList_removeFirstObject(host->pending)) != NULL) {
                    781:        if (PROT_TRACE)
                    782:          HTTrace("Host info... Popping %p from pending net queue\n",
                    783:                  net);
2.8       frystyk   784:        HTList_addObject(host->pipeline, net);
2.11      kahan     785:       }
2.8       frystyk   786:     }
                    787:     return net;
                    788: }
                    789: 
                    790: /*
2.12.2.1  eric      791: **     Return the current list of pending host objects waiting for a socket
2.8       frystyk   792: */
                    793: PUBLIC HTHost * HTHost_nextPendingHost (void)
                    794: {
                    795:     HTHost * host = NULL;
                    796:     if (PendHost) {
                    797:        if ((host = (HTHost *) HTList_removeFirstObject(PendHost)) != NULL)
                    798:            if (PROT_TRACE)
                    799:                HTTrace("Host info... Poping %p from pending host queue\n",
                    800:                        host);
                    801:     }
                    802:     return host;
                    803: }
                    804: 
                    805: /*
                    806: **     Start the next pending request if any. First we look for pending
                    807: **     requests for the same host and then we check for any other pending
                    808: **     hosts
                    809: */
                    810: PUBLIC BOOL HTHost_launchPending (HTHost * host)
                    811: {
                    812:     int available = HTNet_availableSockets();
                    813: 
                    814:     if (!host) {
                    815:        if (PROT_TRACE) HTTrace("Host info... Bad arguments\n");
                    816:        return NO;
                    817:     }
                    818: 
                    819:     /*
                    820:     **  Check if we do have resources available for a new request
                    821:     **  This can either be reusing an existing connection or opening a new one
                    822:     */
                    823:     if (available > 0 || host->mode >= HT_TP_PIPELINE) {
                    824: 
                    825:        /*
2.12.2.7  frystyk   826:        **  In pipeline we can only have one doing writing at a time.
                    827:        **  We therefore check that there are no other Net object
                    828:        **  registered for write
2.12.2.1  eric      829:        */
2.12.2.7  frystyk   830:        if (host->mode == HT_TP_PIPELINE) {
                    831:            HTNet * last = (HTNet *) HTList_lastObject(host->pipeline);
                    832:            if (last && last->registeredFor == HTEvent_WRITE)
                    833:                return NO;
                    834:        }
2.12.2.1  eric      835: 
                    836:        /*
                    837:        **  Check the current Host object for pending Net objects
2.8       frystyk   838:        */
                    839:        if (host) {
                    840:            HTNet * net = HTHost_nextPendingNet(host);
2.12.2.8  eric      841:            if (net) return HTNet_execute(net, HTEvent_WRITE);
2.8       frystyk   842:        }
                    843: 
                    844:        /*
                    845:        **  Check for other pending Host objects
                    846:        */
                    847:        {
                    848:            HTHost * pending = HTHost_nextPendingHost();
                    849:            if (pending) {
                    850:                HTNet * net = HTHost_nextPendingNet(pending);
2.12.2.8  eric      851:                if (net) return HTNet_execute(net, HTEvent_WRITE);
2.8       frystyk   852:            }
                    853:        }
                    854:     } else
2.12.2.1  eric      855:        if (PROT_TRACE) HTTrace("Host info... No more requests.\n");
2.8       frystyk   856:     return NO;
2.1       frystyk   857: }
2.11      kahan     858: 
2.12.2.1  eric      859: PUBLIC HTNet * HTHost_firstNet (HTHost * host)
                    860: {
                    861:     return (HTNet *) HTList_firstObject(host->pipeline);
                    862: }
                    863: 
                    864: /*
                    865: **     The host event manager keeps track of the state of it's client engines
                    866: **     (typically HTTPEvent), accepting multiple blocks on read or write from
                    867: **     multiple pipelined engines. It then registers its own engine 
                    868: **     (HostEvent) with the event manager.
                    869: */
2.12.2.4  frystyk   870: PUBLIC int HTHost_connect (HTHost * host, HTNet * net, char * url, HTProtocolId port)
2.12.2.1  eric      871: {
                    872:     int status;
                    873:     if (host && host->connecttime)
                    874:        return HT_OK;
                    875:     status = HTDoConnect(net, url, port);
                    876:     if (status == HT_OK)
                    877:        return HT_OK;
                    878:     if (status == HT_WOULD_BLOCK || status == HT_PENDING)
                    879:        return HT_WOULD_BLOCK;
                    880:     return HT_ERROR; /* @@@ - some more deletion and stuff here? */
                    881: }
                    882: 
                    883: /*
                    884: **     Rules: SINGLE: one element in pipe, either reading or writing
                    885: **              PIPE: n element in pipe, n-1 reading, 1 writing
                    886: */
2.12.2.3  eric      887: PUBLIC int HTHost_register (HTHost * host, HTNet * net, HTEventType type)
2.12.2.1  eric      888: {
2.12.2.7  frystyk   889:     if (host && net) {
2.12.2.1  eric      890: 
2.12.2.7  frystyk   891:        /* net object may already be registered */
                    892:        if (HTEvent_BITS(type) & net->registeredFor)
                    893:            return NO;
                    894:        net->registeredFor ^= HTEvent_BITS(type);
2.12.2.1  eric      895: 
2.12.2.7  frystyk   896:        /* host object may already be registered */
                    897:        if (host->registeredFor & HTEvent_BITS(type))
                    898:            return YES;
                    899:        host->registeredFor ^= HTEvent_BITS(type);
2.12.2.10! frystyk   900:        return HTEvent_register(HTChannel_socket(host->channel), type, *(host->events+HTEvent_INDEX(type)));
2.12.2.7  frystyk   901:     }
                    902:     return NO;
2.12.2.1  eric      903: }
                    904: 
2.12.2.3  eric      905: PUBLIC int HTHost_unregister (HTHost * host, HTNet * net, HTEventType type)
2.12.2.1  eric      906: {
2.12.2.7  frystyk   907:     if (host && net) {
2.12.2.1  eric      908: 
2.12.2.7  frystyk   909:        /* net object may no be registered */
                    910:        if (!(HTEvent_BITS(type) & net->registeredFor))
                    911:            return NO;
                    912:        net->registeredFor ^= HTEvent_BITS(type);
2.11      kahan     913: 
2.12.2.7  frystyk   914:        /* host object may no be registered */
                    915:        if (!(host->registeredFor & HTEvent_BITS(type)))
                    916:            return YES;
                    917:        host->registeredFor ^= HTEvent_BITS(type);
                    918: 
                    919:        /* stay registered for READ to catch a socket close */
                    920:        /* WRITE and CONNECT can be unregistered, though */
                    921:        if ((type == HTEvent_WRITE && isLastInPipe(host, net)) || 
                    922:            type == HTEvent_CONNECT)
                    923:            /* if we are blocked downstream, shut down the whole pipe */
                    924:            HTEvent_unregister(HTChannel_socket(host->channel), type);
                    925:        return YES;
                    926:     }
                    927:     return NO;
2.12.2.1  eric      928: }
2.11      kahan     929: 
2.12.2.1  eric      930: /*
                    931: **     The reader tells HostEvent that it's stream did not finish the data
                    932: */
                    933: PUBLIC BOOL HTHost_setRemainingRead (HTHost * host, size_t remaining)
                    934: {
                    935:     if (host == NULL) return NO;
                    936:     host->remainingRead = remaining;
                    937:     return YES;
                    938: }
2.11      kahan     939: 
2.12.2.1  eric      940: PUBLIC SockA * HTHost_getSockAddr (HTHost * host)
                    941: {
                    942:     if (!host) return NULL;
                    943:     return &host->sock_addr;
                    944: }
                    945: 
                    946: PUBLIC BOOL HTHost_setHome (HTHost * host, int home)
                    947: {
                    948:     if (!host) return NO;
                    949:     host->home = home;
                    950:     return YES;
                    951: }
                    952: 
                    953: PUBLIC int HTHost_home (HTHost * host)
                    954: {
                    955:     if (!host) return 0;
                    956:     return host->home;
                    957: }
                    958: 
                    959: #if 0  /* Is a macro right now */
                    960: PUBLIC BOOL HTHost_setDNS5 (HTHost * host, HTdns * dns)
                    961: {
                    962:     if (!host) return NO;
                    963:     host->dns = dns;
                    964:     return YES;
                    965: }
                    966: #endif
                    967: 
                    968: PUBLIC BOOL HTHost_setChannel (HTHost * host, HTChannel * channel)
                    969: {
                    970:     if (!host) return NO;
                    971:     host->channel = channel;
                    972:     return YES;
                    973: }
                    974: 
                    975: PUBLIC HTNet * HTHost_getReadNet(HTHost * host)
                    976: {
2.12.2.2  frystyk   977:     if (host) {
2.12.2.7  frystyk   978:        if (host->mode == HT_TP_INTERLEAVE) {
                    979:            HTMuxChannel * muxch = HTMuxChannel_find(host);
                    980:            return HTMuxChannel_net(muxch);
                    981:        }
                    982:        return (HTNet *) HTList_firstObject(host->pipeline);
2.12.2.2  frystyk   983:     }
                    984:     return NULL;
                    985: }
                    986: 
                    987: PUBLIC HTNet * HTHost_getWriteNet(HTHost * host)
                    988: {
                    989:     return host ? (HTNet *) HTList_lastObject(host->pipeline) : NULL;
2.12.2.1  eric      990: }
                    991: 
                    992: /*
                    993: **     Create the input stream and bind it to the channel
                    994: **     Please read the description in the HTIOStream module for the parameters
                    995: */
2.12.2.2  frystyk   996: PUBLIC HTInputStream * HTHost_getInput (HTHost * host, HTTransport * tp,
                    997:                                        void * param, int mode)
2.12.2.1  eric      998: {
2.12.2.2  frystyk   999:     if (host && host->channel && tp) {
2.12.2.1  eric     1000:        HTChannel * ch = host->channel;
                   1001:        HTInputStream * input = (*tp->input_new)(host, ch, param, mode);
                   1002:        HTChannel_setInput(ch, input);
                   1003:        return input;
                   1004:     }
                   1005:     if (CORE_TRACE) HTTrace("Host Object.. Can't create input stream\n");
2.12.2.2  frystyk  1006:     return NULL;
                   1007: }
                   1008: 
                   1009: PUBLIC HTOutputStream * HTHost_getOutput (HTHost * host, HTTransport * tp,
                   1010:                                          void * param, int mode)
                   1011: {
                   1012:     if (host && host->channel && tp) {
                   1013:        HTChannel * ch = host->channel;
                   1014:        HTOutputStream * output = (*tp->output_new)(host, ch, param, mode);
                   1015:        HTChannel_setOutput(ch, output);
                   1016:        return output;
                   1017:     }
                   1018:     if (CORE_TRACE) HTTrace("Host Object.. Can't create output stream\n");
                   1019:     return NULL;
                   1020: }
                   1021: 
                   1022: PUBLIC HTOutputStream * HTHost_output (HTHost * host, HTNet * net)
                   1023: {
                   1024:     if (host && host->channel && net) {
                   1025:        HTOutputStream * output = HTChannel_output(host->channel);
                   1026: 
                   1027:        /*
                   1028:        **  If we are in MUX mode then create new output stream on top
                   1029:        **  of the already existing one. Otherwise just return what we
                   1030:        **  have.
                   1031:        */
                   1032:        if (host->mode == HT_TP_INTERLEAVE) {
2.12.2.4  frystyk  1033:            HTStream * target = (HTStream *) HTChannel_output(host->channel);
                   1034:            output = HTMuxWriter_new(host, net, target);
2.12.2.2  frystyk  1035:        }
                   1036:        return output;
                   1037:     }
2.12.2.1  eric     1038:     return NULL;
                   1039: }
                   1040: 
2.12.2.8  eric     1041: PUBLIC int HTHost_read(HTHost * host, HTNet * net)
2.12.2.1  eric     1042: {
                   1043:     HTInputStream * input = HTChannel_input(host->channel);
2.12.2.8  eric     1044:     if (net != HTHost_getReadNet(host)) {
                   1045:        HTHost_register(host, net, HTEvent_READ);
                   1046:        return HT_WOULD_BLOCK;
                   1047:     }
2.12.2.1  eric     1048:     if (input == NULL) return HT_ERROR;
                   1049:     return (*input->isa->read)(input);
                   1050: }
                   1051: 
                   1052: PUBLIC BOOL HTHost_setConsumed(HTHost * host, size_t bytes)
                   1053: {
                   1054:     HTInputStream * input;
                   1055:     if (!host || !host->channel) return NO;
                   1056:     if ((input = HTChannel_input(host->channel)) == NULL)
                   1057:        return NO;
                   1058:     return (*input->isa->consumed)(input, bytes);
                   1059: }
2.1       frystyk  1060: 
2.12.2.7  frystyk  1061: PUBLIC int HTHost_hash (HTHost * host)
                   1062: {
                   1063:     return host ? host->hash : -1;
                   1064: }

Webmaster