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

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.12.2.14! frystyk     6: **     @(#) $Id: HTHost.c,v 2.12.2.13 1996/11/26 23:22:15 eric 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 */
                     16: #include "sysdep.h"
                     17: #include "WWWUtil.h"
2.12.2.2  frystyk    18: #include "WWWMux.h"
2.1       frystyk    19: #include "HTParse.h"
                     20: #include "HTAlert.h"
                     21: #include "HTError.h"
                     22: #include "HTNetMan.h"
                     23: #include "HTTrans.h"
2.12.2.1  eric       24: #include "HTDNS.h"
                     25: #include "HTTPUtil.h"
                     26: #include "HTTCP.h"
2.12.2.7  frystyk    27: #include "HTHost.h"                                     /* Implemented here */
                     28: #include "HTHstMan.h"
2.1       frystyk    29: 
                     30: #define HOST_TIMEOUT           43200L       /* Default host timeout is 12 h */
                     31: #define TCP_TIMEOUT            3600L           /* Default TCP timeout i 1 h */
2.12.2.1  eric       32: #define MAX_PIPES              5    /* maximum number of pipelined requests */
2.1       frystyk    33: 
2.12.2.1  eric       34: struct _HTInputStream {
                     35:     const HTInputStreamClass * isa;
2.1       frystyk    36: };
                     37: 
2.12.2.3  eric       38: PRIVATE int HostEvent(SOCKET soc, void * pVoid, HTEventType type);
2.12.2.1  eric       39: 
                     40: /* Type definitions and global variables etc. local to this module */
2.1       frystyk    41: PRIVATE time_t HostTimeout = HOST_TIMEOUT;       /* Timeout on host entries */
                     42: PRIVATE time_t TCPTimeout = TCP_TIMEOUT;  /* Timeout on persistent channels */
                     43: 
2.8       frystyk    44: PRIVATE HTList ** HostTable = NULL;
                     45: PRIVATE HTList * PendHost = NULL;          /* List of pending host elements */
2.1       frystyk    46: 
2.12.2.14! frystyk    47: PRIVATE int EventTimeout = -1;                 /* Global Host event timeout */
        !            48: 
2.1       frystyk    49: /* ------------------------------------------------------------------------- */
                     50: 
                     51: PRIVATE void free_object (HTHost * me)
                     52: {
                     53:     if (me) {
                     54:        HT_FREE(me->hostname);
                     55:        HT_FREE(me->type);
2.12      frystyk    56:        HT_FREE(me->server);
                     57:        HT_FREE(me->user_agent);
                     58:        HT_FREE(me->range_units);
2.3       eric       59:        if (me->channel) {
2.5       eric       60:            HTChannel_delete(me->channel, HT_OK);
2.3       eric       61:            me->channel = NULL;
                     62:        }
2.12.2.10  frystyk    63:        HTEvent_delete(me->events[0]);
                     64:        HTEvent_delete(me->events[1]);
2.8       frystyk    65:        HTList_delete(me->pipeline);
                     66:        HTList_delete(me->pending);
2.1       frystyk    67:        HT_FREE(me);
                     68:     }
                     69: }
                     70: 
                     71: PRIVATE BOOL delete_object (HTList * list, HTHost * me)
                     72: {
2.2       frystyk    73:     if (CORE_TRACE) HTTrace("Host info... object %p from list %p\n", me, list);
2.1       frystyk    74:     HTList_removeObject(list, (void *) me);
                     75:     free_object(me);
                     76:     return YES;
                     77: }
                     78: 
2.12.2.1  eric       79: PRIVATE BOOL isLastInPipe (HTHost * host, HTNet * net)
                     80: {
                     81:     return HTList_lastObject(host->pipeline) == net;
                     82: }
                     83: 
                     84: /*
                     85: **     HostEvent - host event manager - recieves events from the event 
                     86: **     manager and dispatches them to the client net objects by calling the 
                     87: **     net object's cbf.
                     88: **
                     89: */
2.12.2.3  eric       90: PRIVATE int HostEvent (SOCKET soc, void * pVoid, HTEventType type)
2.12.2.1  eric       91: {
                     92:     HTHost * host = (HTHost *)pVoid;
                     93: 
                     94:     if (type == HTEvent_READ) {
                     95:        HTNet * targetNet;
                     96: 
                     97:        /* call the first net object */
                     98:        do {
                     99:            int ret;
                    100:            targetNet = (HTNet *)HTList_firstObject(host->pipeline);
                    101:            if (targetNet) {
2.12.2.8  eric      102:                if (CORE_TRACE)
2.12.2.13  eric      103:                    HTTrace(HTHIDE("HostEvent: READ passed to %s.\n"), 
2.12.2.8  eric      104:                            HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
2.12.2.1  eric      105:                if ((ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), 
                    106:                                                  targetNet->event.param, type)) != HT_OK)
                    107:                    return ret;
                    108:            }
2.12.2.11  eric      109:            if (targetNet == NULL && host->remainingRead > 0) {
                    110:                HTTrace("HostEvent... Error: %d bytes left to read and nowhere to put them\n", host->remainingRead);
                    111:                host->remainingRead = 0;
                    112:                /*
                    113:                **      Fall through to close the channel
                    114:                */
                    115:            }
2.12.2.1  eric      116:        /* call pipelined net object to eat all the data in the channel */
                    117:        } while (host->remainingRead > 0);
                    118: 
                    119:        /* last target net should have set remainingRead to 0 */
                    120:        if (targetNet)
                    121:            return HT_OK;
                    122: 
                    123:        /* If there was notargetNet, it should be a close */
2.12.2.13  eric      124:        HTTrace(HTHIDE("HostEvent: host %s closed connection.\n"), 
2.12.2.1  eric      125:                host->hostname);
                    126: 
                    127:        /* Is there garbage in the channel? Let's check: */
                    128:        {
                    129:            char buf[256];
                    130:            int ret;
                    131:            while ((ret = NETREAD(HTChannel_socket(host->channel), buf, sizeof(buf))) > 0)
2.12.2.13  eric      132:                HTTrace(HTHIDE("Host %s had %d extraneous bytes.\n"));
2.12.2.1  eric      133:        }
                    134:        HTHost_clearChannel(host, HT_OK);
                    135:        return HT_OK; /* extra garbage does not constitute an application error */
                    136:        
                    137:     } else if (type == HTEvent_WRITE) {
2.12.2.11  eric      138:        HTNet * targetNet = (HTNet *)HTList_lastObject(host->pipeline);
2.12.2.1  eric      139:        if (targetNet) {
2.12.2.8  eric      140:            if (CORE_TRACE)
2.12.2.13  eric      141:                HTTrace(HTHIDE("HostEvent: WRITE passed to %s.\n"), 
2.12.2.8  eric      142:                        HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
2.12.2.1  eric      143:            return (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, type);
                    144:        }
2.12.2.13  eric      145:        HTTrace(HTHIDE("HostEvent: Who wants to write to %s?\n"), 
2.12.2.1  eric      146:                host->hostname);
                    147:        return HT_ERROR;
                    148:        }
                    149: 
2.12.2.13  eric      150:     HTTrace(HTHIDE("Don't know how to handle OOB data from %s?\n"), 
2.12.2.1  eric      151:            host->hostname);
                    152:     return HT_OK;
                    153: }
                    154: 
2.1       frystyk   155: /*
                    156: **     Search the host info cache for a host object or create a new one
                    157: **     and add it. Examples of host names are
                    158: **
                    159: **             www.w3.org
                    160: **             www.foo.com:8000
                    161: **             18.52.0.18
                    162: **
                    163: **     Returns Host object or NULL if error. You may get back an already
                    164: **     existing host object - you're not guaranteed a new one each time.
                    165: */
2.12.2.1  eric      166: 
2.1       frystyk   167: PUBLIC HTHost * HTHost_new (char * host)
                    168: {
                    169:     HTList * list = NULL;                          /* Current list in cache */
                    170:     HTHost * pres = NULL;
2.12.2.7  frystyk   171:     int hash = 0;
2.1       frystyk   172:     if (!host) {
2.2       frystyk   173:        if (CORE_TRACE) HTTrace("Host info... Bad argument\n");
2.1       frystyk   174:        return NULL;
                    175:     }
                    176:     
                    177:     /* Find a hash for this host */
                    178:     {
                    179:        char *ptr;
                    180:        for (ptr=host; *ptr; ptr++)
2.12.2.7  frystyk   181:            hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.1       frystyk   182:        if (!HostTable) {
2.12.2.7  frystyk   183:            if ((HostTable = (HTList **) HT_CALLOC(HOST_HASH_SIZE,
2.1       frystyk   184:                                                   sizeof(HTList *))) == NULL)
                    185:                HT_OUTOFMEM("HTHost_find");
                    186:        }
                    187:        if (!HostTable[hash]) HostTable[hash] = HTList_new();
                    188:        list = HostTable[hash];
                    189:     }
                    190: 
                    191:     /* Search the cache */
                    192:     {
                    193:        HTList * cur = list;
                    194:        while ((pres = (HTHost *) HTList_nextObject(cur))) {
                    195:            if (!strcmp(pres->hostname, host)) {
2.8       frystyk   196:                if (HTHost_isIdle(pres) && time(NULL)>pres->ntime+HostTimeout){
2.2       frystyk   197:                    if (CORE_TRACE)
2.1       frystyk   198:                        HTTrace("Host info... Collecting host info %p\n",pres);
                    199:                    delete_object(list, pres);
                    200:                    pres = NULL;
                    201:                }
                    202:                break;
                    203:            }
                    204:        }
                    205:     }
                    206: 
2.8       frystyk   207:     /* If not found then create new Host object, else use existing one */
2.1       frystyk   208:     if (pres) {
                    209:        if (pres->channel) {
2.12.2.6  eric      210:            if (pres->expires && pres->expires < time(NULL)) {     /* Cached channel is cold */
2.2       frystyk   211:                if (CORE_TRACE)
2.1       frystyk   212:                    HTTrace("Host info... Persistent channel %p gotten cold\n",
                    213:                            pres->channel);
2.5       eric      214:                HTChannel_delete(pres->channel, HT_OK);
2.1       frystyk   215:                pres->channel = NULL;
                    216:            } else {
2.2       frystyk   217:                if (CORE_TRACE)
2.1       frystyk   218:                    HTTrace("Host info... REUSING CHANNEL %p\n",pres->channel);
                    219:            }
                    220:        }
                    221:     } else {
                    222:        if ((pres = (HTHost *) HT_CALLOC(1, sizeof(HTHost))) == NULL)
                    223:            HT_OUTOFMEM("HTHost_add");
2.12.2.7  frystyk   224:        pres->hash = hash;
2.1       frystyk   225:        StrAllocCopy(pres->hostname, host);
                    226:        pres->ntime = time(NULL);
2.8       frystyk   227:        pres->mode = HT_TP_SINGLE;
2.12.2.10  frystyk   228:        pres->events[HTEvent_INDEX(HTEvent_READ)] = HTEvent_new(HostEvent, pres, HT_PRIORITY_MAX, -1);
                    229:        pres->events[HTEvent_INDEX(HTEvent_WRITE)]= HTEvent_new(HostEvent, pres, HT_PRIORITY_MAX, -1);
2.2       frystyk   230:        if (CORE_TRACE) 
2.1       frystyk   231:            HTTrace("Host info... added `%s\' to list %p\n", host, list);
                    232:        HTList_addObject(list, (void *) pres);
                    233:     }
                    234:     return pres;
2.9       frystyk   235: }
                    236: 
2.12.2.1  eric      237: PUBLIC HTHost * HTHost_newWParse (HTRequest * request, char * url, u_short default_port)
                    238: {
                    239:              char * port;
                    240:              char * fullhost = NULL;
                    241:              char * parsedHost = NULL;
                    242:              SockA * sin;
                    243:              HTHost * me;
                    244:              char * proxy = HTRequest_proxy(request);
                    245: 
                    246:              fullhost = HTParse(proxy ? proxy : url, "", PARSE_HOST);
                    247: 
                    248:              /* If there's an @ then use the stuff after it as a hostname */
                    249:              if (fullhost) {
                    250:                  char * at_sign;
                    251:                  if ((at_sign = strchr(fullhost, '@')) != NULL)
                    252:                      parsedHost = at_sign+1;
                    253:                  else
                    254:                      parsedHost = fullhost;
                    255:              }
                    256:              if (!parsedHost || !*parsedHost) {
                    257:                  HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_HOST,
                    258:                                     NULL, 0, "HTDoConnect");
                    259:                  HT_FREE(fullhost);
                    260:                  return NULL;
                    261:              }
                    262:              port = strchr(parsedHost, ':');
                    263:              if (PROT_TRACE)
                    264:                  HTTrace("HTDoConnect. Looking up `%s\'\n", parsedHost);
                    265:              if (port) {
                    266:                  *port++ = '\0';
                    267:                  if (!*port || !isdigit(*port))
                    268:                      port = 0;
                    269:              }
                    270:              /* Find information about this host */
                    271:              if ((me = HTHost_new(parsedHost)) == NULL) {
                    272:                  if (PROT_TRACE)HTTrace("HTDoConnect. Can't get host info\n");
                    273:                  me->tcpstate = TCP_ERROR;
                    274:                  return NULL;
                    275:              }
                    276:              sin = &me->sock_addr;
                    277:              memset((void *) sin, '\0', sizeof(SockA));
                    278: 
                    279: #ifdef DECNET
                    280:              sin->sdn_family = AF_DECnet;
                    281:              net->sock_addr.sdn_objnum = port ? (unsigned char)(strtol(port, (char **) 0, 10)) : DNP_OBJ;
                    282: #else  /* Internet */
                    283:              sin->sin_family = AF_INET;
                    284:              sin->sin_port = htons(port ? atol(port) : default_port);
                    285: #endif
2.12.2.9  eric      286:              HT_FREE(fullhost);        /* parsedHost points into fullhost */
2.12.2.1  eric      287:              return me;
                    288: }
                    289: 
