-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove useless OOM and applies several code improvements in the error…
… handling. (fix #13)
- Loading branch information
1 parent
8d9a8bb
commit 74914ac
Showing
51 changed files
with
484 additions
and
418 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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)); | ||
|
@@ -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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
@@ -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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
Oops, something went wrong.