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

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.67    ! raff        6: **     @(#) $Id: HTHost.c,v 2.66 1999/07/07 15:43:28 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 */
2.44      frystyk    16: #include "wwwsys.h"
2.1       frystyk    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.48      frystyk    28: #define HOST_OBJECT_TTL            43200L           /* Default host timeout is 12 h */
2.29      frystyk    29: 
2.48      frystyk    30: #define TCP_IDLE_PASSIVE      120L /* Passive TTL in s on an idle connection */
                     31: #define TCP_IDLE_ACTIVE     60000L /* Active TTL in ms on an idle connection */
2.29      frystyk    32: 
2.45      frystyk    33: #define MAX_PIPES              50   /* maximum number of pipelined requests */
2.50      frystyk    34: #define MAX_HOST_RECOVER       1             /* Max number of auto recovery */
2.32      frystyk    35: #define DEFAULT_DELAY          30        /* Default write flush delay in ms */
2.1       frystyk    36: 
2.13      frystyk    37: struct _HTInputStream {
                     38:     const HTInputStreamClass * isa;
2.1       frystyk    39: };
                     40: 
2.13      frystyk    41: PRIVATE int HostEvent(SOCKET soc, void * pVoid, HTEventType type);
                     42: 
                     43: /* Type definitions and global variables etc. local to this module */
2.48      frystyk    44: PRIVATE time_t HostTimeout = HOST_OBJECT_TTL;   /* Timeout for host objects */
2.53      frystyk    45: PRIVATE time_t HTPassiveTimeout = TCP_IDLE_PASSIVE; /* Passive timeout in s */
                     46: PRIVATE ms_t   HTActiveTimeout = TCP_IDLE_ACTIVE;   /* Active timeout in ms */
2.1       frystyk    47: 
2.8       frystyk    48: PRIVATE HTList ** HostTable = NULL;
                     49: PRIVATE HTList * PendHost = NULL;          /* List of pending host elements */
2.1       frystyk    50: 
2.40      kahan      51: /* JK: New functions for interruption the automatic pending request 
                     52:    activation */
                     53: PRIVATE HTHost_ActivateRequestCallback * ActivateReqCBF = NULL;
                     54: PRIVATE int HTHost_ActivateRequest (HTNet *net);
                     55: PRIVATE BOOL DoPendingReqLaunch = YES; /* controls automatic activation
                     56:                                               of pending requests */
                     57: 
2.13      frystyk    58: PRIVATE int EventTimeout = -1;                 /* Global Host event timeout */
                     59: 
2.36      frystyk    60: PRIVATE ms_t WriteDelay = DEFAULT_DELAY;                     /* Delay in ms */
2.26      frystyk    61: 
2.45      frystyk    62: PRIVATE int MaxPipelinedRequests = MAX_PIPES;
                     63: 
2.1       frystyk    64: /* ------------------------------------------------------------------------- */
                     65: 
                     66: PRIVATE void free_object (HTHost * me)
                     67: {
                     68:     if (me) {
2.30      frystyk    69:        int i;
2.1       frystyk    70:        HT_FREE(me->hostname);
                     71:        HT_FREE(me->type);
2.12      frystyk    72:        HT_FREE(me->server);
                     73:        HT_FREE(me->user_agent);
                     74:        HT_FREE(me->range_units);
2.30      frystyk    75: 
                     76:        /* Delete the channel (if any) */
2.3       eric       77:        if (me->channel) {
2.5       eric       78:            HTChannel_delete(me->channel, HT_OK);
2.3       eric       79:            me->channel = NULL;
                     80:        }
2.30      frystyk    81: 
                     82:        /* Unregister the events */
2.18      eric       83:        for (i = 0; i < HTEvent_TYPES; i++)
                     84:            HTEvent_delete(me->events[i]);
2.30      frystyk    85: 
                     86:        /* Delete the timer (if any) */
                     87:        if (me->timer) HTTimer_delete(me->timer);
                     88: 
                     89:        /* Delete the queues */
2.8       frystyk    90:        HTList_delete(me->pipeline);
                     91:        HTList_delete(me->pending);
2.1       frystyk    92:        HT_FREE(me);
                     93:     }
                     94: }
                     95: 
                     96: PRIVATE BOOL delete_object (HTList * list, HTHost * me)
                     97: {
2.59      frystyk    98:     HTTRACE(CORE_TRACE, "Host info... object %p from list %p\n" _ me _ list);
2.1       frystyk    99:     HTList_removeObject(list, (void *) me);
                    100:     free_object(me);
                    101:     return YES;
                    102: }
                    103: 
2.13      frystyk   104: PRIVATE BOOL isLastInPipe (HTHost * host, HTNet * net)
                    105: {
                    106:     return HTList_lastObject(host->pipeline) == net;
                    107: }
                    108: 
2.51      frystyk   109: PRIVATE BOOL killPipeline (HTHost * host, HTEventType type)
2.48      frystyk   110: {
                    111:     if (host) {
                    112:        int piped = HTList_count(host->pipeline);
                    113:        int pending = HTList_count(host->pending);
                    114:        int cnt;
                    115: 
2.59      frystyk   116:        HTTRACE(CORE_TRACE, "Host kill... Pipeline due to %s event\n" _ HTEvent_type2str(type));
2.51      frystyk   117: 
2.48      frystyk   118:        /* Terminate all net objects in pending queue */
                    119:        for (cnt=0; cnt<pending; cnt++) {
                    120:            HTNet * net = HTList_removeLastObject(host->pending);
2.56      frystyk   121:            if (net) {
2.59      frystyk   122:                HTTRACE(CORE_TRACE, "Host kill... Terminating net object %p from pending queue\n" _ net);
2.56      frystyk   123:                net->registeredFor = 0;
                    124:                (*net->event.cbf)(HTChannel_socket(host->channel), net->event.param, type);
2.58      frystyk   125:                if (host->lock == net) host->lock = NULL;
2.56      frystyk   126:            }
2.48      frystyk   127:        }
                    128: 
                    129:        /* Terminate all net objects in pipeline */
                    130:        if (piped >= 1) {
                    131:            
                    132:            /*
                    133:            **  Terminte all net objects in the pipeline
                    134:            */
                    135:            for (cnt=0; cnt<piped; cnt++) {
                    136:                HTNet * net = HTList_firstObject(host->pipeline);
2.56      frystyk   137:                if (net) {
2.59      frystyk   138:                    HTTRACE(CORE_TRACE, "Host kill... Terminating net object %p from pipe line\n" _ net);
2.56      frystyk   139:                    net->registeredFor = 0;
                    140:                    (*net->event.cbf)(HTChannel_socket(host->channel), net->event.param, type);
                    141:                }
2.48      frystyk   142:            }
                    143:        }
                    144:        return YES;
                    145:     }
                    146:     return NO;
                    147: }
                    148: 
                    149: /*
2.51      frystyk   150: **  Silently close an idle persistent connection after 
2.53      frystyk   151: **  HTActiveTimeout secs
2.51      frystyk   152: */
                    153: PRIVATE int IdleTimeoutEvent (HTTimer * timer, void * param, HTEventType type)
                    154: {
                    155:     HTHost * host = (HTHost *) param;
                    156:     SOCKET sockfd = HTChannel_socket(host->channel);
2.67    ! raff      157: 
2.51      frystyk   158:     HTTimer_delete(timer);
                    159:     host->timer = NULL;
2.67    ! raff      160: 
        !           161:     return HostEvent (sockfd, host, HTEvent_CLOSE);
2.51      frystyk   162: }
                    163: 
                    164: /*
2.13      frystyk   165: **     HostEvent - host event manager - recieves events from the event 
                    166: **     manager and dispatches them to the client net objects by calling the 
                    167: **     net object's cbf.
                    168: **
                    169: */
                    170: PRIVATE int HostEvent (SOCKET soc, void * pVoid, HTEventType type)
                    171: {
                    172:     HTHost * host = (HTHost *)pVoid;
                    173: 
2.61      frystyk   174:     if (type == HTEvent_READ || type == HTEvent_CLOSE || type == HTEvent_ACCEPT) {
2.13      frystyk   175:        HTNet * targetNet;
                    176: 
                    177:        /* call the first net object */
                    178:        do {
                    179:            int ret;
2.49      frystyk   180: 
                    181:             /* netscape and apache servers can do a lazy close well after usage
                    182:              * of previous socket has been dispensed by the library,
                    183:              * the section below makes sure the event does not get miss attributed
                    184:              */
2.66      frystyk   185:            if (HTChannel_socket(host->channel) != soc && type != HTEvent_ACCEPT && !host->listening) {
                    186:                HTTRACE(CORE_TRACE, "Host Event.. wild socket %d type = %s real socket is %d\n" _ soc _ 
                    187:                        type == HTEvent_CLOSE ? "Event_Close" : "Event_Read" _ 
                    188:                        HTChannel_socket(host->channel));
                    189:                return HT_OK;
2.49      frystyk   190:            }
                    191: 
2.13      frystyk   192:            targetNet = (HTNet *)HTList_firstObject(host->pipeline);
                    193:            if (targetNet) {
2.59      frystyk   194:                HTTRACE(CORE_TRACE, "Host Event.. READ passed to `%s\'\n" _ 
2.28      frystyk   195:                            HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet))));
2.13      frystyk   196:                if ((ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), 
                    197:                                                  targetNet->event.param, type)) != HT_OK)
                    198:                    return ret;
                    199:            }
                    200:            if (targetNet == NULL && host->remainingRead > 0) {
2.59      frystyk   201:                HTTRACE(CORE_TRACE, "HostEvent... Error: %d bytes left to read and nowhere to put them\n" _ 
2.31      frystyk   202:                            host->remainingRead);
2.13      frystyk   203:                host->remainingRead = 0;
                    204:                /*
                    205:                **      Fall through to close the channel
                    206:                */
                    207:            }
                    208:        /* call pipelined net object to eat all the data in the channel */
                    209:        } while (host->remainingRead > 0);
                    210: 
                    211:        /* last target net should have set remainingRead to 0 */
                    212:        if (targetNet)
                    213:            return HT_OK;
                    214: 
                    215:        /* If there was notargetNet, it should be a close */
2.59      frystyk   216:        HTTRACE(CORE_TRACE, "Host Event.. host %p `%s\' closed connection.\n" _ 
                    217:                    host _ host->hostname);
