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

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

Webmaster