Skip to content

Commit

Permalink
Remove useless OOM and applies several code improvements in the error…
Browse files Browse the repository at this point in the history
… handling. (fix #13)
  • Loading branch information
silvioprog committed Feb 5, 2019
1 parent 8d9a8bb commit 74914ac
Show file tree
Hide file tree
Showing 51 changed files with 484 additions and 418 deletions.
2 changes: 1 addition & 1 deletion cmake/libsagui.rc.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down
2 changes: 1 addition & 1 deletion doxygen/example_httpcomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down
2 changes: 1 addition & 1 deletion examples/example_entrypoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static void r1_route_cb(void *cls, struct sg_route *route) {
fflush(stdout);
}

static void r2_route_cb(__SG_UNUSED void *cls, struct sg_route *route) {
static void r2_route_cb(void *cls, struct sg_route *route) {
fprintf(stdout, "%s: %s\n", sg_route_path(route), (const char *) cls);
fflush(stdout);
}
Expand Down
5 changes: 3 additions & 2 deletions examples/example_httpauth.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down Expand Up @@ -42,7 +42,8 @@ static bool auth_cb(__SG_UNUSED void *cls, struct sg_httpauth *auth, __SG_UNUSED
__SG_UNUSED struct sg_httpres *res) {
bool pass;
sg_httpauth_set_realm(auth, "My realm");
if (!(pass = strmatch(sg_httpauth_usr(auth), "abc") && strmatch(sg_httpauth_pwd(auth), "123")))
pass = strmatch(sg_httpauth_usr(auth), "abc") && strmatch(sg_httpauth_pwd(auth), "123");
if (!pass)
sg_httpauth_deny(auth,
"<html><head><title>Denied</title></head><body><font color=\"red\">Go away</font></body></html>",
"text/html; charset=utf-8");
Expand Down
10 changes: 6 additions & 4 deletions examples/example_httpcomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
static void req_cb(__SG_UNUSED void *cls, struct sg_httpreq *req, struct sg_httpres *res) {
struct sg_strmap **headers;
const char *header;
if ((headers = sg_httpreq_headers(req)) && (header = sg_strmap_get(*headers, "Accept-Encoding")) &&
strstr(header, "deflate"))
sg_httpres_zsendbinary(res, PAGE, strlen(PAGE), CONTENT_TYPE, 200);
else
headers = sg_httpreq_headers(req);
if (headers) {
header = sg_strmap_get(*headers, "Accept-Encoding");
if (header && strstr(header, "deflate"))
sg_httpres_zsendbinary(res, PAGE, strlen(PAGE), CONTENT_TYPE, 200);
} else
sg_httpres_sendbinary(res, PAGE, strlen(PAGE), CONTENT_TYPE, 200);
}

Expand Down
7 changes: 4 additions & 3 deletions examples/example_httpcookie.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down Expand Up @@ -45,14 +45,15 @@ static int strtoint(const char *str) {
return (int) strtol(str, NULL, 10);
}

static void req_cb(__SG_UNUSED void *cls, __SG_UNUSED struct sg_httpreq *req, struct sg_httpres *res) {
struct sg_strmap **cookies = sg_httpreq_cookies(req);
static void req_cb(__SG_UNUSED void *cls, struct sg_httpreq *req, struct sg_httpres *res) {
struct sg_strmap **cookies;
char str[100];
int count;
if (strcmp(sg_httpreq_path(req), "/favicon.ico") == 0) {
sg_httpres_send(res, "", "", 204);
return;
}
cookies = sg_httpreq_cookies(req);
count = cookies ? strtoint(sg_strmap_get(*cookies, COOKIE_NAME)) : 0;
if (count == 0) {
snprintf(str, sizeof(str), INITIAL_PAGE);
Expand Down
4 changes: 2 additions & 2 deletions examples/example_httpreq_payload.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down Expand Up @@ -38,7 +38,7 @@

/* NOTE: Error checking has been omitted to make it clear. */

static void req_cb(__SG_UNUSED void *cls, __SG_UNUSED struct sg_httpreq *req, __SG_UNUSED struct sg_httpres *res) {
static void req_cb(__SG_UNUSED void *cls, struct sg_httpreq *req, struct sg_httpres *res) {
struct sg_str *payload = sg_httpreq_payload(req);
sg_httpres_send(res, sg_str_content(payload), "text/plain", 200);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/example_httpsrv_tls_cert_auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down Expand Up @@ -136,7 +136,7 @@ static bool sess_verify_cert(gnutls_session_t tls_session, const char *line_brea
return len == 0;
}

static void req_cb(__SG_UNUSED void *cls, __SG_UNUSED struct sg_httpreq *req, struct sg_httpres *res) {
static void req_cb(__SG_UNUSED void *cls, struct sg_httpreq *req, struct sg_httpres *res) {
char msg[ERR_SIZE];
char *color, *page;
size_t page_size;
Expand Down
15 changes: 10 additions & 5 deletions examples/example_httpuplds.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down Expand Up @@ -77,7 +77,8 @@ static void process_uploads(struct sg_httpreq *req, struct sg_httpres *res) {
upld = sg_httpreq_uploads(req);
while (upld) {
name = sg_httpupld_name(upld);
if ((errnum = sg_httpupld_save(upld, true)) == 0)
errnum = sg_httpupld_save(upld, true);
if (errnum == 0)
sg_str_printf(body, "<li><a href=\"?file=%s\">%s</a></li>", name, name);
else {
sg_strerror(errnum, errmsg, sizeof(errmsg));
Expand All @@ -101,9 +102,13 @@ static void req_cb(__SG_UNUSED void *cls, struct sg_httpreq *req, struct sg_http
if (sg_httpreq_is_uploading(req))
process_uploads(req, res);
else {
if ((qs = sg_httpreq_params(req)) && (file = sg_strmap_get(*qs, "file"))) {
sprintf(path, "%s%c%s", sg_tmpdir(), PATH_SEP, file);
sg_httpres_download(res, path, 200);
qs = sg_httpreq_params(req);
if (qs) {
file = sg_strmap_get(*qs, "file");
if (file) {
sprintf(path, "%s%c%s", sg_tmpdir(), PATH_SEP, file);
sg_httpres_download(res, path, 200);
}
} else
sg_httpres_send(res, PAGE_FORM, CONTENT_TYPE, 200);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/example_router_segments.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down
18 changes: 9 additions & 9 deletions examples/example_router_srv.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down Expand Up @@ -57,32 +57,32 @@ static int route_download_file_cb(void *cls, const char *name, const char *val)
}

static void route_home_cb(__SG_UNUSED void *cls, struct sg_route *route) {
struct holder *h = sg_route_user_data(route);
sg_httpres_send(h->res, "<html><head><title>Home</title></head><body>Home</body></html>", "text/html", 200);
struct holder *holder = sg_route_user_data(route);
sg_httpres_send(holder->res, "<html><head><title>Home</title></head><body>Home</body></html>", "text/html", 200);
}

static void route_download_cb(__SG_UNUSED void *cls, struct sg_route *route) {
struct holder *h = sg_route_user_data(route);
struct holder *holder = sg_route_user_data(route);
struct sg_str *page = sg_str_new();
char file[256];
memset(file, 0, sizeof(file));
sg_route_vars_iter(route, route_download_file_cb, file);
if (strlen(file) == 0)
strcpy(file, "Download");
sg_str_printf(page, "<html><head><title>Download</title></head><body>%s</body></html>", file);
sg_httpres_send(h->res, sg_str_content(page), "text/html", 200);
sg_httpres_send(holder->res, sg_str_content(page), "text/html", 200);
sg_str_free(page);
}

static void route_about_cb(__SG_UNUSED void *cls, struct sg_route *route) {
struct holder *h = sg_route_user_data(route);
sg_httpres_send(h->res, "<html><head><title>About</title></head><body>About</body></html>", "text/html", 200);
struct holder *holder = sg_route_user_data(route);
sg_httpres_send(holder->res, "<html><head><title>About</title></head><body>About</body></html>", "text/html", 200);
}

static void req_cb(__SG_UNUSED void *cls, struct sg_httpreq *req, struct sg_httpres *res) {
struct sg_router *router = cls;
struct holder h = {req, res};
if (sg_router_dispatch(router, sg_httpreq_path(req), &h) != 0)
struct holder holder = {req, res};
if (sg_router_dispatch(router, sg_httpreq_path(req), &holder) != 0)
sg_httpres_send(res, "<html><head><title>Not found</title></head><body>404</body></html>", "text/html", 404);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/example_router_vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down
7 changes: 4 additions & 3 deletions include/sagui.h
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,7 @@ SG_EXTERN int sg_route_segments_iter(struct sg_route *route, sg_segments_iter_cb
* \param[in,out] cls User-specified value.
* \retval 0 Success.
* \retval EINVAL Invalid argument.
* \retval ENOMEM Out of memory.
* \return Callback result when it is different from `0`.
*/
SG_EXTERN int sg_route_vars_iter(struct sg_route *route, sg_vars_iter_cb cb, void *cls);
Expand All @@ -1476,10 +1477,10 @@ SG_EXTERN void *sg_route_user_data(struct sg_route *route);
* \retval 0 Success.
* \retval EINVAL Invalid argument.
* \retval EALREADY Route already added.
* \retval ENOMEM Out of memory.
* \note The pattern is enclosed between `^` and `$` automatically if it does not start with `(`.
* \note The escape sequence \\K is not supported. It causes `EINVAL` if used.
* \note The pattern is compiled using just-in-time optimization (JIT) when it is supported.
* \warning It exits the application if called when no memory space is available.
*/
SG_EXTERN int sg_routes_add2(struct sg_route **routes, struct sg_route **route, const char *pattern,
char *errmsg, size_t errlen, sg_route_cb cb, void *cls);
Expand All @@ -1493,10 +1494,10 @@ SG_EXTERN int sg_routes_add2(struct sg_route **routes, struct sg_route **route,
* \retval 0 Success.
* \retval EINVAL Invalid argument.
* \retval EALREADY Route already added.
* \retval ENOMEM Out of memory.
* \note The pattern is enclosed between `^` and `$` automatically if it does not start with `(`.
* \note The escape sequence \\K is not supported. It causes `EINVAL` if used.
* \note The pattern is compiled using just-in-time optimization (JIT) when it is supported.
* \warning It exits the application if called when no memory space is available.
*/
SG_EXTERN bool sg_routes_add(struct sg_route **routes, const char *pattern, sg_route_cb cb, void *cls);

Expand Down Expand Up @@ -1572,7 +1573,7 @@ typedef int (*sg_router_match_cb)(void *cls, struct sg_route *route);
* Creates a new path router handle. It requires a filled route list \pr{routes}.
* \param[in] routes Route list handle.
* \return New router handle.
* \retval NULL If the \pr{routes} is null and sets the `errno` to `EINVAL`.
* \retval NULL If the \pr{routes} is null and sets the `errno` to `EINVAL` or no memory space.
*/
SG_EXTERN struct sg_router *sg_router_new(struct sg_route *routes)
__SG_MALLOC;
Expand Down
2 changes: 1 addition & 1 deletion src/sg_entrypoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down
2 changes: 1 addition & 1 deletion src/sg_entrypoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down
2 changes: 1 addition & 1 deletion src/sg_entrypoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down
2 changes: 1 addition & 1 deletion src/sg_httpauth.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down
2 changes: 1 addition & 1 deletion src/sg_httpauth.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down
27 changes: 20 additions & 7 deletions src/sg_httpreq.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down Expand Up @@ -38,16 +38,28 @@

struct sg_httpreq *sg__httpreq_new(struct MHD_Connection *con, const char *version, const char *method,
const char *path) {
struct sg_httpreq *req;
sg__new(req);
struct sg_httpreq *req = sg_alloc(sizeof(struct sg_httpreq));
if (!req)
return NULL;
req->res = sg__httpres_new(con);
if (!req->res)
goto error;
req->auth = sg__httpauth_new(req->res);
if (!req->auth)
goto error;
req->payload = sg_str_new();
if (!req->payload)
goto error;
req->con = con;
req->version = version;
req->method = method;
req->path = path;
return req;
error:
sg__httpres_free(req->res);
sg__httpauth_free(req->auth);
sg_str_free(req->payload);
return NULL;
}

void sg__httpreq_free(struct sg_httpreq *req) {
Expand All @@ -62,7 +74,7 @@ void sg__httpreq_free(struct sg_httpreq *req) {
MHD_destroy_post_processor(req->pp);
sg__httpres_free(req->res);
sg__httpauth_free(req->auth);
sg__free(req);
sg_free(req);
}

struct sg_strmap **sg_httpreq_headers(struct sg_httpreq *req) {
Expand Down Expand Up @@ -149,9 +161,10 @@ struct sg_httpupld *sg_httpreq_uploads(struct sg_httpreq *req) {

void *sg_httpreq_tls_session(struct sg_httpreq *req) {
const union MHD_ConnectionInfo *info;
if (req)
return (info = MHD_get_connection_info(req->con,
MHD_CONNECTION_INFO_GNUTLS_SESSION, NULL)) ? info->tls_session : NULL;
if (req) {
info = MHD_get_connection_info(req->con, MHD_CONNECTION_INFO_GNUTLS_SESSION, NULL);
return info ? info->tls_session : NULL;
}
errno = EINVAL;
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/sg_httpreq.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* –– cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
* Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
*
* This file is part of Sagui library.
*
Expand Down
Loading

0 comments on commit 74914ac

Please sign in to comment.