/*****
* HTTP.h : Public header file for HTTP.c, a simple HTTP/1.0 implementation.
*
* This file Version	$Revision: 1.1 $
*
* Creation date:		Tue Oct 21 01:56:19 GMT+0100 1997
* Last modification: 	$Date: 1997/10/23 00:28:26 $
* By:					$Author: newt $
* Current State:		$State: Exp $
*
* Author:				Richard Offer
*
* Copyright (C) 1994-1997 by Richard Offer <offer@sgi.com>
* All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****/
/*****
* $Source: /usr/local/rcs/Newt/XmHTML/RCS/HTTP.h,v $
*****/
/*****
* ChangeLog 
* $Log: HTTP.h,v $
* Revision 1.1  1997/10/23 00:28:26  newt
* Initial Revision
*
*****/ 

#ifndef _HTTP_h_
#define _HTTP_h_

typedef enum {
	HTTPLoadToFile,
	HTTPLoadToString
} HTTPLoadType;

typedef enum {
	HTTPGET,
	HTTPPOST,
	HTTPHEAD
} HTTPLoadMethod;

/* possible HTTP return values */
typedef enum {
	/* our own error values */
	HTTPInvalid = 0,
	HTTPBadProtocol,
	HTTPBadHost,
	HTTPBadURL,
	HTTPBadLoadType,
	HTTPMethodUnsupported,
	HTTPNoSocket,
	HTTPNoConnection,
	HTTPBadHttp10,
	HTTPCannotCreateFile,

	/* Now 'Real' HTTP return codes */
	HTTPContinue = 100,
	HTTPSwitchProtcols = 101,

	HTTPSuccess = 200,
	HTTPCreated = 201,
	HTTPAccepted = 202,
	HTTPNonAuthoritativeInfo = 203,
	HTTPNoContent = 204,
	HTTPResetContent = 205,
	HTTPPartialContent = 206,

	HTTPMultipleChoices = 300,
	HTTPPermMoved = 301,
	HTTPTempMoved = 302,
	HTTPSeeOther = 303,
	HTTPNotModified = 304,
	HTTPUseProxy = 305,

	HTTPBadRequest = 400,
	HTTPUnauthorised = 401,
	HTTPPaymentReq = 402,
	HTTPForbidden = 403,
	HTTPNotFound = 404,
	HTTPMethodNotAllowed = 405,
	HTTPNotAcceptable = 406,
	HTTPProxyAuthReq = 407,
	HTTPRequestTimeOut = 408,
	HTTPConflict = 409,
	HTTPGone = 410,
	HTTPLengthReq = 411,
	HTTPPreCondFailed = 412,
	HTTPReqEntityTooBig = 413,
	HTTPURITooBig = 414,
	HTTPUnsupportedMediaType = 415,

	HTTPInternalServerError = 500,
	HTTPNotImplemented = 501,
	HTTPBadGateway = 502,
	HTTPServiceUnavailable = 503,
	HTTPGatewayTimeOut = 504,
	HTTPHTTPVersionNotSupported = 505

} HTTPRequestReturn;

/* flags for parseURL */
typedef enum {
	PARSE_SCHEME = 1,
	PARSE_USER = 2,
	PARSE_PASSWORD = 4,
	PARSE_HOSTNAME = 8,
	PARSE_PORT = 16,
	PARSE_FILENAME = 32
}HTTPParseURL;

/*****
* Used internally for HTTP headers and externally for the form data 
* name/value pairs 
*****/
typedef struct _HTTPNamedValues {
	char *name;
	char *value;
} HTTPNamedValues;

/*****
* Definition of a requestor.
* *never* store static data in the ptr fields as deleteHTTPRequest explicitly
* frees any non-NULL fields.
*****/
typedef struct _HTTPRequest {
	HTTPLoadType type;			/* load to string or file */
	char *in_data;				/* filename for LoadToFile */
	HTTPNamedValues *form_data;	/* data for form processing */
	unsigned char *out_data;	/* response string  */
	size_t length;				/* length of out_data, from Content-Length
								 * or strlen
								 */
	HTTPLoadMethod method;		/* get, post etc */
	char *url;					/* fully qualified location */

	HTTPRequestReturn ret;

}HTTPRequest;

/*****
* Create a new, empty, request instance. This is the only safe way to create
* a request. 
*****/
extern HTTPRequest *newHTTPRequest(void);

/* make a request for some URI */
extern void loadHTTPURL(void *unused, HTTPRequest * request, void *unused2);

/*****
* Clear a no longer needed request instance. Deletes any field that have
* been allocated.
*****/
extern void deleteHTTPRequest(HTTPRequest * req);

/* split a full URI into separate fields */
extern void parseURL(char *url, long parseflag, char **scheme,
	char **username, char **password, char **hostname, int *port,
	char **filename);

/* free fields allocated by parseURL */
extern void freeURL(long parseflag, char *scheme, char *username,
	char *password, char *hostname, int port, char *filename);

/* Determine whether or not the given url should be retrieved via HTTP. */
extern int HTTPAbsoluteURL(char *url);

/*****
* Compose a full URI from a (possibly local) url and the base url for this
* document. Return value should be freed by caller unless the return value
* is stored in a HTTPRequest in which case it will be freed when
* deleteHTTPRequest is called.
*****/
extern char *HTTPFindAbsoluteURL(char *url, char *baseUrl);

/* display an error message for the given error code */
extern void HTTPError(char *msg, HTTPRequestReturn error);

/* convenience macros */
#define NewNString(STR,len) \
	((STR) != NULL ? (strncpy(calloc(len+1,sizeof(char)), STR,(len))) : NULL)

#define NewString(STR) \
	((STR) != NULL ? (strcpy(malloc(strlen(STR)+1),STR)) : NULL)

/* Don't add anything after this endif! */
#endif /* _HTTP_h_ */
