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

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.37    ! frystyk     6: **     @(#) $Id: HTHost.c,v 2.36 1998/02/01 19:04:07 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.13      frystyk    23: #include "HTTPUtil.h"
                     24: #include "HTTCP.h"
2.1       frystyk    25: #include "HTHost.h"                                     /* Implemented here */
2.13      frystyk    26: #include "HTHstMan.h"
2.1       frystyk    27: 
2.31      frystyk    28: #ifdef HT_MUX
                     29: #include "WWWMux.h"
                     30: #endif
                     31: 
2.1       frystyk    32: #define HOST_TIMEOUT           43200L       /* Default host timeout is 12 h */
2.29      frystyk    33: 
2.32      frystyk    34: #define TCP_TTL                600L         /* Timeout on a busy connection */
                     35: #define TCP_IDLE_TTL           60L         /* Timeout on an idle connection */
2.29      frystyk    36: 
2.32      frystyk    37: #define MAX_PIPES              100  /* maximum number of pipelined requests */
2.24      frystyk    38: #define MAX_HOST_RECOVER       3             /* Max number of auto recovery */
2.32      frystyk    39: #define DEFAULT_DELAY          30        /* Default write flush delay in ms */
2.1       frystyk    40: 
2.13      frystyk    41: struct _HTInputStream {
                     42:     const HTInputStreamClass * isa;
2.1       frystyk    43: };
                     44: 
2.13      frystyk    45: PRIVATE int HostEvent(SOCKET soc, void * pVoid, HTEventType type);
                     46: 
                     47: /* Type definitions and global variables etc. local to this module */
2.1       frystyk    48: PRIVATE time_t HostTimeout = HOST_TIMEOUT;       /* Timeout on host entries */
2.32      frystyk    49: PRIVATE time_t TcpTtl = TCP_TTL;          /* Timeout on persistent channels */
2.1       frystyk    50: 
2.8       frystyk    51: PRIVATE HTList ** HostTable = NULL;
                     52: PRIVATE HTList * PendHost = NULL;          /* List of pending host elements */
2.1       frystyk    53: 
2.13      frystyk    54: PRIVATE int EventTimeout = -1;                 /* Global Host event timeout */
                     55: 
2.36      frystyk    56: PRIVATE ms_t WriteDelay = DEFAULT_DELAY;                     /* Delay in ms */
2.26      frystyk    57: 
2.1       frystyk    58: /* ------------------------------------------------------------------------- */
                     59: 
                     60: PRIVATE void free_object (HTHost * me)
                     61: {
                     62:     if (me) {
2.30      frystyk    63:        int i;
2.1       frystyk    64:        HT_FREE(me->hostname);
                     65:        HT_FREE(me->type);
2.12      frystyk    66:        HT_FREE(me->server);
                     67:        HT_FREE(me->user_agent);
                     68:        HT_FREE(me->range_units);
2.30      frystyk    69: 
                     70:        /* Delete the channel (if any) */
2.3       eric       71:        if (me->channel) {
2.5       eric       72:            HTChannel_delete(me->channel, HT_OK);
2.3       eric       73:            me->channel = NULL;
                     74:        }
2.30      frystyk    75: 
                     76:        /* Unregister the events */
2.18      eric       77:        for (i = 0; i < HTEvent_TYPES; i++)
                     78:            HTEvent_delete(me->events[i]);
2.30      frystyk    79: 
                     80:        /* Delete the timer (if any) */
                     81:        if (me->timer) HTTimer_delete(me->timer);
                     82: 
                     83:        /* Delete the queues */
2.8       frystyk    84:        HTList_delete(me->pipeline);
                     85:        HTList_delete(me->pending);
2.1       frystyk    86:        HT_FREE(me);
                     87:     }
                     88: }
                     89: 
                     90: PRIVATE BOOL delete_object (HTList * list, HTHost * me)
                     91: {
2.2       frystyk    92:     if (CORE_TRACE) HTTrace("Host info... object %p from list %p\n", me, list);
2.1       frystyk    93:     HTList_removeObject(list, (void *) me);
                     94:     free_object(me);
                     95:     return YES;
                     96: }
                     97: 
2.13      frystyk    98: PRIVATE BOOL isLastInPipe (HTHost * host, HTNet * net)
                     99: {
                    100:     return HTList_lastObject(host->pipeline) == net;
                    101: }
                    102: 
                    103: /*
                    104: **     HostEvent - host event manager - recieves events from the event 
                    105: **     manager and dispatches them to the client net objects by calling the 
                    106: **     net object's cbf.
                    107: **
                    108: */
                    109: PRIVATE int HostEvent (SOCKET soc, void * pVoid, HTEventType type)
                    110: {
                    111:     HTHost * host = (HTHost *)pVoid;
                    112: 
2.18      eric      113:     if (type == HTEvent_READ || type == HTEvent_CLOSE) {
2.13      frystyk   114:        HTNet * targetNet;
                    115: 
                    116:        /* call the first net object */
                    117:        do {
                    118:            int ret;
                    119:            targetNet = (HTNet *)HTList_firstObject(host->pipeline);
                    120:            if (targetNet) {
                    121:                if (CORE_TRACE)
2.28      frystyk   122:                    HTTrace("Host Event.. READ passed to `%s\'\n", 
                    123:                            HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet))));
2.13      frystyk   124:                if ((ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), 
                    125:                                                  targetNet->event.param, type)) != HT_OK)
                    126:                    return ret;
                    127:            }
                    128:            if (targetNet == NULL && host->remainingRead > 0) {
2.31      frystyk   129:                if (CORE_TRACE)
                    130:                    HTTrace("HostEvent... Error: %d bytes left to read and nowhere to put them\n",
                    131:                            host->remainingRead);
2.13      frystyk   132:                host->remainingRead = 0;
                    133:                /*
                    134:                **      Fall through to close the channel
                    135:                */
                    136:            }
                    137:        /* call pipelined net object to eat all the data in the channel */
                    138:        } while (host->remainingRead > 0);
                    139: 
                    140:        /* last target net should have set remainingRead to 0 */
                    141:        if (targetNet)
                    142:            return HT_OK;
                    143: 
                    144:        /* If there was notargetNet, it should be a close */
2.28      frystyk   145:        if (CORE_TRACE)
                    146:            HTTrace("Host Event.. host %p `%s\' closed connection.\n", 
                    147:                    host, host->hostname);
2.13      frystyk   148: 
                    149:        /* Is there garbage in the channel? Let's check: */
                    150:        {
                    151:            char buf[256];
                    152:            int ret;
2.28      frystyk   153:            memset(buf, '\0', sizeof(buf));
                    154:            while ((ret = NETREAD(HTChannel_socket(host->channel), buf, sizeof(buf))) > 0) {
                    155:                if (CORE_TRACE)
                    156:                    HTTrace("Host Event.. Host %p `%s\' had %d extraneous bytes: `%s\'\n",
                    157:                            host, host->hostname, ret, buf);
                    158:                memset(buf, '\0', sizeof(buf));         
                    159:            }       
2.13      frystyk   160:        }
                    161:        HTHost_clearChannel(host, HT_OK);
2.28      frystyk   162:        return HT_OK;        /* extra garbage does not constitute an application error */
2.13      frystyk   163:        
2.18      eric      164:     } else if (type == HTEvent_WRITE || type == HTEvent_CONNECT) {
2.13      frystyk   165:        HTNet * targetNet = (HTNet *)HTList_lastObject(host->pipeline);
                    166:        if (targetNet) {
                    167:            if (CORE_TRACE)
2.28      frystyk   168:                HTTrace("Host Event.. WRITE passed to `%s\'\n", 
                    169:                        HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet))));
2.13      frystyk   170:            return (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, type);
                    171:        }
2.28      frystyk   172:        HTTrace("Host Event.. Who wants to write to `%s\'?\n", host->hostname);
2.13      frystyk   173:        return HT_ERROR;
2.14      frystyk   174:     } else if (type == HTEvent_TIMEOUT) {
                    175: 
                    176:        if (CORE_TRACE)
                    177:            HTTrace("Host Event.. WE SHOULD DELETE ALL REQUEST ON `%s\'?\n",
                    178:                    host->hostname);
2.13      frystyk   179: 
2.14      frystyk   180:     } else {
2.28      frystyk   181:        HTTrace("Don't know how to handle OOB data from `%s\'?\n", 
2.14      frystyk   182:                host->hostname);
                    183:     }
