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

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

Webmaster