


#ifndef DONE_WIO
#define DONE_WIO 1
#include "qpair.h"

// Marijn  4/3/11 : changed below for use in surgeweb, should still build in other uses I think...
//#include "../../sam/xml.h"
#include "xml.h"
#include "file64.h"

typedef enum {
	UA_UNKNOWN,
	UA_NETSCAPE_4,
	UA_NETSCAPE_6,
	UA_IE_5,
	UA_IE_6,
	UA_OPERA,
	UA_SAFARI,
	UA_MOZILLA_5,
} USER_AGENT;

typedef struct WEBIO {
	Chan *c; char *inbf; int inmax; 
	int inlen;
	int ishead;
	void *fn_data;
	int (*fn)(struct WEBIO *,void *data);
	Pair *query;
	Pair *form;
	Pair *headers;
	Pair *head;
	Pair *cookie_list;
	int done_content;
	Pair *attachments;
	QPairList *vlist;
	char *ref;
	USER_AGENT agentid;
	char *cgi;
	int trykeep; 
	int keepalive;
	int iskeep;
	int isadmin;
	int has_attach;
	int closing;
	int depth;
	int wwwget;
	int www_method;
	int gotwebauth;
	int hostport;
	int contentlen;
	int form_mime;
	char *cookie;
	char *mod;
	char *content_store; 
	char *user;
	char *user_groups;
	char *original_request;
	int isread; // user can read this torrrent
	int iscontrol;  // use can control/own this torrent.
	int isproxy;
	char contenttype[BFSZ];	
	char contentfname[BFSZ]; // temp file of contents...
	char agent[BFSZ];	
	char etag[BFSZ];	
	char hostname[BFSZ];
	char late_error[BFSZ];
	char range[BFSZ]; // range request, e.g. bytes=-a,b-c,d-,e-f
	char save_cookie[BFSZ]; // saved cookie for when we need it.
	int chunked; // if we are 'sending' in chunked mode...
	Xml *xml;
	int httpversion;
	void *ftp; // local storage for web sessions, uses ftp structure... this module doesn't know what this points to...
} Webio; 



enum {WEB_GET=0,WEB_POST
	,WEB_PROPFIND,WEB_PROPPATCH,WEB_MKCOL,WEB_DELETE,WEB_PUT,WEB_COPY,WEB_MOVE,WEB_LOCK,WEB_UNLOCK,WEB_OPTIONS
	,WEB_ACL,WEB_REPORT,WEB_HEAD,WEB_MKCALENDAR
};
static char *wio_names[]=
{
	"GET","POST",
	"PROPFIND", "PROPPATCH", "MKCOL", "DELETE", 
	"PUT", "COPY", "MOVE", "LOCK", "UNLOCK"
	,"OPTIONS","ACL","REPORT","HEAD","MKCALENDAR"
	,NULL
};



int wio_handle_request(Chan *c, int (*fn)(Webio *w, void *data),void *data, int isproxy);
char *wio_input_bin(Webio *l, char *var, int *len);
int wio_printf_now(Webio *l, char *format, ...) ;
char *wio_input(Webio *l, char *var);
int wio_show_code(Webio *l, int code, char *reason);
int wio_showimage(Webio *l, char *fname);
int wio_cookie_set(Webio *w, char *var, char *val);
int wio_auth_decodex(Webio *l, char *bf, char *user, char *pass, int bfsz);
int wio_auth_decode(Webio *l, char *user, char *pass);
int wio_auth_required(Webio *w, char *msg);
int wio_free(Webio *w);
int wio_finish(Webio *w);

int wio_any_header(Webio *l, char *content, bigint length, int chunked);
char *wio_input_attachment(Webio *l, char *var);
int wio_showafile(Webio *l, char *fname);

char *wio_cookie_get(Webio *w, char *var);
char *wio_var_find(Webio *l, char *var);
int wio_redirect(Webio *l, char *location);
int wio_with_code(Webio *l, char *content, int code, char *data);
void wio_add_header(Webio *l, char *header, char *value) ;
int wio_printf_flush(Webio *l);
char x2c(char *what);
int wio_set_webdav(int x);
int url_encode(char *string, char *result, int bfsz);

#ifdef WIN32
#define stricmp _stricmp
#define strlwr _strlwr
#define strnicmp _strnicmp
#endif
#endif