2.13      frystyk   184:     return HT_OK;
                    185: }
                    186: 
2.30      frystyk   187: PRIVATE int TimeoutEvent (HTTimer * timer, void * param, HTEventType type)
                    188: {
                    189:     HTHost * host = (HTHost *) param;
                    190:     SOCKET sockfd = HTChannel_socket(host->channel);
                    191:     int result = HostEvent (sockfd, host, HTEvent_CLOSE);
                    192:     HTTimer_delete(timer);
                    193:     host->timer = NULL;
                    194:     return result;
                    195: }
                    196: 
2.1       frystyk   197: /*
                    198: **     Search the host info cache for a host object or create a new one
                    199: **     and add it. Examples of host names are
                    200: **
                    201: **             www.w3.org
                    202: **             www.foo.com:8000
                    203: **             18.52.0.18
                    204: **
                    205: **     Returns Host object or NULL if error. You may get back an already
                    206: **     existing host object - you're not guaranteed a new one each time.
                    207: */
2.15      eric      208: PUBLIC HTHost * HTHost_new (char * host, u_short u_port)
2.1       frystyk   209: {
                    210:     HTList * list = NULL;                          /* Current list in cache */
                    211:     HTHost * pres = NULL;
2.13      frystyk   212:     int hash = 0;
2.1       frystyk   213:     if (!host) {
2.2       frystyk   214:        if (CORE_TRACE) HTTrace("Host info... Bad argument\n");
2.1       frystyk   215:        return NULL;
                    216:     }
                    217:     
                    218:     /* Find a hash for this host */
                    219:     {
                    220:        char *ptr;
                    221:        for (ptr=host; *ptr; ptr++)
2.13      frystyk   222:            hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.1       frystyk   223:        if (!HostTable) {
2.13      frystyk   224:            if ((HostTable = (HTList **) HT_CALLOC(HOST_HASH_SIZE,
2.1       frystyk   225:                                                   sizeof(HTList *))) == NULL)
                    226:                HT_OUTOFMEM("HTHost_find");
                    227:        }
                    228:        if (!HostTable[hash]) HostTable[hash] = HTList_new();
                    229:        list = HostTable[hash];
                    230:     }
                    231: 
                    232:     /* Search the cache */
                    233:     {
                    234:        HTList * cur = list;
                    235:        while ((pres = (HTHost *) HTList_nextObject(cur))) {
2.15      eric      236:            if (!strcmp(pres->hostname, host) && u_port == pres->u_port) {
2.8       frystyk   237:                if (HTHost_isIdle(pres) && time(NULL)>pres->ntime+HostTimeout){
2.2       frystyk   238:                    if (CORE_TRACE)
2.1       frystyk   239:                        HTTrace("Host info... Collecting host info %p\n",pres);
                    240:                    delete_object(list, pres);
                    241:                    pres = NULL;
                    242:                }
                    243:                break;
                    244:            }
                    245:        }
                    246:     }
                    247: 
2.8       frystyk   248:     /* If not found then create new Host object, else use existing one */
2.1       frystyk   249:     if (pres) {
                    250:        if (pres->channel) {
2.32      frystyk   251: 
                    252:             /*
                    253:             **  If we have a TTL for this TCP connection then
                    254:             **  check that we haven't passed it.
                    255:             */
                    256:             if (pres->expires > 0) {
                    257:                 time_t t = time(NULL);
                    258:                 if (pres->expires < t) {          /* Cached channel is cold */
                    259:                     if (CORE_TRACE)
                    260:                         HTTrace("Host info... Persistent channel %p gotten cold\n",
                    261:                         pres->channel);
                    262:                     HTChannel_delete(pres->channel, HT_OK);
                    263:                     pres->channel = NULL;
                    264:                 } else {
                    265:                     pres->expires = t + TcpTtl;
                    266:                     if (CORE_TRACE)
                    267:                         HTTrace("Host info... REUSING CHANNEL %p\n",pres->channel);
                    268:                 }
                    269:             }
2.1       frystyk   270:        }
                    271:     } else {
                    272:        if ((pres = (HTHost *) HT_CALLOC(1, sizeof(HTHost))) == NULL)
                    273:            HT_OUTOFMEM("HTHost_add");
2.13      frystyk   274:        pres->hash = hash;
2.1       frystyk   275:        StrAllocCopy(pres->hostname, host);
2.15      eric      276:        pres->u_port = u_port;
2.1       frystyk   277:        pres->ntime = time(NULL);
2.8       frystyk   278:        pres->mode = HT_TP_SINGLE;
2.26      frystyk   279:        pres->delay = WriteDelay;
2.18      eric      280:        {
2.26      frystyk   281:            int i;
                    282:            for (i = 0; i < HTEvent_TYPES; i++)
                    283:                pres->events[i]= HTEvent_new(HostEvent, pres, HT_PRIORITY_MAX, EventTimeout);
2.18      eric      284:        }
2.2       frystyk   285:        if (CORE_TRACE) 
2.24      frystyk   286:            HTTrace("Host info... added `%s\' with host %p to list %p\n",
                    287:                    host, pres, list);
2.1       frystyk   288:        HTList_addObject(list, (void *) pres);
                    289:     }
                    290:     return pres;
2.9       frystyk   291: }
                    292: 
2.15      eric      293: PUBLIC HTHost * HTHost_newWParse (HTRequest * request, char * url, u_short u_port)
2.13      frystyk   294: {
2.32      frystyk   295:     char * port;
                    296:     char * fullhost = NULL;
                    297:     char * parsedHost = NULL;
                    298:     SockA * sin;
                    299:     HTHost * me;
                    300:     char * proxy = HTRequest_proxy(request);
2.13      frystyk   301: 
2.32      frystyk   302:     fullhost = HTParse(proxy ? proxy : url, "", PARSE_HOST);
2.13      frystyk   303: 
                    304:              /* If there's an @ then use the stuff after it as a hostname */
2.32      frystyk   305:     if (fullhost) {
                    306:        char * at_sign;
                    307:        if ((at_sign = strchr(fullhost, '@')) != NULL)
                    308:            parsedHost = at_sign+1;
                    309:        else
                    310:            parsedHost = fullhost;
                    311:     }
                    312:     if (!parsedHost || !*parsedHost) {
                    313:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_HOST,
                    314:                           NULL, 0, "HTDoConnect");
                    315:        HT_FREE(fullhost);
                    316:        return NULL;
                    317:     }
                    318:     port = strchr(parsedHost, ':');
                    319:     if (PROT_TRACE)
                    320:        HTTrace("HTDoConnect. Looking up `%s\'\n", parsedHost);
                    321:     if (port) {
                    322:        *port++ = '\0';
2.36      frystyk   323:        if (!*port || !isdigit((int) *port))
2.32      frystyk   324:            port = 0;
                    325:        u_port = (u_short) atol(port);
                    326:     }
                    327:     /* Find information about this host */
                    328:     if ((me = HTHost_new(parsedHost, u_port)) == NULL) {
                    329:        if (PROT_TRACE)HTTrace("HTDoConnect. Can't get host info\n");
                    330:        me->tcpstate = TCP_ERROR;
                    331:        return NULL;
                    332:     }
                    333:     sin = &me->sock_addr;
                    334:     memset((void *) sin, '\0', sizeof(SockA));
2.13      frystyk   335: #ifdef DECNET
2.32      frystyk   336:     sin->sdn_family = AF_DECnet;
                    337:     net->sock_addr.sdn_objnum = port ? (unsigned char)(strtol(port, (char **) 0, 10)) : DNP_OBJ;
2.13      frystyk   338: #else  /* Internet */
2.32      frystyk   339:     sin->sin_family = AF_INET;
                    340:     sin->sin_port = htons(u_port);
2.13      frystyk   341: #endif
2.32      frystyk   342:     HT_FREE(fullhost); /* parsedHost points into fullhost */
                    343:     return me;
2.13      frystyk   344: }
                    345: 
