4 * Hyper-Text Transport Protocol definitions for the Common UNIX Printing
7 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
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
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636 USA
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
25 * This file is subject to the Apple OS-Developed Software exception.
29 # define _CUPS_HTTP_H_
32 * Include necessary headers...
41 # include <sys/time.h>
42 # include <sys/types.h>
43 # include <sys/socket.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 */
54 # endif /* AF_LOCAL */
66 # endif /* __cplusplus */
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.
77 * The following will likely need tweaking 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
83 #if defined(AF_INET6) && !defined(s6_addr32)
85 # define s6_addr32 _S6_un._S6_u32
86 # elif defined(__FreeBSD__) || defined(__APPLE__)
87 # define s6_addr32 __u6_addr.__u6_addr32
89 #endif /* AF_INET6 && !s6_addr32 */
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 */
103 * Types and structures...
106 typedef enum http_state_e
/**** HTTP state values; states
107 **** are server-oriented...
110 HTTP_WAITING
, /* Waiting for command */
111 HTTP_OPTIONS
, /* OPTIONS command, waiting for blank line */
112 HTTP_GET
, /* GET command, waiting for blank line */
113 HTTP_GET_SEND
, /* GET command, sending data */
114 HTTP_HEAD
, /* HEAD command, waiting for blank line */
115 HTTP_POST
, /* POST command, waiting for blank line */
116 HTTP_POST_RECV
, /* POST command, receiving data */
117 HTTP_POST_SEND
, /* POST command, sending data */
118 HTTP_PUT
, /* PUT command, waiting for blank line */
119 HTTP_PUT_RECV
, /* PUT command, receiving data */
120 HTTP_DELETE
, /* DELETE command, waiting for blank line */
121 HTTP_TRACE
, /* TRACE command, waiting for blank line */
122 HTTP_CLOSE
, /* CLOSE command, waiting for blank line */
123 HTTP_STATUS
/* Command complete, sending status */
126 typedef enum http_version_e
/**** HTTP version numbers ****/
128 HTTP_0_9
= 9, /* HTTP/0.9 */
129 HTTP_1_0
= 100, /* HTTP/1.0 */
130 HTTP_1_1
= 101 /* HTTP/1.1 */
133 typedef enum http_keepalive_e
/**** HTTP keep-alive values ****/
135 HTTP_KEEPALIVE_OFF
= 0, /* No keep alive support */
136 HTTP_KEEPALIVE_ON
/* Use keep alive */
139 typedef enum http_encoding_e
/**** HTTP transfer encoding values ****/
141 HTTP_ENCODE_LENGTH
, /* Data is sent with Content-Length */
142 HTTP_ENCODE_CHUNKED
/* Data is chunked */
145 typedef enum http_encryption_e
/**** HTTP encryption values ****/
147 HTTP_ENCRYPT_IF_REQUESTED
, /* Encrypt if requested (TLS upgrade) */
148 HTTP_ENCRYPT_NEVER
, /* Never encrypt */
149 HTTP_ENCRYPT_REQUIRED
, /* Encryption is required (TLS upgrade) */
150 HTTP_ENCRYPT_ALWAYS
/* Always encrypt (SSL) */
153 typedef enum http_auth_e
/**** HTTP authentication types ****/
155 HTTP_AUTH_NONE
, /* No authentication in use */
156 HTTP_AUTH_BASIC
, /* Basic authentication in use */
157 HTTP_AUTH_MD5
, /* Digest authentication in use */
158 HTTP_AUTH_MD5_SESS
, /* MD5-session authentication in use */
159 HTTP_AUTH_MD5_INT
, /* Digest authentication in use for body */
160 HTTP_AUTH_MD5_SESS_INT
/* MD5-session authentication in use for body */
163 typedef enum http_status_e
/**** HTTP status codes ****/
165 HTTP_ERROR
= -1, /* An error response from httpXxxx() */
167 HTTP_CONTINUE
= 100, /* Everything OK, keep going... */
168 HTTP_SWITCHING_PROTOCOLS
, /* HTTP upgrade to TLS/SSL */
170 HTTP_OK
= 200, /* OPTIONS/GET/HEAD/POST/TRACE command was successful */
171 HTTP_CREATED
, /* PUT command was successful */
172 HTTP_ACCEPTED
, /* DELETE command was successful */
173 HTTP_NOT_AUTHORITATIVE
, /* Information isn't authoritative */
174 HTTP_NO_CONTENT
, /* Successful command, no new data */
175 HTTP_RESET_CONTENT
, /* Content was reset/recreated */
176 HTTP_PARTIAL_CONTENT
, /* Only a partial file was recieved/sent */
178 HTTP_MULTIPLE_CHOICES
= 300, /* Multiple files match request */
179 HTTP_MOVED_PERMANENTLY
, /* Document has moved permanently */
180 HTTP_MOVED_TEMPORARILY
, /* Document has moved temporarily */
181 HTTP_SEE_OTHER
, /* See this other link... */
182 HTTP_NOT_MODIFIED
, /* File not modified */
183 HTTP_USE_PROXY
, /* Must use a proxy to access this URI */
185 HTTP_BAD_REQUEST
= 400, /* Bad request */
186 HTTP_UNAUTHORIZED
, /* Unauthorized to access host */
187 HTTP_PAYMENT_REQUIRED
, /* Payment required */
188 HTTP_FORBIDDEN
, /* Forbidden to access this URI */
189 HTTP_NOT_FOUND
, /* URI was not found */
190 HTTP_METHOD_NOT_ALLOWED
, /* Method is not allowed */
191 HTTP_NOT_ACCEPTABLE
, /* Not Acceptable */
192 HTTP_PROXY_AUTHENTICATION
, /* Proxy Authentication is Required */
193 HTTP_REQUEST_TIMEOUT
, /* Request timed out */
194 HTTP_CONFLICT
, /* Request is self-conflicting */
195 HTTP_GONE
, /* Server has gone away */
196 HTTP_LENGTH_REQUIRED
, /* A content length or encoding is required */
197 HTTP_PRECONDITION
, /* Precondition failed */
198 HTTP_REQUEST_TOO_LARGE
, /* Request entity too large */
199 HTTP_URI_TOO_LONG
, /* URI too long */
200 HTTP_UNSUPPORTED_MEDIATYPE
, /* The requested media type is unsupported */
201 HTTP_UPGRADE_REQUIRED
= 426, /* Upgrade to SSL/TLS required */
203 HTTP_SERVER_ERROR
= 500, /* Internal server error */
204 HTTP_NOT_IMPLEMENTED
, /* Feature not implemented */
205 HTTP_BAD_GATEWAY
, /* Bad gateway */
206 HTTP_SERVICE_UNAVAILABLE
, /* Service is unavailable */
207 HTTP_GATEWAY_TIMEOUT
, /* Gateway connection timed out */
208 HTTP_NOT_SUPPORTED
/* HTTP version not supported */
211 typedef enum http_field_e
/**** HTTP field names ****/
213 HTTP_FIELD_UNKNOWN
= -1, /* Unknown field */
214 HTTP_FIELD_ACCEPT_LANGUAGE
, /* Accept-Language field */
215 HTTP_FIELD_ACCEPT_RANGES
, /* Accept-Ranges field */
216 HTTP_FIELD_AUTHORIZATION
, /* Authorization field */
217 HTTP_FIELD_CONNECTION
, /* Connection field */
218 HTTP_FIELD_CONTENT_ENCODING
, /* Content-Encoding field */
219 HTTP_FIELD_CONTENT_LANGUAGE
, /* Content-Language field */
220 HTTP_FIELD_CONTENT_LENGTH
, /* Content-Length field */
221 HTTP_FIELD_CONTENT_LOCATION
, /* Content-Location field */
222 HTTP_FIELD_CONTENT_MD5
, /* Content-MD5 field */
223 HTTP_FIELD_CONTENT_RANGE
, /* Content-Range field */
224 HTTP_FIELD_CONTENT_TYPE
, /* Content-Type field */
225 HTTP_FIELD_CONTENT_VERSION
, /* Content-Version field */
226 HTTP_FIELD_DATE
, /* Date field */
227 HTTP_FIELD_HOST
, /* Host field */
228 HTTP_FIELD_IF_MODIFIED_SINCE
, /* If-Modified-Since field */
229 HTTP_FIELD_IF_UNMODIFIED_SINCE
, /* If-Unmodified-Since field */
230 HTTP_FIELD_KEEP_ALIVE
, /* Keep-Alive field */
231 HTTP_FIELD_LAST_MODIFIED
, /* Last-Modified field */
232 HTTP_FIELD_LINK
, /* Link field */
233 HTTP_FIELD_LOCATION
, /* Location field */
234 HTTP_FIELD_RANGE
, /* Range field */
235 HTTP_FIELD_REFERER
, /* Referer field */
236 HTTP_FIELD_RETRY_AFTER
, /* Retry-After field */
237 HTTP_FIELD_TRANSFER_ENCODING
, /* Transfer-Encoding field */
238 HTTP_FIELD_UPGRADE
, /* Upgrade field */
239 HTTP_FIELD_USER_AGENT
, /* User-Agent field */
240 HTTP_FIELD_WWW_AUTHENTICATE
, /* WWW-Authenticate field */
241 HTTP_FIELD_MAX
/* Maximum field index */
244 typedef union http_addr_u
/**** Socket address union, which
245 **** makes using IPv6 and other
246 **** address types easier and
247 **** more portable. @since CUPS 1.2@
250 struct sockaddr addr
; /* Base structure for family value */
251 struct sockaddr_in ipv4
; /* IPv4 address */
253 struct sockaddr_in6 ipv6
; /* IPv6 address */
254 #endif /* AF_INET6 */
256 struct sockaddr_un un
; /* Domain socket file */
257 #endif /* AF_LOCAL */
258 char pad
[256]; /* Padding to ensure binary compatibility */
261 typedef struct http_addrlist_s
/**** Socket address list, which is
262 **** used to enumerate all of the
263 **** addresses that are associated
264 **** with a hostname. @since CUPS 1.2@
267 struct http_addrlist_s
*next
; /* Pointer to next address in list */
268 http_addr_t addr
; /* Address */
271 typedef struct http_s
/**** HTTP connection structure. ****/
273 int fd
; /* File descriptor for this socket */
274 int blocking
; /* To block or not to block */
275 int error
; /* Last error on read */
276 time_t activity
; /* Time since last read/write */
277 http_state_t state
; /* State of client */
278 http_status_t status
; /* Status of last request */
279 http_version_t version
; /* Protocol version */
280 http_keepalive_t keep_alive
; /* Keep-alive supported? */
281 struct sockaddr_in _hostaddr
; /* Address of connected host @deprecated@ */
282 char hostname
[HTTP_MAX_HOST
],
283 /* Name of connected host */
284 fields
[HTTP_FIELD_MAX
][HTTP_MAX_VALUE
];
286 char *data
; /* Pointer to data buffer */
287 http_encoding_t data_encoding
; /* Chunked or not */
288 int _data_remaining
;/* Number of bytes left @deprecated@ */
289 int used
; /* Number of bytes used in buffer */
290 char buffer
[HTTP_MAX_BUFFER
];
291 /* Buffer for incoming data */
292 char wbuffer
[HTTP_MAX_BUFFER
];
293 /* Buffer for outgoing data */
294 int auth_type
; /* Authentication in use */
295 _cups_md5_state_t md5_state
; /* MD5 state */
296 char nonce
[HTTP_MAX_VALUE
];
298 int nonce_count
; /* Nonce count */
299 void *tls
; /* TLS state information */
300 http_encryption_t encryption
; /* Encryption requirements */
301 /**** New in CUPS 1.1.19 ****/
302 fd_set
*input_set
; /* select() set for httpWait() @since CUPS 1.1.19@ */
303 http_status_t expect
; /* Expect: header @since CUPS 1.1.19@ */
304 char *cookie
; /* Cookie value(s) @since CUPS 1.1.19@ */
305 /**** New in CUPS 1.1.20 ****/
306 char authstring
[HTTP_MAX_VALUE
],
307 /* Current Authentication value @since CUPS 1.1.20@ */
308 userpass
[HTTP_MAX_VALUE
];
309 /* Username:password string @since CUPS 1.1.20@ */
310 int digest_tries
; /* Number of tries for digest auth @since CUPS 1.1.20@ */
311 /**** New in CUPS 1.2 ****/
312 http_addr_t
*hostaddr
; /* Current host address and port @since CUPS 1.2@ */
313 http_addrlist_t
*addrlist
; /* List of valid addresses @since CUPS 1.2@ */
314 int wused
; /* Write buffer bytes used @since CUPS 1.2@ */
315 off_t data_remaining
; /* Number of bytes left @since CUPS 1.2@ */
323 # define httpBlocking(http,b) (http)->blocking = (b)
324 extern int httpCheck(http_t
*http
);
325 # define httpClearFields(http) memset((http)->fields, 0, sizeof((http)->fields)),\
326 httpSetField((http), HTTP_FIELD_HOST, (http)->hostname)
327 extern void httpClose(http_t
*http
);
328 extern http_t
*httpConnect(const char *host
, int port
);
329 extern http_t
*httpConnectEncrypt(const char *host
, int port
,
330 http_encryption_t encryption
);
331 extern int httpDelete(http_t
*http
, const char *uri
);
332 extern int httpEncryption(http_t
*http
, http_encryption_t e
);
333 # define httpError(http) ((http)->error)
334 extern void httpFlush(http_t
*http
);
335 extern int httpGet(http_t
*http
, const char *uri
);
336 extern char *httpGets(char *line
, int length
, http_t
*http
);
337 extern const char *httpGetDateString(time_t t
);
338 extern time_t httpGetDateTime(const char *s
);
339 # define httpGetField(http,field) (http)->fields[field]
340 extern struct hostent
*httpGetHostByName(const char *name
);
341 extern char *httpGetSubField(http_t
*http
, http_field_t field
,
342 const char *name
, char *value
);
343 extern int httpHead(http_t
*http
, const char *uri
);
344 extern void httpInitialize(void);
345 extern int httpOptions(http_t
*http
, const char *uri
);
346 extern int httpPost(http_t
*http
, const char *uri
);
347 extern int httpPrintf(http_t
*http
, const char *format
, ...)
349 __attribute__ ((__format__ (__printf__
, 2, 3)))
350 # endif /* __GNUC__ */
352 extern int httpPut(http_t
*http
, const char *uri
);
353 extern int httpRead(http_t
*http
, char *buffer
, int length
);
354 extern int httpReconnect(http_t
*http
);
355 extern void httpSeparate(const char *uri
, char *method
,
356 char *username
, char *host
, int *port
,
358 extern void httpSetField(http_t
*http
, http_field_t field
,
360 extern const char *httpStatus(http_status_t status
);
361 extern int httpTrace(http_t
*http
, const char *uri
);
362 extern http_status_t
httpUpdate(http_t
*http
);
363 extern int httpWrite(http_t
*http
, const char *buffer
, int length
);
364 extern char *httpEncode64(char *out
, const char *in
);
365 extern char *httpDecode64(char *out
, const char *in
);
366 extern int httpGetLength(http_t
*http
);
367 extern char *httpMD5(const char *, const char *, const char *,
369 extern char *httpMD5Final(const char *, const char *, const char *,
371 extern char *httpMD5String(const unsigned char *, char [33]);
373 /**** New in CUPS 1.1.19 ****/
374 extern void httpClearCookie(http_t
*http
);
375 #define httpGetCookie(http) ((http)->cookie)
376 extern void httpSetCookie(http_t
*http
, const char *cookie
);
377 extern int httpWait(http_t
*http
, int msec
);
379 /**** New in CUPS 1.1.21 ****/
380 extern char *httpDecode64_2(char *out
, int *outlen
, const char *in
);
381 extern char *httpEncode64_2(char *out
, int outlen
, const char *in
,
383 extern void httpSeparate2(const char *uri
,
384 char *method
, int methodlen
,
385 char *username
, int usernamelen
,
386 char *host
, int hostlen
, int *port
,
387 char *resource
, int resourcelen
);
389 /**** New in CUPS 1.2 ****/
390 extern int httpAddrAny(const http_addr_t
*addr
);
391 extern http_addrlist_t
*httpAddrConnect(http_addrlist_t
*addrlist
, int *sock
);
392 extern int httpAddrEqual(const http_addr_t
*addr1
,
393 const http_addr_t
*addr2
);
394 extern void httpAddrFreeList(http_addrlist_t
*addrlist
);
395 extern http_addrlist_t
*httpAddrGetList(const char *hostname
, int family
,
396 const char *service
);
397 extern int httpAddrLength(const http_addr_t
*addr
);
398 extern int httpAddrLocalhost(const http_addr_t
*addr
);
399 extern char *httpAddrLookup(const http_addr_t
*addr
,
400 char *name
, int namelen
);
401 extern char *httpAddrString(const http_addr_t
*addr
,
403 extern int httpFlushWrite(http_t
*http
);
404 extern const char *httpGetDateString2(time_t t
, char *s
, int slen
);
405 extern const char *httpGetHostname(char *s
, int slen
);
406 extern off_t
httpGetLength2(http_t
*http
);
407 extern void httpSetLength(http_t
*http
, off_t length
);
416 # endif /* __cplusplus */
417 #endif /* !_CUPS_HTTP_H_ */