]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/http.h
Added write buffering to the HTTP code to improve performance
[thirdparty/cups.git] / cups / http.h
1 /*
2 * "$Id$"
3 *
4 * Hyper-Text Transport Protocol definitions for the Common UNIX Printing
5 * System (CUPS).
6 *
7 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE.txt" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
14 * at:
15 *
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636 USA
20 *
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
24 *
25 * This file is subject to the Apple OS-Developed Software exception.
26 */
27
28 #ifndef _CUPS_HTTP_H_
29 # define _CUPS_HTTP_H_
30
31 /*
32 * Include necessary headers...
33 */
34
35 # include <string.h>
36 # include <time.h>
37 # ifdef WIN32
38 # include <winsock.h>
39 # else
40 # include <unistd.h>
41 # include <sys/time.h>
42 # include <sys/types.h>
43 # include <sys/socket.h>
44 # include <netdb.h>
45 # include <netinet/in.h>
46 # include <arpa/inet.h>
47 # include <netinet/in_systm.h>
48 # include <netinet/ip.h>
49 # if !defined(__APPLE__) || !defined(TCP_NODELAY)
50 # include <netinet/tcp.h>
51 # endif /* !__APPLE__ || !TCP_NODELAY */
52 # ifdef AF_LOCAL
53 # include <sys/un.h>
54 # endif /* AF_LOCAL */
55 # endif /* WIN32 */
56
57 # include "md5.h"
58
59
60 /*
61 * C++ magic...
62 */
63
64 # ifdef __cplusplus
65 extern "C" {
66 # endif /* __cplusplus */
67
68
69 /*
70 * Oh, the wonderful world of IPv6 compatibility. Apparently some
71 * implementations expose the (more logical) 32-bit address parts
72 * to everyone, while others only expose it to kernel code... To
73 * make supporting IPv6 even easier, each vendor chose different
74 * core structure and union names, so the same defines or code
75 * can't be used on all platforms.
76 *
77 * The following will likely need tweeking on new platforms that
78 * support IPv6 - the "s6_addr32" define maps to the 32-bit integer
79 * array in the in6_addr union, which is named differently on various
80 * platforms.
81 */
82
83 #if defined(AF_INET6) && !defined(s6_addr32)
84 # if defined(__sun)
85 # define s6_addr32 _S6_un._S6_u32
86 # elif defined(__FreeBSD__) || defined(__APPLE__)
87 # define s6_addr32 __u6_addr.__u6_addr32
88 # endif /* __sun */
89 #endif /* AF_INET6 && !s6_addr32 */
90
91
92 /*
93 * Limits...
94 */
95
96 # define HTTP_MAX_URI 1024 /* Max length of URI string */
97 # define HTTP_MAX_HOST 256 /* Max length of hostname string */
98 # define HTTP_MAX_BUFFER 1024 /* Max length of data buffer */
99 # define HTTP_MAX_VALUE 256 /* Max header field value length */
100
101
102 /*
103 * HTTP state values...
104 */
105
106 typedef enum /* States are server-oriented */
107 {
108 HTTP_WAITING, /* Waiting for command */
109 HTTP_OPTIONS, /* OPTIONS command, waiting for blank line */
110 HTTP_GET, /* GET command, waiting for blank line */
111 HTTP_GET_SEND, /* GET command, sending data */
112 HTTP_HEAD, /* HEAD command, waiting for blank line */
113 HTTP_POST, /* POST command, waiting for blank line */
114 HTTP_POST_RECV, /* POST command, receiving data */
115 HTTP_POST_SEND, /* POST command, sending data */
116 HTTP_PUT, /* PUT command, waiting for blank line */
117 HTTP_PUT_RECV, /* PUT command, receiving data */
118 HTTP_DELETE, /* DELETE command, waiting for blank line */
119 HTTP_TRACE, /* TRACE command, waiting for blank line */
120 HTTP_CLOSE, /* CLOSE command, waiting for blank line */
121 HTTP_STATUS /* Command complete, sending status */
122 } http_state_t;
123
124
125 /*
126 * HTTP version numbers...
127 */
128
129 typedef enum
130 {
131 HTTP_0_9 = 9, /* HTTP/0.9 */
132 HTTP_1_0 = 100, /* HTTP/1.0 */
133 HTTP_1_1 = 101 /* HTTP/1.1 */
134 } http_version_t;
135
136
137 /*
138 * HTTP keep-alive values...
139 */
140
141 typedef enum
142 {
143 HTTP_KEEPALIVE_OFF = 0,
144 HTTP_KEEPALIVE_ON
145 } http_keepalive_t;
146
147
148 /*
149 * HTTP transfer encoding values...
150 */
151
152 typedef enum
153 {
154 HTTP_ENCODE_LENGTH, /* Data is sent with Content-Length */
155 HTTP_ENCODE_CHUNKED /* Data is chunked */
156 } http_encoding_t;
157
158
159 /*
160 * HTTP encryption values...
161 */
162
163 typedef enum
164 {
165 HTTP_ENCRYPT_IF_REQUESTED, /* Encrypt if requested (TLS upgrade) */
166 HTTP_ENCRYPT_NEVER, /* Never encrypt */
167 HTTP_ENCRYPT_REQUIRED, /* Encryption is required (TLS upgrade) */
168 HTTP_ENCRYPT_ALWAYS /* Always encrypt (SSL) */
169 } http_encryption_t;
170
171
172 /*
173 * HTTP authentication types...
174 */
175
176 typedef enum
177 {
178 HTTP_AUTH_NONE, /* No authentication in use */
179 HTTP_AUTH_BASIC, /* Basic authentication in use */
180 HTTP_AUTH_MD5, /* Digest authentication in use */
181 HTTP_AUTH_MD5_SESS, /* MD5-session authentication in use */
182 HTTP_AUTH_MD5_INT, /* Digest authentication in use for body */
183 HTTP_AUTH_MD5_SESS_INT /* MD5-session authentication in use for body */
184 } http_auth_t;
185
186
187 /*
188 * HTTP status codes...
189 */
190
191 typedef enum
192 {
193 HTTP_ERROR = -1, /* An error response from httpXxxx() */
194
195 HTTP_CONTINUE = 100, /* Everything OK, keep going... */
196 HTTP_SWITCHING_PROTOCOLS, /* HTTP upgrade to TLS/SSL */
197
198 HTTP_OK = 200, /* OPTIONS/GET/HEAD/POST/TRACE command was successful */
199 HTTP_CREATED, /* PUT command was successful */
200 HTTP_ACCEPTED, /* DELETE command was successful */
201 HTTP_NOT_AUTHORITATIVE, /* Information isn't authoritative */
202 HTTP_NO_CONTENT, /* Successful command, no new data */
203 HTTP_RESET_CONTENT, /* Content was reset/recreated */
204 HTTP_PARTIAL_CONTENT, /* Only a partial file was recieved/sent */
205
206 HTTP_MULTIPLE_CHOICES = 300, /* Multiple files match request */
207 HTTP_MOVED_PERMANENTLY, /* Document has moved permanently */
208 HTTP_MOVED_TEMPORARILY, /* Document has moved temporarily */
209 HTTP_SEE_OTHER, /* See this other link... */
210 HTTP_NOT_MODIFIED, /* File not modified */
211 HTTP_USE_PROXY, /* Must use a proxy to access this URI */
212
213 HTTP_BAD_REQUEST = 400, /* Bad request */
214 HTTP_UNAUTHORIZED, /* Unauthorized to access host */
215 HTTP_PAYMENT_REQUIRED, /* Payment required */
216 HTTP_FORBIDDEN, /* Forbidden to access this URI */
217 HTTP_NOT_FOUND, /* URI was not found */
218 HTTP_METHOD_NOT_ALLOWED, /* Method is not allowed */
219 HTTP_NOT_ACCEPTABLE, /* Not Acceptable */
220 HTTP_PROXY_AUTHENTICATION, /* Proxy Authentication is Required */
221 HTTP_REQUEST_TIMEOUT, /* Request timed out */
222 HTTP_CONFLICT, /* Request is self-conflicting */
223 HTTP_GONE, /* Server has gone away */
224 HTTP_LENGTH_REQUIRED, /* A content length or encoding is required */
225 HTTP_PRECONDITION, /* Precondition failed */
226 HTTP_REQUEST_TOO_LARGE, /* Request entity too large */
227 HTTP_URI_TOO_LONG, /* URI too long */
228 HTTP_UNSUPPORTED_MEDIATYPE, /* The requested media type is unsupported */
229 HTTP_UPGRADE_REQUIRED = 426, /* Upgrade to SSL/TLS required */
230
231 HTTP_SERVER_ERROR = 500, /* Internal server error */
232 HTTP_NOT_IMPLEMENTED, /* Feature not implemented */
233 HTTP_BAD_GATEWAY, /* Bad gateway */
234 HTTP_SERVICE_UNAVAILABLE, /* Service is unavailable */
235 HTTP_GATEWAY_TIMEOUT, /* Gateway connection timed out */
236 HTTP_NOT_SUPPORTED /* HTTP version not supported */
237 } http_status_t;
238
239
240 /*
241 * HTTP field names...
242 */
243
244 typedef enum
245 {
246 HTTP_FIELD_UNKNOWN = -1,
247 HTTP_FIELD_ACCEPT_LANGUAGE,
248 HTTP_FIELD_ACCEPT_RANGES,
249 HTTP_FIELD_AUTHORIZATION,
250 HTTP_FIELD_CONNECTION,
251 HTTP_FIELD_CONTENT_ENCODING,
252 HTTP_FIELD_CONTENT_LANGUAGE,
253 HTTP_FIELD_CONTENT_LENGTH,
254 HTTP_FIELD_CONTENT_LOCATION,
255 HTTP_FIELD_CONTENT_MD5,
256 HTTP_FIELD_CONTENT_RANGE,
257 HTTP_FIELD_CONTENT_TYPE,
258 HTTP_FIELD_CONTENT_VERSION,
259 HTTP_FIELD_DATE,
260 HTTP_FIELD_HOST,
261 HTTP_FIELD_IF_MODIFIED_SINCE,
262 HTTP_FIELD_IF_UNMODIFIED_SINCE,
263 HTTP_FIELD_KEEP_ALIVE,
264 HTTP_FIELD_LAST_MODIFIED,
265 HTTP_FIELD_LINK,
266 HTTP_FIELD_LOCATION,
267 HTTP_FIELD_RANGE,
268 HTTP_FIELD_REFERER,
269 HTTP_FIELD_RETRY_AFTER,
270 HTTP_FIELD_TRANSFER_ENCODING,
271 HTTP_FIELD_UPGRADE,
272 HTTP_FIELD_USER_AGENT,
273 HTTP_FIELD_WWW_AUTHENTICATE,
274 HTTP_FIELD_MAX
275 } http_field_t;
276
277
278 /*
279 * HTTP address structure (makes using IPv6 a little easier and more portable.)
280 */
281
282 typedef union
283 {
284 struct sockaddr addr; /* Base structure for family value */
285 struct sockaddr_in ipv4; /* IPv4 address */
286 #ifdef AF_INET6
287 struct sockaddr_in6 ipv6; /* IPv6 address */
288 #endif /* AF_INET6 */
289 #ifdef AF_LOCAL
290 struct sockaddr_un un; /* Domain socket file */
291 #endif /* AF_LOCAL */
292 char pad[128]; /* Pad to ensure binary compatibility */
293 } http_addr_t;
294
295 /*
296 * HTTP connection structure...
297 */
298
299 typedef struct
300 {
301 int fd; /* File descriptor for this socket */
302 int blocking; /* To block or not to block */
303 int error; /* Last error on read */
304 time_t activity; /* Time since last read/write */
305 http_state_t state; /* State of client */
306 http_status_t status; /* Status of last request */
307 http_version_t version; /* Protocol version */
308 http_keepalive_t keep_alive; /* Keep-alive supported? */
309 struct sockaddr_in oldaddr; /* Address of connected host */
310 char hostname[HTTP_MAX_HOST],
311 /* Name of connected host */
312 fields[HTTP_FIELD_MAX][HTTP_MAX_VALUE];
313 /* Field values */
314 char *data; /* Pointer to data buffer */
315 http_encoding_t data_encoding; /* Chunked or not */
316 int data_remaining; /* Number of bytes left */
317 int used; /* Number of bytes used in buffer */
318 char buffer[HTTP_MAX_BUFFER];
319 /* Buffer for incoming data */
320 char wbuffer[HTTP_MAX_BUFFER];
321 /* Buffer for outgoing data */
322 int auth_type; /* Authentication in use */
323 _cups_md5_state_t md5_state; /* MD5 state */
324 char nonce[HTTP_MAX_VALUE];
325 /* Nonce value */
326 int nonce_count; /* Nonce count */
327 void *tls; /* TLS state information */
328 http_encryption_t encryption; /* Encryption requirements */
329 /**** New in CUPS 1.1.19 ****/
330 fd_set *input_set; /* select() set for httpWait() */
331 http_status_t expect; /* Expect: header */
332 char *cookie; /* Cookie value(s) */
333 /**** New in CUPS 1.1.20 ****/
334 char authstring[HTTP_MAX_VALUE],
335 /* Current Authentication value */
336 userpass[HTTP_MAX_VALUE];
337 /* Username:password string */
338 int digest_tries; /* Number of tries for digest auth */
339 /**** New in CUPS 1.2 ****/
340 http_addr_t hostaddr; /* Host address and port */
341 int wused; /* Write buffer bytes used */
342 } http_t;
343
344 /**** New in CUPS 1.1.20+ ****/
345 extern int httpWriteFlush(http_t *http);
346
347 /*
348 * Prototypes...
349 */
350
351 # define httpBlocking(http,b) (http)->blocking = (b)
352 extern int httpCheck(http_t *http);
353 # define httpClearFields(http) memset((http)->fields, 0, sizeof((http)->fields)),\
354 httpSetField((http), HTTP_FIELD_HOST, (http)->hostname)
355 extern void httpClose(http_t *http);
356 extern http_t *httpConnect(const char *host, int port);
357 extern http_t *httpConnectEncrypt(const char *host, int port,
358 http_encryption_t encryption);
359 extern int httpDelete(http_t *http, const char *uri);
360 extern int httpEncryption(http_t *http, http_encryption_t e);
361 # define httpError(http) ((http)->error)
362 extern void httpFlush(http_t *http);
363 extern int httpGet(http_t *http, const char *uri);
364 extern char *httpGets(char *line, int length, http_t *http);
365 extern const char *httpGetDateString(time_t t);
366 extern time_t httpGetDateTime(const char *s);
367 # define httpGetField(http,field) (http)->fields[field]
368 extern struct hostent *httpGetHostByName(const char *name);
369 extern char *httpGetSubField(http_t *http, http_field_t field,
370 const char *name, char *value);
371 extern int httpHead(http_t *http, const char *uri);
372 extern void httpInitialize(void);
373 extern int httpOptions(http_t *http, const char *uri);
374 extern int httpPost(http_t *http, const char *uri);
375 extern int httpPrintf(http_t *http, const char *format, ...)
376 # ifdef __GNUC__
377 __attribute__ ((__format__ (__printf__, 2, 3)))
378 # endif /* __GNUC__ */
379 ;
380 extern int httpPut(http_t *http, const char *uri);
381 extern int httpRead(http_t *http, char *buffer, int length);
382 extern int httpReconnect(http_t *http);
383 extern void httpSeparate(const char *uri, char *method,
384 char *username, char *host, int *port,
385 char *resource);
386 extern void httpSetField(http_t *http, http_field_t field,
387 const char *value);
388 extern const char *httpStatus(http_status_t status);
389 extern int httpTrace(http_t *http, const char *uri);
390 extern http_status_t httpUpdate(http_t *http);
391 extern int httpWrite(http_t *http, const char *buffer, int length);
392 extern char *httpEncode64(char *out, const char *in);
393 extern char *httpDecode64(char *out, const char *in);
394 extern int httpGetLength(http_t *http);
395 extern char *httpMD5(const char *, const char *, const char *,
396 char [33]);
397 extern char *httpMD5Final(const char *, const char *, const char *,
398 char [33]);
399 extern char *httpMD5String(const unsigned char *, char [33]);
400
401 /**** New in CUPS 1.1.19 ****/
402 extern void httpClearCookie(http_t *http);
403 #define httpGetCookie(http) ((http)->cookie)
404 extern void httpSetCookie(http_t *http, const char *cookie);
405 extern int httpWait(http_t *http, int msec);
406
407 /**** New in CUPS 1.1.21 ****/
408 extern char *httpDecode64_2(char *out, int *outlen, const char *in);
409 extern char *httpEncode64_2(char *out, int outlen, const char *in,
410 int inlen);
411 extern void httpSeparate2(const char *uri,
412 char *method, int methodlen,
413 char *username, int usernamelen,
414 char *host, int hostlen, int *port,
415 char *resource, int resourcelen);
416
417 /**** New in CUPS 1.2 ****/
418 extern int httpAddrAny(const http_addr_t *addr);
419 extern int httpAddrEqual(const http_addr_t *addr1,
420 const http_addr_t *addr2);
421 extern void httpAddrLoad(const struct hostent *host, int port,
422 int n, http_addr_t *addr);
423 extern int httpAddrLocalhost(const http_addr_t *addr);
424 extern char *httpAddrLookup(const http_addr_t *addr,
425 char *name, int namelen);
426 extern char *httpAddrString(const http_addr_t *addr,
427 char *s, int slen);
428 extern const char *httpGetDateString2(time_t t, char *s, int slen);
429 extern const char *httpGetHostname(char *s, int slen);
430 extern void httpFlushWrite(http_t *http);
431
432
433 /*
434 * C++ magic...
435 */
436
437 # ifdef __cplusplus
438 }
439 # endif /* __cplusplus */
440 #endif /* !_CUPS_HTTP_H_ */
441
442 /*
443 * End of "$Id$".
444 */