Annotation of libwww/Library/src/HTHost.c, revision 2.52
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.52 ! frystyk 6: ** @(#) $Id: HTHost.c,v 2.51 1998/10/20 13:17:19 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 */
2.44 frystyk 16: #include "wwwsys.h"
2.1 frystyk 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 "HTTPUtil.h"
24: #include "HTTCP.h"
2.1 frystyk 25: #include "HTHost.h" /* Implemented here */
2.13 frystyk 26: #include "HTHstMan.h"
2.1 frystyk 27:
2.48 frystyk 28: #define HOST_OBJECT_TTL 43200L /* Default host timeout is 12 h */
2.29 frystyk 29:
2.48 frystyk 30: #define TCP_IDLE_PASSIVE 120L /* Passive TTL in s on an idle connection */
31: #define TCP_IDLE_ACTIVE 60000L /* Active TTL in ms on an idle connection */
2.29 frystyk 32:
2.45 frystyk 33: #define MAX_PIPES 50 /* maximum number of pipelined requests */
2.50 frystyk 34: #define MAX_HOST_RECOVER 1 /* Max number of auto recovery */
2.32 frystyk 35: #define DEFAULT_DELAY 30 /* Default write flush delay in ms */
2.1 frystyk 36:
2.13 frystyk 37: struct _HTInputStream {
38: const HTInputStreamClass * isa;
2.1 frystyk 39: };
40:
2.13 frystyk 41: PRIVATE int HostEvent(SOCKET soc, void * pVoid, HTEventType type);
42:
43: /* Type definitions and global variables etc. local to this module */
2.48 frystyk 44: PRIVATE time_t HostTimeout = HOST_OBJECT_TTL; /* Timeout for host objects */
45: PRIVATE time_t TcpTtl = TCP_IDLE_PASSIVE; /* Passive TTL for TCP cons. */
2.1 frystyk 46:
2.8 frystyk 47: PRIVATE HTList ** HostTable = NULL;
48: PRIVATE HTList * PendHost = NULL; /* List of pending host elements */
2.1 frystyk 49:
2.40 kahan 50: /* JK: New functions for interruption the automatic pending request
51: activation */
52: PRIVATE HTHost_ActivateRequestCallback * ActivateReqCBF = NULL;
53: PRIVATE int HTHost_ActivateRequest (HTNet *net);
54: PRIVATE BOOL DoPendingReqLaunch = YES; /* controls automatic activation
55: of pending requests */
56:
2.13 frystyk 57: PRIVATE int EventTimeout = -1; /* Global Host event timeout */
58:
2.36 frystyk 59: PRIVATE ms_t WriteDelay = DEFAULT_DELAY; /* Delay in ms */
2.26 frystyk 60:
2.45 frystyk 61: PRIVATE int MaxPipelinedRequests = MAX_PIPES;
62:
2.1 frystyk 63: /* ------------------------------------------------------------------------- */
64:
65: PRIVATE void free_object (HTHost * me)
66: {
67: if (me) {
2.30 frystyk 68: int i;
2.1 frystyk 69: HT_FREE(me->hostname);
70: HT_FREE(me->type);
2.12 frystyk 71: HT_FREE(me->server);
72: HT_FREE(me->user_agent);
73: HT_FREE(me->range_units);
2.30 frystyk 74:
75: /* Delete the channel (if any) */
2.3 eric 76: if (me->channel) {
2.5 eric 77: HTChannel_delete(me->channel, HT_OK);
2.3 eric 78: me->channel = NULL;
79: }
2.30 frystyk 80:
81: /* Unregister the events */
2.18 eric 82: for (i = 0; i < HTEvent_TYPES; i++)
83: HTEvent_delete(me->events[i]);
2.30 frystyk 84:
85: /* Delete the timer (if any) */
86: if (me->timer) HTTimer_delete(me->timer);
87:
88: /* Delete the queues */
2.8 frystyk 89: HTList_delete(me->pipeline);
90: HTList_delete(me->pending);
2.1 frystyk 91: HT_FREE(me);
92: }
93: }
94:
95: PRIVATE BOOL delete_object (HTList * list, HTHost * me)
96: {
2.2 frystyk 97: if (CORE_TRACE) HTTrace("Host info... object %p from list %p\n", me, list);
2.1 frystyk 98: HTList_removeObject(list, (void *) me);
99: free_object(me);
100: return YES;
101: }
102:
2.13 frystyk 103: PRIVATE BOOL isLastInPipe (HTHost * host, HTNet * net)
104: {
105: return HTList_lastObject(host->pipeline) == net;
106: }
107:
2.51 frystyk 108: PRIVATE BOOL killPipeline (HTHost * host, HTEventType type)
2.48 frystyk 109: {
110: if (host) {
111: int piped = HTList_count(host->pipeline);
112: int pending = HTList_count(host->pending);
113: int cnt;
114:
2.51 frystyk 115: if (CORE_TRACE)
116: HTTrace("Host kill... Pipeline due to %s event\n", HTEvent_type2str(type));
117:
2.48 frystyk 118: /* Terminate all net objects in pending queue */
119: for (cnt=0; cnt<pending; cnt++) {
120: HTNet * net = HTList_removeLastObject(host->pending);
2.51 frystyk 121: if (CORE_TRACE) HTTrace("Host kill... Terminating net object %p from pending queue\n", net);
2.48 frystyk 122: net->registeredFor = 0;
2.51 frystyk 123: (*net->event.cbf)(HTChannel_socket(host->channel), net->event.param, type);
2.48 frystyk 124: }
125:
126: /* Terminate all net objects in pipeline */
127: if (piped >= 1) {
128:
2.51 frystyk 129: #if 0
2.48 frystyk 130: /*
131: ** Unregister this host for all events
132: */
133: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
134: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
135: host->registeredFor = 0;
136:
137: /*
138: ** Set new mode to single until we know what is going on
139: */
140: host->mode = HT_TP_SINGLE;
2.51 frystyk 141: #endif
2.48 frystyk 142: /*
143: ** Terminte all net objects in the pipeline
144: */
145: for (cnt=0; cnt<piped; cnt++) {
146: HTNet * net = HTList_firstObject(host->pipeline);
2.51 frystyk 147: if (CORE_TRACE) HTTrace("Host kill... Terminating net object %p from pipe line\n", net);
2.48 frystyk 148: net->registeredFor = 0;
2.51 frystyk 149: (*net->event.cbf)(HTChannel_socket(host->channel), net->event.param, type);
2.48 frystyk 150: }
151:
2.51 frystyk 152: #if 0
2.48 frystyk 153: HTChannel_setSemaphore(host->channel, 0);
154: HTHost_clearChannel(host, HT_INTERRUPTED);
2.51 frystyk 155: #endif
2.48 frystyk 156:
157: }
158: return YES;
159: }
160: return NO;
161: }
162:
163: /*
2.51 frystyk 164: ** Silently close an idle persistent connection after
165: ** TCP_IDLE_ACTIVE secs
166: */
167: PRIVATE int IdleTimeoutEvent (HTTimer * timer, void * param, HTEventType type)
168: {
169: HTHost * host = (HTHost *) param;
170: SOCKET sockfd = HTChannel_socket(host->channel);
171: int result = HostEvent (sockfd, host, HTEvent_CLOSE);
172: HTTimer_delete(timer);
173: host->timer = NULL;
174: return result;
175: }
176:
177: /*
2.13 frystyk 178: ** HostEvent - host event manager - recieves events from the event
179: ** manager and dispatches them to the client net objects by calling the
180: ** net object's cbf.
181: **
182: */
183: PRIVATE int HostEvent (SOCKET soc, void * pVoid, HTEventType type)
184: {
185: HTHost * host = (HTHost *)pVoid;
186:
2.18 eric 187: if (type == HTEvent_READ || type == HTEvent_CLOSE) {
2.13 frystyk 188: HTNet * targetNet;
189:
190: /* call the first net object */
191: do {
192: int ret;
2.49 frystyk 193:
194: /* netscape and apache servers can do a lazy close well after usage
195: * of previous socket has been dispensed by the library,
196: * the section below makes sure the event does not get miss attributed
197: */
198: if (HTChannel_socket(host->channel) != soc) {
199: if (CORE_TRACE)
200: HTTrace("Host Event.. wild socket %d type = %s real socket is %d\n", soc,
201: type == HTEvent_CLOSE ? "Event_Close" : "Event_Read",
202: HTChannel_socket(host->channel));
203: return HT_OK;
204: }
205:
2.13 frystyk 206: targetNet = (HTNet *)HTList_firstObject(host->pipeline);
207: if (targetNet) {
208: if (CORE_TRACE)
2.28 frystyk 209: HTTrace("Host Event.. READ passed to `%s\'\n",
210: HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet))));
2.13 frystyk 211: if ((ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel),
212: targetNet->event.param, type)) != HT_OK)
213: return ret;
214: }
215: if (targetNet == NULL && host->remainingRead > 0) {
2.31 frystyk 216: if (CORE_TRACE)
217: HTTrace("HostEvent... Error: %d bytes left to read and nowhere to put them\n",
218: host->remainingRead);
2.13 frystyk 219: host->remainingRead = 0;
220: /*
221: ** Fall through to close the channel
222: */
223: }
224: /* call pipelined net object to eat all the data in the channel */
225: } while (host->remainingRead > 0);
226:
227: /* last target net should have set remainingRead to 0 */
228: if (targetNet)
229: return HT_OK;
230:
231: /* If there was notargetNet, it should be a close */
2.28 frystyk 232: if (CORE_TRACE)
233: HTTrace("Host Event.. host %p `%s\' closed connection.\n",
234: host, host->hostname);
2.13 frystyk 235:
236: /* Is there garbage in the channel? Let's check: */
237: {
238: char buf[256];
239: int ret;
2.28 frystyk 240: memset(buf, '\0', sizeof(buf));
2.48 frystyk 241: while ((ret = NETREAD(HTChannel_socket(host->channel), buf, sizeof(buf)-1)) > 0) {
2.28 frystyk 242: if (CORE_TRACE)
243: HTTrace("Host Event.. Host %p `%s\' had %d extraneous bytes: `%s\'\n",
244: host, host->hostname, ret, buf);
245: memset(buf, '\0', sizeof(buf));
246: }
2.13 frystyk 247: }
248: HTHost_clearChannel(host, HT_OK);
2.28 frystyk 249: return HT_OK; /* extra garbage does not constitute an application error */
2.13 frystyk 250:
2.18 eric 251: } else if (type == HTEvent_WRITE || type == HTEvent_CONNECT) {
2.13 frystyk 252: HTNet * targetNet = (HTNet *)HTList_lastObject(host->pipeline);
253: if (targetNet) {
254: if (CORE_TRACE)
2.28 frystyk 255: HTTrace("Host Event.. WRITE passed to `%s\'\n",
256: HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet))));
2.13 frystyk 257: return (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, type);
258: }
2.44 frystyk 259: HTTrace("Host Event.. Who wants to write to `%s\'?\n", host->hostname);
2.13 frystyk 260: return HT_ERROR;
2.14 frystyk 261: } else if (type == HTEvent_TIMEOUT) {
2.51 frystyk 262: killPipeline(host, HTEvent_TIMEOUT);
2.14 frystyk 263: } else {
2.44 frystyk 264: HTTrace("Don't know how to handle OOB data from `%s\'?\n",
265: host->hostname);
2.14 frystyk 266: }
2.13 frystyk 267: return HT_OK;
268: }
269:
2.1 frystyk 270: /*
271: ** Search the host info cache for a host object or create a new one
272: ** and add it. Examples of host names are
273: **
274: ** www.w3.org
275: ** www.foo.com:8000
276: ** 18.52.0.18
277: **
278: ** Returns Host object or NULL if error. You may get back an already
279: ** existing host object - you're not guaranteed a new one each time.
280: */
2.15 eric 281: PUBLIC HTHost * HTHost_new (char * host, u_short u_port)
2.1 frystyk 282: {
283: HTList * list = NULL; /* Current list in cache */
284: HTHost * pres = NULL;
2.13 frystyk 285: int hash = 0;
2.1 frystyk 286: if (!host) {
2.2 frystyk 287: if (CORE_TRACE) HTTrace("Host info... Bad argument\n");
2.1 frystyk 288: return NULL;
289: }
290:
291: /* Find a hash for this host */
292: {
293: char *ptr;
294: for (ptr=host; *ptr; ptr++)
2.13 frystyk 295: hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.1 frystyk 296: if (!HostTable) {
2.13 frystyk 297: if ((HostTable = (HTList **) HT_CALLOC(HOST_HASH_SIZE,
2.1 frystyk 298: sizeof(HTList *))) == NULL)
299: HT_OUTOFMEM("HTHost_find");
300: }
301: if (!HostTable[hash]) HostTable[hash] = HTList_new();
302: list = HostTable[hash];
303: }
304:
305: /* Search the cache */
306: {
307: HTList * cur = list;
308: while ((pres = (HTHost *) HTList_nextObject(cur))) {
2.15 eric 309: if (!strcmp(pres->hostname, host) && u_port == pres->u_port) {
2.8 frystyk 310: if (HTHost_isIdle(pres) && time(NULL)>pres->ntime+HostTimeout){
2.2 frystyk 311: if (CORE_TRACE)
2.1 frystyk 312: HTTrace("Host info... Collecting host info %p\n",pres);
313: delete_object(list, pres);
314: pres = NULL;
315: }
316: break;
317: }
318: }
319: }
320:
2.8 frystyk 321: /* If not found then create new Host object, else use existing one */
2.1 frystyk 322: if (pres) {
323: if (pres->channel) {
2.32 frystyk 324:
325: /*
326: ** If we have a TTL for this TCP connection then
327: ** check that we haven't passed it.
328: */
329: if (pres->expires > 0) {
330: time_t t = time(NULL);
2.51 frystyk 331: if (HTHost_isIdle(pres) && pres->expires < t) {
2.32 frystyk 332: if (CORE_TRACE)
333: HTTrace("Host info... Persistent channel %p gotten cold\n",
334: pres->channel);
2.48 frystyk 335: HTHost_clearChannel(pres, HT_OK);
2.32 frystyk 336: } else {
337: pres->expires = t + TcpTtl;
338: if (CORE_TRACE)
339: HTTrace("Host info... REUSING CHANNEL %p\n",pres->channel);
340: }
341: }
2.1 frystyk 342: }
343: } else {
344: if ((pres = (HTHost *) HT_CALLOC(1, sizeof(HTHost))) == NULL)
345: HT_OUTOFMEM("HTHost_add");
2.13 frystyk 346: pres->hash = hash;
2.1 frystyk 347: StrAllocCopy(pres->hostname, host);
2.15 eric 348: pres->u_port = u_port;
2.1 frystyk 349: pres->ntime = time(NULL);
2.8 frystyk 350: pres->mode = HT_TP_SINGLE;
2.26 frystyk 351: pres->delay = WriteDelay;
2.18 eric 352: {
2.26 frystyk 353: int i;
354: for (i = 0; i < HTEvent_TYPES; i++)
355: pres->events[i]= HTEvent_new(HostEvent, pres, HT_PRIORITY_MAX, EventTimeout);
2.18 eric 356: }
2.2 frystyk 357: if (CORE_TRACE)
2.24 frystyk 358: HTTrace("Host info... added `%s\' with host %p to list %p\n",
359: host, pres, list);
2.1 frystyk 360: HTList_addObject(list, (void *) pres);
361: }
362: return pres;
2.9 frystyk 363: }
364:
2.15 eric 365: PUBLIC HTHost * HTHost_newWParse (HTRequest * request, char * url, u_short u_port)
2.13 frystyk 366: {
2.32 frystyk 367: char * port;
368: char * fullhost = NULL;
369: char * parsedHost = NULL;
370: SockA * sin;
371: HTHost * me;
372: char * proxy = HTRequest_proxy(request);
2.13 frystyk 373:
2.32 frystyk 374: fullhost = HTParse(proxy ? proxy : url, "", PARSE_HOST);
2.13 frystyk 375:
376: /* If there's an @ then use the stuff after it as a hostname */
2.32 frystyk 377: if (fullhost) {
378: char * at_sign;
379: if ((at_sign = strchr(fullhost, '@')) != NULL)
380: parsedHost = at_sign+1;
381: else
382: parsedHost = fullhost;
383: }
384: if (!parsedHost || !*parsedHost) {
385: HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_HOST,
2.52 ! frystyk 386: NULL, 0, "HTHost_newWParse");
2.32 frystyk 387: HT_FREE(fullhost);
388: return NULL;
389: }
390: port = strchr(parsedHost, ':');
391: if (PROT_TRACE)
2.52 ! frystyk 392: HTTrace("HTHost parse Looking up `%s\'\n", parsedHost);
2.32 frystyk 393: if (port) {
394: *port++ = '\0';
2.36 frystyk 395: if (!*port || !isdigit((int) *port))
2.32 frystyk 396: port = 0;
397: u_port = (u_short) atol(port);
398: }
399: /* Find information about this host */
400: if ((me = HTHost_new(parsedHost, u_port)) == NULL) {
2.52 ! frystyk 401: if (PROT_TRACE)HTTrace("HTHost parse Can't get host info\n");
2.32 frystyk 402: me->tcpstate = TCP_ERROR;
403: return NULL;
404: }
405: sin = &me->sock_addr;
406: memset((void *) sin, '\0', sizeof(SockA));
2.13 frystyk 407: #ifdef DECNET
2.32 frystyk 408: sin->sdn_family = AF_DECnet;
409: net->sock_addr.sdn_objnum = port ? (unsigned char)(strtol(port, (char **) 0, 10)) : DNP_OBJ;
2.13 frystyk 410: #else /* Internet */
2.32 frystyk 411: sin->sin_family = AF_INET;
412: sin->sin_port = htons(u_port);
2.13 frystyk 413: #endif
2.32 frystyk 414: HT_FREE(fullhost); /* parsedHost points into fullhost */
415: return me;
2.13 frystyk 416: }
417:
2.9 frystyk 418: /*
419: ** Search the host info cache for a host object. Examples of host names:
420: **
421: ** www.w3.org
422: ** www.foo.com:8000
423: ** 18.52.0.18
424: **
425: ** Returns Host object or NULL if not found.
426: */
427: PUBLIC HTHost * HTHost_find (char * host)
428: {
429: HTList * list = NULL; /* Current list in cache */
430: HTHost * pres = NULL;
431: if (CORE_TRACE)
432: HTTrace("Host info... Looking for `%s\'\n", host ? host : "<null>");
433:
434: /* Find a hash for this host */
435: if (host && HostTable) {
436: int hash = 0;
437: char *ptr;
438: for (ptr=host; *ptr; ptr++)
2.13 frystyk 439: hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HOST_HASH_SIZE);
2.9 frystyk 440: if (!HostTable[hash]) return NULL;
441: list = HostTable[hash];
442:
443: /* Search the cache */
444: {
445: HTList * cur = list;
446: while ((pres = (HTHost *) HTList_nextObject(cur))) {
447: if (!strcmp(pres->hostname, host)) {
448: if (time(NULL) > pres->ntime + HostTimeout) {
449: if (CORE_TRACE)
450: HTTrace("Host info... Collecting host %p\n", pres);
451: delete_object(list, pres);
452: pres = NULL;
453: } else {
454: if (CORE_TRACE)
455: HTTrace("Host info... Found `%s\'\n", host);
456: }
457: return pres;
458: }
459: }
460: }
461: }
462: return NULL;
2.1 frystyk 463: }
464:
465: /*
2.8 frystyk 466: ** Get and set the hostname of the remote host
467: */
468: PUBLIC char * HTHost_name (HTHost * host)
469: {
470: return host ? host->hostname : NULL;
471: }
472:
473: /*
2.1 frystyk 474: ** Get and set the type class of the remote host
475: */
476: PUBLIC char * HTHost_class (HTHost * host)
477: {
478: return host ? host->type : NULL;
479: }
480:
481: PUBLIC void HTHost_setClass (HTHost * host, char * s_class)
482: {
483: if (host && s_class) StrAllocCopy(host->type, s_class);
484: }
485:
486: /*
487: ** Get and set the version of the remote host
488: */
489: PUBLIC int HTHost_version (HTHost *host)
490: {
491: return host ? host->version : 0;
492: }
493:
494: PUBLIC void HTHost_setVersion (HTHost * host, int version)
495: {
496: if (host) host->version = version;
497: }
498:
499: /*
2.44 frystyk 500: ** Get and set the cache timeout for persistent entries.
2.1 frystyk 501: ** The default value is TCP_TIMEOUT
502: */
503: PUBLIC void HTHost_setPersistTimeout (time_t timeout)
504: {
2.32 frystyk 505: TcpTtl = timeout;
2.1 frystyk 506: }
507:
508: PUBLIC time_t HTHost_persistTimeout (time_t timeout)
509: {
2.32 frystyk 510: return TcpTtl;
2.1 frystyk 511: }
512:
513: /* Persistent Connection Expiration
514: ** --------------------------------
515: ** Should normally not be used. If, then use calendar time.
516: */
517: PUBLIC void HTHost_setPersistExpires (HTHost * host, time_t expires)
518: {
519: if (host) host->expires = expires;
520: }
521:
522: PUBLIC time_t HTHost_persistExpires (HTHost * host)
523: {
524: return host ? host->expires : -1;
525: }
526:
2.22 eric 527: PUBLIC void HTHost_setReqsPerConnection (HTHost * host, int reqs)
528: {
529: if (host) host->reqsPerConnection = reqs;
530: }
531:
532: PUBLIC int HTHost_reqsPerConnection (HTHost * host)
533: {
534: return host ? host->reqsPerConnection : -1;
535: }
536:
537: PUBLIC void HTHost_setReqsMade (HTHost * host, int reqs)
538: {
539: if (host) host->reqsMade = reqs;
540: }
541:
542: PUBLIC int HTHost_reqsMade (HTHost * host)
543: {
544: return host ? host->reqsMade : -1;
545: }
546:
2.1 frystyk 547: /*
2.6 frystyk 548: ** Public methods for this host
549: */
550: PUBLIC HTMethod HTHost_publicMethods (HTHost * me)
551: {
552: return me ? me->methods : METHOD_INVALID;
553: }
554:
555: PUBLIC void HTHost_setPublicMethods (HTHost * me, HTMethod methodset)
556: {
557: if (me) me->methods = methodset;
558: }
559:
560: PUBLIC void HTHost_appendPublicMethods (HTHost * me, HTMethod methodset)
561: {
562: if (me) me->methods |= methodset;
563: }
564:
565: /*
566: ** Get and set the server name of the remote host
567: */
568: PUBLIC char * HTHost_server (HTHost * host)
569: {
570: return host ? host->server : NULL;
571: }
572:
573: PUBLIC BOOL HTHost_setServer (HTHost * host, const char * server)
574: {
575: if (host && server) {
576: StrAllocCopy(host->server, server);
577: return YES;
578: }
579: return NO;
580: }
581:
582: /*
583: ** Get and set the userAgent name of the remote host
584: */
585: PUBLIC char * HTHost_userAgent (HTHost * host)
586: {
587: return host ? host->user_agent : NULL;
588: }
589:
590: PUBLIC BOOL HTHost_setUserAgent (HTHost * host, const char * userAgent)
591: {
592: if (host && userAgent) {
593: StrAllocCopy(host->user_agent, userAgent);
594: return YES;
2.12 frystyk 595: }
596: return NO;
597: }
598:
599: /*
600: ** Get and set acceptable range units
601: */
602: PUBLIC char * HTHost_rangeUnits (HTHost * host)
603: {
604: return host ? host->range_units : NULL;
605: }
606:
607: PUBLIC BOOL HTHost_setRangeUnits (HTHost * host, const char * units)
608: {
609: if (host && units) {
610: StrAllocCopy(host->range_units, units);
611: return YES;
612: }
613: return NO;
614: }
615:
616: /*
617: ** Checks whether a specific range unit is OK. We always say
618: ** YES except if we have a specific statement from the server that
619: ** it doesn't understand byte ranges - that is - it has sent "none"
620: ** in a "Accept-Range" response header
621: */
622: PUBLIC BOOL HTHost_isRangeUnitAcceptable (HTHost * host, const char * unit)
623: {
624: if (host && unit) {
625: #if 0
626: if (host->range_units) {
627: char * start = strcasestr(host->range_units, "none");
628:
629: /*
630: ** Check that "none" is infact a token. It could be part of some
631: ** other valid string, so we'd better check for it.
632: */
633: if (start) {
634:
635:
636: }
637: return NO;
638: }
639: #endif
640: return strcasecomp(unit, "bytes") ? NO : YES;
2.6 frystyk 641: }
642: return NO;
643: }
644:
2.1 frystyk 645: /* HTHost_catchClose
646: ** -----------------
647: ** This function is registered when the socket is idle so that we get
648: ** a notification if the socket closes at the other end. At this point
649: ** we can't use the request object as it might have been freed a long
650: ** time ago.
651: */
2.13 frystyk 652: PUBLIC int HTHost_catchClose (SOCKET soc, void * context, HTEventType type)
2.1 frystyk 653: {
2.51 frystyk 654: #if 0
655: /* Not used anymore */
2.13 frystyk 656: HTNet * net = (HTNet *)context;
657: HTHost * host = net->host;
2.2 frystyk 658: if (CORE_TRACE)
2.13 frystyk 659: HTTrace("Catch Close. called with socket %d with type %x\n",
660: soc, type);
661: if (type == HTEvent_READ) {
2.1 frystyk 662: HTChannel * ch = HTChannel_find(soc); /* Find associated channel */
2.8 frystyk 663: HTHost * host = HTChannel_host(ch); /* and associated host */
2.1 frystyk 664: if (ch && host) {
2.2 frystyk 665: if (CORE_TRACE) HTTrace("Catch Close. CLOSING socket %d\n", soc);
2.8 frystyk 666: HTHost_clearChannel(host, HT_OK);
2.1 frystyk 667: } else {
2.2 frystyk 668: if (CORE_TRACE) HTTrace("Catch Close. socket %d NOT FOUND!\n",soc);
2.1 frystyk 669: }
670: }
2.13 frystyk 671: HTHost_unregister(host, net, HTEvent_CLOSE);
2.1 frystyk 672: return HT_OK;
2.51 frystyk 673: #endif
674: return HT_ERROR;
2.1 frystyk 675: }
676:
677: /*
678: ** As soon as we know that this host accepts persistent connections,
679: ** we associated the channel with the host.
680: ** We don't want more than MaxSockets-2 connections to be persistent in
681: ** order to avoid deadlock.
682: */
2.13 frystyk 683: PUBLIC BOOL HTHost_setPersistent (HTHost * host,
684: BOOL persistent,
685: HTTransportMode mode)
2.1 frystyk 686: {
2.13 frystyk 687: if (!host) return NO;
688:
689: if (!persistent) {
690: /*
691: ** We use the HT_IGNORE status code as we don't want to free
692: ** the stream at this point in time. The situation we want to
693: ** avoid is that we free the channel from within the stream pipe.
694: ** This will lead to an infinite look having the stream freing
695: ** itself.
696: */
2.30 frystyk 697: host->persistent = NO;
2.13 frystyk 698: return HTHost_clearChannel(host, HT_IGNORE);
699: }
700:
2.18 eric 701: /*
702: ** Set the host persistent if not already. Also update the mode to
703: ** the new one - it may have changed
704: */
705: HTHost_setMode(host, mode);
706: if (!host->persistent) {
2.13 frystyk 707: SOCKET sockfd = HTChannel_socket(host->channel);
2.8 frystyk 708: if (sockfd != INVSOC && HTNet_availablePersistentSockets() > 0) {
2.13 frystyk 709: host->persistent = YES;
2.32 frystyk 710: host->expires = time(NULL) + TcpTtl; /* Default timeout */
2.13 frystyk 711: HTChannel_setHost(host->channel, host);
2.8 frystyk 712: HTNet_increasePersistentSocket();
2.2 frystyk 713: if (CORE_TRACE)
2.1 frystyk 714: HTTrace("Host info... added host %p as persistent\n", host);
715: return YES;
716: } else {
2.2 frystyk 717: if (CORE_TRACE)
718: HTTrace("Host info... no room for persistent socket %d\n",
2.7 frystyk 719: sockfd);
2.18 eric 720: return NO;
2.1 frystyk 721: }
2.18 eric 722: } else {
723: if (CORE_TRACE) HTTrace("Host info... %p already persistent\n", host);
724: return YES;
2.1 frystyk 725: }
726: return NO;
727: }
728:
729: /*
2.13 frystyk 730: ** Check whether we have a persistent channel or not
731: */
732: PUBLIC BOOL HTHost_isPersistent (HTHost * host)
733: {
734: return host && host->persistent;
735: }
736:
737: /*
2.1 frystyk 738: ** Find persistent channel associated with this host.
739: */
740: PUBLIC HTChannel * HTHost_channel (HTHost * host)
741: {
742: return host ? host->channel : NULL;
743: }
744:
2.30 frystyk 745:
2.1 frystyk 746: /*
2.30 frystyk 747: ** Check whether we have got a "close" notification, for example in the
748: ** connection header
749: */
750: PUBLIC BOOL HTHost_setCloseNotification (HTHost * host, BOOL mode)
751: {
752: if (host) {
753: host->close_notification = mode;
2.37 frystyk 754: return YES;
2.30 frystyk 755: }
756: return NO;
757: }
758:
759: PUBLIC BOOL HTHost_closeNotification (HTHost * host)
760: {
761: return host && host->close_notification;
762: }
763:
764: /*
2.1 frystyk 765: ** Clear the persistent entry by deleting the channel object. Note that
766: ** the channel object is only deleted if it's not used anymore.
767: */
2.8 frystyk 768: PUBLIC BOOL HTHost_clearChannel (HTHost * host, int status)
2.1 frystyk 769: {
770: if (host && host->channel) {
2.8 frystyk 771: HTChannel_setHost(host->channel, NULL);
2.10 frystyk 772:
2.13 frystyk 773: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
774: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
2.18 eric 775: host->registeredFor = 0;
2.13 frystyk 776:
2.10 frystyk 777: /*
778: ** We don't want to recursively delete ourselves so if we are
779: ** called from within the stream pipe then don't delete the channel
780: ** at this point
781: */
2.8 frystyk 782: HTChannel_delete(host->channel, status);
2.18 eric 783: host->expires = 0;
2.1 frystyk 784: host->channel = NULL;
2.22 eric 785: host->tcpstate = TCP_BEGIN;
786: host->reqsMade = 0;
2.32 frystyk 787: if (HTHost_isPersistent(host)) {
788: HTNet_decreasePersistentSocket();
789: host->persistent = NO;
790: }
791: host->close_notification = NO;
2.45 frystyk 792: host->broken_pipe = NO;
2.32 frystyk 793: host->mode = HT_TP_SINGLE;
794:
2.28 frystyk 795: if (CORE_TRACE) HTTrace("Host info... removed host %p as persistent\n", host);
2.32 frystyk 796:
797: if (!HTList_isEmpty(host->pending)) {
798: if (CORE_TRACE)
2.45 frystyk 799: HTTrace("Host has %d object(s) pending - attempting launch\n", HTList_count(host->pending));
2.32 frystyk 800: HTHost_launchPending(host);
801: }
2.1 frystyk 802: return YES;
803: }
804: return NO;
805: }
806:
2.37 frystyk 807: PUBLIC BOOL HTHost_doRecover (HTHost * host)
808: {
809: return host ? host->do_recover : NO;
810: }
811:
2.1 frystyk 812: /*
2.18 eric 813: ** Move all entries in the pipeline and move the rest to the pending
814: ** queue. They will get launched at a later point in time.
815: */
816: PUBLIC BOOL HTHost_recoverPipe (HTHost * host)
817: {
818: if (host) {
819: int piped = HTList_count(host->pipeline);
820: if (piped > 0) {
821: int cnt;
2.24 frystyk 822: host->recovered++;
2.18 eric 823: if (CORE_TRACE)
2.24 frystyk 824: HTTrace("Host recovered %d times. Moving %d Net objects from pipe line to pending queue\n",
825: host->recovered, piped);
2.18 eric 826:
827: /*
828: ** Unregister this host for all events
829: */
830: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
831: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
832: host->registeredFor = 0;
833:
834: /*
835: ** Set new mode to single until we know what is going on
836: */
837: host->mode = HT_TP_SINGLE;
838:
839: /*
840: ** Move all net objects from the net object to the pending queue.
841: */
842: if (!host->pending) host->pending = HTList_new();
843: for (cnt=0; cnt<piped; cnt++) {
844: HTNet * net = HTList_removeLastObject(host->pipeline);
845: if (CORE_TRACE) HTTrace("Host recover Resetting net object %p\n", net);
846: net->registeredFor = 0;
847: (*net->event.cbf)(HTChannel_socket(host->channel), net->event.param, HTEvent_RESET);
848: HTList_appendObject(host->pending, net);
849: }
2.37 frystyk 850:
2.18 eric 851: HTChannel_setSemaphore(host->channel, 0);
852: HTHost_clearChannel(host, HT_INTERRUPTED);
2.37 frystyk 853: host->do_recover = NO;
2.18 eric 854: }
2.24 frystyk 855: return YES;
2.18 eric 856: }
857: return NO;
858: }
859:
860: /*
2.51 frystyk 861: ** Terminate a pipeline prematurely, for example because of timeout,
862: ** interruption, etc.
863: */
864: PUBLIC BOOL HTHost_killPipe (HTHost * host)
865: {
866: return killPipeline(host, HTEvent_CLOSE);
867: }
868:
869: /*
2.8 frystyk 870: ** Handle the connection mode. The mode may change mode in the
871: ** middle of a connection.
872: */
873: PUBLIC HTTransportMode HTHost_mode (HTHost * host, BOOL * active)
874: {
875: return host ? host->mode : HT_TP_SINGLE;
876: }
877:
878: /*
879: ** If the new mode is lower than the old mode then adjust the pipeline
880: ** accordingly. That is, if we are going into single mode then move
881: ** all entries in the pipeline and move the rest to the pending
882: ** queue. They will get launched at a later point in time.
883: */
884: PUBLIC BOOL HTHost_setMode (HTHost * host, HTTransportMode mode)
885: {
886: if (host) {
887: /*
888: ** Check the new mode and see if we must adjust the queues.
889: */
890: if (mode == HT_TP_SINGLE && host->mode > mode) {
891: int piped = HTList_count(host->pipeline);
892: if (piped > 0) {
893: int cnt;
894: if (CORE_TRACE)
895: HTTrace("Host info... Moving %d Net objects from pipe line to pending queue\n", piped);
896: if (!host->pending) host->pending = HTList_new();
897: for (cnt=0; cnt<piped; cnt++) {
2.18 eric 898: HTNet * net = HTList_removeLastObject(host->pipeline);
899: if (CORE_TRACE) HTTrace("Host info... Resetting net object %p\n", net);
2.13 frystyk 900: (*net->event.cbf)(HTChannel_socket(host->channel), net->event.param, HTEvent_RESET);
2.8 frystyk 901: HTList_appendObject(host->pending, net);
902: }
2.18 eric 903: HTChannel_setSemaphore(host->channel, 0);
904: HTHost_clearChannel(host, HT_INTERRUPTED);
2.8 frystyk 905: }
2.24 frystyk 906: }
907:
908: /*
909: ** If we know that this host is bad then we don't allow anything than
910: ** single mode. We can't recover connections for the rest of our life
911: */
912: if (mode == HT_TP_PIPELINE && host->recovered > MAX_HOST_RECOVER) {
913: if (PROT_TRACE)
914: HTTrace("Host info... %p is bad for pipelining so we won't do it!!!\n",
915: host);
916: } else {
917: host->mode = mode;
918: if (PROT_TRACE)
919: HTTrace("Host info... New mode is %d for host %p\n", host->mode, host);
920: }
2.8 frystyk 921: }
922: return NO;
923: }
924:
925: /*
926: ** Check whether a host is idle meaning if it is ready for a new
927: ** request which depends on the mode of the host. If the host is
928: ** idle, i.e. ready for use then return YES else NO. If the host supports
929: ** persistent connections then still only return idle if no requests are
930: ** ongoing.
931: */
932: PUBLIC BOOL HTHost_isIdle (HTHost * host)
933: {
2.32 frystyk 934: return (host && HTList_isEmpty(host->pipeline));
2.8 frystyk 935: }
936:
2.13 frystyk 937: PRIVATE BOOL _roomInPipe (HTHost * host)
938: {
939: int count;
2.34 frystyk 940: if (!host ||
941: (host->reqsPerConnection && host->reqsMade >= host->reqsPerConnection) ||
2.45 frystyk 942: HTHost_closeNotification(host) || host->broken_pipe)
2.32 frystyk 943: return NO;
2.13 frystyk 944: count = HTList_count(host->pipeline);
945: switch (host->mode) {
946: case HT_TP_SINGLE:
947: return count <= 0;
948: case HT_TP_PIPELINE:
2.51 frystyk 949: return (host->recovered < MAX_HOST_RECOVER) ?
950: (count < MaxPipelinedRequests) : (count <= 0);
2.13 frystyk 951: case HT_TP_INTERLEAVE:
952: return YES;
953: }
954: return NO;
955: }
956:
2.8 frystyk 957: /*
958: ** Add a net object to the host object. If the host
959: ** is idle then add to active list (pipeline) else add
960: ** it to the pending list
961: ** Return HT_PENDING if we must pend, HT_OK, or HT_ERROR
962: */
963: PUBLIC int HTHost_addNet (HTHost * host, HTNet * net)
964: {
965: if (host && net) {
966: int status = HT_OK;
2.32 frystyk 967: BOOL doit = (host->doit==net);
2.8 frystyk 968:
2.18 eric 969: /*
970: ** If we don't have a socket already then check to see if we can get
971: ** one. Otherwise we put the host object into our pending queue.
2.52 ! frystyk 972: */
2.18 eric 973: if (!host->channel && HTNet_availableSockets() <= 0) {
2.8 frystyk 974: if (!PendHost) PendHost = HTList_new();
975: HTList_addObject(PendHost, host);
2.52 ! frystyk 976: if (!host->pending) host->pending = HTList_new();
! 977: HTList_addObject(host->pending, net);
! 978: if (CORE_TRACE)
! 979: HTTrace("Host info... Added Host %p with Net %p (request %p) as pending, %d requests made, %d requests in pipe, %d pending\n",
! 980: host, net, net->request, host->reqsMade, HTList_count(host->pipeline), HTList_count(host->pending));
! 981: return HT_PENDING;
2.8 frystyk 982: }
983:
2.45 frystyk 984: #if 0
2.18 eric 985: /*
2.45 frystyk 986: ** First check whether the net object is already on either queue.
987: ** Do NOT add extra copies of the HTNet object to
988: ** the pipeline or pending list (if it's already on the list).
989: */
990: if (HTList_indexOf(host->pipeline, net) >= 0) {
991: if (CORE_TRACE)
992: HTTrace("Host info... The Net %p (request %p) is already in pipe,"
993: " %d requests made, %d requests in pipe, %d pending\n",
994: net, net->request, host->reqsMade,
995: HTList_count(host->pipeline),
996: HTList_count(host->pending));
997: HTDebugBreak(__FILE__, __LINE__,
998: "Net object %p registered multiple times in pipeline\n",
999: net);
1000: return HT_OK;
1001: }
2.44 frystyk 1002:
2.45 frystyk 1003: if (HTList_indexOf(host->pending, net) >= 0) {
1004: if (CORE_TRACE)
1005: HTTrace("Host info... The Net %p (request %p) already pending,"
1006: " %d requests made, %d requests in pipe, %d pending\n",
1007: net, net->request, host->reqsMade,
1008: HTList_count(host->pipeline),
1009: HTList_count(host->pending));
1010: HTDebugBreak(__FILE__, __LINE__,
1011: "Net object %p registered multiple times in pending queue\n",
1012: net);
1013:
1014: return HT_PENDING;
1015: }
1016: #endif
2.44 frystyk 1017:
1018: /*
2.18 eric 1019: ** Add net object to either active or pending queue.
1020: */
2.44 frystyk 1021: if (_roomInPipe(host) && (HTList_isEmpty(host->pending) || doit)) {
2.32 frystyk 1022: if (doit) host->doit = NULL;
2.8 frystyk 1023: if (!host->pipeline) host->pipeline = HTList_new();
1024: HTList_addObject(host->pipeline, net);
2.32 frystyk 1025: host->reqsMade++;
1026: if (CORE_TRACE)
1027: HTTrace("Host info... Add Net %p (request %p) to pipe, %d requests made, %d requests in pipe, %d pending\n",
1028: net, net->request, host->reqsMade, HTList_count(host->pipeline), HTList_count(host->pending));
2.18 eric 1029:
2.13 frystyk 1030: /*
2.30 frystyk 1031: ** If we have been idle then make sure we delete the timer
2.13 frystyk 1032: */
2.30 frystyk 1033: if (host->timer) {
1034: HTTimer_delete(host->timer);
1035: host->timer = NULL;
1036: }
2.40 kahan 1037:
1038: /*JK: New CBF function
1039: ** Call any user-defined callback to say the request will
1040: ** be processed.
1041: */
1042: HTHost_ActivateRequest (net);
2.30 frystyk 1043:
2.8 frystyk 1044: } else {
1045: if (!host->pending) host->pending = HTList_new();
2.44 frystyk 1046: HTList_addObject(host->pending, net);
2.32 frystyk 1047: if (CORE_TRACE)
1048: HTTrace("Host info... Add Net %p (request %p) to pending, %d requests made, %d requests in pipe, %d pending\n",
1049: net, net->request, host->reqsMade, HTList_count(host->pipeline), HTList_count(host->pending));
2.8 frystyk 1050: status = HT_PENDING;
1051: }
1052: return status;
1053: }
1054: return HT_ERROR;
1055: }
1056:
2.51 frystyk 1057: PRIVATE BOOL HTHost_free (HTHost * host, int status)
2.13 frystyk 1058: {
2.32 frystyk 1059: if (host->channel) {
2.13 frystyk 1060:
2.32 frystyk 1061: /* Check if we should keep the socket open */
1062: if (HTHost_isPersistent(host)) {
2.48 frystyk 1063: int piped = HTList_count(host->pipeline);
2.37 frystyk 1064: if (HTHost_closeNotification(host)) {
1065: if (CORE_TRACE)
1066: HTTrace("Host Object. got close notifiation on socket %d\n",
1067: HTChannel_socket(host->channel));
1068:
1069: /*
1070: ** If more than a single element (this one) in the pipe
1071: ** then we have to recover gracefully
1072: */
1073: if (piped > 1) {
1074: host->reqsPerConnection = host->reqsMade - piped;
1075: if (CORE_TRACE)
1076: HTTrace("%d requests made, %d in pipe, max %d requests pr connection\n",
1077: host->reqsMade, piped, host->reqsPerConnection);
1078: host->do_recover = YES;
2.38 frystyk 1079: HTChannel_delete(host->channel, status);
1080: } else {
1081: HTChannel_setSemaphore(host->channel, 0);
1082: HTHost_clearChannel(host, status);
2.37 frystyk 1083: }
2.48 frystyk 1084: } else if (piped<=1 && host->reqsMade==host->reqsPerConnection) {
2.37 frystyk 1085: if (CORE_TRACE) HTTrace("Host Object. closing persistent socket %d\n",
1086: HTChannel_socket(host->channel));
2.32 frystyk 1087:
1088: /*
1089: ** By lowering the semaphore we make sure that the channel
1090: ** is gonna be deleted
1091: */
1092: HTChannel_setSemaphore(host->channel, 0);
1093: HTHost_clearChannel(host, status);
1094:
1095: } else {
1096: if (CORE_TRACE) HTTrace("Host Object. keeping persistent socket %d\n", HTChannel_socket(host->channel));
1097: HTChannel_delete(host->channel, status);
1098:
1099: /*
1100: ** If connection is idle then set a timer so that we close the
1101: ** connection if idle too long
1102: */
2.48 frystyk 1103: if (piped<=1 && HTList_isEmpty(host->pending) && !host->timer) {
1104: host->timer = HTTimer_new(NULL, IdleTimeoutEvent,
1105: host, TCP_IDLE_ACTIVE, YES, NO);
2.32 frystyk 1106: if (PROT_TRACE) HTTrace("Host........ Object %p going idle...\n", host);
1107: }
1108: }
1109: return YES;
1110: } else {
2.33 frystyk 1111: if (CORE_TRACE) HTTrace("Host Object. closing socket %d\n", HTChannel_socket(host->channel));
2.47 frystyk 1112: HTChannel_setSemaphore(host->channel, 0);
2.41 frystyk 1113: HTHost_clearChannel(host, status);
2.32 frystyk 1114: }
2.13 frystyk 1115: }
2.32 frystyk 1116: return NO;
2.13 frystyk 1117: }
1118:
2.51 frystyk 1119: PUBLIC BOOL HTHost_deleteNet (HTHost * host, HTNet * net, int status)
2.8 frystyk 1120: {
1121: if (host && net) {
2.44 frystyk 1122: if (CORE_TRACE) HTTrace("Host info... Remove %p from pipe\n", net);
2.51 frystyk 1123:
1124: /* If the Net object is in the pipeline then also update the channel */
1125: if (host->pipeline && HTList_indexOf(host->pipeline, net) >= 0) {
1126: HTHost_free(host, status);
1127: HTList_removeObjectAll(host->pipeline, net);
1128: }
1129:
2.44 frystyk 1130: HTList_removeObjectAll(host->pending, net); /* just to make sure */
2.8 frystyk 1131: return YES;
1132: }
1133: return NO;
1134: }
1135:
1136: /*
1137: ** Handle pending host objects.
1138: ** There are two ways we can end up with pending reqyests:
1139: ** 1) If we are out of sockets then register new host objects as pending.
1140: ** 2) If we are pending on a connection then register new net objects as
1141: ** pending
1142: ** This set of functions handles pending host objects and can start new
1143: ** requests as resources get available
1144: */
1145:
1146: /*
1147: ** Check this host object for any pending requests and return the next
1148: ** registered Net object.
1149: */
1150: PUBLIC HTNet * HTHost_nextPendingNet (HTHost * host)
1151: {
1152: HTNet * net = NULL;
2.32 frystyk 1153: if (host && host->pending) {
2.18 eric 1154: /*JK 23/Sep/96 Bug correction. Associated the following lines to the
1155: **above if. There was a missing pair of brackets.
1156: */
1157: if ((net = (HTNet *) HTList_removeFirstObject(host->pending)) != NULL) {
2.32 frystyk 1158: if (CORE_TRACE)
1159: HTTrace("Host info... Popping %p from pending net queue\n", net);
2.33 frystyk 1160: #if 0
1161: {
1162: HTRequest * request = HTNet_request(net);
1163: char * uri = HTAnchor_address((HTAnchor *) HTRequest_anchor(request));
1164: fprintf(stderr, "Popping '%s'\n", uri);
1165: }
1166: #endif
2.32 frystyk 1167: host->doit = net;
2.18 eric 1168: }
2.8 frystyk 1169: }
1170: return net;
1171: }
1172:
1173: /*
2.13 frystyk 1174: ** Return the current list of pending host objects waiting for a socket
2.8 frystyk 1175: */
1176: PUBLIC HTHost * HTHost_nextPendingHost (void)
1177: {
1178: HTHost * host = NULL;
1179: if (PendHost) {
1180: if ((host = (HTHost *) HTList_removeFirstObject(PendHost)) != NULL)
1181: if (PROT_TRACE)
2.32 frystyk 1182: HTTrace("Host info... Popping %p from pending host queue\n",
2.8 frystyk 1183: host);
1184: }
1185: return host;
1186: }
1187:
1188: /*
1189: ** Start the next pending request if any. First we look for pending
1190: ** requests for the same host and then we check for any other pending
1191: ** hosts
1192: */
1193: PUBLIC BOOL HTHost_launchPending (HTHost * host)
1194: {
2.52 ! frystyk 1195: HTNet * net = NULL;
2.8 frystyk 1196: if (!host) {
1197: if (PROT_TRACE) HTTrace("Host info... Bad arguments\n");
1198: return NO;
1199: }
1200:
1201: /*
2.52 ! frystyk 1202: ** In pipeline we can only have one doing writing at a time.
! 1203: ** We therefore check that there are no other Net object
! 1204: ** registered for write
2.8 frystyk 1205: */
2.52 ! frystyk 1206: if (host->mode == HT_TP_PIPELINE) {
! 1207: net = (HTNet *) HTList_lastObject(host->pipeline);
! 1208: if (net && net->registeredFor == HTEvent_WRITE)
! 1209: return NO;
! 1210: }
2.8 frystyk 1211:
2.52 ! frystyk 1212: /*
! 1213: ** Check the current Host object for pending Net objects
! 1214: */
! 1215: if (_roomInPipe(host) && DoPendingReqLaunch &&
! 1216: (net = HTHost_nextPendingNet(host))) {
! 1217: HTHost_ActivateRequest(net);
! 1218: if (CORE_TRACE)
! 1219: HTTrace("Launch pending net object %p with %d reqs in pipe (%d reqs made)\n",
! 1220: net, HTList_count(host->pipeline), host->reqsMade);
! 1221: return HTNet_execute(net, HTEvent_WRITE);
! 1222: }
2.13 frystyk 1223:
2.52 ! frystyk 1224: /*
! 1225: ** Check for other pending Host objects
! 1226: */
! 1227: if (DoPendingReqLaunch && HTNet_availableSockets() > 0) {
! 1228: HTHost * pending = HTHost_nextPendingHost();
! 1229: if (pending && (net = HTHost_nextPendingNet(pending))) {
! 1230: if (!pending->pipeline) pending->pipeline = HTList_new();
! 1231: HTList_addObject(pending->pipeline, net);
! 1232: host->reqsMade++;
! 1233: if (CORE_TRACE)
! 1234: HTTrace("Launch pending host object %p, net %p with %d reqs in pipe (%d reqs made)\n",
! 1235: pending, net, HTList_count(pending->pipeline), pending->reqsMade);
! 1236: HTHost_ActivateRequest(net);
! 1237: return HTNet_execute(net, HTEvent_WRITE);
2.8 frystyk 1238: }
2.52 ! frystyk 1239: }
! 1240: return YES;
2.13 frystyk 1241: }
1242:
1243: PUBLIC HTNet * HTHost_firstNet (HTHost * host)
1244: {
1245: return (HTNet *) HTList_firstObject(host->pipeline);
1246: }
1247:
1248: /*
1249: ** The host event manager keeps track of the state of it's client engines
1250: ** (typically HTTPEvent), accepting multiple blocks on read or write from
1251: ** multiple pipelined engines. It then registers its own engine
1252: ** (HostEvent) with the event manager.
1253: */
1254: PUBLIC int HTHost_connect (HTHost * host, HTNet * net, char * url, HTProtocolId port)
1255: {
2.42 frystyk 1256: HTRequest * request = HTNet_request(net);
2.52 ! frystyk 1257: int status = HT_OK;
2.42 frystyk 1258: if (!host) {
1259: HTProtocol * protocol = HTNet_protocol(net);
1260: if ((host = HTHost_newWParse(request, url, HTProtocol_id(protocol))) == NULL)
2.45 frystyk 1261: return HT_ERROR;
2.52 ! frystyk 1262:
! 1263: /*
! 1264: ** If not already locked and without a channel
! 1265: ** then lock the darn thing
! 1266: */
! 1267: if (!host->lock && !host->channel) {
2.42 frystyk 1268: host->forceWriteFlush = YES;
1269: host->lock = net;
1270: }
1271: HTNet_setHost(net, host);
1272: }
1273:
1274: if (!host->lock || (host->lock && host->lock == net)) {
1275: status = HTDoConnect(net, url, port);
1276: if (status == HT_OK) {
1277: host->lock = NULL;
1278: return HT_OK;
1279: }
1280: if (status == HT_WOULD_BLOCK) {
1281: host->lock = net;
2.52 ! frystyk 1282: return status;
2.42 frystyk 1283: }
1284: if (status == HT_PENDING) return HT_WOULD_BLOCK;
1285: } else {
1286: if ((status = HTHost_addNet(host, net)) == HT_PENDING) {
1287: return HT_PENDING;
1288: }
1289: }
2.13 frystyk 1290: return HT_ERROR; /* @@@ - some more deletion and stuff here? */
1291: }
1292:
1293: /*
1294: ** Rules: SINGLE: one element in pipe, either reading or writing
1295: ** PIPE: n element in pipe, n-1 reading, 1 writing
1296: */
1297: PUBLIC int HTHost_register (HTHost * host, HTNet * net, HTEventType type)
1298: {
2.40 kahan 1299: HTEvent *event;
1300:
2.13 frystyk 1301: if (host && net) {
1302:
2.28 frystyk 1303: if (type == HTEvent_CLOSE) {
2.13 frystyk 1304:
2.28 frystyk 1305: /*
1306: ** Unregister this host for all events
1307: */
1308: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_READ);
1309: HTEvent_unregister(HTChannel_socket(host->channel), HTEvent_WRITE);
1310: host->registeredFor = 0;
2.13 frystyk 1311: return YES;
2.28 frystyk 1312:
1313: } else {
1314:
1315: /* net object may already be registered */
1316: if (HTEvent_BITS(type) & net->registeredFor)
1317: return NO;
1318: net->registeredFor ^= HTEvent_BITS(type);
1319:
1320: /* host object may already be registered */
1321: if (host->registeredFor & HTEvent_BITS(type))
1322: return YES;
1323: host->registeredFor ^= HTEvent_BITS(type);
2.46 frystyk 1324:
1325: #ifdef WWW_WIN_ASYNC
1326: /* Make sure we are registered for CLOSE on windows */
1327: event = *(host->events+HTEvent_INDEX(HTEvent_CLOSE));
1328: HTEvent_register(HTChannel_socket(host->channel), HTEvent_CLOSE, event);
1329: #endif /* WWW_WIN_ASYNC */
1330:
1331: /* JK: register a request in the event structure */
2.40 kahan 1332: event = *(host->events+HTEvent_INDEX(type));
1333: event->request = HTNet_request (net);
2.28 frystyk 1334: return HTEvent_register(HTChannel_socket(host->channel),
2.40 kahan 1335: type, event);
2.28 frystyk 1336: }
2.52 ! frystyk 1337:
! 1338: return YES;
2.13 frystyk 1339: }
2.52 ! frystyk 1340: if ("HTHost req.. Bad arguments\n");
2.13 frystyk 1341: return NO;
1342: }
1343:
1344: PUBLIC int HTHost_unregister (HTHost * host, HTNet * net, HTEventType type)
1345: {
1346: if (host && net) {
1347:
2.28 frystyk 1348: /* net object may not be registered */
2.13 frystyk 1349: if (!(HTEvent_BITS(type) & net->registeredFor))
1350: return NO;
1351: net->registeredFor ^= HTEvent_BITS(type);
1352:
2.28 frystyk 1353: /* host object may not be registered */
2.13 frystyk 1354: if (!(host->registeredFor & HTEvent_BITS(type)))
1355: return YES;
1356: host->registeredFor ^= HTEvent_BITS(type);
1357:
1358: /* stay registered for READ to catch a socket close */
1359: /* WRITE and CONNECT can be unregistered, though */
1360: if ((type == HTEvent_WRITE && isLastInPipe(host, net)) ||
1361: type == HTEvent_CONNECT)
1362: /* if we are blocked downstream, shut down the whole pipe */
1363: HTEvent_unregister(HTChannel_socket(host->channel), type);
1364: return YES;
1365: }
1366: return NO;
1367: }
1368:
1369: /*
1370: ** The reader tells HostEvent that it's stream did not finish the data
1371: */
1372: PUBLIC BOOL HTHost_setRemainingRead (HTHost * host, size_t remaining)
1373: {
1374: if (host == NULL) return NO;
1375: host->remainingRead = remaining;
2.20 frystyk 1376: if (PROT_TRACE) HTTrace("Host........ %d bytes remaining \n", remaining);
2.45 frystyk 1377: if (host->broken_pipe && remaining == 0) {
1378: if (PROT_TRACE) HTTrace("Host........ Emtied out connection\n");
1379: }
2.13 frystyk 1380: return YES;
1381: }
1382:
2.32 frystyk 1383: PUBLIC size_t HTHost_remainingRead (HTHost * host)
1384: {
1385: return host ? host->remainingRead : -1;
1386: }
1387:
2.13 frystyk 1388: PUBLIC SockA * HTHost_getSockAddr (HTHost * host)
1389: {
1390: if (!host) return NULL;
1391: return &host->sock_addr;
1392: }
1393:
1394: PUBLIC BOOL HTHost_setHome (HTHost * host, int home)
1395: {
1396: if (!host) return NO;
1397: host->home = home;
1398: return YES;
1399: }
1400:
1401: PUBLIC int HTHost_home (HTHost * host)
1402: {
1403: if (!host) return 0;
1404: return host->home;
1405: }
1406:
2.27 frystyk 1407: PUBLIC BOOL HTHost_setRetry (HTHost * host, int retry)
1408: {
1409: if (!host) return NO;
1410: host->retry = retry;
1411: return YES;
1412: }
1413:
1414: PUBLIC BOOL HTHost_decreaseRetry (HTHost * host)
1415: {
2.44 frystyk 1416: if (!host) return NO;
1417:
1418: if (host->retry > 0) host->retry--;
1419: return YES;
1420:
2.27 frystyk 1421: }
1422:
1423: PUBLIC int HTHost_retry (HTHost * host)
1424: {
1425: if (!host) return 0;
1426: return host->retry;
1427: }
1428:
2.13 frystyk 1429: #if 0 /* Is a macro right now */
2.21 frystyk 1430: PRIVATE BOOL HTHost_setDNS5 (HTHost * host, HTdns * dns)
2.13 frystyk 1431: {
1432: if (!host) return NO;
1433: host->dns = dns;
1434: return YES;
1435: }
1436: #endif
1437:
1438: PUBLIC BOOL HTHost_setChannel (HTHost * host, HTChannel * channel)
1439: {
1440: if (!host) return NO;
1441: host->channel = channel;
1442: return YES;
1443: }
1444:
1445: PUBLIC HTNet * HTHost_getReadNet(HTHost * host)
1446: {
2.38 frystyk 1447: return host ? (HTNet *) HTList_firstObject(host->pipeline) : NULL;
2.13 frystyk 1448: }
1449:
1450: PUBLIC HTNet * HTHost_getWriteNet(HTHost * host)
1451: {
1452: return host ? (HTNet *) HTList_lastObject(host->pipeline) : NULL;
1453: }
1454:
1455: /*
1456: ** Create the input stream and bind it to the channel
1457: ** Please read the description in the HTIOStream module for the parameters
1458: */
1459: PUBLIC HTInputStream * HTHost_getInput (HTHost * host, HTTransport * tp,
1460: void * param, int mode)
1461: {
1462: if (host && host->channel && tp) {
1463: HTChannel * ch = host->channel;
1464: HTInputStream * input = (*tp->input_new)(host, ch, param, mode);
1465: HTChannel_setInput(ch, input);
1466: return HTChannel_getChannelIStream(ch);
1467: }
2.24 frystyk 1468: if (CORE_TRACE) HTTrace("Host Object. Can't create input stream\n");
2.13 frystyk 1469: return NULL;
1470: }
1471:
1472: PUBLIC HTOutputStream * HTHost_getOutput (HTHost * host, HTTransport * tp,
1473: void * param, int mode)
1474: {
1475: if (host && host->channel && tp) {
1476: HTChannel * ch = host->channel;
1477: HTOutputStream * output = (*tp->output_new)(host, ch, param, mode);
1478: HTChannel_setOutput(ch, output);
1479: return output;
1480: }
2.24 frystyk 1481: if (CORE_TRACE) HTTrace("Host Object. Can't create output stream\n");
2.13 frystyk 1482: return NULL;
1483: }
1484:
1485: PUBLIC HTOutputStream * HTHost_output (HTHost * host, HTNet * net)
1486: {
1487: if (host && host->channel && net) {
1488: HTOutputStream * output = HTChannel_output(host->channel);
1489: return output;
1490: }
1491: return NULL;
1492: }
1493:
1494: PUBLIC int HTHost_read(HTHost * host, HTNet * net)
1495: {
1496: HTInputStream * input = HTChannel_input(host->channel);
1497: if (net != HTHost_getReadNet(host)) {
1498: HTHost_register(host, net, HTEvent_READ);
1499: return HT_WOULD_BLOCK;
1500: }
2.17 frystyk 1501:
1502: /*
1503: ** If there is no input channel then this can either mean that
1504: ** we have lost the channel or an error occurred. We return
1505: ** HT_CLOSED as this is a sign to the caller that we don't
1506: ** have a channel
1507: */
1508: return input ? (*input->isa->read)(input) : HT_CLOSED;
2.13 frystyk 1509: }
1510:
1511: PUBLIC BOOL HTHost_setConsumed(HTHost * host, size_t bytes)
1512: {
1513: HTInputStream * input;
1514: if (!host || !host->channel) return NO;
1515: if ((input = HTChannel_input(host->channel)) == NULL)
1516: return NO;
2.32 frystyk 1517: if (CORE_TRACE)
2.20 frystyk 1518: HTTrace("Host........ passing %d bytes as consumed to %p\n", bytes, input);
2.13 frystyk 1519: return (*input->isa->consumed)(input, bytes);
1520: }
1521:
1522: PUBLIC int HTHost_hash (HTHost * host)
1523: {
1524: return host ? host->hash : -1;
1525: }
1526:
2.26 frystyk 1527: PUBLIC BOOL HTHost_setWriteDelay (HTHost * host, ms_t delay)
2.13 frystyk 1528: {
2.26 frystyk 1529: if (host && delay >= 0) {
1530: host->delay = delay;
1531: return YES;
1532: }
1533: return NO;
1534: }
1535:
1536: PUBLIC ms_t HTHost_writeDelay (HTHost * host)
1537: {
1538: return host ? host->delay : 0;
1539: }
1540:
1541: PUBLIC int HTHost_findWriteDelay (HTHost * host, ms_t lastFlushTime, int buffSize)
1542: {
2.35 frystyk 1543: #if 0
2.15 eric 1544: unsigned short mtu;
2.18 eric 1545: int ret = -1;
2.15 eric 1546: int socket = HTChannel_socket(host->channel);
2.18 eric 1547: #ifndef WWW_MSWINDOWS
2.15 eric 1548: ret = ioctl(socket, 666, (unsigned long)&mtu);
2.18 eric 1549: #endif /* WWW_MSWINDOWS */
2.15 eric 1550: if ((ret == 0 && buffSize >= mtu) || host->forceWriteFlush)
2.13 frystyk 1551: return 0;
2.26 frystyk 1552: return host->delay;
2.35 frystyk 1553: #else
1554: return host->forceWriteFlush ? 0 : host->delay;
1555: #endif
2.13 frystyk 1556: }
1557:
2.26 frystyk 1558: PUBLIC BOOL HTHost_setDefaultWriteDelay (ms_t delay)
1559: {
1560: if (delay >= 0) {
1561: WriteDelay = delay;
1562: if (CORE_TRACE) HTTrace("Host........ Default write delay is %d ms\n", delay);
1563: return YES;
1564: }
1565: return NO;
1566: }
1567:
1568: PUBLIC ms_t HTHost_defaultWriteDelay (void)
1569: {
1570: return WriteDelay;
1571: }
1572:
2.13 frystyk 1573: PUBLIC int HTHost_forceFlush(HTHost * host)
1574: {
2.35 frystyk 1575: HTNet * targetNet = (HTNet *) HTList_lastObject(host->pipeline);
2.13 frystyk 1576: int ret;
2.35 frystyk 1577: if (targetNet == NULL) return HT_ERROR;
2.13 frystyk 1578: if (CORE_TRACE)
2.28 frystyk 1579: HTTrace("Host Event.. FLUSH passed to `%s\'\n",
1580: HTAnchor_physical(HTRequest_anchor(HTNet_request(targetNet))));
2.13 frystyk 1581: host->forceWriteFlush = YES;
1582: ret = (*targetNet->event.cbf)(HTChannel_socket(host->channel), targetNet->event.param, HTEvent_FLUSH);
2.35 frystyk 1583: host->forceWriteFlush = NO;
2.13 frystyk 1584: return ret;
2.39 frystyk 1585: }
1586:
1587: /*
1588: ** Context pointer to be used as a user defined context
1589: */
1590: PUBLIC void HTHost_setContext (HTHost * me, void * context)
1591: {
2.40 kahan 1592: if (me) me->context = context;
2.39 frystyk 1593: }
1594:
1595: PUBLIC void * HTHost_context (HTHost * me)
1596: {
2.40 kahan 1597: return me ? me->context : NULL;
2.1 frystyk 1598: }
2.11 kahan 1599:
2.13 frystyk 1600: PUBLIC int HTHost_eventTimeout (void)
1601: {
1602: return EventTimeout;
1603: }
2.11 kahan 1604:
2.13 frystyk 1605: PUBLIC void HTHost_setEventTimeout (int millis)
1606: {
1607: EventTimeout = millis;
1608: if (CORE_TRACE) HTTrace("Host........ Setting event timeout to %d ms\n", millis);
1609: }
2.40 kahan 1610:
2.45 frystyk 1611: PUBLIC BOOL HTHost_setMaxPipelinedRequests (int max)
1612: {
1613: if (max > 1) {
1614: MaxPipelinedRequests = max;
1615: return YES;
1616: }
1617: return NO;
1618: }
2.40 kahan 1619:
2.45 frystyk 1620: PUBLIC int HTHost_maxPipelinedRequests (void)
2.40 kahan 1621: {
2.45 frystyk 1622: return MaxPipelinedRequests;
1623: }
1624:
1625: PUBLIC void HTHost_setActivateRequestCallback (HTHost_ActivateRequestCallback * cbf)
1626: {
1627: if (CORE_TRACE) HTTrace("HTHost...... Registering %p\n", cbf);
2.40 kahan 1628: ActivateReqCBF = cbf;
1629: }
1630:
2.45 frystyk 1631: PRIVATE int HTHost_ActivateRequest (HTNet * net)
2.40 kahan 1632: {
2.45 frystyk 1633: HTRequest * request = NULL;
1634: if (!ActivateReqCBF) {
1635: if (CORE_TRACE)
1636: HTTrace("HTHost...... No ActivateRequest callback handler registered\n");
1637: return HT_ERROR;
1638: }
1639: request = HTNet_request(net);
1640: return (*ActivateReqCBF)(request);
2.40 kahan 1641: }
1642:
1643: PUBLIC void HTHost_disable_PendingReqLaunch (void)
1644: {
2.45 frystyk 1645: DoPendingReqLaunch = NO;
2.40 kahan 1646: }
1647:
1648: PUBLIC void HTHost_enable_PendingReqLaunch (void)
1649: {
2.45 frystyk 1650: DoPendingReqLaunch = YES;
2.40 kahan 1651: }
1652:
Webmaster