Annotation of libwww/Library/src/HTNet.c, revision 2.88
2.23 frystyk 1: /* HTNet.c
2.79 frystyk 2: ** HTNet Class
2.1 frystyk 3: **
2.10 frystyk 4: ** (c) COPYRIGHT MIT 1995.
2.4 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
2.88 ! frystyk 6: ** @(#) $Id: HTNet.c,v 2.87 1997/02/16 17:49:23 frystyk Exp $
2.4 frystyk 7: **
8: ** This is the implementation of the internal library multithreading
2.1 frystyk 9: ** functions. This includes an interrupt handler and a event loop.
10: **
11: ** History:
2.14 frystyk 12: ** 12 June 94 Written by Henrik Frystyk, frystyk@w3.org
2.17 frystyk 13: ** 31 May 95 Charlie Brooks cbrooks@osf.org
14: **
2.1 frystyk 15: */
16:
2.9 frystyk 17: /* Implemention dependent include files */
2.52 frystyk 18: #include "sysdep.h"
2.9 frystyk 19:
2.1 frystyk 20: /* Library include files */
2.59 frystyk 21: #include "WWWUtil.h"
2.16 frystyk 22: #include "HTProt.h"
2.1 frystyk 23: #include "HTError.h"
2.25 frystyk 24: #include "HTAlert.h"
2.37 frystyk 25: #include "HTParse.h"
2.59 frystyk 26: #include "HTTrans.h"
2.74 frystyk 27: #include "HTHost.h"
28: #include "HTReq.h"
2.65 frystyk 29: #include "HTEvent.h"
2.23 frystyk 30: #include "HTStream.h"
2.81 frystyk 31: #include "HTHstMan.h"
32: #include "HTIOStream.h"
2.24 frystyk 33: #include "HTNetMan.h" /* Implemented here */
2.1 frystyk 34:
2.23 frystyk 35: #ifndef HT_MAX_SOCKETS
36: #define HT_MAX_SOCKETS 6
37: #endif
38:
2.74 frystyk 39: #define HASH_SIZE 67
40:
2.79 frystyk 41: typedef struct _BeforeFilter {
42: HTNetBefore * before; /* Filter function */
43: char * tmplate; /* URL template for when to call filter */
44: int order; /* Relative execution order */
2.60 frystyk 45: void * param; /* Local context */
2.79 frystyk 46: } BeforeFilter;
47:
48: typedef struct _AfterFilter {
49: HTNetAfter * after; /* Filter function */
50: char * tmplate; /* URL template for when to call filter */
51: int order; /* Relative execution order */
52: void * param; /* Local context */
53: int status; /* Status of load for when to call filter */
54: } AfterFilter;
2.23 frystyk 55:
56: struct _HTStream {
2.52 frystyk 57: const HTStreamClass * isa;
2.23 frystyk 58: /* ... */
59: };
60:
2.81 frystyk 61: struct _HTInputStream {
62: const HTInputStreamClass * isa;
63: /* ... */
64: };
65:
2.79 frystyk 66: PRIVATE HTList * HTBefore = NULL; /* List of global BEFORE filters */
67: PRIVATE HTList * HTAfter = NULL; /* List of global AFTER filters */
2.24 frystyk 68:
2.74 frystyk 69: PRIVATE int MaxActive = HT_MAX_SOCKETS; /* Max active requests */
70: PRIVATE int Active = 0; /* Counts open sockets */
71: PRIVATE int Persistent = 0; /* Counts persistent sockets */
2.1 frystyk 72:
2.74 frystyk 73: PRIVATE HTList ** NetTable = NULL; /* List of net objects */
74: PRIVATE int HTNetCount = 0; /* Counting elements in table */
2.23 frystyk 75:
76: /* ------------------------------------------------------------------------- */
2.79 frystyk 77: /* GENERIC BEFORE and AFTER filter Management */
2.23 frystyk 78: /* ------------------------------------------------------------------------- */
79:
2.79 frystyk 80: PRIVATE int HTBeforeOrder (const void * a, const void * b)
81: {
82: return ((BeforeFilter *) b)->order - ((BeforeFilter *) a)->order;
83: }
84:
85: PRIVATE int HTAfterOrder (const void * a, const void * b)
86: {
87: return ((AfterFilter *) b)->order - ((AfterFilter *) a)->order;
88: }
89:
2.80 frystyk 90: PRIVATE int check_order (HTFilterOrder order)
2.79 frystyk 91: {
92: return (order<HT_FILTER_FIRST) ? HT_FILTER_FIRST :
93: (order>HT_FILTER_LAST) ? HT_FILTER_LAST : order;
94: }
95:
96: /*
97: ** Register a BEFORE filter in the list provided by the caller.
98: ** Several filters can be registered in which case they are called
99: ** with the filter ordering in mind.
100: */
101: PUBLIC BOOL HTNetCall_addBefore (HTList * list, HTNetBefore * before,
102: const char * tmplate, void * param,
2.80 frystyk 103: HTFilterOrder order)
2.79 frystyk 104: {
105: if (list && before) {
106: BeforeFilter * me;
107: if ((me = (BeforeFilter *) HT_CALLOC(1, sizeof(BeforeFilter)))==NULL)
108: HT_OUTOFMEM("HTNetCall_addBefore");
109: me->before = before;
110: if (tmplate) StrAllocCopy(me->tmplate, tmplate);
111: me->order = check_order(order);
112: me->param = param;
113: if (CORE_TRACE)
114: HTTrace("Net Before.. Add %p with order %d tmplate `%s\' context %p\n",
115: before, me->order, tmplate ? tmplate : "<null>", param);
116: return (HTList_addObject(list, me) &&
117: HTList_insertionSort(list, HTBeforeOrder));
118: }
119: return NO;
120: }
121:
122: /*
123: ** Unregister all instances of a BEFORE filter from a list.
124: */
125: PUBLIC BOOL HTNetCall_deleteBefore (HTList * list, HTNetBefore * before)
126: {
127: if (CORE_TRACE) HTTrace("Net Before.. Delete %p\n", before);
128: if (list && before) {
129: HTList * cur = list;
130: BeforeFilter * pres;
131: while ((pres = (BeforeFilter *) HTList_nextObject(list))) {
132: if (pres->before == before) {
133: HTList_removeObject(list, (void *) pres);
134: HT_FREE(pres->tmplate);
135: HT_FREE(pres);
136: cur = list;
137: }
138: }
139: }
140: return NO;
141: }
142:
143: /*
144: ** Deletes all BEFORE filters in list
145: */
146: PUBLIC BOOL HTNetCall_deleteBeforeAll (HTList * list)
147: {
148: if (CORE_TRACE) HTTrace("Net Before. Delete All filters\n");
149: if (list) {
150: HTList * cur = list;
151: BeforeFilter * pres;
152: while ((pres = (BeforeFilter *) HTList_nextObject(cur))) {
153: HT_FREE(pres->tmplate);
154: HT_FREE(pres);
155: }
156: HTList_delete(list);
157: return YES;
158: }
159: return NO;
160: }
161:
162: /*
163: ** Call all the BEFORE filters in the order specified at registration
164: ** time. We also check for any template and whether it matches or not.
165: ** If a filter returns other than HT_OK then stop and return immediately.
166: ** Otherwise return what the last filter returns.
2.23 frystyk 167: */
2.79 frystyk 168: PUBLIC int HTNetCall_executeBefore (HTList * list, HTRequest * request)
2.23 frystyk 169: {
2.79 frystyk 170: HTParentAnchor * anchor = HTRequest_anchor(request);
171: char * url = HTAnchor_physical(anchor);
172: char * addr = url ? url : HTAnchor_address((HTAnchor *) anchor);
173: int ret = HT_OK;
174: int mode = 0;
175: if (list && request && addr) {
176: BeforeFilter * pres;
177: while ((pres = (BeforeFilter *) HTList_nextObject(list))) {
178: if (!pres->tmplate ||
179: (pres->tmplate && HTStrMatch(pres->tmplate, addr))) {
180: if (CORE_TRACE) HTTrace("Net Before.. calling %p (request %p, context %p)\n",
181: pres->before,
182: request, pres->param);
183: ret = (*pres->before)(request, pres->param, mode);
184: if (ret != HT_OK) break;
2.88 ! frystyk 185:
! 186: /*
! 187: ** Update the address to match against if the filter changed
! 188: ** the physical address.
! 189: */
! 190: if ((url = HTAnchor_physical(anchor))) addr = url;
2.79 frystyk 191: }
192: }
193: }
194: if (!url) HT_FREE(addr);
195: return ret;
196: }
197:
198: /*
199: ** Register a AFTER filter in the list provided by the caller.
200: ** Several filters can be registered in which case they are called
201: ** with the filter ordering in mind.
202: */
203: PUBLIC BOOL HTNetCall_addAfter (HTList * list, HTNetAfter * after,
204: const char * tmplate, void * param,
2.80 frystyk 205: int status, HTFilterOrder order)
2.79 frystyk 206: {
207: if (list && after) {
208: AfterFilter * me;
209: if ((me = (AfterFilter *) HT_CALLOC(1, sizeof(AfterFilter)))==NULL)
210: HT_OUTOFMEM("HTNetCall_addAfter");
211: me->after = after;
212: if (tmplate) StrAllocCopy(me->tmplate, tmplate);
213: me->order = check_order(order);
2.60 frystyk 214: me->param = param;
2.33 frystyk 215: me->status = status;
2.79 frystyk 216: if (CORE_TRACE)
217: HTTrace("Net After... Add %p with order %d tmplate `%s\' code %d context %p\n",
218: after, me->order, tmplate ? tmplate : "<null>", status, param);
219: return (HTList_addObject(list, me) &&
220: HTList_insertionSort(list, HTAfterOrder));
221: }
222: return NO;
223: }
224:
225: /*
226: ** Unregister all instances of an AFTER filter from a list.
227: */
228: PUBLIC BOOL HTNetCall_deleteAfter (HTList * list, HTNetAfter * after)
229: {
230: if (CORE_TRACE) HTTrace("Net After... Delete %p\n", after);
231: if (list && after) {
232: HTList * cur = list;
233: AfterFilter * pres;
234: while ((pres = (AfterFilter *) HTList_nextObject(cur))) {
235: if (pres->after == after) {
236: HTList_removeObject(list, (void *) pres);
237: HT_FREE(pres->tmplate);
238: HT_FREE(pres);
239: cur = list;
240: }
241: }
2.23 frystyk 242: }
243: return NO;
244: }
245:
2.79 frystyk 246: /*
247: ** Unregister all filters registered for a given status.
2.23 frystyk 248: */
2.79 frystyk 249: PUBLIC BOOL HTNetCall_deleteAfterStatus (HTList * list, int status)
2.23 frystyk 250: {
2.79 frystyk 251: if (CORE_TRACE) HTTrace("Net After... Delete all with status %d\n",status);
252: if (list) {
253: HTList * cur = list;
254: AfterFilter * pres;
255: while ((pres = (AfterFilter *) HTList_nextObject(cur))) {
256: if (pres->status == status) {
2.33 frystyk 257: HTList_removeObject(list, (void *) pres);
2.79 frystyk 258: HT_FREE(pres->tmplate);
2.49 frystyk 259: HT_FREE(pres);
2.79 frystyk 260: cur = list;
2.23 frystyk 261: }
262: }
2.79 frystyk 263: return YES;
2.23 frystyk 264: }
265: return NO;
266: }
2.1 frystyk 267:
2.79 frystyk 268: /*
269: ** Deletes all AFTER filters in list
2.23 frystyk 270: */
2.79 frystyk 271: PUBLIC BOOL HTNetCall_deleteAfterAll (HTList * list)
2.23 frystyk 272: {
2.79 frystyk 273: if (CORE_TRACE) HTTrace("Net After. Delete All filters\n");
2.33 frystyk 274: if (list) {
2.79 frystyk 275: HTList * cur = list;
276: AfterFilter * pres;
277: while ((pres = (AfterFilter *) HTList_nextObject(cur))) {
278: HT_FREE(pres->tmplate);
2.49 frystyk 279: HT_FREE(pres);
2.23 frystyk 280: }
2.33 frystyk 281: HTList_delete(list);
2.23 frystyk 282: return YES;
283: }
284: return NO;
285: }
286:
2.79 frystyk 287: /*
288: ** Call all the AFTER filters in the order specified at registration
289: ** time and if it has the right status code and it's not HT_IGNORE.
290: ** We also check for any template and whether it matches or not.
291: ** If a filter returns other than HT_OK then stop and return immediately.
292: ** Otherwise return what the last filter returns.
2.23 frystyk 293: */
2.79 frystyk 294: PUBLIC int HTNetCall_executeAfter (HTList * list, HTRequest * request,
295: int status)
2.23 frystyk 296: {
2.33 frystyk 297: int ret = HT_OK;
2.79 frystyk 298: if (status != HT_IGNORE) {
299: HTParentAnchor * anchor = HTRequest_anchor(request);
300: char * url = HTAnchor_physical(anchor);
301: char * addr = url ? url : HTAnchor_address((HTAnchor *) anchor);
302: HTResponse * response = HTRequest_response(request);
303: if (list && request && addr) {
304: AfterFilter * pres;
305: while ((pres = (AfterFilter *) HTList_nextObject(list))) {
306: if ((pres->status == status || pres->status == HT_ALL) &&
307: (!pres->tmplate ||
308: (pres->tmplate && HTStrMatch(pres->tmplate, addr)))) {
309: if (CORE_TRACE)
310: HTTrace("Net After... calling %p (request %p, response %p, status %d, context %p)\n",
311: pres->after, request, response,
312: status, pres->param);
313: ret = (*pres->after)(request, response, pres->param, status);
314: if (ret != HT_OK) break;
2.88 ! frystyk 315:
! 316: /*
! 317: ** Update the address to match against if the filter changed
! 318: ** the physical address.
! 319: */
! 320: if ((url = HTAnchor_physical(anchor))) addr = url;
2.79 frystyk 321: }
2.23 frystyk 322: }
323: }
2.79 frystyk 324: if (!url) HT_FREE(addr);
2.1 frystyk 325: }
2.33 frystyk 326: return ret;
327: }
328:
2.79 frystyk 329: /* ------------------------------------------------------------------------- */
330: /* GLOBAL BEFORE and AFTER filter Management */
331: /* ------------------------------------------------------------------------- */
332:
2.33 frystyk 333: /*
334: ** Global set of callback functions BEFORE the request is issued
335: ** list can be NULL
336: */
337: PUBLIC BOOL HTNet_setBefore (HTList *list)
338: {
339: HTBefore = list;
340: return YES;
341: }
342:
343: PUBLIC HTList * HTNet_before (void)
344: {
345: return HTBefore;
346: }
347:
2.79 frystyk 348: PUBLIC BOOL HTNet_addBefore (HTNetBefore * before, const char * tmplate,
2.80 frystyk 349: void * param, HTFilterOrder order)
2.33 frystyk 350: {
2.79 frystyk 351: if (!HTBefore) HTBefore = HTList_new();
352: return HTNetCall_addBefore(HTBefore, before, tmplate, param, order);
2.33 frystyk 353: }
354:
2.79 frystyk 355: PUBLIC BOOL HTNet_deleteBefore (HTNetBefore * cbf)
2.33 frystyk 356: {
2.79 frystyk 357: return HTNetCall_deleteBefore(HTBefore, cbf);
2.33 frystyk 358: }
359:
2.79 frystyk 360: /*
361: ** Call both the local and the global BEFORE filters (if any)
362: */
363: PUBLIC int HTNet_executeBeforeAll (HTRequest * request)
2.73 frystyk 364: {
2.79 frystyk 365: int ret;
366: BOOL override = NO;
367: HTList * befores;
368: if ((befores = HTRequest_before(request, &override))) {
369: if ((ret = HTNetCall_executeBefore(befores, request)) != HT_OK)
370: return ret;
371: }
372: return override ? HT_OK : HTNetCall_executeBefore(HTBefore, request);
2.73 frystyk 373: }
374:
2.33 frystyk 375: /*
376: ** Global set of callback functions AFTER the request is issued
377: ** list can be NULL
378: */
379: PUBLIC BOOL HTNet_setAfter (HTList *list)
380: {
381: HTAfter = list;
382: return YES;
383: }
384:
385: PUBLIC HTList * HTNet_after (void)
386: {
387: return HTAfter;
388: }
389:
2.79 frystyk 390: PUBLIC BOOL HTNet_addAfter (HTNetAfter * after, const char * tmplate,
2.80 frystyk 391: void * param, int status, HTFilterOrder order)
2.33 frystyk 392: {
2.79 frystyk 393: if (!HTAfter) HTAfter = HTList_new();
394: return HTNetCall_addAfter(HTAfter, after, tmplate, param, status, order);
395: }
2.58 hallam 396:
2.79 frystyk 397: PUBLIC BOOL HTNet_deleteAfter (HTNetAfter * cbf)
398: {
399: return HTNetCall_deleteAfter(HTAfter, cbf);
2.33 frystyk 400: }
401:
2.79 frystyk 402: PUBLIC BOOL HTNet_deleteAfterStatus (int status)
2.33 frystyk 403: {
2.79 frystyk 404: return HTNetCall_deleteAfterStatus(HTAfter, status);
2.73 frystyk 405: }
406:
2.79 frystyk 407: /*
408: ** Call both the local and the global AFTER filters (if any)
409: */
410: PUBLIC int HTNet_executeAfterAll (HTRequest * request, int status)
2.73 frystyk 411: {
2.79 frystyk 412: int ret;
413: BOOL override = NO;
414: HTList * afters;
415: if ((afters = HTRequest_after(request, &override))) {
416: if ((ret = HTNetCall_executeAfter(afters, request, status)) != HT_OK)
417: return ret;
418: }
419: return override ? HT_OK : HTNetCall_executeAfter(HTAfter, request, status);
2.1 frystyk 420: }
421:
2.23 frystyk 422: /* ------------------------------------------------------------------------- */
2.74 frystyk 423: /* Socket Management */
2.23 frystyk 424: /* ------------------------------------------------------------------------- */
2.1 frystyk 425:
2.74 frystyk 426: PUBLIC int HTNet_maxSocket (void)
427: {
428: return MaxActive;
429: }
430:
431: PUBLIC BOOL HTNet_setMaxSocket (int newmax)
432: {
433: if (newmax > 0) {
434: MaxActive = newmax;
435: return YES;
436: }
437: return NO;
438: }
439:
440: PUBLIC void HTNet_increaseSocket (void)
441: {
442: Active++;
2.87 frystyk 443: if (CORE_TRACE)
444: HTTrace("Net Manager. Increasing active sockets to %d, %d persistent sockets\n",
445: Active, Persistent);
2.74 frystyk 446: }
447:
448: PUBLIC void HTNet_decreaseSocket (void)
449: {
450: if (--Active < 0) Active = 0;
2.87 frystyk 451: if (CORE_TRACE)
452: HTTrace("Net Manager. Decreasing active sockets to %d, %d persistent sockets\n",
453: Active, Persistent);
2.74 frystyk 454: }
455:
456: PUBLIC int HTNet_availableSockets (void)
457: {
458: int available = MaxActive - Active;
459: return available > 0 ? available : 0;
460: }
461:
462: PUBLIC void HTNet_increasePersistentSocket (void)
463: {
464: Persistent++;
2.87 frystyk 465: if (CORE_TRACE)
466: HTTrace("Net Manager. %d active sockets, increasing persistent sockets to %d\n",
467: Active, Persistent);
2.74 frystyk 468: }
469:
470: PUBLIC void HTNet_decreasePersistentSocket (void)
2.23 frystyk 471: {
2.74 frystyk 472: if (--Persistent < 0) Persistent = 0;
2.87 frystyk 473: if (CORE_TRACE)
474: HTTrace("Net Manager. %d active sockets, decreasing persistent sockets to %d\n",
475: Active, Persistent);
2.23 frystyk 476: }
2.17 frystyk 477:
2.74 frystyk 478: PUBLIC int HTNet_availablePersistentSockets (void)
2.28 frystyk 479: {
2.74 frystyk 480: int available = MaxActive - 2 - Persistent;
481: return available > 0 ? available : 0;
2.28 frystyk 482: }
483:
2.74 frystyk 484: /*
485: ** Returns whether there are any Net objects pending or active
2.40 frystyk 486: */
2.74 frystyk 487: PUBLIC BOOL HTNet_isIdle (void)
2.40 frystyk 488: {
2.74 frystyk 489: return (HTNetCount > 0);
2.40 frystyk 490: }
491:
2.74 frystyk 492: PUBLIC BOOL HTNet_isEmpty (void)
2.1 frystyk 493: {
2.74 frystyk 494: return (HTNetCount <= 0);
2.1 frystyk 495: }
496:
2.81 frystyk 497: PUBLIC int HTNet_count (void)
498: {
499: return HTNetCount;
500: }
501:
2.23 frystyk 502: /* ------------------------------------------------------------------------- */
503: /* Creation and deletion methods */
504: /* ------------------------------------------------------------------------- */
505:
2.74 frystyk 506: PRIVATE HTNet * create_object (void)
507: {
508: static int net_hash = 0;
509: HTNet * me = NULL;
510:
511: /* Create new object */
512: if ((me = (HTNet *) HT_CALLOC(1, sizeof(HTNet))) == NULL)
513: HT_OUTOFMEM("HTNet_new");
514: me->hash = net_hash++ % HASH_SIZE;
515:
516: /* Insert into hash table */
517: if (!NetTable) {
518: if ((NetTable = (HTList **) HT_CALLOC(HASH_SIZE, sizeof(HTList *))) == NULL)
519: HT_OUTOFMEM("create_object");
520: }
521: if (!NetTable[me->hash]) NetTable[me->hash] = HTList_new();
522: HTList_addObject(NetTable[me->hash], (void *) me);
523: HTNetCount++;
524: if (CORE_TRACE)
525: HTTrace("Net Object.. %p created with hash %d\n",me, me->hash);
526: return me;
527: }
528:
2.27 frystyk 529: /* HTNet_duplicate
530: ** ---------------
531: ** Creates a new HTNet object as a duplicate of the same request.
532: ** Returns YES if OK, else NO
533: ** BUG: We do not check if we have a socket free!
534: */
2.43 frystyk 535: PUBLIC HTNet * HTNet_dup (HTNet * src)
2.27 frystyk 536: {
2.74 frystyk 537: if (src) {
538: HTNet * me;
2.75 frystyk 539: int hash;
2.74 frystyk 540: if ((me = create_object()) == NULL) return NULL;
2.75 frystyk 541: hash = me->hash;
2.74 frystyk 542: if (CORE_TRACE) HTTrace("Net Object.. Duplicated %p\n", src);
543: memcpy((void *) me, src, sizeof(HTNet));
2.75 frystyk 544: me->hash = hash; /* Carry over hash entry */
2.74 frystyk 545: return me;
546: }
547: return NULL;
2.27 frystyk 548: }
549:
2.81 frystyk 550: PUBLIC BOOL HTNet_execute (HTNet * net, HTEventType type)
551: {
552: if (net && net->event.cbf && net->request) {
553: if (CORE_TRACE)
2.82 frystyk 554: HTTrace("Net Object.. %p calling %p with event type %d and context %p\n",
555: net, net->event.cbf, type, net->event.param);
556: (*(net->event.cbf))(HTNet_socket(net), net->event.param, type);
2.81 frystyk 557: return YES;
558: }
559: return NO;
560: }
561:
2.74 frystyk 562: /*
563: ** Start a Net obejct by calling the protocol module.
2.30 frystyk 564: */
2.74 frystyk 565: PUBLIC BOOL HTNet_start (HTNet * net)
2.30 frystyk 566: {
2.81 frystyk 567: if (net && net->event.cbf && net->request) {
2.74 frystyk 568: if (CORE_TRACE) HTTrace("Net Object.. Launching %p\n", net);
2.81 frystyk 569: (*(net->event.cbf))(HTNet_socket(net), net->event.param, HTEvent_BEGIN);
2.30 frystyk 570: return YES;
571: }
572: return NO;
573: }
574:
2.37 frystyk 575: /* HTNet_new
576: ** ---------
577: ** This function creates a new HTNet object and assigns the socket number
578: ** to it. This is intended to be used when you are going to listen on a
579: ** socket using the HTDoListen() function in HTTCP.c. The function do NOT
580: ** call any of the callback functions.
581: ** Returns new object or NULL on error
582: */
2.81 frystyk 583: PUBLIC HTNet * HTNet_new (HTRequest * request)
2.37 frystyk 584: {
2.81 frystyk 585: HTNet * me;
586: if ((me = create_object()) == NULL) return NULL;
587: me->preemptive = HTRequest_preemptive(request);
588: HTNet_setEventPriority(me, HTRequest_priority(request));
589: me->request = request;
590: HTRequest_setNet(request, me);
591: return me;
2.37 frystyk 592: }
593:
2.67 frystyk 594: /* HTNet_newServer
595: ** ---------------
596: ** Create a new HTNet object as a new request to be handled. If we have
2.74 frystyk 597: ** more than MaxActive connections already then return NO.
2.67 frystyk 598: ** Returns YES if OK, else NO
2.37 frystyk 599: */
2.81 frystyk 600:
2.69 frystyk 601: PUBLIC BOOL HTNet_newServer (HTRequest * request, HTNet * net, char * access)
2.37 frystyk 602: {
603: HTProtocol * protocol;
2.67 frystyk 604: HTTransport * tp = NULL; /* added JTD:5/28/96 */
2.81 frystyk 605: HTProtCallback * cbf;
2.37 frystyk 606: if (!request) return NO;
607:
608: /* Find a protocol object for this access scheme */
2.46 frystyk 609: protocol = HTProtocol_find(request, access);
2.67 frystyk 610:
611: /* added - JTD:5/28/96 */
612: /* Find a transport object for this protocol */
613: tp = HTTransport_find(request, HTProtocol_transport(protocol));
614: if (tp == NULL) {
2.74 frystyk 615: if (CORE_TRACE) HTTrace("Net Object.. NO TRANSPORT OBJECT\n");
2.67 frystyk 616: return NO;
617: }
618: /* end of additions - JTD:5/28/96 */
619:
2.69 frystyk 620: /* Fill out the net object and bind it to the request object */
2.74 frystyk 621: net->preemptive = (HTProtocol_preemptive(protocol) || HTRequest_preemptive(request));
2.81 frystyk 622: #if 0
2.69 frystyk 623: net->protocol = protocol;
624: net->transport = tp; /* added - JTD:5/28/96 */
2.81 frystyk 625: #endif
626: net->event.priority = HTRequest_priority(request);
2.69 frystyk 627: net->request = request;
628: HTRequest_setNet(request, net);
2.81 frystyk 629: if (!(cbf = HTProtocol_server(protocol))) {
2.74 frystyk 630: if (CORE_TRACE) HTTrace("Net Object.. NO CALL BACK FUNCTION!\n");
2.67 frystyk 631: return NO;
2.37 frystyk 632: }
2.74 frystyk 633:
634: /* Increase the number of retrys for this download */
635: HTRequest_addRetry(request);
2.37 frystyk 636:
637: /* Start the server request */
2.58 hallam 638: if (CORE_TRACE)
2.74 frystyk 639: HTTrace("Net Object.. starting SERVER request %p with net object %p\n", request, net);
2.81 frystyk 640: (*(cbf))(HTNet_socket(net), request);
2.37 frystyk 641: return YES;
642: }
2.36 frystyk 643:
2.30 frystyk 644: /* HTNet_new
645: ** ---------
2.23 frystyk 646: ** Create a new HTNet object as a new request to be handled. If we have
2.74 frystyk 647: ** more than MaxActive connections already then put this into the
2.23 frystyk 648: ** pending queue, else start the request by calling the call back
649: ** function registered with this access method.
650: ** Returns YES if OK, else NO
651: */
2.37 frystyk 652: PUBLIC BOOL HTNet_newClient (HTRequest * request)
2.23 frystyk 653: {
2.74 frystyk 654: HTParentAnchor * anchor = HTRequest_anchor(request);
2.59 frystyk 655: HTNet * me = NULL;
656: HTProtocol * protocol = NULL;
657: HTTransport * tp = NULL;
2.37 frystyk 658: char * physical = NULL;
2.74 frystyk 659: int status;
2.81 frystyk 660: HTProtCallback * cbf;
661:
2.27 frystyk 662: if (!request) return NO;
2.33 frystyk 663: /*
664: ** First we do all the "BEFORE" callbacks in order to see if we are to
665: ** continue with this request or not. If we receive a callback status
666: ** that is NOT HT_OK then jump directly to the after callbacks and return
667: */
2.79 frystyk 668: if ((status = HTNet_executeBeforeAll(request)) != HT_OK) {
669: HTNet_executeAfterAll(request, status);
2.33 frystyk 670: return YES;
671: }
672:
2.36 frystyk 673: /*
2.79 frystyk 674: ** If no translation was provided by the filters then use the anchor
2.36 frystyk 675: ** address directly
676: */
2.74 frystyk 677: if (!(physical = HTAnchor_physical(anchor))) {
678: char * addr = HTAnchor_address((HTAnchor *) anchor);
679: if (CORE_TRACE) HTTrace("Net Object.. Using default address\n");
680: HTAnchor_setPhysical(anchor, addr);
681: physical = HTAnchor_physical(anchor);
2.49 frystyk 682: HT_FREE(addr);
2.33 frystyk 683: }
684:
2.37 frystyk 685: /* Find a protocol object for this access scheme */
686: {
2.81 frystyk 687: char * proxy = HTRequest_proxy(request);
688: char * access = HTParse(proxy ? proxy : physical, "", PARSE_ACCESS);
2.37 frystyk 689: if ((protocol = HTProtocol_find(request, access)) == NULL) {
2.74 frystyk 690: if (CORE_TRACE) HTTrace("Net Object.. NO PROTOCOL OBJECT\n");
2.49 frystyk 691: HT_FREE(access);
2.37 frystyk 692: return NO;
693: }
2.49 frystyk 694: HT_FREE(access);
2.37 frystyk 695: }
2.59 frystyk 696:
697: /* Find a transport object for this protocol */
698: tp = HTTransport_find(request, HTProtocol_transport(protocol));
699: if (tp == NULL) {
2.74 frystyk 700: if (CORE_TRACE) HTTrace("Net Object.. NO TRANSPORT OBJECT\n");
2.59 frystyk 701: return NO;
702: }
703:
2.23 frystyk 704: /* Create new net object and bind it to the request object */
2.69 frystyk 705: if ((me = create_object()) == NULL) return NO;
2.74 frystyk 706: me->preemptive = (HTProtocol_preemptive(protocol) || HTRequest_preemptive(request));
2.81 frystyk 707: #if 0
2.69 frystyk 708: me->priority = HTRequest_priority(request);
2.81 frystyk 709: #endif
710: HTNet_setEventPriority(me, HTRequest_priority(request));
2.59 frystyk 711: me->protocol = protocol;
712: me->transport = tp;
2.69 frystyk 713: me->request = request;
714: HTRequest_setNet(request, me);
2.81 frystyk 715: if (!(cbf = HTProtocol_client(protocol))) {
2.74 frystyk 716: if (CORE_TRACE) HTTrace("Net Object.. NO CALL BACK FUNCTION!\n");
2.49 frystyk 717: HT_FREE(me);
2.23 frystyk 718: return NO;
719: }
2.74 frystyk 720:
721: /* Increase the number of retrys for this download */
722: HTRequest_addRetry(request);
2.23 frystyk 723:
724: /*
725: ** Check if we can start the request, else put it into pending queue
726: ** If so then call the call back function associated with the anchor.
727: ** We use the INVSOC as we don't have a valid socket yet!
728: */
2.74 frystyk 729: if (CORE_TRACE)
730: HTTrace("Net Object.. starting request %p (retry=%d) with net object %p\n",
731: request, HTRequest_retrys(request), me);
2.81 frystyk 732: (*(cbf))(INVSOC, request);
2.36 frystyk 733: return YES;
734: }
735:
2.23 frystyk 736: /* delete_object
737: ** -------------
738: ** Deletes an HTNet object
2.40 frystyk 739: ** Return YES if OK, else NO
2.15 frystyk 740: */
2.81 frystyk 741: PRIVATE BOOL delete_object (HTNet * net)
2.15 frystyk 742: {
2.74 frystyk 743: if (CORE_TRACE) HTTrace("Net Object.. Remove object %p\n", net);
2.23 frystyk 744: if (net) {
745:
746: /* Close socket */
2.74 frystyk 747: /*
748: ** As we may have a socket available we check for whether
749: ** we can start any pending requests. We do this by asking for
750: ** pending Host objects. If none then use the current object
751: */
2.81 frystyk 752: /* (*net->input->isa->consumed)(net->input, net->header_length + net->bytes_read);
753: */
2.74 frystyk 754: HTHost_launchPending(net->host);
755:
756: /*
757: ** Break the link to the request and free the Net object
758: */
759: HTRequest_setNet(net->request, NULL);
2.49 frystyk 760: HT_FREE(net);
2.81 frystyk 761: return YES;
762: }
763: return NO;
764: }
765:
766: PRIVATE BOOL remove_net (HTNet * net)
767: {
768: if (net && NetTable) {
769: HTList * list = NetTable[net->hash];
770: if (list) {
771: HTList_removeObject(list, (void *) net);
772: delete_object(net);
773: HTNetCount--;
774: return YES;
775: }
2.23 frystyk 776: }
777: return NO;
778: }
779:
2.74 frystyk 780: /*
781: ** Clears the contents of the Net object so that we can use it again.
782: */
783: PUBLIC BOOL HTNet_clear (HTNet * net)
784: {
785: if (net) {
2.81 frystyk 786: net->host->channel = NULL;
787: net->readStream = NULL;
788: net->bytesRead = 0;
789: net->bytesWritten = 0;
790: net->headerLength = 0;
2.74 frystyk 791: return YES;
792: }
793: return NO;
794: }
795:
2.23 frystyk 796: /* HTNet_delete
797: ** ------------
798: ** Deletes the HTNet object from the list of active requests and calls
799: ** any registered call back functions IF not the status is HT_IGNORE.
800: ** This is used if we have internal requests that the app doesn't know
801: ** about. We also see if we have pending requests that can be started
802: ** up now when we have a socket free.
803: ** The callback functions are called in the reverse order of which they
804: ** were registered (last one first)
2.40 frystyk 805: ** Return YES if OK, else NO
2.23 frystyk 806: */
807: PUBLIC BOOL HTNet_delete (HTNet * net, int status)
808: {
2.58 hallam 809: if (CORE_TRACE)
2.74 frystyk 810: HTTrace("Net Object.. Delete %p and call AFTER filters\n", net);
811: if (net) {
812: HTRequest * request = net->request;
2.23 frystyk 813:
2.25 frystyk 814: /*
2.74 frystyk 815: ** If we have a premature close then recover the request. Otherwise
816: ** break the link to the Host object and continue deleting the net
817: ** object
2.25 frystyk 818: */
2.74 frystyk 819: if (net->host) {
2.81 frystyk 820: HTHost_unregister (net->host, net, HTEvent_READ);
821: HTHost_unregister (net->host, net, HTEvent_WRITE);
2.74 frystyk 822: if (status == HT_RECOVER_PIPE) {
823: HTNet_clear(net);
2.58 hallam 824: if (CORE_TRACE)
2.74 frystyk 825: HTTrace("Net Object.. Restarting request %p (retry=%d) with net object %p\n",
826: request, HTRequest_retrys(request), net);
827: return YES;
2.81 frystyk 828: }
829: HTHost_free(net->host, status);
830: HTHost_deleteNet(net->host, net);
2.74 frystyk 831: }
832:
833: /* Remove object from the table of Net Objects */
2.81 frystyk 834: remove_net(net);
2.82 frystyk 835:
2.74 frystyk 836: /* Call AFTER filters */
2.79 frystyk 837: HTNet_executeAfterAll(request, status);
2.74 frystyk 838: return YES;
2.23 frystyk 839: }
840: return NO;
841: }
842:
843: /* HTNet_deleteAll
844: ** ---------------
845: ** Deletes all HTNet object that might either be active or pending
2.74 frystyk 846: ** We DO NOT call the AFTER filters - A crude way of saying goodbye!
2.23 frystyk 847: */
848: PUBLIC BOOL HTNet_deleteAll (void)
849: {
2.58 hallam 850: if (CORE_TRACE)
2.74 frystyk 851: HTTrace("Net Object.. Remove all Net objects, NO filters\n");
852: if (NetTable) {
853: HTList * cur = NULL;
854: HTNet * pres = NULL;
855: int cnt;
856: for (cnt=0; cnt<HASH_SIZE; cnt++) {
857: if ((cur = NetTable[cnt])) {
858: while ((pres = (HTNet *) HTList_nextObject(cur)) != NULL)
2.81 frystyk 859: delete_object(pres);
2.74 frystyk 860: }
861: HTList_delete(NetTable[cnt]);
2.25 frystyk 862: }
2.74 frystyk 863: HT_FREE(NetTable);
864: HTNetCount = 0;
865: return YES;
2.25 frystyk 866: }
867: return NO;
868: }
869:
2.23 frystyk 870: /* HTNet_kill
871: ** ----------
872: ** Kill the request by calling the call back function with a request for
873: ** closing the connection. Does not remove the object. This is done by
874: ** HTNet_delete() function which is called by the load routine.
875: ** Returns OK if success, NO on error
876: */
2.74 frystyk 877: PUBLIC BOOL HTNet_kill (HTNet * net)
2.23 frystyk 878: {
2.81 frystyk 879: if (net) {
880: if (CORE_TRACE) HTTrace("Net Object.. Killing %p\n", net);
881: if (net->event.cbf) {
882: (*(net->event.cbf))(HTNet_socket(net), net->event.param, HTEvent_CLOSE);
883: return YES;
884: }
885: return remove_net(net);
2.23 frystyk 886: }
2.81 frystyk 887: if (CORE_TRACE) HTTrace("Net Object.. No object to kill\n");
2.23 frystyk 888: return NO;
889: }
890:
891: /* HTNet_killAll
892: ** -------------
2.74 frystyk 893: ** Kills all registered net objects by calling the call
2.23 frystyk 894: ** back function with a request for closing the connection. We do not
895: ** remove the HTNet object as it is done by HTNet_delete().
896: ** Returns OK if success, NO on error
897: */
898: PUBLIC BOOL HTNet_killAll (void)
899: {
2.74 frystyk 900: if (CORE_TRACE) HTTrace("Net Object.. Kill ALL Net objects!!!\n");
901: if (NetTable) {
902: HTList * cur = NULL;
903: HTNet * pres = NULL;
904: int cnt;
905: for (cnt=0; cnt<HASH_SIZE; cnt++) {
906: if ((cur = NetTable[cnt])) {
2.75 frystyk 907: while ((pres = (HTNet *) HTList_lastObject(cur)) != NULL)
2.74 frystyk 908: HTNet_kill(pres);
909: }
2.25 frystyk 910: }
2.74 frystyk 911: return YES;
2.25 frystyk 912: }
2.74 frystyk 913: if (CORE_TRACE) HTTrace("Net Object.. No objects to kill\n");
914: return NO;
2.23 frystyk 915: }
2.38 frystyk 916:
917: /* ------------------------------------------------------------------------- */
2.59 frystyk 918: /* Connection Specifics */
919: /* ------------------------------------------------------------------------- */
920:
2.74 frystyk 921: /* HTNet_priority
922: ** --------------
923: ** Get the current priority of the Net object
924: */
925: PUBLIC HTPriority HTNet_priority (HTNet * net)
926: {
2.81 frystyk 927: return (net ? net->event.priority : HT_PRIORITY_INV);
2.74 frystyk 928: }
929:
930: /* HTNet_setPriority
931: ** -----------------
932: ** Set the current priority of the Net object
933: ** This will change the priority next time the thread is blocked
934: */
935: PUBLIC BOOL HTNet_setPriority (HTNet * net, HTPriority priority)
936: {
937: if (net) {
2.81 frystyk 938: net->event.priority = priority;
2.74 frystyk 939: return YES;
940: }
941: return NO;
942: }
943:
2.59 frystyk 944: /* HTNet_Persistent
945: ** ----------------
946: ** Check whether the net object handles persistent connections
947: ** If we have a DNS entry then check that as well.
948: */
949: PUBLIC BOOL HTNet_persistent (HTNet * net)
950: {
951: return (net && HTHost_isPersistent(net->host));
952: }
953:
954: /* HTNet_persistent
955: ** ----------------
956: ** Set the net object to handle persistent connections
957: ** If we also have a DNS entry then update that as well
958: */
2.74 frystyk 959: PUBLIC BOOL HTNet_setPersistent (HTNet * net,
960: BOOL persistent,
961: HTTransportMode mode)
2.59 frystyk 962: {
963: if (net) {
2.81 frystyk 964: BOOL result = HTHost_setPersistent(net->host, persistent, mode);
2.74 frystyk 965: if (CORE_TRACE)
966: HTTrace("Net Object.. Persistent connection set %s %s\n",
967: persistent ? "ON" : "OFF",
968: result ? "succeeded" : "failed");
2.81 frystyk 969: return result;
2.59 frystyk 970: }
971: return NO;
972: }
973:
2.66 frystyk 974: /*
975: ** Context pointer to be used in context call back function
976: */
977: PUBLIC BOOL HTNet_setContext (HTNet * net, void * context)
978: {
979: if (net) {
980: net->context = context;
981: return YES;
982: }
983: return NO;
984: }
985:
986: PUBLIC void * HTNet_context (HTNet * net)
987: {
988: return net ? net->context : NULL;
989: }
2.38 frystyk 990:
991: /*
2.60 frystyk 992: ** Get and set the socket number
993: */
994: PUBLIC BOOL HTNet_setSocket (HTNet * net, SOCKET sockfd)
995: {
2.81 frystyk 996: if (net && net->host && net->host->channel) {
997: HTChannel_setSocket(net->host->channel, sockfd);
2.60 frystyk 998: return YES;
999: }
1000: return NO;
1001: }
1002:
1003: PUBLIC SOCKET HTNet_socket (HTNet * net)
1004: {
2.81 frystyk 1005: return (net && net->host && net->host->channel ? HTChannel_socket(net->host->channel) : INVSOC);
2.69 frystyk 1006: }
1007:
1008: /*
1009: ** Get and set the HTRequest object
1010: */
1011: PUBLIC BOOL HTNet_setRequest (HTNet * net, HTRequest * request)
1012: {
1013: if (net && request) {
1014: net->request = request;
1015: return YES;
1016: }
1017: return NO;
1018: }
1019:
1020: PUBLIC HTRequest * HTNet_request (HTNet * net)
1021: {
1022: return (net ? net->request : NULL);
2.60 frystyk 1023: }
1024:
1025: /*
2.59 frystyk 1026: ** Get and set the HTChannel object
2.55 frystyk 1027: */
2.59 frystyk 1028: PUBLIC BOOL HTNet_setChannel (HTNet * net, HTChannel * channel)
2.55 frystyk 1029: {
2.81 frystyk 1030: return (net && channel) ? HTHost_setChannel(net->host, channel) : NO;
2.55 frystyk 1031: }
1032:
2.59 frystyk 1033: PUBLIC HTChannel * HTNet_channel (HTNet * net)
2.55 frystyk 1034: {
2.81 frystyk 1035: return net ? HTHost_channel(net->host) : NULL;
2.59 frystyk 1036: }
1037:
1038: /*
1039: ** Get and set the HTHost object
1040: */
1041: PUBLIC BOOL HTNet_setHost (HTNet * net, HTHost * host)
1042: {
1043: if (net && host) {
1044: net->host = host;
1045: return YES;
1046: }
1047: return NO;
1048: }
1049:
1050: PUBLIC HTHost * HTNet_host (HTNet * net)
1051: {
1052: return (net ? net->host : NULL);
1053: }
1054:
1055: /*
1056: ** Get and set the HTdns object
1057: */
1058: PUBLIC BOOL HTNet_setDns (HTNet * net, HTdns * dns)
1059: {
1060: if (net && dns) {
2.81 frystyk 1061: net->host->dns = dns;
2.59 frystyk 1062: return YES;
1063: }
1064: return NO;
1065: }
1066:
1067: PUBLIC HTdns * HTNet_dns (HTNet * net)
1068: {
2.81 frystyk 1069: return (net ? net->host->dns : NULL);
2.59 frystyk 1070: }
1071:
2.81 frystyk 1072: PUBLIC BOOL HTNet_setProtocol (HTNet * net, HTProtocol * protocol)
1073: {
1074: if (net && protocol) {
1075: net->protocol = protocol;
1076: return YES;
1077: }
1078: return NO;
1079: }
1080:
1081: PUBLIC HTProtocol * HTNet_protocol (HTNet * net)
2.72 frystyk 1082: {
2.81 frystyk 1083: return (net ? net->protocol : NULL);
2.72 frystyk 1084: }
2.59 frystyk 1085:
2.86 frystyk 1086: PUBLIC BOOL HTNet_setTransport (HTNet * net, HTTransport * tp)
1087: {
1088: if (net && tp) {
1089: net->transport = tp;
1090: return YES;
1091: }
1092: return NO;
1093: }
1094:
1095: PUBLIC HTTransport * HTNet_transport (HTNet * net)
1096: {
1097: return (net ? net->transport : NULL);
1098: }
1099:
1100: PUBLIC BOOL HTNet_preemptive (HTNet * net)
1101: {
1102: return (net ? net->preemptive : NO);
1103: }
1104:
2.59 frystyk 1105: /*
1106: ** Create the output stream and bind it to the channel
1107: ** Please read the description in the HTIOStream module on the parameters
1108: */
2.81 frystyk 1109: PUBLIC HTOutputStream * HTNet_getOutput (HTNet * me, void * param, int mode)
2.59 frystyk 1110: {
2.81 frystyk 1111: if (me && me->host && me->host->channel && me->transport) {
1112: HTTransport * tp = me->transport;
1113: HTChannel * ch = me->host->channel;
1114: HTOutputStream * output = (*tp->output_new)(me->host, ch, param, mode);
2.74 frystyk 1115: HTChannel_setOutput(ch, output);
2.59 frystyk 1116: return output;
1117: }
2.81 frystyk 1118: if (CORE_TRACE) HTTrace("Host Object.. Can't create output stream\n");
2.59 frystyk 1119: return NULL;
2.81 frystyk 1120: }
1121:
2.87 frystyk 1122: PUBLIC HTEvent * HTNet_event (HTNet * net)
2.81 frystyk 1123: {
2.87 frystyk 1124: return net ? &net->event : NULL;
1125: }
1126:
1127: PUBLIC BOOL HTNet_setEventParam (HTNet * net, void * eventParam)
1128: {
2.81 frystyk 1129: if (net) return HTEvent_setParam(&net->event, eventParam);
1130: return NO;
1131: }
1132:
2.87 frystyk 1133: PUBLIC void * HTNet_eventParam (HTNet * net)
2.81 frystyk 1134: {
2.87 frystyk 1135: return net ? net->event.param : NULL;
2.81 frystyk 1136: }
1137:
1138: PUBLIC BOOL HTNet_setEventCallback(HTNet * net, HTEventCallback * cbf)
1139: {
1140: if (net) return HTEvent_setCallback(&net->event, cbf);
1141: return NO;
1142: }
1143:
1144: PUBLIC HTEventCallback * HTNet_eventCallback(HTNet * net)
1145: {
1146: return net->event.cbf;
1147: }
1148:
1149: PUBLIC BOOL HTNet_setEventPriority(HTNet * net, HTPriority priority)
1150: {
1151: if (net) return HTEvent_setPriority(&net->event, priority);
1152: return NO;
1153: }
1154:
1155: PUBLIC HTPriority HTNet_eventPriority(HTNet * net)
1156: {
1157: return net->event.priority;
1158: }
1159:
1160: PUBLIC HTStream * HTNet_readStream(HTNet * net)
1161: {
1162: if (!net) return NULL;
1163: return net->readStream;
1164: }
1165:
1166: PUBLIC BOOL HTNet_setReadStream (HTNet * net, HTStream * stream)
1167: {
1168: if (net) {
1169: net->readStream = stream;
1170: return YES;
1171: }
1172: return NO;
2.55 frystyk 1173: }
Webmaster