2.9       frystyk   346: /*
                    347: **     Search the host info cache for a host object. Examples of host names:
                    348: **
                    349: **             www.w3.org
                    350: **             www.foo.com:8000
                    351: **             18.52.0.18
                    352: **
                    353: **     Returns Host object or NULL if not found.
                    354: */
                    355: PUBLIC HTHost * HTHost_find (char * host)
                    356: {
                    357:     HTList * list = NULL;                          /* Current list in cache */
                    358:     HTHost * pres = NULL;
                    359:     if (CORE_TRACE)
                    360:        HTTrace("Host info... Looking for `%s\'\n", host ? host : "<null>");
                    361: 
                    362:     /* Find a hash for this host */
                    363:     if (host && HostTable) {
                    364:        int hash = 0;
                    365:        char *ptr;
                    366:        for (ptr=host; *ptr; ptr++)
2.13      frystyk   367:            hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.9       frystyk   368:        if (!HostTable[hash]) return NULL;
                    369:        list = HostTable[hash];
                    370: 
                    371:        /* Search the cache */
                    372:        {
                    373:            HTList * cur = list;
                    374:            while ((pres = (HTHost *) HTList_nextObject(cur))) {
                    375:                if (!strcmp(pres->hostname, host)) {
                    376:                    if (time(NULL) > pres->ntime + HostTimeout) {
                    377:                        if (CORE_TRACE)
                    378:                            HTTrace("Host info... Collecting host %p\n", pres);
                    379:                        delete_object(list, pres);
                    380:                        pres = NULL;
                    381:                    } else {
                    382:                        if (CORE_TRACE)
                    383:                            HTTrace("Host info... Found `%s\'\n", host);
                    384:                    }
                    385:                    return pres;
                    386:                }
                    387:            }
                    388:        }
                    389:     }
                    390:     return NULL;
2.1       frystyk   391: }
                    392: 
                    393: /*
2.8       frystyk   394: **     Get and set the hostname of the remote host
                    395: */
                    396: PUBLIC char * HTHost_name (HTHost * host)
                    397: {
                    398:      return host ? host->hostname : NULL;
                    399: }
                    400: 
                    401: /*
2.1       frystyk   402: **     Get and set the type class of the remote host
                    403: */
                    404: PUBLIC char * HTHost_class (HTHost * host)
                    405: {
                    406:      return host ? host->type : NULL;
                    407: }
                    408: 
                    409: PUBLIC void HTHost_setClass (HTHost * host, char * s_class)
                    410: {
                    411:     if (host && s_class) StrAllocCopy(host->type, s_class);
                    412: }
                    413: 
                    414: /*
                    415: **     Get and set the version of the remote host
                    416: */
                    417: PUBLIC int HTHost_version (HTHost *host)
                    418: {
                    419:      return host ? host->version : 0;
                    420: }
                    421: 
                    422: PUBLIC void HTHost_setVersion (HTHost * host, int version)
                    423: {
                    424:     if (host) host->version = version;
                    425: }
                    426: 
                    427: /*
                    428: **     Get and set the cache timeout for persistent entries.
                    429: **     The default value is TCP_TIMEOUT
                    430: */
                    431: PUBLIC void HTHost_setPersistTimeout (time_t timeout)
                    432: {
2.32      frystyk   433:     TcpTtl = timeout;
2.1       frystyk   434: }
                    435: 
                    436: PUBLIC time_t HTHost_persistTimeout (time_t timeout)
                    437: {
2.32      frystyk   438:     return TcpTtl;
2.1       frystyk   439: }
                    440: 
                    441: /*     Persistent Connection Expiration
                    442: **     --------------------------------
                    443: **     Should normally not be used. If, then use calendar time.
                    444: */
                    445: PUBLIC void HTHost_setPersistExpires (HTHost * host, time_t expires)
                    446: {
                    447:     if (host) host->expires = expires;
                    448: }
                    449: 
                    450: PUBLIC time_t HTHost_persistExpires (HTHost * host)
                    451: {
                    452:     return host ? host->expires : -1;
                    453: }
                    454: 
2.22      eric      455: PUBLIC void HTHost_setReqsPerConnection (HTHost * host, int reqs)
                    456: {
                    457:     if (host) host->reqsPerConnection = reqs;
                    458: }
                    459: 
                    460: PUBLIC int HTHost_reqsPerConnection (HTHost * host)
                    461: {
                    462:     return host ? host->reqsPerConnection : -1;
                    463: }
                    464: 
                    465: PUBLIC void HTHost_setReqsMade (HTHost * host, int reqs)
                    466: {
                    467:     if (host) host->reqsMade = reqs;
                    468: }
                    469: 
                    470: PUBLIC int HTHost_reqsMade (HTHost * host)
                    471: {
                    472:     return host ? host->reqsMade : -1;
                    473: }
                    474: 
                    475: 
2.1       frystyk   476: /*
2.6       frystyk   477: **     Public methods for this host
                    478: */
                    479: PUBLIC HTMethod HTHost_publicMethods (HTHost * me)
                    480: {
                    481:     return me ? me->methods : METHOD_INVALID;
                    482: }
                    483: 
                    484: PUBLIC void HTHost_setPublicMethods (HTHost * me, HTMethod methodset)
                    485: {
                    486:     if (me) me->methods = methodset;
                    487: }
                    488: 
                    489: PUBLIC void HTHost_appendPublicMethods (HTHost * me, HTMethod methodset)
                    490: {
                    491:     if (me) me->methods |= methodset;
                    492: }
                    493: 
                    494: /*
                    495: **     Get and set the server name of the remote host
                    496: */
                    497: PUBLIC char * HTHost_server (HTHost * host)
                    498: {
                    499:      return host ? host->server : NULL;
                    500: }
                    501: 
                    502: PUBLIC BOOL HTHost_setServer (HTHost * host, const char * server)
                    503: {
                    504:     if (host && server) {
                    505:        StrAllocCopy(host->server, server);
                    506:        return YES;
                    507:     }
                    508:     return NO;
                    509: }
                    510: 
                    511: /*
                    512: **     Get and set the userAgent name of the remote host
                    513: */
                    514: PUBLIC char * HTHost_userAgent (HTHost * host)
                    515: {
                    516:      return host ? host->user_agent : NULL;
                    517: }
                    518: 
                    519: PUBLIC BOOL HTHost_setUserAgent (HTHost * host, const char * userAgent)
                    520: {
                    521:     if (host && userAgent) {
                    522:        StrAllocCopy(host->user_agent, userAgent);
                    523:        return YES;
2.12      frystyk   524:     }
                    525:     return NO;
                    526: }
                    527: 
                    528: /*
                    529: **     Get and set acceptable range units
                    530: */
                    531: PUBLIC char * HTHost_rangeUnits (HTHost * host)
                    532: {
                    533:      return host ? host->range_units : NULL;
                    534: }
                    535: 
                    536: PUBLIC BOOL HTHost_setRangeUnits (HTHost * host, const char * units)
                    537: {
                    538:     if (host && units) {
                    539:        StrAllocCopy(host->range_units, units);
                    540:        return YES;
                    541:     }
                    542:     return NO;
                    543: }
                    544: 
                    545: /*
                    546: **     Checks whether a specific range unit is OK. We always say
                    547: **     YES except if we have a specific statement from the server that
                    548: **     it doesn't understand byte ranges - that is - it has sent "none"
                    549: **     in a "Accept-Range" response header
                    550: */
                    551: PUBLIC BOOL HTHost_isRangeUnitAcceptable (HTHost * host, const char * unit)
                    552: {
                    553:     if (host && unit) {
                    554: #if 0
                    555:        if (host->range_units) {
                    556:            char * start = strcasestr(host->range_units, "none");
                    557: 
                    558:            /*
                    559:            **  Check that "none" is infact a token. It could be part of some
                    560:            **  other valid string, so we'd better check for it.
                    561:            */
                    562:            if (start) {
                    563:                
                    564:                
                    565:            }
                    566:            return NO;
                    567:        }
                    568: #endif
                    569:        return strcasecomp(unit, "bytes") ? NO : YES;
2.6       frystyk   570:     }
                    571:     return NO;
                    572: }
                    573: 
