src/server/public/nsapi.h

Wed, 27 Nov 2024 23:00:07 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 27 Nov 2024 23:00:07 +0100
changeset 563
6ca97c99173e
parent 498
0d80f8a2b29f
permissions
-rw-r--r--

add TODO to use a future ucx feature

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
 *
 * THE BSD LICENSE
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution. 
 *
 * Neither the name of the  nor the names of its contributors may be
 * used to endorse or promote products derived from this software without 
 * specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef PUBLIC_NSAPI_H
#define PUBLIC_NSAPI_H

/*
 * File:        nsapi.h
 *
 * Description:
 *
 *      This file defines an interface for extending the server with
 *      in-process plug-ins.
 */

#ifdef __cplusplus
extern "C" {
#endif

/* NSAPI version defined by this header file */
#define NSAPI_VERSION 303

/* Define USE_NSAPI_VERSION to use a specific NSAPI version at compile time */
#ifdef USE_NSAPI_VERSION
#if USE_NSAPI_VERSION < 300 || USE_NSAPI_VERSION > NSAPI_VERSION
#error This header file does not support the requested NSAPI version
#else
#undef NSAPI_VERSION
#define NSAPI_VERSION USE_NSAPI_VERSION
#endif
#endif

/* --- Begin native platform configuration definitions --- */

#if !defined(XP_WIN32) && !defined(XP_UNIX)
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#define XP_WIN32
#else
#define XP_UNIX
#endif
#endif

#ifdef XP_UNIX
#define NSAPI_PUBLIC
#define ZERO(ptr, len) memset(ptr, 0, len)
#ifdef AIX
#define TCPLEN_T size_t
#endif
#ifdef HPUX
#define TCPLEN_T int
#endif
#ifndef TCPLEN_T
#define TCPLEN_T socklen_t
#endif
#endif /* XP_UNIX */

#ifdef XP_WIN32
#define NSAPI_PUBLIC __declspec(dllexport)
struct iovec {
    char *iov_base;
    unsigned iov_len;
};
#ifndef S_ISDIR
#define S_ISDIR(mode) ((mode & S_IFMT) == S_IFDIR)
#endif
#ifndef S_ISREG
#define S_ISREG(mode) ((mode & S_IFMT) == S_IFREG)
#endif
#ifndef S_ISLNK
#define S_ISLNK(x) (0)
#endif
#define caddr_t PCHAR
#define NEED_STRCASECMP
#define NEED_STRNCASECMP
#define ZERO(ptr, len) ZeroMemory(ptr, len)
#define TCPLEN_T int
#endif /* XP_WIN32 */

/* --- End native platform configuration definitions --- */

/* --- Begin miscellaneous definitions --- */

#define WS_TRUE 1
#define WS_FALSE 0

/* Used in some places as a length limit on error messages */
#define MAGNUS_ERROR_LEN 1024

/* Carriage return and line feed */
#define CR 13
#define LF 10
#ifdef XP_WIN32
#define ENDLINE "\r\n"
#else
#define ENDLINE "\n"
#endif

/* mime.types file identification line */
#define NCC_MT_MAGIC "#--Netscape Communications Corporation MIME Information"
#define NCC_MT_MAGIC_LEN 55

/* The character which separates extensions with cinfo_find */
#define CINFO_SEPARATOR '.'

/* The maximum length of a line in a mime.types file */
#define CINFO_MAX_LEN 1024

/* The maximum length of an error message */
#define MAX_ERROR_LEN 4096

/*
 * A warning is a minor mishap, such as a 404 being issued.
 */
#define LOG_WARN 0

/* 
 * A misconfig is when there is a syntax error or permission violation in
 * a config file.
 */
#define LOG_MISCONFIG 1

/* 
 * Security warnings are issued when authentication fails, or a host is
 * given a 403 return code.
 */
#define LOG_SECURITY 2

/*
 * A failure is when a request could not be fulfilled due to an internal
 * problem, such as a CGI script exiting prematurely, or a filesystem 
 * permissions problem.
 */
#define LOG_FAILURE 3

/*
 * A catastrophe is a fatal server error such as running out of
 * memory or processes, or a system call failing, or even a server crash. 
 * The server child cannot recover from a catastrophe.
 */
#define LOG_CATASTROPHE 4

/*
 * Informational message, of no concern.
 */
#define LOG_INFORM 5

/*
 * Internal diagnostic message.
 */
#define LOG_VERBOSE 6

// new
#define LOG_DEBUG 7

/*
 * The time format to use in the error log
 */
#define ERR_TIMEFMT "[%d/%b/%Y:%H:%M:%S]"


/* The fd you will get if you are reporting errors to SYSLOG */
#define ERRORS_TO_SYSLOG NULL

/* Return codes from file I/O routines */
#define IO_OKAY 1
#define IO_ERROR -1
#define IO_EOF 0
#define NETBUF_EOF      -1
#define NETBUF_ERROR    -2
#define NETBUF_FULL     -3

/* The disk page size on this machine. */
#define FILE_BUFFERSIZE 4096

#ifdef XP_UNIX

#define FILE_PATHSEP '/'
#define FILE_PARENT "../"

#elif defined(XP_WIN32)

#define FILE_PATHSEP '/'
#define FILE_PARENT "..\\"

#endif /* XP_WIN32 */

#define NET_INFINITE_TIMEOUT 0
#define NET_ZERO_TIMEOUT -1

#ifdef USE_REGEX
/* WILDPAT uses regular expressions */
#define WILDPAT_VALID(exp)              regexp_valid(exp)
#define WILDPAT_MATCH(str, exp)         regexp_match(str, exp)
#define WILDPAT_CMP(str, exp)           regexp_cmp(str, exp)
#define WILDPAT_CASECMP(str, exp)       regexp_casecmp(str, exp)
#define WILDPAT_USES_REGEXP              1
#else
/* WILDPAT uses shell expressions */
#define WILDPAT_VALID(exp)              shexp_valid(exp)
#define WILDPAT_MATCH(str, exp)         shexp_match(str, exp)
#define WILDPAT_CMP(str, exp)           shexp_cmp(str, exp)
#define WILDPAT_CASECMP(str, exp)       shexp_casecmp(str, exp)
#define WILDPAT_USES_SHEXP              1
#endif

/* Define return codes from WILDPAT_VALID */
#define NON_WILDPAT     -1              /* exp is ordinary string */
#define INVALID_WILDPAT -2              /* exp is an invalid pattern */
#define VALID_WILDPAT   1               /* exp is a valid pattern */

/* Define return codes from regexp_valid and shexp_valid */
#define NON_SXP         NON_WILDPAT     /* exp is an ordinary string */
#define INVALID_SXP     INVALID_WILDPAT /* exp is an invalid shell exp */
#define VALID_SXP       VALID_WILDPAT   /* exp is a valid shell exp */

#define NON_REGEXP      NON_SXP
#define INVALID_REGEXP  INVALID_SXP
#define VALID_REGEXP    VALID_SXP

#define SYSTHREAD_DEFAULT_PRIORITY 16

/* The longest line in the configuration file */
#define CONF_MAXLEN 16384

#define HTTP_DATE_LEN 30
#define HTTP_DATE_FMT "%a, %d %b %Y %T GMT"

/* HTTP status codes */
#define PROTOCOL_CONTINUE 100
#define PROTOCOL_SWITCHING 101
#define PROTOCOL_OK 200
#define PROTOCOL_CREATED 201
#define PROTOCOL_ACCEPTED 202
#define PROTOCOL_NONAUTHORITATIVE 203
#define PROTOCOL_NO_RESPONSE 204
#define PROTOCOL_NO_CONTENT 204
#define PROTOCOL_RESET_CONTENT 205
#define PROTOCOL_PARTIAL_CONTENT 206
#define PROTOCOL_MULTI_STATUS 207
#define PROTOCOL_MULTIPLE_CHOICES 300
#define PROTOCOL_MOVED_PERMANENTLY 301
#define PROTOCOL_REDIRECT 302
#define PROTOCOL_SEE_OTHER 303
#define PROTOCOL_NOT_MODIFIED 304
#define PROTOCOL_USE_PROXY 305
#define PROTOCOL_TEMPORARY_REDIRECT 307
#define PROTOCOL_BAD_REQUEST 400
#define PROTOCOL_UNAUTHORIZED 401
#define PROTOCOL_PAYMENT_REQUIRED 402
#define PROTOCOL_FORBIDDEN 403
#define PROTOCOL_NOT_FOUND 404
#define PROTOCOL_METHOD_NOT_ALLOWED 405
#define PROTOCOL_NOT_ACCEPTABLE 406
#define PROTOCOL_PROXY_UNAUTHORIZED 407
#define PROTOCOL_REQUEST_TIMEOUT 408
#define PROTOCOL_CONFLICT 409
#define PROTOCOL_GONE 410
#define PROTOCOL_LENGTH_REQUIRED 411
#define PROTOCOL_PRECONDITION_FAIL 412
#define PROTOCOL_ENTITY_TOO_LARGE 413
#define PROTOCOL_URI_TOO_LARGE 414
#define PROTOCOL_UNSUPPORTED_MEDIA_TYPE 415
#define PROTOCOL_REQUESTED_RANGE_NOT_SATISFIABLE 416
#define PROTOCOL_EXPECTATION_FAILED 417
#define PROTOCOL_LOCKED 423
#define PROTOCOL_FAILED_DEPENDENCY 424
#define PROTOCOL_SERVER_ERROR 500
#define PROTOCOL_NOT_IMPLEMENTED 501
#define PROTOCOL_BAD_GATEWAY 502
#define PROTOCOL_SERVICE_UNAVAILABLE 503
#define PROTOCOL_GATEWAY_TIMEOUT 504
#define PROTOCOL_VERSION_NOT_SUPPORTED 505
#define PROTOCOL_INSUFFICIENT_STORAGE 507

#define PROTOCOL_VERSION_HTTP09 9
#define PROTOCOL_VERSION_HTTP10 100
#define PROTOCOL_VERSION_HTTP11 101
#define CURRENT_PROTOCOL_VERSION PROTOCOL_VERSION_HTTP11

/* Definitions for HTTP over SSL */
#define HTTPS_PORT 443
#define HTTPS_URL "https"

/* Definitions for HTTP over TCP */
#define HTTP_PORT 80
#define HTTP_URL "http"


#define REQ_MAX_LINE 4096
    
/*
 * The REQ_ return codes. These codes are used to determine what the server
 * should do after a particular module completes its task.
 *
 * Func type functions return these as do many internal functions.
 */

/* The function performed its task, proceed with the request */
#define REQ_PROCEED 0
/* The entire request should be aborted: An error occurred */
#define REQ_ABORTED -1
/* The function performed no task, but proceed anyway */
#define REQ_NOACTION -2
/* Tear down the session and exit */
#define REQ_EXIT -3
/* Restart the entire request-response process */
#define REQ_RESTART -4
/* Too busy to execute this now */
#define REQ_TOOBUSY -5


/**** NSAPI extensions ****/

/* The function is still in progress (async extension) */
#define REQ_PROCESSING -8

/**** END NSAPI extensions ****/


/* --- End miscellaneous definitions --- */

/* --- Begin native platform includes --- */

#ifdef XP_UNIX
#include <unistd.h>
#include <sys/file.h>
#include <pthread.h>
#ifndef HPUX
#include <sys/select.h>
#endif
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <dirent.h>
#include <pwd.h>
#include <netinet/in.h>
#endif /* XP_UNIX */

#ifdef XP_WIN32
#include <wtypes.h>
#include <winbase.h>
#include <direct.h>
#include <dirent.h> // TODO: works only with mingw
#include <winsock.h>
#endif /* XP_WIN32 */

#include <sys/stat.h>
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <inttypes.h>

/* --- End native platform includes --- */

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

/* --- Begin type definitions --- */

/* NOTE: both SYS_FILE and SYS_NETFD are actually NSPR PRFileDesc * and can */
/*       be used with NSPR API calls (after casting them to PRFileDesc *) */

// NOTE: no they are not NSPR PRFileDesc*
//          they are VFSFile*
// TODO: fix NOTE

typedef int WSBool;

#ifndef SYS_FILE_T
typedef struct VFSFile *SYS_FILE;
#define SYS_FILE_T void *
#endif /* !SYS_FILE_T */

#define SYS_ERROR_FD ((SYS_FILE)-1) // TODO: fix

#ifndef SYS_NETFD_T
typedef void *SYS_NETFD;
#define SYS_NETFD_T void *
#endif /* !SYS_NETFD_T */

/* Error value for a SYS_NETFD */
#ifndef SYS_NET_ERRORFD
#define SYS_NET_ERRORFD ((SYS_NETFD)-1)
#endif /* !SYS_NET_ERRORFD */

/*
 * These structures were originally defined in nsapi.h, but those definitions
 * were removed in iPlanet Web Server 4.0.  The contents of these structures
 * are now private to the server implementation and must not be accessed
 * directly.  Instead, use the objset_*, object_*, directive_table_*, and
 * directive_* accessor functions.
 */
typedef struct directive directive;
typedef struct dtable dtable;
typedef struct httpd_object httpd_object;
typedef struct httpd_objset httpd_objset;

/*
 * Type:        filebuffer, filebuf_t
 *
 * Description:
 *
 *      This structure is used to represent a buffered file.  On some
 *      systems the file may be memory-mapped.  A filebuffer is created
 *      by filebuf_open(), and destroyed by filebuf_close().
 *
 * Notes:
 *
 *      Direct access to the members of this structure, not using
 *      macros defined here, is discouraged.
 *
 *      The filebuf alias that used to be defined for this type was
 *      found to conflict with a C++ class of the same name, so it
 *      has been renamed to filebuf_t.
 */
typedef struct filebuffer filebuffer;

/* Version of filebuffer when memory-mapped files are supported */
struct filebuffer {
    SYS_FILE fd;
#ifdef XP_WIN32
    HANDLE fdmap;
#endif
    caddr_t fp;
    int len;

    unsigned char *inbuf;
    int cursize;

    int pos;
    const char *errmsg;
};

/* Return next character or IO_EOF */
#define filebuf_getc(b) ((b)->pos == (b)->len ? IO_EOF : (int)((b)->fp)[(b)->pos++])

#define filebuf_iseof(b) ((b)->pos == (b)->len)

/* C++ streamio defines a filebuf class. */
typedef filebuffer filebuf_t;

#ifdef XP_WIN32
/* Use a filebuffer to read data from a pipe */
#define pipebuf_getc(b) \
 ((b)->pos != (b)->cursize ? (int)((b)->inbuf[(b)->pos++]) : pipebuf_next(b,1))
#endif /* XP_WIN32 */

/*
 * Type:        netbuf
 *
 * Description:
 *
 *      This structure is used to represent a buffered network socket.
 *      It is created by netbuf_open(), and destroyed by netbuf_close().
 *
 * Notes:
 *
 *      Direct access to the members of this structure, not using
 *      macros defined here, is discouraged.
 *
 *      The inbuf field used to be (unsigned char *), but is now
 *      simply (char *).  The value returned by the netbuf_getc()
 *      macro is (int).
 */
typedef struct netbuf netbuf;
struct netbuf {
    SYS_NETFD sd;

    int pos, cursize, maxsize, rdtimeout;
#ifdef XP_WIN32
    CHAR address[64];
#endif /* XP_WIN32 */
    unsigned char *inbuf;
    char *errmsg;
#ifndef XP_WIN32
    char address[64];
#endif /* !XP_WIN32 */
};

/*
 * netbuf_getc gets a character from the given network buffer and returns 
 * it (as an integer).
 * 
 * It will return (int) IO_ERROR for an error and (int) IO_EOF for
 * an error condition or EOF respectively.
 */
#define netbuf_getc(b) \
 ((b)->pos != (b)->cursize ? (int)((b)->inbuf[(b)->pos++]) : netbuf_next(b,1))

/*
 * buffer_error returns the last error that occurred with buffer.  Don't use
 * this unless you know an error occurred.  Independent of network/file type.
 */
#define buffer_error(b) ((b)->errmsg)

/*
 * Type:        sendfiledata
 *
 * Description:
 *
 *      This structure is used to pass arguments to the net_sendfile()
 *      function.  offset and len may be set to 0 to transmit a file in its
 *      entirety.
 */
typedef struct sendfiledata sendfiledata;
struct sendfiledata {
    SYS_FILE fd;         /* file to send */
    off_t  offset;       /* offset in file to start sending from */
    size_t len;          /* number of bytes to send from file */
    const void *header;  /* data to send before file */
    int hlen;            /* number of bytes to send before file */
    const void *trailer; /* data to send after file */
    int tlen;            /* number of bytes to send after file */
};

/*
 * Type:        NSAPIIOVec
 *
 * Description:
 *
 *      This structure is used to pass arguments to the net_writev()
 *      and FilterWritevFunc() functions.  
 */
typedef struct iovec NSAPIIOVec;

/*
//ifdef _LP64
typedef struct NSAPIIOVec NSAPIIOVec;
struct NSAPIIOVec {
    char *iov_base;
    int iov_len;
};
//else
typedef struct iovec NSAPIIOVec;
//endif
*/


/*
 * Type:        cinfo
 *
 * Description:
 *
 *      This is a structure that captures the information in the name/value
 *      pairs on one line of a mime.types file.  A cinfo structure is
 *      stored in the memory-resident database, indexed by each of the
 *      file extensions specified in the "exts" name/value pair.  It
 *      defines various attributes of resources with names containing
 *      the specified file extensions.
 *
 * Notes:
 *
 *      Pointers to cinfo structures returned by this API may or may not
 *      need to freed by the caller.  See the individual function
 *      descriptions.
 *
 *      The strings referenced by the fields of cinfo structures returned
 *      by this API should be considered read-only, and do not need to be
 *      freed by the caller, even when the cinfo structure does.
 */
typedef struct cinfo cinfo;
struct cinfo {
    char *type;
    char *encoding;
    char *language;
};


// nsapi ext begin

#define EVENT_POLLIN   0x1
#define EVENT_POLLOUT  0x2

typedef struct EventHandler EventHandler;
typedef struct Event        Event;

typedef int(*eventfunc)(EventHandler*, Event*);

struct Event {
    eventfunc   fn;
    eventfunc   finish;
    void        *cookie;
    intptr_t    object;
    int         events;
    int         error;
};

// nsapi ext end


typedef void* CONDVAR;
typedef void *COUNTING_SEMAPHORE;
typedef void* CRITICAL;

#ifdef XP_UNIX
typedef DIR* SYS_DIR;
typedef struct dirent SYS_DIRENT;
#endif /* XP_UNIX */

#ifdef XP_WIN32

typedef struct dirent_s dirent_s;
struct dirent_s {
    char *d_name;
};

typedef struct dir_s dir_s;
struct dir_s {
    HANDLE dp;
    WIN32_FIND_DATA fdata;
    dirent_s de;
};

typedef dir_s* SYS_DIR;
typedef dirent_s SYS_DIRENT;

#endif /* XP_WIN32 */

typedef struct pb_param pb_param;
struct pb_param {
    char *name,*value;
};

typedef struct pb_entry pb_entry;
struct pb_entry {
    pb_param *param;
    struct pb_entry *next;
};

typedef struct pblock pblock;
struct pblock {
    int hsize;
    struct pb_entry **ht;
};

#ifndef POOL_HANDLE_T
#define POOL_HANDLE_T
typedef void *pool_handle_t;
#endif

#ifndef SEMAPHORE_T
// TODO: fix
//typedef void *SEMAPHORE;
//#define SEMAPHORE_T void *
#endif /* !SEMAPHORE_T */

#define SESSION_HASHSIZE 5

typedef struct PListStruct_s PListStruct_s;
typedef struct ACLListHandle ACLListHandle;
typedef struct VFS           VFS;
typedef struct VFSContext    VFSContext;

enum WSConfigNodeType {
    WS_CONFIG_NODE_OBJECT = 0,
    WS_CONFIG_NODE_DIRECTIVE
};

typedef struct ServerConfiguration ServerConfiguration;
typedef struct ConfigNode          WSConfigNode;
typedef enum WSConfigNodeType      WSConfigNodeType;

// new
typedef struct HttpResponseWriter  HttpResponseWriter;

#ifndef PR_AF_INET
typedef union PRNetAddr PRNetAddr;
#endif

typedef struct Session Session;
typedef struct Request Request;
struct Session {
    pblock *client;     /* client-specific information */

    SYS_NETFD csd;      /* client file descriptor */
    netbuf *inbuf;      /* input buffer */
    int csd_open;

    struct in_addr iaddr;

    pool_handle_t *pool;
    
    EventHandler *ev; /* event handler instance (new) */

    void *clauth;       /* v2 ACL client authentication information */
    struct Session *next;
    int fill;
    struct sockaddr_in local_addr;      /* local addr for this session */

    PListStruct_s *subject;
    int ssl;            /* 0 = SSL OFF, 1 = SSL ON */
    int clientauth;     /* 0 = client auth OFF, 1 = client auth ON */

    PRNetAddr *pr_client_addr;
    PRNetAddr *pr_local_addr;
};


typedef struct FuncStruct FuncStruct;

/*
 * FuncPtr is a pointer to an NSAPI SAF function
 */

// TODO: fix typedefs
#ifdef XP_UNIX
typedef int Func(pblock *, Session *, Request *);
// new func executor
typedef int FuncExec(FuncStruct *, pblock *, Session *, Request *);
#else /* XP_WIN32 */
typedef int _cdecl Func(pblock *, Session *, Request *);
typedef int _cdecl FuncExec(FuncStruct *, pblock *, Session *, Request *);
#endif /* XP_WIN32 */

typedef Func     *FuncPtr;
typedef FuncExec *FuncExecPtr;

/*
 * FuncStruct is a structure used in the static declaration of the 
 * functions.  This static declaration is parsed into a hash table at 
 * startup.
 */

struct FuncStruct {
    const char  *name;
    FuncPtr     func;
    //struct FuncStruct *next;
    FuncExecPtr func_exec;
    void        *exec_data;
    unsigned flags;
    unsigned poolID;
    unsigned pool_resolved;
};

////// new

typedef struct _http_listener HttpListener;

typedef struct ResourcePool ResourcePool;

typedef struct ResourceType ResourceType;
typedef struct ResourceData ResourceData;

typedef void * (*resource_pool_init_func)(pool_handle_t *, const char *, pblock *);
typedef void   (*resource_pool_destroy_func)(void *); 
typedef void * (*resource_pool_createresource_func)(void *);
typedef void   (*resource_pool_freeresource_func)(void *, void *);
typedef int    (*resource_pool_prepare_func)(void *, void *);
typedef int    (*resource_pool_finish_func)(void *, void *);
typedef void * (*resource_pool_getresourcedata_func)(void *);

struct ResourceType {
    void * (*init)(pool_handle_t *, const char *, pblock *);
    void   (*destroy)(void *);
    void * (*createresource)(void *);
    void   (*freeresource)(void *, void *);
    int    (*prepare)(void *, void *);
    int    (*finish)(void *, void *);
    void * (*getresourcedata)(void *);
};

struct ResourceData {
    ResourcePool *resourcepool;
    void *data;
};

int resourcepool_register_type(const char *type_name, ResourceType *type_info);

//////
/*
 * VSInitFunc, VSDestroyFunc, VSDirectiveInitFunc and VSDirectiveDestroyFunc
 */
typedef struct VirtualServer VirtualServer;
typedef int VSInitFunc(VirtualServer *incoming, const VirtualServer *current);
typedef void VSDestroyFunc(VirtualServer *outgoing);
typedef VSInitFunc *VSInitFuncPtr;
typedef VSDestroyFunc *VSDestroyFuncPtr;
typedef int VSDirectiveInitFunc(const directive *dir, VirtualServer *incoming, const VirtualServer *current);
typedef void VSDirectiveDestroyFunc(const directive *dir, VirtualServer *outgoing);
typedef VSDirectiveInitFunc *VSDirectiveInitFuncPtr;
typedef VSDirectiveDestroyFunc *VSDirectiveDestroyFuncPtr;

/*
 * Filter is an opaque filter identifier
 */
typedef struct Filter Filter;

/*
 * FilterContext stores context associated with a particular filter layer
 */

typedef struct FilterContext FilterContext;

struct FilterContext {
    pool_handle_t *pool; /* pool context was allocated from */
    Session *sn;         /* session being processed */
    Request *rq;         /* request being processed */
    void *data;          /* filter-defined private data */
};

/*
 * FilterLayer represents one layer of a filter stack
 */

typedef struct FilterLayer FilterLayer;

struct FilterLayer {
    Filter *filter;         /* the filter at this layer in the filter stack */
    FilterContext *context; /* context for the filter */
    SYS_NETFD lower;        /* access to the next filter layer in the stack */
};

/*
 * Function prototypes for filter methods
 */

/*
 * TODO: modified: added * bevor Filter...Func
 */
typedef int (*FilterInsertFunc)(FilterLayer *layer, pblock *pb);
typedef void (*FilterRemoveFunc)(FilterLayer *layer);
typedef int (*FilterFlushFunc)(FilterLayer *layer);
typedef int (*FilterReadFunc)(FilterLayer *layer, void *buf, int amount, int timeout);
typedef int (*FilterWriteFunc)(FilterLayer *layer, const void *buf, int amount);
typedef int (*FilterWritevFunc)(FilterLayer *layer, const NSAPIIOVec *iov, int iov_size);
typedef int (*FilterSendfileFunc)(FilterLayer *layer, sendfiledata *sfd);

/*
 * FilterMethods is passed to filter_create() to declare the filter methods for
 * a new filter.  Each instance of the FilterMethods structure should be
 * initialized using the FILTER_METHODS_INITIALIZER macro.
 */

typedef struct FilterMethods FilterMethods;

struct FilterMethods {
    size_t size;
#if NSAPI_VERSION >= 302
    FilterInsertFunc *insert;
    FilterRemoveFunc *remove;
    FilterFlushFunc *flush;
    FilterReadFunc *read;
    FilterWriteFunc *write;
    FilterWritevFunc *writev;
    FilterSendfileFunc *sendfile;
#else
    void *reserved1;
    void *reserved2;
    void *reserved3;
    void *reserved4;
    void *reserved5;
    void *reserved6;
    void *reserved7;
#endif /* NSAPI_VERSION >= 302 */
};

#define FILTER_METHODS_INITIALIZER \
{                                  \
    sizeof(FilterMethods),         \
    NULL, /* insert */             \
    NULL, /* remove */             \
    NULL, /* flush */              \
    NULL, /* read */               \
    NULL, /* write */              \
    NULL, /* writev */             \
    NULL  /* sendfile */           \
}

/*
 * Filter order definitions for filter_create()
 */
#define FILTER_CONTENT_GENERATION       0xf0000
#define FILTER_CONTENT_TRANSLATION_HIGH 0xa0000
#define FILTER_CONTENT_TRANSLATION      0x90000
#define FILTER_CONTENT_TRANSLATION_LOW  0x80000
#define FILTER_CONTENT_CODING           0x50000
#define FILTER_TRANSFER_CODING          0x40000
#define FILTER_MESSAGE_CODING           0x30000
#define FILTER_TRANSPORT_CODING         0x20000
#define FILTER_NETWORK                  0x10000

typedef struct shmem_s shmem_s;
struct shmem_s {
    void *data;   /* the data */
#ifdef XP_WIN32
    HANDLE fdmap;
#endif /* XP_WIN32 */
    int size;     /* the maximum length of the data */

    char *name;   /* internal use: filename to unlink if exposed */
    SYS_FILE fd;  /* internal use: file descriptor for region */
};

/* Define a handle for a thread */
//typedef void* SYS_THREAD;

#ifdef XP_UNIX
typedef pthread_t SYS_THREAD;
#else
typedef void*     SYS_THREAD;
#endif

/* Define an error value for the thread handle */
#define SYS_THREAD_ERROR NULL


typedef struct conf_global_vars_s conf_global_vars_s;
struct conf_global_vars_s {

    /* What port we listen to */
    int Vport;                                  /* OBSOLETE */
#define server_portnum 80

    /* What address to bind to */
    char *Vaddr;                                /* OBSOLETE */

    /* User to run as */
    struct passwd *Vuserpw;

    /* Directory to chroot to */
    char *Vchr;

    /* Where to log our pid to */
    char *Vpidfn;

#define pool_max conf_getglobals()->Vpool_max
    int Vpool_max;
#define pool_min conf_getglobals()->Vpool_min
    int Vpool_min;                              /* OBSOLETE */
#define pool_life conf_getglobals()->Vpool_life
    int Vpool_life;                             /* OBSOLETE */

    /* For multiprocess UNIX servers, the maximum threads per process */
#define pool_maxthreads conf_getglobals()->Vpool_maxthreads
    int Vpool_maxthreads;

#define pool_minthreads conf_getglobals()->Vpool_minthreads
    int Vpool_minthreads;                       /* OBSOLETE */

    char *Vsecure_keyfn;                        /* OBSOLETE */
    char *Vsecure_certfn;                       /* OBSOLETE */

#define security_active 0
    int Vsecurity_active;
    int Vssl3_active;                           /* OBSOLETE */
    int Vssl2_active;                           /* OBSOLETE */
    int Vsecure_auth;                           /* OBSOLETE */
    int Vsecurity_session_timeout;
    long Vssl3_session_timeout;

    /* The server's hostname as should be reported in self-ref URLs */
#define server_hostname "x4" // TODO: fix
    char *Vserver_hostname;

    /* The main object from which all are derived */
#define root_object conf_getglobals()->Vroot_object
    char *Vroot_object;

    /* The object set the administrator has asked us to load */
#define std_os conf_getglobals()->Vstd_os
    httpd_objset *Vstd_os;

    /* The root of ACL data structures */
    void *Vacl_root;

    /* The main error log, where all errors are logged */
    char *Vmaster_error_log;

    /* The server root directory (contains instance subdirectories) */
#define server_root conf_getglobals()->Vserver_root
    char *Vserver_root;

    /* This server's id */
#define server_id conf_getglobals()->Vserver_id
    char *Vserver_id;

    /* Admin server users file */
    char *Vadmin_users;

    /* The installation directory (contains bin and lib subdirectories) */
    char *Vnetsite_root;

    /* Digest authentication stale nonce timeout value */
    int digest_stale_timeout;

    int single_accept;          /* OBSOLETE */
    int num_keep_alives;        /* OBSOLETE */
    int log_verbose;            /* OBSOLETE */
    int mmap_flags;             /* OBSOLETE */
    int mmap_prots;             /* OBSOLETE */
    int unused1;
    int unused2;

    /* Begin Enterprise 3.0 fields */
    int accept_language;        /* turn accept-language on/off */

    char *mtahost;
    char *nntphost;             /* OBSOLETE */

    /* The root of ACL data structures */
    void *Vacl_root_30;

    char *agentFilePath;        /* OBSOLETE */

    int Allowed;                /* OBSOLETE */

    pblock *genericGlobals;     /* OBSOLETE */

    char *agentsACLFile;        /* OBSOLETE */

    int wait_for_cgi;           /* OBSOLETE */
    int cgiwatch_timeout;       /* OBSOLETE */
    int started_by_watchdog;
    int restarted_by_watchdog;
    int old_accel_cache_enabled; /* OBSOLETE */
    int Vssl_cache_entries;
    int blocking_listen_socket; /* OBSOLETE */
    pblock **initfns;
    char *vs_config_file;       /* OBSOLETE */
};

/* Type used for Request rq_attr bit flags */
#ifdef AIX
#define RQATTR unsigned
#else
#define RQATTR unsigned long
#endif

struct Request {
    /* Server working variables */
    pblock *vars;

    /* The method, URI, and protocol revision of this request */
    pblock *reqpb;
    /* Protocol specific headers */
    int loadhdrs;
    pblock *headers;

    /* Server's response headers */
    int senthdrs;
    pblock *srvhdrs;

    /* The object set constructed to fulfill this request */
    httpd_objset *os;
    /* Array of objects that were created from .nsconfig files */
    httpd_objset *tmpos;

    /* The stat last returned by request_stat_path */
    char *statpath;
    char *staterr;
    struct stat *finfo;

    /* access control state */
    int aclstate;               /* ACL decision state */
    int acldirno;               /* deciding ACL directive number */
    char *aclname;              /* name of deciding ACL */
    pblock *aclpb;              /* OBSOLETE */
    /* 3.0 ACL list pointer */
    ACLListHandle *acllist;
    uint32_t aclreqaccess; /* new - required access rights */
    
    VFS *vfs; /* new - virtual file system */
    
    void *davCollection;

    int request_is_cacheable;   /* */
    int directive_is_cacheable; /* set by SAFs with no external side effects that make decisions based solely on URI and path */

    char *cached_headers;       /* OBSOLETE */
    int cached_headers_len;     /* OBSOLETE */
    char *unused;

    /* HTTP/1.1 features */
#define REQ_TIME(x)             (x)->req_start
    time_t req_start;           /* time request arrived - used for selecting weak or strong cache validation */
    short protv_num;            /* protocol version number */
    short method_num;           /* method number */
    struct rq_attr {
        RQATTR abs_uri:1;               /* set if absolute URI was used */
        RQATTR chunked:1;               /* chunked transfer-coding */
        RQATTR keep_alive:1;            /* connection keep-alive */
        RQATTR pipelined:1;             /* request packet is pipelined */
        RQATTR internal_req:1;          /* this was an internal request */
        RQATTR perm_req:1;              /* don't FREE() this request */
        RQATTR header_file_present:1;   /* OBSOLETE */
        RQATTR footer_file_present:1;   /* OBSOLETE */
        RQATTR jvm_attached:1;          /* OBSOLETE */
        RQATTR req_restarted:1;         /* request was restarted */
        RQATTR jvm_request_locked:1;    /* used for first-request serialization on some platforms */
        RQATTR default_type_set:1;      /* set if default types were set using set-default-type objecttype function */
        RQATTR is_web_app:1;            /* OBSOLETE */
        RQATTR ssl_unclean_shutdown:1;  /* set if browser requires unclean SSL shutdown */
        RQATTR vary_accept_language:1;  /* set if request was restarted based on an accept-language header */
        RQATTR reserved:17;             /* if you add a flag, make sure to subtract from this */
    } rq_attr;
    char *hostname;             /* hostname used to access server (always non-NULL) */
    int allowed;                /* OBSOLETE */
    int byterange;              /* OBSOLETE */
    short status_num;           /* HTTP status code */

    int staterrno;              /* used for rqstat */
    Request *orig_rq;           /* original Request - used for internal redirects */
};

/* Request attribute macros */
#define ABS_URI(x)                      (x)->rq_attr.abs_uri
#define CHUNKED(x)                      (x)->rq_attr.chunked
#define KEEP_ALIVE(x)                   (x)->rq_attr.keep_alive 
#define PIPELINED(x)                    (x)->rq_attr.pipelined 
#define INTERNAL_REQUEST(x)             (x)->rq_attr.internal_req 
#define RESTARTED_REQUEST(x)            (x)->rq_attr.req_restarted
#define PERM_REQUEST(x)                 (x)->rq_attr.perm_req
#define JVM_REQUEST_LOCKED(x)           (x)->rq_attr.jvm_request_locked
#define SSL_UNCLEAN_SHUTDOWN(x)         (x)->rq_attr.ssl_unclean_shutdown
#define VARY_ACCEPT_LANGUAGE(x)         (x)->rq_attr.vary_accept_language

/* Define methods for HTTP/1.1 */
#define METHOD_HEAD     0
#define METHOD_GET      1
#define METHOD_PUT      2
#define METHOD_POST     3
#define METHOD_DELETE   4
#define METHOD_TRACE    5
#define METHOD_OPTIONS  6
/* The following methods are Netscape method extensions */
#define METHOD_MOVE     7
#define METHOD_INDEX    8
#define METHOD_MKDIR    9
#define METHOD_RMDIR    10
#define METHOD_COPY     11
#define METHOD_MAX      12      /* Number of methods available on this server */

#define ISMGET(r)       ((r)->method_num == METHOD_GET)
#define ISMHEAD(r)      ((r)->method_num == METHOD_HEAD)
#define ISMPUT(r)       ((r)->method_num == METHOD_PUT)
#define ISMPOST(r)      ((r)->method_num == METHOD_POST)
#define ISMDELETE(r)    ((r)->method_num == METHOD_DELETE)
#define ISMMOVE(r)      ((r)->method_num == METHOD_MOVE)
#define ISMINDEX(r)     ((r)->method_num == METHOD_INDEX)
#define ISMMKDIR(r)     ((r)->method_num == METHOD_MKDIR)
#define ISMRMDIR(r)     ((r)->method_num == METHOD_RMDIR)
#define ISMCOPY(r)      ((r)->method_num == METHOD_COPY)
#define ISMTRACE(r)     ((r)->method_num == METHOD_TRACE)
#define ISMOPTIONS(r)   ((r)->method_num == METHOD_OPTIONS)


// new types
typedef struct aiocb_s {
    SYS_FILE     filedes;
    void         *buf;
    size_t       nbytes;
    off_t        offset;
    ssize_t      result;
    int          result_errno;
    Event        *event;
    EventHandler *evhandler;
} aiocb_s;


typedef struct _thread_pool       threadpool_t;
typedef struct  _threadpool_job   threadpool_job;
typedef void*(*job_callback_f)(void *data);


typedef struct WebSocket WebSocket;
typedef struct WSMessage WSMessage;

struct WebSocket {
    int (*on_open)(WebSocket *);
    int (*on_error)(WebSocket *);
    int (*on_message)(WebSocket *, WSMessage *msg);
    int (*on_close)(WebSocket *);
    void *userdata;
};

struct WSMessage {
    /*
     * message data (text or binary)
     */
    char      *data;
    
    /*
     * data length
     */
    size_t    length;
    
    /*
     * message type (opcode)
     * 0x0: continuation
     * 0x1: text
     * 0x2: binary
     * 0x3-7: reserved non-control frame
     * 0x8: close
     * 0x9: ping
     * 0xa: pong
     * 0xb-f: reserved control frame
     */
    int       type;
    
    /*
     * if the message is incomplete, next points to the continuation message
     */
    WSMessage *next;
};

/* --- End type definitions --- */

/* --- Begin dispatch vector table definition --- */
/* --- End dispatch vector table definition --- */

/* --- Begin API macro definitions --- */

#ifndef INTNSAPI

#if NSAPI_VERSION >= 301

/*
 * In Sun ONE Web Server 6.1 and higher, http_parse_request("", NULL, NULL)
 * returns the NSAPI version.  In previous releases, it returns -1.
 */
//define __NSAPI_RUNTIME_VERSION \
//        ((*__nsapi30_table->f_http_parse_request)("", NULL, NULL))

// new:
#define __NSAPI_RUNTIME_VERSION nsapi_runtime_version()

/*
 * NSAPI_RUNTIME_VERSION returns the NSAPI version the server implements.  The
 * returned NSAPI version number is reliable only in iPlanet Web Server 6.0,
 * Netscape Enterprise Server 6.0, and Sun ONE Web Server 6.0 and higher.
 */
//define NSAPI_RUNTIME_VERSION \
//        (__NSAPI_RUNTIME_VERSION > 0 ? __NSAPI_RUNTIME_VERSION : 301)

// new:
#define NSAPI_RUNTIME_VERSION nsapi_runtime_version()

#endif /* NSAPI_VERSION >= 301 */

//define system_version (*__nsapi30_table->f_system_version)
// new:
NSAPI_PUBLIC char* system_version();

/*
 * Depending on the system, memory allocated via these macros may come from 
 * an arena.  If these functions are called from within an Init function, they
 * will be allocated from permanent storage.  Otherwise, they will be freed 
 * when the current request is finished.
 */



#endif /* !INTNSAPI */

#ifdef XP_UNIX
#define dir_open opendir
#define dir_read readdir
#define dir_close closedir
#define dir_create(path) mkdir(path, 0755)
#define dir_remove rmdir
#define system_chdir chdir
#define file_unix2local(path,p2) strcpy(p2,path)
#endif /* XP_UNIX */

#ifdef XP_WIN32
#define dir_create _mkdir
#define dir_remove _rmdir
#define system_chdir SetCurrentDirectory
#endif /* XP_WIN32 */

/*
 * Thread-safe variants of localtime and gmtime
 */
#define system_localtime(curtime, ret) util_localtime(curtime, ret)
#define system_gmtime(curtime, ret) util_gmtime(curtime, ret)

/* 
 * pblock_find finds the entry with the given name in pblock pb.
 *
 * If it is successful, it returns the param block.  If not, it returns NULL.
 */
#define pblock_find(name, pb) (pblock_fr(name,pb,0))

/*
 * pblock_remove behaves exactly like pblock_find, but removes the given
 * entry from pb.
 */
#define pblock_remove(name, pb) (pblock_fr(name,pb,1))

/*
 * session_dns returns the DNS hostname of the client of this session,
 * and inserts it into the client pblock.  Returns NULL if unavailable.
 */
#define session_dns(sn) session_dns_lookup(sn, 0)

/*
 * session_maxdns looks up a hostname from an IP address, and then verifies 
 * that the host is really who they claim to be. 
 */
#define session_maxdns(sn) session_dns_lookup(sn, 1)


/* nsapi functions */

NSAPI_PUBLIC conf_global_vars_s* conf_getglobals();

/*
 * Query the VirtualServer* associated with a given Request*.
 */
const VirtualServer* request_get_vs(Request *rq);
#define request_get_vs request_get_vs

struct stat* request_stat_path(const char *path, Request *rq);
#define request_stat_path request_stat_path

void request_free(Request *rq);
#define request_free request_free

Request* request_restart_internal(const char *uri, Request *rq);
#define request_restart_internal request_restart_internal


char* vs_translate_uri(const VirtualServer *vs, const char *uri);
#define vs_translate_uri vs_translate_uri

char* servact_translate_uri(char *uri, Session *sn);
#define servact_translate_uri servact_translate_uri
#define request_translate_uri servact_translate_uri

ssize_t net_write(SYS_NETFD fd, const void *buf, size_t nbytes);
ssize_t net_writev(SYS_NETFD fd, struct iovec *iovec, int iovcnt);
ssize_t net_sendfile(SYS_NETFD fd, sendfiledata *sfd);
ssize_t net_read(SYS_NETFD fd, void *buf, size_t nbytes);
int net_flush(SYS_NETFD sd);
void net_close(SYS_NETFD fd);

// NSAPI extension
ssize_t net_printf(SYS_NETFD fd, char *format, ...);

int net_setnonblock(SYS_NETFD fd, int nonblock);
int net_errno(SYS_NETFD fd);

NSAPI_PUBLIC pb_param *INTparam_create(const char *name, const char *value);

NSAPI_PUBLIC int INTparam_free(pb_param *pp);

NSAPI_PUBLIC pblock *INTpblock_create(int n);

NSAPI_PUBLIC void INTpblock_free(pblock *pb);

NSAPI_PUBLIC char *INTpblock_findval(const char *name, const pblock *pb);

NSAPI_PUBLIC pb_param *INTpblock_nvinsert(const char *name, const char *value, pblock *pb);

NSAPI_PUBLIC pb_param *pblock_nvlinsert(const char *name, int namelen, const char *value, int valuelen, pblock *pb);

NSAPI_PUBLIC pb_param *INTpblock_nninsert(const char *name, int value, pblock *pb);

NSAPI_PUBLIC void INTpblock_pinsert(pb_param *pp, pblock *pb);

NSAPI_PUBLIC int INTpblock_str2pblock(const char *str, pblock *pb);

NSAPI_PUBLIC char *INTpblock_pblock2str(const pblock *pb, char *str);

NSAPI_PUBLIC int INTpblock_copy(const pblock *src, pblock *dst);

NSAPI_PUBLIC pblock *INTpblock_dup(const pblock *src);

NSAPI_PUBLIC char **INTpblock_pb2env(const pblock *pb, char **env);

NSAPI_PUBLIC void pblock_nvreplace (const char *name, const char *value, pblock *pb);

NSAPI_PUBLIC pb_param *pblock_param_create(pblock *pb, const char *name, const char *value);

NSAPI_PUBLIC pblock *pblock_create_pool(pool_handle_t *pool_handle, int n);

NSAPI_PUBLIC pb_param *INTpblock_fr(const char *name, pblock *pb, int remove);

NSAPI_PUBLIC char *INTpblock_replace(const char *name,char * new_value,pblock *pb);

NSAPI_PUBLIC int INTpblock_str2pblock_lowercasename(const char *str, pblock *pb);

//NSAPI_PUBLIC pb_param *pblock_removeone(pblock *pb);

//NSAPI_PUBLIC const pb_key *pblock_key(const char *name);

//NSAPI_PUBLIC pb_param *pblock_key_param_create(pblock *pb, const pb_key *key, const char *value, int valuelen);

//NSAPI_PUBLIC char *pblock_findkeyval(const pb_key *key, const pblock *pb);

//NSAPI_PUBLIC pb_param *pblock_findkey(const pb_key *key, const pblock *pb);

//NSAPI_PUBLIC pb_param *pblock_removekey(const pb_key *key, pblock *pb);

//NSAPI_PUBLIC pb_param *pblock_kvinsert(const pb_key *key, const char *value, int valuelen, pblock *pb);

//NSAPI_PUBLIC void pblock_kpinsert(const pb_key *key, pb_param *pp, pblock *pb);

//NSAPI_PUBLIC void pblock_kvreplace(const pb_key *key, const char *value, int valuelen, pblock *pb);

//NSAPI_PUBLIC pb_param *pblock_kninsert(const pb_key *key, int value, pblock *pb);

//NSAPI_PUBLIC pb_param *pblock_kllinsert(const pb_key *key, PRInt64 value, pblock *pb);

#define pblock_remove(name, pb) (pblock_fr(name,pb,1))

#define param_create INTparam_create
#define param_free INTparam_free
#define pblock_create INTpblock_create
#define pblock_free INTpblock_free
#define pblock_findval INTpblock_findval
#define pblock_nvinsert INTpblock_nvinsert
#define pblock_nninsert INTpblock_nninsert
#define pblock_pinsert INTpblock_pinsert
#define pblock_str2pblock INTpblock_str2pblock
#define pblock_pblock2str INTpblock_pblock2str
#define pblock_copy INTpblock_copy
#define pblock_dup INTpblock_dup
#define pblock_pb2env INTpblock_pb2env
#define pblock_fr INTpblock_fr
#define pblock_replace INTpblock_replace


// pool
NSAPI_PUBLIC pool_handle_t *INTpool_create(void);

NSAPI_PUBLIC void *INTpool_mark(pool_handle_t *pool_handle);

NSAPI_PUBLIC void INTpool_recycle(pool_handle_t *pool_handle, void *mark);

NSAPI_PUBLIC void INTpool_destroy(pool_handle_t *pool_handle);

NSAPI_PUBLIC int INTpool_enabled(void);

NSAPI_PUBLIC void *INTpool_malloc(pool_handle_t *pool_handle, size_t size );

NSAPI_PUBLIC void INTpool_free(pool_handle_t *pool_handle, void *ptr );

NSAPI_PUBLIC 
void *INTpool_calloc(pool_handle_t *pool_handle, size_t nelem, size_t elsize);

NSAPI_PUBLIC 
void *INTpool_realloc(pool_handle_t *pool_handle, void *ptr, size_t size );

NSAPI_PUBLIC
char *INTpool_strdup(pool_handle_t *pool_handle, const char *orig_str );

#define pool_create INTpool_create
#define pool_mark INTpool_mark
#define pool_recycle INTpool_recycle
#define pool_destroy INTpool_destroy
#define pool_enabled INTpool_enabled
#define pool_malloc INTpool_malloc
#define pool_free INTpool_free
#define pool_calloc INTpool_calloc
#define pool_realloc INTpool_realloc
#define pool_strdup INTpool_strdup


#define MALLOC  malloc
#define CALLOC(size) calloc(1, size)
#define REALLOC realloc
#define FREE    free
#define STRDUP  strdup

// func util functions
FuncStruct* func_resolve(pblock *pb, Session *sn, Request *rq);
int func_exec(pblock *pb, Session *sn, Request *rq);
struct FuncStruct func_insert(char *name, FuncPtr fn);
#define func_insert func_insert


void protocol_status(Session *sn, Request *rq, int n, const char *m);

int http_start_response(Session *sn, Request *rq);
#define protocol_start_response http_start_response

HttpResponseWriter *http_create_response(Session *sn, Request *rq);

/*
 * return: -1: error
 *          0: finished
 *          1: EWOULDBLOCK
 */
int http_send_response(HttpResponseWriter *writer);

int request_header(char *name, char **value, Session *sn, Request *rq);

char *http_uri2url(const char *prefix, const char *suffix);
char *http_uri2url_dynamic(const char *prefix, const char *suffix,
                                    Session *sn, Request *rq);
#define protocol_uri2url http_uri2url
#define protocol_uri2url_dynamic http_uri2url_dynamic


NSAPI_PUBLIC void http_format_etag(Session *sn, Request *rq, char *etagp, int etaglen, off_t size, time_t mtime);
NSAPI_PUBLIC int http_check_preconditions(Session *sn, Request *rq, struct tm *mtm, const char *etag);
NSAPI_PUBLIC int http_set_finfo(Session *sn, Request *rq, struct stat *finfo);
NSAPI_PUBLIC int http_set_finfo_etag(Session *sn, Request *rq, struct stat *finfo, const char *etag);

NSAPI_PUBLIC char **http_hdrs2env(pblock *pb);

// new websocket API begin

NSAPI_PUBLIC int http_handle_websocket(Session *sn, Request *rq, WebSocket *websocket);

NSAPI_PUBLIC int websocket_send_text(SYS_NETFD csd, char *msg, size_t len);

// websocket API end


typedef void (*thrstartfunc)(void *);
SYS_THREAD INTsysthread_start(int prio, int stksz, thrstartfunc fn, void *arg);
NSAPI_PUBLIC void INTsysthread_sleep(int milliseconds);

#define systhread_start INTsysthread_start
#define systhread_sleep INTsysthread_sleep


void webserver_atrestart(void (*fn)(void *), void *data);
#define magnus_atrestart webserver_atrestart
#define daemon_atrestart webserver_atrestart


NSAPI_PUBLIC int INTshexp_match(const char *str, const char *exp);
#define shexp_match INTshexp_match



NSAPI_PUBLIC char *session_dns_lookup(Session *s, int verify);



/* netbuf functions */
NSAPI_PUBLIC netbuf *netbuf_open(SYS_NETFD sd, int sz);

NSAPI_PUBLIC void netbuf_close(netbuf *buf);

NSAPI_PUBLIC unsigned char * netbuf_replace(netbuf *buf,
    unsigned char *inbuf, int pos, int cursize, int maxsize);

NSAPI_PUBLIC int netbuf_next(netbuf *buf, int advance);

NSAPI_PUBLIC int netbuf_getbytes(netbuf *buf, char *buffer, int size);

NSAPI_PUBLIC int netbuf_grab(netbuf *buf, int sz);

#define netbuf_open        netbuf_open
#define netbuf_close       netbuf_close
#define netbuf_replace     netbuf_replace
#define netbuf_next        netbuf_next
#define netbuf_getbytes    netbuf_getbytes
#define netbuf_grab        netbuf_grab

/* file */
NSAPI_PUBLIC int system_fread(SYS_FILE fd, void *buf, int nbyte);
NSAPI_PUBLIC int system_fwrite(SYS_FILE fd, const void *buf, int nbyte);
NSAPI_PUBLIC int system_pread(SYS_FILE fd, void *buf, int nbyte, off_t offset);
NSAPI_PUBLIC int system_pwrite(SYS_FILE fd, const void *buf, int nbyte, off_t offset);
NSAPI_PUBLIC off_t system_lseek(SYS_FILE fd, off_t offset, int whence);
NSAPI_PUBLIC int system_fclose(SYS_FILE fd);

NSAPI_PUBLIC int system_aio_read(aiocb_s *aiocb);
NSAPI_PUBLIC int system_aio_write(aiocb_s *aiocb);


int log_ereport(int degree, const char *format, ...);
int log_ereport_v(int degree, const char *format, va_list args);
int log_error(int degree, const char *func, Session *sn, Request *rq,
                       const char *format, ...);
int log_error_v(int degree, const char *func, Session *sn, Request *rq,
                       const char *format, va_list args);
int log_message(const char *degree, const char *format, ...);
int log_message_v(const char *degree, const char *format, va_list args);

/* new macro and function definitions begin */

NSAPI_PUBLIC int util_errno2status(int errno_value); // new
#define util_errno2status util_errno2status
NSAPI_PUBLIC pblock* util_parse_param(pool_handle_t *pool, char *query);
#define util_parse_param util_parse_param

void nsapi_function_return(Session *sn, Request *rq, int ret);

// threadpool
threadpool_t* threadpool_new(int min, int max);
int threadpool_start(threadpool_t *pool);
void* threadpool_func(void *data);
threadpool_job* threadpool_get_job(threadpool_t *pool);
void threadpool_run(threadpool_t *pool, job_callback_f func, void *data);

int event_pollin(EventHandler *ev, SYS_NETFD fd, Event *event);
int event_pollout(EventHandler *ev, SYS_NETFD fd, Event *event);
int event_removepoll(EventHandler *ev, SYS_NETFD fd);
int event_send(EventHandler *ev, Event *event);

// resource pool
ResourceData* resourcepool_lookup(Session *sn, Request *rq, const char *name, int flags);
ResourceData* resourcepool_cfg_lookup(ServerConfiguration *cfg, const char *name, int flags);
void resourcepool_free(Session *sn, Request *rq, ResourceData *resource);

// utils
NSAPI_PUBLIC char *util_html_escape(const char *s);

// assert
void ws_log_assert(const char *file, const char *func, int line);
#ifdef _DEBUG
#ifndef __FUNCTION__
#define __FUNCTION__ __func__
#endif 
#define WS_ASSERT(c) if(!c) ws_log_assert(__FILE__, __FUNCTION__, __LINE__)
#else
#define WS_ASSERT(c)
#endif

/* end new macro and function definitions */

#define SYS_STDERR STDERR_FILENO

#ifdef XP_WIN32

//typedef HANDLE pid_t; // TODO

#define ERROR_PIPE \
        (ERROR_BROKEN_PIPE | ERROR_BAD_PIPE | \
         ERROR_PIPE_BUSY | ERROR_PIPE_LISTENING | ERROR_PIPE_NOT_CONNECTED)
#define CONVERT_TO_PRINTABLE_FORMAT(Filename)   \
{                                               \
        register char *s;                       \
        if (Filename)                           \
        for (s = Filename; *s; s++)             \
                if ( *s == '\\')                \
                        *s = '/';               \
}
#define CONVERT_TO_NATIVE_FS(Filename)          \
{                                               \
        register char *s;                       \
        if (Filename)                           \
                for (s = Filename; *s; s++)     \
                        if ( *s == '/')         \
                                *s = '\\';      \
}                                                                        

#ifdef INTNSAPI
//NSAPI_PUBLIC extern nsapi_dispatch_t *__nsapi30_table;
#if NSAPI_VERSION >= 302
//NSAPI_PUBLIC extern nsapi302_dispatch_t *__nsapi302_table;
#endif /* NSAPI_VERSION >= 302 */
#if NSAPI_VERSION >= 303
//NSAPI_PUBLIC extern nsapi303_dispatch_t *__nsapi303_table;
#endif /* NSAPI_VERSION >= 303 */
#else
//__declspec(dllimport) nsapi_dispatch_t *__nsapi30_table;
#if NSAPI_VERSION >= 302
//__declspec(dllimport) nsapi302_dispatch_t *__nsapi302_table;
#endif /* NSAPI_VERSION >= 302 */
#if NSAPI_VERSION >= 303
//__declspec(dllimport) nsapi303_dispatch_t *__nsapi303_table;
#endif /* NSAPI_VERSION >= 303 */
#endif /* INTNSAPI */

#else /* !XP_WIN32 */


#endif /* XP_WIN32 */

#ifdef __cplusplus
}
#endif

#endif /* !PUBLIC_NSAPI_H */

mercurial