1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-07 16:02:55 +03:00

Handler functions now get context passed

This commit is contained in:
Lammert Bies
2017-01-02 00:06:27 +01:00
parent 1ec398461e
commit 0a319fc780
13 changed files with 101 additions and 359 deletions

View File

@@ -145,41 +145,35 @@ struct pollfd {
#endif /* _WIN32 && ! POLLIN */ #endif /* _WIN32 && ! POLLIN */
struct httplib_context; /* Handle for the HTTP service itself */ struct httplib_context; /* Handle for the HTTP service itself */
struct httplib_connection; /* Handle for the individual connection */ struct httplib_connection; /* Handle for the individual connection */
/* This structure contains information about the HTTP request. */ /************************************************************************************************/
struct httplib_request_info { /* */
const char *request_method; /* "GET", "POST", etc */ /* This structure contains information about the HTTP request. */
const char *request_uri; /* URL-decoded URI (absolute or relative, struct httplib_request_info { /* */
* as in the request) */ const char * request_method; /* "GET", "POST", etc */
const char *local_uri; /* URL-decoded URI (relative). Can be NULL const char * request_uri; /* URL-decoded URI (absolute or relative, as in the request) */
* if the request_uri does not address a const char * local_uri; /* URL-decoded URI (relative). Can be NULL if request_uri is not a resource at the server host */
* resource at the server host. */ const char * uri; /* Deprecated: use local_uri instead */
const char *uri; /* Deprecated: use local_uri instead */ const char * http_version; /* E.g. "1.0", "1.1" */
const char *http_version; /* E.g. "1.0", "1.1" */ const char * query_string; /* URL part after '?', not including '?', or NULL */
const char *query_string; /* URL part after '?', not including '?', or const char * remote_user; /* Authenticated user, or NULL if no auth used */
NULL */ char remote_addr[48]; /* Client's IP address as a string. */
const char *remote_user; /* Authenticated user, or NULL if no auth int64_t content_length; /* Length (in bytes) of the request body, can be -1 if no length was given. */
used */ int remote_port; /* Client's port */
char remote_addr[48]; /* Client's IP address as a string. */ bool has_ssl; /* 1 if SSL-ed, 0 if not */
void * user_data; /* User data pointer passed to httplib_start() */
int64_t content_length; /* Length (in bytes) of the request body, void * conn_data; /* Connection-specific user data */
can be -1 if no length was given. */ int num_headers; /* Number of HTTP headers */
int remote_port; /* Client's port */ struct httplib_header { /* */
bool has_ssl; /* 1 if SSL-ed, 0 if not */ const char *name; /* HTTP header name */
void *user_data; /* User data pointer passed to httplib_start() */ const char *value; /* HTTP header value */
void *conn_data; /* Connection-specific user data */ } http_headers[64]; /* Maximum 64 headers */
struct client_cert * client_cert; /* Client certificate information */
int num_headers; /* Number of HTTP headers */ }; /* */
struct httplib_header { /************************************************************************************************/
const char *name; /* HTTP header name */
const char *value; /* HTTP header value */
} http_headers[64]; /* Maximum 64 headers */
struct client_cert *client_cert; /* Client certificate information */
};
/* Client certificate information (part of httplib_request_info) */ /* Client certificate information (part of httplib_request_info) */
@@ -191,170 +185,37 @@ struct client_cert {
}; };
/* This structure needs to be passed to httplib_start(), to let LibHTTP know /*
which callbacks to invoke. For a detailed description, see * This structure needs to be passed to httplib_start(), to let LibHTTP know
https://github.com/lammertb/libhttp/blob/master/docs/UserManual.md */ * which callbacks to invoke. For a detailed description, see
* https://github.com/lammertb/libhttp/blob/master/docs/UserManual.md
*/
struct httplib_callbacks { struct httplib_callbacks {
/* Called when LibHTTP has received new HTTP request. int (*begin_request)( const struct httplib_context *ctx, struct httplib_connection *conn );
If the callback returns one, it must process the request void (*end_request)( const struct httplib_context *ctx, const struct httplib_connection *conn, int reply_status_code );
by sending valid HTTP headers and a body. LibHTTP will not do int (*log_message)( const struct httplib_context *ctx, const struct httplib_connection *conn, const char *message );
any further processing. Otherwise it must return zero. int (*log_access)( const struct httplib_context *ctx, const struct httplib_connection *conn, const char *message );
Note that since V1.7 the "begin_request" function is called int (*init_ssl)( const struct httplib_context *ctx, void *ssl_context, void *user_data );
before an authorization check. If an authorization check is void (*connection_close)( const struct httplib_context *ctx, const struct httplib_connection *conn );
required, use a request_handler instead. const char * (*open_file)( const struct httplib_context *ctx, const struct httplib_connection *conn, const char *path, size_t *data_len );
Return value: void (*init_lua)( const struct httplib_context *ctx, const struct httplib_connection *conn, void *lua_context );
0: LibHTTP will process the request itself. In this case, int (*http_error)( const struct httplib_context *ctx, struct httplib_connection *, int status);
the callback must not send any data to the client. void (*init_context)( const struct httplib_context *ctx );
1-999: callback already processed the request. LibHTTP will void (*init_thread)( const struct httplib_context *ctx, int thread_type );
not send any data after the callback returned. The void (*exit_context)( const struct httplib_context *ctx );
return code is stored as a HTTP status code for the
access log. */
int (*begin_request)(struct httplib_connection *);
/* Called when LibHTTP has finished processing request. */
void (*end_request)(const struct httplib_connection *, int reply_status_code);
int (*log_message)( const struct httplib_context *ctx, const struct httplib_connection * conn, const char *message );
/* Called when LibHTTP is about to log access. If callback returns
non-zero, LibHTTP does not log anything. */
int (*log_access)(const struct httplib_connection *, const char *message);
/* Called when LibHTTP initializes SSL library.
Parameters:
user_data: parameter user_data passed when starting the server.
Return value:
0: LibHTTP will set up the SSL certificate.
1: LibHTTP assumes the callback already set up the certificate.
-1: initializing ssl fails. */
int (*init_ssl)(void *ssl_context, void *user_data);
/* Called when LibHTTP is closing a connection. The per-context mutex is
locked when this is invoked. This is primarily useful for noting when
a websocket is closing and removing it from any application-maintained
list of clients.
Using this callback for websocket connections is deprecated: Use
httplib_set_websocket_handler instead. */
void (*connection_close)(const struct httplib_connection *);
/* Called when LibHTTP tries to open a file. Used to intercept file open
calls, and serve file data from memory instead.
Parameters:
path: Full path to the file to open.
data_len: Placeholder for the file size, if file is served from
memory.
Return value:
NULL: do not serve file from memory, proceed with normal file open.
non-NULL: pointer to the file contents in memory. data_len must be
initialized with the size of the memory block. */
const char *(*open_file)(const struct httplib_connection *, const char *path, size_t *data_len);
/* Called when LibHTTP is about to serve Lua server page, if
Lua support is enabled.
Parameters:
lua_context: "lua_State *" pointer. */
void (*init_lua)(const struct httplib_connection *, void *lua_context);
/* Called when LibHTTP is about to send HTTP error to the client.
Implementing this callback allows to create custom error pages.
Parameters:
status: HTTP error status code.
Return value:
1: run LibHTTP error handler.
0: callback already handled the error. */
int (*http_error)(struct httplib_connection *, int status);
/* Called after LibHTTP context has been created, before requests
are processed.
Parameters:
ctx: context handle */
void (*init_context)(const struct httplib_context *ctx);
/* Called when a new worker thread is initialized.
Parameters:
ctx: context handle
thread_type:
0 indicates the master thread
1 indicates a worker thread handling client connections
2 indicates an internal helper thread (timer thread)
*/
void (*init_thread)(const struct httplib_context *ctx, int thread_type);
/* Called when LibHTTP context is deleted.
Parameters:
ctx: context handle */
void (*exit_context)(const struct httplib_context *ctx);
}; };
struct httplib_option_t { /************************************************************************************************/
const char * name; struct httplib_option_t { /* */
const char * value; const char * name; /* name of the option used when creating a context */
}; const char * value; /* value of the option */
}; /* */
/************************************************************************************************/
/* Start web server. typedef int (*httplib_request_handler)( const struct httplib_context *ctx, const struct httplib_connection *conn, void *cbdata );
typedef int (*httplib_authorization_handler)( const struct httplib_context *ctx, const struct httplib_connection *conn, void *cbdata );
Parameters:
callbacks: httplib_callbacks structure with user-defined callbacks.
options: NULL terminated list of option_name, option_value pairs that
specify LibHTTP configuration parameters.
Side-effects: on UNIX, ignores SIGCHLD and SIGPIPE signals. If custom
processing is required for these, signal handlers must be set up
after calling httplib_start().
Example:
const char *options[] = {
"document_root", "/var/www",
"listening_ports", "80,443s",
NULL
};
struct httplib_context *ctx = httplib_start(&my_func, NULL, options);
Refer to https://github.com/lammertb/libhttp/blob/master/docs/UserManual.md
for the list of valid option and their possible values.
Return:
web server context, or NULL on error. */
/* httplib_request_handler
Called when a new request comes in. This callback is URI based
and configured with httplib_set_request_handler().
Parameters:
conn: current connection information.
cbdata: the callback data configured with httplib_set_request_handler().
Returns:
0: the handler could not handle the request, so fall through.
1 - 999: the handler processed the request. The return code is
stored as a HTTP status code for the access log. */
typedef int (*httplib_request_handler)(struct httplib_connection *conn, void *cbdata);
/* httplib_set_request_handler
Sets or removes a URI mapping for a request handler.
This function uses httplib_lock_context internally.
URI's are ordered and prefixed URI's are supported. For example,
consider two URIs: /a/b and /a
/a matches /a
/a/b matches /a/b
/a/c matches /a
Parameters:
ctx: server context
uri: the URI (exact or pattern) for the handler
handler: the callback handler to use when the URI is requested.
If NULL, an already registered handler for this URI will be
removed.
The URI used to remove a handler must match exactly the one used
to
register it (not only a pattern match).
cbdata: the callback data to give to the handler when it is called. */
LIBHTTP_API void httplib_set_request_handler(struct httplib_context *ctx, const char *uri, httplib_request_handler handler, void *cbdata);
/* Callback types for websocket handlers in C/C++. /* Callback types for websocket handlers in C/C++.
@@ -382,45 +243,16 @@ LIBHTTP_API void httplib_set_request_handler(struct httplib_context *ctx, const
httplib_connection_close_handler httplib_connection_close_handler
Is called, when the connection is closed.*/ Is called, when the connection is closed.*/
typedef int (*httplib_websocket_connect_handler)(const struct httplib_connection *, void *); typedef int (*httplib_websocket_connect_handler)( const struct httplib_context *ctx, const struct httplib_connection *conn, void *);
typedef void (*httplib_websocket_ready_handler)(struct httplib_connection *, void *); typedef void (*httplib_websocket_ready_handler)( const struct httplib_context *ctx, const struct httplib_connection *conn, void *);
typedef int (*httplib_websocket_data_handler)(struct httplib_connection *, int, char *, size_t, void *); typedef int (*httplib_websocket_data_handler)( const struct httplib_context *ctx, const struct httplib_connection *conn, int, char *, size_t, void *);
typedef void (*httplib_websocket_close_handler)(const struct httplib_connection *, void *); typedef void (*httplib_websocket_close_handler)( const struct httplib_context *ctx, const struct httplib_connection *conn, void *);
/* httplib_set_websocket_handler
Set or remove handler functions for websocket connections.
This function works similar to httplib_set_request_handler - see there. */
LIBHTTP_API void
httplib_set_websocket_handler(struct httplib_context *ctx,
const char *uri,
httplib_websocket_connect_handler connect_handler,
httplib_websocket_ready_handler ready_handler,
httplib_websocket_data_handler data_handler,
httplib_websocket_close_handler close_handler,
void *cbdata);
/* httplib_authorization_handler
Some description here
Parameters:
conn: current connection information.
cbdata: the callback data configured with httplib_set_request_handler().
Returns:
0: access denied
1: access granted
*/
typedef int (*httplib_authorization_handler)(struct httplib_connection *conn, void *cbdata);
/* httplib_set_auth_handler /* httplib_set_auth_handler
Sets or removes a URI mapping for an authorization handler. Sets or removes a URI mapping for an authorization handler.
This function works similar to httplib_set_request_handler - see there. */ This function works similar to httplib_set_request_handler - see there. */
LIBHTTP_API void httplib_set_auth_handler(struct httplib_context *ctx, const char *uri, httplib_authorization_handler handler, void *cbdata);
/* Get the value of particular configuration parameter. /* Get the value of particular configuration parameter.
@@ -504,19 +336,6 @@ LIBHTTP_API const struct httplib_request_info *httplib_get_request_info(const st
>0 number of bytes written on success */ >0 number of bytes written on success */
/* Blocks until unique access is obtained to this connection. Intended for use
with websockets only.
Invoke this before httplib_write or httplib_printf when communicating with a
websocket if your code has server-initiated communication as well as
communication in direct response to a message. */
LIBHTTP_API void httplib_lock_connection(struct httplib_connection *conn);
LIBHTTP_API void httplib_unlock_connection(struct httplib_connection *conn);
/* Lock server context. This lock may be used to protect resources
that are shared between different connection/worker threads. */
LIBHTTP_API void httplib_lock_context(struct httplib_context *ctx);
LIBHTTP_API void httplib_unlock_context(struct httplib_context *ctx);
/* Opcodes, from http://tools.ietf.org/html/rfc6455 */ /* Opcodes, from http://tools.ietf.org/html/rfc6455 */
@@ -550,19 +369,6 @@ enum {
#endif #endif
/* Send data to the client using printf() semantics.
Works exactly like httplib_write(), but allows to do message formatting. */
/* Store body data into a file. */
/* Read entire request body and store it in a file "path".
Return:
< 0 Error
>= 0 Number of bytes stored in file "path".
*/
/* Read data from the remote end, return number of bytes read. /* Read data from the remote end, return number of bytes read.
Return: Return:
0 connection has been closed by peer. No more data could be read. 0 connection has been closed by peer. No more data could be read.
@@ -575,7 +381,6 @@ enum {
This is a helper function. It traverses request_info->http_headers array, This is a helper function. It traverses request_info->http_headers array,
and if the header is present in the array, returns its value. If it is and if the header is present in the array, returns its value. If it is
not present, NULL is returned. */ not present, NULL is returned. */
LIBHTTP_API const char *httplib_get_header(const struct httplib_connection *, const char *name);
/* Get a value of particular form variable. /* Get a value of particular form variable.
@@ -597,7 +402,6 @@ LIBHTTP_API const char *httplib_get_header(const struct httplib_connection *, co
Destination buffer is guaranteed to be '\0' - terminated if it is not Destination buffer is guaranteed to be '\0' - terminated if it is not
NULL or zero length. */ NULL or zero length. */
LIBHTTP_API int httplib_get_var(const char *data, size_t data_len, const char *var_name, char *dst, size_t dst_len);
/* Get a value of particular form variable. /* Get a value of particular form variable.
@@ -623,7 +427,6 @@ LIBHTTP_API int httplib_get_var(const char *data, size_t data_len, const char *v
Destination buffer is guaranteed to be '\0' - terminated if it is not Destination buffer is guaranteed to be '\0' - terminated if it is not
NULL or zero length. */ NULL or zero length. */
LIBHTTP_API int httplib_get_var2(const char *data, size_t data_len, const char *var_name, char *dst, size_t dst_len, size_t occurrence);
/* Fetch value of certain cookie variable into the destination buffer. /* Fetch value of certain cookie variable into the destination buffer.
@@ -639,7 +442,6 @@ LIBHTTP_API int httplib_get_var2(const char *data, size_t data_len, const char *
parameter is not found). parameter is not found).
-2 (destination buffer is NULL, zero length or too small to hold the -2 (destination buffer is NULL, zero length or too small to hold the
value). */ value). */
LIBHTTP_API int httplib_get_cookie(const char *cookie, const char *var_name, char *buf, size_t buf_len);
/* Download data from the remote web server. /* Download data from the remote web server.
@@ -722,18 +524,16 @@ struct httplib_form_data_handler {
}; };
/* Return values definition for the "field_found" callback in /*
* httplib_form_data_handler. */ * Return values definition for the "field_found" callback in
* httplib_form_data_handler.
*/
enum { enum {
/* Skip this field (neither get nor store it). Continue with the FORM_FIELD_STORAGE_SKIP = 0x00, /* Skip this field (neither get nor store it). Continue with the next field. */
* next field. */ FORM_FIELD_STORAGE_GET = 0x01, /* Get the field value. */
FORM_FIELD_STORAGE_SKIP = 0x0, FORM_FIELD_STORAGE_STORE = 0x02, /* Store the field value into a file. */
/* Get the field value. */ FORM_FIELD_STORAGE_ABORT = 0x10 /* Stop parsing this request. Skip the remaining fields */
FORM_FIELD_STORAGE_GET = 0x1,
/* Store the field value into a file. */
FORM_FIELD_STORAGE_STORE = 0x2,
/* Stop parsing this request. Skip the remaining fields. */
FORM_FIELD_STORAGE_ABORT = 0x10
}; };
@@ -768,72 +568,12 @@ typedef LIBHTTP_THREAD_TYPE (LIBHTTP_THREAD_CALLING_CONV *httplib_thread_func_t)
LIBHTTP_API int httplib_start_thread(httplib_thread_func_t f, void *p); LIBHTTP_API int httplib_start_thread(httplib_thread_func_t f, void *p);
/* Get text representation of HTTP status code. */
/* URL-decode input buffer into destination buffer. /* URL-decode input buffer into destination buffer.
0-terminate the destination buffer. 0-terminate the destination buffer.
form-url-encoded data differs from URI encoding in a way that it form-url-encoded data differs from URI encoding in a way that it
uses '+' as character for space, see RFC 1866 section 8.2.1 uses '+' as character for space, see RFC 1866 section 8.2.1
http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
Return: length of the decoded data, or -1 if dst buffer is too small. */ Return: length of the decoded data, or -1 if dst buffer is too small. */
LIBHTTP_API int httplib_url_decode(const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded);
/* URL-encode input buffer into destination buffer.
returns the length of the resulting buffer or -1
is the buffer is too small. */
LIBHTTP_API int httplib_url_encode(const char *src, char *dst, size_t dst_len);
/* MD5 hash given strings.
Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
ASCIIz strings. When function returns, buf will contain human-readable
MD5 hash. Example:
char buf[33];
httplib_md5(buf, "aa", "bb", NULL); */
LIBHTTP_API char *httplib_md5(char buf[33], ...);
/* utility methods to compare two buffers, case insensitive. */
/* Connect to a websocket as a client
Parameters:
host: host to connect to, i.e. "echo.websocket.org" or "192.168.1.1" or
"localhost"
port: server port
use_ssl: make a secure connection to server
error_buffer, error_buffer_size: buffer for an error message
path: server path you are trying to connect to, i.e. if connection to
localhost/app, path should be "/app"
origin: value of the Origin HTTP header
data_func: callback that should be used when data is received from the
server
user_data: user supplied argument
Return:
On success, valid httplib_connection object.
On error, NULL. Se error_buffer for details.
*/
/* Connect to a TCP server as a client (can be used to connect to a HTTP server)
Parameters:
host: host to connect to, i.e. "www.wikipedia.org" or "192.168.1.1" or
"localhost"
port: server port
use_ssl: make a secure connection to server
error_buffer, error_buffer_size: buffer for an error message
Return:
On success, valid httplib_connection object.
On error, NULL. Se error_buffer for details.
*/
struct httplib_client_options { struct httplib_client_options {
@@ -859,20 +599,6 @@ enum debug_level_t {
enum { TIMEOUT_INFINITE = -1 }; enum { TIMEOUT_INFINITE = -1 };
/* Wait for a response from the server
Parameters:
conn: connection
ebuf, ebuf_len: error message placeholder.
timeout: time to wait for a response in milliseconds (if < 0 then wait
forever)
Return:
On success, >= 0
On error/timeout, < 0
*/
typedef void (*httplib_alloc_callback_func)( const char *file, unsigned line, const char *action, int64_t current_bytes, int64_t total_blocks, int64_t total_bytes ); typedef void (*httplib_alloc_callback_func)( const char *file, unsigned line, const char *action, int64_t current_bytes, int64_t total_blocks, int64_t total_bytes );
#define httplib_calloc(a, b) XX_httplib_calloc_ex(a, b, __FILE__, __LINE__) #define httplib_calloc(a, b) XX_httplib_calloc_ex(a, b, __FILE__, __LINE__)
@@ -900,16 +626,23 @@ LIBHTTP_API void httplib_destroy_client_context( struct httplib_context *ctx )
LIBHTTP_API struct httplib_connection * httplib_download( struct httplib_context *ctx, const char *host, int port, int use_ssl, PRINTF_FORMAT_STRING(const char *request_fmt), ...) PRINTF_ARGS(5, 6); LIBHTTP_API struct httplib_connection * httplib_download( struct httplib_context *ctx, const char *host, int port, int use_ssl, PRINTF_FORMAT_STRING(const char *request_fmt), ...) PRINTF_ARGS(5, 6);
LIBHTTP_API char * httplib_error_string( int error_code, char *buf, size_t buf_len ); LIBHTTP_API char * httplib_error_string( int error_code, char *buf, size_t buf_len );
LIBHTTP_API const char * httplib_get_builtin_mime_type( const char *file_name ); LIBHTTP_API const char * httplib_get_builtin_mime_type( const char *file_name );
LIBHTTP_API int httplib_get_cookie( const char *cookie, const char *var_name, char *buf, size_t buf_len );
LIBHTTP_API enum debug_level_t httplib_get_debug_level( struct httplib_context *ctx ); LIBHTTP_API enum debug_level_t httplib_get_debug_level( struct httplib_context *ctx );
LIBHTTP_API const char * httplib_get_header( const struct httplib_connection *conn, const char *name );
LIBHTTP_API const char * httplib_get_option( const struct httplib_context *ctx, const char *name, char *buffer, size_t buflen ); LIBHTTP_API const char * httplib_get_option( const struct httplib_context *ctx, const char *name, char *buffer, size_t buflen );
LIBHTTP_API uint64_t httplib_get_random( void ); LIBHTTP_API uint64_t httplib_get_random( void );
LIBHTTP_API int httplib_get_response( const struct httplib_context *ctx, struct httplib_connection *conn, int timeout ); LIBHTTP_API int httplib_get_response( const struct httplib_context *ctx, struct httplib_connection *conn, int timeout );
LIBHTTP_API const char * httplib_get_response_code_text( const struct httplib_context *ctx, struct httplib_connection *conn, int response_code ); LIBHTTP_API const char * httplib_get_response_code_text( const struct httplib_context *ctx, struct httplib_connection *conn, int response_code );
LIBHTTP_API void * httplib_get_user_connection_data( const struct httplib_connection *conn ); LIBHTTP_API void * httplib_get_user_connection_data( const struct httplib_connection *conn );
LIBHTTP_API int httplib_get_var( const char *data, size_t data_len, const char *var_name, char *dst, size_t dst_len );
LIBHTTP_API int httplib_get_var2( const char *data, size_t data_len, const char *var_name, char *dst, size_t dst_len, size_t occurrence );
LIBHTTP_API struct tm * httplib_gmtime_r( const time_t *clock, struct tm *result ); LIBHTTP_API struct tm * httplib_gmtime_r( const time_t *clock, struct tm *result );
LIBHTTP_API int httplib_handle_form_request( const struct httplib_context *ctx, struct httplib_connection *conn, struct httplib_form_data_handler *fdh ); LIBHTTP_API int httplib_handle_form_request( const struct httplib_context *ctx, struct httplib_connection *conn, struct httplib_form_data_handler *fdh );
LIBHTTP_API int httplib_kill( pid_t pid, int sig_num ); LIBHTTP_API int httplib_kill( pid_t pid, int sig_num );
LIBHTTP_API struct tm * httplib_localtime_r( const time_t *clock, struct tm *result ); LIBHTTP_API struct tm * httplib_localtime_r( const time_t *clock, struct tm *result );
LIBHTTP_API void httplib_lock_connection( struct httplib_connection *conn );
LIBHTTP_API void httplib_lock_context( struct httplib_context *ctx );
LIBHTTP_API char * httplib_md5( char buf[33], ... );
LIBHTTP_API int httplib_mkdir( const char *path, int mode ); LIBHTTP_API int httplib_mkdir( const char *path, int mode );
LIBHTTP_API DIR * httplib_opendir( const char *name ); LIBHTTP_API DIR * httplib_opendir( const char *name );
LIBHTTP_API int httplib_poll( struct pollfd *pfd, unsigned int nfds, int timeout ); LIBHTTP_API int httplib_poll( struct pollfd *pfd, unsigned int nfds, int timeout );
@@ -936,8 +669,11 @@ LIBHTTP_API struct dirent * httplib_readdir( DIR *dir );
LIBHTTP_API int httplib_remove( const char *path ); LIBHTTP_API int httplib_remove( const char *path );
LIBHTTP_API void httplib_send_file( const struct httplib_context *ctx, struct httplib_connection *conn, const char *path, const char *mime_type, const char *additional_headers ); LIBHTTP_API void httplib_send_file( const struct httplib_context *ctx, struct httplib_connection *conn, const char *path, const char *mime_type, const char *additional_headers );
LIBHTTP_API void httplib_set_alloc_callback_func( httplib_alloc_callback_func log_func ); LIBHTTP_API void httplib_set_alloc_callback_func( httplib_alloc_callback_func log_func );
LIBHTTP_API void httplib_set_auth_handler( struct httplib_context *ctx, const char *uri, httplib_authorization_handler handler, void *cbdata );
LIBHTTP_API enum debug_level_t httplib_set_debug_level( struct httplib_context *ctx, enum debug_level_t new_level ); LIBHTTP_API enum debug_level_t httplib_set_debug_level( struct httplib_context *ctx, enum debug_level_t new_level );
LIBHTTP_API void httplib_set_request_handler( struct httplib_context *ctx, const char *uri, httplib_request_handler handler, void *cbdata );
LIBHTTP_API void httplib_set_user_connection_data( struct httplib_connection *conn, void *data ); LIBHTTP_API void httplib_set_user_connection_data( struct httplib_connection *conn, void *data );
LIBHTTP_API void httplib_set_websocket_handler( struct httplib_context *ctx, const char *uri, httplib_websocket_connect_handler connect_handler, httplib_websocket_ready_handler ready_handler, httplib_websocket_data_handler data_handler, httplib_websocket_close_handler close_handler, void *cbdata );
LIBHTTP_API struct httplib_context * httplib_start(const struct httplib_callbacks *callbacks, void *user_data, const struct httplib_option_t *options ); LIBHTTP_API struct httplib_context * httplib_start(const struct httplib_callbacks *callbacks, void *user_data, const struct httplib_option_t *options );
LIBHTTP_API void httplib_stop( struct httplib_context *ctx ); LIBHTTP_API void httplib_stop( struct httplib_context *ctx );
LIBHTTP_API int64_t httplib_store_body( const struct httplib_context *ctx, struct httplib_connection *conn, const char *path ); LIBHTTP_API int64_t httplib_store_body( const struct httplib_context *ctx, struct httplib_connection *conn, const char *path );
@@ -949,6 +685,10 @@ LIBHTTP_API int httplib_strncasecmp( const char *s1, const char *s2, size_t l
LIBHTTP_API char * httplib_strndup( const char *str, size_t len ); LIBHTTP_API char * httplib_strndup( const char *str, size_t len );
LIBHTTP_API int httplib_system_exit( void ); LIBHTTP_API int httplib_system_exit( void );
LIBHTTP_API int httplib_system_init( void ); LIBHTTP_API int httplib_system_init( void );
LIBHTTP_API void httplib_unlock_connection( struct httplib_connection *conn );
LIBHTTP_API void httplib_unlock_context( struct httplib_context *ctx );
LIBHTTP_API int httplib_url_decode( const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded );
LIBHTTP_API int httplib_url_encode( const char *src, char *dst, size_t dst_len );
LIBHTTP_API const char * httplib_version( void ); LIBHTTP_API const char * httplib_version( void );
LIBHTTP_API int httplib_websocket_client_write( const struct httplib_context *ctx, struct httplib_connection *conn, int opcode, const char *data, size_t data_len ); LIBHTTP_API int httplib_websocket_client_write( const struct httplib_context *ctx, struct httplib_connection *conn, int opcode, const char *data, size_t data_len );
LIBHTTP_API int httplib_websocket_write( const struct httplib_context *ctx, struct httplib_connection *conn, int opcode, const char *data, size_t data_len ); LIBHTTP_API int httplib_websocket_write( const struct httplib_context *ctx, struct httplib_connection *conn, int opcode, const char *data, size_t data_len );

View File

@@ -44,7 +44,7 @@ void XX_httplib_close_connection( struct httplib_context *ctx, struct httplib_co
* call the connection_close callback if assigned * call the connection_close callback if assigned
*/ */
if ( ctx->callbacks.connection_close != NULL && ctx->ctx_type == CTX_TYPE_SERVER ) ctx->callbacks.connection_close( conn ); if ( ctx->callbacks.connection_close != NULL && ctx->ctx_type == CTX_TYPE_SERVER ) ctx->callbacks.connection_close( ctx, conn );
httplib_lock_connection( conn ); httplib_lock_connection( conn );

View File

@@ -163,7 +163,7 @@ void XX_httplib_handle_request( struct httplib_context *ctx, struct httplib_conn
* required, use a request_handler instead. * required, use a request_handler instead.
*/ */
i = ctx->callbacks.begin_request( conn ); i = ctx->callbacks.begin_request( ctx, conn );
if ( i > 0 ) { if ( i > 0 ) {
@@ -253,7 +253,7 @@ no_callback_resource:
if ( XX_httplib_get_request_handler( ctx, conn, AUTH_HANDLER, NULL, NULL, NULL, NULL, NULL, &auth_handler, &auth_callback_data ) ) { if ( XX_httplib_get_request_handler( ctx, conn, AUTH_HANDLER, NULL, NULL, NULL, NULL, NULL, &auth_handler, &auth_callback_data ) ) {
if ( ! auth_handler( conn, auth_callback_data ) ) return; if ( ! auth_handler( ctx, conn, auth_callback_data ) ) return;
} }
else if ( is_put_or_delete_request && ! is_script_resource && ! is_callback_resource ) { else if ( is_put_or_delete_request && ! is_script_resource && ! is_callback_resource ) {
@@ -311,7 +311,7 @@ no_callback_resource:
if ( ! is_websocket_request ) { if ( ! is_websocket_request ) {
i = callback_handler( conn, callback_data ); i = callback_handler( ctx, conn, callback_data );
if ( i > 0 ) { if ( i > 0 ) {
/* /*

View File

@@ -122,7 +122,7 @@ void XX_httplib_handle_websocket_request( const struct httplib_context *ctx, str
if ( is_callback_resource ) { if ( is_callback_resource ) {
if ( ws_connect_handler != NULL && ws_connect_handler( conn, cbData ) != 0 ) { if ( ws_connect_handler != NULL && ws_connect_handler( ctx, conn, cbData ) != 0 ) {
/* /*
* C callback has returned non-zero, do not proceed with * C callback has returned non-zero, do not proceed with
@@ -169,7 +169,7 @@ void XX_httplib_handle_websocket_request( const struct httplib_context *ctx, str
if ( is_callback_resource ) { if ( is_callback_resource ) {
if ( ws_ready_handler != NULL ) ws_ready_handler( conn, cbData ); if ( ws_ready_handler != NULL ) ws_ready_handler( ctx, conn, cbData );
} }
/* /*
@@ -182,6 +182,6 @@ void XX_httplib_handle_websocket_request( const struct httplib_context *ctx, str
* Step 8: Call the close handler * Step 8: Call the close handler
*/ */
if ( ws_close_handler ) ws_close_handler( conn, cbData ); if ( ws_close_handler != NULL ) ws_close_handler( ctx, conn, cbData );
} /* XX_httplib_handle_websocket_request */ } /* XX_httplib_handle_websocket_request */

View File

@@ -44,10 +44,12 @@ bool XX_httplib_is_file_in_memory( const struct httplib_context *ctx, const stru
if ( ctx->callbacks.open_file ) { if ( ctx->callbacks.open_file ) {
filep->membuf = ctx->callbacks.open_file( conn, path, & size ); filep->membuf = ctx->callbacks.open_file( ctx, conn, path, & size );
/* NOTE: override filep->size only on success. Otherwise, it might /*
* break constructs like if (!XX_httplib_stat() || !XX_httplib_fopen()) ... */ * NOTE: override filep->size only on success. Otherwise, it might
* break constructs like if (!XX_httplib_stat() || !XX_httplib_fopen()) ...
*/
if ( filep->membuf != NULL ) filep->size = size; if ( filep->membuf != NULL ) filep->size = size;
} }

View File

@@ -94,7 +94,7 @@ void XX_httplib_log_access( const struct httplib_context *ctx, const struct http
referer, referer,
user_agent ); user_agent );
if ( ctx->callbacks.log_access != NULL ) ctx->callbacks.log_access( conn, buf ); if ( ctx->callbacks.log_access != NULL ) ctx->callbacks.log_access( ctx, conn, buf );
if ( fi.fp ) { if ( fi.fp ) {

View File

@@ -132,7 +132,7 @@ void XX_httplib_process_new_connection( struct httplib_context *ctx, struct http
*/ */
XX_httplib_handle_request( ctx, conn ); XX_httplib_handle_request( ctx, conn );
if ( ctx->callbacks.end_request != NULL ) ctx->callbacks.end_request( conn, conn->status_code ); if ( ctx->callbacks.end_request != NULL ) ctx->callbacks.end_request( ctx, conn, conn->status_code );
XX_httplib_log_access( ctx, conn ); XX_httplib_log_access( ctx, conn );
} }

View File

@@ -249,7 +249,7 @@ void XX_httplib_read_websocket( const struct httplib_context *ctx, struct httpli
*/ */
exit_by_callback = 0; exit_by_callback = 0;
if ((ws_data_handler != NULL) && !ws_data_handler(conn, mop, data, data_len, callback_data)) exit_by_callback = 1; if ((ws_data_handler != NULL) && !ws_data_handler( ctx, conn, mop, data, data_len, callback_data)) exit_by_callback = 1;
if ( data != mem ) data = httplib_free( data ); if ( data != mem ) data = httplib_free( data );

View File

@@ -55,7 +55,7 @@ void XX_httplib_send_http_error( const struct httplib_context *ctx, struct httpl
conn->status_code = status; conn->status_code = status;
if ( conn->in_error_handler || ctx->callbacks.http_error == NULL || ctx->callbacks.http_error( conn, status ) ) { if ( conn->in_error_handler || ctx->callbacks.http_error == NULL || ctx->callbacks.http_error( ctx, conn, status ) ) {
if ( ! conn->in_error_handler ) { if ( ! conn->in_error_handler ) {

View File

@@ -34,7 +34,7 @@
* authorization requests. * authorization requests.
*/ */
void httplib_set_auth_handler( struct httplib_context *ctx, const char *uri, httplib_request_handler handler, void *cbdata ) { LIBHTTP_API void httplib_set_auth_handler( struct httplib_context *ctx, const char *uri, httplib_request_handler handler, void *cbdata ) {
XX_httplib_set_handler_type( ctx, uri, AUTH_HANDLER, (handler == NULL), NULL, NULL, NULL, NULL, NULL, handler, cbdata ); XX_httplib_set_handler_type( ctx, uri, AUTH_HANDLER, (handler == NULL), NULL, NULL, NULL, NULL, NULL, handler, cbdata );

View File

@@ -28,13 +28,13 @@
#include "httplib_main.h" #include "httplib_main.h"
/* /*
* void httplib_set_request_handler( struct httplib_context *ctx, const char *uri, httplib_request_handler handler, void *cbdata ); * void httplib_set_request_handler( httplib_context *ctx, const char *uri, httplib_request_handler handler, void *cbdata );
* *
* The function httplib_set_request_handler() sets a request handler for a specific * The function httplib_set_request_handler() sets a request handler for a specific
* uri in a server context. * uri in a server context.
*/ */
void httplib_set_request_handler( struct httplib_context *ctx, const char *uri, httplib_request_handler handler, void *cbdata ) { LIBHTTP_API void httplib_set_request_handler( struct httplib_context *ctx, const char *uri, httplib_request_handler handler, void *cbdata ) {
XX_httplib_set_handler_type( ctx, uri, REQUEST_HANDLER, (handler == NULL), handler, NULL, NULL, NULL, NULL, NULL, cbdata ); XX_httplib_set_handler_type( ctx, uri, REQUEST_HANDLER, (handler == NULL), handler, NULL, NULL, NULL, NULL, NULL, cbdata );

View File

@@ -91,7 +91,7 @@ bool XX_httplib_set_ssl_option( struct httplib_context *ctx ) {
/* If a callback has been specified, call it. */ /* If a callback has been specified, call it. */
if ( ctx->callbacks.init_ssl == NULL ) callback_ret = 0; if ( ctx->callbacks.init_ssl == NULL ) callback_ret = 0;
else callback_ret = ctx->callbacks.init_ssl( ctx->ssl_ctx, ctx->user_data ); else callback_ret = ctx->callbacks.init_ssl( ctx, ctx->ssl_ctx, ctx->user_data );
/* /*
* If callback returns 0, LibHTTP sets up the SSL certificate. * If callback returns 0, LibHTTP sets up the SSL certificate.

View File

@@ -55,7 +55,7 @@ LIBHTTP_THREAD XX_httplib_websocket_client_thread( void *data ) {
XX_httplib_read_websocket( ctx, conn, cdata->data_handler, cdata->callback_data ); XX_httplib_read_websocket( ctx, conn, cdata->data_handler, cdata->callback_data );
if ( cdata->close_handler != NULL ) cdata->close_handler( conn, cdata->callback_data ); if ( cdata->close_handler != NULL ) cdata->close_handler( ctx, conn, cdata->callback_data );
ctx->workerthreadids = httplib_free( ctx->workerthreadids ); ctx->workerthreadids = httplib_free( ctx->workerthreadids );
conn = httplib_free( conn ); conn = httplib_free( conn );