2.1       frystyk   574: /*     HTHost_catchClose
                    575: **     -----------------
                    576: **     This function is registered when the socket is idle so that we get
                    577: **     a notification if the socket closes at the other end. At this point
                    578: **     we can't use the request object as it might have been freed a long
                    579: **     time ago.
                    580: */
2.13      frystyk   581: PUBLIC int HTHost_catchClose (SOCKET soc, void * context, HTEventType type)
2.1       frystyk   582: {
2.13      frystyk   583:     HTNet * net = (HTNet *)context;
                    584:     HTHost * host = net->host;
2.2       frystyk   585:     if (CORE_TRACE)
2.13      frystyk   586:        HTTrace("Catch Close. called with socket %d with type %x\n",
                    587:                soc, type);
                    588:     if (type == HTEvent_READ) {
2.1       frystyk   589:        HTChannel * ch = HTChannel_find(soc);     /* Find associated channel */
2.8       frystyk   590:        HTHost * host = HTChannel_host(ch);           /* and associated host */
2.1       frystyk   591:        if (ch && host) {           
2.2       frystyk   592:            if (CORE_TRACE) HTTrace("Catch Close. CLOSING socket %d\n", soc);
2.8       frystyk   593:            HTHost_clearChannel(host, HT_OK);
2.1       frystyk   594:        } else {
2.2       frystyk   595:            if (CORE_TRACE) HTTrace("Catch Close. socket %d NOT FOUND!\n",soc);
2.1       frystyk   596:        }
                    597:     }
2.13      frystyk   598:     HTHost_unregister(host, net, HTEvent_CLOSE);
2.1       frystyk   599:     return HT_OK;
                    600: }
                    601: 
                    602: /*
                    603: **     As soon as we know that this host accepts persistent connections,
                    604: **     we associated the channel with the host. 
                    605: **     We don't want more than MaxSockets-2 connections to be persistent in
                    606: **     order to avoid deadlock.
                    607: */
2.13      frystyk   608: PUBLIC BOOL HTHost_setPersistent (HTHost *             host,
                    609:                                  BOOL                  persistent,
                    610:                                  HTTransportMode       mode)
2.1       frystyk   611: {
2.13      frystyk   612:     if (!host) return NO;
                    613: 
                    614:     if (!persistent) {
                    615:        /*
                    616:        **  We use the HT_IGNORE status code as we don't want to free
                    617:        **  the stream at this point in time. The situation we want to
                    618:        **  avoid is that we free the channel from within the stream pipe.
                    619:        **  This will lead to an infinite look having the stream freing
                    620:        **  itself.
                    621:        */
2.30      frystyk   622:        host->persistent = NO;
2.13      frystyk   623:        return HTHost_clearChannel(host, HT_IGNORE);
                    624:     }
                    625: 
2.18      eric      626:     /*
                    627:     ** Set the host persistent if not already. Also update the mode to
                    628:     ** the new one - it may have changed
                    629:     */
                    630:     HTHost_setMode(host, mode);
                    631:     if (!host->persistent) {
2.13      frystyk   632:        SOCKET sockfd = HTChannel_socket(host->channel);
2.8       frystyk   633:        if (sockfd != INVSOC && HTNet_availablePersistentSockets() > 0) {
2.13      frystyk   634:            host->persistent = YES;
2.32      frystyk   635:            host->expires = time(NULL) + TcpTtl;     /* Default timeout */
2.13      frystyk   636:            HTChannel_setHost(host->channel, host);
2.8       frystyk   637:            HTNet_increasePersistentSocket();
2.2       frystyk   638:            if (CORE_TRACE)
2.1       frystyk   639:                HTTrace("Host info... added host %p as persistent\n", host);
                    640:            return YES;
                    641:        } else {
2.2       frystyk   642:            if (CORE_TRACE)
                    643:                HTTrace("Host info... no room for persistent socket %d\n",
2.7       frystyk   644:                        sockfd);
2.18      eric      645:            return NO;
2.1       frystyk   646:        }
2.18      eric      647:     } else {
                    648:        if (CORE_TRACE) HTTrace("Host info... %p already persistent\n", host);
                    649:        return YES;
2.1       frystyk   650:     }
                    651:     return NO;
                    652: }
                    653: 
                    654: /*
2.13      frystyk   655: **     Check whether we have a persistent channel or not
                    656: */
                    657: PUBLIC BOOL HTHost_isPersistent (HTHost * host)
                    658: {
                    659:     return host && host->persistent;
                    660: }
                    661: 
                    662: /*
2.1       frystyk   663: **     Find persistent channel associated with this host.
                    664: */
                    665: PUBLIC HTChannel * HTHost_channel (HTHost * host)
                    666: {
                    667:     return host ? host->channel : NULL;
                    668: }
                    669: 
2.30      frystyk   670: 
2.1       frystyk   671: /*
2.30      frystyk   672: **  Check whether we have got a "close" notification, for example in the
                    673: **  connection header
                    674: */
                    675: PUBLIC BOOL HTHost_setCloseNotification (HTHost * host, BOOL mode)
                    676: {
                    677:     if (host) {
                    678:        host->close_notification = mode;
2.37    ! frystyk   679:        return YES;
2.30      frystyk   680:     }
                    681:     return NO;
                    682: }
                    683: 
                    684: PUBLIC BOOL HTHost_closeNotification (HTHost * host)
                    685: {
                    686:     return host && host->close_notification;
                    687: }
                    688: 
                    689: /*
2.1       frystyk   690: **     Clear the persistent entry by deleting the channel object. Note that
                    691: **     the channel object is only deleted if it's not used anymore.
                    692: */
2.8       frystyk   693: PUBLIC BOOL HTHost_clearChannel (HTHost * host, int status)
2.1       frystyk   694: {
                    695:     if (host && host->channel) {
2.8       frystyk   696:        HTChannel_setHost(host->channel, NULL);
2.10      frystyk   697:        
2.13      frystyk   698:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
                    699:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
2.18      eric      700:        host->registeredFor = 0;
2.13      frystyk   701: 
2.10      frystyk   702:        /*
                    703:        **  We don't want to recursively delete ourselves so if we are
                    704:        **  called from within the stream pipe then don't delete the channel
                    705:        **  at this point
                    706:        */
2.8       frystyk   707:        HTChannel_delete(host->channel, status);
2.18      eric      708:        host->expires = 0;      
2.1       frystyk   709:        host->channel = NULL;
2.22      eric      710:        host->tcpstate = TCP_BEGIN;
                    711:        host->reqsMade = 0;
2.32      frystyk   712:        if (HTHost_isPersistent(host)) {
                    713:            HTNet_decreasePersistentSocket();
                    714:            host->persistent = NO;
                    715:        }
                    716:        host->close_notification = NO;
                    717:                host->mode = HT_TP_SINGLE;
                    718: 
2.28      frystyk   719:        if (CORE_TRACE) HTTrace("Host info... removed host %p as persistent\n", host);
2.32      frystyk   720: 
                    721:        if (!HTList_isEmpty(host->pending)) {
                    722:            if (CORE_TRACE)
                    723:                HTTrace("Host has %d object pending - attempting launch\n", HTList_count(host->pending));
                    724:            HTHost_launchPending(host);
                    725:        }
2.1       frystyk   726:        return YES;
                    727:     }
                    728:     return NO;
                    729: }
                    730: 
2.37    ! frystyk   731: PUBLIC BOOL HTHost_doRecover (HTHost * host)
        !           732: {
        !           733:     return host ? host->do_recover : NO;
        !           734: }
        !           735: 