2.9       frystyk   290: /*
                    291: **     Search the host info cache for a host object. Examples of host names:
                    292: **
                    293: **             www.w3.org
                    294: **             www.foo.com:8000
                    295: **             18.52.0.18
                    296: **
                    297: **     Returns Host object or NULL if not found.
                    298: */
                    299: PUBLIC HTHost * HTHost_find (char * host)
                    300: {
                    301:     HTList * list = NULL;                          /* Current list in cache */
                    302:     HTHost * pres = NULL;
                    303:     if (CORE_TRACE)
                    304:        HTTrace("Host info... Looking for `%s\'\n", host ? host : "<null>");
                    305: 
                    306:     /* Find a hash for this host */
                    307:     if (host && HostTable) {
                    308:        int hash = 0;
                    309:        char *ptr;
                    310:        for (ptr=host; *ptr; ptr++)
2.12.2.7  frystyk   311:            hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.9       frystyk   312:        if (!HostTable[hash]) return NULL;
                    313:        list = HostTable[hash];
                    314: 
                    315:        /* Search the cache */
                    316:        {
                    317:            HTList * cur = list;
                    318:            while ((pres = (HTHost *) HTList_nextObject(cur))) {
                    319:                if (!strcmp(pres->hostname, host)) {
                    320:                    if (time(NULL) > pres->ntime + HostTimeout) {
                    321:                        if (CORE_TRACE)
                    322:                            HTTrace("Host info... Collecting host %p\n", pres);
                    323:                        delete_object(list, pres);
                    324:                        pres = NULL;
                    325:                    } else {
                    326:                        if (CORE_TRACE)
                    327:                            HTTrace("Host info... Found `%s\'\n", host);
                    328:                    }
                    329:                    return pres;
                    330:                }
                    331:            }
                    332:        }
                    333:     }
                    334:     return NULL;
2.1       frystyk   335: }
                    336: 
                    337: /*
2.8       frystyk   338: **     Get and set the hostname of the remote host
                    339: */
                    340: PUBLIC char * HTHost_name (HTHost * host)
                    341: {
                    342:      return host ? host->hostname : NULL;
                    343: }
                    344: 
                    345: /*
2.1       frystyk   346: **     Get and set the type class of the remote host
                    347: */
                    348: PUBLIC char * HTHost_class (HTHost * host)
                    349: {
                    350:      return host ? host->type : NULL;
                    351: }
                    352: 
                    353: PUBLIC void HTHost_setClass (HTHost * host, char * s_class)
                    354: {
                    355:     if (host && s_class) StrAllocCopy(host->type, s_class);
                    356: }
                    357: 
                    358: /*
                    359: **     Get and set the version of the remote host
                    360: */
                    361: PUBLIC int HTHost_version (HTHost *host)
                    362: {
                    363:      return host ? host->version : 0;
                    364: }
                    365: 
                    366: PUBLIC void HTHost_setVersion (HTHost * host, int version)
                    367: {
                    368:     if (host) host->version = version;
                    369: }
                    370: 
                    371: /*
                    372: **     Get and set the cache timeout for persistent entries.
                    373: **     The default value is TCP_TIMEOUT
                    374: */
                    375: PUBLIC void HTHost_setPersistTimeout (time_t timeout)
                    376: {
                    377:     TCPTimeout = timeout;
                    378: }
                    379: 
                    380: PUBLIC time_t HTHost_persistTimeout (time_t timeout)
                    381: {
                    382:     return TCPTimeout;
                    383: }
                    384: 
                    385: /*     Persistent Connection Expiration
                    386: **     --------------------------------
                    387: **     Should normally not be used. If, then use calendar time.
                    388: */
                    389: PUBLIC void HTHost_setPersistExpires (HTHost * host, time_t expires)
                    390: {
                    391:     if (host) host->expires = expires;
                    392: }
                    393: 
                    394: PUBLIC time_t HTHost_persistExpires (HTHost * host)
                    395: {
                    396:     return host ? host->expires : -1;
                    397: }
                    398: 
                    399: /*
2.6       frystyk   400: **     Public methods for this host
                    401: */
                    402: PUBLIC HTMethod HTHost_publicMethods (HTHost * me)
                    403: {
                    404:     return me ? me->methods : METHOD_INVALID;
                    405: }
                    406: 
                    407: PUBLIC void HTHost_setPublicMethods (HTHost * me, HTMethod methodset)
                    408: {
                    409:     if (me) me->methods = methodset;
                    410: }
                    411: 
                    412: PUBLIC void HTHost_appendPublicMethods (HTHost * me, HTMethod methodset)
                    413: {
                    414:     if (me) me->methods |= methodset;
                    415: }
                    416: 
                    417: /*
                    418: **     Get and set the server name of the remote host
                    419: */
                    420: PUBLIC char * HTHost_server (HTHost * host)
                    421: {
                    422:      return host ? host->server : NULL;
                    423: }
                    424: 
                    425: PUBLIC BOOL HTHost_setServer (HTHost * host, const char * server)
                    426: {
                    427:     if (host && server) {
                    428:        StrAllocCopy(host->server, server);
                    429:        return YES;
                    430:     }
                    431:     return NO;
                    432: }
                    433: 
                    434: /*
                    435: **     Get and set the userAgent name of the remote host
                    436: */
                    437: PUBLIC char * HTHost_userAgent (HTHost * host)
                    438: {
                    439:      return host ? host->user_agent : NULL;
                    440: }
                    441: 
                    442: PUBLIC BOOL HTHost_setUserAgent (HTHost * host, const char * userAgent)
                    443: {
                    444:     if (host && userAgent) {
                    445:        StrAllocCopy(host->user_agent, userAgent);
                    446:        return YES;
2.12      frystyk   447:     }
                    448:     return NO;
                    449: }
                    450: 
                    451: /*
                    452: **     Get and set acceptable range units
                    453: */
                    454: PUBLIC char * HTHost_rangeUnits (HTHost * host)
                    455: {
                    456:      return host ? host->range_units : NULL;
                    457: }
                    458: 
                    459: PUBLIC BOOL HTHost_setRangeUnits (HTHost * host, const char * units)
                    460: {
                    461:     if (host && units) {
                    462:        StrAllocCopy(host->range_units, units);
                    463:        return YES;
                    464:     }
                    465:     return NO;
                    466: }
                    467: 
                    468: /*
                    469: **     Checks whether a specific range unit is OK. We always say
                    470: **     YES except if we have a specific statement from the server that
                    471: **     it doesn't understand byte ranges - that is - it has sent "none"
                    472: **     in a "Accept-Range" response header
                    473: */
                    474: PUBLIC BOOL HTHost_isRangeUnitAcceptable (HTHost * host, const char * unit)
                    475: {
                    476:     if (host && unit) {
                    477: #if 0
                    478:        if (host->range_units) {
                    479:            char * start = strcasestr(host->range_units, "none");
                    480: 
                    481:            /*
                    482:            **  Check that "none" is infact a token. It could be part of some
                    483:            **  other valid string, so we'd better check for it.
                    484:            */
                    485:            if (start) {
                    486:                
                    487:                
                    488:            }
                    489:            return NO;
                    490:        }
                    491: #endif
                    492:        return strcasecomp(unit, "bytes") ? NO : YES;
2.6       frystyk   493:     }
                    494:     return NO;
                    495: }
                    496: 
