Annotation of libwww/Library/src/HTHost.c, revision 2.12.2.9
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.9! eric 6: ** @(#) $Id: HTHost.c,v 2.12.2.8 1996/11/08 19:49:40 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"
25: #include "HTTPUtil.h"
26: #include "HTTCP.h"
27: #include "HTWatch.h"
2.12.2.7 frystyk 28: #include "HTHost.h" /* Implemented here */
29: #include "HTHstMan.h"
2.1 frystyk 30:
31: #define HOST_TIMEOUT 43200L /* Default host timeout is 12 h */
32: #define TCP_TIMEOUT 3600L /* Default TCP timeout i 1 h */
2.12.2.1 eric 33: #define MAX_PIPES 5 /* maximum number of pipelined requests */
2.1 frystyk 34:
2.12.2.1 eric 35: struct _HTInputStream {
36: const HTInputStreamClass * isa;
2.1 frystyk 37: };
38:
2.12.2.3 eric 39: PRIVATE int HostEvent(SOCKET soc, void * pVoid, HTEventType type);
2.12.2.1 eric 40:
41: /* Type definitions and global variables etc. local to this module */
2.1 frystyk 42: PRIVATE time_t HostTimeout = HOST_TIMEOUT; /* Timeout on host entries */
43: PRIVATE time_t TCPTimeout = TCP_TIMEOUT; /* Timeout on persistent channels */
44:
2.8 frystyk 45: PRIVATE HTList ** HostTable = NULL;
46: PRIVATE HTList * PendHost = NULL; /* List of pending host elements */
2.1 frystyk 47:
48: /* ------------------------------------------------------------------------- */
49:
50: PRIVATE void free_object (HTHost * me)
51: {
52: if (me) {
53: HT_FREE(me->hostname);
54: HT_FREE(me->type);
2.12 frystyk 55: HT_FREE(me->server);
56: HT_FREE(me->user_agent);
57: HT_FREE(me->range_units);
2.3 eric 58: if (me->channel) {
2.5 eric 59: HTChannel_delete(me->channel, HT_OK);
2.3 eric 60: me->channel = NULL;
61: }
2.8 frystyk 62: HTList_delete(me->pipeline);
63: HTList_delete(me->pending);
2.1 frystyk 64: HT_FREE(me);
65: }
66: }
67:
68: PRIVATE BOOL delete_object (HTList * list, HTHost * me)
69: {
2.2 frystyk 70: if (CORE_TRACE) HTTrace("Host info... object %p from list %p\n", me, list);
2.1 frystyk 71: HTList_removeObject(list, (void *) me);
72: free_object(me);
73: return YES;
74: }
75:
2.12.2.1 eric 76: PRIVATE BOOL isLastInPipe (HTHost * host, HTNet * net)
77: {
78: return HTList_lastObject(host->pipeline) == net;
79: }
80:
81: /*
82: ** HostEvent - host event manager - recieves events from the event
83: ** manager and dispatches them to the client net objects by calling the
84: ** net object's cbf.
85: **
86: */
2.12.2.3 eric 87: PRIVATE int HostEvent (SOCKET soc, void * pVoid, HTEventType type)
2.12.2.1 eric 88: {
89: HTHost * host = (HTHost *)pVoid;
90:
91: if (type == HTEvent_READ) {
92: HTNet * targetNet;
93:
94: /* call the first net object */
95: do {
96: int ret;
97: targetNet = (HTNet *)HTList_firstObject(host->pipeline);
98: if (targetNet) {
2.12.2.8 eric 99: if (CORE_TRACE)
100: HTWATCH(HTWatch_TCP, host, HTHIDE("HostEvent: READ passed to %s.\n"),
101: HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
2.12.2.1 eric 102: if ((ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel),
103: targetNet->event.param, type)) != HT_OK)
104: return ret;
105: }
106: /* call pipelined net object to eat all the data in the channel */
107: } while (host->remainingRead > 0);
108:
109: /* last target net should have set remainingRead to 0 */
110: if (targetNet)
111: return HT_OK;
112:
113: /* If there was notargetNet, it should be a close */
114: HTWATCH(HTWatch_TCP, host, HTHIDE("HostEvent: host %s closed connection.\n"),
115: host->hostname);
116:
117: /* Is there garbage in the channel? Let's check: */
118: {
119: char buf[256];
120: int ret;
121: while ((ret = NETREAD(HTChannel_socket(host->channel), buf, sizeof(buf))) > 0)
122: HTWATCH(HTWatch_TCP, host, HTHIDE("Host %s had %d extraneous bytes.\n"));
123: }
124: HTHost_clearChannel(host, HT_OK);
125: return HT_OK; /* extra garbage does not constitute an application error */
126:
127: } else if (type == HTEvent_WRITE) {
128: HTNet * targetNet = (HTNet *)HTList_lastObject(host->pipeline);
129: if (targetNet) {
2.12.2.8 eric 130: if (CORE_TRACE)
131: HTWATCH(HTWatch_TCP, host, HTHIDE("HostEvent: WRITE passed to %s.\n"),
132: HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
2.12.2.1 eric 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;
2.12.2.7 frystyk 161: int hash = 0;
2.1 frystyk 162: if (!host) {
2.2 frystyk 163: if (CORE_TRACE) HTTrace("Host info... Bad argument\n");
2.1 frystyk 164: return NULL;
165: }
166:
167: /* Find a hash for this host */
168: {
169: char *ptr;
170: for (ptr=host; *ptr; ptr++)
2.12.2.7 frystyk 171: hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.1 frystyk 172: if (!HostTable) {
2.12.2.7 frystyk 173: if ((HostTable = (HTList **) HT_CALLOC(HOST_HASH_SIZE,
2.1 frystyk 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) {
2.12.2.6 eric 200: if (pres->expires && 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.1 frystyk 214: if ((pres = (HTHost *) HT_CALLOC(1, sizeof(HTHost))) == NULL)
215: HT_OUTOFMEM("HTHost_add");
2.12.2.5 frystyk 216: newEvent.param = pres;
2.12.2.7 frystyk 217: pres->hash = hash;
2.1 frystyk 218: StrAllocCopy(pres->hostname, host);
219: pres->ntime = time(NULL);
2.8 frystyk 220: pres->mode = HT_TP_SINGLE;
2.12.2.1 eric 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: sin = &me->sock_addr;
270: memset((void *) sin, '\0', sizeof(SockA));
271:
272: #ifdef DECNET
273: sin->sdn_family = AF_DECnet;
274: net->sock_addr.sdn_objnum = port ? (unsigned char)(strtol(port, (char **) 0, 10)) : DNP_OBJ;
275: #else /* Internet */
276: sin->sin_family = AF_INET;
277: sin->sin_port = htons(port ? atol(port) : default_port);
278: #endif
2.12.2.9! eric 279: HT_FREE(fullhost); /* parsedHost points into fullhost */
2.12.2.1 eric 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++)
2.12.2.7 frystyk 304: hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.9 frystyk 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.12.2.7 frystyk 550: HTHost_setMode(host, 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:
2.12.2.6 eric 591: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
592: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
593:
2.10 frystyk 594: /*
595: ** We don't want to recursively delete ourselves so if we are
596: ** called from within the stream pipe then don't delete the channel
597: ** at this point
598: */
2.8 frystyk 599: HTChannel_delete(host->channel, status);
2.1 frystyk 600: host->expires = 0;
601: host->channel = NULL;
2.8 frystyk 602: HTNet_decreasePersistentSocket();
2.2 frystyk 603: if (CORE_TRACE)
604: HTTrace("Host info... removed host %p as persistent\n", host);
2.1 frystyk 605: return YES;
606: }
607: return NO;
608: }
609:
610: /*
2.8 frystyk 611: ** Handle the connection mode. The mode may change mode in the
612: ** middle of a connection.
613: */
614: PUBLIC HTTransportMode HTHost_mode (HTHost * host, BOOL * active)
615: {
616: return host ? host->mode : HT_TP_SINGLE;
617: }
618:
619: /*
620: ** If the new mode is lower than the old mode then adjust the pipeline
621: ** accordingly. That is, if we are going into single mode then move
622: ** all entries in the pipeline and move the rest to the pending
623: ** queue. They will get launched at a later point in time.
624: */
625: PUBLIC BOOL HTHost_setMode (HTHost * host, HTTransportMode mode)
626: {
627: if (host) {
628: /*
629: ** Check the new mode and see if we must adjust the queues.
630: */
631: if (mode == HT_TP_SINGLE && host->mode > mode) {
632: int piped = HTList_count(host->pipeline);
633: if (piped > 0) {
634: int cnt;
635: if (CORE_TRACE)
636: HTTrace("Host info... Moving %d Net objects from pipe line to pending queue\n", piped);
637: if (!host->pending) host->pending = HTList_new();
638: for (cnt=0; cnt<piped; cnt++) {
639: HTNet * net = HTList_removeFirstObject(host->pipeline);
640: HTList_appendObject(host->pending, net);
641: }
642: }
643: }
2.12.2.7 frystyk 644: if (PROT_TRACE)
645: HTTrace("Host info... New mode is %d for host %p\n", host->mode, host);
2.8 frystyk 646: host->mode = mode;
647: return YES;
648: }
649: return NO;
650: }
651:
652: /*
653: ** Check whether a host is idle meaning if it is ready for a new
654: ** request which depends on the mode of the host. If the host is
655: ** idle, i.e. ready for use then return YES else NO. If the host supports
656: ** persistent connections then still only return idle if no requests are
657: ** ongoing.
658: */
659: PUBLIC BOOL HTHost_isIdle (HTHost * host)
660: {
661: return (host && HTList_count(host->pipeline) <= 0);
662: }
663:
2.12.2.1 eric 664: PRIVATE BOOL _roomInPipe (HTHost * host)
665: {
666: int count;
667: if (!host) return NO;
668: count = HTList_count(host->pipeline);
2.12.2.7 frystyk 669: return host->mode == HT_TP_INTERLEAVE ? YES :
670: (host->version == HTTP_11) ? (count < MAX_PIPES) : (count <= 0);
2.12.2.1 eric 671: }
672:
2.8 frystyk 673: /*
674: ** Add a net object to the host object. If the host
675: ** is idle then add to active list (pipeline) else add
676: ** it to the pending list
677: ** Return HT_PENDING if we must pend, HT_OK, or HT_ERROR
678: */
679: PUBLIC int HTHost_addNet (HTHost * host, HTNet * net)
680: {
681: if (host && net) {
682: int status = HT_OK;
683:
684: /* Check to see if we can get a socket */
685: if (HTNet_availableSockets() <= 0) {
686: if (!PendHost) PendHost = HTList_new();
687: if (CORE_TRACE)
688: HTTrace("Host info... Add Host %p as pending\n", host);
689: HTList_addObject(PendHost, host);
690: status = HT_PENDING;
691: }
692:
693: /* Add to either active or pending queue */
2.12.2.1 eric 694: if (_roomInPipe(host)) {
2.8 frystyk 695: if (CORE_TRACE) HTTrace("Host info... Add Net %p to pipeline of host %p\n", net, host);
696: if (!host->pipeline) host->pipeline = HTList_new();
697: HTList_addObject(host->pipeline, net);
2.12.2.8 eric 698: #if 0
2.8 frystyk 699: /*
700: ** We have been idle and must hence unregister our catch close
701: ** event handler
702: */
703: if (host->channel) {
2.12.2.1 eric 704: HTHost_unregister(host, net, HTEvent_CLOSE);
2.8 frystyk 705: }
2.12.2.8 eric 706: #endif
2.12.2.1 eric 707: /*
708: ** Send out the request if we're not blocked on write
709: */
710: if (!(host->registeredFor & HTEvent_BITS(HTEvent_WRITE)))
711: status = HTHost_launchPending(host) == TRUE ? HT_OK : HT_ERROR;
2.8 frystyk 712: } else {
713: if (CORE_TRACE) HTTrace("Host info... Add Net %p as pending\n", net);
714: if (!host->pending) host->pending = HTList_new();
715: HTList_addObject(host->pending, net);
716: status = HT_PENDING;
717: }
718: return status;
719: }
720: return HT_ERROR;
721: }
722:
2.12.2.1 eric 723: PUBLIC BOOL HTHost_free (HTHost * host, int status)
724: {
725: if (host->channel == NULL) return NO;
726: if (host->persistent) {
727: if (CORE_TRACE)
728: HTTrace("Host Object. keeping socket %d\n", HTChannel_socket(host->channel));
729: HTChannel_delete(host->channel, status);
730: } else {
731: if (CORE_TRACE)
732: HTTrace("Host Object. closing socket %d\n", HTChannel_socket(host->channel));
733:
734: /*
735: ** By lowering the semaphore we make sure that the channel
736: ** is gonna be deleted
737: */
2.12.2.3 eric 738: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
739: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
740: host->registeredFor = 0;
2.12.2.1 eric 741: HTChannel_downSemaphore(host->channel);
742: HTChannel_delete(host->channel, status);
743: host->channel = NULL;
744: }
745: return YES;
746: }
747:
2.8 frystyk 748: PUBLIC BOOL HTHost_deleteNet (HTHost * host, HTNet * net)
749: {
750: if (host && net) {
751: if (CORE_TRACE)
752: HTTrace("Host info... Remove Net %p from pipe line\n", net);
753: HTList_removeObject(host->pipeline, net);
754: HTList_removeObject(host->pending, net);
755: return YES;
756: }
757: return NO;
758: }
759:
760: /*
761: ** Handle pending host objects.
762: ** There are two ways we can end up with pending reqyests:
763: ** 1) If we are out of sockets then register new host objects as pending.
764: ** 2) If we are pending on a connection then register new net objects as
765: ** pending
766: ** This set of functions handles pending host objects and can start new
767: ** requests as resources get available
768: */
769:
770: /*
771: ** Check this host object for any pending requests and return the next
772: ** registered Net object.
773: */
774: PUBLIC HTNet * HTHost_nextPendingNet (HTHost * host)
775: {
776: HTNet * net = NULL;
777: if (host && host->pending && host->pipeline) {
2.11 kahan 778: /*JK 23/Sep/96 Bug correction. Associated the following lines to the
779: **above if. There was a missing pair of brackets.
780: */
781: if ((net = (HTNet *) HTList_removeFirstObject(host->pending)) != NULL) {
782: if (PROT_TRACE)
783: HTTrace("Host info... Popping %p from pending net queue\n",
784: net);
2.8 frystyk 785: HTList_addObject(host->pipeline, net);
2.11 kahan 786: }
2.8 frystyk 787: }
788: return net;
789: }
790:
791: /*
2.12.2.1 eric 792: ** Return the current list of pending host objects waiting for a socket
2.8 frystyk 793: */
794: PUBLIC HTHost * HTHost_nextPendingHost (void)
795: {
796: HTHost * host = NULL;
797: if (PendHost) {
798: if ((host = (HTHost *) HTList_removeFirstObject(PendHost)) != NULL)
799: if (PROT_TRACE)
800: HTTrace("Host info... Poping %p from pending host queue\n",
801: host);
802: }
803: return host;
804: }
805:
806: /*
807: ** Start the next pending request if any. First we look for pending
808: ** requests for the same host and then we check for any other pending
809: ** hosts
810: */
811: PUBLIC BOOL HTHost_launchPending (HTHost * host)
812: {
813: int available = HTNet_availableSockets();
814:
815: if (!host) {
816: if (PROT_TRACE) HTTrace("Host info... Bad arguments\n");
817: return NO;
818: }
819:
820: /*
821: ** Check if we do have resources available for a new request
822: ** This can either be reusing an existing connection or opening a new one
823: */
824: if (available > 0 || host->mode >= HT_TP_PIPELINE) {
825:
826: /*
2.12.2.7 frystyk 827: ** In pipeline we can only have one doing writing at a time.
828: ** We therefore check that there are no other Net object
829: ** registered for write
2.12.2.1 eric 830: */
2.12.2.7 frystyk 831: if (host->mode == HT_TP_PIPELINE) {
832: HTNet * last = (HTNet *) HTList_lastObject(host->pipeline);
833: if (last && last->registeredFor == HTEvent_WRITE)
834: return NO;
835: }
2.12.2.1 eric 836:
837: /*
838: ** Check the current Host object for pending Net objects
2.8 frystyk 839: */
840: if (host) {
841: HTNet * net = HTHost_nextPendingNet(host);
2.12.2.8 eric 842: if (net) return HTNet_execute(net, HTEvent_WRITE);
2.8 frystyk 843: }
844:
845: /*
846: ** Check for other pending Host objects
847: */
848: {
849: HTHost * pending = HTHost_nextPendingHost();
850: if (pending) {
851: HTNet * net = HTHost_nextPendingNet(pending);
2.12.2.8 eric 852: if (net) return HTNet_execute(net, HTEvent_WRITE);
2.8 frystyk 853: }
854: }
855: } else
2.12.2.1 eric 856: if (PROT_TRACE) HTTrace("Host info... No more requests.\n");
2.8 frystyk 857: return NO;
2.1 frystyk 858: }
2.11 kahan 859:
2.12.2.1 eric 860: PUBLIC HTNet * HTHost_firstNet (HTHost * host)
861: {
862: return (HTNet *) HTList_firstObject(host->pipeline);
863: }
864:
865: /*
866: ** The host event manager keeps track of the state of it's client engines
867: ** (typically HTTPEvent), accepting multiple blocks on read or write from
868: ** multiple pipelined engines. It then registers its own engine
869: ** (HostEvent) with the event manager.
870: */
2.12.2.4 frystyk 871: PUBLIC int HTHost_connect (HTHost * host, HTNet * net, char * url, HTProtocolId port)
2.12.2.1 eric 872: {
873: int status;
874: if (host && host->connecttime)
875: return HT_OK;
876: status = HTDoConnect(net, url, port);
877: if (status == HT_OK)
878: return HT_OK;
879: if (status == HT_WOULD_BLOCK || status == HT_PENDING)
880: return HT_WOULD_BLOCK;
881: return HT_ERROR; /* @@@ - some more deletion and stuff here? */
882: }
883:
884: /*
885: ** Rules: SINGLE: one element in pipe, either reading or writing
886: ** PIPE: n element in pipe, n-1 reading, 1 writing
887: */
2.12.2.3 eric 888: PUBLIC int HTHost_register (HTHost * host, HTNet * net, HTEventType type)
2.12.2.1 eric 889: {
2.12.2.7 frystyk 890: if (host && net) {
2.12.2.1 eric 891:
2.12.2.7 frystyk 892: /* net object may already be registered */
893: if (HTEvent_BITS(type) & net->registeredFor)
894: return NO;
895: net->registeredFor ^= HTEvent_BITS(type);
2.12.2.1 eric 896:
2.12.2.7 frystyk 897: /* host object may already be registered */
898: if (host->registeredFor & HTEvent_BITS(type))
899: return YES;
900: host->registeredFor ^= HTEvent_BITS(type);
901: return HTEvent_register(HTChannel_socket(host->channel), type, host->events+HTEvent_INDEX(type));
902: }
903: return NO;
2.12.2.1 eric 904: }
905:
2.12.2.3 eric 906: PUBLIC int HTHost_unregister (HTHost * host, HTNet * net, HTEventType type)
2.12.2.1 eric 907: {
2.12.2.7 frystyk 908: if (host && net) {
2.12.2.1 eric 909:
2.12.2.7 frystyk 910: /* net object may no be registered */
911: if (!(HTEvent_BITS(type) & net->registeredFor))
912: return NO;
913: net->registeredFor ^= HTEvent_BITS(type);
2.11 kahan 914:
2.12.2.7 frystyk 915: /* host object may no be registered */
916: if (!(host->registeredFor & HTEvent_BITS(type)))
917: return YES;
918: host->registeredFor ^= HTEvent_BITS(type);
919:
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: }
928: return NO;
2.12.2.1 eric 929: }
2.11 kahan 930:
2.12.2.1 eric 931: /*
932: ** The reader tells HostEvent that it's stream did not finish the data
933: */
934: PUBLIC BOOL HTHost_setRemainingRead (HTHost * host, size_t remaining)
935: {
936: if (host == NULL) return NO;
937: host->remainingRead = remaining;
938: return YES;
939: }
2.11 kahan 940:
2.12.2.1 eric 941: PUBLIC SockA * HTHost_getSockAddr (HTHost * host)
942: {
943: if (!host) return NULL;
944: return &host->sock_addr;
945: }
946:
947: PUBLIC BOOL HTHost_setHome (HTHost * host, int home)
948: {
949: if (!host) return NO;
950: host->home = home;
951: return YES;
952: }
953:
954: PUBLIC int HTHost_home (HTHost * host)
955: {
956: if (!host) return 0;
957: return host->home;
958: }
959:
960: #if 0 /* Is a macro right now */
961: PUBLIC BOOL HTHost_setDNS5 (HTHost * host, HTdns * dns)
962: {
963: if (!host) return NO;
964: host->dns = dns;
965: return YES;
966: }
967: #endif
968:
969: PUBLIC BOOL HTHost_setChannel (HTHost * host, HTChannel * channel)
970: {
971: if (!host) return NO;
972: host->channel = channel;
973: return YES;
974: }
975:
976: PUBLIC HTNet * HTHost_getReadNet(HTHost * host)
977: {
2.12.2.2 frystyk 978: if (host) {
2.12.2.7 frystyk 979: if (host->mode == HT_TP_INTERLEAVE) {
980: HTMuxChannel * muxch = HTMuxChannel_find(host);
981: return HTMuxChannel_net(muxch);
982: }
983: return (HTNet *) HTList_firstObject(host->pipeline);
2.12.2.2 frystyk 984: }
985: return NULL;
986: }
987:
988: PUBLIC HTNet * HTHost_getWriteNet(HTHost * host)
989: {
990: return host ? (HTNet *) HTList_lastObject(host->pipeline) : NULL;
2.12.2.1 eric 991: }
992:
993: /*
994: ** Create the input stream and bind it to the channel
995: ** Please read the description in the HTIOStream module for the parameters
996: */
2.12.2.2 frystyk 997: PUBLIC HTInputStream * HTHost_getInput (HTHost * host, HTTransport * tp,
998: void * param, int mode)
2.12.2.1 eric 999: {
2.12.2.2 frystyk 1000: if (host && host->channel && tp) {
2.12.2.1 eric 1001: HTChannel * ch = host->channel;
1002: HTInputStream * input = (*tp->input_new)(host, ch, param, mode);
1003: HTChannel_setInput(ch, input);
1004: return input;
1005: }
1006: if (CORE_TRACE) HTTrace("Host Object.. Can't create input stream\n");
2.12.2.2 frystyk 1007: return NULL;
1008: }
1009:
1010: PUBLIC HTOutputStream * HTHost_getOutput (HTHost * host, HTTransport * tp,
1011: void * param, int mode)
1012: {
1013: if (host && host->channel && tp) {
1014: HTChannel * ch = host->channel;
1015: HTOutputStream * output = (*tp->output_new)(host, ch, param, mode);
1016: HTChannel_setOutput(ch, output);
1017: return output;
1018: }
1019: if (CORE_TRACE) HTTrace("Host Object.. Can't create output stream\n");
1020: return NULL;
1021: }
1022:
1023: PUBLIC HTOutputStream * HTHost_output (HTHost * host, HTNet * net)
1024: {
1025: if (host && host->channel && net) {
1026: HTOutputStream * output = HTChannel_output(host->channel);
1027:
1028: /*
1029: ** If we are in MUX mode then create new output stream on top
1030: ** of the already existing one. Otherwise just return what we
1031: ** have.
1032: */
1033: if (host->mode == HT_TP_INTERLEAVE) {
2.12.2.4 frystyk 1034: HTStream * target = (HTStream *) HTChannel_output(host->channel);
1035: output = HTMuxWriter_new(host, net, target);
2.12.2.2 frystyk 1036: }
1037: return output;
1038: }
2.12.2.1 eric 1039: return NULL;
1040: }
1041:
2.12.2.8 eric 1042: PUBLIC int HTHost_read(HTHost * host, HTNet * net)
2.12.2.1 eric 1043: {
1044: HTInputStream * input = HTChannel_input(host->channel);
2.12.2.8 eric 1045: if (net != HTHost_getReadNet(host)) {
1046: HTHost_register(host, net, HTEvent_READ);
1047: return HT_WOULD_BLOCK;
1048: }
2.12.2.1 eric 1049: if (input == NULL) return HT_ERROR;
1050: return (*input->isa->read)(input);
1051: }
1052:
1053: PUBLIC BOOL HTHost_setConsumed(HTHost * host, size_t bytes)
1054: {
1055: HTInputStream * input;
1056: if (!host || !host->channel) return NO;
1057: if ((input = HTChannel_input(host->channel)) == NULL)
1058: return NO;
1059: return (*input->isa->consumed)(input, bytes);
1060: }
2.1 frystyk 1061:
2.12.2.7 frystyk 1062: PUBLIC int HTHost_hash (HTHost * host)
1063: {
1064: return host ? host->hash : -1;
1065: }
Webmaster