aboutsummaryrefslogtreecommitdiff
path: root/page.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-03-06 05:09:56 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-03-06 05:51:49 +0100
commitff8da797a143cb1dfbeb4ce2d2c3e4a1d0c2e56a (patch)
treec51bbbd084f531d11ba765186cc5d39709e40ba3 /page.c
parent804b8841f3fe59f7058c91fa25c1694f4433642a (diff)
downloadslcl-ff8da797a143cb1dfbeb4ce2d2c3e4a1d0c2e56a.tar.gz
Implement user quota
This feature allows admins to set a specific quota for each user, in MiB. This feature is particularly useful for shared instances, where unlimited user storage might be unfeasible or even dangerous for the server. Also, a nice HTML5 <progress> element has been added to the site that shows how much of the quota has been consumed. If no quota is set, slcl falls back to the default behaviour i.e., assume unlimited storage. Limitations: - While HTTP does specify a Content-Length, which determines the length of the whole request, it does not specify how many files are involved or their individual sizes. - Because of this, if multiple files are uploaded simultaneously, the whole request would be dropped if user quota is exceeded, even if not all files exceeded it. - Also, Content-Length adds the length of some HTTP boilerplate (e.g.: boundaries), but slcl must rely on this before accepting the whole request. In other words, this means some requests might be rejected by slcl because of the extra bytes caused by such boilerplate. - When the quota is exceeded, slcl must close the connection so that the rest of the transfer is cancelled. Unfortunately, this means no HTML can be sent back to the customer to inform about the situation.
Diffstat (limited to 'page.c')
-rw-r--r--page.c104
1 files changed, 101 insertions, 3 deletions
diff --git a/page.c b/page.c
index 87486ae..4471cd2 100644
--- a/page.c
+++ b/page.c
@@ -374,6 +374,97 @@ static int prepare_mkdir_form(struct html_node *const n, const char *const dir)
return 0;
}
+static int prepare_quota_form(struct html_node *const n,
+ const struct page_quota *const q)
+{
+ int ret = -1;
+ struct html_node *progress = NULL, *div, *label;
+ char cur[sizeof "18446744073709551615"], max[sizeof cur];
+ int res = snprintf(cur, sizeof cur, "%llu", q->cur);
+ struct dynstr d, pd;
+
+ dynstr_init(&d);
+ dynstr_init(&pd);
+
+ if (res < 0 || res >= sizeof cur)
+ {
+ fprintf(stderr, "%s: snprintf(3) cur failed\n", __func__);
+ goto end;
+ }
+
+ res = snprintf(max, sizeof max, "%llu", q->max);
+
+ if (res < 0 || res >= sizeof cur)
+ {
+ fprintf(stderr, "%s: snprintf(3) max failed\n", __func__);
+ goto end;
+ }
+ else if (!(div = html_node_add_child(n, "div")))
+ {
+ fprintf(stderr, "%s: html_node_add_child div failed\n", __func__);
+ goto end;
+ }
+ else if (!(label = html_node_add_child(div, "label")))
+ {
+ fprintf(stderr, "%s: html_node_add_child label failed\n", __func__);
+ goto end;
+ }
+ else if (!(progress = html_node_alloc("progress")))
+ {
+ fprintf(stderr, "%s: html_node_alloc progress failed\n", __func__);
+ goto end;
+ }
+ else if (html_node_add_attr(progress, "value", cur))
+ {
+ fprintf(stderr, "%s: html_node_add_attr value failed\n", __func__);
+ goto end;
+ }
+ else if (html_node_add_attr(progress, "max", max))
+ {
+ fprintf(stderr, "%s: html_node_add_attr max failed\n", __func__);
+ goto end;
+ }
+ else if (dynstr_append(&pd, "%llu/%llu bytes", q->cur, q->max))
+ {
+ fprintf(stderr, "%s: dynstr_append pd failed\n", __func__);
+ goto end;
+ }
+ else if (html_node_set_value(progress, pd.str))
+ {
+ fprintf(stderr, "%s: html_node_set_value progress failed\n", __func__);
+ goto end;
+ }
+ else if (dynstr_append(&d, "File quota: "))
+ {
+ fprintf(stderr, "%s: dynstr_append failed\n", __func__);
+ goto end;
+ }
+ else if (html_serialize(progress, &d))
+ {
+ fprintf(stderr, "%s: html_serialize failed\n", __func__);
+ goto end;
+ }
+ else if (dynstr_append(&d, "%s", pd.str))
+ {
+ fprintf(stderr, "%s: dynstr_append failed\n", __func__);
+ goto end;
+ }
+ else if (html_node_set_value_unescaped(label, d.str))
+ {
+ fprintf(stderr, "%s: html_node_set_value_unescaped label failed\n",
+ __func__);
+ goto end;
+ }
+
+ ret = 0;
+
+end:
+ html_node_free(progress);
+ dynstr_free(&d);
+ dynstr_free(&pd);
+ return ret;
+}
+
static int prepare_logout_form(struct html_node *const n)
{
struct html_node *div, *form, *input;
@@ -470,7 +561,8 @@ end:
}
static int list_dir(struct http_response *const r, const char *const dir,
- const char *const root, const char *const res)
+ const char *const root, const char *const res,
+ const struct page_quota *const q)
{
int ret = -1;
DIR *const d = opendir(res);
@@ -560,6 +652,11 @@ static int list_dir(struct http_response *const r, const char *const dir,
fprintf(stderr, "%s: prepare_upload_form failed\n", __func__);
goto end;
}
+ else if (q && prepare_quota_form(body, q))
+ {
+ fprintf(stderr, "%s: prepare_quota_form failed\n", __func__);
+ goto end;
+ }
else if (prepare_logout_form(body))
{
fprintf(stderr, "%s: prepare_logout_form failed\n", __func__);
@@ -652,7 +749,8 @@ static int serve_file(struct http_response *const r,
}
int page_resource(struct http_response *const r, const char *const dir,
- const char *const root, const char *const res)
+ const char *const root, const char *const res,
+ const struct page_quota *const q)
{
struct stat sb;
@@ -677,7 +775,7 @@ int page_resource(struct http_response *const r, const char *const dir,
const mode_t m = sb.st_mode;
if (S_ISDIR(m))
- return list_dir(r, dir, root, res);
+ return list_dir(r, dir, root, res, q);
else if (S_ISREG(m))
return serve_file(r, &sb, res);