2.1       frystyk   497: /*     HTHost_catchClose
                    498: **     -----------------
                    499: **     This function is registered when the socket is idle so that we get
                    500: **     a notification if the socket closes at the other end. At this point
                    501: **     we can't use the request object as it might have been freed a long
                    502: **     time ago.
                    503: */
2.12.2.3  eric      504: PUBLIC int HTHost_catchClose (SOCKET soc, void * context, HTEventType type)
2.1       frystyk   505: {
2.12.2.1  eric      506:     HTNet * net = (HTNet *)context;
                    507:     HTHost * host = net->host;
2.2       frystyk   508:     if (CORE_TRACE)
2.12.2.1  eric      509:        HTTrace("Catch Close. called with socket %d with type %x\n",
                    510:                soc, type);
                    511:     if (type == HTEvent_READ) {
2.1       frystyk   512:        HTChannel * ch = HTChannel_find(soc);     /* Find associated channel */
2.8       frystyk   513:        HTHost * host = HTChannel_host(ch);           /* and associated host */
2.1       frystyk   514:        if (ch && host) {           
2.2       frystyk   515:            if (CORE_TRACE) HTTrace("Catch Close. CLOSING socket %d\n", soc);
2.8       frystyk   516:            HTHost_clearChannel(host, HT_OK);
2.1       frystyk   517:        } else {
2.2       frystyk   518:            if (CORE_TRACE) HTTrace("Catch Close. socket %d NOT FOUND!\n",soc);
2.1       frystyk   519:        }
                    520:     }
2.12.2.1  eric      521:     HTHost_unregister(host, net, HTEvent_CLOSE);
2.1       frystyk   522:     return HT_OK;
                    523: }
                    524: 
                    525: /*
                    526: **     As soon as we know that this host accepts persistent connections,
                    527: **     we associated the channel with the host. 
                    528: **     We don't want more than MaxSockets-2 connections to be persistent in
                    529: **     order to avoid deadlock.
                    530: */
