Annotation of libwww/Library/src/HTHost.c, revision 2.12.2.12
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.12! eric 6: ** @(#) $Id: HTHost.c,v 2.12.2.11 1996/11/18 05:00:55 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.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);
2.12.2.12! eric 646: if (CORE_TRACE) HTTrace("Net Object.. Resetting %p\n", net);
! 647: (*net->event.cbf)(HTChannel_socket(host->channel), net->event.param, HTEvent_RESET);
2.8 frystyk 648: HTList_appendObject(host->pending, net);
649: }
650: }
651: }
2.12.2.7 frystyk 652: if (PROT_TRACE)
653: HTTrace("Host info... New mode is %d for host %p\n", host->mode, host);
2.8 frystyk 654: host->mode = mode;
2.12.2.12! eric 655: return HTHost_launchPending(host);
! 656: #if 0
! 657: {
! 658: HTNet * net = (HTNet *)HTList_firstObject(host->pipeline);
! 659: if (net)
! 660: return HTNet_start(net);
! 661: return YES;
! 662: }
! 663: #endif
2.8 frystyk 664: }
665: return NO;
666: }
667:
668: /*
669: ** Check whether a host is idle meaning if it is ready for a new
670: ** request which depends on the mode of the host. If the host is
671: ** idle, i.e. ready for use then return YES else NO. If the host supports
672: ** persistent connections then still only return idle if no requests are
673: ** ongoing.
674: */
675: PUBLIC BOOL HTHost_isIdle (HTHost * host)
676: {
677: return (host && HTList_count(host->pipeline) <= 0);
678: }
679:
2.12.2.1 eric 680: PRIVATE BOOL _roomInPipe (HTHost * host)
681: {
682: int count;
683: if (!host) return NO;
684: count = HTList_count(host->pipeline);
2.12.2.12! eric 685: switch (host->mode) {
! 686: case HT_TP_SINGLE:
! 687: return count <= 0;
! 688: case HT_TP_PIPELINE:
! 689: return count < MAX_PIPES;
! 690: case HT_TP_INTERLEAVE:
! 691: return YES;
! 692: }
! 693: return NO;
2.12.2.1 eric 694: }
695:
2.8 frystyk 696: /*
697: ** Add a net object to the host object. If the host
698: ** is idle then add to active list (pipeline) else add
699: ** it to the pending list
700: ** Return HT_PENDING if we must pend, HT_OK, or HT_ERROR
701: */
702: PUBLIC int HTHost_addNet (HTHost * host, HTNet * net)
703: {
704: if (host && net) {
705: int status = HT_OK;
706:
707: /* Check to see if we can get a socket */
708: if (HTNet_availableSockets() <= 0) {
709: if (!PendHost) PendHost = HTList_new();
710: if (CORE_TRACE)
711: HTTrace("Host info... Add Host %p as pending\n", host);
712: HTList_addObject(PendHost, host);
713: status = HT_PENDING;
714: }
715:
716: /* Add to either active or pending queue */
2.12.2.1 eric 717: if (_roomInPipe(host)) {
2.8 frystyk 718: if (CORE_TRACE) HTTrace("Host info... Add Net %p to pipeline of host %p\n", net, host);
719: if (!host->pipeline) host->pipeline = HTList_new();
720: HTList_addObject(host->pipeline, net);
2.12.2.8 eric 721: #if 0
2.8 frystyk 722: /*
723: ** We have been idle and must hence unregister our catch close
724: ** event handler
725: */
726: if (host->channel) {
2.12.2.1 eric 727: HTHost_unregister(host, net, HTEvent_CLOSE);
2.8 frystyk 728: }
2.12.2.8 eric 729: #endif
2.12.2.1 eric 730: /*
731: ** Send out the request if we're not blocked on write
732: */
733: if (!(host->registeredFor & HTEvent_BITS(HTEvent_WRITE)))
2.12.2.12! eric 734: status = HTHost_launchPending(host) == YES ? HT_OK : HT_ERROR;
2.8 frystyk 735: } else {
736: if (CORE_TRACE) HTTrace("Host info... Add Net %p as pending\n", net);
737: if (!host->pending) host->pending = HTList_new();
738: HTList_addObject(host->pending, net);
739: status = HT_PENDING;
740: }
741: return status;
742: }
743: return HT_ERROR;
744: }
745:
2.12.2.1 eric 746: PUBLIC BOOL HTHost_free (HTHost * host, int status)
747: {
748: if (host->channel == NULL) return NO;
749: if (host->persistent) {
750: if (CORE_TRACE)
751: HTTrace("Host Object. keeping socket %d\n", HTChannel_socket(host->channel));
752: HTChannel_delete(host->channel, status);
753: } else {
754: if (CORE_TRACE)
755: HTTrace("Host Object. closing socket %d\n", HTChannel_socket(host->channel));
756:
757: /*
758: ** By lowering the semaphore we make sure that the channel
759: ** is gonna be deleted
760: */
2.12.2.3 eric 761: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
762: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
763: host->registeredFor = 0;
2.12.2.1 eric 764: HTChannel_downSemaphore(host->channel);
765: HTChannel_delete(host->channel, status);
766: host->channel = NULL;
767: }
768: return YES;
769: }
770:
2.8 frystyk 771: PUBLIC BOOL HTHost_deleteNet (HTHost * host, HTNet * net)
772: {
773: if (host && net) {
774: if (CORE_TRACE)
775: HTTrace("Host info... Remove Net %p from pipe line\n", net);
776: HTList_removeObject(host->pipeline, net);
777: HTList_removeObject(host->pending, net);
778: return YES;
779: }
780: return NO;
781: }
782:
783: /*
784: ** Handle pending host objects.
785: ** There are two ways we can end up with pending reqyests:
786: ** 1) If we are out of sockets then register new host objects as pending.
787: ** 2) If we are pending on a connection then register new net objects as
788: ** pending
789: ** This set of functions handles pending host objects and can start new
790: ** requests as resources get available
791: */
792:
793: /*
794: ** Check this host object for any pending requests and return the next
795: ** registered Net object.
796: */
797: PUBLIC HTNet * HTHost_nextPendingNet (HTHost * host)
798: {
799: HTNet * net = NULL;
800: if (host && host->pending && host->pipeline) {
2.11 kahan 801: /*JK 23/Sep/96 Bug correction. Associated the following lines to the
802: **above if. There was a missing pair of brackets.
803: */
804: if ((net = (HTNet *) HTList_removeFirstObject(host->pending)) != NULL) {
805: if (PROT_TRACE)
806: HTTrace("Host info... Popping %p from pending net queue\n",
807: net);
2.8 frystyk 808: HTList_addObject(host->pipeline, net);
2.11 kahan 809: }
2.8 frystyk 810: }
811: return net;
812: }
813:
814: /*
2.12.2.1 eric 815: ** Return the current list of pending host objects waiting for a socket
2.8 frystyk 816: */
817: PUBLIC HTHost * HTHost_nextPendingHost (void)
818: {
819: HTHost * host = NULL;
820: if (PendHost) {
821: if ((host = (HTHost *) HTList_removeFirstObject(PendHost)) != NULL)
822: if (PROT_TRACE)
823: HTTrace("Host info... Poping %p from pending host queue\n",
824: host);
825: }
826: return host;
827: }
828:
829: /*
830: ** Start the next pending request if any. First we look for pending
831: ** requests for the same host and then we check for any other pending
832: ** hosts
833: */
834: PUBLIC BOOL HTHost_launchPending (HTHost * host)
835: {
836: int available = HTNet_availableSockets();
837:
838: if (!host) {
839: if (PROT_TRACE) HTTrace("Host info... Bad arguments\n");
840: return NO;
841: }
842:
843: /*
844: ** Check if we do have resources available for a new request
845: ** This can either be reusing an existing connection or opening a new one
846: */
847: if (available > 0 || host->mode >= HT_TP_PIPELINE) {
848:
849: /*
2.12.2.7 frystyk 850: ** In pipeline we can only have one doing writing at a time.
851: ** We therefore check that there are no other Net object
852: ** registered for write
2.12.2.1 eric 853: */
2.12.2.7 frystyk 854: if (host->mode == HT_TP_PIPELINE) {
855: HTNet * last = (HTNet *) HTList_lastObject(host->pipeline);
856: if (last && last->registeredFor == HTEvent_WRITE)
857: return NO;
858: }
2.12.2.1 eric 859:
860: /*
861: ** Check the current Host object for pending Net objects
2.8 frystyk 862: */
2.12.2.12! eric 863: if (host && _roomInPipe(host)) {
2.8 frystyk 864: HTNet * net = HTHost_nextPendingNet(host);
2.12.2.8 eric 865: if (net) return HTNet_execute(net, HTEvent_WRITE);
2.8 frystyk 866: }
867:
868: /*
869: ** Check for other pending Host objects
870: */
871: {
872: HTHost * pending = HTHost_nextPendingHost();
873: if (pending) {
874: HTNet * net = HTHost_nextPendingNet(pending);
2.12.2.8 eric 875: if (net) return HTNet_execute(net, HTEvent_WRITE);
2.8 frystyk 876: }
877: }
878: } else
2.12.2.1 eric 879: if (PROT_TRACE) HTTrace("Host info... No more requests.\n");
2.8 frystyk 880: return NO;
2.1 frystyk 881: }
2.11 kahan 882:
2.12.2.1 eric 883: PUBLIC HTNet * HTHost_firstNet (HTHost * host)
884: {
885: return (HTNet *) HTList_firstObject(host->pipeline);
886: }
887:
888: /*
889: ** The host event manager keeps track of the state of it's client engines
890: ** (typically HTTPEvent), accepting multiple blocks on read or write from
891: ** multiple pipelined engines. It then registers its own engine
892: ** (HostEvent) with the event manager.
893: */
2.12.2.4 frystyk 894: PUBLIC int HTHost_connect (HTHost * host, HTNet * net, char * url, HTProtocolId port)
2.12.2.1 eric 895: {
896: int status;
897: if (host && host->connecttime)
898: return HT_OK;
899: status = HTDoConnect(net, url, port);
900: if (status == HT_OK)
901: return HT_OK;
902: if (status == HT_WOULD_BLOCK || status == HT_PENDING)
903: return HT_WOULD_BLOCK;
904: return HT_ERROR; /* @@@ - some more deletion and stuff here? */
905: }
906:
907: /*
908: ** Rules: SINGLE: one element in pipe, either reading or writing
909: ** PIPE: n element in pipe, n-1 reading, 1 writing
910: */
2.12.2.3 eric 911: PUBLIC int HTHost_register (HTHost * host, HTNet * net, HTEventType type)
2.12.2.1 eric 912: {
2.12.2.7 frystyk 913: if (host && net) {
2.12.2.1 eric 914:
2.12.2.7 frystyk 915: /* net object may already be registered */
916: if (HTEvent_BITS(type) & net->registeredFor)
917: return NO;
918: net->registeredFor ^= HTEvent_BITS(type);
2.12.2.1 eric 919:
2.12.2.7 frystyk 920: /* host object may already be registered */
921: if (host->registeredFor & HTEvent_BITS(type))
922: return YES;
923: host->registeredFor ^= HTEvent_BITS(type);
2.12.2.10 frystyk 924: return HTEvent_register(HTChannel_socket(host->channel), type, *(host->events+HTEvent_INDEX(type)));
2.12.2.7 frystyk 925: }
926: return NO;
2.12.2.1 eric 927: }
928:
2.12.2.3 eric 929: PUBLIC int HTHost_unregister (HTHost * host, HTNet * net, HTEventType type)
2.12.2.1 eric 930: {
2.12.2.7 frystyk 931: if (host && net) {
2.12.2.1 eric 932:
2.12.2.7 frystyk 933: /* net object may no be registered */
934: if (!(HTEvent_BITS(type) & net->registeredFor))
935: return NO;
936: net->registeredFor ^= HTEvent_BITS(type);
2.11 kahan 937:
2.12.2.7 frystyk 938: /* host object may no be registered */
939: if (!(host->registeredFor & HTEvent_BITS(type)))
940: return YES;
941: host->registeredFor ^= HTEvent_BITS(type);
942:
943: /* stay registered for READ to catch a socket close */
944: /* WRITE and CONNECT can be unregistered, though */
945: if ((type == HTEvent_WRITE && isLastInPipe(host, net)) ||
946: type == HTEvent_CONNECT)
947: /* if we are blocked downstream, shut down the whole pipe */
948: HTEvent_unregister(HTChannel_socket(host->channel), type);
949: return YES;
950: }
951: return NO;
2.12.2.1 eric 952: }
2.11 kahan 953:
2.12.2.1 eric 954: /*
955: ** The reader tells HostEvent that it's stream did not finish the data
956: */
957: PUBLIC BOOL HTHost_setRemainingRead (HTHost * host, size_t remaining)
958: {
959: if (host == NULL) return NO;
960: host->remainingRead = remaining;
961: return YES;
962: }
2.11 kahan 963:
2.12.2.1 eric 964: PUBLIC SockA * HTHost_getSockAddr (HTHost * host)
965: {
966: if (!host) return NULL;
967: return &host->sock_addr;
968: }
969:
970: PUBLIC BOOL HTHost_setHome (HTHost * host, int home)
971: {
972: if (!host) return NO;
973: host->home = home;
974: return YES;
975: }
976:
977: PUBLIC int HTHost_home (HTHost * host)
978: {
979: if (!host) return 0;
980: return host->home;
981: }
982:
983: #if 0 /* Is a macro right now */
984: PUBLIC BOOL HTHost_setDNS5 (HTHost * host, HTdns * dns)
985: {
986: if (!host) return NO;
987: host->dns = dns;
988: return YES;
989: }
990: #endif
991:
992: PUBLIC BOOL HTHost_setChannel (HTHost * host, HTChannel * channel)
993: {
994: if (!host) return NO;
995: host->channel = channel;
996: return YES;
997: }
998:
999: PUBLIC HTNet * HTHost_getReadNet(HTHost * host)
1000: {
2.12.2.2 frystyk 1001: if (host) {
2.12.2.7 frystyk 1002: if (host->mode == HT_TP_INTERLEAVE) {
1003: HTMuxChannel * muxch = HTMuxChannel_find(host);
1004: return HTMuxChannel_net(muxch);
1005: }
1006: return (HTNet *) HTList_firstObject(host->pipeline);
2.12.2.2 frystyk 1007: }
1008: return NULL;
1009: }
1010:
1011: PUBLIC HTNet * HTHost_getWriteNet(HTHost * host)
1012: {
1013: return host ? (HTNet *) HTList_lastObject(host->pipeline) : NULL;
2.12.2.1 eric 1014: }
1015:
1016: /*
1017: ** Create the input stream and bind it to the channel
1018: ** Please read the description in the HTIOStream module for the parameters
1019: */
2.12.2.2 frystyk 1020: PUBLIC HTInputStream * HTHost_getInput (HTHost * host, HTTransport * tp,
1021: void * param, int mode)
2.12.2.1 eric 1022: {
2.12.2.2 frystyk 1023: if (host && host->channel && tp) {
2.12.2.1 eric 1024: HTChannel * ch = host->channel;
1025: HTInputStream * input = (*tp->input_new)(host, ch, param, mode);
1026: HTChannel_setInput(ch, input);
2.12.2.12! eric 1027: return HTChannel_getChannelIStream(ch);
2.12.2.1 eric 1028: }
1029: if (CORE_TRACE) HTTrace("Host Object.. Can't create input stream\n");
2.12.2.2 frystyk 1030: return NULL;
1031: }
1032:
1033: PUBLIC HTOutputStream * HTHost_getOutput (HTHost * host, HTTransport * tp,
1034: void * param, int mode)
1035: {
1036: if (host && host->channel && tp) {
1037: HTChannel * ch = host->channel;
1038: HTOutputStream * output = (*tp->output_new)(host, ch, param, mode);
1039: HTChannel_setOutput(ch, output);
1040: return output;
1041: }
1042: if (CORE_TRACE) HTTrace("Host Object.. Can't create output stream\n");
1043: return NULL;
1044: }
1045:
1046: PUBLIC HTOutputStream * HTHost_output (HTHost * host, HTNet * net)
1047: {
1048: if (host && host->channel && net) {
1049: HTOutputStream * output = HTChannel_output(host->channel);
1050:
1051: /*
1052: ** If we are in MUX mode then create new output stream on top
1053: ** of the already existing one. Otherwise just return what we
1054: ** have.
1055: */
1056: if (host->mode == HT_TP_INTERLEAVE) {
2.12.2.4 frystyk 1057: HTStream * target = (HTStream *) HTChannel_output(host->channel);
1058: output = HTMuxWriter_new(host, net, target);
2.12.2.2 frystyk 1059: }
1060: return output;
1061: }
2.12.2.1 eric 1062: return NULL;
1063: }
1064:
2.12.2.8 eric 1065: PUBLIC int HTHost_read(HTHost * host, HTNet * net)
2.12.2.1 eric 1066: {
1067: HTInputStream * input = HTChannel_input(host->channel);
2.12.2.8 eric 1068: if (net != HTHost_getReadNet(host)) {
1069: HTHost_register(host, net, HTEvent_READ);
1070: return HT_WOULD_BLOCK;
1071: }
2.12.2.1 eric 1072: if (input == NULL) return HT_ERROR;
1073: return (*input->isa->read)(input);
1074: }
1075:
1076: PUBLIC BOOL HTHost_setConsumed(HTHost * host, size_t bytes)
1077: {
1078: HTInputStream * input;
1079: if (!host || !host->channel) return NO;
1080: if ((input = HTChannel_input(host->channel)) == NULL)
1081: return NO;
1082: return (*input->isa->consumed)(input, bytes);
1083: }
2.1 frystyk 1084:
2.12.2.7 frystyk 1085: PUBLIC int HTHost_hash (HTHost * host)
1086: {
1087: return host ? host->hash : -1;
1088: }
2.12.2.11 eric 1089:
1090: PUBLIC int HTHost_writeDelay(HTHost * host, int lastFlushTime, int buffSize)
1091: {
1092: if (host->forceWriteFlush)
1093: return 0;
1094: return 1000;
1095: }
1096:
1097: PUBLIC int HTHost_forceFlush(HTHost * host)
1098: {
1099: HTNet * targetNet = (HTNet *)HTList_lastObject(host->pipeline);
1100: int wasForced = host->forceWriteFlush;
1101: int ret;
1102: if (targetNet == NULL)
1103: return HT_ERROR;
1104: if (CORE_TRACE)
1105: HTWATCH(HTWatch_TCP, host, HTHIDE("HostEvent: FLUSH passed to %s.\n"),
1106: HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
1107: host->forceWriteFlush = YES;
1108: ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, HTEvent_FLUSH);
1109: host->forceWriteFlush = wasForced;
1110: return ret;
1111: }
1112:
1113: #if 0
1114: PRIVATE int lazyWriteFlushEvent (SOCKET soc, void * pVoid, HTEventType type)
1115: {
1116: HTOutputStream * stream = (HTOutputStream *) pVoid;
1117: HTBufferWriter_reallyFlush(me);
1118: return HT_OK;
1119: }
1120:
1121: /*
1122: ** HTHost_lazyFlush(host, cbf) - call cbf with an HTEvent_TIMEOUT
1123: ** when the host's write interval has expired.
1124: */
1125: PUBLIC int HTHost_lazyFlush (HTHost * host, int (*lazyFlush)(HTOutputStream *))
1126: {
1127: /*
1128: ** If we are allowed to delay the flush then register an event with the
1129: ** delay descibed by our delay variable. If we can't delay then flush
1130: ** right away.
1131: */
1132: if (host->delay_output) {
1133: HTChannel * ch = HTHost_channel(host);
1134: me->delay_event = HTEvent_new(FlushEvent, host, HT_PRIORITY_MAX, me->delay_ms);
1135: HTEvent_register(HTChannel_socket(ch), HTEvent_TIMEOUT, me->delay_event);
1136: me->delaying = YES;
1137: if (PROT_TRACE) HTTrace("Buffer...... Waiting...\n");
1138:
1139: int delay_ms; /* Delay in ms */
1140: BOOL delaying;
1141: HTEvent * delay_event;
1142: me->delay_ms = 10000;
1143:
1144: PUBLIC int HTHost_cancelLazyFlush(me->host)
1145: if (me->delay_event && me->delaying) {
1146: HTChannel * ch = HTHost_channel(me->host);
1147: HTEvent_unregister(HTChannel_socket(ch), HTEvent_TIMEOUT);
1148: me->delaying = NO;
1149: }
1150: #endif /* 0 */
1151:
Webmaster