/* $Header: /san01/cvs/ashpool/csrc/modhttp/Attic/http.h,v 1.18 2004/04/02 16:17:18 aleigh Exp $ */

/* Copyright (c) 1994, 2001 Alex Leigh, All Rights Reserved
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF ALEX LEIGH
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 * 
 * Disclosure, copying, distribution, or transmission of
 * this code without express permission is prohibited.
 */
   
#ifndef HTTP_H
#define HTTP_H

#include "continuity.h"
#include "mecha.h"

#define HTTP_OK 200
#define HTTP_CREATED 201
#define HTTP_ACCEPTED 202
#define HTTP_PARTIAL 203
#define HTTP_NORESPONSE 204
#define HTTP_MOVED 301  
#define HTTP_FOUND 302
#define HTTP_METHOD 303
#define HTTP_NOTMODIFIED 304
#define HTTP_BADREQUEST 400
#define HTTP_UNAUTHORIZED 401
#define HTTP_PAYMENTREQUIRED 402
#define HTTP_FORBIDDEN 403
#define HTTP_NOTFOUND 404
#define HTTP_ERROR 500
#define HTTP_UNIMPLEMENTED 501
#define HTTP_TIMEDOUT 502       /* unofficial */

#define REQ_UNSTARTED 0
#define REQ_STARTED 1
#define REQ_HEADERSENT 2

struct httpStrans {
   int sd;                      /* Socket Descriptor */
   uint32_t cli_ipv4_addr;      /* Remote IPv4 Address */
   netTreader *reader;          /* Network Reader */

   struct stat statb;           /* A stat of the response file */

   lstTset *req_hdrs;           /* Client HTTP headers */
   lstTset *res_hdrs;           /* Response HTTP headers */
   lstTset *vars;               /* Working Variables */
   lstTset *query;              /* Query Cache */

   int res_code;                /* Response Code */
   const char *res_reason;      /* Response Message */

   int started;                 /* Transaction status */
   int client_read;             /* Client status */

   dynTstring *response;        /* HTTP Protocol Response */
   netTconn *conn;

   int has_statb;               /* Indicate if statb has been filled in */
};
typedef struct httpStrans httpTtrans;

int httpFcreate_dispatcher(int ipv4port, int ipv4ip);
int httpFthread_settrans(httpTtrans * t);

/* Handlers */
void httpFhandler(void *p, const lstTset *arg);
int httpFinit(void *p, const lstTset *opts);

/* Public Functions */
httpTtrans *httpFtrans_alloc(void);
void httpFtrans_free(httpTtrans * t);
httpTtrans *httpFthread_gettrans(void);
void httpFset_status(httpTtrans * t, int status, const char *reason);
void httpFstart_response(httpTtrans * t);
void httpFsend_response(httpTtrans * t);
int httpFsend_resp_headers(httpTtrans * t);
int httpFread (httpTtrans * t, char *buf, size_t len);
int httpFwrite(httpTtrans * t, const char *buf, size_t len);
int httpFwritef(httpTtrans *t, const char *fmt, ...);
int httpFredirect(httpTtrans *t, const char *dest);
const lstTset *httpFget_query(httpTtrans * t);
const lstTset *httpFget_post_query(httpTtrans * t);
const char *httpFquery_val(httpTtrans *t, const char *name);

int httpFread_post(httpTtrans * t);
int httpFdebug_page (httpTtrans * t, char *msg);
int httpFlog_trans(httpTtrans * t, char *msg);
int httpFparse(httpTtrans * t);
int httpFget_boundry(dynTstring * str, httpTtrans * t);
int httpFparse_query(httpTtrans * t, lstTset *set);
int httpFerror_page (httpTtrans * t, const char *msg);
int httpFmaterial_compare(const httpTtrans *trans, const httpTtrans *b);

void httpFset_state (httpTtrans *t, int state);

int httpFset_nph (httpTtrans *t);

#endif