2.12.2.1  eric      531: PUBLIC BOOL HTHost_setPersistent (HTHost *             host,
                    532:                                  BOOL                  persistent,
                    533:                                  HTTransportMode       mode)
2.1       frystyk   534: {
2.12.2.1  eric      535:     if (!host) return NO;
                    536: 
                    537:     if (!persistent) {
                    538:        /*
                    539:        **  We use the HT_IGNORE status code as we don't want to free
                    540:        **  the stream at this point in time. The situation we want to
                    541:        **  avoid is that we free the channel from within the stream pipe.
                    542:        **  This will lead to an infinite look having the stream freing
                    543:        **  itself.
                    544:        */
                    545:        return HTHost_clearChannel(host, HT_IGNORE);
                    546:     }
                    547: 
                    548:     if (host->persistent) {
2.2       frystyk   549:        if (CORE_TRACE) HTTrace("Host info... %p already persistent\n", host);
                    550:        return YES;
2.12.2.1  eric      551:     }
                    552: 
                    553:     {
                    554:        SOCKET sockfd = HTChannel_socket(host->channel);
2.8       frystyk   555:        if (sockfd != INVSOC && HTNet_availablePersistentSockets() > 0) {
2.12.2.1  eric      556:            host->persistent = YES;
2.12.2.7  frystyk   557:            HTHost_setMode(host, mode);
2.1       frystyk   558:            host->expires = time(NULL) + TCPTimeout;      /* Default timeout */
2.12.2.1  eric      559:            HTChannel_setHost(host->channel, host);
2.8       frystyk   560:            HTNet_increasePersistentSocket();
2.2       frystyk   561:            if (CORE_TRACE)
2.1       frystyk   562:                HTTrace("Host info... added host %p as persistent\n", host);
                    563:            return YES;
                    564:        } else {
2.2       frystyk   565:            if (CORE_TRACE)
                    566:                HTTrace("Host info... no room for persistent socket %d\n",
2.7       frystyk   567:                        sockfd);
2.1       frystyk   568:        }
                    569:     }
                    570:     return NO;
                    571: }
                    572: 
                    573: /*
2.12.2.1  eric      574: **     Check whether we have a persistent channel or not
                    575: */
                    576: PUBLIC BOOL HTHost_isPersistent (HTHost * host)
                    577: {
                    578:     return host && host->persistent;
                    579: }
                    580: 
                    581: /*
2.1       frystyk   582: **     Find persistent channel associated with this host.
                    583: */
                    584: PUBLIC HTChannel * HTHost_channel (HTHost * host)
                    585: {
                    586:     return host ? host->channel : NULL;
                    587: }
                    588: 
                    589: /*
                    590: **     Clear the persistent entry by deleting the channel object. Note that
                    591: **     the channel object is only deleted if it's not used anymore.
                    592: */