2.13      frystyk   218: 
                    219:        /* Is there garbage in the channel? Let's check: */
                    220:        {
                    221:            char buf[256];
                    222:            int ret;
2.28      frystyk   223:            memset(buf, '\0', sizeof(buf));
2.48      frystyk   224:            while ((ret = NETREAD(HTChannel_socket(host->channel), buf, sizeof(buf)-1)) > 0) {
2.59      frystyk   225:                HTTRACE(CORE_TRACE, "Host Event.. Host %p `%s\' had %d extraneous bytes: `%s\'\n" _ 
                    226:                            host _ host->hostname _ ret _ buf);
2.28      frystyk   227:                memset(buf, '\0', sizeof(buf));         
                    228:            }       
2.13      frystyk   229:        }
                    230:        HTHost_clearChannel(host, HT_OK);
2.28      frystyk   231:        return HT_OK;        /* extra garbage does not constitute an application error */
2.13      frystyk   232:        
2.18      eric      233:     } else if (type == HTEvent_WRITE || type == HTEvent_CONNECT) {
2.13      frystyk   234:        HTNet * targetNet = (HTNet *)HTList_lastObject(host->pipeline);
                    235:        if (targetNet) {
2.59      frystyk   236:            HTTRACE(CORE_TRACE, "Host Event.. WRITE passed to `%s\'\n" _ 
2.28      frystyk   237:                        HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet))));
2.13      frystyk   238:            return (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, type);
                    239:        }
2.59      frystyk   240:        HTTRACE(CORE_TRACE, "Host Event Host %p (`%s\') dispatched with event %s but doesn't have a target - %d requests made, %d requests in pipe, %d pending\n" _ 
                    241:                    host _ 
                    242:                    host ? host->hostname : "<null>" _ 
                    243:                    HTEvent_type2str(type) _ 
                    244:                    host ? host->reqsMade : -1 _ 
                    245:                    HTList_count(host->pipeline) _ 
2.56      frystyk   246:                    HTList_count(host->pending));
2.55      frystyk   247: #if 0
2.59      frystyk   248:        HTDEBUGBREAK("Host Event.. Host %p (`%s\') dispatched with event %d\n" _ 
                    249:                     host _ host ? host->hostname : "<null>" _ type);
2.13      frystyk   250:        return HT_ERROR;
2.55      frystyk   251: #else
                    252:        return HT_OK;
                    253: #endif
2.14      frystyk   254:     } else if (type == HTEvent_TIMEOUT) {
2.51      frystyk   255:        killPipeline(host, HTEvent_TIMEOUT);
2.14      frystyk   256:     } else {
2.59      frystyk   257:        HTTRACE(CORE_TRACE, "Don't know how to handle OOB data from `%s\'?\n" _
2.44      frystyk   258:                host->hostname);
2.14      frystyk   259:     }
2.13      frystyk   260:     return HT_OK;
                    261: }
                    262: 
2.1       frystyk   263: /*
                    264: **     Search the host info cache for a host object or create a new one
                    265: **     and add it. Examples of host names are
                    266: **
                    267: **             www.w3.org
                    268: **             www.foo.com:8000
                    269: **             18.52.0.18
                    270: **
                    271: **     Returns Host object or NULL if error. You may get back an already
                    272: **     existing host object - you're not guaranteed a new one each time.
                    273: */
2.15      eric      274: PUBLIC HTHost * HTHost_new (char * host, u_short u_port)
2.1       frystyk   275: {
                    276:     HTList * list = NULL;                          /* Current list in cache */
                    277:     HTHost * pres = NULL;
2.13      frystyk   278:     int hash = 0;
2.1       frystyk   279:     if (!host) {
2.59      frystyk   280:        HTTRACE(CORE_TRACE, "Host info... Bad argument\n");
2.1       frystyk   281:        return NULL;
                    282:     }
                    283:     
                    284:     /* Find a hash for this host */
                    285:     {
                    286:        char *ptr;
                    287:        for (ptr=host; *ptr; ptr++)
2.13      frystyk   288:            hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.1       frystyk   289:        if (!HostTable) {
2.13      frystyk   290:            if ((HostTable = (HTList **) HT_CALLOC(HOST_HASH_SIZE,
2.1       frystyk   291:                                                   sizeof(HTList *))) == NULL)
                    292:                HT_OUTOFMEM("HTHost_find");
                    293:        }
                    294:        if (!HostTable[hash]) HostTable[hash] = HTList_new();
                    295:        list = HostTable[hash];
                    296:     }
                    297: 
                    298:     /* Search the cache */
                    299:     {
                    300:        HTList * cur = list;
                    301:        while ((pres = (HTHost *) HTList_nextObject(cur))) {
2.15      eric      302:            if (!strcmp(pres->hostname, host) && u_port == pres->u_port) {
2.8       frystyk   303:                if (HTHost_isIdle(pres) && time(NULL)>pres->ntime+HostTimeout){
2.59      frystyk   304:                    HTTRACE(CORE_TRACE, "Host info... Collecting host info %p\n" _ pres);
2.1       frystyk   305:                    delete_object(list, pres);
                    306:                    pres = NULL;
                    307:                }
                    308:                break;
                    309:            }
                    310:        }
                    311:     }
                    312: 
2.8       frystyk   313:     /* If not found then create new Host object, else use existing one */
2.1       frystyk   314:     if (pres) {
                    315:        if (pres->channel) {
2.32      frystyk   316: 
                    317:             /*
                    318:             **  If we have a TTL for this TCP connection then
                    319:             **  check that we haven't passed it.
                    320:             */
                    321:             if (pres->expires > 0) {
                    322:                 time_t t = time(NULL);
2.51      frystyk   323:                 if (HTHost_isIdle(pres) && pres->expires < t) {
2.59      frystyk   324:                     HTTRACE(CORE_TRACE, "Host info... Persistent channel %p gotten cold\n" _ 
2.32      frystyk   325:                         pres->channel);
2.48      frystyk   326:                    HTHost_clearChannel(pres, HT_OK);
2.32      frystyk   327:                 } else {
2.53      frystyk   328:                     pres->expires = t + HTPassiveTimeout;
2.59      frystyk   329:                     HTTRACE(CORE_TRACE, "Host info... REUSING CHANNEL %p\n" _ pres->channel);
2.32      frystyk   330:                 }
                    331:             }
2.55      frystyk   332:        } else {
2.59      frystyk   333:            HTTRACE(CORE_TRACE, "Host info... Found Host %p with no active channel\n" _ pres);
2.1       frystyk   334:        }
                    335:     } else {
                    336:        if ((pres = (HTHost *) HT_CALLOC(1, sizeof(HTHost))) == NULL)
                    337:            HT_OUTOFMEM("HTHost_add");
2.13      frystyk   338:        pres->hash = hash;
2.1       frystyk   339:        StrAllocCopy(pres->hostname, host);
2.15      eric      340:        pres->u_port = u_port;
2.1       frystyk   341:        pres->ntime = time(NULL);
2.8       frystyk   342:        pres->mode = HT_TP_SINGLE;
2.26      frystyk   343:        pres->delay = WriteDelay;
2.18      eric      344:        {
2.26      frystyk   345:            int i;
                    346:            for (i = 0; i < HTEvent_TYPES; i++)
                    347:                pres->events[i]= HTEvent_new(HostEvent, pres, HT_PRIORITY_MAX, EventTimeout);
2.18      eric      348:        }
2.59      frystyk   349:        HTTRACE(CORE_TRACE, "Host info... added `%s\' with host %p to list %p\n" _ 
                    350:                    host _ pres _ list);
2.1       frystyk   351:        HTList_addObject(list, (void *) pres);
                    352:     }
                    353:     return pres;
2.9       frystyk   354: }
                    355: 
2.15      eric      356: PUBLIC HTHost * HTHost_newWParse (HTRequest * request, char * url, u_short u_port)
2.13      frystyk   357: {
2.32      frystyk   358:     char * port;
                    359:     char * fullhost = NULL;
                    360:     char * parsedHost = NULL;
                    361:     SockA * sin;
                    362:     HTHost * me;
                    363:     char * proxy = HTRequest_proxy(request);
2.13      frystyk   364: 
2.32      frystyk   365:     fullhost = HTParse(proxy ? proxy : url, "", PARSE_HOST);
2.13      frystyk   366: 
2.66      frystyk   367:     /* If there's an @ then use the stuff after it as a hostname */
2.32      frystyk   368:     if (fullhost) {
                    369:        char * at_sign;
                    370:        if ((at_sign = strchr(fullhost, '@')) != NULL)
                    371:            parsedHost = at_sign+1;
                    372:        else
                    373:            parsedHost = fullhost;
                    374:     }
                    375:     if (!parsedHost || !*parsedHost) {
                    376:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_HOST,
2.52      frystyk   377:                           NULL, 0, "HTHost_newWParse");
2.32      frystyk   378:        HT_FREE(fullhost);
                    379:        return NULL;
                    380:     }
2.55      frystyk   381: 
                    382:     /* See if the default port should be overridden */
                    383:     if ((port = strchr(parsedHost, ':')) != NULL) {
2.32      frystyk   384:        *port++ = '\0';
2.55      frystyk   385:        if (*port && isdigit((int) *port)) u_port = (u_short) atol(port);
2.32      frystyk   386:     }
2.59      frystyk   387:     HTTRACE(PROT_TRACE, "HTHost parse Looking up `%s\' on port %u\n" _ parsedHost _ u_port);
2.55      frystyk   388: 
2.32      frystyk   389:     /* Find information about this host */
                    390:     if ((me = HTHost_new(parsedHost, u_port)) == NULL) {
2.59      frystyk   391:        HTTRACE(PROT_TRACE, "HTHost parse Can't get host info\n");
2.32      frystyk   392:        me->tcpstate = TCP_ERROR;
                    393:        return NULL;
                    394:     }
                    395:     sin = &me->sock_addr;
                    396:     memset((void *) sin, '\0', sizeof(SockA));
2.13      frystyk   397: #ifdef DECNET
2.32      frystyk   398:     sin->sdn_family = AF_DECnet;
                    399:     net->sock_addr.sdn_objnum = port ? (unsigned char)(strtol(port, (char **) 0, 10)) : DNP_OBJ;
2.13      frystyk   400: #else  /* Internet */
2.32      frystyk   401:     sin->sin_family = AF_INET;
                    402:     sin->sin_port = htons(u_port);
2.13      frystyk   403: #endif
2.32      frystyk   404:     HT_FREE(fullhost); /* parsedHost points into fullhost */
                    405:     return me;
2.13      frystyk   406: }
                    407: 
