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

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

Webmaster