2.8       frystyk   593: PUBLIC BOOL HTHost_clearChannel (HTHost * host, int status)
2.1       frystyk   594: {
                    595:     if (host && host->channel) {
2.8       frystyk   596:        HTChannel_setHost(host->channel, NULL);
2.10      frystyk   597:        
2.12.2.6  eric      598:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
                    599:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
                    600: 
2.10      frystyk   601:        /*
                    602:        **  We don't want to recursively delete ourselves so if we are
                    603:        **  called from within the stream pipe then don't delete the channel
                    604:        **  at this point
                    605:        */
2.8       frystyk   606:        HTChannel_delete(host->channel, status);
2.1       frystyk   607:        host->expires = 0;
                    608:        host->channel = NULL;
2.8       frystyk   609:        HTNet_decreasePersistentSocket();
2.2       frystyk   610:        if (CORE_TRACE)
                    611:            HTTrace("Host info... removed host %p as persistent\n", host);
2.1       frystyk   612:        return YES;
                    613:     }
                    614:     return NO;
                    615: }
                    616: 
                    617: /*
2.8       frystyk   618: **     Handle the connection mode. The mode may change mode in the 
                    619: **     middle of a connection.
                    620: */
                    621: PUBLIC HTTransportMode HTHost_mode (HTHost * host, BOOL * active)
                    622: {
                    623:     return host ? host->mode : HT_TP_SINGLE;
                    624: }
                    625: 
                    626: /*
                    627: **     If the new mode is lower than the old mode then adjust the pipeline
                    628: **     accordingly. That is, if we are going into single mode then move
                    629: **     all entries in the pipeline and move the rest to the pending
                    630: **     queue. They will get launched at a later point in time.
                    631: */
                    632: PUBLIC BOOL HTHost_setMode (HTHost * host, HTTransportMode mode)
                    633: {
                    634:     if (host) {
                    635:        /*
                    636:        **  Check the new mode and see if we must adjust the queues.
                    637:        */
                    638:        if (mode == HT_TP_SINGLE && host->mode > mode) {
                    639:            int piped = HTList_count(host->pipeline);
                    640:            if (piped > 0) {
                    641:                int cnt;
                    642:                if (CORE_TRACE)
                    643:                    HTTrace("Host info... Moving %d Net objects from pipe line to pending queue\n", piped);
                    644:                if (!host->pending) host->pending = HTList_new();
                    645:                for (cnt=0; cnt<piped; cnt++) {
                    646:                    HTNet * net = HTList_removeFirstObject(host->pipeline);
2.12.2.12  eric      647:                    if (CORE_TRACE) HTTrace("Net Object.. Resetting %p\n", net);
                    648:                    (*net->event.cbf)(HTChannel_socket(host->channel), net->event.param, HTEvent_RESET);
2.8       frystyk   649:                    HTList_appendObject(host->pending, net);
                    650:                }
                    651:            }
                    652:        }
2.12.2.7  frystyk   653:        if (PROT_TRACE)
                    654:            HTTrace("Host info... New mode is %d for host %p\n", host->mode, host);
2.8       frystyk   655:        host->mode = mode;
2.12.2.12  eric      656:        return HTHost_launchPending(host);
                    657: #if 0
                    658:        {
                    659:            HTNet * net = (HTNet *)HTList_firstObject(host->pipeline);
                    660:            if (net)
                    661:                return HTNet_start(net);
                    662:            return YES;
                    663:        }
                    664: #endif
2.8       frystyk   665:     }
                    666:     return NO;
                    667: }
                    668: 
                    669: /*
                    670: **     Check whether a host is idle meaning if it is ready for a new
                    671: **     request which depends on the mode of the host. If the host is 
                    672: **     idle, i.e. ready for use then return YES else NO. If the host supports
                    673: **     persistent connections then still only return idle if no requests are
                    674: **     ongoing. 
                    675: */
                    676: PUBLIC BOOL HTHost_isIdle (HTHost * host)
                    677: {
                    678:     return (host && HTList_count(host->pipeline) <= 0);
                    679: }
                    680: 
2.12.2.1  eric      681: PRIVATE BOOL _roomInPipe (HTHost * host)
                    682: {
                    683:     int count;
                    684:     if (!host) return NO;
                    685:     count = HTList_count(host->pipeline);
2.12.2.12  eric      686:     switch (host->mode) {
                    687:     case HT_TP_SINGLE:
                    688:        return count <= 0;
                    689:     case HT_TP_PIPELINE:
                    690:        return count < MAX_PIPES;
                    691:     case HT_TP_INTERLEAVE:
                    692:        return YES;
                    693:     }
                    694:     return NO;
2.12.2.1  eric      695: }
                    696: 
2.8       frystyk   697: /*
                    698: **     Add a net object to the host object. If the host
                    699: **     is idle then add to active list (pipeline) else add
                    700: **     it to the pending list
                    701: **     Return HT_PENDING if we must pend, HT_OK, or HT_ERROR
                    702: */
                    703: PUBLIC int HTHost_addNet (HTHost * host, HTNet * net)
                    704: {
                    705:     if (host && net) {
                    706:        int status = HT_OK;
                    707: 
                    708:        /* Check to see if we can get a socket */
                    709:        if (HTNet_availableSockets() <= 0) {
                    710:            if (!PendHost) PendHost = HTList_new();
                    711:            if (CORE_TRACE)
                    712:                HTTrace("Host info... Add Host %p as pending\n", host);
                    713:            HTList_addObject(PendHost, host);
                    714:            status = HT_PENDING;
                    715:        }
                    716: 
                    717:        /* Add to either active or pending queue */
2.12.2.1  eric      718:        if (_roomInPipe(host)) {
2.8       frystyk   719:            if (CORE_TRACE) HTTrace("Host info... Add Net %p to pipeline of host %p\n", net, host);
                    720:            if (!host->pipeline) host->pipeline = HTList_new();
                    721:            HTList_addObject(host->pipeline, net);
2.12.2.8  eric      722: #if 0
2.8       frystyk   723:            /*
                    724:            **  We have been idle and must hence unregister our catch close
                    725:            **  event handler
                    726:            */
                    727:            if (host->channel) {
2.12.2.1  eric      728:                HTHost_unregister(host, net, HTEvent_CLOSE);
2.8       frystyk   729:            }
2.12.2.8  eric      730: #endif
2.12.2.1  eric      731:            /*
                    732:            ** Send out the request if we're not blocked on write
                    733:            */
                    734:            if (!(host->registeredFor & HTEvent_BITS(HTEvent_WRITE)))
2.12.2.12  eric      735:                status = HTHost_launchPending(host) == YES ? HT_OK : HT_ERROR;
2.8       frystyk   736:        } else {
                    737:            if (CORE_TRACE) HTTrace("Host info... Add Net %p as pending\n", net);
                    738:            if (!host->pending) host->pending = HTList_new();
                    739:            HTList_addObject(host->pending, net);
                    740:            status = HT_PENDING;
                    741:        }
                    742:        return status;
                    743:     }
                    744:     return HT_ERROR;
                    745: }
                    746: 
