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

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

Webmaster