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