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