2.9       frystyk   408: /*
                    409: **     Search the host info cache for a host object. Examples of host names:
                    410: **
                    411: **             www.w3.org
                    412: **             www.foo.com:8000
                    413: **             18.52.0.18
                    414: **
                    415: **     Returns Host object or NULL if not found.
                    416: */
                    417: PUBLIC HTHost * HTHost_find (char * host)
                    418: {
                    419:     HTList * list = NULL;                          /* Current list in cache */
                    420:     HTHost * pres = NULL;
2.59      frystyk   421:     HTTRACE(CORE_TRACE, "Host info... Looking for `%s\'\n" _ host ? host : "<null>");
2.9       frystyk   422: 
                    423:     /* Find a hash for this host */
                    424:     if (host && HostTable) {
                    425:        int hash = 0;
                    426:        char *ptr;
                    427:        for (ptr=host; *ptr; ptr++)
2.13      frystyk   428:            hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.9       frystyk   429:        if (!HostTable[hash]) return NULL;
                    430:        list = HostTable[hash];
                    431: 
                    432:        /* Search the cache */
                    433:        {
                    434:            HTList * cur = list;
                    435:            while ((pres = (HTHost *) HTList_nextObject(cur))) {
                    436:                if (!strcmp(pres->hostname, host)) {
                    437:                    if (time(NULL) > pres->ntime + HostTimeout) {
2.59      frystyk   438:                        HTTRACE(CORE_TRACE, "Host info... Collecting host %p\n" _ pres);
2.9       frystyk   439:                        delete_object(list, pres);
                    440:                        pres = NULL;
                    441:                    } else {
2.59      frystyk   442:                        HTTRACE(CORE_TRACE, "Host info... Found `%s\'\n" _ host);
2.9       frystyk   443:                    }
                    444:                    return pres;
                    445:                }
                    446:            }
                    447:        }
                    448:     }
                    449:     return NULL;
2.1       frystyk   450: }
                    451: 
                    452: /*
2.62      frystyk   453: **     Cleanup the host list
                    454: */
                    455: PUBLIC void HTHost_deleteAll (void)
                    456: {
                    457:     HTList * list;
                    458:     HTHost * host;
                    459:     int i;
                    460: 
                    461:     if (!HostTable)
                    462:        return;
                    463: 
                    464:     for (i=0; i < HOST_HASH_SIZE; i++) {
                    465:        list = HostTable[i];
                    466:        if (!list) continue;
                    467: 
                    468:        while ((host = (HTHost *) HTList_removeFirstObject(list)) != NULL)
                    469:            free_object(host);
                    470: 
                    471:        HTList_delete(list);
                    472:     }
                    473: 
                    474:     HT_FREE(HostTable);
                    475:     HostTable = NULL;
                    476: }
                    477: 
                    478: /*
2.8       frystyk   479: **     Get and set the hostname of the remote host
                    480: */
                    481: PUBLIC char * HTHost_name (HTHost * host)
                    482: {
                    483:      return host ? host->hostname : NULL;
                    484: }
                    485: 
                    486: /*
2.1       frystyk   487: **     Get and set the type class of the remote host
                    488: */
                    489: PUBLIC char * HTHost_class (HTHost * host)
                    490: {
                    491:      return host ? host->type : NULL;
                    492: }
                    493: 
                    494: PUBLIC void HTHost_setClass (HTHost * host, char * s_class)
                    495: {
                    496:     if (host && s_class) StrAllocCopy(host->type, s_class);
                    497: }
                    498: 
                    499: /*
                    500: **     Get and set the version of the remote host
                    501: */
                    502: PUBLIC int HTHost_version (HTHost *host)
                    503: {
                    504:      return host ? host->version : 0;
                    505: }
                    506: 
                    507: PUBLIC void HTHost_setVersion (HTHost * host, int version)
                    508: {
                    509:     if (host) host->version = version;
                    510: }
                    511: 
                    512: /*
2.53      frystyk   513: **  Get and set the passive timeout for persistent entries.
2.1       frystyk   514: */
2.53      frystyk   515: PUBLIC BOOL HTHost_setPersistTimeout (time_t timeout)
2.1       frystyk   516: {
2.53      frystyk   517:     if (timeout > 0) {
                    518:        HTPassiveTimeout = timeout;
                    519:        return YES;
                    520:     }
                    521:     return NO;
                    522: }
                    523: 
                    524: PUBLIC time_t HTHost_persistTimeout (void)
                    525: {
                    526:     return HTPassiveTimeout;
                    527: }
                    528: 
                    529: /*
                    530: **  Get and set the active timeout for persistent entries.
                    531: */
                    532: PUBLIC BOOL HTHost_setActiveTimeout (ms_t timeout)
                    533: {
                    534:     if (timeout > 1000) {
                    535:        HTActiveTimeout = timeout;
                    536:        return YES;
                    537:     }
                    538:     return NO;
2.1       frystyk   539: }
                    540: 
2.53      frystyk   541: PUBLIC ms_t HTHost_activeTimeout (void)
2.1       frystyk   542: {
2.53      frystyk   543:     return HTActiveTimeout;
2.1       frystyk   544: }
                    545: 
                    546: /*     Persistent Connection Expiration
                    547: **     --------------------------------
                    548: **     Should normally not be used. If, then use calendar time.
                    549: */
                    550: PUBLIC void HTHost_setPersistExpires (HTHost * host, time_t expires)
                    551: {
                    552:     if (host) host->expires = expires;
                    553: }
                    554: 
                    555: PUBLIC time_t HTHost_persistExpires (HTHost * host)
                    556: {
                    557:     return host ? host->expires : -1;
                    558: }
                    559: 
2.22      eric      560: PUBLIC void HTHost_setReqsPerConnection (HTHost * host, int reqs)
                    561: {
                    562:     if (host) host->reqsPerConnection = reqs;
                    563: }
                    564: 
                    565: PUBLIC int HTHost_reqsPerConnection (HTHost * host)
                    566: {
                    567:     return host ? host->reqsPerConnection : -1;
                    568: }
                    569: 
                    570: PUBLIC void HTHost_setReqsMade (HTHost * host, int reqs)
                    571: {
                    572:     if (host) host->reqsMade = reqs;
                    573: }
                    574: 
                    575: PUBLIC int HTHost_reqsMade (HTHost * host)
                    576: {
                    577:     return host ? host->reqsMade : -1;
                    578: }
                    579: 
2.1       frystyk   580: /*
2.6       frystyk   581: **     Public methods for this host
                    582: */
                    583: PUBLIC HTMethod HTHost_publicMethods (HTHost * me)
                    584: {
                    585:     return me ? me->methods : METHOD_INVALID;
                    586: }
                    587: 
                    588: PUBLIC void HTHost_setPublicMethods (HTHost * me, HTMethod methodset)
                    589: {
                    590:     if (me) me->methods = methodset;
                    591: }
                    592: 
                    593: PUBLIC void HTHost_appendPublicMethods (HTHost * me, HTMethod methodset)
                    594: {
                    595:     if (me) me->methods |= methodset;
                    596: }
                    597: 
                    598: /*
                    599: **     Get and set the server name of the remote host
                    600: */
                    601: PUBLIC char * HTHost_server (HTHost * host)
                    602: {
                    603:      return host ? host->server : NULL;
                    604: }
                    605: 
                    606: PUBLIC BOOL HTHost_setServer (HTHost * host, const char * server)
                    607: {
                    608:     if (host && server) {
                    609:        StrAllocCopy(host->server, server);
                    610:        return YES;
                    611:     }
                    612:     return NO;
                    613: }
                    614: 
                    615: /*
                    616: **     Get and set the userAgent name of the remote host
                    617: */
                    618: PUBLIC char * HTHost_userAgent (HTHost * host)
                    619: {
                    620:      return host ? host->user_agent : NULL;
                    621: }
                    622: 
                    623: PUBLIC BOOL HTHost_setUserAgent (HTHost * host, const char * userAgent)
                    624: {
                    625:     if (host && userAgent) {
                    626:        StrAllocCopy(host->user_agent, userAgent);
                    627:        return YES;
2.12      frystyk   628:     }
                    629:     return NO;
                    630: }
                    631: 
                    632: /*
                    633: **     Get and set acceptable range units
                    634: */
                    635: PUBLIC char * HTHost_rangeUnits (HTHost * host)
                    636: {
                    637:      return host ? host->range_units : NULL;
                    638: }
                    639: 
                    640: PUBLIC BOOL HTHost_setRangeUnits (HTHost * host, const char * units)
                    641: {
                    642:     if (host && units) {
                    643:        StrAllocCopy(host->range_units, units);
                    644:        return YES;
                    645:     }
                    646:     return NO;
                    647: }
                    648: 
                    649: /*
                    650: **     Checks whether a specific range unit is OK. We always say
                    651: **     YES except if we have a specific statement from the server that
                    652: **     it doesn't understand byte ranges - that is - it has sent "none"
                    653: **     in a "Accept-Range" response header
                    654: */
                    655: PUBLIC BOOL HTHost_isRangeUnitAcceptable (HTHost * host, const char * unit)
                    656: {
                    657:     if (host && unit) {
                    658: #if 0
                    659:        if (host->range_units) {
2.63      frystyk   660:            char * start = HTStrCaseStr(host->range_units, "none");
2.12      frystyk   661: 
                    662:            /*
                    663:            **  Check that "none" is infact a token. It could be part of some
                    664:            **  other valid string, so we'd better check for it.
                    665:            */
                    666:            if (start) {
                    667:                
                    668:                
                    669:            }
                    670:            return NO;
                    671:        }
                    672: #endif
                    673:        return strcasecomp(unit, "bytes") ? NO : YES;
2.6       frystyk   674:     }
                    675:     return NO;
                    676: }
                    677: 
2.1       frystyk   678: /*
                    679: **     As soon as we know that this host accepts persistent connections,
                    680: **     we associated the channel with the host. 
                    681: **     We don't want more than MaxSockets-2 connections to be persistent in
                    682: **     order to avoid deadlock.
                    683: */
2.13      frystyk   684: PUBLIC BOOL HTHost_setPersistent (HTHost *             host,
                    685:                                  BOOL                  persistent,
                    686:                                  HTTransportMode       mode)
