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

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

Webmaster