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