2.1       frystyk   687: {
2.13      frystyk   688:     if (!host) return NO;
                    689: 
                    690:     if (!persistent) {
                    691:        /*
                    692:        **  We use the HT_IGNORE status code as we don't want to free
                    693:        **  the stream at this point in time. The situation we want to
                    694:        **  avoid is that we free the channel from within the stream pipe.
                    695:        **  This will lead to an infinite look having the stream freing
                    696:        **  itself.
                    697:        */
2.30      frystyk   698:        host->persistent = NO;
2.13      frystyk   699:        return HTHost_clearChannel(host, HT_IGNORE);
                    700:     }
                    701: 
2.18      eric      702:     /*
                    703:     ** Set the host persistent if not already. Also update the mode to
                    704:     ** the new one - it may have changed
                    705:     */
                    706:     HTHost_setMode(host, mode);
                    707:     if (!host->persistent) {
2.13      frystyk   708:        SOCKET sockfd = HTChannel_socket(host->channel);
2.8       frystyk   709:        if (sockfd != INVSOC && HTNet_availablePersistentSockets() > 0) {
2.13      frystyk   710:            host->persistent = YES;
2.53      frystyk   711:            host->expires = time(NULL) + HTPassiveTimeout;     /* Default timeout */
2.13      frystyk   712:            HTChannel_setHost(host->channel, host);
2.8       frystyk   713:            HTNet_increasePersistentSocket();
2.59      frystyk   714:            HTTRACE(CORE_TRACE, "Host info... added host %p as persistent\n" _ host);
2.1       frystyk   715:            return YES;
                    716:        } else {
2.59      frystyk   717:            HTTRACE(CORE_TRACE, "Host info... no room for persistent socket %d\n" _ 
2.7       frystyk   718:                        sockfd);
2.18      eric      719:            return NO;
2.1       frystyk   720:        }
2.18      eric      721:     } else {
2.59      frystyk   722:        HTTRACE(CORE_TRACE, "Host info... %p already persistent\n" _ host);
2.18      eric      723:        return YES;
2.1       frystyk   724:     }
                    725:     return NO;
                    726: }
                    727: 
                    728: /*
2.13      frystyk   729: **     Check whether we have a persistent channel or not
                    730: */
                    731: PUBLIC BOOL HTHost_isPersistent (HTHost * host)
                    732: {
                    733:     return host && host->persistent;
                    734: }
                    735: 
                    736: /*
2.1       frystyk   737: **     Find persistent channel associated with this host.
                    738: */
                    739: PUBLIC HTChannel * HTHost_channel (HTHost * host)
                    740: {
                    741:     return host ? host->channel : NULL;
                    742: }
                    743: 
2.30      frystyk   744: 
2.1       frystyk   745: /*
2.30      frystyk   746: **  Check whether we have got a "close" notification, for example in the
                    747: **  connection header
                    748: */
                    749: PUBLIC BOOL HTHost_setCloseNotification (HTHost * host, BOOL mode)
                    750: {
                    751:     if (host) {
                    752:        host->close_notification = mode;
2.37      frystyk   753:        return YES;
2.30      frystyk   754:     }
                    755:     return NO;
                    756: }
                    757: 
                    758: PUBLIC BOOL HTHost_closeNotification (HTHost * host)
                    759: {
                    760:     return host && host->close_notification;
                    761: }
                    762: 
                    763: /*
2.1       frystyk   764: **     Clear the persistent entry by deleting the channel object. Note that
                    765: **     the channel object is only deleted if it's not used anymore.
                    766: */
2.8       frystyk   767: PUBLIC BOOL HTHost_clearChannel (HTHost * host, int status)
2.1       frystyk   768: {
                    769:     if (host && host->channel) {
2.8       frystyk   770:        HTChannel_setHost(host->channel, NULL);
2.10      frystyk   771:        
2.13      frystyk   772:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
                    773:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
2.18      eric      774:        host->registeredFor = 0;
2.13      frystyk   775: 
2.10      frystyk   776:        /*
                    777:        **  We don't want to recursively delete ourselves so if we are
                    778:        **  called from within the stream pipe then don't delete the channel
                    779:        **  at this point
                    780:        */
2.8       frystyk   781:        HTChannel_delete(host->channel, status);
2.18      eric      782:        host->expires = 0;      
2.1       frystyk   783:        host->channel = NULL;
2.22      eric      784:        host->tcpstate = TCP_BEGIN;
                    785:        host->reqsMade = 0;
2.32      frystyk   786:        if (HTHost_isPersistent(host)) {
                    787:            HTNet_decreasePersistentSocket();
                    788:            host->persistent = NO;
                    789:        }
                    790:        host->close_notification = NO;
2.45      frystyk   791:        host->broken_pipe = NO;
2.32      frystyk   792:                host->mode = HT_TP_SINGLE;
2.67    ! raff      793: 
        !           794:        host->recovered = 0;
2.32      frystyk   795: 
2.59      frystyk   796:        HTTRACE(CORE_TRACE, "Host info... removed host %p as persistent\n" _ host);
2.32      frystyk   797: 
                    798:        if (!HTList_isEmpty(host->pending)) {
2.59      frystyk   799:            HTTRACE(CORE_TRACE, "Host has %d object(s) pending - attempting launch\n" _ HTList_count(host->pending));
2.32      frystyk   800:            HTHost_launchPending(host);
                    801:        }
2.1       frystyk   802:        return YES;
                    803:     }
                    804:     return NO;
                    805: }
                    806: 
2.37      frystyk   807: PUBLIC BOOL HTHost_doRecover (HTHost * host)
                    808: {
                    809:     return host ? host->do_recover : NO;
                    810: }
                    811: 
2.1       frystyk   812: /*
2.18      eric      813: **     Move all entries in the pipeline and move the rest to the pending
                    814: **     queue. They will get launched at a later point in time.
                    815: */
                    816: PUBLIC BOOL HTHost_recoverPipe (HTHost * host)
                    817: {
                    818:     if (host) {
                    819:        int piped = HTList_count(host->pipeline);
2.64      frystyk   820: 
                    821:        /*
                    822:        **  First check that we haven't already recovered more than we want
                    823:        */
                    824:        if (host->recovered > MAX_HOST_RECOVER) {
                    825:            HTTRACE(CORE_TRACE, "Host recover %p already %d times - not doing it anymore\n" _ host _ host->recovered);
                    826:            return NO;
                    827:        }
                    828: 
                    829:        /*
                    830:        **  If we decided to recover and actually have something in the pipe
                    831:        **  then go ahead.
                    832:        */
2.18      eric      833:        if (piped > 0) {
                    834:            int cnt;
2.24      frystyk   835:            host->recovered++;
2.64      frystyk   836:            HTTRACE(CORE_TRACE, "Host recover %p recovered %d times. Moving %d Net objects from pipe line to pending queue\n" _ host _ host->recovered _ piped);
2.18      eric      837:            
                    838:            /*
                    839:            **  Unregister this host for all events
                    840:            */
                    841:            HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
                    842:            HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
                    843:            host->registeredFor = 0;
                    844: 
                    845:            /*
                    846:            **  Set new mode to single until we know what is going on
                    847:            */
                    848:            host->mode = HT_TP_SINGLE;
                    849: 
                    850:            /*
                    851:            **  Move all net objects from the net object to the pending queue.
                    852:            */
                    853:            if (!host->pending) host->pending = HTList_new();
                    854:            for (cnt=0; cnt<piped; cnt++) {
                    855:                HTNet * net = HTList_removeLastObject(host->pipeline);
2.59      frystyk   856:                HTTRACE(CORE_TRACE, "Host recover Resetting net object %p\n" _ net);
2.18      eric      857:                net->registeredFor = 0;
                    858:                (*net->event.cbf)(HTChannel_socket(host->channel), net->event.param, HTEvent_RESET);
                    859:                HTList_appendObject(host->pending, net);
2.57      frystyk   860:                host->lock = net;
2.18      eric      861:            }
2.37      frystyk   862: 
2.18      eric      863:            HTChannel_setSemaphore(host->channel, 0);
                    864:            HTHost_clearChannel(host, HT_INTERRUPTED);
2.37      frystyk   865:            host->do_recover = NO;
2.18      eric      866:        }
2.24      frystyk   867:        return YES;
2.18      eric      868:     }
                    869:     return NO;
                    870: }
                    871: 
                    872: /*
2.51      frystyk   873: **     Terminate a pipeline prematurely, for example because of timeout,
                    874: **     interruption, etc.
                    875: */
                    876: PUBLIC BOOL HTHost_killPipe (HTHost * host)
                    877: {
                    878:     return killPipeline(host, HTEvent_CLOSE);
                    879: }
                    880: 
                    881: /*
2.8       frystyk   882: **     Handle the connection mode. The mode may change mode in the 
                    883: **     middle of a connection.
                    884: */
                    885: PUBLIC HTTransportMode HTHost_mode (HTHost * host, BOOL * active)
                    886: {
                    887:     return host ? host->mode : HT_TP_SINGLE;
                    888: }
                    889: 
                    890: /*
                    891: **     If the new mode is lower than the old mode then adjust the pipeline
                    892: **     accordingly. That is, if we are going into single mode then move
                    893: **     all entries in the pipeline and move the rest to the pending
                    894: **     queue. They will get launched at a later point in time.
                    895: */
                    896: PUBLIC BOOL HTHost_setMode (HTHost * host, HTTransportMode mode)
                    897: {
                    898:     if (host) {
                    899:        /*
                    900:        **  Check the new mode and see if we must adjust the queues.
                    901:        */
                    902:        if (mode == HT_TP_SINGLE && host->mode > mode) {
                    903:            int piped = HTList_count(host->pipeline);
                    904:            if (piped > 0) {
                    905:                int cnt;
2.59      frystyk   906:                HTTRACE(CORE_TRACE, "Host info... Moving %d Net objects from pipe line to pending queue\n" _ piped);
2.8       frystyk   907:                if (!host->pending) host->pending = HTList_new();
                    908:                for (cnt=0; cnt<piped; cnt++) {
2.18      eric      909:                    HTNet * net = HTList_removeLastObject(host->pipeline);
2.59      frystyk   910:                    HTTRACE(CORE_TRACE, "Host info... Resetting net object %p\n" _ net);
2.13      frystyk   911:                    (*net->event.cbf)(HTChannel_socket(host->channel), net->event.param, HTEvent_RESET);
2.8       frystyk   912:                    HTList_appendObject(host->pending, net);
                    913:                }
2.18      eric      914:                HTChannel_setSemaphore(host->channel, 0);
                    915:                HTHost_clearChannel(host, HT_INTERRUPTED);
2.8       frystyk   916:            }
2.24      frystyk   917:        }
                    918: 
                    919:        /*
                    920:        **  If we know that this host is bad then we don't allow anything than
                    921:        **  single mode. We can't recover connections for the rest of our life
                    922:        */
                    923:        if (mode == HT_TP_PIPELINE && host->recovered > MAX_HOST_RECOVER) {
2.59      frystyk   924:            HTTRACE(PROT_TRACE, "Host info... %p is bad for pipelining so we won't do it!!!\n" _ 
2.24      frystyk   925:                        host);
                    926:        } else {
                    927:            host->mode = mode;
2.59      frystyk   928:            HTTRACE(PROT_TRACE, "Host info... New mode is %d for host %p\n" _ host->mode _ host);
2.24      frystyk   929:        }
2.8       frystyk   930:     }
                    931:     return NO;
                    932: }
                    933: 
                    934: /*
                    935: **     Check whether a host is idle meaning if it is ready for a new
                    936: **     request which depends on the mode of the host. If the host is 
                    937: **     idle, i.e. ready for use then return YES else NO. If the host supports
                    938: **     persistent connections then still only return idle if no requests are
                    939: **     ongoing. 
                    940: */
                    941: PUBLIC BOOL HTHost_isIdle (HTHost * host)
                    942: {
2.32      frystyk   943:     return (host && HTList_isEmpty(host->pipeline));
2.8       frystyk   944: }
                    945: 