2.12.2.1  eric      747: PUBLIC BOOL HTHost_free (HTHost * host, int status)
                    748: {
                    749:     if (host->channel == NULL) return NO;
                    750:     if (host->persistent) {
                    751:        if (CORE_TRACE)
                    752:            HTTrace("Host Object. keeping socket %d\n", HTChannel_socket(host->channel));
                    753:        HTChannel_delete(host->channel, status);
                    754:     } else {
                    755:        if (CORE_TRACE)
                    756:            HTTrace("Host Object. closing socket %d\n", HTChannel_socket(host->channel));
                    757: 
                    758:        /* 
                    759:        **  By lowering the semaphore we make sure that the channel
                    760:        **  is gonna be deleted
                    761:        */
2.12.2.3  eric      762:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
                    763:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
                    764:        host->registeredFor = 0;
2.12.2.1  eric      765:        HTChannel_downSemaphore(host->channel);
                    766:        HTChannel_delete(host->channel, status);
                    767:        host->channel = NULL;
                    768:     }
                    769:     return YES;
                    770: }
                    771: 
2.8       frystyk   772: PUBLIC BOOL HTHost_deleteNet (HTHost * host, HTNet * net)
                    773: {
                    774:     if (host && net) {
                    775:        if (CORE_TRACE)
                    776:            HTTrace("Host info... Remove Net %p from pipe line\n", net);
                    777:        HTList_removeObject(host->pipeline, net);
                    778:        HTList_removeObject(host->pending, net);
                    779:        return YES;
                    780:     }
                    781:     return NO;
                    782: }
                    783: 
                    784: /*
                    785: **     Handle pending host objects.
                    786: **     There are two ways we can end up with pending reqyests:
                    787: **      1) If we are out of sockets then register new host objects as pending.
                    788: **      2) If we are pending on a connection then register new net objects as
                    789: **         pending
                    790: **     This set of functions handles pending host objects and can start new
                    791: **     requests as resources get available
                    792: */
                    793: 
                    794: /*
                    795: **     Check this host object for any pending requests and return the next
                    796: **     registered Net object.
                    797: */
                    798: PUBLIC HTNet * HTHost_nextPendingNet (HTHost * host)
                    799: {
                    800:     HTNet * net = NULL;
                    801:     if (host && host->pending && host->pipeline) {
2.11      kahan     802:       /*JK 23/Sep/96 Bug correction. Associated the following lines to the
                    803:       **above if. There was a missing pair of brackets. 
                    804:       */
                    805:       if ((net = (HTNet *) HTList_removeFirstObject(host->pending)) != NULL) {
                    806:        if (PROT_TRACE)
                    807:          HTTrace("Host info... Popping %p from pending net queue\n",
                    808:                  net);
2.8       frystyk   809:        HTList_addObject(host->pipeline, net);
2.11      kahan     810:       }
2.8       frystyk   811:     }
                    812:     return net;
                    813: }
                    814: 
                    815: /*
2.12.2.1  eric      816: **     Return the current list of pending host objects waiting for a socket
2.8       frystyk   817: */
                    818: PUBLIC HTHost * HTHost_nextPendingHost (void)
                    819: {
                    820:     HTHost * host = NULL;
                    821:     if (PendHost) {
                    822:        if ((host = (HTHost *) HTList_removeFirstObject(PendHost)) != NULL)
                    823:            if (PROT_TRACE)
                    824:                HTTrace("Host info... Poping %p from pending host queue\n",
                    825:                        host);
                    826:     }
                    827:     return host;
                    828: }
                    829: 
                    830: /*
                    831: **     Start the next pending request if any. First we look for pending
                    832: **     requests for the same host and then we check for any other pending
                    833: **     hosts
                    834: */
                    835: PUBLIC BOOL HTHost_launchPending (HTHost * host)
                    836: {
                    837:     int available = HTNet_availableSockets();
                    838: 
                    839:     if (!host) {
                    840:        if (PROT_TRACE) HTTrace("Host info... Bad arguments\n");
                    841:        return NO;
                    842:     }
                    843: 
                    844:     /*
                    845:     **  Check if we do have resources available for a new request
                    846:     **  This can either be reusing an existing connection or opening a new one
                    847:     */
                    848:     if (available > 0 || host->mode >= HT_TP_PIPELINE) {
                    849: 
                    850:        /*
2.12.2.7  frystyk   851:        **  In pipeline we can only have one doing writing at a time.
                    852:        **  We therefore check that there are no other Net object
                    853:        **  registered for write
2.12.2.1  eric      854:        */
2.12.2.7  frystyk   855:        if (host->mode == HT_TP_PIPELINE) {
                    856:            HTNet * last = (HTNet *) HTList_lastObject(host->pipeline);
                    857:            if (last && last->registeredFor == HTEvent_WRITE)
                    858:                return NO;
                    859:        }
2.12.2.1  eric      860: 
                    861:        /*
                    862:        **  Check the current Host object for pending Net objects
2.8       frystyk   863:        */
2.12.2.12  eric      864:        if (host && _roomInPipe(host)) {
2.8       frystyk   865:            HTNet * net = HTHost_nextPendingNet(host);
2.12.2.8  eric      866:            if (net) return HTNet_execute(net, HTEvent_WRITE);
2.8       frystyk   867:        }
                    868: 
                    869:        /*
                    870:        **  Check for other pending Host objects
                    871:        */
                    872:        {
                    873:            HTHost * pending = HTHost_nextPendingHost();
                    874:            if (pending) {
                    875:                HTNet * net = HTHost_nextPendingNet(pending);
2.12.2.8  eric      876:                if (net) return HTNet_execute(net, HTEvent_WRITE);
2.8       frystyk   877:            }
                    878:        }
                    879:     } else
2.12.2.1  eric      880:        if (PROT_TRACE) HTTrace("Host info... No more requests.\n");
2.8       frystyk   881:     return NO;
2.1       frystyk   882: }
2.11      kahan     883: 
2.12.2.1  eric      884: PUBLIC HTNet * HTHost_firstNet (HTHost * host)
                    885: {
                    886:     return (HTNet *) HTList_firstObject(host->pipeline);
                    887: }
                    888: 
                    889: /*
                    890: **     The host event manager keeps track of the state of it's client engines
                    891: **     (typically HTTPEvent), accepting multiple blocks on read or write from
                    892: **     multiple pipelined engines. It then registers its own engine 
                    893: **     (HostEvent) with the event manager.
                    894: */
