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