2.13      frystyk   946: PRIVATE BOOL _roomInPipe (HTHost * host)
                    947: {
                    948:     int count;
2.34      frystyk   949:     if (!host ||
                    950:        (host->reqsPerConnection && host->reqsMade >= host->reqsPerConnection) ||
2.45      frystyk   951:        HTHost_closeNotification(host) || host->broken_pipe)
2.32      frystyk   952:        return NO;
2.13      frystyk   953:     count = HTList_count(host->pipeline);
                    954:     switch (host->mode) {
                    955:     case HT_TP_SINGLE:
                    956:        return count <= 0;
                    957:     case HT_TP_PIPELINE:
2.51      frystyk   958:        return (host->recovered < MAX_HOST_RECOVER) ?
                    959:            (count < MaxPipelinedRequests) : (count <= 0);
2.13      frystyk   960:     case HT_TP_INTERLEAVE:
                    961:        return YES;
                    962:     }
                    963:     return NO;
                    964: }
                    965: 
2.8       frystyk   966: /*
                    967: **     Add a net object to the host object. If the host
                    968: **     is idle then add to active list (pipeline) else add
                    969: **     it to the pending list
                    970: **     Return HT_PENDING if we must pend, HT_OK, or HT_ERROR
                    971: */
                    972: PUBLIC int HTHost_addNet (HTHost * host, HTNet * net)
                    973: {
                    974:     if (host && net) {
                    975:        int status = HT_OK;
2.32      frystyk   976:        BOOL doit = (host->doit==net);
2.8       frystyk   977: 
2.18      eric      978:        /*
                    979:        **  If we don't have a socket already then check to see if we can get
                    980:        **  one. Otherwise we put the host object into our pending queue.
2.52      frystyk   981:        */
2.18      eric      982:        if (!host->channel && HTNet_availableSockets() <= 0) {
2.56      frystyk   983: 
                    984:            /* Create list for pending Host objects */
2.8       frystyk   985:            if (!PendHost) PendHost = HTList_new();
2.56      frystyk   986: 
                    987:            /* Add the host object ad pending if not already */
                    988:            if (HTList_indexOf(PendHost, host) < 0) HTList_addObject(PendHost, host);
                    989: 
                    990:            /* 
                    991:            ** Add the Net object to the Host object. If it is the current Net
                    992:            ** obejct holding the lock then add it to the beginning of the list.
                    993:            ** Otherwise add it to the end
                    994:            */
2.52      frystyk   995:            if (!host->pending) host->pending = HTList_new();
2.56      frystyk   996:            if (host->lock == net)
                    997:                HTList_appendObject(host->pending, net);
                    998:            else
                    999:                HTList_addObject(host->pending, net);
                   1000: 
2.59      frystyk  1001:            HTTRACE(CORE_TRACE, "Host info... Added Net %p (request %p) as pending on pending Host %p, %d requests made, %d requests in pipe, %d pending\n" _ 
                   1002:                        net _ net->request _ host _ host->reqsMade _ 
                   1003:                        HTList_count(host->pipeline) _ HTList_count(host->pending));
2.52      frystyk  1004:            return HT_PENDING;
2.8       frystyk  1005:        }
                   1006: 
2.45      frystyk  1007: #if 0
2.18      eric     1008:        /*
2.45      frystyk  1009:        ** First check whether the net object is already on either queue.
                   1010:        ** Do NOT add extra copies of the HTNet object to
                   1011:        ** the pipeline or pending list (if it's already on the list).
                   1012:        */
                   1013:        if (HTList_indexOf(host->pipeline, net) >= 0) {
2.59      frystyk  1014:            HTTRACE(CORE_TRACE, "Host info... The Net %p (request %p) is already in pipe,"
                   1015:                        " %d requests made, %d requests in pipe, %d pending\n" _ 
                   1016:                        net _ net->request _ host->reqsMade _ 
                   1017:                        HTList_count(host->pipeline) _ 
2.45      frystyk  1018:                        HTList_count(host->pending));
2.59      frystyk  1019:            HTDEBUGBREAK("Net object %p registered multiple times in pipeline\n" _ 
2.45      frystyk  1020:                         net);
                   1021:            return HT_OK;
                   1022:        }
2.44      frystyk  1023: 
2.45      frystyk  1024:        if (HTList_indexOf(host->pending,  net) >= 0) {
2.59      frystyk  1025:            HTTRACE(CORE_TRACE, "Host info... The Net %p (request %p) already pending,"
                   1026:                        " %d requests made, %d requests in pipe, %d pending\n" _ 
                   1027:                        net _ net->request _ host->reqsMade _ 
                   1028:                        HTList_count(host->pipeline) _ 
2.45      frystyk  1029:                        HTList_count(host->pending));
2.59      frystyk  1030:            HTDEBUGBREAK("Net object %p registered multiple times in pending queue\n" _ 
2.45      frystyk  1031:                         net);
                   1032: 
                   1033:            return HT_PENDING;
                   1034:        }
                   1035: #endif
2.44      frystyk  1036: 
                   1037:        /*
2.18      eric     1038:        **  Add net object to either active or pending queue.
                   1039:        */
2.44      frystyk  1040:        if (_roomInPipe(host) && (HTList_isEmpty(host->pending) || doit)) {
2.32      frystyk  1041:            if (doit) host->doit = NULL;
2.8       frystyk  1042:            if (!host->pipeline) host->pipeline = HTList_new();
                   1043:            HTList_addObject(host->pipeline, net);
2.32      frystyk  1044:            host->reqsMade++;
2.59      frystyk  1045:             HTTRACE(CORE_TRACE, "Host info... Added Net %p (request %p) to pipe on Host %p, %d requests made, %d requests in pipe, %d pending\n" _ 
                   1046:                        net _ net->request _ host _ host->reqsMade _ 
                   1047:                        HTList_count(host->pipeline) _ HTList_count(host->pending));
2.18      eric     1048: 
2.13      frystyk  1049:            /*
2.30      frystyk  1050:            **  If we have been idle then make sure we delete the timer
2.13      frystyk  1051:            */
2.30      frystyk  1052:            if (host->timer) {
                   1053:                HTTimer_delete(host->timer);
                   1054:                host->timer = NULL;
                   1055:            }
2.40      kahan    1056:            
                   1057:             /*JK: New CBF function
                   1058:            ** Call any user-defined callback to say the request will
                   1059:             ** be processed.
                   1060:             */
                   1061:             HTHost_ActivateRequest (net);
2.30      frystyk  1062: 
2.8       frystyk  1063:        } else {
                   1064:            if (!host->pending) host->pending = HTList_new();
2.44      frystyk  1065:            HTList_addObject(host->pending, net);           
2.59      frystyk  1066:            HTTRACE(CORE_TRACE, "Host info... Added Net %p (request %p) as pending on Host %p, %d requests made, %d requests in pipe, %d pending\n" _ 
                   1067:                        net _ net->request _ 
                   1068:                        host _ host->reqsMade _ 
                   1069:                        HTList_count(host->pipeline) _ HTList_count(host->pending));
2.8       frystyk  1070:            status = HT_PENDING;
                   1071:        }
                   1072:        return status;
                   1073:     }
                   1074:     return HT_ERROR;
                   1075: }
                   1076: 
2.51      frystyk  1077: PRIVATE BOOL HTHost_free (HTHost * host, int status)
2.13      frystyk  1078: {
2.32      frystyk  1079:     if (host->channel) {
2.13      frystyk  1080: 
2.32      frystyk  1081:        /* Check if we should keep the socket open */
                   1082:         if (HTHost_isPersistent(host)) {
2.48      frystyk  1083:            int piped = HTList_count(host->pipeline);
2.37      frystyk  1084:             if (HTHost_closeNotification(host)) {
2.59      frystyk  1085:                HTTRACE(CORE_TRACE, "Host Object. got close notifiation on socket %d\n" _ 
2.37      frystyk  1086:                            HTChannel_socket(host->channel));
                   1087:                 
                   1088:                /*
                   1089:                **  If more than a single element (this one) in the pipe
                   1090:                **  then we have to recover gracefully
                   1091:                */
                   1092:                if (piped > 1) {
                   1093:                    host->reqsPerConnection = host->reqsMade - piped;
2.59      frystyk  1094:                    HTTRACE(CORE_TRACE, "%d requests made, %d in pipe, max %d requests pr connection\n" _ 
                   1095:                                host->reqsMade _ piped _ host->reqsPerConnection);
2.37      frystyk  1096:                    host->do_recover = YES;
2.38      frystyk  1097:                    HTChannel_delete(host->channel, status);
2.62      frystyk  1098:                    host->channel = NULL;
2.38      frystyk  1099:                } else {
                   1100:                    HTChannel_setSemaphore(host->channel, 0);
                   1101:                    HTHost_clearChannel(host, status);
2.37      frystyk  1102:                }
2.48      frystyk  1103:            } else if (piped<=1 && host->reqsMade==host->reqsPerConnection) {
2.59      frystyk  1104:                 HTTRACE(CORE_TRACE, "Host Object. closing persistent socket %d\n" _ 
2.37      frystyk  1105:                                        HTChannel_socket(host->channel));
2.32      frystyk  1106:                 
                   1107:                 /* 
                   1108:                 **  By lowering the semaphore we make sure that the channel
                   1109:                 **  is gonna be deleted
                   1110:                 */
                   1111:                 HTChannel_setSemaphore(host->channel, 0);
                   1112:                 HTHost_clearChannel(host, status);
                   1113: 
                   1114:             } else {
2.59      frystyk  1115:                 HTTRACE(CORE_TRACE, "Host Object. keeping persistent socket %d\n" _ 
2.56      frystyk  1116:                                        HTChannel_socket(host->channel));
2.62      frystyk  1117:                 if (HTChannel_delete(host->channel, status)) {
2.59      frystyk  1118:                    HTDEBUGBREAK("Host Event.. Channel unexpected deleted from host %p (%s)\n" _ host _ host->hostname);
2.62      frystyk  1119:                    host->channel = NULL;
                   1120:                 }
2.32      frystyk  1121:                 /*
                   1122:                 **  If connection is idle then set a timer so that we close the 
                   1123:                 **  connection if idle too long
                   1124:                 */
2.48      frystyk  1125:                 if (piped<=1 && HTList_isEmpty(host->pending) && !host->timer) {
                   1126:                     host->timer = HTTimer_new(NULL, IdleTimeoutEvent,
2.53      frystyk  1127:                                              host, HTActiveTimeout, YES, NO);
2.59      frystyk  1128:                     HTTRACE(PROT_TRACE, "Host........ Object %p going idle...\n" _ host);
2.32      frystyk  1129:                 }
                   1130:             }
                   1131:             return YES;
                   1132:         } else {
2.59      frystyk  1133:             HTTRACE(CORE_TRACE, "Host Object. closing socket %d\n" _ HTChannel_socket(host->channel));
2.47      frystyk  1134:            HTChannel_setSemaphore(host->channel, 0);
2.41      frystyk  1135:            HTHost_clearChannel(host, status);
2.32      frystyk  1136:         }
2.13      frystyk  1137:     }
2.32      frystyk  1138:     return NO;
2.13      frystyk  1139: }
                   1140: 
