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

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

Webmaster