2.12.2.4  frystyk   895: PUBLIC int HTHost_connect (HTHost * host, HTNet * net, char * url, HTProtocolId port)
2.12.2.1  eric      896: {
                    897:     int status;
                    898:     if (host && host->connecttime)
                    899:        return HT_OK;
                    900:     status = HTDoConnect(net, url, port);
                    901:     if (status == HT_OK)
                    902:        return HT_OK;
                    903:     if (status == HT_WOULD_BLOCK || status == HT_PENDING)
                    904:        return HT_WOULD_BLOCK;
                    905:     return HT_ERROR; /* @@@ - some more deletion and stuff here? */
                    906: }
                    907: 
                    908: /*
                    909: **     Rules: SINGLE: one element in pipe, either reading or writing
                    910: **              PIPE: n element in pipe, n-1 reading, 1 writing
                    911: */
2.12.2.3  eric      912: PUBLIC int HTHost_register (HTHost * host, HTNet * net, HTEventType type)
2.12.2.1  eric      913: {
2.12.2.7  frystyk   914:     if (host && net) {
2.12.2.1  eric      915: 
2.12.2.7  frystyk   916:        /* net object may already be registered */
                    917:        if (HTEvent_BITS(type) & net->registeredFor)
                    918:            return NO;
                    919:        net->registeredFor ^= HTEvent_BITS(type);
2.12.2.1  eric      920: 
2.12.2.7  frystyk   921:        /* host object may already be registered */
                    922:        if (host->registeredFor & HTEvent_BITS(type))
                    923:            return YES;
                    924:        host->registeredFor ^= HTEvent_BITS(type);
2.12.2.10  frystyk   925:        return HTEvent_register(HTChannel_socket(host->channel), type, *(host->events+HTEvent_INDEX(type)));
2.12.2.7  frystyk   926:     }
                    927:     return NO;
2.12.2.1  eric      928: }
                    929: 
2.12.2.3  eric      930: PUBLIC int HTHost_unregister (HTHost * host, HTNet * net, HTEventType type)
2.12.2.1  eric      931: {
2.12.2.7  frystyk   932:     if (host && net) {
2.12.2.1  eric      933: 
2.12.2.7  frystyk   934:        /* net object may no be registered */
                    935:        if (!(HTEvent_BITS(type) & net->registeredFor))
                    936:            return NO;
                    937:        net->registeredFor ^= HTEvent_BITS(type);
2.11      kahan     938: 
2.12.2.7  frystyk   939:        /* host object may no be registered */
                    940:        if (!(host->registeredFor & HTEvent_BITS(type)))
                    941:            return YES;
                    942:        host->registeredFor ^= HTEvent_BITS(type);
                    943: 
                    944:        /* stay registered for READ to catch a socket close */
                    945:        /* WRITE and CONNECT can be unregistered, though */
                    946:        if ((type == HTEvent_WRITE && isLastInPipe(host, net)) || 
                    947:            type == HTEvent_CONNECT)
                    948:            /* if we are blocked downstream, shut down the whole pipe */
                    949:            HTEvent_unregister(HTChannel_socket(host->channel), type);
                    950:        return YES;
                    951:     }
                    952:     return NO;
2.12.2.1  eric      953: }
2.11      kahan     954: 
2.12.2.1  eric      955: /*
                    956: **     The reader tells HostEvent that it's stream did not finish the data
                    957: */
                    958: PUBLIC BOOL HTHost_setRemainingRead (HTHost * host, size_t remaining)
                    959: {
                    960:     if (host == NULL) return NO;
                    961:     host->remainingRead = remaining;
                    962:     return YES;
                    963: }
2.11      kahan     964: 
2.12.2.1  eric      965: PUBLIC SockA * HTHost_getSockAddr (HTHost * host)
                    966: {
                    967:     if (!host) return NULL;
                    968:     return &host->sock_addr;
                    969: }
                    970: 
                    971: PUBLIC BOOL HTHost_setHome (HTHost * host, int home)
                    972: {
                    973:     if (!host) return NO;
                    974:     host->home = home;
                    975:     return YES;
                    976: }
                    977: 
                    978: PUBLIC int HTHost_home (HTHost * host)
                    979: {
                    980:     if (!host) return 0;
                    981:     return host->home;
                    982: }
                    983: 
                    984: #if 0  /* Is a macro right now */
                    985: PUBLIC BOOL HTHost_setDNS5 (HTHost * host, HTdns * dns)
                    986: {
                    987:     if (!host) return NO;
                    988:     host->dns = dns;
                    989:     return YES;
                    990: }
                    991: #endif
                    992: 
                    993: PUBLIC BOOL HTHost_setChannel (HTHost * host, HTChannel * channel)
                    994: {
                    995:     if (!host) return NO;
                    996:     host->channel = channel;
                    997:     return YES;
                    998: }
                    999: 
                   1000: PUBLIC HTNet * HTHost_getReadNet(HTHost * host)
                   1001: {
2.12.2.2  frystyk  1002:     if (host) {
2.12.2.7  frystyk  1003:        if (host->mode == HT_TP_INTERLEAVE) {
                   1004:            HTMuxChannel * muxch = HTMuxChannel_find(host);
                   1005:            return HTMuxChannel_net(muxch);
                   1006:        }
                   1007:        return (HTNet *) HTList_firstObject(host->pipeline);
2.12.2.2  frystyk  1008:     }
                   1009:     return NULL;
                   1010: }
                   1011: 
                   1012: PUBLIC HTNet * HTHost_getWriteNet(HTHost * host)
                   1013: {
                   1014:     return host ? (HTNet *) HTList_lastObject(host->pipeline) : NULL;
2.12.2.1  eric     1015: }
                   1016: 
                   1017: /*
                   1018: **     Create the input stream and bind it to the channel
                   1019: **     Please read the description in the HTIOStream module for the parameters
                   1020: */
2.12.2.2  frystyk  1021: PUBLIC HTInputStream * HTHost_getInput (HTHost * host, HTTransport * tp,
                   1022:                                        void * param, int mode)
