Sagui library v3.5.0
Cross-platform library which helps to develop web servers or frameworks.
|
Data Structures | |
struct | sg_httpauth |
struct | sg_httpupld |
struct | sg_httpreq |
struct | sg_httpres |
struct | sg_httpsrv |
Macros | |
#define | sg_httpres_send(res, val, content_type, status) |
#define | sg_httpres_download(res, filename, status) sg_httpres_sendfile2((res), 0, 0, 0, (filename), "attachment", (status)) |
#define | sg_httpres_render(res, filename, status) sg_httpres_sendfile2((res), 0, 0, 0, (filename), "inline", (status)) |
#define | sg_httpres_zsend(res, val, content_type, status) |
#define | sg_httpres_zdownload(res, filename, status) sg_httpres_zsendfile2((res), 1, 0, 0, 0, (filename), "attachment", (status)) |
#define | sg_httpres_zrender(res, filename, status) sg_httpres_zsendfile2((res), 1, 0, 0, 0, (filename), "inline", (status)) |
Typedefs | |
typedef void(* | sg_httpsrv_cli_cb) (void *cls, const void *client, bool *closed) |
typedef bool(* | sg_httpauth_cb) (void *cls, struct sg_httpauth *auth, struct sg_httpreq *req, struct sg_httpres *res) |
typedef int(* | sg_httpupld_cb) (void *cls, void **handle, const char *dir, const char *field, const char *name, const char *mime, const char *encoding) |
typedef int(* | sg_httpuplds_iter_cb) (void *cls, struct sg_httpupld *upld) |
typedef void(* | sg_httpreq_cb) (void *cls, struct sg_httpreq *req, struct sg_httpres *res) |
Functions | |
int | sg_httpauth_set_realm (struct sg_httpauth *auth, const char *realm) |
const char * | sg_httpauth_realm (struct sg_httpauth *auth) |
int | sg_httpauth_deny2 (struct sg_httpauth *auth, const char *reason, const char *content_type, unsigned int status) |
int | sg_httpauth_deny (struct sg_httpauth *auth, const char *reason, const char *content_type) |
int | sg_httpauth_cancel (struct sg_httpauth *auth) |
const char * | sg_httpauth_usr (struct sg_httpauth *auth) |
const char * | sg_httpauth_pwd (struct sg_httpauth *auth) |
int | sg_httpuplds_iter (struct sg_httpupld *uplds, sg_httpuplds_iter_cb cb, void *cls) |
int | sg_httpuplds_next (struct sg_httpupld **upld) |
unsigned int | sg_httpuplds_count (struct sg_httpupld *uplds) |
void * | sg_httpupld_handle (struct sg_httpupld *upld) |
const char * | sg_httpupld_dir (struct sg_httpupld *upld) |
const char * | sg_httpupld_field (struct sg_httpupld *upld) |
const char * | sg_httpupld_name (struct sg_httpupld *upld) |
const char * | sg_httpupld_mime (struct sg_httpupld *upld) |
const char * | sg_httpupld_encoding (struct sg_httpupld *upld) |
uint64_t | sg_httpupld_size (struct sg_httpupld *upld) |
int | sg_httpupld_save (struct sg_httpupld *upld, bool overwritten) |
int | sg_httpupld_save_as (struct sg_httpupld *upld, const char *path, bool overwritten) |
struct sg_httpsrv * | sg_httpreq_srv (struct sg_httpreq *req) |
struct sg_strmap ** | sg_httpreq_headers (struct sg_httpreq *req) |
struct sg_strmap ** | sg_httpreq_cookies (struct sg_httpreq *req) |
struct sg_strmap ** | sg_httpreq_params (struct sg_httpreq *req) |
struct sg_strmap ** | sg_httpreq_fields (struct sg_httpreq *req) |
const char * | sg_httpreq_version (struct sg_httpreq *req) |
const char * | sg_httpreq_method (struct sg_httpreq *req) |
const char * | sg_httpreq_path (struct sg_httpreq *req) |
struct sg_str * | sg_httpreq_payload (struct sg_httpreq *req) |
bool | sg_httpreq_is_uploading (struct sg_httpreq *req) |
struct sg_httpupld * | sg_httpreq_uploads (struct sg_httpreq *req) |
const void * | sg_httpreq_client (struct sg_httpreq *req) |
void * | sg_httpreq_tls_session (struct sg_httpreq *req) |
int | sg_httpreq_isolate (struct sg_httpreq *req, sg_httpreq_cb cb, void *cls) |
int | sg_httpreq_set_user_data (struct sg_httpreq *req, void *data) |
void * | sg_httpreq_user_data (struct sg_httpreq *req) |
struct sg_strmap ** | sg_httpres_headers (struct sg_httpres *res) |
int | sg_httpres_set_cookie (struct sg_httpres *res, const char *name, const char *val) |
int | sg_httpres_sendbinary (struct sg_httpres *res, void *buf, size_t size, const char *content_type, unsigned int status) |
int | sg_httpres_sendfile2 (struct sg_httpres *res, uint64_t size, uint64_t max_size, uint64_t offset, const char *filename, const char *disposition, unsigned int status) |
int | sg_httpres_sendfile (struct sg_httpres *res, uint64_t size, uint64_t max_size, uint64_t offset, const char *filename, bool downloaded, unsigned int status) |
int | sg_httpres_sendstream (struct sg_httpres *res, uint64_t size, sg_read_cb read_cb, void *handle, sg_free_cb free_cb, unsigned int status) |
int | sg_httpres_zsendbinary2 (struct sg_httpres *res, int level, void *buf, size_t size, const char *content_type, unsigned int status) |
int | sg_httpres_zsendbinary (struct sg_httpres *res, void *buf, size_t size, const char *content_type, unsigned int status) |
int | sg_httpres_zsendstream2 (struct sg_httpres *res, int level, uint64_t size, sg_read_cb read_cb, void *handle, sg_free_cb free_cb, unsigned int status) |
int | sg_httpres_zsendstream (struct sg_httpres *res, sg_read_cb read_cb, void *handle, sg_free_cb free_cb, unsigned int status) |
int | sg_httpres_zsendfile2 (struct sg_httpres *res, int level, uint64_t size, uint64_t max_size, uint64_t offset, const char *filename, const char *disposition, unsigned int status) |
int | sg_httpres_zsendfile (struct sg_httpres *res, uint64_t size, uint64_t max_size, uint64_t offset, const char *filename, bool downloaded, unsigned int status) |
int | sg_httpres_reset (struct sg_httpres *res) |
int | sg_httpres_clear (struct sg_httpres *res) |
bool | sg_httpres_is_empty (struct sg_httpres *res) |
struct sg_httpsrv * | sg_httpsrv_new2 (sg_httpauth_cb auth_cb, sg_httpreq_cb req_cb, sg_err_cb err_cb, void *cls) __attribute__((malloc)) |
struct sg_httpsrv * | sg_httpsrv_new (sg_httpreq_cb cb, void *cls) __attribute__((malloc)) |
void | sg_httpsrv_free (struct sg_httpsrv *srv) |
bool | sg_httpsrv_tls_listen4 (struct sg_httpsrv *srv, const char *key, const char *pwd, const char *cert, const char *trust, const char *dhparams, const char *priorities, const char *hostname, uint16_t port, uint32_t backlog, bool threaded) |
bool | sg_httpsrv_tls_listen3 (struct sg_httpsrv *srv, const char *key, const char *pwd, const char *cert, const char *trust, const char *dhparams, const char *priorities, uint16_t port, bool threaded) |
bool | sg_httpsrv_tls_listen2 (struct sg_httpsrv *srv, const char *key, const char *pwd, const char *cert, const char *trust, const char *dhparams, uint16_t port, bool threaded) |
bool | sg_httpsrv_tls_listen (struct sg_httpsrv *srv, const char *key, const char *cert, uint16_t port, bool threaded) |
bool | sg_httpsrv_listen2 (struct sg_httpsrv *srv, const char *hostname, uint16_t port, uint32_t backlog, bool threaded) |
bool | sg_httpsrv_listen (struct sg_httpsrv *srv, uint16_t port, bool threaded) |
int | sg_httpsrv_shutdown (struct sg_httpsrv *srv) |
uint16_t | sg_httpsrv_port (struct sg_httpsrv *srv) |
bool | sg_httpsrv_is_threaded (struct sg_httpsrv *srv) |
int | sg_httpsrv_set_cli_cb (struct sg_httpsrv *srv, sg_httpsrv_cli_cb cb, void *cls) |
int | sg_httpsrv_set_upld_cbs (struct sg_httpsrv *srv, sg_httpupld_cb cb, void *cls, sg_write_cb write_cb, sg_free_cb free_cb, sg_save_cb save_cb, sg_save_as_cb save_as_cb) |
int | sg_httpsrv_set_upld_dir (struct sg_httpsrv *srv, const char *dir) |
const char * | sg_httpsrv_upld_dir (struct sg_httpsrv *srv) |
int | sg_httpsrv_set_post_buf_size (struct sg_httpsrv *srv, size_t size) |
size_t | sg_httpsrv_post_buf_size (struct sg_httpsrv *srv) |
int | sg_httpsrv_set_payld_limit (struct sg_httpsrv *srv, size_t limit) |
size_t | sg_httpsrv_payld_limit (struct sg_httpsrv *srv) |
int | sg_httpsrv_set_uplds_limit (struct sg_httpsrv *srv, uint64_t limit) |
uint64_t | sg_httpsrv_uplds_limit (struct sg_httpsrv *srv) |
int | sg_httpsrv_set_thr_pool_size (struct sg_httpsrv *srv, unsigned int size) |
unsigned int | sg_httpsrv_thr_pool_size (struct sg_httpsrv *srv) |
int | sg_httpsrv_set_con_timeout (struct sg_httpsrv *srv, unsigned int timeout) |
unsigned int | sg_httpsrv_con_timeout (struct sg_httpsrv *srv) |
int | sg_httpsrv_set_con_limit (struct sg_httpsrv *srv, unsigned int limit) |
unsigned int | sg_httpsrv_con_limit (struct sg_httpsrv *srv) |
void * | sg_httpsrv_handle (struct sg_httpsrv *srv) |
Fast event-driven HTTP server.
#define sg_httpres_send | ( | res, | |
val, | |||
content_type, | |||
status | |||
) |
Sends a null-terminated string content to the client.
[in] | res | Response handle. |
[in] | val | Null-terminated string. |
[in] | content_type | Content type. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Operation already in progress. |
ENOMEM | Out of memory. |
#define sg_httpres_download | ( | res, | |
filename, | |||
status | |||
) | sg_httpres_sendfile2((res), 0, 0, 0, (filename), "attachment", (status)) |
Offers a file as download.
[in] | res | Response handle. |
[in] | filename | Path of the file to be sent. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Operation already in progress. |
EISDIR | Is a directory. |
EBADF | Bad file number. |
ENOMEM | Out of memory. |
#define sg_httpres_render | ( | res, | |
filename, | |||
status | |||
) | sg_httpres_sendfile2((res), 0, 0, 0, (filename), "inline", (status)) |
Sends a file to be rendered.
[in] | res | Response handle. |
[in] | filename | Path of the file to be sent. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Operation already in progress. |
EISDIR | Is a directory. |
EBADF | Bad file number. |
ENOMEM | Out of memory. |
#define sg_httpres_zsend | ( | res, | |
val, | |||
content_type, | |||
status | |||
) |
Compresses a null-terminated string content and sends it to the client. The compression is done by zlib library using the DEFLATE compression algorithm.
[in] | res | Response handle. |
[in] | val | Null-terminated string. |
[in] | content_type | Content type. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
ENOMEM | Out of memory. |
ENOBUFS | No buffer space available. |
EALREADY | Operation already in progress. |
Z_<ERROR> | zlib error as negative number. |
Content-Encoding: deflate
is automatically added to the response. #define sg_httpres_zdownload | ( | res, | |
filename, | |||
status | |||
) | sg_httpres_zsendfile2((res), 1, 0, 0, 0, (filename), "attachment", (status)) |
Compresses a file in Gzip format and offers it as download. The compression is done by zlib library using the DEFLATE compression algorithm.
[in] | res | Response handle. |
[in] | filename | Path of the file to be compressed and sent. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Operation already in progress. |
EISDIR | Is a directory. |
EBADF | Bad file number. |
ENOMEM | Out of memory. |
Z_<ERROR> | zlib error as negative number. |
Content-Encoding: gzip
is automatically added to the response. #define sg_httpres_zrender | ( | res, | |
filename, | |||
status | |||
) | sg_httpres_zsendfile2((res), 1, 0, 0, 0, (filename), "inline", (status)) |
Compresses a file in Gzip format and sends it to be rendered. The compression is done by zlib library using the DEFLATE compression algorithm.
[in] | res | Response handle. |
[in] | filename | Path of the file to be sent. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Operation already in progress. |
EISDIR | Is a directory. |
EBADF | Bad file number. |
ENOMEM | Out of memory. |
Z_<ERROR> | zlib error as negative number. |
Content-Encoding: gzip
is automatically added to the response. typedef void(* sg_httpsrv_cli_cb) (void *cls, const void *client, bool *closed) |
Callback signature used to handle client connection events.
[out] | cls | User-defined closure. |
[out] | client | Socket handle of the client. |
[in,out] | closed | Indicates if the client is connected allowing to close it. |
typedef bool(* sg_httpauth_cb) (void *cls, struct sg_httpauth *auth, struct sg_httpreq *req, struct sg_httpres *res) |
Callback signature used to grant or deny the user access to the server resources.
[out] | cls | User-defined closure. |
[out] | auth | Authentication handle. |
[out] | req | Request handle. |
[out] | res | Response handle. |
true | Grants the user access. |
false | Denies the user access. |
typedef int(* sg_httpupld_cb) (void *cls, void **handle, const char *dir, const char *field, const char *name, const char *mime, const char *encoding) |
Callback signature used to handle uploaded files and/or fields.
[out] | cls | User-defined closure. |
[in,out] | handle | Stream handle pointer. |
[out] | dir | Directory to store the uploaded files. |
[out] | field | Posted field. |
[out] | name | Uploaded file name. |
[out] | mime | Uploaded file content-type (e.g.: text/plain , image/png , application/json etc.). |
[out] | encoding | Uploaded file transfer-encoding (e.g.: chunked , deflate , gzip etc.). |
0 | Success. |
E<ERROR> | User-defined error to refuse the upload. |
typedef int(* sg_httpuplds_iter_cb) (void *cls, struct sg_httpupld *upld) |
Callback signature used to iterate uploaded files.
[out] | cls | User-defined closure. |
[out] | upld | Current upload item. |
0 | Success. |
E<ERROR> | User-defined error to stop list iteration. |
typedef void(* sg_httpreq_cb) (void *cls, struct sg_httpreq *req, struct sg_httpres *res) |
Callback signature used to handle requests and responses.
[out] | cls | User-defined closure. |
[out] | req | Request handle. |
[out] | res | Response handle. |
int sg_httpauth_set_realm | ( | struct sg_httpauth * | auth, |
const char * | realm | ||
) |
Sets the authentication protection space (realm).
[in] | auth | Authentication handle. |
[in] | realm | Realm string. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Realm already set. |
ENOMEM | Out of memory. |
const char * sg_httpauth_realm | ( | struct sg_httpauth * | auth | ) |
Gets the authentication protection space (realm).
[in] | auth | Authentication handle. |
NULL | If auth is null and set the errno to EINVAL . |
int sg_httpauth_deny2 | ( | struct sg_httpauth * | auth, |
const char * | reason, | ||
const char * | content_type, | ||
unsigned int | status | ||
) |
Deny the authentication sending the reason to the user.
[in] | auth | Authentication handle. |
[in] | reason | Denial reason. |
[in] | content_type | Content type. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Already denied. |
ENOMEM | Out of memory. |
int sg_httpauth_deny | ( | struct sg_httpauth * | auth, |
const char * | reason, | ||
const char * | content_type | ||
) |
Deny the authentication sending the reason to the user.
[in] | auth | Authentication handle. |
[in] | reason | Denial reason. |
[in] | content_type | Content type. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Already denied. |
ENOMEM | Out of memory. |
int sg_httpauth_cancel | ( | struct sg_httpauth * | auth | ) |
Cancels the authentication loop while the user is trying to access the server.
[in] | auth | Authentication handle. |
0 | Success. |
EINVAL | Invalid argument. |
const char * sg_httpauth_usr | ( | struct sg_httpauth * | auth | ) |
Returns the authentication user.
[in] | auth | Authentication handle. |
NULL | If auth is null and set the errno to EINVAL . |
const char * sg_httpauth_pwd | ( | struct sg_httpauth * | auth | ) |
Returns the authentication password.
[in] | auth | Authentication handle. |
NULL | If auth is null and set the errno to EINVAL . |
int sg_httpuplds_iter | ( | struct sg_httpupld * | uplds, |
sg_httpuplds_iter_cb | cb, | ||
void * | cls | ||
) |
Iterates over all the upload items in the uplds
list.
[in] | uplds | Uploads list handle. |
[in] | cb | Callback to iterate over upload items. |
[in] | cls | User-defined closure. |
0 | Success. |
EINVAL | Invalid argument. |
E<ERROR> | User-defined error to abort the list iteration. |
int sg_httpuplds_next | ( | struct sg_httpupld ** | upld | ) |
Gets the next upload item starting from the first item pointer upld
.
[in,out] | upld | Next upload item starting from the first item pointer. |
0 | Success. |
EINVAL | Invalid argument. |
unsigned int sg_httpuplds_count | ( | struct sg_httpupld * | uplds | ) |
Counts the total upload items in the list uplds
.
[in] | uplds | Uploads list. |
0 | If the list is empty or null. |
void * sg_httpupld_handle | ( | struct sg_httpupld * | upld | ) |
Returns the stream handle of the upload handle upld
.
[in] | upld | Upload handle. |
NULL | If upld is null and set the errno to EINVAL . |
const char * sg_httpupld_dir | ( | struct sg_httpupld * | upld | ) |
Returns the directory of the upload handle upld
.
[in] | upld | Upload handle. |
NULL | If upld is null and set the errno to EINVAL . |
const char * sg_httpupld_field | ( | struct sg_httpupld * | upld | ) |
Returns the field of the upload handle upld
.
[in] | upld | Upload handle. |
NULL | If upld is null and set the errno to EINVAL . |
const char * sg_httpupld_name | ( | struct sg_httpupld * | upld | ) |
Returns the name of the upload handle upld
.
[in] | upld | Upload handle. |
NULL | If upld is null and set the errno to EINVAL . |
const char * sg_httpupld_mime | ( | struct sg_httpupld * | upld | ) |
Returns the MIME (content-type) of the upload.
[in] | upld | Upload handle. |
NULL | If upld is null and set the errno to EINVAL . |
const char * sg_httpupld_encoding | ( | struct sg_httpupld * | upld | ) |
Returns the encoding (transfer-encoding) of the upload.
[in] | upld | Upload handle. |
NULL | If upld is null and set the errno to EINVAL . |
uint64_t sg_httpupld_size | ( | struct sg_httpupld * | upld | ) |
Returns the size of the upload.
[in] | upld | Upload handle. |
uint64
. If upld
is null, set the errno
to EINVAL
. int sg_httpupld_save | ( | struct sg_httpupld * | upld, |
bool | overwritten | ||
) |
Saves the uploaded file defining the destination path by upload name and directory.
[in] | upld | Upload handle. |
[in] | overwritten | Overwrite upload file if it exists. |
0 | Success. |
EINVAL | Invalid argument. |
EEXIST | File already exists (if overwritten is false ). |
EISDIR | Destination file is a directory. |
int sg_httpupld_save_as | ( | struct sg_httpupld * | upld, |
const char * | path, | ||
bool | overwritten | ||
) |
Saves the uploaded file allowing to define the destination path.
[in] | upld | Upload handle. |
[in] | path | Absolute destination path. |
[in] | overwritten | Overwrite upload file if it exists. |
0 | Success. |
EINVAL | Invalid argument. |
EEXIST | File already exists (if overwritten is true ). |
EISDIR | Destination file is a directory. |
struct sg_httpsrv * sg_httpreq_srv | ( | struct sg_httpreq * | req | ) |
Returns the server instance.
[in] | req | Request handle. |
NULL | If req is null and set the errno to EINVAL |
struct sg_strmap ** sg_httpreq_headers | ( | struct sg_httpreq * | req | ) |
Returns the client headers into sg_strmap map.
[in] | req | Request handle. |
NULL | If req is null and set the errno to EINVAL |
struct sg_strmap ** sg_httpreq_cookies | ( | struct sg_httpreq * | req | ) |
Returns the client cookies into sg_strmap map.
[in] | req | Request handle. |
NULL | If req is null and set the errno to EINVAL |
struct sg_strmap ** sg_httpreq_params | ( | struct sg_httpreq * | req | ) |
Returns the query-string into sg_strmap map.
[in] | req | Request handle. |
NULL | If req is null and set the errno to EINVAL |
struct sg_strmap ** sg_httpreq_fields | ( | struct sg_httpreq * | req | ) |
Returns the fields of a HTML form into sg_strmap map.
[in] | req | Request handle. |
NULL | If req is null and set the errno to EINVAL |
const char * sg_httpreq_version | ( | struct sg_httpreq * | req | ) |
Returns the HTTP version.
[in] | req | Request handle. |
NULL | If req is null and set the errno to EINVAL . |
const char * sg_httpreq_method | ( | struct sg_httpreq * | req | ) |
Returns the HTTP method.
[in] | req | Request handle. |
NULL | If req is null and set the errno to EINVAL . |
const char * sg_httpreq_path | ( | struct sg_httpreq * | req | ) |
Returns the path component.
[in] | req | Request handle. |
NULL | If req is null and set the errno to EINVAL . |
struct sg_str * sg_httpreq_payload | ( | struct sg_httpreq * | req | ) |
Returns the posting payload into a sg_str instance.
[in] | req | Request handle. |
NULL | If req is null and set the errno to EINVAL . |
bool sg_httpreq_is_uploading | ( | struct sg_httpreq * | req | ) |
Checks if the client is uploading data.
[in] | req | Request handle. |
true | If the client is uploading data, false otherwise. If req is null, set the errno to EINVAL . |
struct sg_httpupld * sg_httpreq_uploads | ( | struct sg_httpreq * | req | ) |
Returns the list of the uploaded files.
[in] | req | Request handle. |
NULL | If req is null and set the errno to EINVAL . |
const void * sg_httpreq_client | ( | struct sg_httpreq * | req | ) |
Gets the socket handle of the client.
[in] | req | Request handle. |
NULL | If req is null and set the errno to EINVAL . |
void * sg_httpreq_tls_session | ( | struct sg_httpreq * | req | ) |
Returns the GnuTLS session handle.
[in] | req | Request handle. |
0 | Success. |
EINVAL | Invalid argument. |
int sg_httpreq_isolate | ( | struct sg_httpreq * | req, |
sg_httpreq_cb | cb, | ||
void * | cls | ||
) |
Isolates a request from the main event loop to an own dedicated thread, bringing it back when the request finishes.
[in] | req | Request handle. |
[in] | cb | Callback to handle requests and responses isolated from the main event loop. |
[in] | cls | User-defined closure. |
0 | Success. |
EINVAL | Invalid argument. |
ENOMEM | Out of memory. |
E<ERROR> | Any returned error from the OS threading library. |
int sg_httpreq_set_user_data | ( | struct sg_httpreq * | req, |
void * | data | ||
) |
Sets user data to the request handle.
[in] | req | Request handle. |
[in] | data | User data pointer. |
0 | Success. |
EINVAL | Invalid argument. |
void * sg_httpreq_user_data | ( | struct sg_httpreq * | req | ) |
Gets user data from the request handle.
[in] | req | Request handle. |
NULL | If req is null and set the errno to EINVAL . |
struct sg_strmap ** sg_httpres_headers | ( | struct sg_httpres * | res | ) |
Returns the server headers into sg_strmap map.
[in] | res | Response handle. |
NULL | If res is null and set the errno to EINVAL |
int sg_httpres_set_cookie | ( | struct sg_httpres * | res, |
const char * | name, | ||
const char * | val | ||
) |
Sets server cookie to the response handle.
[in] | res | Response handle. |
[in] | name | Cookie name. |
[in] | val | Cookie value. |
0 | Success. |
EINVAL | Invalid argument. |
ENOMEM | Out of memory. |
int sg_httpres_sendbinary | ( | struct sg_httpres * | res, |
void * | buf, | ||
size_t | size, | ||
const char * | content_type, | ||
unsigned int | status | ||
) |
Sends a binary content to the client.
[in] | res | Response handle. |
[in] | buf | Binary content. |
[in] | size | Content size. |
[in] | content_type | Content type. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Operation already in progress. |
ENOMEM | Out of memory. |
int sg_httpres_sendfile2 | ( | struct sg_httpres * | res, |
uint64_t | size, | ||
uint64_t | max_size, | ||
uint64_t | offset, | ||
const char * | filename, | ||
const char * | disposition, | ||
unsigned int | status | ||
) |
Sends a file to the client.
[in] | res | Response handle. |
[in] | size | Size of the file to be sent. Use zero to calculate automatically. |
[in] | max_size | Maximum allowed file size. Use zero for no limit. |
[in] | offset | Offset to start reading from in the file to be sent. |
[in] | filename | Path of the file to be sent. |
[in] | disposition | Content disposition as a null-terminated string (attachment or inline). |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Operation already in progress. |
EISDIR | Is a directory. |
EBADF | Bad file number. |
EFBIG | File too large. |
ENOMEM | Out of memory. |
disposition
is not checked internally, thus any non-NULL
value is passed directly to the header Content-Disposition
. int sg_httpres_sendfile | ( | struct sg_httpres * | res, |
uint64_t | size, | ||
uint64_t | max_size, | ||
uint64_t | offset, | ||
const char * | filename, | ||
bool | downloaded, | ||
unsigned int | status | ||
) |
Sends a file to the client.
[in] | res | Response handle. |
[in] | size | Size of the file to be sent. Use zero to calculate automatically. |
[in] | max_size | Maximum allowed file size. Use zero for no limit. |
[in] | offset | Offset to start reading from in the file to be sent. |
[in] | filename | Path of the file to be sent. |
[in] | downloaded | If true it offers the file as download. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Operation already in progress. |
EISDIR | Is a directory. |
EBADF | Bad file number. |
EFBIG | File too large. |
ENOMEM | Out of memory. |
int sg_httpres_sendstream | ( | struct sg_httpres * | res, |
uint64_t | size, | ||
sg_read_cb | read_cb, | ||
void * | handle, | ||
sg_free_cb | free_cb, | ||
unsigned int | status | ||
) |
Sends a stream to the client.
[in] | res | Response handle. |
[in] | size | Size of the stream. |
[in] | read_cb | Callback to read data from stream handle. |
[in] | handle | Stream handle. |
[in] | free_cb | Callback to free the stream handle. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Operation already in progress. |
ENOMEM | Out of memory. |
size = 0
if the stream size is unknown. int sg_httpres_zsendbinary2 | ( | struct sg_httpres * | res, |
int | level, | ||
void * | buf, | ||
size_t | size, | ||
const char * | content_type, | ||
unsigned int | status | ||
) |
Compresses a binary content and sends it to the client. The compression is done by zlib library using the DEFLATE compression algorithm.
[in] | res | Response handle. |
[in] | level | Compression level (1..9 or -1 for default). |
[in] | buf | Binary content. |
[in] | size | Content size. |
[in] | content_type | Content type. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
ENOMEM | Out of memory. |
ENOBUFS | No buffer space available. |
EALREADY | Operation already in progress. |
Z_<ERROR> | zlib error as negative number. |
Content-Encoding: deflate
is automatically added to the response. int sg_httpres_zsendbinary | ( | struct sg_httpres * | res, |
void * | buf, | ||
size_t | size, | ||
const char * | content_type, | ||
unsigned int | status | ||
) |
Compresses a binary content and sends it to the client. The compression is done by zlib library using the DEFLATE compression algorithm.
[in] | res | Response handle. |
[in] | buf | Binary content. |
[in] | size | Content size. |
[in] | content_type | Content type. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
ENOMEM | Out of memory. |
ENOBUFS | No buffer space available. |
EALREADY | Operation already in progress. |
Z_<ERROR> | zlib error as negative number. |
Content-Encoding: deflate
is automatically added to the response. int sg_httpres_zsendstream2 | ( | struct sg_httpres * | res, |
int | level, | ||
uint64_t | size, | ||
sg_read_cb | read_cb, | ||
void * | handle, | ||
sg_free_cb | free_cb, | ||
unsigned int | status | ||
) |
Compresses a stream and sends it to the client. The compression is done by zlib library using the DEFLATE compression algorithm.
[in] | res | Response handle. |
[in] | level | Compression level (1..9 or -1 for default). |
[in] | size | Size of the stream. |
[in] | read_cb | Callback to read data from stream handle. |
[in] | handle | Stream handle. |
[in] | free_cb | Callback to free the stream handle. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Operation already in progress. |
ENOMEM | Out of memory. |
Z_<ERROR> | zlib error as negative number. |
Content-Encoding: deflate
is automatically added to the response. int sg_httpres_zsendstream | ( | struct sg_httpres * | res, |
sg_read_cb | read_cb, | ||
void * | handle, | ||
sg_free_cb | free_cb, | ||
unsigned int | status | ||
) |
Compresses a stream and sends it to the client. The compression is done by zlib library using the DEFLATE compression algorithm.
[in] | res | Response handle. |
[in] | read_cb | Callback to read data from stream handle. |
[in] | handle | Stream handle. |
[in] | free_cb | Callback to free the stream handle. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Operation already in progress. |
ENOMEM | Out of memory. |
Z_<ERROR> | zlib error as negative number. |
Content-Encoding: deflate
is automatically added to the response. int sg_httpres_zsendfile2 | ( | struct sg_httpres * | res, |
int | level, | ||
uint64_t | size, | ||
uint64_t | max_size, | ||
uint64_t | offset, | ||
const char * | filename, | ||
const char * | disposition, | ||
unsigned int | status | ||
) |
Compresses a file in Gzip format and sends it to the client. The compression is done by zlib library using the DEFLATE compression algorithm.
[in] | res | Response handle. |
[in] | level | Compression level (1..9 or -1 for default). |
[in] | size | Size of the file to be sent. Use zero to calculate automatically. |
[in] | max_size | Maximum allowed file size. Use zero for no limit. |
[in] | offset | Offset to start reading from in the file to be sent. |
[in] | filename | Path of the file to be sent. |
[in] | disposition | Content disposition as a null-terminated string (attachment or inline). |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Operation already in progress. |
EISDIR | Is a directory. |
EBADF | Bad file number. |
EFBIG | File too large. |
ENOMEM | Out of memory. |
Z_<ERROR> | zlib error as negative number. |
Content-Encoding: gzip
is automatically added to the response. disposition
is not checked internally, thus any non-NULL
value is passed directly to the header Content-Disposition
. int sg_httpres_zsendfile | ( | struct sg_httpres * | res, |
uint64_t | size, | ||
uint64_t | max_size, | ||
uint64_t | offset, | ||
const char * | filename, | ||
bool | downloaded, | ||
unsigned int | status | ||
) |
Compresses a file in Gzip format and sends it to the client. The compression is done by zlib library using the DEFLATE compression algorithm.
[in] | res | Response handle. |
[in] | size | Size of the file to be sent. Use zero to calculate automatically. |
[in] | max_size | Maximum allowed file size. Use zero for no limit. |
[in] | offset | Offset to start reading from in the file to be sent. |
[in] | filename | Path of the file to be sent. |
[in] | downloaded | If true it offers the file as download. |
[in] | status | HTTP status code. |
0 | Success. |
EINVAL | Invalid argument. |
EALREADY | Operation already in progress. |
EISDIR | Is a directory. |
EBADF | Bad file number. |
EFBIG | File too large. |
ENOMEM | Out of memory. |
Z_<ERROR> | zlib error as negative number. |
Content-Encoding: gzip
is automatically added to the response. int sg_httpres_reset | ( | struct sg_httpres * | res | ) |
Resets status and internal buffers of the response handle preserving all headers and cookies.
[in] | res | Response handle. |
0 | Success. |
EINVAL | Invalid argument. |
int sg_httpres_clear | ( | struct sg_httpres * | res | ) |
Clears all headers, cookies, status and internal buffers of the response handle.
[in] | res | Response handle. |
0 | Success. |
EINVAL | Invalid argument. |
bool sg_httpres_is_empty | ( | struct sg_httpres * | res | ) |
Checks if the response is empty.
[in] | res | Response handle. |
true | If the response is empty, false otherwise. If res is null, set the errno to EINVAL . |
struct sg_httpsrv * sg_httpsrv_new2 | ( | sg_httpauth_cb | auth_cb, |
sg_httpreq_cb | req_cb, | ||
sg_err_cb | err_cb, | ||
void * | cls | ||
) |
Creates a new HTTP server handle.
[in] | auth_cb | Callback to grant/deny user access to the server resources. |
[in] | req_cb | Callback to handle requests and responses. |
[in] | err_cb | Callback to handle server errors. |
[in] | cls | User-defined closure. |
NULL |
|
struct sg_httpsrv * sg_httpsrv_new | ( | sg_httpreq_cb | cb, |
void * | cls | ||
) |
Creates a new HTTP server handle.
[in] | cb | Callback to handle requests and responses. |
[in] | cls | User-defined closure. |
NULL |
|
void sg_httpsrv_free | ( | struct sg_httpsrv * | srv | ) |
Frees the server handle previously allocated by sg_httpsrv_new() or sg_httpsrv_new2().
[in] | srv | Pointer of the server to be freed. |
bool sg_httpsrv_tls_listen4 | ( | struct sg_httpsrv * | srv, |
const char * | key, | ||
const char * | pwd, | ||
const char * | cert, | ||
const char * | trust, | ||
const char * | dhparams, | ||
const char * | priorities, | ||
const char * | hostname, | ||
uint16_t | port, | ||
uint32_t | backlog, | ||
bool | threaded | ||
) |
Starts the HTTPS server.
[in] | srv | Server handle. |
[in] | key | Memory pointer for the private key (key.pem) to be used by the HTTPS server. |
[in] | pwd | Password for the private key. |
[in] | cert | Memory pointer for the certificate (cert.pem) to be used by the HTTPS server. |
[in] | trust | Memory pointer for the certificate (ca.pem) to be used by the HTTPS server for client authentication. |
[in] | dhparams | Memory pointer for the Diffie Hellman parameters (dh.pem) to be used by the HTTPS server for key exchange. |
[in] | priorities | Memory pointer specifying the cipher algorithm. Default: "NORMAL" . |
[in] | hostname | Host name for listening to connections. |
[in] | port | Port for listening to connections. |
[in] | backlog | Maximum length of the queue of pending connections. Default: 511 . |
[in] | threaded | Enables/disables the threaded mode. If true , the server creates one thread per connection. |
true | If the server is started, false otherwise. If srv is null, set the errno to EINVAL . |
0
, the operating system will assign an unused port randomly. bool sg_httpsrv_tls_listen3 | ( | struct sg_httpsrv * | srv, |
const char * | key, | ||
const char * | pwd, | ||
const char * | cert, | ||
const char * | trust, | ||
const char * | dhparams, | ||
const char * | priorities, | ||
uint16_t | port, | ||
bool | threaded | ||
) |
Starts the HTTPS server.
[in] | srv | Server handle. |
[in] | key | Memory pointer for the private key (key.pem) to be used by the HTTPS server. |
[in] | pwd | Password for the private key. |
[in] | cert | Memory pointer for the certificate (cert.pem) to be used by the HTTPS server. |
[in] | trust | Memory pointer for the certificate (ca.pem) to be used by the HTTPS server for client authentication. |
[in] | dhparams | Memory pointer for the Diffie Hellman parameters (dh.pem) to be used by the HTTPS server for key exchange. |
[in] | priorities | Memory pointer specifying the cipher algorithm. Default: "NORMAL" . |
[in] | port | Port for listening to connections. |
[in] | threaded | Enables/disables the threaded mode. If true , the server creates one thread per connection. |
true | If the server is started, false otherwise. If srv is null, set the errno to EINVAL . |
0
, the operating system will assign an unused port randomly. bool sg_httpsrv_tls_listen2 | ( | struct sg_httpsrv * | srv, |
const char * | key, | ||
const char * | pwd, | ||
const char * | cert, | ||
const char * | trust, | ||
const char * | dhparams, | ||
uint16_t | port, | ||
bool | threaded | ||
) |
Starts the HTTPS server.
[in] | srv | Server handle. |
[in] | key | Memory pointer for the private key (key.pem) to be used by the HTTPS server. |
[in] | pwd | Password for the private key. |
[in] | cert | Memory pointer for the certificate (cert.pem) to be used by the HTTPS server. |
[in] | trust | Memory pointer for the certificate (ca.pem) to be used by the HTTPS server for client authentication. |
[in] | dhparams | Memory pointer for the Diffie Hellman parameters (dh.pem) to be used by the HTTPS server for key exchange. |
[in] | port | Port for listening to connections. |
[in] | threaded | Enables/disables the threaded mode. If true , the server creates one thread per connection. |
true | If the server is started, false otherwise. If srv is null, set the errno to EINVAL . |
0
, the operating system will assign an unused port randomly. bool sg_httpsrv_tls_listen | ( | struct sg_httpsrv * | srv, |
const char * | key, | ||
const char * | cert, | ||
uint16_t | port, | ||
bool | threaded | ||
) |
Starts the HTTPS server.
[in] | srv | Server handle. |
[in] | key | Memory pointer for the private key (key.pem) to be used by the HTTPS server. |
[in] | cert | Memory pointer for the certificate (cert.pem) to be used by the HTTPS server. |
[in] | port | Port for listening to connections. |
[in] | threaded | Enables/disables the threaded mode. If true , the server creates one thread per connection. |
true | If the server is started, false otherwise. If srv is null, set the errno to EINVAL . |
0
, the operating system will assign an unused port randomly. bool sg_httpsrv_listen2 | ( | struct sg_httpsrv * | srv, |
const char * | hostname, | ||
uint16_t | port, | ||
uint32_t | backlog, | ||
bool | threaded | ||
) |
Starts the HTTP server.
[in] | srv | Server handle. |
[in] | hostname | Host name for listening to connections. |
[in] | port | Port for listening to connections. |
[in] | backlog | Maximum length of the queue of pending connections. Default: 511 . |
[in] | threaded | Enables/disables the threaded mode. If true , the server creates one thread per connection. |
true | If the server is started, false otherwise. If srv is null, set the errno to EINVAL . |
0
, the operating system will randomly assign an unused port. bool sg_httpsrv_listen | ( | struct sg_httpsrv * | srv, |
uint16_t | port, | ||
bool | threaded | ||
) |
Starts the HTTP server.
[in] | srv | Server handle. |
[in] | port | Port for listening to connections. |
[in] | threaded | Enables/disables the threaded mode. If true , the server creates one thread per connection. |
true | If the server is started, false otherwise. If srv is null, set the errno to EINVAL . |
0
, the operating system will randomly assign an unused port. int sg_httpsrv_shutdown | ( | struct sg_httpsrv * | srv | ) |
Stops the server not to accept new connections.
[in] | srv | Server handle. |
0 | If the server is stopped. |
EINVAL | Invalid argument. |
EALREADY | Already shut down. |
uint16_t sg_httpsrv_port | ( | struct sg_httpsrv * | srv | ) |
Returns the server listening port.
[in] | srv | Server handle. |
0
otherwise. If srv
is null, set the errno
to EINVAL
. bool sg_httpsrv_is_threaded | ( | struct sg_httpsrv * | srv | ) |
Checks if the server was started in threaded mode.
[in] | srv | Server handle. |
true | If the server is in threaded mode, false otherwise. If srv is null, set the errno to EINVAL . |
int sg_httpsrv_set_cli_cb | ( | struct sg_httpsrv * | srv, |
sg_httpsrv_cli_cb | cb, | ||
void * | cls | ||
) |
Sets the server callback for client connection events.
[in] | srv | Server handle. |
[in] | cb | Callback to handle client events. |
[in] | cls | User-defined closure. |
0 | Success. |
EINVAL | Invalid argument. |
int sg_httpsrv_set_upld_cbs | ( | struct sg_httpsrv * | srv, |
sg_httpupld_cb | cb, | ||
void * | cls, | ||
sg_write_cb | write_cb, | ||
sg_free_cb | free_cb, | ||
sg_save_cb | save_cb, | ||
sg_save_as_cb | save_as_cb | ||
) |
Sets the server uploading callbacks.
[in] | srv | Server handle. |
[in] | cb | Callback to handle uploaded files and/or fields. |
[in] | cls | User-defined closure. |
[in] | write_cb | Callback to write the stream of the uploaded files. |
[in] | free_cb | Callback to free stream of the uploaded files. |
[in] | save_cb | Callback to save the uploaded files. |
[in] | save_as_cb | Callback to save the uploaded files defining their path. |
0 | Success. |
EINVAL | Invalid argument. |
int sg_httpsrv_set_upld_dir | ( | struct sg_httpsrv * | srv, |
const char * | dir | ||
) |
Sets the directory to save the uploaded files.
[in] | srv | Server handle. |
[in] | dir | Directory as a null-terminated string. |
0 | Success. |
EINVAL | Invalid argument. |
const char * sg_httpsrv_upld_dir | ( | struct sg_httpsrv * | srv | ) |
Gets the directory of the uploaded files.
[in] | srv | Server handle. |
NULL | If the srv is null and set the errno to EINVAL . |
int sg_httpsrv_set_post_buf_size | ( | struct sg_httpsrv * | srv, |
size_t | size | ||
) |
Sets a size to the post buffering.
[in] | srv | Server handle. |
[in] | size | Post buffering size. |
0 | Success. |
EINVAL | Invalid argument. |
size_t sg_httpsrv_post_buf_size | ( | struct sg_httpsrv * | srv | ) |
Gets the size of the post buffering.
[in] | srv | Server handle. |
0 | If the srv is null and set the errno to EINVAL . |
int sg_httpsrv_set_payld_limit | ( | struct sg_httpsrv * | srv, |
size_t | limit | ||
) |
Sets a limit to the total payload.
[in] | srv | Server handle. |
[in] | limit | Payload total limit. Use zero for no limit. |
0 | Success. |
EINVAL | Invalid argument. |
size_t sg_httpsrv_payld_limit | ( | struct sg_httpsrv * | srv | ) |
Gets the limit of the total payload.
[in] | srv | Server handle. |
0 | If the srv is null and set the errno to EINVAL . |
int sg_httpsrv_set_uplds_limit | ( | struct sg_httpsrv * | srv, |
uint64_t | limit | ||
) |
Sets a limit to the total uploads.
[in] | srv | Server handle. |
[in] | limit | Uploads total limit. Use zero for no limit. |
0 | Success. |
EINVAL | Invalid argument. |
uint64_t sg_httpsrv_uplds_limit | ( | struct sg_httpsrv * | srv | ) |
Gets the limit of the total uploads.
[in] | srv | Server handle. |
0 | If the srv is null and set the errno to EINVAL . |
int sg_httpsrv_set_thr_pool_size | ( | struct sg_httpsrv * | srv, |
unsigned int | size | ||
) |
Sets the size for the thread pool.
[in] | srv | Server handle. |
[in] | size | Thread pool size. Size greater than 1 enables the thread pooling. |
0 | Success. |
EINVAL | Invalid argument. |
unsigned int sg_httpsrv_thr_pool_size | ( | struct sg_httpsrv * | srv | ) |
Gets the size of the thread pool.
[in] | srv | Server handle. |
0 | If the srv is null and set the errno to EINVAL . |
int sg_httpsrv_set_con_timeout | ( | struct sg_httpsrv * | srv, |
unsigned int | timeout | ||
) |
Sets the inactivity time to a client to get time out.
[in] | srv | Server handle. |
[in] | timeout | Timeout (in seconds). Use zero for infinity timeout. Default: 15 seconds. |
0 | Success. |
EINVAL | Invalid argument. |
unsigned int sg_httpsrv_con_timeout | ( | struct sg_httpsrv * | srv | ) |
Gets the inactivity time to a client to get time out.
[in] | srv | Server handle. |
0 | If the srv is null and set the errno to EINVAL . |
int sg_httpsrv_set_con_limit | ( | struct sg_httpsrv * | srv, |
unsigned int | limit | ||
) |
Sets the limit of concurrent connections.
[in] | srv | Server handle. |
[in] | limit | Concurrent connections limit. Use zero for no limit. |
0 | Success. |
EINVAL | Invalid argument. |
unsigned int sg_httpsrv_con_limit | ( | struct sg_httpsrv * | srv | ) |
Gets the limit of concurrent connections.
[in] | srv | Server handle. |
0 | If the srv is null and set the errno to EINVAL . |
void * sg_httpsrv_handle | ( | struct sg_httpsrv * | srv | ) |
Returns the MHD instance.
[in] | srv | Server handle. |
NULL | If srv is null and set the errno to EINVAL . |