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

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

Webmaster