2.51      frystyk  1141: PUBLIC BOOL HTHost_deleteNet (HTHost * host, HTNet * net, int status)
2.8       frystyk  1142: {
                   1143:     if (host && net) {
2.59      frystyk  1144:         HTTRACE(CORE_TRACE, "Host info... Remove %p from pipe\n" _ net);
2.51      frystyk  1145: 
                   1146:        /* If the Net object is in the pipeline then also update the channel */
                   1147:        if (host->pipeline && HTList_indexOf(host->pipeline, net) >= 0) {
                   1148:            HTHost_free(host, status);
                   1149:            HTList_removeObjectAll(host->pipeline, net);
                   1150:        }
                   1151: 
2.44      frystyk  1152:        HTList_removeObjectAll(host->pending, net); /* just to make sure */
2.62      frystyk  1153:        host->lock = HTList_firstObject(host->pending);
2.8       frystyk  1154:        return YES;
                   1155:     }
                   1156:     return NO;
                   1157: }
                   1158: 
                   1159: /*
                   1160: **     Handle pending host objects.
                   1161: **     There are two ways we can end up with pending reqyests:
                   1162: **      1) If we are out of sockets then register new host objects as pending.
                   1163: **      2) If we are pending on a connection then register new net objects as
                   1164: **         pending
                   1165: **     This set of functions handles pending host objects and can start new
                   1166: **     requests as resources get available
                   1167: */
                   1168: 
                   1169: /*
                   1170: **     Check this host object for any pending requests and return the next
                   1171: **     registered Net object.
                   1172: */
                   1173: PUBLIC HTNet * HTHost_nextPendingNet (HTHost * host)
                   1174: {
                   1175:     HTNet * net = NULL;
2.32      frystyk  1176:     if (host && host->pending) {
2.18      eric     1177:        /*JK 23/Sep/96 Bug correction. Associated the following lines to the
                   1178:        **above if. There was a missing pair of brackets. 
                   1179:        */
                   1180:        if ((net = (HTNet *) HTList_removeFirstObject(host->pending)) != NULL) {
2.59      frystyk  1181:            HTTRACE(CORE_TRACE, "Host info... Popping %p from pending net queue on host %p\n" _ 
                   1182:                        net _ host);
2.33      frystyk  1183: #if 0
                   1184:            {
                   1185:                HTRequest * request = HTNet_request(net);
                   1186:                char * uri = HTAnchor_address((HTAnchor *) HTRequest_anchor(request));
                   1187:                fprintf(stderr, "Popping '%s'\n", uri);
                   1188:            }
                   1189: #endif
2.32      frystyk  1190:            host->doit = net;
2.18      eric     1191:        }
2.8       frystyk  1192:     }
                   1193:     return net;
                   1194: }
                   1195: 
                   1196: /*
2.13      frystyk  1197: **     Return the current list of pending host objects waiting for a socket
2.8       frystyk  1198: */
                   1199: PUBLIC HTHost * HTHost_nextPendingHost (void)
                   1200: {
                   1201:     HTHost * host = NULL;
                   1202:     if (PendHost) {
                   1203:        if ((host = (HTHost *) HTList_removeFirstObject(PendHost)) != NULL)
2.59      frystyk  1204:            HTTRACE(PROT_TRACE, "Host info... Popping %p from pending host queue\n" _ 
2.8       frystyk  1205:                        host);
                   1206:     }
                   1207:     return host;
                   1208: }
                   1209: 
                   1210: /*
                   1211: **     Start the next pending request if any. First we look for pending
                   1212: **     requests for the same host and then we check for any other pending
                   1213: **     hosts
                   1214: */
                   1215: PUBLIC BOOL HTHost_launchPending (HTHost * host)
                   1216: {
2.52      frystyk  1217:     HTNet * net = NULL;
2.8       frystyk  1218:     if (!host) {
2.59      frystyk  1219:        HTTRACE(PROT_TRACE, "Host info... Bad arguments\n");
2.8       frystyk  1220:        return NO;
                   1221:     }
                   1222: 
                   1223:     /*
2.52      frystyk  1224:     **  In pipeline we can only have one doing writing at a time.
                   1225:     **  We therefore check that there are no other Net object
                   1226:     **  registered for write
2.8       frystyk  1227:     */
2.52      frystyk  1228:     if (host->mode == HT_TP_PIPELINE) {
                   1229:        net = (HTNet *) HTList_lastObject(host->pipeline);
                   1230:        if (net && net->registeredFor == HTEvent_WRITE)
                   1231:            return NO;
                   1232:     }
2.8       frystyk  1233: 
2.52      frystyk  1234:     /*
                   1235:     **  Check the current Host object for pending Net objects
                   1236:     */
                   1237:     if (_roomInPipe(host) && DoPendingReqLaunch &&
                   1238:           (net = HTHost_nextPendingNet(host))) {
                   1239:        HTHost_ActivateRequest(net);
2.59      frystyk  1240:        HTTRACE(CORE_TRACE, "Launch pending net object %p with %d reqs in pipe (%d reqs made)\n" _ 
                   1241:                    net _ HTList_count(host->pipeline) _ host->reqsMade);
2.52      frystyk  1242:        return HTNet_execute(net, HTEvent_WRITE);
                   1243:     }
2.13      frystyk  1244: 
2.52      frystyk  1245:     /*
                   1246:     **  Check for other pending Host objects
                   1247:     */
                   1248:     if (DoPendingReqLaunch && HTNet_availableSockets() > 0) {
                   1249:        HTHost * pending = HTHost_nextPendingHost();
                   1250:        if (pending && (net = HTHost_nextPendingNet(pending))) {
                   1251:            if (!pending->pipeline) pending->pipeline = HTList_new();
                   1252:            HTList_addObject(pending->pipeline, net);
                   1253:            host->reqsMade++;
2.59      frystyk  1254:            HTTRACE(CORE_TRACE, "Launch pending host object %p, net %p with %d reqs in pipe (%d reqs made)\n" _ 
                   1255:                        pending _ net _ HTList_count(pending->pipeline) _ pending->reqsMade);
2.52      frystyk  1256:            HTHost_ActivateRequest(net);
                   1257:            return HTNet_execute(net, HTEvent_WRITE);
2.8       frystyk  1258:        }
2.52      frystyk  1259:     }
                   1260:     return YES;
2.13      frystyk  1261: }
                   1262: 
                   1263: PUBLIC HTNet * HTHost_firstNet (HTHost * host)
                   1264: {
                   1265:     return (HTNet *) HTList_firstObject(host->pipeline);
2.60      frystyk  1266: }
                   1267: 
                   1268: PUBLIC int HTHost_numberOfOutstandingNetObjects (HTHost * host)
                   1269: {
                   1270:     return host ? HTList_count(host->pipeline) : -1;
                   1271: }
                   1272: 
                   1273: PUBLIC int HTHost_numberOfPendingNetObjects (HTHost * host)
                   1274: {
                   1275:     return host ? HTList_count(host->pending) : -1;
2.13      frystyk  1276: }
                   1277: 
                   1278: /*
                   1279: **     The host event manager keeps track of the state of it's client engines
                   1280: **     (typically HTTPEvent), accepting multiple blocks on read or write from
                   1281: **     multiple pipelined engines. It then registers its own engine 
                   1282: **     (HostEvent) with the event manager.
                   1283: */
2.66      frystyk  1284: PUBLIC int HTHost_connect (HTHost * host, HTNet * net, char * url)
2.13      frystyk  1285: {
2.42      frystyk  1286:     HTRequest * request = HTNet_request(net);
2.52      frystyk  1287:     int status = HT_OK;
2.42      frystyk  1288:     if (!host) {
                   1289:        HTProtocol * protocol = HTNet_protocol(net);
                   1290:        if ((host = HTHost_newWParse(request, url, HTProtocol_id(protocol))) == NULL)
2.45      frystyk  1291:            return HT_ERROR;
2.52      frystyk  1292: 
                   1293:        /*
                   1294:        ** If not already locked and without a channel
2.55      frystyk  1295:        ** then lock the darn thing with the first Net object
                   1296:        ** pending.
2.52      frystyk  1297:        */
                   1298:        if (!host->lock && !host->channel) {
2.55      frystyk  1299:            HTNet * next_pending = NULL;
2.42      frystyk  1300:            host->forceWriteFlush = YES;
2.55      frystyk  1301:            host->lock = (next_pending = HTList_firstObject(host->pending)) ?
                   1302:                next_pending : net;
2.59      frystyk  1303:            HTTRACE(CORE_TRACE, "Host connect Grabbing lock on Host %p with %p\n" _ host _ host->lock);
2.42      frystyk  1304:        }
                   1305:        HTNet_setHost(net, host);
                   1306:     }
                   1307: 
                   1308:     if (!host->lock || (host->lock && host->lock == net)) {
2.66      frystyk  1309:        status = HTDoConnect(net);
2.55      frystyk  1310:        if (status == HT_PENDING)
                   1311:            return HT_WOULD_BLOCK;
                   1312:        else if (status == HT_WOULD_BLOCK) {
2.42      frystyk  1313:            host->lock = net;
2.52      frystyk  1314:            return status;
2.55      frystyk  1315:        } else {
                   1316: 
                   1317:            /*
                   1318:            **  See if there is already a new pending request that should
                   1319:            **  take over the current lock
                   1320:            */
                   1321:            HTNet * next_pending = NULL;
                   1322:            if ((next_pending = HTList_firstObject(host->pending))) {
2.59      frystyk  1323:                HTTRACE(CORE_TRACE, "Host connect Changing lock on Host %p to %p\n" _ 
2.66      frystyk  1324:                        host _ next_pending);
2.55      frystyk  1325:                host->lock = next_pending;          
                   1326:            } else {
2.59      frystyk  1327:                HTTRACE(CORE_TRACE, "Host connect Unlocking Host %p\n" _ host);
2.55      frystyk  1328:                host->lock = NULL;          
                   1329:            }
                   1330:            return status;
2.42      frystyk  1331:        }
                   1332:     } else {
2.59      frystyk  1333:        HTTRACE(CORE_TRACE, "Host connect Host %p already locked with %p\n" _ host _ host->lock);
2.42      frystyk  1334:        if ((status = HTHost_addNet(host, net)) == HT_PENDING) {
                   1335:            return HT_PENDING;
                   1336:        }
2.54      frystyk  1337:     }
                   1338:     return HT_ERROR; /* @@@ - some more deletion and stuff here? */
                   1339: }
                   1340: 
