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

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

Webmaster