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