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

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

Webmaster