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

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

Webmaster