Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals

http.h

Go to the documentation of this file.
00001 #ifndef _CP_HTTP_H
00002 #define _CP_HTTP_H
00003 
00014 #include "config.h"
00015 #include "common.h"
00016 
00017 #ifdef _WINDOWS
00018 #include <Winsock2.h>
00019 #endif
00020 
00021 __BEGIN_DECLS
00022 
00023 #include "hashtable.h"
00024 #include "trie.h"
00025 #include "vector.h"
00026 #include "socket.h"
00027 #include "str.h"
00028 
00029 #include <string.h>
00030 #include <time.h>
00031 
00032 /* HTTP 1.1 defines these methods (rfc 2616)
00033  * 
00034  *     Method         = "OPTIONS"
00035  *                    | "GET"   
00036  *                    | "HEAD"  
00037  *                    | "POST"  
00038  *                    | "PUT"   
00039  *                    | "DELETE"
00040  *                    | "TRACE" 
00041  *                    | "CONNECT"
00042  *
00043  *     general-header = Cache-Control     
00044  *                    | Connection        
00045  *                    | Date              
00046  *                    | Pragma            
00047  *                    | Trailer           
00048  *                    | Transfer-Encoding 
00049  *                    | Upgrade           
00050  *                    | Via               
00051  *                    | Warning           
00052  *
00053  *
00054  * 
00055  *     request-header = Accept             
00056  *                    | Accept-Charset     
00057  *                    | Accept-Encoding    
00058  *                    | Accept-Language    
00059  *                    | Authorization      
00060  *                    | Expect             
00061  *                    | From               
00062  *                    | Host               
00063  *                    | If-Match           
00064  *                    | If-Modified-Since  
00065  *                    | If-None-Match      
00066  *                    | If-Range           
00067  *                    | If-Unmodified-Since
00068  *                    | Max-Forwards       
00069  *                    | Proxy-Authorization
00070  *                    | Range              
00071  *                    | Referer            
00072  *                    | TE                 
00073  *                    | User-Agent         
00074  *
00075  *
00076  *     entity-header  = Allow              
00077  *                    | Content-Encoding   
00078  *                    | Content-Language   
00079  *                    | Content-Length     
00080  *                    | Content-Location   
00081  *                    | Content-MD5        
00082  *                    | Content-Range      
00083  *                    | Content-Type       
00084  *                    | Expires            
00085  *                    | Last-Modified      
00086  *
00087  *
00088  *     Status-Code    = "100"  : Continue
00089  *                    | "101"  : Switching Protocols
00090  *                    | "200"  : OK
00091  *                    | "201"  : Created
00092  *                    | "202"  : Accepted
00093  *                    | "203"  : Non-Authoritative Information
00094  *                    | "204"  : No Content
00095  *                    | "205"  : Reset Content
00096  *                    | "206"  : Partial Content
00097  *                    | "300"  : Multiple Choices
00098  *                    | "301"  : Moved Permanently
00099  *                    | "302"  : Found
00100  *                    | "303"  : See Other
00101  *                    | "304"  : Not Modified
00102  *                    | "305"  : Use Proxy
00103  *                    | "307"  : Temporary Redirect
00104  *                    | "400"  : Bad Request
00105  *                    | "401"  : Unauthorized
00106  *                    | "402"  : Payment Required
00107  *                    | "403"  : Forbidden
00108  *                    | "404"  : Not Found
00109  *                    | "405"  : Method Not Allowed
00110  *                    | "406"  : Not Acceptable
00111  *                    | "407"  : Proxy Authentication Required
00112  *                    | "408"  : Request Time-out
00113  *                    | "409"  : Conflict
00114  *                    | "410"  : Gone
00115  *                    | "411"  : Length Required
00116  *                    | "412"  : Precondition Failed
00117  *                    | "413"  : Request Entity Too Large
00118  *                    | "414"  : Request-URI Too Large
00119  *                    | "415"  : Unsupported Media Type
00120  *                    | "416"  : Requested range not satisfiable
00121  *                    | "417"  : Expectation Failed
00122  *                    | "500"  : Internal Server Error
00123  *                    | "501"  : Not Implemented
00124  *                    | "502"  : Bad Gateway
00125  *                    | "503"  : Service Unavailable
00126  *                    | "504"  : Gateway Time-out
00127  *                    | "505"  : HTTP Version not supported
00128  *
00129  * 
00130  *
00131  *    response-header = Accept-Ranges           ; Section 14.5
00132  *                    | Age                     ; Section 14.6
00133  *                    | ETag                    ; Section 14.19
00134  *                    | Location                ; Section 14.30
00135  *                    | Proxy-Authenticate      ; Section 14.33
00136  *                    | Retry-After             ; Section 14.37
00137  *                    | Server                  ; Section 14.38
00138  *                    | Vary                    ; Section 14.44
00139  *                    | WWW-Authenticate        ; Section 14.47
00140  *
00141  */
00142 
00144 typedef CPROPS_DLL enum 
00145 { 
00146     OPTIONS, 
00147     GET, 
00148     HEAD, 
00149     POST, 
00150     PUT, 
00151 #ifndef _WINDOWS
00152     DELETE, 
00153 #endif
00154     TRACE, 
00155     CONNECT 
00156 } cp_http_request_type;
00157 
00158 CPROPS_DLL
00159 char *get_http_request_type_lit(cp_http_request_type type);
00160 
00162 typedef CPROPS_DLL enum { HTTP_1_0, HTTP_1_1} cp_http_version;
00163 
00165 typedef CPROPS_DLL enum 
00166 {
00167     HTTP_NULL_STATUS = -1,
00168     HTTP_100_CONTINUE = 100,
00169     HTTP_101_SWITCHING_PROTOCOLS = 101,
00170     HTTP_200_OK = 200,
00171     HTTP_201_CREATED = 201,
00172     HTTP_202_ACCEPTED = 202,
00173     HTTP_203_NON_AUTHORITATIVE_INFORMATION = 203,
00174     HTTP_204_NO_CONTENT = 204,
00175     HTTP_205_RESET_CONTENT = 205,
00176     HTTP_206_PARTIAL_CONTENT = 206,
00177     HTTP_300_MULTIPLE_CHOICES = 300,
00178     HTTP_301_MOVED_PERMANENTLY = 301,
00179     HTTP_302_FOUND = 302,
00180     HTTP_303_SEE_OTHER = 303,
00181     HTTP_304_NOT_MODIFIED = 304,
00182     HTTP_305_USE_PROXY = 305,
00183     HTTP_307_TEMPORARY_REDIRECT = 307,
00184     HTTP_400_BAD_REQUEST = 400,
00185     HTTP_401_UNAUTHORIZED = 401,
00186     HTTP_402_PAYMENT_REQUIRED = 402,
00187     HTTP_403_FORBIDDEN = 403,
00188     HTTP_404_NOT_FOUND = 404,
00189     HTTP_405_METHOD_NOT_ALLOWED = 405,
00190     HTTP_406_NOT_ACCEPTABLE = 406,
00191     HTTP_407_PROXY_AUTHENTICATION_REQUIRED = 407,
00192     HTTP_408_REQUEST_TIME_OUT = 408,
00193     HTTP_409_CONFLICT = 409,
00194     HTTP_410_GONE = 410,
00195     HTTP_411_LENGTH_REQUIRED = 411,
00196     HTTP_412_PRECONDITION_FAILED = 412,
00197     HTTP_413_REQUEST_ENTITY_TOO_LARGE = 413,
00198     HTTP_414_REQUEST_URI_TOO_LARGE = 414,
00199     HTTP_415_UNSUPPORTED_MEDIA_TYPE = 415,
00200     HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE = 416,
00201     HTTP_417_EXPECTATION_FAILED = 417,
00202     HTTP_500_INTERNAL_SERVER_ERROR = 500,
00203     HTTP_501_NOT_IMPLEMENTED = 501,
00204     HTTP_502_BAD_GATEWAY = 502,
00205     HTTP_503_SERVICE_UNAVAILABLE = 503,
00206     HTTP_504_GATEWAY_TIME_OUT = 504,
00207     HTTP_505_HTTP_VERSION_NOT_SUPPORTED = 505
00208 } cp_http_status_code;
00209 
00215 typedef CPROPS_DLL enum
00216 { 
00217     HTTP_CONNECTION_POLICY_DEFAULT,
00218     HTTP_CONNECTION_POLICY_CLOSE, 
00219     HTTP_CONNECTION_POLICY_KEEP_ALIVE
00220 } connection_policy;
00221 
00222 typedef CPROPS_DLL enum
00223 {
00224     TEXT,
00225     HTML,
00226     JPEG
00227 } cp_http_content_type;
00228 
00229 #define DEFAULT_SERVER_NAME "libcprops-0.1.7"
00230 
00231 #define DEFAULT_KEEPALIVE 300
00232 
00233 #ifndef HTTP_KEEPALIVE
00234 #define HTTP_KEEPALIVE DEFAULT_KEEPALIVE
00235 #endif
00236 
00237 #ifndef MAX_URL_LENGTH
00238 #define MAX_URL_LENGTH   0x400
00239 #endif
00240 
00242 CPROPS_DLL
00243 int cp_http_init();
00244 
00246 CPROPS_DLL
00247 void cp_http_shutdown();
00248 
00249 #ifdef CP_USE_COOKIES
00250 
00251 #ifndef MAX_COOKIE_LENGTH
00252 #define MAX_COOKIE_LENGTH   0x1000
00253 #endif /* MAX_COOKIE_LENGTH */
00254 
00255 /* Wdy, DD-Mon-YYYY HH:MM:SS GMT */
00256 #define COOKIE_TIME_FMT "a, 0-b-Y H:M: GMT"
00257 #endif /* CP_USE_COOKIES */
00258 
00259 #ifdef CP_USE_HTTP_SESSIONS
00260 
00261 #define CP_HTTP_SESSION_PRM "CPSID"
00262 #define CP_HTTP_SESSION_MARKER "CPSID="
00263 #define CP_HTTP_SESSION_MARKER_LEN 6
00264 
00265 #define DEFAULT_SESSION_VALIDITY 86400 /* 24 hours */
00266 
00271 CPROPS_DLL
00272 void cp_httpsocket_stop_all();
00273 
00278 typedef CPROPS_DLL enum 
00279 { 
00280 #ifdef CP_USE_COOKIES
00281     SESSION_TYPE_COOKIE = 1, 
00282 #endif /* CP_USE_COOKIES */
00283     SESSION_TYPE_URLREWRITE = 2 
00284 } cp_http_session_type;
00285 
00286 typedef CPROPS_DLL struct _cp_http_session
00287 {
00288     char *sid;                  
00289     cp_http_session_type type;  
00290     time_t created;             
00291     time_t access;              
00292     long validity;              
00293     short renew_on_access;      
00294     cp_hashtable *key;          
00295     short valid;                
00296     short fresh;                
00297     int refcount;               
00298 } cp_http_session;
00299 
00301 CPROPS_DLL
00302 void *cp_http_session_get(cp_http_session *session, char *key);
00304 CPROPS_DLL
00305 void *cp_http_session_set(cp_http_session *session, 
00306                                  char *key, void *value);
00308 CPROPS_DLL
00309 void *cp_http_session_set_dtr(cp_http_session *session, 
00310                               char *key, void *value, 
00311                               cp_destructor_fn dtr);
00313 CPROPS_DLL
00314 void cp_http_session_set_validity(cp_http_session *session, long sec);
00316 CPROPS_DLL
00317 short cp_http_session_is_fresh(cp_http_session *session);
00319 CPROPS_DLL
00320 void cp_http_session_delete(cp_http_session *session);
00321 
00322 #endif /* CP_USE_HTTP_SESSIONS */
00323 
00324 
00325 struct CPROPS_DLL _cp_httpsocket;
00326         
00328 typedef CPROPS_DLL struct _cp_http_request
00329 {
00330     cp_http_request_type type;      
00331     cp_http_version version;        
00332     char *uri;                      
00333     cp_hashtable *header;           
00334     char *query_string;             
00335     cp_hashtable *prm;              
00336     cp_vector *prm_name;            
00337 #ifdef CP_USE_COOKIES
00338     cp_vector *cookie;              
00339 #endif
00340     char *content;                  
00341     struct _cp_httpsocket *owner;   
00342     cp_connection_descriptor *connection; 
00343 #ifdef CP_USE_HTTP_SESSIONS
00344     cp_http_session *session;       
00345 #endif
00346 } cp_http_request;
00347 
00349 CPROPS_DLL
00350 cp_http_request *cp_http_request_parse(struct _cp_httpsocket *owner, 
00351                                        char *request, 
00352                                        int *err);
00354 CPROPS_DLL
00355 void cp_http_request_delete(cp_http_request *request);
00357 CPROPS_DLL
00358 char *cp_http_request_get_header(cp_http_request *request, char *name);
00360 CPROPS_DLL
00361 char **cp_http_request_get_headers(cp_http_request *request);
00363 CPROPS_DLL
00364 char *cp_http_request_get_parameter(cp_http_request *request, char *name);
00366 CPROPS_DLL
00367 cp_vector *cp_http_request_get_param_vector(cp_http_request *request, char *name);
00369 CPROPS_DLL
00370 cp_vector *cp_http_request_get_params(cp_http_request *request);
00371 #ifdef CP_USE_HTTP_SESSIONS
00372 
00373 CPROPS_DLL
00374 cp_http_session *cp_http_request_get_session(cp_http_request *request, 
00375                                              int create);
00376 #endif
00377 
00378 CPROPS_DLL
00379 void cp_http_request_dump(cp_http_request *req);
00380 
00382 typedef CPROPS_DLL struct _cp_http_response
00383 {
00384     cp_http_version version;            
00385     cp_http_status_code status;         
00386     cp_http_request *request;           
00387     char *servername;                   
00388     connection_policy connection;       
00389     cp_hashtable *header;               
00390 #ifdef CP_USE_COOKIES
00391     cp_vector *cookie;                  
00392 #endif
00393     cp_http_content_type content_type;  
00394     char *content_type_lit;             
00395     char *body;                         
00396     cp_string *content;                 
00397     int len;                            
00398     short skip;                         
00399     char *status_lit;                   
00400 } cp_http_response;
00401 
00403 CPROPS_DLL
00404 cp_http_response *cp_http_response_create(cp_http_request *request);
00406 CPROPS_DLL
00407 void cp_http_response_delete(cp_http_response *res);
00409 CPROPS_DLL
00410 void cp_http_response_destroy(cp_http_response *res);
00412 CPROPS_DLL
00413 int cp_http_response_write(cp_connection_descriptor *cdesc, 
00414                            cp_http_response *res);
00415 
00417 CPROPS_DLL
00418 void cp_http_response_set_status(cp_http_response *response, 
00419                                         cp_http_status_code code);
00421 CPROPS_DLL
00422 cp_http_status_code cp_http_response_get_status(cp_http_response *response);
00424 CPROPS_DLL
00425 void cp_http_response_set_content_type(cp_http_response *response, 
00426                                        cp_http_content_type type);
00428 CPROPS_DLL
00429 void cp_http_response_set_content_type_string(cp_http_response *response,
00430                                               char *content_type_lit);
00432 CPROPS_DLL
00433 char *cp_http_response_get_content_type(cp_http_response *response);
00435 CPROPS_DLL
00436 void cp_http_response_set_header(cp_http_response *response, 
00437                                         char *name, char *value);
00439 CPROPS_DLL
00440 char *cp_http_response_get_header(cp_http_response *response, char *name);
00442 CPROPS_DLL
00443 cp_vector *cp_http_response_get_header_names(cp_http_response *response);
00445 CPROPS_DLL
00446 void cp_http_response_set_body(cp_http_response *response, 
00447                                       char *body);
00449 CPROPS_DLL
00450 void cp_http_response_set_content(cp_http_response *response, 
00451                                          cp_string *content);
00453 CPROPS_DLL
00454 cp_string *cp_http_response_get_content(cp_http_response *response);
00455 
00457 CPROPS_DLL
00458 void cp_http_response_set_connection_policy(cp_http_response *response, 
00459                                             connection_policy policy);
00460 
00461 #ifdef CP_USE_COOKIES
00462 
00463 CPROPS_DLL
00464 int cp_http_response_set_cookie(cp_http_response *response, char *name, 
00465         char *content, char *host, char *path, long validity, int secure);
00466 #endif
00467 
00473 CPROPS_DLL
00474 void cp_http_response_skip(cp_http_response *response);
00475 
00477 CPROPS_DLL
00478 void cp_http_response_report_error(cp_http_response *response, 
00479                                    cp_http_status_code code, 
00480                                    char *message);
00481 
00483 typedef int (*cp_http_service_callback)(cp_http_request *request, 
00484                                         cp_http_response *response);
00485 
00487 typedef CPROPS_DLL struct _cp_httpsocket
00488 {
00489     int id;                 
00490     cp_socket *sock;        
00491     int keepalive;          
00492 #ifdef CP_USE_HTTP_SESSIONS
00493     cp_hashlist *session;   
00494     cp_hashlist *pending;   
00495 #endif
00496     cp_trie *service;       
00497     cp_http_service_callback default_service;   
00498     char *server_name;      
00499 } cp_httpsocket;
00500 
00501 
00505 typedef CPROPS_DLL struct _cp_http_service
00506 {
00507     char *name;                         
00508     char *path;                         
00509     cp_http_service_callback service;   
00510 } cp_http_service;
00511 
00512 CPROPS_DLL
00513 void *cp_http_thread_fn(void *prm);
00514 
00516 CPROPS_DLL
00517 cp_httpsocket *
00518     cp_httpsocket_create(int port, cp_http_service_callback default_service);
00519 #ifdef CP_USE_SSL
00520 
00524 CPROPS_DLL
00525 cp_httpsocket *
00526     cp_httpsocket_create_ssl(int port, 
00527                              cp_http_service_callback default_service,
00528                              char *certificate_file, 
00529                              char *key_file, 
00530                              int verification_mode);
00531 #endif
00532 
00533 CPROPS_DLL
00534 void cp_httpsocket_delete(cp_httpsocket *svc);
00535 
00537 CPROPS_DLL
00538 void cp_httpsocket_set_keepalive(cp_httpsocket *socket, int sec);
00540 CPROPS_DLL
00541 void cp_httpsocket_set_server_name(cp_httpsocket *socket, char *name);
00546 CPROPS_DLL
00547 void cp_httpsocket_set_backlog(cp_httpsocket *socket, int backlog);
00549 CPROPS_DLL
00550 void cp_httpsocket_set_delay(cp_httpsocket *socket, struct timeval delay);
00551 /* set sec. to block in accept() */
00552 CPROPS_DLL
00553 void cp_httpsocket_set_delay_sec(cp_httpsocket *socket, long sec);
00555 CPROPS_DLL
00556 void cp_httpsocket_set_delay_usec(cp_httpsocket *socket, long usec);
00558 CPROPS_DLL
00559 void cp_httpsocket_set_poolsize_min(cp_httpsocket *socket, int min);
00561 CPROPS_DLL
00562 void cp_httpsocket_set_poolsize_max(cp_httpsocket *socket, int max);
00563 
00565 CPROPS_DLL
00566 void *cp_httpsocket_add_shutdown_callback(cp_httpsocket *socket, 
00567                                           void (*cb)(void *),
00568                                           void *prm);
00569 
00571 CPROPS_DLL
00572 int cp_httpsocket_listen(cp_httpsocket *sock);
00573 
00579 CPROPS_DLL
00580 cp_http_service *cp_http_service_create(char *name, 
00581                                         char *path, 
00582                                         cp_http_service_callback service);
00584 CPROPS_DLL
00585 void cp_http_service_delete(cp_http_service *svc);
00586 
00588 CPROPS_DLL
00589 int cp_httpsocket_register_service(cp_httpsocket *server, cp_http_service *service);
00591 CPROPS_DLL
00592 void *cp_httpsocket_unregister_service(cp_httpsocket *server, cp_http_service *service);
00593 
00595 CPROPS_DLL
00596 void *cp_http_add_shutdown_callback(void (*cb)(void *), void *prm);
00597 
00598 __END_DECLS
00601 #endif
00602 

Generated on Sat Dec 1 10:25:29 2007 for cprops by  doxygen 1.3.9.1