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