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