2.1       frystyk   736: /*
2.18      eric      737: **     Move all entries in the pipeline and move the rest to the pending
                    738: **     queue. They will get launched at a later point in time.
                    739: */
                    740: PUBLIC BOOL HTHost_recoverPipe (HTHost * host)
                    741: {
                    742:     if (host) {
                    743:        int piped = HTList_count(host->pipeline);
                    744:        if (piped > 0) {
                    745:            int cnt;
2.24      frystyk   746:            host->recovered++;
2.18      eric      747:            if (CORE_TRACE)
2.24      frystyk   748:                HTTrace("Host recovered %d times. Moving %d Net objects from pipe line to pending queue\n",
                    749:                        host->recovered, piped);
2.18      eric      750:            
                    751:            /*
                    752:            **  Unregister this host for all events
                    753:            */
                    754:            HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
                    755:            HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
                    756:            host->registeredFor = 0;
                    757: 
                    758:            /*
                    759:            **  Set new mode to single until we know what is going on
                    760:            */
                    761:            host->mode = HT_TP_SINGLE;
                    762: 
                    763:            /*
                    764:            **  Move all net objects from the net object to the pending queue.
                    765:            */
                    766:            if (!host->pending) host->pending = HTList_new();
                    767:            for (cnt=0; cnt<piped; cnt++) {
                    768:                HTNet * net = HTList_removeLastObject(host->pipeline);
                    769:                if (CORE_TRACE) HTTrace("Host recover Resetting net object %p\n", net);
                    770:                net->registeredFor = 0;
                    771:                (*net->event.cbf)(HTChannel_socket(host->channel), net->event.param, HTEvent_RESET);
                    772:                HTList_appendObject(host->pending, net);
                    773:            }
2.37    ! frystyk   774: 
2.18      eric      775:            HTChannel_setSemaphore(host->channel, 0);
                    776:            HTHost_clearChannel(host, HT_INTERRUPTED);
2.37    ! frystyk   777:            host->do_recover = NO;
2.18      eric      778:        }
2.24      frystyk   779:        return YES;
2.18      eric      780:     }
                    781:     return NO;
                    782: }
                    783: 
                    784: /*
2.8       frystyk   785: **     Handle the connection mode. The mode may change mode in the 
                    786: **     middle of a connection.
                    787: */
                    788: PUBLIC HTTransportMode HTHost_mode (HTHost * host, BOOL * active)
                    789: {
                    790:     return host ? host->mode : HT_TP_SINGLE;
                    791: }
                    792: 
                    793: /*
                    794: **     If the new mode is lower than the old mode then adjust the pipeline
                    795: **     accordingly. That is, if we are going into single mode then move
                    796: **     all entries in the pipeline and move the rest to the pending
                    797: **     queue. They will get launched at a later point in time.
                    798: */
                    799: PUBLIC BOOL HTHost_setMode (HTHost * host, HTTransportMode mode)
                    800: {
                    801:     if (host) {
                    802:        /*
                    803:        **  Check the new mode and see if we must adjust the queues.
                    804:        */
                    805:        if (mode == HT_TP_SINGLE && host->mode > mode) {
                    806:            int piped = HTList_count(host->pipeline);
                    807:            if (piped > 0) {
                    808:                int cnt;
                    809:                if (CORE_TRACE)
                    810:                    HTTrace("Host info... Moving %d Net objects from pipe line to pending queue\n", piped);
                    811:                if (!host->pending) host->pending = HTList_new();
                    812:                for (cnt=0; cnt<piped; cnt++) {
2.18      eric      813:                    HTNet * net = HTList_removeLastObject(host->pipeline);
                    814:                    if (CORE_TRACE) HTTrace("Host info... Resetting net object %p\n", net);
2.13      frystyk   815:                    (*net->event.cbf)(HTChannel_socket(host->channel), net->event.param, HTEvent_RESET);
2.8       frystyk   816:                    HTList_appendObject(host->pending, net);
                    817:                }
2.18      eric      818:                HTChannel_setSemaphore(host->channel, 0);
                    819:                HTHost_clearChannel(host, HT_INTERRUPTED);
2.8       frystyk   820:            }
2.24      frystyk   821:        }
                    822: 
                    823:        /*
                    824:        **  If we know that this host is bad then we don't allow anything than
                    825:        **  single mode. We can't recover connections for the rest of our life
                    826:        */
                    827:        if (mode == HT_TP_PIPELINE && host->recovered > MAX_HOST_RECOVER) {
                    828:            if (PROT_TRACE)
                    829:                HTTrace("Host info... %p is bad for pipelining so we won't do it!!!\n",
                    830:                        host);
                    831:        } else {
                    832:            host->mode = mode;
                    833:            if (PROT_TRACE)
                    834:                HTTrace("Host info... New mode is %d for host %p\n", host->mode, host);
                    835:        }
2.8       frystyk   836:     }
                    837:     return NO;
                    838: }
                    839: 
                    840: /*
                    841: **     Check whether a host is idle meaning if it is ready for a new
                    842: **     request which depends on the mode of the host. If the host is 
                    843: **     idle, i.e. ready for use then return YES else NO. If the host supports
                    844: **     persistent connections then still only return idle if no requests are
                    845: **     ongoing. 
                    846: */
                    847: PUBLIC BOOL HTHost_isIdle (HTHost * host)
                    848: {
2.32      frystyk   849:     return (host && HTList_isEmpty(host->pipeline));
2.8       frystyk   850: }
                    851: 
2.13      frystyk   852: PRIVATE BOOL _roomInPipe (HTHost * host)
                    853: {
                    854:     int count;
2.34      frystyk   855:     if (!host ||
                    856:        (host->reqsPerConnection && host->reqsMade >= host->reqsPerConnection) ||
                    857:        HTHost_closeNotification(host))
2.32      frystyk   858:        return NO;
2.13      frystyk   859:     count = HTList_count(host->pipeline);
                    860:     switch (host->mode) {
                    861:     case HT_TP_SINGLE:
                    862:        return count <= 0;
                    863:     case HT_TP_PIPELINE:
                    864:        return count < MAX_PIPES;
                    865:     case HT_TP_INTERLEAVE:
                    866:        return YES;
                    867:     }
                    868:     return NO;
                    869: }
                    870: 
2.8       frystyk   871: /*
                    872: **     Add a net object to the host object. If the host
                    873: **     is idle then add to active list (pipeline) else add
                    874: **     it to the pending list
                    875: **     Return HT_PENDING if we must pend, HT_OK, or HT_ERROR
                    876: */
                    877: PUBLIC int HTHost_addNet (HTHost * host, HTNet * net)
                    878: {
                    879:     if (host && net) {
                    880:        int status = HT_OK;
2.32      frystyk   881:        BOOL doit = (host->doit==net);
2.8       frystyk   882: 
2.18      eric      883:        /*
                    884:        **  If we don't have a socket already then check to see if we can get
                    885:        **  one. Otherwise we put the host object into our pending queue.
                    886:        */      
                    887:        if (!host->channel && HTNet_availableSockets() <= 0) {
2.8       frystyk   888:            if (!PendHost) PendHost = HTList_new();
                    889:            if (CORE_TRACE)
                    890:                HTTrace("Host info... Add Host %p as pending\n", host);
                    891:            HTList_addObject(PendHost, host);
                    892:            status = HT_PENDING;
                    893:        }
                    894: 
2.18      eric      895:        /*
                    896:        **  Add net object to either active or pending queue.
                    897:        */
2.32      frystyk   898:        if (_roomInPipe(host) && (HTList_isEmpty(host->pending) || doit)) {
                    899:            if (doit) host->doit = NULL;
2.8       frystyk   900:            if (!host->pipeline) host->pipeline = HTList_new();
                    901:            HTList_addObject(host->pipeline, net);
2.32      frystyk   902:            host->reqsMade++;
                    903:             if (CORE_TRACE)
                    904:                HTTrace("Host info... Add Net %p (request %p) to pipe, %d requests made, %d requests in pipe, %d pending\n",
                    905:                        net, net->request, host->reqsMade, HTList_count(host->pipeline), HTList_count(host->pending));
2.18      eric      906: 
2.13      frystyk   907:            /*
2.30      frystyk   908:            **  If we have been idle then make sure we delete the timer
2.13      frystyk   909:            */
2.30      frystyk   910:            if (host->timer) {
                    911:                HTTimer_delete(host->timer);
                    912:                host->timer = NULL;
                    913:            }
                    914: 
2.8       frystyk   915:        } else {
                    916:            if (!host->pending) host->pending = HTList_new();
                    917:            HTList_addObject(host->pending, net);
2.32      frystyk   918:            if (CORE_TRACE)
                    919:                HTTrace("Host info... Add Net %p (request %p) to pending, %d requests made, %d requests in pipe, %d pending\n",
                    920:                        net, net->request, host->reqsMade, HTList_count(host->pipeline), HTList_count(host->pending));
2.8       frystyk   921:            status = HT_PENDING;
                    922:        }
                    923:        return status;
                    924:     }
                    925:     return HT_ERROR;
                    926: }
                    927: 
