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

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.25    ! frystyk     6: **     @(#) $Id: HTHost.c,v 2.24 1997/01/23 19:49:54 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.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.25    ! frystyk   848: #if 0
2.23      eric      849:     if (host->persistent && !(host->reqsMade >= host->reqsPerConnection && HTList_count(host->pipeline) <= 1))
2.25    ! frystyk   850: #else
        !           851:     /* Check this with FTP as well */
        !           852:     if (host->persistent &&
        !           853:        (!host->reqsPerConnection ||
        !           854:         (host->reqsPerConnection && host->reqsMade < host->reqsPerConnection)))
        !           855: #endif
2.23      eric      856:     {
2.13      frystyk   857:        if (CORE_TRACE)
                    858:            HTTrace("Host Object. keeping socket %d\n", HTChannel_socket(host->channel));
                    859:        HTChannel_delete(host->channel, status);
                    860:     } else {
                    861:        if (CORE_TRACE)
                    862:            HTTrace("Host Object. closing socket %d\n", HTChannel_socket(host->channel));
                    863: 
                    864:        /* 
                    865:        **  By lowering the semaphore we make sure that the channel
                    866:        **  is gonna be deleted
                    867:        */
2.23      eric      868: #if 1
                    869:        HTChannel_downSemaphore(host->channel);
                    870:        HTHost_clearChannel(host, status);
                    871: #else
2.13      frystyk   872:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
                    873:        HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
                    874:        host->registeredFor = 0;
                    875:        HTChannel_downSemaphore(host->channel);
                    876:        HTChannel_delete(host->channel, status);
                    877:        host->channel = NULL;
2.22      eric      878:        host->tcpstate = TCP_BEGIN;
2.23      eric      879: #endif
2.13      frystyk   880:     }
                    881:     return YES;
                    882: }
                    883: 
