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