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

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

Webmaster