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