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