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

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

Webmaster