2.13      frystyk   928: PUBLIC BOOL HTHost_free (HTHost * host, int status)
                    929: {
2.32      frystyk   930:     if (host->channel) {
2.13      frystyk   931: 
2.32      frystyk   932:        /* Check if we should keep the socket open */
                    933:         if (HTHost_isPersistent(host)) {
2.37    ! frystyk   934:             if (HTHost_closeNotification(host)) {
        !           935:                int piped = HTList_count(host->pipeline);
        !           936:                if (CORE_TRACE)
        !           937:                    HTTrace("Host Object. got close notifiation on socket %d\n",
        !           938:                            HTChannel_socket(host->channel));
        !           939:                 
        !           940:                /*
        !           941:                **  If more than a single element (this one) in the pipe
        !           942:                **  then we have to recover gracefully
        !           943:                */
        !           944:                if (piped > 1) {
        !           945:                    host->reqsPerConnection = host->reqsMade - piped;
        !           946:                    if (CORE_TRACE)
        !           947:                        HTTrace("%d requests made, %d in pipe, max %d requests pr connection\n",
        !           948:                                host->reqsMade, piped, host->reqsPerConnection);
        !           949:                    host->do_recover = YES;
        !           950:                }
        !           951: 
        !           952:                 HTChannel_delete(host->channel, status);
        !           953: 
        !           954:            } else if ((HTList_count(host->pipeline) <= 1 &&
        !           955:                        host->reqsMade == host->reqsPerConnection)) {
        !           956:                 if (CORE_TRACE) HTTrace("Host Object. closing persistent socket %d\n",
        !           957:                                        HTChannel_socket(host->channel));
2.32      frystyk   958:                 
                    959:                 /* 
                    960:                 **  By lowering the semaphore we make sure that the channel
                    961:                 **  is gonna be deleted
                    962:                 */
                    963:                 HTChannel_setSemaphore(host->channel, 0);
                    964:                 HTHost_clearChannel(host, status);
                    965: 
                    966:             } else {
                    967:                 if (CORE_TRACE) HTTrace("Host Object. keeping persistent socket %d\n", HTChannel_socket(host->channel));
                    968:                 HTChannel_delete(host->channel, status);
                    969:                 
                    970:                 /*
                    971:                 **  If connection is idle then set a timer so that we close the 
                    972:                 **  connection if idle too long
                    973:                 */
                    974:                 if (HTHost_isIdle(host) && HTList_isEmpty(host->pending) && !host->timer) {
                    975:                     host->timer = HTTimer_new(NULL, TimeoutEvent, host, TCP_IDLE_TTL, YES);
                    976:                     if (PROT_TRACE) HTTrace("Host........ Object %p going idle...\n", host);
                    977:                 }
                    978:             }
                    979:             return YES;
                    980:         } else {
2.33      frystyk   981:             if (CORE_TRACE) HTTrace("Host Object. closing socket %d\n", HTChannel_socket(host->channel));
2.32      frystyk   982:             
                    983:             /* 
                    984:             **  By lowering the semaphore we make sure that the channel
                    985:             **  is gonna be deleted
                    986:             */
                    987:             HTChannel_setSemaphore(host->channel, 0);
                    988:             HTHost_clearChannel(host, status);
                    989:         }
2.13      frystyk   990:     }
2.32      frystyk   991:     return NO;
2.13      frystyk   992: }
                    993: 