2.8       frystyk   884: PUBLIC BOOL HTHost_deleteNet (HTHost * host, HTNet * net)
                    885: {
                    886:     if (host && net) {
                    887:        if (CORE_TRACE)
                    888:            HTTrace("Host info... Remove Net %p from pipe line\n", net);
                    889:        HTList_removeObject(host->pipeline, net);
                    890:        HTList_removeObject(host->pending, net);
                    891:        return YES;
                    892:     }
                    893:     return NO;
                    894: }
                    895: 
                    896: /*
                    897: **     Handle pending host objects.
                    898: **     There are two ways we can end up with pending reqyests:
                    899: **      1) If we are out of sockets then register new host objects as pending.
                    900: **      2) If we are pending on a connection then register new net objects as
                    901: **         pending
                    902: **     This set of functions handles pending host objects and can start new
                    903: **     requests as resources get available
                    904: */
                    905: 
                    906: /*
                    907: **     Check this host object for any pending requests and return the next
                    908: **     registered Net object.
                    909: */
                    910: PUBLIC HTNet * HTHost_nextPendingNet (HTHost * host)
                    911: {
                    912:     HTNet * net = NULL;
                    913:     if (host && host->pending && host->pipeline) {
2.18      eric      914:        /*JK 23/Sep/96 Bug correction. Associated the following lines to the
                    915:        **above if. There was a missing pair of brackets. 
                    916:        */
                    917:        if ((net = (HTNet *) HTList_removeFirstObject(host->pending)) != NULL) {
                    918:          if (PROT_TRACE)
                    919:              HTTrace("Host info... Popping %p from pending net queue\n", net);
2.22      eric      920: /*       HTList_addObject(host->pipeline, net); */
2.18      eric      921:        }
2.8       frystyk   922:     }
                    923:     return net;
                    924: }
                    925: 
                    926: /*
2.13      frystyk   927: **     Return the current list of pending host objects waiting for a socket
2.8       frystyk   928: */
                    929: PUBLIC HTHost * HTHost_nextPendingHost (void)
                    930: {
                    931:     HTHost * host = NULL;
                    932:     if (PendHost) {
                    933:        if ((host = (HTHost *) HTList_removeFirstObject(PendHost)) != NULL)
                    934:            if (PROT_TRACE)
                    935:                HTTrace("Host info... Poping %p from pending host queue\n",
                    936:                        host);
                    937:     }
                    938:     return host;
                    939: }
                    940: 
                    941: /*
                    942: **     Start the next pending request if any. First we look for pending
                    943: **     requests for the same host and then we check for any other pending
                    944: **     hosts
                    945: */
                    946: PUBLIC BOOL HTHost_launchPending (HTHost * host)
                    947: {
                    948:     int available = HTNet_availableSockets();
                    949: 
                    950:     if (!host) {
                    951:        if (PROT_TRACE) HTTrace("Host info... Bad arguments\n");
                    952:        return NO;
                    953:     }
                    954: 
                    955:     /*
                    956:     **  Check if we do have resources available for a new request
                    957:     **  This can either be reusing an existing connection or opening a new one
                    958:     */
                    959:     if (available > 0 || host->mode >= HT_TP_PIPELINE) {
2.23      eric      960:        HTNet * net;
2.8       frystyk   961: 
                    962:        /*
2.13      frystyk   963:        **  In pipeline we can only have one doing writing at a time.
                    964:        **  We therefore check that there are no other Net object
                    965:        **  registered for write
                    966:        */
                    967:        if (host->mode == HT_TP_PIPELINE) {
2.23      eric      968:            net = (HTNet *) HTList_lastObject(host->pipeline);
                    969:            if (net && net->registeredFor == HTEvent_WRITE)
2.13      frystyk   970:                return NO;
                    971:        }
                    972: 
                    973:        /*
                    974:        **  Check the current Host object for pending Net objects
2.23      eric      975:        **
                    976:        **  Send out as many as will fit in pipe.
2.8       frystyk   977:        */
2.23      eric      978:        while (_roomInPipe(host) && (net = HTHost_nextPendingNet(host))) {
                    979:            int status = HTNet_execute(net, HTEvent_WRITE);
                    980:            if (status != HT_OK)
                    981:                return status;
2.8       frystyk   982:        }
                    983: 
                    984:        /*
                    985:        **  Check for other pending Host objects
                    986:        */
                    987:        {
                    988:            HTHost * pending = HTHost_nextPendingHost();
                    989:            if (pending) {
                    990:                HTNet * net = HTHost_nextPendingNet(pending);
2.13      frystyk   991:                if (net) return HTNet_execute(net, HTEvent_WRITE);
2.8       frystyk   992:            }
                    993:        }
2.13      frystyk   994:     } else
                    995:        if (PROT_TRACE) HTTrace("Host info... No more requests.\n");
                    996:     return NO;
                    997: }
                    998: 
                    999: PUBLIC HTNet * HTHost_firstNet (HTHost * host)
                   1000: {
                   1001:     return (HTNet *) HTList_firstObject(host->pipeline);
                   1002: }
                   1003: 
                   1004: /*
                   1005: **     The host event manager keeps track of the state of it's client engines
                   1006: **     (typically HTTPEvent), accepting multiple blocks on read or write from
                   1007: **     multiple pipelined engines. It then registers its own engine 
                   1008: **     (HostEvent) with the event manager.
                   1009: */
                   1010: PUBLIC int HTHost_connect (HTHost * host, HTNet * net, char * url, HTProtocolId port)
                   1011: {
                   1012:     int status;
2.18      eric     1013: /*    if (host && host->connecttime)
2.13      frystyk  1014:        return HT_OK;
2.18      eric     1015: */
2.13      frystyk  1016:     status = HTDoConnect(net, url, port);
2.22      eric     1017:     if (status == HT_OK) {
                   1018:        HTNet_host(net)->reqsMade++;    /* @@@ - what if there's a connect but no req sent? */
2.13      frystyk  1019:        return HT_OK;
2.22      eric     1020:     }
2.13      frystyk  1021:     if (status == HT_WOULD_BLOCK || status == HT_PENDING)
                   1022:        return HT_WOULD_BLOCK;
                   1023:     return HT_ERROR; /* @@@ - some more deletion and stuff here? */
                   1024: }
                   1025: 
                   1026: /*
                   1027: **     Rules: SINGLE: one element in pipe, either reading or writing
                   1028: **              PIPE: n element in pipe, n-1 reading, 1 writing
                   1029: */
                   1030: PUBLIC int HTHost_register (HTHost * host, HTNet * net, HTEventType type)
                   1031: {
                   1032:     if (host && net) {
                   1033: 
                   1034:        /* net object may already be registered */
                   1035:        if (HTEvent_BITS(type) & net->registeredFor)
                   1036:            return NO;
                   1037:        net->registeredFor ^= HTEvent_BITS(type);
                   1038: 
                   1039:        /* host object may already be registered */
                   1040:        if (host->registeredFor & HTEvent_BITS(type))
                   1041:            return YES;
                   1042:        host->registeredFor ^= HTEvent_BITS(type);
                   1043:        return HTEvent_register(HTChannel_socket(host->channel), type, *(host->events+HTEvent_INDEX(type)));
                   1044:     }
                   1045:     return NO;
                   1046: }
                   1047: 
                   1048: PUBLIC int HTHost_unregister (HTHost * host, HTNet * net, HTEventType type)
                   1049: {
                   1050:     if (host && net) {
                   1051: 
                   1052:        /* net object may no be registered */
                   1053:        if (!(HTEvent_BITS(type) & net->registeredFor))
                   1054:            return NO;
                   1055:        net->registeredFor ^= HTEvent_BITS(type);
                   1056: 
                   1057:        /* host object may no be registered */
                   1058:        if (!(host->registeredFor & HTEvent_BITS(type)))
                   1059:            return YES;
                   1060:        host->registeredFor ^= HTEvent_BITS(type);
                   1061: 
                   1062:        /* stay registered for READ to catch a socket close */
                   1063:        /* WRITE and CONNECT can be unregistered, though */
                   1064:        if ((type == HTEvent_WRITE && isLastInPipe(host, net)) || 
                   1065:            type == HTEvent_CONNECT)
                   1066:            /* if we are blocked downstream, shut down the whole pipe */
                   1067:            HTEvent_unregister(HTChannel_socket(host->channel), type);
                   1068:        return YES;
                   1069:     }
                   1070:     return NO;
                   1071: }
                   1072: 
                   1073: /*
                   1074: **     The reader tells HostEvent that it's stream did not finish the data
                   1075: */
                   1076: PUBLIC BOOL HTHost_setRemainingRead (HTHost * host, size_t remaining)
                   1077: {
                   1078:     if (host == NULL) return NO;
                   1079:     host->remainingRead = remaining;
2.20      frystyk  1080:     if (PROT_TRACE) HTTrace("Host........ %d bytes remaining \n", remaining);
2.13      frystyk  1081:     return YES;
                   1082: }
                   1083: 
                   1084: PUBLIC SockA * HTHost_getSockAddr (HTHost * host)
                   1085: {
                   1086:     if (!host) return NULL;
                   1087:     return &host->sock_addr;
                   1088: }
                   1089: 
                   1090: PUBLIC BOOL HTHost_setHome (HTHost * host, int home)
                   1091: {
                   1092:     if (!host) return NO;
                   1093:     host->home = home;
                   1094:     return YES;
                   1095: }
                   1096: 
                   1097: PUBLIC int HTHost_home (HTHost * host)
                   1098: {
                   1099:     if (!host) return 0;
                   1100:     return host->home;
                   1101: }
                   1102: 
                   1103: #if 0  /* Is a macro right now */
