Annotation of libwww/Library/src/HTHost.c, revision 2.44
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.44 ! frystyk 6: ** @(#) $Id: HTHost.c,v 2.41 1998/03/05 21:56:16 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 */
2.44 ! frystyk 16: #include "wwwsys.h"
2.1 frystyk 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.44 ! 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.44 ! frystyk 184: HTTrace("Don't know how to handle OOB data from `%s\'?\n",
! 185: host->hostname);
2.14 frystyk 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: /*
2.44 ! frystyk 431: ** Get and set the cache timeout for persistent entries.
2.1 frystyk 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: /*
2.44 ! frystyk 899: * First check whether the net object is already on either queue.
! 900: * Do NOT add extra copies of the HTNet object to
! 901: * the pipeline or pending list (if it's already on the list).
! 902: */
! 903: if (HTList_indexOf(host->pipeline, net) >= 0)
! 904: {
! 905: if (CORE_TRACE)
! 906: HTTrace("Host info... The Net %p (request %p) is already on pipe,"
! 907: " %d requests made, %d requests in pipe, %d pending\n",
! 908: net, net->request, host->reqsMade,
! 909: HTList_count(host->pipeline),
! 910: HTList_count(host->pending));
! 911:
! 912: return HT_OK;
! 913: }
! 914:
! 915: if (HTList_indexOf(host->pending, net) >= 0)
! 916: {
! 917: if (CORE_TRACE)
! 918: HTTrace("Host info... The Net %p (request %p) already pending,"
! 919: " %d requests made, %d requests in pipe, %d pending\n",
! 920: net, net->request, host->reqsMade,
! 921: HTList_count(host->pipeline),
! 922: HTList_count(host->pending));
! 923:
! 924: return HT_PENDING;
! 925: }
! 926:
! 927: /*
2.18 eric 928: ** Add net object to either active or pending queue.
929: */
2.44 ! frystyk 930: if (_roomInPipe(host) && (HTList_isEmpty(host->pending) || doit)) {
2.32 frystyk 931: if (doit) host->doit = NULL;
2.8 frystyk 932: if (!host->pipeline) host->pipeline = HTList_new();
933: HTList_addObject(host->pipeline, net);
2.32 frystyk 934: host->reqsMade++;
935: if (CORE_TRACE)
936: HTTrace("Host info... Add Net %p (request %p) to pipe, %d requests made, %d requests in pipe, %d pending\n",
937: net, net->request, host->reqsMade, HTList_count(host->pipeline), HTList_count(host->pending));
2.18 eric 938:
2.13 frystyk 939: /*
2.30 frystyk 940: ** If we have been idle then make sure we delete the timer
2.13 frystyk 941: */
2.30 frystyk 942: if (host->timer) {
943: HTTimer_delete(host->timer);
944: host->timer = NULL;
945: }
2.40 kahan 946:
947: /*JK: New CBF function
948: ** Call any user-defined callback to say the request will
949: ** be processed.
950: */
951: HTHost_ActivateRequest (net);
2.30 frystyk 952:
2.8 frystyk 953: } else {
954: if (!host->pending) host->pending = HTList_new();
2.44 ! frystyk 955: HTList_addObject(host->pending, net);
2.32 frystyk 956: if (CORE_TRACE)
957: HTTrace("Host info... Add Net %p (request %p) to pending, %d requests made, %d requests in pipe, %d pending\n",
958: net, net->request, host->reqsMade, HTList_count(host->pipeline), HTList_count(host->pending));
2.8 frystyk 959: status = HT_PENDING;
960: }
961: return status;
962: }
963: return HT_ERROR;
964: }
965:
2.13 frystyk 966: PUBLIC BOOL HTHost_free (HTHost * host, int status)
967: {
2.32 frystyk 968: if (host->channel) {
2.13 frystyk 969:
2.32 frystyk 970: /* Check if we should keep the socket open */
971: if (HTHost_isPersistent(host)) {
2.37 frystyk 972: if (HTHost_closeNotification(host)) {
973: int piped = HTList_count(host->pipeline);
974: if (CORE_TRACE)
975: HTTrace("Host Object. got close notifiation on socket %d\n",
976: HTChannel_socket(host->channel));
977:
978: /*
979: ** If more than a single element (this one) in the pipe
980: ** then we have to recover gracefully
981: */
982: if (piped > 1) {
983: host->reqsPerConnection = host->reqsMade - piped;
984: if (CORE_TRACE)
985: HTTrace("%d requests made, %d in pipe, max %d requests pr connection\n",
986: host->reqsMade, piped, host->reqsPerConnection);
987: host->do_recover = YES;
2.38 frystyk 988: HTChannel_delete(host->channel, status);
989: } else {
990: HTChannel_setSemaphore(host->channel, 0);
991: HTHost_clearChannel(host, status);
2.37 frystyk 992: }
993: } else if ((HTList_count(host->pipeline) <= 1 &&
994: host->reqsMade == host->reqsPerConnection)) {
995: if (CORE_TRACE) HTTrace("Host Object. closing persistent socket %d\n",
996: HTChannel_socket(host->channel));
2.32 frystyk 997:
998: /*
999: ** By lowering the semaphore we make sure that the channel
1000: ** is gonna be deleted
1001: */
1002: HTChannel_setSemaphore(host->channel, 0);
1003: HTHost_clearChannel(host, status);
1004:
1005: } else {
1006: if (CORE_TRACE) HTTrace("Host Object. keeping persistent socket %d\n", HTChannel_socket(host->channel));
1007: HTChannel_delete(host->channel, status);
1008:
1009: /*
1010: ** If connection is idle then set a timer so that we close the
1011: ** connection if idle too long
1012: */
1013: if (HTHost_isIdle(host) && HTList_isEmpty(host->pending) && !host->timer) {
1014: host->timer = HTTimer_new(NULL, TimeoutEvent, host, TCP_IDLE_TTL, YES);
1015: if (PROT_TRACE) HTTrace("Host........ Object %p going idle...\n", host);
1016: }
1017: }
1018: return YES;
1019: } else {
2.33 frystyk 1020: if (CORE_TRACE) HTTrace("Host Object. closing socket %d\n", HTChannel_socket(host->channel));
2.41 frystyk 1021: HTHost_clearChannel(host, status);
2.32 frystyk 1022: }
2.13 frystyk 1023: }
2.32 frystyk 1024: return NO;
2.13 frystyk 1025: }
1026:
2.8 frystyk 1027: PUBLIC BOOL HTHost_deleteNet (HTHost * host, HTNet * net)
1028: {
1029: if (host && net) {
2.44 ! frystyk 1030: if (CORE_TRACE) HTTrace("Host info... Remove %p from pipe\n", net);
! 1031: HTList_removeObjectAll(host->pipeline, net);
! 1032: HTList_removeObjectAll(host->pending, net); /* just to make sure */
2.8 frystyk 1033: return YES;
1034: }
1035: return NO;
1036: }
1037:
1038: /*
1039: ** Handle pending host objects.
1040: ** There are two ways we can end up with pending reqyests:
1041: ** 1) If we are out of sockets then register new host objects as pending.
1042: ** 2) If we are pending on a connection then register new net objects as
1043: ** pending
1044: ** This set of functions handles pending host objects and can start new
1045: ** requests as resources get available
1046: */
1047:
1048: /*
1049: ** Check this host object for any pending requests and return the next
1050: ** registered Net object.
1051: */
1052: PUBLIC HTNet * HTHost_nextPendingNet (HTHost * host)
1053: {
1054: HTNet * net = NULL;
2.32 frystyk 1055: if (host && host->pending) {
2.18 eric 1056: /*JK 23/Sep/96 Bug correction. Associated the following lines to the
1057: **above if. There was a missing pair of brackets.
1058: */
1059: if ((net = (HTNet *) HTList_removeFirstObject(host->pending)) != NULL) {
2.32 frystyk 1060: if (CORE_TRACE)
1061: HTTrace("Host info... Popping %p from pending net queue\n", net);
2.33 frystyk 1062: #if 0
1063: {
1064: HTRequest * request = HTNet_request(net);
1065: char * uri = HTAnchor_address((HTAnchor *) HTRequest_anchor(request));
1066: fprintf(stderr, "Popping '%s'\n", uri);
1067: }
1068: #endif
2.32 frystyk 1069: host->doit = net;
2.18 eric 1070: }
2.8 frystyk 1071: }
1072: return net;
1073: }
1074:
1075: /*
2.13 frystyk 1076: ** Return the current list of pending host objects waiting for a socket
2.8 frystyk 1077: */
1078: PUBLIC HTHost * HTHost_nextPendingHost (void)
1079: {
1080: HTHost * host = NULL;
1081: if (PendHost) {
1082: if ((host = (HTHost *) HTList_removeFirstObject(PendHost)) != NULL)
1083: if (PROT_TRACE)
2.32 frystyk 1084: HTTrace("Host info... Popping %p from pending host queue\n",
2.8 frystyk 1085: host);
1086: }
1087: return host;
1088: }
1089:
1090: /*
1091: ** Start the next pending request if any. First we look for pending
1092: ** requests for the same host and then we check for any other pending
1093: ** hosts
1094: */
1095: PUBLIC BOOL HTHost_launchPending (HTHost * host)
1096: {
1097: int available = HTNet_availableSockets();
1098:
1099: if (!host) {
1100: if (PROT_TRACE) HTTrace("Host info... Bad arguments\n");
1101: return NO;
1102: }
1103:
1104: /*
1105: ** Check if we do have resources available for a new request
1106: ** This can either be reusing an existing connection or opening a new one
1107: */
1108: if (available > 0 || host->mode >= HT_TP_PIPELINE) {
2.23 eric 1109: HTNet * net;
2.8 frystyk 1110:
1111: /*
2.13 frystyk 1112: ** In pipeline we can only have one doing writing at a time.
1113: ** We therefore check that there are no other Net object
1114: ** registered for write
1115: */
1116: if (host->mode == HT_TP_PIPELINE) {
2.23 eric 1117: net = (HTNet *) HTList_lastObject(host->pipeline);
1118: if (net && net->registeredFor == HTEvent_WRITE)
2.13 frystyk 1119: return NO;
1120: }
1121:
1122: /*
1123: ** Check the current Host object for pending Net objects
2.23 eric 1124: **
1125: ** Send out as many as will fit in pipe.
2.8 frystyk 1126: */
2.23 eric 1127: while (_roomInPipe(host) && (net = HTHost_nextPendingNet(host))) {
2.40 kahan 1128: int status = NO;
1129: /* JK: Added new code to interrupt pending requests*/
1130: if (DoPendingReqLaunch) {
1131: HTHost_ActivateRequest (net);
1132: if (CORE_TRACE)
1133: HTTrace("Launch pending net object %p with %d reqs in pipe (%d reqs made)\n", net, HTList_count(host->pipeline), host->reqsMade);
1134: status = HTNet_execute(net, HTEvent_WRITE);
1135: } else
1136: {
1137: /* replace the object that was taken off the list */
1138: HTList_appendObject (host->pending, (void *) net);
1139: /* go the the other pending hosts routine */
1140: break;
1141: }
1142:
2.23 eric 1143: if (status != HT_OK)
1144: return status;
2.32 frystyk 1145: }
2.8 frystyk 1146:
1147: /*
1148: ** Check for other pending Host objects
1149: */
1150: {
1151: HTHost * pending = HTHost_nextPendingHost();
1152: if (pending) {
1153: HTNet * net = HTHost_nextPendingNet(pending);
2.40 kahan 1154: if (net) {
1155: if (DoPendingReqLaunch) {
1156: HTHost_ActivateRequest (net);
1157: return HTNet_execute(net, HTEvent_WRITE);
1158: } else {
1159: /* replace the object that was taken off the list */
1160: HTList_appendObject (PendHost, (void *) pending);
1161: return NO;
1162: }
1163: }
2.8 frystyk 1164: }
1165: }
2.13 frystyk 1166: } else
1167: if (PROT_TRACE) HTTrace("Host info... No more requests.\n");
1168: return NO;
1169: }
1170:
1171: PUBLIC HTNet * HTHost_firstNet (HTHost * host)
1172: {
1173: return (HTNet *) HTList_firstObject(host->pipeline);
1174: }
1175:
1176: /*
1177: ** The host event manager keeps track of the state of it's client engines
1178: ** (typically HTTPEvent), accepting multiple blocks on read or write from
1179: ** multiple pipelined engines. It then registers its own engine
1180: ** (HostEvent) with the event manager.
1181: */
1182: PUBLIC int HTHost_connect (HTHost * host, HTNet * net, char * url, HTProtocolId port)
1183: {
2.44 ! frystyk 1184: #if 1
2.42 frystyk 1185: HTRequest * request = HTNet_request(net);
2.13 frystyk 1186: int status;
2.42 frystyk 1187: if (!host) {
1188: HTProtocol * protocol = HTNet_protocol(net);
1189: if ((host = HTHost_newWParse(request, url, HTProtocol_id(protocol))) == NULL)
1190: return NO;
1191: if (!host->channel) {
1192: host->forceWriteFlush = YES;
1193: host->lock = net;
1194: }
1195: HTNet_setHost(net, host);
1196: }
1197:
1198: if (!host->lock || (host->lock && host->lock == net)) {
1199: status = HTDoConnect(net, url, port);
1200: if (status == HT_OK) {
1201: host->lock = NULL;
1202: return HT_OK;
1203: }
1204: if (status == HT_WOULD_BLOCK) {
1205: host->lock = net;
1206: return HT_WOULD_BLOCK;
1207: }
1208: if (status == HT_PENDING) return HT_WOULD_BLOCK;
1209: } else {
1210: if ((status = HTHost_addNet(host, net)) == HT_PENDING) {
1211: return HT_PENDING;
1212: }
1213: }
2.13 frystyk 1214: return HT_ERROR; /* @@@ - some more deletion and stuff here? */
2.44 ! frystyk 1215: #else
! 1216: int status;
! 1217: status = HTDoConnect(net, url, port);
! 1218: if (status == HT_OK) return HT_OK;
! 1219: if (status == HT_WOULD_BLOCK || status == HT_PENDING)
! 1220: return HT_WOULD_BLOCK;
! 1221: return HT_ERROR; /* @@@ - some more deletion and stuff here? */
! 1222: #endif
2.13 frystyk 1223: }
1224:
1225: /*
1226: ** Rules: SINGLE: one element in pipe, either reading or writing
1227: ** PIPE: n element in pipe, n-1 reading, 1 writing
1228: */
1229: PUBLIC int HTHost_register (HTHost * host, HTNet * net, HTEventType type)
1230: {
2.40 kahan 1231: HTEvent *event;
1232:
2.13 frystyk 1233: if (host && net) {
1234:
2.28 frystyk 1235: if (type == HTEvent_CLOSE) {
2.13 frystyk 1236:
2.28 frystyk 1237: /*
1238: ** Unregister this host for all events
1239: */
1240: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
1241: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
1242: host->registeredFor = 0;
2.13 frystyk 1243: return YES;
2.28 frystyk 1244:
1245: } else {
1246:
1247: /* net object may already be registered */
1248: if (HTEvent_BITS(type) & net->registeredFor)
1249: return NO;
1250: net->registeredFor ^= HTEvent_BITS(type);
1251:
1252: /* host object may already be registered */
1253: if (host->registeredFor & HTEvent_BITS(type))
1254: return YES;
1255: host->registeredFor ^= HTEvent_BITS(type);
2.40 kahan 1256: /* JK: register a request in the event structure */
1257: event = *(host->events+HTEvent_INDEX(type));
1258: event->request = HTNet_request (net);
2.28 frystyk 1259: return HTEvent_register(HTChannel_socket(host->channel),
2.40 kahan 1260: type, event);
2.28 frystyk 1261: }
2.13 frystyk 1262: }
1263: return NO;
1264: }
1265:
1266: PUBLIC int HTHost_unregister (HTHost * host, HTNet * net, HTEventType type)
1267: {
1268: if (host && net) {
1269:
2.28 frystyk 1270: /* net object may not be registered */
2.13 frystyk 1271: if (!(HTEvent_BITS(type) & net->registeredFor))
1272: return NO;
1273: net->registeredFor ^= HTEvent_BITS(type);
1274:
2.28 frystyk 1275: /* host object may not be registered */
2.13 frystyk 1276: if (!(host->registeredFor & HTEvent_BITS(type)))
1277: return YES;
1278: host->registeredFor ^= HTEvent_BITS(type);
1279:
1280: /* stay registered for READ to catch a socket close */
1281: /* WRITE and CONNECT can be unregistered, though */
1282: if ((type == HTEvent_WRITE && isLastInPipe(host, net)) ||
1283: type == HTEvent_CONNECT)
1284: /* if we are blocked downstream, shut down the whole pipe */
1285: HTEvent_unregister(HTChannel_socket(host->channel), type);
1286: return YES;
1287: }
1288: return NO;
1289: }
1290:
1291: /*
1292: ** The reader tells HostEvent that it's stream did not finish the data
1293: */
1294: PUBLIC BOOL HTHost_setRemainingRead (HTHost * host, size_t remaining)
1295: {
1296: if (host == NULL) return NO;
1297: host->remainingRead = remaining;
2.20 frystyk 1298: if (PROT_TRACE) HTTrace("Host........ %d bytes remaining \n", remaining);
2.13 frystyk 1299: return YES;
1300: }
1301:
2.32 frystyk 1302: PUBLIC size_t HTHost_remainingRead (HTHost * host)
1303: {
1304: return host ? host->remainingRead : -1;
1305: }
1306:
2.13 frystyk 1307: PUBLIC SockA * HTHost_getSockAddr (HTHost * host)
1308: {
1309: if (!host) return NULL;
1310: return &host->sock_addr;
1311: }
1312:
1313: PUBLIC BOOL HTHost_setHome (HTHost * host, int home)
1314: {
1315: if (!host) return NO;
1316: host->home = home;
1317: return YES;
1318: }
1319:
1320: PUBLIC int HTHost_home (HTHost * host)
1321: {
1322: if (!host) return 0;
1323: return host->home;
1324: }
1325:
2.27 frystyk 1326: PUBLIC BOOL HTHost_setRetry (HTHost * host, int retry)
1327: {
1328: if (!host) return NO;
1329: host->retry = retry;
1330: return YES;
1331: }
1332:
1333: PUBLIC BOOL HTHost_decreaseRetry (HTHost * host)
1334: {
2.44 ! frystyk 1335: if (!host) return NO;
! 1336:
! 1337: if (host->retry > 0) host->retry--;
! 1338: return YES;
! 1339:
2.27 frystyk 1340: }
1341:
1342: PUBLIC int HTHost_retry (HTHost * host)
1343: {
1344: if (!host) return 0;
1345: return host->retry;
1346: }
1347:
2.13 frystyk 1348: #if 0 /* Is a macro right now */
2.21 frystyk 1349: PRIVATE BOOL HTHost_setDNS5 (HTHost * host, HTdns * dns)
2.13 frystyk 1350: {
1351: if (!host) return NO;
1352: host->dns = dns;
1353: return YES;
1354: }
1355: #endif
1356:
1357: PUBLIC BOOL HTHost_setChannel (HTHost * host, HTChannel * channel)
1358: {
1359: if (!host) return NO;
1360: host->channel = channel;
1361: return YES;
1362: }
1363:
1364: PUBLIC HTNet * HTHost_getReadNet(HTHost * host)
1365: {
2.38 frystyk 1366: return host ? (HTNet *) HTList_firstObject(host->pipeline) : NULL;
2.13 frystyk 1367: }
1368:
1369: PUBLIC HTNet * HTHost_getWriteNet(HTHost * host)
1370: {
1371: return host ? (HTNet *) HTList_lastObject(host->pipeline) : NULL;
1372: }
1373:
1374: /*
1375: ** Create the input stream and bind it to the channel
1376: ** Please read the description in the HTIOStream module for the parameters
1377: */
1378: PUBLIC HTInputStream * HTHost_getInput (HTHost * host, HTTransport * tp,
1379: void * param, int mode)
1380: {
1381: if (host && host->channel && tp) {
1382: HTChannel * ch = host->channel;
1383: HTInputStream * input = (*tp->input_new)(host, ch, param, mode);
1384: HTChannel_setInput(ch, input);
1385: return HTChannel_getChannelIStream(ch);
1386: }
2.24 frystyk 1387: if (CORE_TRACE) HTTrace("Host Object. Can't create input stream\n");
2.13 frystyk 1388: return NULL;
1389: }
1390:
1391: PUBLIC HTOutputStream * HTHost_getOutput (HTHost * host, HTTransport * tp,
1392: void * param, int mode)
1393: {
1394: if (host && host->channel && tp) {
1395: HTChannel * ch = host->channel;
1396: HTOutputStream * output = (*tp->output_new)(host, ch, param, mode);
1397: HTChannel_setOutput(ch, output);
1398: return output;
1399: }
2.24 frystyk 1400: if (CORE_TRACE) HTTrace("Host Object. Can't create output stream\n");
2.13 frystyk 1401: return NULL;
1402: }
1403:
1404: PUBLIC HTOutputStream * HTHost_output (HTHost * host, HTNet * net)
1405: {
1406: if (host && host->channel && net) {
1407: HTOutputStream * output = HTChannel_output(host->channel);
1408: return output;
1409: }
1410: return NULL;
1411: }
1412:
1413: PUBLIC int HTHost_read(HTHost * host, HTNet * net)
1414: {
1415: HTInputStream * input = HTChannel_input(host->channel);
1416: if (net != HTHost_getReadNet(host)) {
1417: HTHost_register(host, net, HTEvent_READ);
1418: return HT_WOULD_BLOCK;
1419: }
2.17 frystyk 1420:
1421: /*
1422: ** If there is no input channel then this can either mean that
1423: ** we have lost the channel or an error occurred. We return
1424: ** HT_CLOSED as this is a sign to the caller that we don't
1425: ** have a channel
1426: */
1427: return input ? (*input->isa->read)(input) : HT_CLOSED;
2.13 frystyk 1428: }
1429:
1430: PUBLIC BOOL HTHost_setConsumed(HTHost * host, size_t bytes)
1431: {
1432: HTInputStream * input;
1433: if (!host || !host->channel) return NO;
1434: if ((input = HTChannel_input(host->channel)) == NULL)
1435: return NO;
2.32 frystyk 1436: if (CORE_TRACE)
2.20 frystyk 1437: HTTrace("Host........ passing %d bytes as consumed to %p\n", bytes, input);
2.13 frystyk 1438: return (*input->isa->consumed)(input, bytes);
1439: }
1440:
1441: PUBLIC int HTHost_hash (HTHost * host)
1442: {
1443: return host ? host->hash : -1;
1444: }
1445:
2.26 frystyk 1446: PUBLIC BOOL HTHost_setWriteDelay (HTHost * host, ms_t delay)
2.13 frystyk 1447: {
2.26 frystyk 1448: if (host && delay >= 0) {
1449: host->delay = delay;
1450: return YES;
1451: }
1452: return NO;
1453: }
1454:
1455: PUBLIC ms_t HTHost_writeDelay (HTHost * host)
1456: {
1457: return host ? host->delay : 0;
1458: }
1459:
1460: PUBLIC int HTHost_findWriteDelay (HTHost * host, ms_t lastFlushTime, int buffSize)
1461: {
2.35 frystyk 1462: #if 0
2.15 eric 1463: unsigned short mtu;
2.18 eric 1464: int ret = -1;
2.15 eric 1465: int socket = HTChannel_socket(host->channel);
2.18 eric 1466: #ifndef WWW_MSWINDOWS
2.15 eric 1467: ret = ioctl(socket, 666, (unsigned long)&mtu);
2.18 eric 1468: #endif /* WWW_MSWINDOWS */
2.15 eric 1469: if ((ret == 0 && buffSize >= mtu) || host->forceWriteFlush)
2.13 frystyk 1470: return 0;
2.26 frystyk 1471: return host->delay;
2.35 frystyk 1472: #else
1473: return host->forceWriteFlush ? 0 : host->delay;
1474: #endif
2.13 frystyk 1475: }
1476:
2.26 frystyk 1477: PUBLIC BOOL HTHost_setDefaultWriteDelay (ms_t delay)
1478: {
1479: if (delay >= 0) {
1480: WriteDelay = delay;
1481: if (CORE_TRACE) HTTrace("Host........ Default write delay is %d ms\n", delay);
1482: return YES;
1483: }
1484: return NO;
1485: }
1486:
1487: PUBLIC ms_t HTHost_defaultWriteDelay (void)
1488: {
1489: return WriteDelay;
1490: }
1491:
2.13 frystyk 1492: PUBLIC int HTHost_forceFlush(HTHost * host)
1493: {
2.35 frystyk 1494: HTNet * targetNet = (HTNet *) HTList_lastObject(host->pipeline);
2.13 frystyk 1495: int ret;
2.35 frystyk 1496: if (targetNet == NULL) return HT_ERROR;
2.13 frystyk 1497: if (CORE_TRACE)
2.28 frystyk 1498: HTTrace("Host Event.. FLUSH passed to `%s\'\n",
1499: HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet))));
2.13 frystyk 1500: host->forceWriteFlush = YES;
1501: ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, HTEvent_FLUSH);
2.35 frystyk 1502: host->forceWriteFlush = NO;
2.13 frystyk 1503: return ret;
2.39 frystyk 1504: }
1505:
1506: /*
1507: ** Context pointer to be used as a user defined context
1508: */
1509: PUBLIC void HTHost_setContext (HTHost * me, void * context)
1510: {
2.40 kahan 1511: if (me) me->context = context;
2.39 frystyk 1512: }
1513:
1514: PUBLIC void * HTHost_context (HTHost * me)
1515: {
2.40 kahan 1516: return me ? me->context : NULL;
2.1 frystyk 1517: }
2.11 kahan 1518:
2.13 frystyk 1519: PUBLIC int HTHost_eventTimeout (void)
1520: {
1521: return EventTimeout;
1522: }
2.11 kahan 1523:
2.13 frystyk 1524: PUBLIC void HTHost_setEventTimeout (int millis)
1525: {
1526: EventTimeout = millis;
1527: if (CORE_TRACE) HTTrace("Host........ Setting event timeout to %d ms\n", millis);
1528: }
2.40 kahan 1529:
1530:
1531: PUBLIC void HTHost_setActivateRequestCallback (HTHost_ActivateRequestCallback *
1532: cbf)
1533: {
1534: if (CORE_TRACE) HTTrace("HTHost....... registering %p\n", cbf);
1535: ActivateReqCBF = cbf;
1536: }
1537:
1538: PRIVATE int HTHost_ActivateRequest (HTNet *net)
1539: {
1540: HTRequest *request;
1541:
1542: if (!ActivateReqCBF) {
2.44 ! frystyk 1543: if (CORE_TRACE) HTTrace("HTHost....... No ActivateRequest "
2.40 kahan 1544: "callback handler registered\n");
1545: return -1;
1546: }
1547:
1548: request = HTNet_request (net);
1549: return (*ActivateReqCBF) (request);
1550: }
1551:
1552: PUBLIC void HTHost_disable_PendingReqLaunch (void)
1553: {
1554: DoPendingReqLaunch = NO;
1555: }
1556:
1557: PUBLIC void HTHost_enable_PendingReqLaunch (void)
1558: {
1559: DoPendingReqLaunch = YES;
1560: }
1561:
Webmaster