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

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

Webmaster