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

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

Webmaster