Annotation of libwww/Library/src/HTHost.c, revision 2.15
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.15 ! eric 6: ** @(#) $Id: HTHost.c,v 2.14 1996/12/02 20:19:45 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.13 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.13 frystyk 24: #include "HTDNS.h"
25: #include "HTTPUtil.h"
26: #include "HTTCP.h"
2.1 frystyk 27: #include "HTHost.h" /* Implemented here */
2.13 frystyk 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.13 frystyk 32: #define MAX_PIPES 5 /* maximum number of pipelined requests */
2.1 frystyk 33:
2.13 frystyk 34: struct _HTInputStream {
35: const HTInputStreamClass * isa;
2.1 frystyk 36: };
37:
2.13 frystyk 38: PRIVATE int HostEvent(SOCKET soc, void * pVoid, HTEventType type);
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:
2.13 frystyk 47: PRIVATE int EventTimeout = -1; /* Global Host event timeout */
48:
2.1 frystyk 49: /* ------------------------------------------------------------------------- */
50:
51: PRIVATE void free_object (HTHost * me)
52: {
53: if (me) {
54: HT_FREE(me->hostname);
55: HT_FREE(me->type);
2.12 frystyk 56: HT_FREE(me->server);
57: HT_FREE(me->user_agent);
58: HT_FREE(me->range_units);
2.3 eric 59: if (me->channel) {
2.5 eric 60: HTChannel_delete(me->channel, HT_OK);
2.3 eric 61: me->channel = NULL;
62: }
2.13 frystyk 63: HTEvent_delete(me->events[0]);
64: HTEvent_delete(me->events[1]);
2.8 frystyk 65: HTList_delete(me->pipeline);
66: HTList_delete(me->pending);
2.1 frystyk 67: HT_FREE(me);
68: }
69: }
70:
71: PRIVATE BOOL delete_object (HTList * list, HTHost * me)
72: {
2.2 frystyk 73: if (CORE_TRACE) HTTrace("Host info... object %p from list %p\n", me, list);
2.1 frystyk 74: HTList_removeObject(list, (void *) me);
75: free_object(me);
76: return YES;
77: }
78:
2.13 frystyk 79: PRIVATE BOOL isLastInPipe (HTHost * host, HTNet * net)
80: {
81: return HTList_lastObject(host->pipeline) == net;
82: }
83:
84: /*
85: ** HostEvent - host event manager - recieves events from the event
86: ** manager and dispatches them to the client net objects by calling the
87: ** net object's cbf.
88: **
89: */
90: PRIVATE int HostEvent (SOCKET soc, void * pVoid, HTEventType type)
91: {
92: HTHost * host = (HTHost *)pVoid;
93:
94: if (type == HTEvent_READ) {
95: HTNet * targetNet;
96:
97: /* call the first net object */
98: do {
99: int ret;
100: targetNet = (HTNet *)HTList_firstObject(host->pipeline);
101: if (targetNet) {
102: if (CORE_TRACE)
2.14 frystyk 103: HTTrace(HTHIDE("Host Event.. READ passed to %s.\n"),
2.13 frystyk 104: HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
105: if ((ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel),
106: targetNet->event.param, type)) != HT_OK)
107: return ret;
108: }
109: if (targetNet == NULL && host->remainingRead > 0) {
110: HTTrace("HostEvent... Error: %d bytes left to read and nowhere to put them\n", host->remainingRead);
111: host->remainingRead = 0;
112: /*
113: ** Fall through to close the channel
114: */
115: }
116: /* call pipelined net object to eat all the data in the channel */
117: } while (host->remainingRead > 0);
118:
119: /* last target net should have set remainingRead to 0 */
120: if (targetNet)
121: return HT_OK;
122:
123: /* If there was notargetNet, it should be a close */
2.14 frystyk 124: HTTrace(HTHIDE("Host Event.. host %s closed connection.\n"),
2.13 frystyk 125: host->hostname);
126:
127: /* Is there garbage in the channel? Let's check: */
128: {
129: char buf[256];
130: int ret;
131: while ((ret = NETREAD(HTChannel_socket(host->channel), buf, sizeof(buf))) > 0)
132: HTTrace(HTHIDE("Host %s had %d extraneous bytes.\n"));
133: }
134: HTHost_clearChannel(host, HT_OK);
135: return HT_OK; /* extra garbage does not constitute an application error */
136:
137: } else if (type == HTEvent_WRITE) {
138: HTNet * targetNet = (HTNet *)HTList_lastObject(host->pipeline);
139: if (targetNet) {
140: if (CORE_TRACE)
2.14 frystyk 141: HTTrace(HTHIDE("Host Event.. WRITE passed to %s.\n"),
2.13 frystyk 142: HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
143: return (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, type);
144: }
2.14 frystyk 145: HTTrace(HTHIDE("Host Event.. Who wants to write to %s?\n"), host->hostname);
2.13 frystyk 146: return HT_ERROR;
2.14 frystyk 147: } else if (type == HTEvent_TIMEOUT) {
148:
149: if (CORE_TRACE)
150: HTTrace("Host Event.. WE SHOULD DELETE ALL REQUEST ON `%s\'?\n",
151: host->hostname);
2.13 frystyk 152:
2.14 frystyk 153: } else {
154: HTTrace(HTHIDE("Don't know how to handle OOB data from %s?\n"),
155: host->hostname);
156: }
2.13 frystyk 157: return HT_OK;
158: }
159:
2.1 frystyk 160: /*
161: ** Search the host info cache for a host object or create a new one
162: ** and add it. Examples of host names are
163: **
164: ** www.w3.org
165: ** www.foo.com:8000
166: ** 18.52.0.18
167: **
168: ** Returns Host object or NULL if error. You may get back an already
169: ** existing host object - you're not guaranteed a new one each time.
170: */
2.13 frystyk 171:
2.15 ! eric 172: PUBLIC HTHost * HTHost_new (char * host, u_short u_port)
2.1 frystyk 173: {
174: HTList * list = NULL; /* Current list in cache */
175: HTHost * pres = NULL;
2.13 frystyk 176: int hash = 0;
2.1 frystyk 177: if (!host) {
2.2 frystyk 178: if (CORE_TRACE) HTTrace("Host info... Bad argument\n");
2.1 frystyk 179: return NULL;
180: }
181:
182: /* Find a hash for this host */
183: {
184: char *ptr;
185: for (ptr=host; *ptr; ptr++)
2.13 frystyk 186: hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.1 frystyk 187: if (!HostTable) {
2.13 frystyk 188: if ((HostTable = (HTList **) HT_CALLOC(HOST_HASH_SIZE,
2.1 frystyk 189: sizeof(HTList *))) == NULL)
190: HT_OUTOFMEM("HTHost_find");
191: }
192: if (!HostTable[hash]) HostTable[hash] = HTList_new();
193: list = HostTable[hash];
194: }
195:
196: /* Search the cache */
197: {
198: HTList * cur = list;
199: while ((pres = (HTHost *) HTList_nextObject(cur))) {
2.15 ! eric 200: if (!strcmp(pres->hostname, host) && u_port == pres->u_port) {
2.8 frystyk 201: if (HTHost_isIdle(pres) && time(NULL)>pres->ntime+HostTimeout){
2.2 frystyk 202: if (CORE_TRACE)
2.1 frystyk 203: HTTrace("Host info... Collecting host info %p\n",pres);
204: delete_object(list, pres);
205: pres = NULL;
206: }
207: break;
208: }
209: }
210: }
211:
2.8 frystyk 212: /* If not found then create new Host object, else use existing one */
2.1 frystyk 213: if (pres) {
214: if (pres->channel) {
2.13 frystyk 215: if (pres->expires && pres->expires < time(NULL)) { /* Cached channel is cold */
2.2 frystyk 216: if (CORE_TRACE)
2.1 frystyk 217: HTTrace("Host info... Persistent channel %p gotten cold\n",
218: pres->channel);
2.5 eric 219: HTChannel_delete(pres->channel, HT_OK);
2.1 frystyk 220: pres->channel = NULL;
221: } else {
2.2 frystyk 222: if (CORE_TRACE)
2.1 frystyk 223: HTTrace("Host info... REUSING CHANNEL %p\n",pres->channel);
224: }
225: }
226: } else {
227: if ((pres = (HTHost *) HT_CALLOC(1, sizeof(HTHost))) == NULL)
228: HT_OUTOFMEM("HTHost_add");
2.13 frystyk 229: pres->hash = hash;
2.1 frystyk 230: StrAllocCopy(pres->hostname, host);
2.15 ! eric 231: pres->u_port = u_port;
2.1 frystyk 232: pres->ntime = time(NULL);
2.8 frystyk 233: pres->mode = HT_TP_SINGLE;
2.13 frystyk 234: pres->events[HTEvent_INDEX(HTEvent_READ)] = HTEvent_new(HostEvent, pres, HT_PRIORITY_MAX, -1);
235: pres->events[HTEvent_INDEX(HTEvent_WRITE)]= HTEvent_new(HostEvent, pres, HT_PRIORITY_MAX, -1);
2.2 frystyk 236: if (CORE_TRACE)
2.1 frystyk 237: HTTrace("Host info... added `%s\' to list %p\n", host, list);
238: HTList_addObject(list, (void *) pres);
239: }
240: return pres;
2.9 frystyk 241: }
242:
2.15 ! eric 243: PUBLIC HTHost * HTHost_newWParse (HTRequest * request, char * url, u_short u_port)
2.13 frystyk 244: {
245: char * port;
246: char * fullhost = NULL;
247: char * parsedHost = NULL;
248: SockA * sin;
249: HTHost * me;
250: char * proxy = HTRequest_proxy(request);
251:
252: fullhost = HTParse(proxy ? proxy : url, "", PARSE_HOST);
253:
254: /* If there's an @ then use the stuff after it as a hostname */
255: if (fullhost) {
256: char * at_sign;
257: if ((at_sign = strchr(fullhost, '@')) != NULL)
258: parsedHost = at_sign+1;
259: else
260: parsedHost = fullhost;
261: }
262: if (!parsedHost || !*parsedHost) {
263: HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_HOST,
264: NULL, 0, "HTDoConnect");
265: HT_FREE(fullhost);
266: return NULL;
267: }
268: port = strchr(parsedHost, ':');
269: if (PROT_TRACE)
270: HTTrace("HTDoConnect. Looking up `%s\'\n", parsedHost);
271: if (port) {
272: *port++ = '\0';
273: if (!*port || !isdigit(*port))
274: port = 0;
2.15 ! eric 275: u_port = atol(port);
2.13 frystyk 276: }
277: /* Find information about this host */
2.15 ! eric 278: if ((me = HTHost_new(parsedHost, u_port)) == NULL) {
2.13 frystyk 279: if (PROT_TRACE)HTTrace("HTDoConnect. Can't get host info\n");
280: me->tcpstate = TCP_ERROR;
281: return NULL;
282: }
283: sin = &me->sock_addr;
284: memset((void *) sin, '\0', sizeof(SockA));
285: #ifdef DECNET
286: sin->sdn_family = AF_DECnet;
287: net->sock_addr.sdn_objnum = port ? (unsigned char)(strtol(port, (char **) 0, 10)) : DNP_OBJ;
288: #else /* Internet */
289: sin->sin_family = AF_INET;
2.15 ! eric 290: sin->sin_port = htons(u_port);
2.13 frystyk 291: #endif
292: HT_FREE(fullhost); /* parsedHost points into fullhost */
293: return me;
294: }
295:
2.9 frystyk 296: /*
297: ** Search the host info cache for a host object. Examples of host names:
298: **
299: ** www.w3.org
300: ** www.foo.com:8000
301: ** 18.52.0.18
302: **
303: ** Returns Host object or NULL if not found.
304: */
305: PUBLIC HTHost * HTHost_find (char * host)
306: {
307: HTList * list = NULL; /* Current list in cache */
308: HTHost * pres = NULL;
309: if (CORE_TRACE)
310: HTTrace("Host info... Looking for `%s\'\n", host ? host : "<null>");
311:
312: /* Find a hash for this host */
313: if (host && HostTable) {
314: int hash = 0;
315: char *ptr;
316: for (ptr=host; *ptr; ptr++)
2.13 frystyk 317: hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.9 frystyk 318: if (!HostTable[hash]) return NULL;
319: list = HostTable[hash];
320:
321: /* Search the cache */
322: {
323: HTList * cur = list;
324: while ((pres = (HTHost *) HTList_nextObject(cur))) {
325: if (!strcmp(pres->hostname, host)) {
326: if (time(NULL) > pres->ntime + HostTimeout) {
327: if (CORE_TRACE)
328: HTTrace("Host info... Collecting host %p\n", pres);
329: delete_object(list, pres);
330: pres = NULL;
331: } else {
332: if (CORE_TRACE)
333: HTTrace("Host info... Found `%s\'\n", host);
334: }
335: return pres;
336: }
337: }
338: }
339: }
340: return NULL;
2.1 frystyk 341: }
342:
343: /*
2.8 frystyk 344: ** Get and set the hostname of the remote host
345: */
346: PUBLIC char * HTHost_name (HTHost * host)
347: {
348: return host ? host->hostname : NULL;
349: }
350:
351: /*
2.1 frystyk 352: ** Get and set the type class of the remote host
353: */
354: PUBLIC char * HTHost_class (HTHost * host)
355: {
356: return host ? host->type : NULL;
357: }
358:
359: PUBLIC void HTHost_setClass (HTHost * host, char * s_class)
360: {
361: if (host && s_class) StrAllocCopy(host->type, s_class);
362: }
363:
364: /*
365: ** Get and set the version of the remote host
366: */
367: PUBLIC int HTHost_version (HTHost *host)
368: {
369: return host ? host->version : 0;
370: }
371:
372: PUBLIC void HTHost_setVersion (HTHost * host, int version)
373: {
374: if (host) host->version = version;
375: }
376:
377: /*
378: ** Get and set the cache timeout for persistent entries.
379: ** The default value is TCP_TIMEOUT
380: */
381: PUBLIC void HTHost_setPersistTimeout (time_t timeout)
382: {
383: TCPTimeout = timeout;
384: }
385:
386: PUBLIC time_t HTHost_persistTimeout (time_t timeout)
387: {
388: return TCPTimeout;
389: }
390:
391: /* Persistent Connection Expiration
392: ** --------------------------------
393: ** Should normally not be used. If, then use calendar time.
394: */
395: PUBLIC void HTHost_setPersistExpires (HTHost * host, time_t expires)
396: {
397: if (host) host->expires = expires;
398: }
399:
400: PUBLIC time_t HTHost_persistExpires (HTHost * host)
401: {
402: return host ? host->expires : -1;
403: }
404:
405: /*
2.6 frystyk 406: ** Public methods for this host
407: */
408: PUBLIC HTMethod HTHost_publicMethods (HTHost * me)
409: {
410: return me ? me->methods : METHOD_INVALID;
411: }
412:
413: PUBLIC void HTHost_setPublicMethods (HTHost * me, HTMethod methodset)
414: {
415: if (me) me->methods = methodset;
416: }
417:
418: PUBLIC void HTHost_appendPublicMethods (HTHost * me, HTMethod methodset)
419: {
420: if (me) me->methods |= methodset;
421: }
422:
423: /*
424: ** Get and set the server name of the remote host
425: */
426: PUBLIC char * HTHost_server (HTHost * host)
427: {
428: return host ? host->server : NULL;
429: }
430:
431: PUBLIC BOOL HTHost_setServer (HTHost * host, const char * server)
432: {
433: if (host && server) {
434: StrAllocCopy(host->server, server);
435: return YES;
436: }
437: return NO;
438: }
439:
440: /*
441: ** Get and set the userAgent name of the remote host
442: */
443: PUBLIC char * HTHost_userAgent (HTHost * host)
444: {
445: return host ? host->user_agent : NULL;
446: }
447:
448: PUBLIC BOOL HTHost_setUserAgent (HTHost * host, const char * userAgent)
449: {
450: if (host && userAgent) {
451: StrAllocCopy(host->user_agent, userAgent);
452: return YES;
2.12 frystyk 453: }
454: return NO;
455: }
456:
457: /*
458: ** Get and set acceptable range units
459: */
460: PUBLIC char * HTHost_rangeUnits (HTHost * host)
461: {
462: return host ? host->range_units : NULL;
463: }
464:
465: PUBLIC BOOL HTHost_setRangeUnits (HTHost * host, const char * units)
466: {
467: if (host && units) {
468: StrAllocCopy(host->range_units, units);
469: return YES;
470: }
471: return NO;
472: }
473:
474: /*
475: ** Checks whether a specific range unit is OK. We always say
476: ** YES except if we have a specific statement from the server that
477: ** it doesn't understand byte ranges - that is - it has sent "none"
478: ** in a "Accept-Range" response header
479: */
480: PUBLIC BOOL HTHost_isRangeUnitAcceptable (HTHost * host, const char * unit)
481: {
482: if (host && unit) {
483: #if 0
484: if (host->range_units) {
485: char * start = strcasestr(host->range_units, "none");
486:
487: /*
488: ** Check that "none" is infact a token. It could be part of some
489: ** other valid string, so we'd better check for it.
490: */
491: if (start) {
492:
493:
494: }
495: return NO;
496: }
497: #endif
498: return strcasecomp(unit, "bytes") ? NO : YES;
2.6 frystyk 499: }
500: return NO;
501: }
502:
2.1 frystyk 503: /* HTHost_catchClose
504: ** -----------------
505: ** This function is registered when the socket is idle so that we get
506: ** a notification if the socket closes at the other end. At this point
507: ** we can't use the request object as it might have been freed a long
508: ** time ago.
509: */
2.13 frystyk 510: PUBLIC int HTHost_catchClose (SOCKET soc, void * context, HTEventType type)
2.1 frystyk 511: {
2.13 frystyk 512: HTNet * net = (HTNet *)context;
513: HTHost * host = net->host;
2.2 frystyk 514: if (CORE_TRACE)
2.13 frystyk 515: HTTrace("Catch Close. called with socket %d with type %x\n",
516: soc, type);
517: if (type == HTEvent_READ) {
2.1 frystyk 518: HTChannel * ch = HTChannel_find(soc); /* Find associated channel */
2.8 frystyk 519: HTHost * host = HTChannel_host(ch); /* and associated host */
2.1 frystyk 520: if (ch && host) {
2.2 frystyk 521: if (CORE_TRACE) HTTrace("Catch Close. CLOSING socket %d\n", soc);
2.8 frystyk 522: HTHost_clearChannel(host, HT_OK);
2.1 frystyk 523: } else {
2.2 frystyk 524: if (CORE_TRACE) HTTrace("Catch Close. socket %d NOT FOUND!\n",soc);
2.1 frystyk 525: }
526: }
2.13 frystyk 527: HTHost_unregister(host, net, HTEvent_CLOSE);
2.1 frystyk 528: return HT_OK;
529: }
530:
531: /*
532: ** As soon as we know that this host accepts persistent connections,
533: ** we associated the channel with the host.
534: ** We don't want more than MaxSockets-2 connections to be persistent in
535: ** order to avoid deadlock.
536: */
2.13 frystyk 537: PUBLIC BOOL HTHost_setPersistent (HTHost * host,
538: BOOL persistent,
539: HTTransportMode mode)
2.1 frystyk 540: {
2.13 frystyk 541: if (!host) return NO;
542:
543: if (!persistent) {
544: /*
545: ** We use the HT_IGNORE status code as we don't want to free
546: ** the stream at this point in time. The situation we want to
547: ** avoid is that we free the channel from within the stream pipe.
548: ** This will lead to an infinite look having the stream freing
549: ** itself.
550: */
551: return HTHost_clearChannel(host, HT_IGNORE);
552: }
553:
554: if (host->persistent) {
2.2 frystyk 555: if (CORE_TRACE) HTTrace("Host info... %p already persistent\n", host);
556: return YES;
2.13 frystyk 557: }
558:
559: {
560: SOCKET sockfd = HTChannel_socket(host->channel);
2.8 frystyk 561: if (sockfd != INVSOC && HTNet_availablePersistentSockets() > 0) {
2.13 frystyk 562: host->persistent = YES;
563: HTHost_setMode(host, mode);
2.1 frystyk 564: host->expires = time(NULL) + TCPTimeout; /* Default timeout */
2.13 frystyk 565: HTChannel_setHost(host->channel, host);
2.8 frystyk 566: HTNet_increasePersistentSocket();
2.2 frystyk 567: if (CORE_TRACE)
2.1 frystyk 568: HTTrace("Host info... added host %p as persistent\n", host);
569: return YES;
570: } else {
2.2 frystyk 571: if (CORE_TRACE)
572: HTTrace("Host info... no room for persistent socket %d\n",
2.7 frystyk 573: sockfd);
2.1 frystyk 574: }
575: }
576: return NO;
577: }
578:
579: /*
2.13 frystyk 580: ** Check whether we have a persistent channel or not
581: */
582: PUBLIC BOOL HTHost_isPersistent (HTHost * host)
583: {
584: return host && host->persistent;
585: }
586:
587: /*
2.1 frystyk 588: ** Find persistent channel associated with this host.
589: */
590: PUBLIC HTChannel * HTHost_channel (HTHost * host)
591: {
592: return host ? host->channel : NULL;
593: }
594:
595: /*
596: ** Clear the persistent entry by deleting the channel object. Note that
597: ** the channel object is only deleted if it's not used anymore.
598: */
2.8 frystyk 599: PUBLIC BOOL HTHost_clearChannel (HTHost * host, int status)
2.1 frystyk 600: {
601: if (host && host->channel) {
2.8 frystyk 602: HTChannel_setHost(host->channel, NULL);
2.10 frystyk 603:
2.13 frystyk 604: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
605: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
606:
2.10 frystyk 607: /*
608: ** We don't want to recursively delete ourselves so if we are
609: ** called from within the stream pipe then don't delete the channel
610: ** at this point
611: */
2.8 frystyk 612: HTChannel_delete(host->channel, status);
2.1 frystyk 613: host->expires = 0;
614: host->channel = NULL;
2.8 frystyk 615: HTNet_decreasePersistentSocket();
2.2 frystyk 616: if (CORE_TRACE)
617: HTTrace("Host info... removed host %p as persistent\n", host);
2.1 frystyk 618: return YES;
619: }
620: return NO;
621: }
622:
623: /*
2.8 frystyk 624: ** Handle the connection mode. The mode may change mode in the
625: ** middle of a connection.
626: */
627: PUBLIC HTTransportMode HTHost_mode (HTHost * host, BOOL * active)
628: {
629: return host ? host->mode : HT_TP_SINGLE;
630: }
631:
632: /*
633: ** If the new mode is lower than the old mode then adjust the pipeline
634: ** accordingly. That is, if we are going into single mode then move
635: ** all entries in the pipeline and move the rest to the pending
636: ** queue. They will get launched at a later point in time.
637: */
638: PUBLIC BOOL HTHost_setMode (HTHost * host, HTTransportMode mode)
639: {
640: if (host) {
641: /*
642: ** Check the new mode and see if we must adjust the queues.
643: */
644: if (mode == HT_TP_SINGLE && host->mode > mode) {
645: int piped = HTList_count(host->pipeline);
646: if (piped > 0) {
647: int cnt;
648: if (CORE_TRACE)
649: HTTrace("Host info... Moving %d Net objects from pipe line to pending queue\n", piped);
650: if (!host->pending) host->pending = HTList_new();
651: for (cnt=0; cnt<piped; cnt++) {
652: HTNet * net = HTList_removeFirstObject(host->pipeline);
2.13 frystyk 653: if (CORE_TRACE) HTTrace("Net Object.. Resetting %p\n", net);
654: (*net->event.cbf)(HTChannel_socket(host->channel), net->event.param, HTEvent_RESET);
2.8 frystyk 655: HTList_appendObject(host->pending, net);
656: }
657: }
658: }
2.13 frystyk 659: if (PROT_TRACE)
660: HTTrace("Host info... New mode is %d for host %p\n", host->mode, host);
2.8 frystyk 661: host->mode = mode;
2.13 frystyk 662: return HTHost_launchPending(host);
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.13 frystyk 679: PRIVATE BOOL _roomInPipe (HTHost * host)
680: {
681: int count;
682: if (!host) return NO;
683: count = HTList_count(host->pipeline);
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;
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.13 frystyk 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.13 frystyk 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.13 frystyk 726: HTHost_unregister(host, net, HTEvent_CLOSE);
2.8 frystyk 727: }
2.13 frystyk 728: #endif
729: /*
730: ** Send out the request if we're not blocked on write
731: */
732: if (!(host->registeredFor & HTEvent_BITS(HTEvent_WRITE)))
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.13 frystyk 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: */
760: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
761: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
762: host->registeredFor = 0;
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.13 frystyk 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.13 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
852: */
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: }
858:
859: /*
860: ** Check the current Host object for pending Net objects
2.8 frystyk 861: */
2.13 frystyk 862: if (host && _roomInPipe(host)) {
2.8 frystyk 863: HTNet * net = HTHost_nextPendingNet(host);
2.13 frystyk 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.13 frystyk 874: if (net) return HTNet_execute(net, HTEvent_WRITE);
2.8 frystyk 875: }
876: }
2.13 frystyk 877: } else
878: if (PROT_TRACE) HTTrace("Host info... No more requests.\n");
879: return NO;
880: }
881:
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: */
893: PUBLIC int HTHost_connect (HTHost * host, HTNet * net, char * url, HTProtocolId port)
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: */
910: PUBLIC int HTHost_register (HTHost * host, HTNet * net, HTEventType type)
911: {
912: if (host && net) {
913:
914: /* net object may already be registered */
915: if (HTEvent_BITS(type) & net->registeredFor)
916: return NO;
917: net->registeredFor ^= HTEvent_BITS(type);
918:
919: /* host object may already be registered */
920: if (host->registeredFor & HTEvent_BITS(type))
921: return YES;
922: host->registeredFor ^= HTEvent_BITS(type);
923: return HTEvent_register(HTChannel_socket(host->channel), type, *(host->events+HTEvent_INDEX(type)));
924: }
925: return NO;
926: }
927:
928: PUBLIC int HTHost_unregister (HTHost * host, HTNet * net, HTEventType type)
929: {
930: if (host && net) {
931:
932: /* net object may no be registered */
933: if (!(HTEvent_BITS(type) & net->registeredFor))
934: return NO;
935: net->registeredFor ^= HTEvent_BITS(type);
936:
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;
951: }
952:
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: }
962:
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: {
1000: if (host) {
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);
1006: }
1007: return NULL;
1008: }
1009:
1010: PUBLIC HTNet * HTHost_getWriteNet(HTHost * host)
1011: {
1012: return host ? (HTNet *) HTList_lastObject(host->pipeline) : NULL;
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: */
1019: PUBLIC HTInputStream * HTHost_getInput (HTHost * host, HTTransport * tp,
1020: void * param, int mode)
1021: {
1022: if (host && host->channel && tp) {
1023: HTChannel * ch = host->channel;
1024: HTInputStream * input = (*tp->input_new)(host, ch, param, mode);
1025: HTChannel_setInput(ch, input);
1026: return HTChannel_getChannelIStream(ch);
1027: }
1028: if (CORE_TRACE) HTTrace("Host Object.. Can't create input stream\n");
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);
2.8 frystyk 1049:
1050: /*
2.13 frystyk 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.
2.8 frystyk 1054: */
2.13 frystyk 1055: if (host->mode == HT_TP_INTERLEAVE) {
1056: HTStream * target = (HTStream *) HTChannel_output(host->channel);
1057: output = HTMuxWriter_new(host, net, target);
2.8 frystyk 1058: }
2.13 frystyk 1059: return output;
1060: }
1061: return NULL;
1062: }
1063:
1064: PUBLIC int HTHost_read(HTHost * host, HTNet * net)
1065: {
1066: HTInputStream * input = HTChannel_input(host->channel);
1067: if (net != HTHost_getReadNet(host)) {
1068: HTHost_register(host, net, HTEvent_READ);
1069: return HT_WOULD_BLOCK;
1070: }
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: }
1083:
1084: PUBLIC int HTHost_hash (HTHost * host)
1085: {
1086: return host ? host->hash : -1;
1087: }
1088:
2.14 frystyk 1089: PUBLIC int HTHost_writeDelay(HTHost * host, ms_t lastFlushTime, int buffSize)
2.13 frystyk 1090: {
2.15 ! eric 1091: unsigned short mtu;
! 1092: int ret;
! 1093: int socket = HTChannel_socket(host->channel);
! 1094: ret = ioctl(socket, 666, (unsigned long)&mtu);
! 1095: if ((ret == 0 && buffSize >= mtu) || host->forceWriteFlush)
2.13 frystyk 1096: return 0;
1097: return 1000;
1098: }
1099:
1100: PUBLIC int HTHost_forceFlush(HTHost * host)
1101: {
1102: HTNet * targetNet = (HTNet *)HTList_lastObject(host->pipeline);
1103: int wasForced = host->forceWriteFlush;
1104: int ret;
1105: if (targetNet == NULL)
1106: return HT_ERROR;
1107: if (CORE_TRACE)
2.14 frystyk 1108: HTTrace(HTHIDE("Host Event.. FLUSH passed to %s.\n"),
2.13 frystyk 1109: HTHIDE(HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet)))));
1110: host->forceWriteFlush = YES;
1111: ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, HTEvent_FLUSH);
1112: host->forceWriteFlush = wasForced;
1113: return ret;
2.1 frystyk 1114: }
2.11 kahan 1115:
2.13 frystyk 1116: PUBLIC int HTHost_eventTimeout (void)
1117: {
1118: return EventTimeout;
1119: }
2.11 kahan 1120:
2.13 frystyk 1121: PUBLIC void HTHost_setEventTimeout (int millis)
1122: {
1123: EventTimeout = millis;
1124: if (CORE_TRACE) HTTrace("Host........ Setting event timeout to %d ms\n", millis);
1125: }
2.11 kahan 1126:
2.13 frystyk 1127: #if 0
1128: PRIVATE int lazyWriteFlushEvent (SOCKET soc, void * pVoid, HTEventType type)
1129: {
1130: HTOutputStream * stream = (HTOutputStream *) pVoid;
1131: HTBufferWriter_reallyFlush(me);
1132: return HT_OK;
1133: }
2.11 kahan 1134:
2.13 frystyk 1135: /*
1136: ** HTHost_lazyFlush(host, cbf) - call cbf with an HTEvent_TIMEOUT
1137: ** when the host's write interval has expired.
1138: */
1139: PUBLIC int HTHost_lazyFlush (HTHost * host, int (*lazyFlush)(HTOutputStream *))
1140: {
1141: /*
1142: ** If we are allowed to delay the flush then register an event with the
1143: ** delay descibed by our delay variable. If we can't delay then flush
1144: ** right away.
1145: */
1146: if (host->delay_output) {
1147: HTChannel * ch = HTHost_channel(host);
1148: me->delay_event = HTEvent_new(FlushEvent, host, HT_PRIORITY_MAX, me->delay_ms);
1149: HTEvent_register(HTChannel_socket(ch), HTEvent_TIMEOUT, me->delay_event);
1150: me->delaying = YES;
1151: if (PROT_TRACE) HTTrace("Buffer...... Waiting...\n");
1152:
1153: int delay_ms; /* Delay in ms */
1154: BOOL delaying;
1155: HTEvent * delay_event;
1156: me->delay_ms = 10000;
1157:
1158: PUBLIC int HTHost_cancelLazyFlush(me->host)
1159: if (me->delay_event && me->delaying) {
1160: HTChannel * ch = HTHost_channel(me->host);
1161: HTEvent_unregister(HTChannel_socket(ch), HTEvent_TIMEOUT);
1162: me->delaying = NO;
1163: }
1164: #endif /* 0 */
2.1 frystyk 1165:
Webmaster