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
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
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 "cprops-__CPROPSVERSION"
00230 #define DEFAULT_KEEPALIVE 300
00231
00232 #ifndef HTTP_KEEPALIVE
00233 #define HTTP_KEEPALIVE DEFAULT_KEEPALIVE
00234 #endif
00235
00236 #ifndef MAX_URL_LENGTH
00237 #define MAX_URL_LENGTH 0x400
00238 #endif
00239
00241 CPROPS_DLL
00242 int cp_http_init();
00243
00245 CPROPS_DLL
00246 void cp_http_shutdown();
00247
00248 #ifdef CP_USE_COOKIES
00249
00250 #ifndef MAX_COOKIE_LENGTH
00251 #define MAX_COOKIE_LENGTH 0x1000
00252 #endif
00253
00254
00255 #define COOKIE_TIME_FMT "a, 0-b-Y H:M: GMT"
00256 #endif
00257
00258 #ifdef CP_USE_HTTP_SESSIONS
00259
00260 #define CP_HTTP_SESSION_PRM "CPSID"
00261 #define CP_HTTP_SESSION_MARKER "CPSID="
00262 #define CP_HTTP_SESSION_MARKER_LEN 6
00263
00264 #define DEFAULT_SESSION_VALIDITY 86400
00265
00270 CPROPS_DLL
00271 void cp_httpsocket_stop_all();
00272
00277 typedef CPROPS_DLL enum
00278 {
00279 #ifdef CP_USE_COOKIES
00280 SESSION_TYPE_COOKIE = 1,
00281 #endif
00282 SESSION_TYPE_URLREWRITE = 2
00283 } cp_http_session_type;
00284
00285 typedef CPROPS_DLL struct _cp_http_session
00286 {
00287 char *sid;
00288 cp_http_session_type type;
00289 time_t created;
00290 time_t access;
00291 long validity;
00292 short renew_on_access;
00293 cp_hashtable *key;
00294 short valid;
00295 short fresh;
00296 int refcount;
00297 } cp_http_session;
00298
00300 CPROPS_DLL
00301 void *cp_http_session_get(cp_http_session *session, char *key);
00303 CPROPS_DLL
00304 void *cp_http_session_set(cp_http_session *session,
00305 char *key, void *value);
00307 CPROPS_DLL
00308 void *cp_http_session_set_dtr(cp_http_session *session,
00309 char *key, void *value,
00310 cp_destructor_fn dtr);
00312 CPROPS_DLL
00313 void cp_http_session_set_validity(cp_http_session *session, long sec);
00315 CPROPS_DLL
00316 short cp_http_session_is_fresh(cp_http_session *session);
00318 CPROPS_DLL
00319 void cp_http_session_delete(cp_http_session *session);
00320
00321 #endif
00322
00323
00324 struct CPROPS_DLL _cp_httpsocket;
00325
00327 typedef CPROPS_DLL struct _cp_http_request
00328 {
00329 cp_http_request_type type;
00330 cp_http_version version;
00331 char *uri;
00332 cp_hashtable *header;
00333 char *query_string;
00334 cp_hashtable *prm;
00335 cp_vector *prm_name;
00336 #ifdef CP_USE_COOKIES
00337 cp_vector *cookie;
00338 #endif
00339 char *content;
00340 struct _cp_httpsocket *owner;
00341 cp_connection_descriptor *connection;
00342 #ifdef CP_USE_HTTP_SESSIONS
00343 cp_http_session *session;
00344 #endif
00345 } cp_http_request;
00346
00348 CPROPS_DLL
00349 cp_http_request *cp_http_request_parse(struct _cp_httpsocket *owner,
00350 char *request,
00351 int *err);
00353 CPROPS_DLL
00354 void cp_http_request_delete(cp_http_request *request);
00356 CPROPS_DLL
00357 char *cp_http_request_get_header(cp_http_request *request, char *name);
00359 CPROPS_DLL
00360 char **cp_http_request_get_headers(cp_http_request *request);
00362 CPROPS_DLL
00363 char *cp_http_request_get_parameter(cp_http_request *request, char *name);
00365 CPROPS_DLL
00366 cp_vector *cp_http_request_get_param_vector(cp_http_request *request, char *name);
00368 CPROPS_DLL
00369 cp_vector *cp_http_request_get_params(cp_http_request *request);
00370 #ifdef CP_USE_HTTP_SESSIONS
00371
00372 CPROPS_DLL
00373 cp_http_session *cp_http_request_get_session(cp_http_request *request,
00374 int create);
00375 #endif
00376
00377 CPROPS_DLL
00378 void cp_http_request_dump(cp_http_request *req);
00379
00381 typedef CPROPS_DLL struct _cp_http_response
00382 {
00383 cp_http_version version;
00384 cp_http_status_code status;
00385 cp_http_request *request;
00386 char *servername;
00387 connection_policy connection;
00388 cp_hashtable *header;
00389 #ifdef CP_USE_COOKIES
00390 cp_vector *cookie;
00391 #endif
00392 cp_http_content_type content_type;
00393 char *content_type_lit;
00394 char *body;
00395 cp_string *content;
00396 int len;
00397 short skip;
00398 char *status_lit;
00399 } cp_http_response;
00400
00402 CPROPS_DLL
00403 cp_http_response *cp_http_response_create(cp_http_request *request);
00405 CPROPS_DLL
00406 void cp_http_response_delete(cp_http_response *res);
00408 CPROPS_DLL
00409 void cp_http_response_destroy(cp_http_response *res);
00411 CPROPS_DLL
00412 int cp_http_response_write(cp_connection_descriptor *cdesc,
00413 cp_http_response *res);
00414
00416 CPROPS_DLL
00417 void cp_http_response_set_status(cp_http_response *response,
00418 cp_http_status_code code);
00420 CPROPS_DLL
00421 cp_http_status_code cp_http_response_get_status(cp_http_response *response);
00423 CPROPS_DLL
00424 void cp_http_response_set_content_type(cp_http_response *response,
00425 cp_http_content_type type);
00427 CPROPS_DLL
00428 void cp_http_response_set_content_type_string(cp_http_response *response,
00429 char *content_type_lit);
00431 CPROPS_DLL
00432 char *cp_http_response_get_content_type(cp_http_response *response);
00434 CPROPS_DLL
00435 void cp_http_response_set_header(cp_http_response *response,
00436 char *name, char *value);
00438 CPROPS_DLL
00439 char *cp_http_response_get_header(cp_http_response *response, char *name);
00441 CPROPS_DLL
00442 cp_vector *cp_http_response_get_header_names(cp_http_response *response);
00444 CPROPS_DLL
00445 void cp_http_response_set_body(cp_http_response *response,
00446 char *body);
00448 CPROPS_DLL
00449 void cp_http_response_set_content(cp_http_response *response,
00450 cp_string *content);
00452 CPROPS_DLL
00453 cp_string *cp_http_response_get_content(cp_http_response *response);
00454
00456 CPROPS_DLL
00457 void cp_http_response_set_connection_policy(cp_http_response *response,
00458 connection_policy policy);
00459
00460 #ifdef CP_USE_COOKIES
00461
00462 CPROPS_DLL
00463 int cp_http_response_set_cookie(cp_http_response *response, char *name,
00464 char *content, char *host, char *path, long validity, int secure);
00465 #endif
00466
00472 CPROPS_DLL
00473 void cp_http_response_skip(cp_http_response *response);
00474
00476 CPROPS_DLL
00477 void cp_http_response_report_error(cp_http_response *response,
00478 cp_http_status_code code,
00479 char *message);
00480
00482 typedef int (*cp_http_service_callback)(cp_http_request *request,
00483 cp_http_response *response);
00484
00486 typedef CPROPS_DLL struct _cp_httpsocket
00487 {
00488 int id;
00489 cp_socket *sock;
00490 int keepalive;
00491 #ifdef CP_USE_HTTP_SESSIONS
00492 cp_hashlist *session;
00493 cp_hashlist *pending;
00494 #endif
00495 cp_trie *service;
00496 cp_http_service_callback default_service;
00497 char *server_name;
00498 } cp_httpsocket;
00499
00500
00504 typedef CPROPS_DLL struct _cp_http_service
00505 {
00506 char *name;
00507 char *path;
00508 cp_http_service_callback service;
00509 } cp_http_service;
00510
00511 CPROPS_DLL
00512 void *cp_http_thread_fn(void *prm);
00513
00515 CPROPS_DLL
00516 cp_httpsocket *
00517 cp_httpsocket_create(int port, cp_http_service_callback default_service);
00518 #ifdef CP_USE_SSL
00519
00523 CPROPS_DLL
00524 cp_httpsocket *
00525 cp_httpsocket_create_ssl(int port,
00526 cp_http_service_callback default_service,
00527 char *certificate_file,
00528 char *key_file,
00529 int verification_mode);
00530 #endif
00531
00532 CPROPS_DLL
00533 void cp_httpsocket_delete(cp_httpsocket *svc);
00534
00536 CPROPS_DLL
00537 void cp_httpsocket_set_keepalive(cp_httpsocket *socket, int sec);
00539 CPROPS_DLL
00540 void cp_httpsocket_set_server_name(cp_httpsocket *socket, char *name);
00545 CPROPS_DLL
00546 void cp_httpsocket_set_backlog(cp_httpsocket *socket, int backlog);
00548 CPROPS_DLL
00549 void cp_httpsocket_set_delay(cp_httpsocket *socket, struct timeval delay);
00550
00551 CPROPS_DLL
00552 void cp_httpsocket_set_delay_sec(cp_httpsocket *socket, long sec);
00554 CPROPS_DLL
00555 void cp_httpsocket_set_delay_usec(cp_httpsocket *socket, long usec);
00557 CPROPS_DLL
00558 void cp_httpsocket_set_poolsize_min(cp_httpsocket *socket, int min);
00560 CPROPS_DLL
00561 void cp_httpsocket_set_poolsize_max(cp_httpsocket *socket, int max);
00562
00564 CPROPS_DLL
00565 void *cp_httpsocket_add_shutdown_callback(cp_httpsocket *socket,
00566 void (*cb)(void *),
00567 void *prm);
00568
00570 CPROPS_DLL
00571 int cp_httpsocket_listen(cp_httpsocket *sock);
00572
00578 CPROPS_DLL
00579 cp_http_service *cp_http_service_create(char *name,
00580 char *path,
00581 cp_http_service_callback service);
00583 CPROPS_DLL
00584 void cp_http_service_delete(cp_http_service *svc);
00585
00587 CPROPS_DLL
00588 int cp_httpsocket_register_service(cp_httpsocket *server, cp_http_service *service);
00590 CPROPS_DLL
00591 void *cp_httpsocket_unregister_service(cp_httpsocket *server, cp_http_service *service);
00592
00594 CPROPS_DLL
00595 void *cp_http_add_shutdown_callback(void (*cb)(void *), void *prm);
00596
00597 __END_DECLS
00600 #endif
00601