1 /* 2 * "$Id: http.h 148 2006-04-25 16:54:17Z njacobs $" 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 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 /* 34 * Include necessary headers... 35 */ 36 37 # include <string.h> 38 # include <time.h> 39 # ifdef WIN32 40 # include <winsock.h> 41 # else 42 # include <unistd.h> 43 # include <sys/time.h> 44 # include <sys/types.h> 45 # include <sys/socket.h> 46 # include <netdb.h> 47 # include <netinet/in.h> 48 # include <arpa/inet.h> 49 # include <netinet/in_systm.h> 50 # include <netinet/ip.h> 51 # if !defined(__APPLE__) || !defined(TCP_NODELAY) 52 # include <netinet/tcp.h> 53 # endif /* !__APPLE__ || !TCP_NODELAY */ 54 # ifdef AF_LOCAL 55 # include <sys/un.h> 56 # endif /* AF_LOCAL */ 57 # endif /* WIN32 */ 58 59 60 /* 61 * C++ magic... 62 */ 63 64 #ifdef __cplusplus 65 extern "C" { 66 #endif 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 2048 /* 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 messages */ 320 int auth_type; /* Authentication in use */ 321 char nonce[HTTP_MAX_VALUE]; 322 /* Nonce value */ 323 int nonce_count; /* Nonce count */ 324 void *tls; /* TLS state information */ 325 http_encryption_t encryption; /* Encryption requirements */ 326 /**** New in CUPS 1.1.19 ****/ 327 fd_set *input_set; /* select() set for httpWait() */ 328 http_status_t expect; /* Expect: header */ 329 char *cookie; /* Cookie value(s) */ 330 /**** New in CUPS 1.1.20 ****/ 331 char authstring[HTTP_MAX_VALUE], 332 /* Current Authentication value */ 333 userpass[HTTP_MAX_VALUE]; 334 /* Username:password string */ 335 int digest_tries; /* Number of tries for digest auth */ 336 /**** New in CUPS 1.2 ****/ 337 http_addr_t hostaddr; /* Host address and port */ 338 } http_t; 339 340 341 /* 342 * Prototypes... 343 */ 344 345 # define httpBlocking(http,b) (http)->blocking = (b) 346 extern int httpCheck(http_t *http); 347 # define httpClearFields(http) memset((http)->fields, 0, sizeof((http)->fields)),\ 348 httpSetField((http), HTTP_FIELD_HOST, (http)->hostname) 349 extern void httpClose(http_t *http); 350 extern http_t *httpConnect(const char *host, int port); 351 extern http_t *httpConnectEncrypt(const char *host, int port, 352 http_encryption_t encrypt); 353 extern int httpDelete(http_t *http, const char *uri); 354 extern int httpEncryption(http_t *http, http_encryption_t e); 355 # define httpError(http) ((http)->error) 356 extern void httpFlush(http_t *http); 357 extern int httpGet(http_t *http, const char *uri); 358 extern char *httpGets(char *line, int length, http_t *http); 359 extern const char *httpGetDateString(time_t t); 360 extern time_t httpGetDateTime(const char *s); 361 # define httpGetField(http,field) (http)->fields[field] 362 extern struct hostent *httpGetHostByName(const char *name); 363 extern char *httpGetSubField(http_t *http, http_field_t field, 364 const char *name, char *value); 365 extern int httpHead(http_t *http, const char *uri); 366 extern void httpInitialize(void); 367 extern int httpOptions(http_t *http, const char *uri); 368 extern int httpPost(http_t *http, const char *uri); 369 extern int httpPrintf(http_t *http, const char *format, ...) 370 # ifdef __GNUC__ 371 __attribute__ ((__format__ (__printf__, 2, 3))) 372 # endif /* __GNUC__ */ 373 ; 374 extern int httpPut(http_t *http, const char *uri); 375 extern int httpRead(http_t *http, char *buffer, int length); 376 extern int httpReconnect(http_t *http); 377 extern void httpSeparate(const char *uri, char *method, 378 char *username, char *host, int *port, 379 char *resource); 380 extern void httpSetField(http_t *http, http_field_t field, 381 const char *value); 382 extern const char *httpStatus(http_status_t status); 383 extern int httpTrace(http_t *http, const char *uri); 384 extern http_status_t httpUpdate(http_t *http); 385 extern int httpWrite(http_t *http, const char *buffer, int length); 386 extern char *httpEncode64(char *out, const char *in); 387 extern char *httpDecode64(char *out, const char *in); 388 extern int httpGetLength(http_t *http); 389 extern char *httpMD5(const char *, const char *, const char *, 390 char [33]); 391 extern char *httpMD5Final(const char *, const char *, const char *, 392 char [33]); 393 extern char *httpMD5String(const unsigned char *, char [33]); 394 395 /**** New in CUPS 1.1.19 ****/ 396 extern void httpClearCookie(http_t *http); 397 #define httpGetCookie(http) ((http)->cookie) 398 extern void httpSetCookie(http_t *http, const char *cookie); 399 extern int httpWait(http_t *http, int msec); 400 401 /**** New in CUPS 1.1.21 ****/ 402 extern char *httpDecode64_2(char *out, int *outlen, const char *in); 403 extern char *httpEncode64_2(char *out, int outlen, const char *in, 404 int inlen); 405 extern void httpSeparate2(const char *uri, 406 char *method, int methodlen, 407 char *username, int usernamelen, 408 char *host, int hostlen, int *port, 409 char *resource, int resourcelen); 410 411 /**** New in CUPS 1.2 ****/ 412 extern int httpAddrAny(const http_addr_t *addr); 413 extern int httpAddrEqual(const http_addr_t *addr1, 414 const http_addr_t *addr2); 415 extern void httpAddrLoad(const struct hostent *host, int port, 416 int n, http_addr_t *addr); 417 extern int httpAddrLocalhost(const http_addr_t *addr); 418 extern char *httpAddrLookup(const http_addr_t *addr, 419 char *name, int namelen); 420 extern char *httpAddrString(const http_addr_t *addr, 421 char *s, int slen); 422 423 #include <stdio.h> 424 extern void httpDumpData(FILE *, const char *, const char *, int); 425 426 427 /* 428 * C++ magic... 429 */ 430 431 #ifdef __cplusplus 432 } 433 #endif 434 435 #endif /* !_CUPS_HTTP_H_ */ 436 437 /* 438 * End of "$Id: http.h 148 2006-04-25 16:54:17Z njacobs $" 439 */ 440