2.21      frystyk  1104: PRIVATE BOOL HTHost_setDNS5 (HTHost * host, HTdns * dns)
2.13      frystyk  1105: {
                   1106:     if (!host) return NO;
                   1107:     host->dns = dns;
                   1108:     return YES;
                   1109: }
                   1110: #endif
                   1111: 
                   1112: PUBLIC BOOL HTHost_setChannel (HTHost * host, HTChannel * channel)
                   1113: {
                   1114:     if (!host) return NO;
                   1115:     host->channel = channel;
                   1116:     return YES;
                   1117: }
                   1118: 
                   1119: PUBLIC HTNet * HTHost_getReadNet(HTHost * host)
                   1120: {
                   1121:     if (host) {
                   1122:        if (host->mode == HT_TP_INTERLEAVE) {
2.21      frystyk  1123: #ifdef HT_MUX
2.13      frystyk  1124:            HTMuxChannel * muxch = HTMuxChannel_find(host);
                   1125:            return HTMuxChannel_net(muxch);
2.21      frystyk  1126: #endif
2.13      frystyk  1127:        }
                   1128:        return (HTNet *) HTList_firstObject(host->pipeline);
                   1129:     }
                   1130:     return NULL;
                   1131: }
                   1132: 
                   1133: PUBLIC HTNet * HTHost_getWriteNet(HTHost * host)
                   1134: {
                   1135:     return host ? (HTNet *) HTList_lastObject(host->pipeline) : NULL;
                   1136: }
                   1137: 
                   1138: /*
                   1139: **     Create the input stream and bind it to the channel
                   1140: **     Please read the description in the HTIOStream module for the parameters
                   1141: */
                   1142: PUBLIC HTInputStream * HTHost_getInput (HTHost * host, HTTransport * tp,
                   1143:                                        void * param, int mode)
                   1144: {
                   1145:     if (host && host->channel && tp) {
                   1146:        HTChannel * ch = host->channel;
                   1147:        HTInputStream * input = (*tp->input_new)(host, ch, param, mode);
                   1148:        HTChannel_setInput(ch, input);
                   1149:        return HTChannel_getChannelIStream(ch);
                   1150:     }
2.24      frystyk  1151:     if (CORE_TRACE) HTTrace("Host Object. Can't create input stream\n");
2.13      frystyk  1152:     return NULL;
                   1153: }
                   1154: 
                   1155: PUBLIC HTOutputStream * HTHost_getOutput (HTHost * host, HTTransport * tp,
                   1156:                                          void * param, int mode)
                   1157: {
                   1158:     if (host && host->channel && tp) {
                   1159:        HTChannel * ch = host->channel;
                   1160:        HTOutputStream * output = (*tp->output_new)(host, ch, param, mode);
                   1161:        HTChannel_setOutput(ch, output);
                   1162:        return output;
                   1163:     }
2.24      frystyk  1164:     if (CORE_TRACE) HTTrace("Host Object. Can't create output stream\n");
2.13      frystyk  1165:     return NULL;
                   1166: }
                   1167: 
                   1168: PUBLIC HTOutputStream * HTHost_output (HTHost * host, HTNet * net)
                   1169: {
                   1170:     if (host && host->channel && net) {
                   1171:        HTOutputStream * output = HTChannel_output(host->channel);
2.8       frystyk  1172: 
                   1173:        /*
2.13      frystyk  1174:        **  If we are in MUX mode then create new output stream on top
                   1175:        **  of the already existing one. Otherwise just return what we
                   1176:        **  have.
2.8       frystyk  1177:        */
2.13      frystyk  1178:        if (host->mode == HT_TP_INTERLEAVE) {
2.21      frystyk  1179: #ifdef HT_MUX
2.13      frystyk  1180:            HTStream * target = (HTStream *) HTChannel_output(host->channel);
                   1181:            output = HTMuxWriter_new(host, net, target);
2.21      frystyk  1182: #endif
2.8       frystyk  1183:        }
2.13      frystyk  1184:        return output;
                   1185:     }
                   1186:     return NULL;
                   1187: }
                   1188: 
                   1189: PUBLIC int HTHost_read(HTHost * host, HTNet * net)
                   1190: {
                   1191:     HTInputStream * input = HTChannel_input(host->channel);
                   1192:     if (net != HTHost_getReadNet(host)) {
                   1193:        HTHost_register(host, net, HTEvent_READ);
                   1194:        return HT_WOULD_BLOCK;
                   1195:     }
2.17      frystyk  1196: 
                   1197:     /*
                   1198:     **  If there is no input channel then this can either mean that
                   1199:     **  we have lost the channel or an error occurred. We return
                   1200:     **  HT_CLOSED as this is a sign to the caller that we don't 
                   1201:     **  have a channel
                   1202:     */
                   1203:     return input ? (*input->isa->read)(input) : HT_CLOSED;
2.13      frystyk  1204: }
                   1205: 
                   1206: PUBLIC BOOL HTHost_setConsumed(HTHost * host, size_t bytes)
                   1207: {
                   1208:     HTInputStream * input;
                   1209:     if (!host || !host->channel) return NO;
                   1210:     if ((input = HTChannel_input(host->channel)) == NULL)
                   1211:        return NO;
2.20      frystyk  1212:     if (PROT_TRACE)
                   1213:        HTTrace("Host........ passing %d bytes as consumed to %p\n", bytes, input);
2.13      frystyk  1214:     return (*input->isa->consumed)(input, bytes);
                   1215: }
                   1216: 
                   1217: PUBLIC int HTHost_hash (HTHost * host)
                   1218: {
                   1219:     return host ? host->hash : -1;
                   1220: }
                   1221: 
