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

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

Webmaster