2.8       frystyk   994: PUBLIC BOOL HTHost_deleteNet (HTHost * host, HTNet * net)
                    995: {
                    996:     if (host && net) {
2.32      frystyk   997:         if (CORE_TRACE) HTTrace("Host info... Remove %p from pipe\n", net);
2.8       frystyk   998:        HTList_removeObject(host->pipeline, net);
2.32      frystyk   999:        HTList_removeObject(host->pending, net); /* just to make sure */
2.8       frystyk  1000:        return YES;
                   1001:     }
                   1002:     return NO;
                   1003: }
                   1004: 
                   1005: /*
                   1006: **     Handle pending host objects.
                   1007: **     There are two ways we can end up with pending reqyests:
                   1008: **      1) If we are out of sockets then register new host objects as pending.
                   1009: **      2) If we are pending on a connection then register new net objects as
                   1010: **         pending
                   1011: **     This set of functions handles pending host objects and can start new
                   1012: **     requests as resources get available
                   1013: */
                   1014: 
                   1015: /*
                   1016: **     Check this host object for any pending requests and return the next
                   1017: **     registered Net object.
                   1018: */
                   1019: PUBLIC HTNet * HTHost_nextPendingNet (HTHost * host)
                   1020: {
                   1021:     HTNet * net = NULL;
2.32      frystyk  1022:     if (host && host->pending) {
2.18      eric     1023:        /*JK 23/Sep/96 Bug correction. Associated the following lines to the
                   1024:        **above if. There was a missing pair of brackets. 
                   1025:        */
                   1026:        if ((net = (HTNet *) HTList_removeFirstObject(host->pending)) != NULL) {
2.32      frystyk  1027:            if (CORE_TRACE)
                   1028:                HTTrace("Host info... Popping %p from pending net queue\n", net);
2.33      frystyk  1029: #if 0
                   1030:            {
                   1031:                HTRequest * request = HTNet_request(net);
                   1032:                char * uri = HTAnchor_address((HTAnchor *) HTRequest_anchor(request));
                   1033:                fprintf(stderr, "Popping '%s'\n", uri);
                   1034:            }
                   1035: #endif
2.32      frystyk  1036:            host->doit = net;
2.18      eric     1037:        }
2.8       frystyk  1038:     }
                   1039:     return net;
                   1040: }
                   1041: 
                   1042: /*
2.13      frystyk  1043: **     Return the current list of pending host objects waiting for a socket
2.8       frystyk  1044: */
                   1045: PUBLIC HTHost * HTHost_nextPendingHost (void)
                   1046: {
                   1047:     HTHost * host = NULL;
                   1048:     if (PendHost) {
                   1049:        if ((host = (HTHost *) HTList_removeFirstObject(PendHost)) != NULL)
                   1050:            if (PROT_TRACE)
2.32      frystyk  1051:                HTTrace("Host info... Popping %p from pending host queue\n",
2.8       frystyk  1052:                        host);
                   1053:     }
                   1054:     return host;
                   1055: }
                   1056: 
                   1057: /*
                   1058: **     Start the next pending request if any. First we look for pending
                   1059: **     requests for the same host and then we check for any other pending
                   1060: **     hosts
                   1061: */
                   1062: PUBLIC BOOL HTHost_launchPending (HTHost * host)
                   1063: {
                   1064:     int available = HTNet_availableSockets();
                   1065: 
                   1066:     if (!host) {
                   1067:        if (PROT_TRACE) HTTrace("Host info... Bad arguments\n");
                   1068:        return NO;
                   1069:     }
                   1070: 
                   1071:     /*
                   1072:     **  Check if we do have resources available for a new request
                   1073:     **  This can either be reusing an existing connection or opening a new one
                   1074:     */
                   1075:     if (available > 0 || host->mode >= HT_TP_PIPELINE) {
2.23      eric     1076:        HTNet * net;
2.8       frystyk  1077: 
                   1078:        /*
2.13      frystyk  1079:        **  In pipeline we can only have one doing writing at a time.
                   1080:        **  We therefore check that there are no other Net object
                   1081:        **  registered for write
                   1082:        */
                   1083:        if (host->mode == HT_TP_PIPELINE) {
2.23      eric     1084:            net = (HTNet *) HTList_lastObject(host->pipeline);
                   1085:            if (net && net->registeredFor == HTEvent_WRITE)
2.13      frystyk  1086:                return NO;
                   1087:        }
                   1088: 
                   1089:        /*
                   1090:        **  Check the current Host object for pending Net objects
2.23      eric     1091:        **
                   1092:        **  Send out as many as will fit in pipe.
2.8       frystyk  1093:        */
2.23      eric     1094:        while (_roomInPipe(host) && (net = HTHost_nextPendingNet(host))) {
2.32      frystyk  1095:            int status;
                   1096:            if (CORE_TRACE)
                   1097:                HTTrace("Launch pending net object %p with %d reqs in pipe (%d reqs made)\n",
                   1098:                        net, HTList_count(host->pipeline), host->reqsMade);
                   1099:            status = HTNet_execute(net, HTEvent_WRITE);
2.23      eric     1100:            if (status != HT_OK)
                   1101:                return status;
2.32      frystyk  1102: 
                   1103:         }
2.8       frystyk  1104: 
                   1105:        /*
                   1106:        **  Check for other pending Host objects
                   1107:        */
                   1108:        {
                   1109:            HTHost * pending = HTHost_nextPendingHost();
                   1110:            if (pending) {
                   1111:                HTNet * net = HTHost_nextPendingNet(pending);
2.13      frystyk  1112:                if (net) return HTNet_execute(net, HTEvent_WRITE);
2.8       frystyk  1113:            }
                   1114:        }
2.13      frystyk  1115:     } else
                   1116:        if (PROT_TRACE) HTTrace("Host info... No more requests.\n");
                   1117:     return NO;
                   1118: }
                   1119: 
                   1120: PUBLIC HTNet * HTHost_firstNet (HTHost * host)
                   1121: {
                   1122:     return (HTNet *) HTList_firstObject(host->pipeline);
                   1123: }
                   1124: 
                   1125: /*
                   1126: **     The host event manager keeps track of the state of it's client engines
                   1127: **     (typically HTTPEvent), accepting multiple blocks on read or write from
                   1128: **     multiple pipelined engines. It then registers its own engine 
                   1129: **     (HostEvent) with the event manager.
                   1130: */
                   1131: PUBLIC int HTHost_connect (HTHost * host, HTNet * net, char * url, HTProtocolId port)
                   1132: {
                   1133:     int status;
                   1134:     status = HTDoConnect(net, url, port);
2.32      frystyk  1135:     if (status == HT_OK) return HT_OK;
2.13      frystyk  1136:     if (status == HT_WOULD_BLOCK || status == HT_PENDING)
                   1137:        return HT_WOULD_BLOCK;
                   1138:     return HT_ERROR; /* @@@ - some more deletion and stuff here? */
                   1139: }
                   1140: 
                   1141: /*
                   1142: **     Rules: SINGLE: one element in pipe, either reading or writing
                   1143: **              PIPE: n element in pipe, n-1 reading, 1 writing
                   1144: */
                   1145: PUBLIC int HTHost_register (HTHost * host, HTNet * net, HTEventType type)
                   1146: {
                   1147:     if (host && net) {
                   1148: 
2.28      frystyk  1149:        if (type == HTEvent_CLOSE) {
2.13      frystyk  1150: 
2.28      frystyk  1151:            /*
                   1152:            **  Unregister this host for all events
                   1153:            */
                   1154:            HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
                   1155:            HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
                   1156:            host->registeredFor = 0;
2.13      frystyk  1157:            return YES;
2.28      frystyk  1158: 
                   1159:        } else {
                   1160: 
                   1161:            /* net object may already be registered */
                   1162:            if (HTEvent_BITS(type) & net->registeredFor)
                   1163:                return NO;
                   1164:            net->registeredFor ^= HTEvent_BITS(type);
                   1165: 
                   1166:            /* host object may already be registered */
                   1167:            if (host->registeredFor & HTEvent_BITS(type))
                   1168:                return YES;
                   1169:            host->registeredFor ^= HTEvent_BITS(type);
                   1170:            return HTEvent_register(HTChannel_socket(host->channel),
                   1171:                                    type, *(host->events+HTEvent_INDEX(type)));
                   1172:        }
2.13      frystyk  1173:     }
                   1174:     return NO;
                   1175: }
                   1176: 
                   1177: PUBLIC int HTHost_unregister (HTHost * host, HTNet * net, HTEventType type)
                   1178: {
                   1179:     if (host && net) {
                   1180: 
2.28      frystyk  1181:        /* net object may not be registered */
2.13      frystyk  1182:        if (!(HTEvent_BITS(type) & net->registeredFor))
                   1183:            return NO;
                   1184:        net->registeredFor ^= HTEvent_BITS(type);
                   1185: 
2.28      frystyk  1186:        /* host object may not be registered */
2.13      frystyk  1187:        if (!(host->registeredFor & HTEvent_BITS(type)))
                   1188:            return YES;
                   1189:        host->registeredFor ^= HTEvent_BITS(type);
                   1190: 
                   1191:        /* stay registered for READ to catch a socket close */
                   1192:        /* WRITE and CONNECT can be unregistered, though */
                   1193:        if ((type == HTEvent_WRITE && isLastInPipe(host, net)) || 
                   1194:            type == HTEvent_CONNECT)
                   1195:            /* if we are blocked downstream, shut down the whole pipe */
                   1196:            HTEvent_unregister(HTChannel_socket(host->channel), type);
                   1197:        return YES;
                   1198:     }
                   1199:     return NO;
                   1200: }
                   1201: 
                   1202: /*
                   1203: **     The reader tells HostEvent that it's stream did not finish the data
                   1204: */
                   1205: PUBLIC BOOL HTHost_setRemainingRead (HTHost * host, size_t remaining)
                   1206: {
                   1207:     if (host == NULL) return NO;
                   1208:     host->remainingRead = remaining;
2.20      frystyk  1209:     if (PROT_TRACE) HTTrace("Host........ %d bytes remaining \n", remaining);
2.13      frystyk  1210:     return YES;
                   1211: }
                   1212: 
2.32      frystyk  1213: PUBLIC size_t HTHost_remainingRead (HTHost * host)
                   1214: {
                   1215:     return host ? host->remainingRead : -1;
                   1216: }
                   1217: 
2.13      frystyk  1218: PUBLIC SockA * HTHost_getSockAddr (HTHost * host)
                   1219: {
                   1220:     if (!host) return NULL;
                   1221:     return &host->sock_addr;
                   1222: }
                   1223: 
                   1224: PUBLIC BOOL HTHost_setHome (HTHost * host, int home)
                   1225: {
                   1226:     if (!host) return NO;
                   1227:     host->home = home;
                   1228:     return YES;
                   1229: }
                   1230: 
                   1231: PUBLIC int HTHost_home (HTHost * host)
                   1232: {
                   1233:     if (!host) return 0;
                   1234:     return host->home;
                   1235: }
                   1236: 
2.27      frystyk  1237: PUBLIC BOOL HTHost_setRetry (HTHost * host, int retry)
                   1238: {
                   1239:     if (!host) return NO;
                   1240:     host->retry = retry;
                   1241:     return YES;
                   1242: }
                   1243: 
                   1244: PUBLIC BOOL HTHost_decreaseRetry (HTHost * host)
                   1245: {
                   1246:     if (!host) {
                   1247:        if (host->retry > 0) host->retry--;
                   1248:        return YES;
                   1249:     }
                   1250:     return NO;
                   1251: }
                   1252: 
                   1253: PUBLIC int HTHost_retry (HTHost * host)
                   1254: {
                   1255:     if (!host) return 0;
                   1256:     return host->retry;
                   1257: }
                   1258: 
