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

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

Webmaster