Annotation of libwww/Library/src/HTNet.c, revision 2.82

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

Webmaster