2.12.2.1  eric     1023: {
2.12.2.2  frystyk  1024:     if (host && host->channel && tp) {
2.12.2.1  eric     1025:        HTChannel * ch = host->channel;
                   1026:        HTInputStream * input = (*tp->input_new)(host, ch, param, mode);
                   1027:        HTChannel_setInput(ch, input);
2.12.2.12  eric     1028:        return HTChannel_getChannelIStream(ch);
2.12.2.1  eric     1029:     }
                   1030:     if (CORE_TRACE) HTTrace("Host Object.. Can't create input stream\n");
2.12.2.2  frystyk  1031:     return NULL;
                   1032: }
                   1033: 
                   1034: PUBLIC HTOutputStream * HTHost_getOutput (HTHost * host, HTTransport * tp,
                   1035:                                          void * param, int mode)
                   1036: {
                   1037:     if (host && host->channel && tp) {
                   1038:        HTChannel * ch = host->channel;
                   1039:        HTOutputStream * output = (*tp->output_new)(host, ch, param, mode);
                   1040:        HTChannel_setOutput(ch, output);
                   1041:        return output;
                   1042:     }
                   1043:     if (CORE_TRACE) HTTrace("Host Object.. Can't create output stream\n");
                   1044:     return NULL;
                   1045: }
                   1046: 
                   1047: PUBLIC HTOutputStream * HTHost_output (HTHost * host, HTNet * net)
                   1048: {
                   1049:     if (host && host->channel && net) {
                   1050:        HTOutputStream * output = HTChannel_output(host->channel);
                   1051: 
                   1052:        /*
                   1053:        **  If we are in MUX mode then create new output stream on top
                   1054:        **  of the already existing one. Otherwise just return what we
                   1055:        **  have.
                   1056:        */
                   1057:        if (host->mode == HT_TP_INTERLEAVE) {
2.12.2.4  frystyk  1058:            HTStream * target = (HTStream *) HTChannel_output(host->channel);
                   1059:            output = HTMuxWriter_new(host, net, target);
2.12.2.2  frystyk  1060:        }
                   1061:        return output;
                   1062:     }
2.12.2.1  eric     1063:     return NULL;
                   1064: }
                   1065: 
2.12.2.8  eric     1066: PUBLIC int HTHost_read(HTHost * host, HTNet * net)
2.12.2.1  eric     1067: {
                   1068:     HTInputStream * input = HTChannel_input(host->channel);
2.12.2.8  eric     1069:     if (net != HTHost_getReadNet(host)) {
                   1070:        HTHost_register(host, net, HTEvent_READ);
                   1071:        return HT_WOULD_BLOCK;
                   1072:     }
2.12.2.1  eric     1073:     if (input == NULL) return HT_ERROR;
                   1074:     return (*input->isa->read)(input);
                   1075: }
                   1076: 
                   1077: PUBLIC BOOL HTHost_setConsumed(HTHost * host, size_t bytes)
                   1078: {
                   1079:     HTInputStream * input;
                   1080:     if (!host || !host->channel) return NO;
                   1081:     if ((input = HTChannel_input(host->channel)) == NULL)
                   1082:        return NO;
                   1083:     return (*input->isa->consumed)(input, bytes);
                   1084: }
2.1       frystyk  1085: 
2.12.2.7  frystyk  1086: PUBLIC int HTHost_hash (HTHost * host)
                   1087: {
                   1088:     return host ? host->hash : -1;
                   1089: }
2.12.2.11  eric     1090: 
                   1091: PUBLIC int HTHost_writeDelay(HTHost * host, int lastFlushTime, int buffSize)
                   1092: {
                   1093:     if (host->forceWriteFlush)
                   1094:        return 0;
                   1095:     return 1000;
                   1096: }
                   1097: 
                   1098: PUBLIC int HTHost_forceFlush(HTHost * host)
                   1099: {
                   1100:     HTNet * targetNet = (HTNet *)HTList_lastObject(host->pipeline);
                   1101:     int wasForced = host->forceWriteFlush;
                   1102:     int ret;
                   1103:     if (targetNet == NULL)
                   1104:        return HT_ERROR;
                   1105:     if (CORE_TRACE)
2.12.2.13  eric     1106:        HTTrace(HTHIDE("HostEvent: FLUSH passed to %s.\n"), 
2.12.2.11  eric     1107:                HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
                   1108:     host->forceWriteFlush = YES;
                   1109:     ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, HTEvent_FLUSH);
                   1110:     host->forceWriteFlush = wasForced;
                   1111:     return ret;
2.12.2.14! frystyk  1112: }
        !          1113: 
        !          1114: PUBLIC int HTHost_eventTimeout (void)
        !          1115: {
        !          1116:     return EventTimeout;
        !          1117: }
        !          1118: 
        !          1119: PUBLIC void HTHost_setEventTimeout (int millis)
        !          1120: {
        !          1121:     EventTimeout = millis;
        !          1122:     if (CORE_TRACE) HTTrace("Host........ Setting event timeout to %d ms\n", millis);
2.12.2.11  eric     1123: }
                   1124: 
                   1125: #if 0
                   1126: PRIVATE int lazyWriteFlushEvent (SOCKET soc, void * pVoid, HTEventType type)
                   1127: {
                   1128:     HTOutputStream * stream = (HTOutputStream *) pVoid;
                   1129:     HTBufferWriter_reallyFlush(me);
                   1130:     return HT_OK;
                   1131: }
                   1132: 
                   1133: /*
                   1134: **     HTHost_lazyFlush(host, cbf) - call cbf with an HTEvent_TIMEOUT 
                   1135: **     when the host's write interval has expired.
                   1136: */
                   1137: PUBLIC int HTHost_lazyFlush (HTHost * host, int (*lazyFlush)(HTOutputStream *))
                   1138: {
                   1139:     /*
                   1140:     **  If we are allowed to delay the flush then register an event with the
                   1141:     **  delay descibed by our delay variable. If we can't delay then flush 
                   1142:     **  right away.
                   1143:     */
                   1144:     if (host->delay_output) {
                   1145:        HTChannel * ch = HTHost_channel(host);
                   1146:        me->delay_event = HTEvent_new(FlushEvent, host, HT_PRIORITY_MAX, me->delay_ms);
                   1147:        HTEvent_register(HTChannel_socket(ch), HTEvent_TIMEOUT, me->delay_event);
                   1148:        me->delaying = YES;
                   1149:        if (PROT_TRACE) HTTrace("Buffer...... Waiting...\n");
                   1150: 
                   1151:     int                                delay_ms;                     /* Delay in ms */
                   1152:     BOOL                       delaying;
                   1153:     HTEvent *                  delay_event;
                   1154:            me->delay_ms = 10000;
                   1155: 
                   1156: PUBLIC int HTHost_cancelLazyFlush(me->host)
                   1157:            if (me->delay_event && me->delaying) {
                   1158:                HTChannel * ch = HTHost_channel(me->host);
                   1159:                HTEvent_unregister(HTChannel_socket(ch), HTEvent_TIMEOUT);
                   1160:                me->delaying = NO;
                   1161:            }
                   1162: #endif /* 0 */
                   1163: 

Webmaster