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