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