/*(( "Header" */
/*
 * $Id: httpproxy.h,v 1.5 1996/06/06 23:01:23 mshopf Exp mshopf $
 *
 * (c) 1995-96 Matthias Hopf
 */

/*
 * $Log: httpproxy.h,v $
 * Revision 1.5  1996/06/06  23:01:23  mshopf
 * added AlwaysReload
 *
 * Revision 1.4  1996/06/03  04:08:19  mshopf
 * added timeout values.
 * added connection error state.
 *
 * Revision 1.3  1996/04/26  05:14:03  mshopf
 * move network encapsulation to net.h
 * V0.13 alpha 5 fix.
 *
 * Revision 1.2  1996/04/24  17:41:16  mshopf
 * config changes.
 * net handler enhancement.
 *
 * Revision 1.1  1996/04/24  03:20:13  mshopf
 * Initial revision
 *
 */

/*)) */

#ifndef _HTTPPROXY_H__
#define _HTTPPROXY_H__

#include <stdio.h>
#include <dos.h>
#include <dos/datetime.h>

/*(( "Debuging" */

#ifdef DEBUG
#  define debug(x)      printf x
#  define vdebug(x,y)   vprintf (x,y)
#else
#  define debug(x)      ((void) 0)
#  define vdebug(x,y)   ((void) 0)
#endif

/* own assert() writing assertation to logfile */
#ifndef NDEBUG
void ASSERT (int, const char *, const char *, int);
#  define assert(x)  ASSERT ((int)(x), #x, __FILE__, __LINE__)
#else
#  define assert(x)  ((void) 0)
#endif


/*)) */
/*(( "Defaults" */

#define DEFAULT_PROXYPORT 8080
#define DEFAULT_TIMEOUT  (10*60)         /* default: timeout (10 mins) */
#define DEFAULT_DELTIME  (60*24*60*60)   /* default: delete cached files on startup */
#define DEFAULT_EXPIRETIME (7*24*60*60)  /* default: delete cache, when the page is requested and too old */
#define DEFAULT_RELOADTIME 10            /* default: expire cache on reload inbetween */
#define DEFAULT_MAX_REQUESTS 16          /* Maximum number of pending requests */

/*)) */
/*(( "Constants" */

#define NAME_CACHETABLE   ".cachetable"

#define MIN_REQUESTS     12       /* Minimum number of free requests for interactive actions */
#define MAX_CACHES       32       /* maximum number of cache slots per cachearray */
#define MAX_FILENAME     20       /* maximum size of filename (>= 20) */
#define MAX_URLSAVE      128      /* maximum size of saved url requests */
#define MAX_URLBUFFER    512      /* no Url of any request may be larger than this value (<=MAX_REQBUFFER) */
#define MAX_REQBUFFER    1024
#define MAX_DATABUFFER   2048
#define SHIFT_DATABUFFER 512      /* when x bytes are in the data buffer, shift it after send() */
#define SHIFT_REQBUFFER  512      /* dto. for the request buffer */

#define MAX_HOSTNAMELEN  64       /* maximum number of characters of the hostname saved */

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

/*)) */
/*(( "Flags" */

#define REQ_REQSOCKET    0x01     /* a request socket is open */
#define REQ_CONNSOCKET   0x02     /* a connection socket is open / to be connected */
#define REQ_DONE         0x04     /* the connection socket is already closed */
#define REQ_REQDONE      0x08     /* the URL is completely read (cache may be sent) */
#define REQ_HTTP1X0      0x10     /* the URL was sent with HTTP/1.0 */
#define REQ_URLDONE      0x40     /* the URL request (the first) line is already there */
#define REQ_URLINBUFFER  0x80     /* the valid URL description can be found '\0'-terminated in the
				   * UrlBuffer entry, not in any cache entry (proxy only mode) */
#define REQ_LOGGED       0x100    /* The request is already logged (error/message information) */
#define REQ_CONNERR      0x200    /* Error while trying to open connection socket */

#define REQ_PENDINGMASK  0x03     /* There is any Connection pending */

#define CACHE_VALID      0x01     /* Cache is filled and ready to serve */
#define CACHE_QUEUED     0x02     /* Cache is already queued for regetting */
#define CACHE_DELETETMP  0x04     /* There's a temporaray cache file to be removed */

/*
 * Some typical Flag combinations in typical order:
 * None:                           Empty slot
 * REQ_REQSOCKET:                  Awaiting URL
 * REQ_REQSOCKET | REQ_CONNSOCKET: Still geting URL, but 1. line is already there
 *                                   *or*  URL done
 *                                 Awaiting connection to remote host
 *                                   *or*  already transfering data
 * REQ_REQSOCKET | REQ_DONE:       The connection socket is already closed, but data is
 *                                   still to be delivered, or data is sent from the cache
 * REQ_CONNSOCKET:                 Request was terminated, still geting data to fill up
 *                                   the cache.
 */

/* REQ_REQSOCKET: alone will never occour on ProxyProxy==TRUE... */


/*)) */
/*(( "Makros" */

				  /* Needed Memory */
#define NEED_MEM         ((MaxRequests * sizeof (request_t) + sizeof (cachearray_t)) / 1024)

#define difftime(x,y)    (((u_long)(x)) - ((u_long)(y)))
#define isvalidhttp(x)   ((unsigned char)(x) > ' ' && (x) != '%' && (x) != '\\' && (unsigned char)(x) < 128)

/*)) */
/*(( "Types" */

typedef struct {
		   int  Flags;
		   char File [MAX_FILENAME];        /* "" if free slot / with begining '_' (data) */
		   char Url  [MAX_URLSAVE];         /* "" if free slot or just getting data */
	       } cache_t;

typedef struct cachearray {
			      struct cachearray *Next;
			      cache_t Caches [MAX_CACHES];
			  } cachearray_t;

typedef struct {
		   int   ReqSocket, ConnSocket;
		   int   Flags;
		   char  Address    [MAX_HOSTNAMELEN];
		   char  ReqBuffer  [MAX_REQBUFFER];
		   char  UrlBuffer  [MAX_URLBUFFER];/* This one contains the URL line for sending the request */
		   char  DataBuffer [MAX_DATABUFFER];
		   int   ReqSent, ReqRecv;
		   int   UrlSent, UrlRecv;
		   int   DataSent, DataRecv;
		   cache_t *Cache;                  /* NULL if not cacheable */
		   FILE  *Stream;                   /* != NULL on open data transfer to/from cache */
		   struct DateStamp LastStamp;      /* last access time for timeouts */
	       } request_t;

/*)) */
/*(( "Global Variables" */

extern request_t *Requests;
extern int OffLine;
extern int GetQueued;
extern int AlwaysReload;
extern struct DateStamp RequestStamp;               /* Current time while the request is processed */

/*)) */

extern void ExitAll (int);
extern void CheckGetQueued (int);
extern int  ScanService (request_t *, const char *);
extern void ErrToReq (request_t *, int, int, const char *, const char *, const char *);
extern void ShutdownConnects (void);

#endif /* _HTTPPROXY_H__ */

