File: HTTP.h

package info (click to toggle)
xmhtml 1.1.7-18
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,576 kB
  • sloc: ansic: 68,174; makefile: 438; sh: 158; perl: 36
file content (372 lines) | stat: -rw-r--r-- 11,134 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*****
* 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_

#define HTTPVERSION	0
#define HTTPREVISION	1
#define HTTPUPDATE_LEVEL 1
#define HTTPVersion \
	(HTTPVERSION * 1000 + HTTPREVISION * 100 + HTTPUPDATE_LEVEL)

/* used by Imake to get Shared library version numbering */
#ifndef _LIBRARY

#define LIBHTTPVERSION_STRING \
	"Client/HTTP Version 0.1.1 (C)Richard Offer and Ripley Software Development"

/*****
* Feel free to re-define the following, this is passed to every web-server 
* during all requests. 
* If you do change this, be careful (bad editing may lead to failed requests) 
* the format is
* User-Agent: <space> (manditory)
* <client identifier,most significant parts first> / <version number>
* <space>
* <extra fields, for development software, a point of contact is reccommended
* \r\n terminator
* 
* wrapped in a #ifdef so an application can define it before #include'ing 
* this file
*****/
#ifndef USER_AGENT
#define USER_AGENT "User-Agent: XmHTML-HTTP/0.1.x ripley@xs4all.nl\r\n"
#endif /* USER_AGENT */	

typedef enum {
	HTTPLoadToFile,
	HTTPLoadToString
} HTTPLoadType;

typedef enum {
	HTTPGET,
	HTTPPOST,
	HTTPHEAD
} HTTPLoadMethod;

/* possible HTTP return values */
typedef enum {
	/* our own error values */
	HTTPInvalid					= 0,
	HTTPBadProtocol				= 1,
	HTTPBadHost					= 2,
	HTTPBadURL					= 3,
	HTTPBadLoadType				= 4,
	HTTPMethodUnsupported		= 5,
	HTTPNoSocket				= 6,
	HTTPNoConnection			= 7,
	HTTPBadHttp10				= 8,
	HTTPCannotCreateFile		= 9,
	HTTPConnectTimeout			= 10,
	HTTPTimeout					= 11,

	/* Now 'Real' HTTP return codes */
	HTTPContinue				= 100,
	HTTPSwitchProtocols			= 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;

#define PARSE_URL (PARSE_SCHEME|PARSE_HOSTNAME|PARSE_PORT|PARSE_FILENAME)

/*****
* 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			*/

	int timeout;				/* select() timeout in seconds		*/
	int retry;					/* retry count, zero based			*/

	HTTPNamedValues *headers;	/* array of returned headers		*/
	int num_headers;			/* no of headers					*/

	HTTPRequestReturn ret;		/* Server return value				*/
}HTTPRequest;

/* type of cookies 
 *   Not a lot an application can do here, it all depends on what the 
 *   server sends.
 *   
 *   SetCookie implies the server is using Netscape style cookies.
 *   SetCookie means the server is using RFC2109 style cookies.
 */
enum { SetCookie, SetCookie2 }; 

/* type of cookie file 
 *   For safety I am only going to support the writting of CookieJar files
 *   however, I will allow an application to read cookie files generated by 
 *   netscape.
 *   
 *   CookieJar (a name I've just invented) files are much richer than Netscape 
 *   ones, they have to be they store the full SetCookie2 response. 
 */
enum { NetscapeCookieFile = 1, CookieJar = 2 } ;  

typedef struct _HTTPCookie {
	
	HTTPNamedValues	cookie;
	char	type;
	char	*comment;	/* this and the url are not preserved between sessions*/
	char	*commentURL;
	int		discard;
	char	*domain;
	int		exactHostMatch;
	int		secure;		/* not supported in HTTP.c */
	char	*path;
	int		expires;
	char	*port;
	int		version;

} HTTPCookie; 

typedef struct _HTTPCookieList {
	
	HTTPCookie	*cookie;
	struct _HTTPCookieList *next;

} HTTPCookieList;

typedef struct _HTTPCookieRequest {
	
	HTTPCookieList *cookieList;
	HTTPCookieList *setCookie;
	
	int	sendCookie;
	
} HTTPCookieRequest;


typedef struct _HTTPCookieCache {

	HTTPCookie	**cookies;
	int			ncookies;

	char		*filename;	
	char		fileType;

} HTTPCookieCache ;


/*****
* 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, HTTPCookieRequest *cookieReq);

/*****
* 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);

extern const char *HTTPErrorString(HTTPRequestReturn error);

/* unescape a HTTP escaped string */
void HTTPUnescapeResponse(char *buf);

/* load the cookie file into memory, a cookieCache is self-contained so 
 * multiple files can be read within an app. 
 */ 
extern HTTPCookieCache * loadCookieFileToCache(char * filename, char fileType);

/* The procedure for using cookies
 * 
 *  1) load the cookie cache.
 *  2) for a given URL, call getCookieFromCache(), this returns a 
 *     CookieRequest, req->cookieList is a list of all the cookies that apply 
 *     to the the URL (it will be NULL if no cookies are wanted by the server).
 *  3) at this point the application can step in and inform the user that a 
 *     cookie is wanted by the server, if the user doesn't want to send the 
 *     cookie, it should set cookieRequest->sendCookie to false _before_ calling
 *     loadHTTPUrl() (this is better than simply passing NULL into 
 *     loadHTTPUrl() since it gives tha application chnace to recieve any new 
 *     cookies.
 *  4) on return from loadHTTPUrl() cookieRequest->setCookie will be non-null 
 *     if the server sent a save cookie response. The application can then 
 *     decide to save this cookie for the rest of the session by calling 
 *     addCookieListToCache (passing in cookieRequest->setCookie).
 *  5) for permanant saving of the cookies the application needs to call    
 *     writeCookieCache();
 */

/* 
 * Notes.
 * 
 * url must be fully qualified, CookieList is a linked-list of Cookies, all 
 * that apply to the url.
 * 
 * To save the cookies between sessions, use writeCookieCache(). This writes 
 * cache back out (including any new cookies set during the session). Note that
 * for safety, I'm not going to allow for the writing of Netscape cookie files 
 * (but using mergeCookieCache() we can read the existing Netscape cookie file
 * and merge it with a custom cookie file (makes it easier moving to a new 
 * browser from netscape.
 * ---if pushed, this may change later.
 */  

extern HTTPCookieRequest *getCookieFromCache(HTTPCookieCache *, char * url);

extern void addCookieListToCache(HTTPCookieCache *, HTTPCookieList *);

extern void writeCookieCache(HTTPCookieCache *cache);

/* take the cookies from cache c2 and add them into c1.
 *   The reason for this is so that you can open an empty CookieJar file _and_
 *   a Netscape cookie file (in that order), then take the cookies from the 
 *   Netscape file and save them into the CookieJar file (since I don't
 *   support the writing of Netscape cookie files
 *   
 *   Do not free c2 
 */ 
extern void mergeCookieCache(HTTPCookieCache *c1, HTTPCookieCache *c2);

void freeCookieCache(HTTPCookieCache * , int /* free cookies ? */);
void freeCookieRequest(HTTPCookieRequest *);

/* 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)

#define	stringToBoolean(str) \
	((str) != NULL && *(str) ? ( *(str) == 'T' || *(str) == 't' || *(str) == 'y' ? 1: 0 ) : 0)

#endif /* _LIBRARY */

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