00001 #ifndef _CP_HTTP_H
00002 #define _CP_HTTP_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
00143
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
00161
00162 typedef CPROPS_DLL enum { HTTP_1_0, HTTP_1_1} cp_http_version;
00163
00164
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
00210
00211
00212
00213
00214
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.8"
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
00241
00242 CPROPS_DLL
00243 int cp_http_init();
00244
00245
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
00254
00255
00256 #define COOKIE_TIME_FMT "a, 0-b-Y H:M: GMT"
00257 #endif
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
00266
00267
00268
00269
00270
00271 CPROPS_DLL
00272 void cp_httpsocket_stop_all();
00273
00274
00275
00276
00277
00278 typedef CPROPS_DLL enum
00279 {
00280 #ifdef CP_USE_COOKIES
00281 SESSION_TYPE_COOKIE = 1,
00282 #endif
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
00300
00301 CPROPS_DLL
00302 void *cp_http_session_get(cp_http_session *session, char *key);
00303
00304 CPROPS_DLL
00305 void *cp_http_session_set(cp_http_session *session,
00306 char *key, void *value);
00307
00308 CPROPS_DLL
00309 void *cp_http_session_set_dtr(cp_http_session *session,
00310 char *key, void *value,
00311 cp_destructor_fn dtr);
00312
00313 CPROPS_DLL
00314 void cp_http_session_set_validity(cp_http_session *session, long sec);
00315
00316 CPROPS_DLL
00317 short cp_http_session_is_fresh(cp_http_session *session);
00318
00319 CPROPS_DLL
00320 void cp_http_session_delete(cp_http_session *session);
00321
00322 #endif
00323
00324
00325 struct CPROPS_DLL _cp_httpsocket;
00326
00327
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
00348
00349 CPROPS_DLL
00350 cp_http_request *cp_http_request_parse(struct _cp_httpsocket *owner,
00351 char *request,
00352 int *err);
00353
00354 CPROPS_DLL
00355 void cp_http_request_delete(cp_http_request *request);
00356
00357 CPROPS_DLL
00358 char *cp_http_request_get_header(cp_http_request *request, char *name);
00359
00360 CPROPS_DLL
00361 char **cp_http_request_get_headers(cp_http_request *request);
00362
00363 CPROPS_DLL
00364 char *cp_http_request_get_parameter(cp_http_request *request, char *name);
00365
00366 CPROPS_DLL
00367 cp_vector *cp_http_request_get_param_vector(cp_http_request *request, char *name);
00368
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
00381
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
00402
00403 CPROPS_DLL
00404 cp_http_response *cp_http_response_create(cp_http_request *request);
00405
00406 CPROPS_DLL
00407 void cp_http_response_delete(cp_http_response *res);
00408
00409 CPROPS_DLL
00410 void cp_http_response_destroy(cp_http_response *res);
00411
00412 CPROPS_DLL
00413 int cp_http_response_write(cp_connection_descriptor *cdesc,
00414 cp_http_response *res);
00415
00416
00417 CPROPS_DLL
00418 void cp_http_response_set_status(cp_http_response *response,
00419 cp_http_status_code code);
00420
00421 CPROPS_DLL
00422 cp_http_status_code cp_http_response_get_status(cp_http_response *response);
00423
00424 CPROPS_DLL
00425 void cp_http_response_set_content_type(cp_http_response *response,
00426 cp_http_content_type type);
00427
00428 CPROPS_DLL
00429 void cp_http_response_set_content_type_string(cp_http_response *response,
00430 char *content_type_lit);
00431
00432 CPROPS_DLL
00433 char *cp_http_response_get_content_type(cp_http_response *response);
00434
00435 CPROPS_DLL
00436 void cp_http_response_set_header(cp_http_response *response,
00437 char *name, char *value);
00438
00439 CPROPS_DLL
00440 char *cp_http_response_get_header(cp_http_response *response, char *name);
00441
00442 CPROPS_DLL
00443 cp_vector *cp_http_response_get_header_names(cp_http_response *response);
00444
00445 CPROPS_DLL
00446 void cp_http_response_set_body(cp_http_response *response,
00447 char *body);
00448
00449 CPROPS_DLL
00450 void cp_http_response_set_content(cp_http_response *response,
00451 cp_string *content);
00452
00453 CPROPS_DLL
00454 cp_string *cp_http_response_get_content(cp_http_response *response);
00455
00456
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
00468
00469
00470
00471
00472
00473 CPROPS_DLL
00474 void cp_http_response_skip(cp_http_response *response);
00475
00476
00477 CPROPS_DLL
00478 void cp_http_response_report_error(cp_http_response *response,
00479 cp_http_status_code code,
00480 char *message);
00481
00482
00483 typedef int (*cp_http_service_callback)(cp_http_request *request,
00484 cp_http_response *response);
00485
00486
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
00502
00503
00504
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
00515
00516 CPROPS_DLL
00517 cp_httpsocket *
00518 cp_httpsocket_create(int port, cp_http_service_callback default_service);
00519 #ifdef CP_USE_SSL
00520
00521
00522
00523
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
00536
00537 CPROPS_DLL
00538 void cp_httpsocket_set_keepalive(cp_httpsocket *socket, int sec);
00539
00540 CPROPS_DLL
00541 void cp_httpsocket_set_server_name(cp_httpsocket *socket, char *name);
00542
00543
00544
00545
00546 CPROPS_DLL
00547 void cp_httpsocket_set_backlog(cp_httpsocket *socket, int backlog);
00548
00549 CPROPS_DLL
00550 void cp_httpsocket_set_delay(cp_httpsocket *socket, struct timeval delay);
00551
00552 CPROPS_DLL
00553 void cp_httpsocket_set_delay_sec(cp_httpsocket *socket, long sec);
00554
00555 CPROPS_DLL
00556 void cp_httpsocket_set_delay_usec(cp_httpsocket *socket, long usec);
00557
00558 CPROPS_DLL
00559 void cp_httpsocket_set_poolsize_min(cp_httpsocket *socket, int min);
00560
00561 CPROPS_DLL
00562 void cp_httpsocket_set_poolsize_max(cp_httpsocket *socket, int max);
00563
00564
00565 CPROPS_DLL
00566 void *cp_httpsocket_add_shutdown_callback(cp_httpsocket *socket,
00567 void (*cb)(void *),
00568 void *prm);
00569
00570
00571 CPROPS_DLL
00572 int cp_httpsocket_listen(cp_httpsocket *sock);
00573
00574
00575
00576
00577
00578
00579 CPROPS_DLL
00580 cp_http_service *cp_http_service_create(char *name,
00581 char *path,
00582 cp_http_service_callback service);
00583
00584 CPROPS_DLL
00585 void cp_http_service_delete(cp_http_service *svc);
00586
00587
00588 CPROPS_DLL
00589 int cp_httpsocket_register_service(cp_httpsocket *server, cp_http_service *service);
00590
00591 CPROPS_DLL
00592 void *cp_httpsocket_unregister_service(cp_httpsocket *server, cp_http_service *service);
00593
00594
00595 CPROPS_DLL
00596 void *cp_http_add_shutdown_callback(void (*cb)(void *), void *prm);
00597
00598 __END_DECLS
00599
00600
00601 #endif
00602