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