2.13      frystyk  1259: #if 0  /* Is a macro right now */
2.21      frystyk  1260: PRIVATE BOOL HTHost_setDNS5 (HTHost * host, HTdns * dns)
2.13      frystyk  1261: {
                   1262:     if (!host) return NO;
                   1263:     host->dns = dns;
                   1264:     return YES;
                   1265: }
                   1266: #endif
                   1267: 
                   1268: PUBLIC BOOL HTHost_setChannel (HTHost * host, HTChannel * channel)
                   1269: {
                   1270:     if (!host) return NO;
                   1271:     host->channel = channel;
                   1272:     return YES;
                   1273: }
                   1274: 
                   1275: PUBLIC HTNet * HTHost_getReadNet(HTHost * host)
                   1276: {
                   1277:     if (host) {
                   1278:        if (host->mode == HT_TP_INTERLEAVE) {
2.21      frystyk  1279: #ifdef HT_MUX
2.13      frystyk  1280:            HTMuxChannel * muxch = HTMuxChannel_find(host);
                   1281:            return HTMuxChannel_net(muxch);
2.21      frystyk  1282: #endif
2.13      frystyk  1283:        }
2.32      frystyk  1284:         return (HTNet *) HTList_firstObject(host->pipeline);
2.13      frystyk  1285:     }
                   1286:     return NULL;
                   1287: }
                   1288: 
                   1289: PUBLIC HTNet * HTHost_getWriteNet(HTHost * host)
                   1290: {
                   1291:     return host ? (HTNet *) HTList_lastObject(host->pipeline) : NULL;
                   1292: }
                   1293: 
                   1294: /*
                   1295: **     Create the input stream and bind it to the channel
                   1296: **     Please read the description in the HTIOStream module for the parameters
                   1297: */
                   1298: PUBLIC HTInputStream * HTHost_getInput (HTHost * host, HTTransport * tp,
                   1299:                                        void * param, int mode)
                   1300: {
                   1301:     if (host && host->channel && tp) {
                   1302:        HTChannel * ch = host->channel;
                   1303:        HTInputStream * input = (*tp->input_new)(host, ch, param, mode);
                   1304:        HTChannel_setInput(ch, input);
                   1305:        return HTChannel_getChannelIStream(ch);
                   1306:     }
2.24      frystyk  1307:     if (CORE_TRACE) HTTrace("Host Object. Can't create input stream\n");
2.13      frystyk  1308:     return NULL;
                   1309: }
                   1310: 
                   1311: PUBLIC HTOutputStream * HTHost_getOutput (HTHost * host, HTTransport * tp,
                   1312:                                          void * param, int mode)
                   1313: {
                   1314:     if (host && host->channel && tp) {
                   1315:        HTChannel * ch = host->channel;
                   1316:        HTOutputStream * output = (*tp->output_new)(host, ch, param, mode);
                   1317:        HTChannel_setOutput(ch, output);
                   1318:        return output;
                   1319:     }
2.24      frystyk  1320:     if (CORE_TRACE) HTTrace("Host Object. Can't create output stream\n");
2.13      frystyk  1321:     return NULL;
                   1322: }
                   1323: 
                   1324: PUBLIC HTOutputStream * HTHost_output (HTHost * host, HTNet * net)
                   1325: {
                   1326:     if (host && host->channel && net) {
                   1327:        HTOutputStream * output = HTChannel_output(host->channel);
2.8       frystyk  1328: 
                   1329:        /*
2.13      frystyk  1330:        **  If we are in MUX mode then create new output stream on top
                   1331:        **  of the already existing one. Otherwise just return what we
                   1332:        **  have.
2.8       frystyk  1333:        */
2.13      frystyk  1334:        if (host->mode == HT_TP_INTERLEAVE) {
2.21      frystyk  1335: #ifdef HT_MUX
2.13      frystyk  1336:            HTStream * target = (HTStream *) HTChannel_output(host->channel);
                   1337:            output = HTMuxWriter_new(host, net, target);
2.21      frystyk  1338: #endif
2.8       frystyk  1339:        }
2.13      frystyk  1340:        return output;
                   1341:     }
                   1342:     return NULL;
                   1343: }
                   1344: 
                   1345: PUBLIC int HTHost_read(HTHost * host, HTNet * net)
                   1346: {
                   1347:     HTInputStream * input = HTChannel_input(host->channel);
                   1348:     if (net != HTHost_getReadNet(host)) {
                   1349:        HTHost_register(host, net, HTEvent_READ);
                   1350:        return HT_WOULD_BLOCK;
                   1351:     }
2.17      frystyk  1352: 
                   1353:     /*
                   1354:     **  If there is no input channel then this can either mean that
                   1355:     **  we have lost the channel or an error occurred. We return
                   1356:     **  HT_CLOSED as this is a sign to the caller that we don't 
                   1357:     **  have a channel
                   1358:     */
                   1359:     return input ? (*input->isa->read)(input) : HT_CLOSED;
2.13      frystyk  1360: }
                   1361: 
                   1362: PUBLIC BOOL HTHost_setConsumed(HTHost * host, size_t bytes)
                   1363: {
                   1364:     HTInputStream * input;
                   1365:     if (!host || !host->channel) return NO;
                   1366:     if ((input = HTChannel_input(host->channel)) == NULL)
                   1367:        return NO;
2.32      frystyk  1368:     if (CORE_TRACE)
2.20      frystyk  1369:        HTTrace("Host........ passing %d bytes as consumed to %p\n", bytes, input);
2.13      frystyk  1370:     return (*input->isa->consumed)(input, bytes);
                   1371: }
                   1372: 
                   1373: PUBLIC int HTHost_hash (HTHost * host)
                   1374: {
                   1375:     return host ? host->hash : -1;
                   1376: }
                   1377: 
2.26      frystyk  1378: PUBLIC BOOL HTHost_setWriteDelay (HTHost * host, ms_t delay)
2.13      frystyk  1379: {
2.26      frystyk  1380:     if (host && delay >= 0) {
                   1381:        host->delay = delay;
                   1382:        return YES;
                   1383:     }
                   1384:     return NO;
                   1385: }
                   1386: 
                   1387: PUBLIC ms_t HTHost_writeDelay (HTHost * host)
                   1388: {
                   1389:     return host ? host->delay : 0;
                   1390: }
                   1391: 
                   1392: PUBLIC int HTHost_findWriteDelay (HTHost * host, ms_t lastFlushTime, int buffSize)
                   1393: {
2.35      frystyk  1394: #if 0
2.15      eric     1395:     unsigned short mtu;
2.18      eric     1396:     int ret = -1;
2.15      eric     1397:     int socket = HTChannel_socket(host->channel);
2.18      eric     1398: #ifndef WWW_MSWINDOWS
2.15      eric     1399:     ret = ioctl(socket, 666, (unsigned long)&mtu);
2.18      eric     1400: #endif /* WWW_MSWINDOWS */
2.15      eric     1401:     if ((ret == 0 && buffSize >= mtu) || host->forceWriteFlush)
2.13      frystyk  1402:        return 0;
2.26      frystyk  1403:     return host->delay;
2.35      frystyk  1404: #else
                   1405:     return host->forceWriteFlush ? 0 : host->delay;
                   1406: #endif
2.13      frystyk  1407: }
                   1408: 
2.26      frystyk  1409: PUBLIC BOOL HTHost_setDefaultWriteDelay (ms_t delay)
                   1410: {
                   1411:     if (delay >= 0) {
                   1412:        WriteDelay = delay;
                   1413:        if (CORE_TRACE) HTTrace("Host........ Default write delay is %d ms\n", delay);
                   1414:        return YES;
                   1415:     }
                   1416:     return NO;
                   1417: }
                   1418: 
                   1419: PUBLIC ms_t HTHost_defaultWriteDelay (void)
                   1420: {
                   1421:     return WriteDelay;
                   1422: }
                   1423: 
2.13      frystyk  1424: PUBLIC int HTHost_forceFlush(HTHost * host)
                   1425: {
2.35      frystyk  1426:     HTNet * targetNet = (HTNet *) HTList_lastObject(host->pipeline);
2.13      frystyk  1427:     int ret;
2.35      frystyk  1428:     if (targetNet == NULL) return HT_ERROR;
2.13      frystyk  1429:     if (CORE_TRACE)
2.28      frystyk  1430:        HTTrace("Host Event.. FLUSH passed to `%s\'\n", 
                   1431:                HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet))));
2.13      frystyk  1432:     host->forceWriteFlush = YES;
                   1433:     ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, HTEvent_FLUSH);
2.35      frystyk  1434:     host->forceWriteFlush = NO;
2.13      frystyk  1435:     return ret;
2.1       frystyk  1436: }
2.11      kahan    1437: 
2.13      frystyk  1438: PUBLIC int HTHost_eventTimeout (void)
                   1439: {
                   1440:     return EventTimeout;
                   1441: }
2.11      kahan    1442: 
2.13      frystyk  1443: PUBLIC void HTHost_setEventTimeout (int millis)
                   1444: {
                   1445:     EventTimeout = millis;
                   1446:     if (CORE_TRACE) HTTrace("Host........ Setting event timeout to %d ms\n", millis);
                   1447: }

Webmaster