2.66      frystyk  1341: PUBLIC int HTHost_listen (HTHost * host, HTNet * net, char * url)
2.54      frystyk  1342: {
                   1343:     HTRequest * request = HTNet_request(net);
                   1344:     int status = HT_OK;
                   1345:     if (!host) {
                   1346:        HTProtocol * protocol = HTNet_protocol(net);
                   1347:        if ((host = HTHost_newWParse(request, url, HTProtocol_id(protocol))) == NULL)
                   1348:            return HT_ERROR;
                   1349: 
                   1350:        /*
                   1351:        ** If not already locked and without a channel
2.66      frystyk  1352:        ** then lock the darn thing with the first Net object
                   1353:        ** pending.
2.54      frystyk  1354:        */
                   1355:        if (!host->lock && !host->channel) {
                   1356:            host->forceWriteFlush = YES;
                   1357:            host->lock = net;
                   1358:        }
                   1359:        HTNet_setHost(net, host);
                   1360:     }
                   1361: 
2.66      frystyk  1362:     /*
                   1363:     ** See if we already have a dedicated listen Net object. If not 
                   1364:     ** then create one.
                   1365:     */
                   1366:     if (!host->listening) host->listening = HTNet_new(host);
2.54      frystyk  1367: 
2.66      frystyk  1368:     /*
                   1369:     ** Start listening on the Net object
                   1370:     */
                   1371:     status = HTDoListen(host->listening, net, HT_BACKLOG);
                   1372:     if (status != HT_OK) {
                   1373:        HTTRACE(CORE_TRACE, "Host listen. On Host %p resulted in %d\n" _ host _ status);
                   1374:        return status;
                   1375:     }
                   1376:     return HT_OK;
                   1377: }
2.54      frystyk  1378: 
2.66      frystyk  1379: PUBLIC int HTHost_accept (HTHost * host, HTNet * net, char * url)
                   1380: {
                   1381:     int status = HT_OK;
                   1382:     if (!host || !host->listening) {
                   1383:        HTTRACE(CORE_TRACE, "Host accept. No host object or not listening on anything\n");
                   1384:        return HT_ERROR;
                   1385:     }
2.54      frystyk  1386: 
2.66      frystyk  1387:     if (!host->lock || (host->lock && host->lock == net)) {
                   1388:        status = HTDoAccept(host->listening, net);
                   1389:        if (status == HT_PENDING)
                   1390:            return HT_WOULD_BLOCK;
                   1391:        else if (status == HT_WOULD_BLOCK) {
2.54      frystyk  1392:            host->lock = net;
                   1393:            return status;
2.66      frystyk  1394:        } else {
                   1395: 
                   1396:            /*
                   1397:            **  See if there is already a new pending request that should
                   1398:            **  take over the current lock
                   1399:            */
                   1400:            HTNet * next_pending = NULL;
                   1401:            if ((next_pending = HTList_firstObject(host->pending))) {
                   1402:                HTTRACE(CORE_TRACE, "Host connect Changing lock on Host %p to %p\n" _ 
                   1403:                        host _ next_pending);
                   1404:                host->lock = next_pending;          
                   1405:            } else {
                   1406:                HTTRACE(CORE_TRACE, "Host connect Unlocking Host %p\n" _ host);
                   1407:                host->lock = NULL;          
                   1408:            }
                   1409:            return status;
                   1410:        }
                   1411:     } else {
                   1412:        HTTRACE(CORE_TRACE, "Host connect Host %p already locked with %p\n" _ host _ host->lock);
                   1413:        if ((status = HTHost_addNet(host, net)) == HT_PENDING) {
                   1414:            return HT_PENDING;
2.54      frystyk  1415:        }
2.42      frystyk  1416:     }
2.66      frystyk  1417:     return HT_ERROR;
2.13      frystyk  1418: }
                   1419: 
                   1420: /*
                   1421: **     Rules: SINGLE: one element in pipe, either reading or writing
                   1422: **              PIPE: n element in pipe, n-1 reading, 1 writing
                   1423: */
                   1424: PUBLIC int HTHost_register (HTHost * host, HTNet * net, HTEventType type)
                   1425: {
2.40      kahan    1426:   HTEvent *event;
                   1427: 
2.13      frystyk  1428:     if (host && net) {
                   1429: 
2.28      frystyk  1430:        if (type == HTEvent_CLOSE) {
2.13      frystyk  1431: 
2.28      frystyk  1432:            /*
                   1433:            **  Unregister this host for all events
                   1434:            */
                   1435:            HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
                   1436:            HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
                   1437:            host->registeredFor = 0;
2.13      frystyk  1438:            return YES;
2.28      frystyk  1439: 
                   1440:        } else {
                   1441: 
                   1442:            /* net object may already be registered */
                   1443:            if (HTEvent_BITS(type) & net->registeredFor)
                   1444:                return NO;
                   1445:            net->registeredFor ^= HTEvent_BITS(type);
                   1446: 
                   1447:            /* host object may already be registered */
                   1448:            if (host->registeredFor & HTEvent_BITS(type))
                   1449:                return YES;
                   1450:            host->registeredFor ^= HTEvent_BITS(type);
2.46      frystyk  1451: 
                   1452: #ifdef WWW_WIN_ASYNC
                   1453:             /* Make sure we are registered for CLOSE on windows */
                   1454:            event =  *(host->events+HTEvent_INDEX(HTEvent_CLOSE));
                   1455:            HTEvent_register(HTChannel_socket(host->channel), HTEvent_CLOSE, event);
                   1456: #endif /* WWW_WIN_ASYNC */
                   1457:             
                   1458:             /* JK:  register a request in the event structure */
2.40      kahan    1459:            event =  *(host->events+HTEvent_INDEX(type));
                   1460:            event->request = HTNet_request (net);
2.28      frystyk  1461:            return HTEvent_register(HTChannel_socket(host->channel),
2.40      kahan    1462:                                    type, event);
2.28      frystyk  1463:        }
2.52      frystyk  1464: 
                   1465:        return YES;
2.13      frystyk  1466:     }
2.65      frystyk  1467:     HTTRACE(CORE_TRACE, "HTHost...... Don't register event with bad arguments\n");
2.13      frystyk  1468:     return NO;
                   1469: }
                   1470: 
                   1471: PUBLIC int HTHost_unregister (HTHost * host, HTNet * net, HTEventType type)
                   1472: {
                   1473:     if (host && net) {
                   1474: 
2.28      frystyk  1475:        /* net object may not be registered */
2.13      frystyk  1476:        if (!(HTEvent_BITS(type) & net->registeredFor))
                   1477:            return NO;
                   1478:        net->registeredFor ^= HTEvent_BITS(type);
                   1479: 
2.28      frystyk  1480:        /* host object may not be registered */
2.13      frystyk  1481:        if (!(host->registeredFor & HTEvent_BITS(type)))
                   1482:            return YES;
                   1483:        host->registeredFor ^= HTEvent_BITS(type);
                   1484: 
                   1485:        /* stay registered for READ to catch a socket close */
                   1486:        /* WRITE and CONNECT can be unregistered, though */
                   1487:        if ((type == HTEvent_WRITE && isLastInPipe(host, net)) || 
                   1488:            type == HTEvent_CONNECT)
                   1489:            /* if we are blocked downstream, shut down the whole pipe */
                   1490:            HTEvent_unregister(HTChannel_socket(host->channel), type);
                   1491:        return YES;
                   1492:     }
                   1493:     return NO;
                   1494: }
                   1495: 
                   1496: /*
                   1497: **     The reader tells HostEvent that it's stream did not finish the data
                   1498: */
                   1499: PUBLIC BOOL HTHost_setRemainingRead (HTHost * host, size_t remaining)
                   1500: {
                   1501:     if (host == NULL) return NO;
                   1502:     host->remainingRead = remaining;
2.59      frystyk  1503:     HTTRACE(PROT_TRACE, "Host........ %d bytes remaining \n" _ remaining);
2.45      frystyk  1504:     if (host->broken_pipe && remaining == 0) {
2.59      frystyk  1505:        HTTRACE(PROT_TRACE, "Host........ Emtied out connection\n");
2.45      frystyk  1506:     }
2.13      frystyk  1507:     return YES;
                   1508: }
                   1509: 
2.32      frystyk  1510: PUBLIC size_t HTHost_remainingRead (HTHost * host)
                   1511: {
                   1512:     return host ? host->remainingRead : -1;
                   1513: }
                   1514: 
2.13      frystyk  1515: PUBLIC SockA * HTHost_getSockAddr (HTHost * host)
                   1516: {
                   1517:     if (!host) return NULL;
                   1518:     return &host->sock_addr;
                   1519: }
                   1520: 
                   1521: PUBLIC BOOL HTHost_setHome (HTHost * host, int home)
                   1522: {
                   1523:     if (!host) return NO;
                   1524:     host->home = home;
                   1525:     return YES;
                   1526: }
                   1527: 
                   1528: PUBLIC int HTHost_home (HTHost * host)
                   1529: {
                   1530:     if (!host) return 0;
                   1531:     return host->home;
                   1532: }
                   1533: 
2.27      frystyk  1534: PUBLIC BOOL HTHost_setRetry (HTHost * host, int retry)
                   1535: {
                   1536:     if (!host) return NO;
                   1537:     host->retry = retry;
                   1538:     return YES;
                   1539: }
                   1540: 
                   1541: PUBLIC BOOL HTHost_decreaseRetry (HTHost * host)
                   1542: {
2.44      frystyk  1543:     if (!host) return NO;
                   1544: 
                   1545:     if (host->retry > 0) host->retry--;
                   1546:     return YES;
                   1547: 
2.27      frystyk  1548: }
                   1549: 
                   1550: PUBLIC int HTHost_retry (HTHost * host)
                   1551: {
                   1552:     if (!host) return 0;
                   1553:     return host->retry;
                   1554: }
                   1555: 
