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