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