Annotation of libwww/Library/src/HTHost.c, revision 2.36
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.36 ! frystyk 6: ** @(#) $Id: HTHost.c,v 2.35 1998/01/04 16:53:56 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.36 ! frystyk 56: PRIVATE ms_t WriteDelay = DEFAULT_DELAY; /* Delay in ms */
2.26 frystyk 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';
2.36 ! frystyk 323: if (!*port || !isdigit((int) *port))
2.32 frystyk 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.34 frystyk 848: if (!host ||
849: (host->reqsPerConnection && host->reqsMade >= host->reqsPerConnection) ||
850: HTHost_closeNotification(host))
2.32 frystyk 851: return NO;
2.13 frystyk 852: count = HTList_count(host->pipeline);
853: switch (host->mode) {
854: case HT_TP_SINGLE:
855: return count <= 0;
856: case HT_TP_PIPELINE:
857: return count < MAX_PIPES;
858: case HT_TP_INTERLEAVE:
859: return YES;
860: }
861: return NO;
862: }
863:
2.8 frystyk 864: /*
865: ** Add a net object to the host object. If the host
866: ** is idle then add to active list (pipeline) else add
867: ** it to the pending list
868: ** Return HT_PENDING if we must pend, HT_OK, or HT_ERROR
869: */
870: PUBLIC int HTHost_addNet (HTHost * host, HTNet * net)
871: {
872: if (host && net) {
873: int status = HT_OK;
2.32 frystyk 874: BOOL doit = (host->doit==net);
2.8 frystyk 875:
2.18 eric 876: /*
877: ** If we don't have a socket already then check to see if we can get
878: ** one. Otherwise we put the host object into our pending queue.
879: */
880: if (!host->channel && HTNet_availableSockets() <= 0) {
2.8 frystyk 881: if (!PendHost) PendHost = HTList_new();
882: if (CORE_TRACE)
883: HTTrace("Host info... Add Host %p as pending\n", host);
884: HTList_addObject(PendHost, host);
885: status = HT_PENDING;
886: }
887:
2.18 eric 888: /*
889: ** Add net object to either active or pending queue.
890: */
2.32 frystyk 891: if (_roomInPipe(host) && (HTList_isEmpty(host->pending) || doit)) {
892: if (doit) host->doit = NULL;
2.8 frystyk 893: if (!host->pipeline) host->pipeline = HTList_new();
894: HTList_addObject(host->pipeline, net);
2.32 frystyk 895: host->reqsMade++;
896: if (CORE_TRACE)
897: HTTrace("Host info... Add Net %p (request %p) to pipe, %d requests made, %d requests in pipe, %d pending\n",
898: net, net->request, host->reqsMade, HTList_count(host->pipeline), HTList_count(host->pending));
2.18 eric 899:
2.13 frystyk 900: /*
2.30 frystyk 901: ** If we have been idle then make sure we delete the timer
2.13 frystyk 902: */
2.30 frystyk 903: if (host->timer) {
904: HTTimer_delete(host->timer);
905: host->timer = NULL;
906: }
907:
2.8 frystyk 908: } else {
909: if (!host->pending) host->pending = HTList_new();
910: HTList_addObject(host->pending, net);
2.32 frystyk 911: if (CORE_TRACE)
912: HTTrace("Host info... Add Net %p (request %p) to pending, %d requests made, %d requests in pipe, %d pending\n",
913: net, net->request, host->reqsMade, HTList_count(host->pipeline), HTList_count(host->pending));
2.8 frystyk 914: status = HT_PENDING;
915: }
916: return status;
917: }
918: return HT_ERROR;
919: }
920:
2.13 frystyk 921: PUBLIC BOOL HTHost_free (HTHost * host, int status)
922: {
2.32 frystyk 923: if (host->channel) {
2.13 frystyk 924:
2.32 frystyk 925: /* Check if we should keep the socket open */
926: if (HTHost_isPersistent(host)) {
927: if (HTHost_closeNotification(host) ||
928: (HTList_count(host->pipeline)<=1 && host->reqsMade==host->reqsPerConnection)) {
929: if (CORE_TRACE) HTTrace("Host Object. closing persistent socket %d\n", HTChannel_socket(host->channel));
930:
931: /*
932: ** By lowering the semaphore we make sure that the channel
933: ** is gonna be deleted
934: */
935: HTChannel_setSemaphore(host->channel, 0);
936: HTHost_clearChannel(host, status);
937:
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 {
2.33 frystyk 953: if (CORE_TRACE) HTTrace("Host Object. closing socket %d\n", HTChannel_socket(host->channel));
2.32 frystyk 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);
2.33 frystyk 1001: #if 0
1002: {
1003: HTRequest * request = HTNet_request(net);
1004: char * uri = HTAnchor_address((HTAnchor *) HTRequest_anchor(request));
1005: fprintf(stderr, "Popping '%s'\n", uri);
1006: }
1007: #endif
2.32 frystyk 1008: host->doit = net;
2.18 eric 1009: }
2.8 frystyk 1010: }
1011: return net;
1012: }
1013:
1014: /*
2.13 frystyk 1015: ** Return the current list of pending host objects waiting for a socket
2.8 frystyk 1016: */
1017: PUBLIC HTHost * HTHost_nextPendingHost (void)
1018: {
1019: HTHost * host = NULL;
1020: if (PendHost) {
1021: if ((host = (HTHost *) HTList_removeFirstObject(PendHost)) != NULL)
1022: if (PROT_TRACE)
2.32 frystyk 1023: HTTrace("Host info... Popping %p from pending host queue\n",
2.8 frystyk 1024: host);
1025: }
1026: return host;
1027: }
1028:
1029: /*
1030: ** Start the next pending request if any. First we look for pending
1031: ** requests for the same host and then we check for any other pending
1032: ** hosts
1033: */
1034: PUBLIC BOOL HTHost_launchPending (HTHost * host)
1035: {
1036: int available = HTNet_availableSockets();
1037:
1038: if (!host) {
1039: if (PROT_TRACE) HTTrace("Host info... Bad arguments\n");
1040: return NO;
1041: }
1042:
1043: /*
1044: ** Check if we do have resources available for a new request
1045: ** This can either be reusing an existing connection or opening a new one
1046: */
1047: if (available > 0 || host->mode >= HT_TP_PIPELINE) {
2.23 eric 1048: HTNet * net;
2.8 frystyk 1049:
1050: /*
2.13 frystyk 1051: ** In pipeline we can only have one doing writing at a time.
1052: ** We therefore check that there are no other Net object
1053: ** registered for write
1054: */
1055: if (host->mode == HT_TP_PIPELINE) {
2.23 eric 1056: net = (HTNet *) HTList_lastObject(host->pipeline);
1057: if (net && net->registeredFor == HTEvent_WRITE)
2.13 frystyk 1058: return NO;
1059: }
1060:
1061: /*
1062: ** Check the current Host object for pending Net objects
2.23 eric 1063: **
1064: ** Send out as many as will fit in pipe.
2.8 frystyk 1065: */
2.23 eric 1066: while (_roomInPipe(host) && (net = HTHost_nextPendingNet(host))) {
2.32 frystyk 1067: int status;
1068: if (CORE_TRACE)
1069: HTTrace("Launch pending net object %p with %d reqs in pipe (%d reqs made)\n",
1070: net, HTList_count(host->pipeline), host->reqsMade);
1071: status = HTNet_execute(net, HTEvent_WRITE);
2.23 eric 1072: if (status != HT_OK)
1073: return status;
2.32 frystyk 1074:
1075: }
2.8 frystyk 1076:
1077: /*
1078: ** Check for other pending Host objects
1079: */
1080: {
1081: HTHost * pending = HTHost_nextPendingHost();
1082: if (pending) {
1083: HTNet * net = HTHost_nextPendingNet(pending);
2.13 frystyk 1084: if (net) return HTNet_execute(net, HTEvent_WRITE);
2.8 frystyk 1085: }
1086: }
2.13 frystyk 1087: } else
1088: if (PROT_TRACE) HTTrace("Host info... No more requests.\n");
1089: return NO;
1090: }
1091:
1092: PUBLIC HTNet * HTHost_firstNet (HTHost * host)
1093: {
1094: return (HTNet *) HTList_firstObject(host->pipeline);
1095: }
1096:
1097: /*
1098: ** The host event manager keeps track of the state of it's client engines
1099: ** (typically HTTPEvent), accepting multiple blocks on read or write from
1100: ** multiple pipelined engines. It then registers its own engine
1101: ** (HostEvent) with the event manager.
1102: */
1103: PUBLIC int HTHost_connect (HTHost * host, HTNet * net, char * url, HTProtocolId port)
1104: {
1105: int status;
1106: status = HTDoConnect(net, url, port);
2.32 frystyk 1107: if (status == HT_OK) return HT_OK;
2.13 frystyk 1108: if (status == HT_WOULD_BLOCK || status == HT_PENDING)
1109: return HT_WOULD_BLOCK;
1110: return HT_ERROR; /* @@@ - some more deletion and stuff here? */
1111: }
1112:
1113: /*
1114: ** Rules: SINGLE: one element in pipe, either reading or writing
1115: ** PIPE: n element in pipe, n-1 reading, 1 writing
1116: */
1117: PUBLIC int HTHost_register (HTHost * host, HTNet * net, HTEventType type)
1118: {
1119: if (host && net) {
1120:
2.28 frystyk 1121: if (type == HTEvent_CLOSE) {
2.13 frystyk 1122:
2.28 frystyk 1123: /*
1124: ** Unregister this host for all events
1125: */
1126: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
1127: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
1128: host->registeredFor = 0;
2.13 frystyk 1129: return YES;
2.28 frystyk 1130:
1131: } else {
1132:
1133: /* net object may already be registered */
1134: if (HTEvent_BITS(type) & net->registeredFor)
1135: return NO;
1136: net->registeredFor ^= HTEvent_BITS(type);
1137:
1138: /* host object may already be registered */
1139: if (host->registeredFor & HTEvent_BITS(type))
1140: return YES;
1141: host->registeredFor ^= HTEvent_BITS(type);
1142: return HTEvent_register(HTChannel_socket(host->channel),
1143: type, *(host->events+HTEvent_INDEX(type)));
1144: }
2.13 frystyk 1145: }
1146: return NO;
1147: }
1148:
1149: PUBLIC int HTHost_unregister (HTHost * host, HTNet * net, HTEventType type)
1150: {
1151: if (host && net) {
1152:
2.28 frystyk 1153: /* net object may not be registered */
2.13 frystyk 1154: if (!(HTEvent_BITS(type) & net->registeredFor))
1155: return NO;
1156: net->registeredFor ^= HTEvent_BITS(type);
1157:
2.28 frystyk 1158: /* host object may not be registered */
2.13 frystyk 1159: if (!(host->registeredFor & HTEvent_BITS(type)))
1160: return YES;
1161: host->registeredFor ^= HTEvent_BITS(type);
1162:
1163: /* stay registered for READ to catch a socket close */
1164: /* WRITE and CONNECT can be unregistered, though */
1165: if ((type == HTEvent_WRITE && isLastInPipe(host, net)) ||
1166: type == HTEvent_CONNECT)
1167: /* if we are blocked downstream, shut down the whole pipe */
1168: HTEvent_unregister(HTChannel_socket(host->channel), type);
1169: return YES;
1170: }
1171: return NO;
1172: }
1173:
1174: /*
1175: ** The reader tells HostEvent that it's stream did not finish the data
1176: */
1177: PUBLIC BOOL HTHost_setRemainingRead (HTHost * host, size_t remaining)
1178: {
1179: if (host == NULL) return NO;
1180: host->remainingRead = remaining;
2.20 frystyk 1181: if (PROT_TRACE) HTTrace("Host........ %d bytes remaining \n", remaining);
2.13 frystyk 1182: return YES;
1183: }
1184:
2.32 frystyk 1185: PUBLIC size_t HTHost_remainingRead (HTHost * host)
1186: {
1187: return host ? host->remainingRead : -1;
1188: }
1189:
2.13 frystyk 1190: PUBLIC SockA * HTHost_getSockAddr (HTHost * host)
1191: {
1192: if (!host) return NULL;
1193: return &host->sock_addr;
1194: }
1195:
1196: PUBLIC BOOL HTHost_setHome (HTHost * host, int home)
1197: {
1198: if (!host) return NO;
1199: host->home = home;
1200: return YES;
1201: }
1202:
1203: PUBLIC int HTHost_home (HTHost * host)
1204: {
1205: if (!host) return 0;
1206: return host->home;
1207: }
1208:
2.27 frystyk 1209: PUBLIC BOOL HTHost_setRetry (HTHost * host, int retry)
1210: {
1211: if (!host) return NO;
1212: host->retry = retry;
1213: return YES;
1214: }
1215:
1216: PUBLIC BOOL HTHost_decreaseRetry (HTHost * host)
1217: {
1218: if (!host) {
1219: if (host->retry > 0) host->retry--;
1220: return YES;
1221: }
1222: return NO;
1223: }
1224:
1225: PUBLIC int HTHost_retry (HTHost * host)
1226: {
1227: if (!host) return 0;
1228: return host->retry;
1229: }
1230:
2.13 frystyk 1231: #if 0 /* Is a macro right now */
2.21 frystyk 1232: PRIVATE BOOL HTHost_setDNS5 (HTHost * host, HTdns * dns)
2.13 frystyk 1233: {
1234: if (!host) return NO;
1235: host->dns = dns;
1236: return YES;
1237: }
1238: #endif
1239:
1240: PUBLIC BOOL HTHost_setChannel (HTHost * host, HTChannel * channel)
1241: {
1242: if (!host) return NO;
1243: host->channel = channel;
1244: return YES;
1245: }
1246:
1247: PUBLIC HTNet * HTHost_getReadNet(HTHost * host)
1248: {
1249: if (host) {
1250: if (host->mode == HT_TP_INTERLEAVE) {
2.21 frystyk 1251: #ifdef HT_MUX
2.13 frystyk 1252: HTMuxChannel * muxch = HTMuxChannel_find(host);
1253: return HTMuxChannel_net(muxch);
2.21 frystyk 1254: #endif
2.13 frystyk 1255: }
2.32 frystyk 1256: return (HTNet *) HTList_firstObject(host->pipeline);
2.13 frystyk 1257: }
1258: return NULL;
1259: }
1260:
1261: PUBLIC HTNet * HTHost_getWriteNet(HTHost * host)
1262: {
1263: return host ? (HTNet *) HTList_lastObject(host->pipeline) : NULL;
1264: }
1265:
1266: /*
1267: ** Create the input stream and bind it to the channel
1268: ** Please read the description in the HTIOStream module for the parameters
1269: */
1270: PUBLIC HTInputStream * HTHost_getInput (HTHost * host, HTTransport * tp,
1271: void * param, int mode)
1272: {
1273: if (host && host->channel && tp) {
1274: HTChannel * ch = host->channel;
1275: HTInputStream * input = (*tp->input_new)(host, ch, param, mode);
1276: HTChannel_setInput(ch, input);
1277: return HTChannel_getChannelIStream(ch);
1278: }
2.24 frystyk 1279: if (CORE_TRACE) HTTrace("Host Object. Can't create input stream\n");
2.13 frystyk 1280: return NULL;
1281: }
1282:
1283: PUBLIC HTOutputStream * HTHost_getOutput (HTHost * host, HTTransport * tp,
1284: void * param, int mode)
1285: {
1286: if (host && host->channel && tp) {
1287: HTChannel * ch = host->channel;
1288: HTOutputStream * output = (*tp->output_new)(host, ch, param, mode);
1289: HTChannel_setOutput(ch, output);
1290: return output;
1291: }
2.24 frystyk 1292: if (CORE_TRACE) HTTrace("Host Object. Can't create output stream\n");
2.13 frystyk 1293: return NULL;
1294: }
1295:
1296: PUBLIC HTOutputStream * HTHost_output (HTHost * host, HTNet * net)
1297: {
1298: if (host && host->channel && net) {
1299: HTOutputStream * output = HTChannel_output(host->channel);
2.8 frystyk 1300:
1301: /*
2.13 frystyk 1302: ** If we are in MUX mode then create new output stream on top
1303: ** of the already existing one. Otherwise just return what we
1304: ** have.
2.8 frystyk 1305: */
2.13 frystyk 1306: if (host->mode == HT_TP_INTERLEAVE) {
2.21 frystyk 1307: #ifdef HT_MUX
2.13 frystyk 1308: HTStream * target = (HTStream *) HTChannel_output(host->channel);
1309: output = HTMuxWriter_new(host, net, target);
2.21 frystyk 1310: #endif
2.8 frystyk 1311: }
2.13 frystyk 1312: return output;
1313: }
1314: return NULL;
1315: }
1316:
1317: PUBLIC int HTHost_read(HTHost * host, HTNet * net)
1318: {
1319: HTInputStream * input = HTChannel_input(host->channel);
1320: if (net != HTHost_getReadNet(host)) {
1321: HTHost_register(host, net, HTEvent_READ);
1322: return HT_WOULD_BLOCK;
1323: }
2.17 frystyk 1324:
1325: /*
1326: ** If there is no input channel then this can either mean that
1327: ** we have lost the channel or an error occurred. We return
1328: ** HT_CLOSED as this is a sign to the caller that we don't
1329: ** have a channel
1330: */
1331: return input ? (*input->isa->read)(input) : HT_CLOSED;
2.13 frystyk 1332: }
1333:
1334: PUBLIC BOOL HTHost_setConsumed(HTHost * host, size_t bytes)
1335: {
1336: HTInputStream * input;
1337: if (!host || !host->channel) return NO;
1338: if ((input = HTChannel_input(host->channel)) == NULL)
1339: return NO;
2.32 frystyk 1340: if (CORE_TRACE)
2.20 frystyk 1341: HTTrace("Host........ passing %d bytes as consumed to %p\n", bytes, input);
2.13 frystyk 1342: return (*input->isa->consumed)(input, bytes);
1343: }
1344:
1345: PUBLIC int HTHost_hash (HTHost * host)
1346: {
1347: return host ? host->hash : -1;
1348: }
1349:
2.26 frystyk 1350: PUBLIC BOOL HTHost_setWriteDelay (HTHost * host, ms_t delay)
2.13 frystyk 1351: {
2.26 frystyk 1352: if (host && delay >= 0) {
1353: host->delay = delay;
1354: return YES;
1355: }
1356: return NO;
1357: }
1358:
1359: PUBLIC ms_t HTHost_writeDelay (HTHost * host)
1360: {
1361: return host ? host->delay : 0;
1362: }
1363:
1364: PUBLIC int HTHost_findWriteDelay (HTHost * host, ms_t lastFlushTime, int buffSize)
1365: {
2.35 frystyk 1366: #if 0
2.15 eric 1367: unsigned short mtu;
2.18 eric 1368: int ret = -1;
2.15 eric 1369: int socket = HTChannel_socket(host->channel);
2.18 eric 1370: #ifndef WWW_MSWINDOWS
2.15 eric 1371: ret = ioctl(socket, 666, (unsigned long)&mtu);
2.18 eric 1372: #endif /* WWW_MSWINDOWS */
2.15 eric 1373: if ((ret == 0 && buffSize >= mtu) || host->forceWriteFlush)
2.13 frystyk 1374: return 0;
2.26 frystyk 1375: return host->delay;
2.35 frystyk 1376: #else
1377: return host->forceWriteFlush ? 0 : host->delay;
1378: #endif
2.13 frystyk 1379: }
1380:
2.26 frystyk 1381: PUBLIC BOOL HTHost_setDefaultWriteDelay (ms_t delay)
1382: {
1383: if (delay >= 0) {
1384: WriteDelay = delay;
1385: if (CORE_TRACE) HTTrace("Host........ Default write delay is %d ms\n", delay);
1386: return YES;
1387: }
1388: return NO;
1389: }
1390:
1391: PUBLIC ms_t HTHost_defaultWriteDelay (void)
1392: {
1393: return WriteDelay;
1394: }
1395:
2.13 frystyk 1396: PUBLIC int HTHost_forceFlush(HTHost * host)
1397: {
2.35 frystyk 1398: HTNet * targetNet = (HTNet *) HTList_lastObject(host->pipeline);
2.13 frystyk 1399: int ret;
2.35 frystyk 1400: if (targetNet == NULL) return HT_ERROR;
2.13 frystyk 1401: if (CORE_TRACE)
2.28 frystyk 1402: HTTrace("Host Event.. FLUSH passed to `%s\'\n",
1403: HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet))));
2.13 frystyk 1404: host->forceWriteFlush = YES;
1405: ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, HTEvent_FLUSH);
2.35 frystyk 1406: host->forceWriteFlush = NO;
2.13 frystyk 1407: return ret;
2.1 frystyk 1408: }
2.11 kahan 1409:
2.13 frystyk 1410: PUBLIC int HTHost_eventTimeout (void)
1411: {
1412: return EventTimeout;
1413: }
2.11 kahan 1414:
2.13 frystyk 1415: PUBLIC void HTHost_setEventTimeout (int millis)
1416: {
1417: EventTimeout = millis;
1418: if (CORE_TRACE) HTTrace("Host........ Setting event timeout to %d ms\n", millis);
1419: }
Webmaster