2.14      frystyk  1222: PUBLIC int HTHost_writeDelay(HTHost * host, ms_t lastFlushTime, int buffSize)
2.13      frystyk  1223: {
2.15      eric     1224:     unsigned short mtu;
2.18      eric     1225:     int ret = -1;
2.15      eric     1226:     int socket = HTChannel_socket(host->channel);
2.18      eric     1227: #ifndef WWW_MSWINDOWS
2.15      eric     1228:     ret = ioctl(socket, 666, (unsigned long)&mtu);
2.18      eric     1229: #endif /* WWW_MSWINDOWS */
2.15      eric     1230:     if ((ret == 0 && buffSize >= mtu) || host->forceWriteFlush)
2.13      frystyk  1231:        return 0;
                   1232:     return 1000;
                   1233: }
                   1234: 
                   1235: PUBLIC int HTHost_forceFlush(HTHost * host)
                   1236: {
                   1237:     HTNet * targetNet = (HTNet *)HTList_lastObject(host->pipeline);
                   1238:     int wasForced = host->forceWriteFlush;
                   1239:     int ret;
                   1240:     if (targetNet == NULL)
                   1241:        return HT_ERROR;
                   1242:     if (CORE_TRACE)
2.14      frystyk  1243:        HTTrace(HTHIDE("Host Event.. FLUSH passed to %s.\n"), 
2.13      frystyk  1244:                HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
                   1245:     host->forceWriteFlush = YES;
                   1246:     ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, HTEvent_FLUSH);
                   1247:     host->forceWriteFlush = wasForced;
                   1248:     return ret;
2.1       frystyk  1249: }
2.11      kahan    1250: 
2.13      frystyk  1251: PUBLIC int HTHost_eventTimeout (void)
                   1252: {
                   1253:     return EventTimeout;
                   1254: }
2.11      kahan    1255: 
2.13      frystyk  1256: PUBLIC void HTHost_setEventTimeout (int millis)
                   1257: {
                   1258:     EventTimeout = millis;
                   1259:     if (CORE_TRACE) HTTrace("Host........ Setting event timeout to %d ms\n", millis);
                   1260: }

Webmaster