2.13      frystyk  1556: #if 0  /* Is a macro right now */
2.21      frystyk  1557: PRIVATE BOOL HTHost_setDNS5 (HTHost * host, HTdns * dns)
2.13      frystyk  1558: {
                   1559:     if (!host) return NO;
                   1560:     host->dns = dns;
                   1561:     return YES;
                   1562: }
                   1563: #endif
                   1564: 
                   1565: PUBLIC BOOL HTHost_setChannel (HTHost * host, HTChannel * channel)
                   1566: {
                   1567:     if (!host) return NO;
                   1568:     host->channel = channel;
                   1569:     return YES;
                   1570: }
                   1571: 
                   1572: PUBLIC HTNet * HTHost_getReadNet(HTHost * host)
                   1573: {
2.38      frystyk  1574:     return host ? (HTNet *) HTList_firstObject(host->pipeline) : NULL;
2.13      frystyk  1575: }
                   1576: 
                   1577: PUBLIC HTNet * HTHost_getWriteNet(HTHost * host)
                   1578: {
                   1579:     return host ? (HTNet *) HTList_lastObject(host->pipeline) : NULL;
                   1580: }
                   1581: 
                   1582: /*
                   1583: **     Create the input stream and bind it to the channel
                   1584: **     Please read the description in the HTIOStream module for the parameters
                   1585: */
                   1586: PUBLIC HTInputStream * HTHost_getInput (HTHost * host, HTTransport * tp,
                   1587:                                        void * param, int mode)
                   1588: {
                   1589:     if (host && host->channel && tp) {
                   1590:        HTChannel * ch = host->channel;
                   1591:        HTInputStream * input = (*tp->input_new)(host, ch, param, mode);
                   1592:        HTChannel_setInput(ch, input);
                   1593:        return HTChannel_getChannelIStream(ch);
                   1594:     }
2.59      frystyk  1595:     HTTRACE(CORE_TRACE, "Host Object. Can't create input stream\n");
2.13      frystyk  1596:     return NULL;
                   1597: }
                   1598: 
                   1599: PUBLIC HTOutputStream * HTHost_getOutput (HTHost * host, HTTransport * tp,
                   1600:                                          void * param, int mode)
                   1601: {
                   1602:     if (host && host->channel && tp) {
                   1603:        HTChannel * ch = host->channel;
                   1604:        HTOutputStream * output = (*tp->output_new)(host, ch, param, mode);
                   1605:        HTChannel_setOutput(ch, output);
                   1606:        return output;
                   1607:     }
2.59      frystyk  1608:     HTTRACE(CORE_TRACE, "Host Object. Can't create output stream\n");
2.13      frystyk  1609:     return NULL;
                   1610: }
                   1611: 
                   1612: PUBLIC HTOutputStream * HTHost_output (HTHost * host, HTNet * net)
                   1613: {
                   1614:     if (host && host->channel && net) {
                   1615:        HTOutputStream * output = HTChannel_output(host->channel);
                   1616:        return output;
                   1617:     }
                   1618:     return NULL;
                   1619: }
                   1620: 
                   1621: PUBLIC int HTHost_read(HTHost * host, HTNet * net)
                   1622: {
                   1623:     HTInputStream * input = HTChannel_input(host->channel);
                   1624:     if (net != HTHost_getReadNet(host)) {
                   1625:        HTHost_register(host, net, HTEvent_READ);
                   1626:        return HT_WOULD_BLOCK;
                   1627:     }
2.17      frystyk  1628: 
                   1629:     /*
                   1630:     **  If there is no input channel then this can either mean that
                   1631:     **  we have lost the channel or an error occurred. We return
                   1632:     **  HT_CLOSED as this is a sign to the caller that we don't 
                   1633:     **  have a channel
                   1634:     */
                   1635:     return input ? (*input->isa->read)(input) : HT_CLOSED;
2.13      frystyk  1636: }
                   1637: 
                   1638: PUBLIC BOOL HTHost_setConsumed(HTHost * host, size_t bytes)
                   1639: {
                   1640:     HTInputStream * input;
                   1641:     if (!host || !host->channel) return NO;
                   1642:     if ((input = HTChannel_input(host->channel)) == NULL)
                   1643:        return NO;
2.59      frystyk  1644:     HTTRACE(CORE_TRACE, "Host........ passing %d bytes as consumed to %p\n" _ bytes _ input);
2.13      frystyk  1645:     return (*input->isa->consumed)(input, bytes);
                   1646: }
                   1647: 
                   1648: PUBLIC int HTHost_hash (HTHost * host)
                   1649: {
                   1650:     return host ? host->hash : -1;
                   1651: }
                   1652: 
2.26      frystyk  1653: PUBLIC BOOL HTHost_setWriteDelay (HTHost * host, ms_t delay)
2.13      frystyk  1654: {
2.26      frystyk  1655:     if (host && delay >= 0) {
                   1656:        host->delay = delay;
                   1657:        return YES;
                   1658:     }
                   1659:     return NO;
                   1660: }
                   1661: 
                   1662: PUBLIC ms_t HTHost_writeDelay (HTHost * host)
                   1663: {
                   1664:     return host ? host->delay : 0;
                   1665: }
                   1666: 
                   1667: PUBLIC int HTHost_findWriteDelay (HTHost * host, ms_t lastFlushTime, int buffSize)
                   1668: {
2.35      frystyk  1669: #if 0
2.15      eric     1670:     unsigned short mtu;
2.18      eric     1671:     int ret = -1;
2.15      eric     1672:     int socket = HTChannel_socket(host->channel);
2.18      eric     1673: #ifndef WWW_MSWINDOWS
2.15      eric     1674:     ret = ioctl(socket, 666, (unsigned long)&mtu);
2.18      eric     1675: #endif /* WWW_MSWINDOWS */
2.15      eric     1676:     if ((ret == 0 && buffSize >= mtu) || host->forceWriteFlush)
2.13      frystyk  1677:        return 0;
2.26      frystyk  1678:     return host->delay;
2.35      frystyk  1679: #else
                   1680:     return host->forceWriteFlush ? 0 : host->delay;
                   1681: #endif
2.13      frystyk  1682: }
                   1683: 
2.26      frystyk  1684: PUBLIC BOOL HTHost_setDefaultWriteDelay (ms_t delay)
                   1685: {
                   1686:     if (delay >= 0) {
                   1687:        WriteDelay = delay;
2.59      frystyk  1688:        HTTRACE(CORE_TRACE, "Host........ Default write delay is %d ms\n" _ delay);
2.26      frystyk  1689:        return YES;
                   1690:     }
                   1691:     return NO;
                   1692: }
                   1693: 
                   1694: PUBLIC ms_t HTHost_defaultWriteDelay (void)
                   1695: {
                   1696:     return WriteDelay;
                   1697: }
                   1698: 
2.13      frystyk  1699: PUBLIC int HTHost_forceFlush(HTHost * host)
                   1700: {
2.35      frystyk  1701:     HTNet * targetNet = (HTNet *) HTList_lastObject(host->pipeline);
2.13      frystyk  1702:     int ret;
2.35      frystyk  1703:     if (targetNet == NULL) return HT_ERROR;
2.59      frystyk  1704:     HTTRACE(CORE_TRACE, "Host Event.. FLUSH passed to `%s\'\n" _ 
2.28      frystyk  1705:                HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet))));
2.13      frystyk  1706:     host->forceWriteFlush = YES;
                   1707:     ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, HTEvent_FLUSH);
2.35      frystyk  1708:     host->forceWriteFlush = NO;
2.13      frystyk  1709:     return ret;
2.39      frystyk  1710: }
                   1711: 
                   1712: /*
                   1713: ** Context pointer to be used as a user defined context 
                   1714: */
                   1715: PUBLIC void HTHost_setContext (HTHost * me, void * context)
                   1716: {
2.40      kahan    1717:   if (me) me->context = context;
2.39      frystyk  1718: }
                   1719: 
                   1720: PUBLIC void * HTHost_context (HTHost * me)
                   1721: {
2.40      kahan    1722:   return me ? me->context : NULL;
2.1       frystyk  1723: }
2.11      kahan    1724: 
2.13      frystyk  1725: PUBLIC int HTHost_eventTimeout (void)
                   1726: {
                   1727:     return EventTimeout;
                   1728: }
2.11      kahan    1729: 
2.13      frystyk  1730: PUBLIC void HTHost_setEventTimeout (int millis)
                   1731: {
                   1732:     EventTimeout = millis;
2.59      frystyk  1733:     HTTRACE(CORE_TRACE, "Host........ Setting event timeout to %d ms\n" _ millis);
2.13      frystyk  1734: }
2.40      kahan    1735: 
2.45      frystyk  1736: PUBLIC BOOL HTHost_setMaxPipelinedRequests (int max)
                   1737: {
                   1738:     if (max > 1) {
                   1739:        MaxPipelinedRequests = max;
                   1740:        return YES;
                   1741:     }
                   1742:     return NO;
                   1743: }
2.40      kahan    1744: 
2.45      frystyk  1745: PUBLIC int HTHost_maxPipelinedRequests (void)
2.40      kahan    1746: {
2.45      frystyk  1747:     return MaxPipelinedRequests;
                   1748: }
                   1749: 
                   1750: PUBLIC void HTHost_setActivateRequestCallback (HTHost_ActivateRequestCallback * cbf)
                   1751: {
2.59      frystyk  1752:     HTTRACE(CORE_TRACE, "HTHost...... Registering %p\n" _ cbf);
2.40      kahan    1753:     ActivateReqCBF = cbf;
                   1754: }
                   1755: 
2.45      frystyk  1756: PRIVATE int HTHost_ActivateRequest (HTNet * net)
2.40      kahan    1757: {
2.45      frystyk  1758:     HTRequest * request = NULL;
                   1759:     if (!ActivateReqCBF) {
2.59      frystyk  1760:        HTTRACE(CORE_TRACE, "HTHost...... No ActivateRequest callback handler registered\n");
2.45      frystyk  1761:        return HT_ERROR;
                   1762:     }
                   1763:     request = HTNet_request(net);
                   1764:     return (*ActivateReqCBF)(request);
2.40      kahan    1765: }
                   1766: 
                   1767: PUBLIC void HTHost_disable_PendingReqLaunch (void)
                   1768: {
2.45      frystyk  1769:     DoPendingReqLaunch = NO;
2.40      kahan    1770: }
                   1771: 
                   1772: PUBLIC void HTHost_enable_PendingReqLaunch (void)
                   1773: {
2.45      frystyk  1774:     DoPendingReqLaunch = YES;
2.40      kahan    1775: }
                   1776: 

Webmaster