Backport from xs.

This commit is contained in:
default 2023-01-12 09:28:02 +01:00
parent 16be679ecd
commit bb0d8f2a27
11 changed files with 45 additions and 45 deletions

2
data.c
View File

@ -31,7 +31,7 @@ int srv_open(char *basedir, int auto_upgrade)
srv_basedir = xs_str_new(basedir); srv_basedir = xs_str_new(basedir);
if (xs_endswith(srv_basedir, "/")) if (xs_endswith(srv_basedir, "/"))
srv_basedir = xs_crop(srv_basedir, 0, -1); srv_basedir = xs_crop_i(srv_basedir, 0, -1);
cfg_file = xs_fmt("%s/server.json", basedir); cfg_file = xs_fmt("%s/server.json", basedir);

View File

@ -53,19 +53,19 @@ static d_char *format_line(const char *line)
if ((n & 0x1)) { if ((n & 0x1)) {
/* markup */ /* markup */
if (xs_startswith(v, "`")) { if (xs_startswith(v, "`")) {
xs *s1 = xs_crop(xs_dup(v), 1, -1); xs *s1 = xs_crop_i(xs_dup(v), 1, -1);
xs *s2 = xs_fmt("<code>%s</code>", s1); xs *s2 = xs_fmt("<code>%s</code>", s1);
s = xs_str_cat(s, s2); s = xs_str_cat(s, s2);
} }
else else
if (xs_startswith(v, "**")) { if (xs_startswith(v, "**")) {
xs *s1 = xs_crop(xs_dup(v), 2, -2); xs *s1 = xs_crop_i(xs_dup(v), 2, -2);
xs *s2 = xs_fmt("<b>%s</b>", s1); xs *s2 = xs_fmt("<b>%s</b>", s1);
s = xs_str_cat(s, s2); s = xs_str_cat(s, s2);
} }
else else
if (xs_startswith(v, "*")) { if (xs_startswith(v, "*")) {
xs *s1 = xs_crop(xs_dup(v), 1, -1); xs *s1 = xs_crop_i(xs_dup(v), 1, -1);
xs *s2 = xs_fmt("<i>%s</i>", s1); xs *s2 = xs_fmt("<i>%s</i>", s1);
s = xs_str_cat(s, s2); s = xs_str_cat(s, s2);
} }
@ -117,11 +117,11 @@ d_char *not_really_markdown(const char *content)
if (in_pre) if (in_pre)
ss = xs_dup(v); ss = xs_dup(v);
else else
ss = xs_strip(format_line(v)); ss = xs_strip_i(format_line(v));
if (xs_startswith(ss, ">")) { if (xs_startswith(ss, ">")) {
/* delete the > and subsequent spaces */ /* delete the > and subsequent spaces */
ss = xs_strip(xs_crop(ss, 1, 0)); ss = xs_strip_i(xs_crop_i(ss, 1, 0));
if (!in_blq) { if (!in_blq) {
s = xs_str_cat(s, "<blockquote>"); s = xs_str_cat(s, "<blockquote>");
@ -184,9 +184,9 @@ d_char *sanitize(const char *content)
while (xs_list_iter(&p, &v)) { while (xs_list_iter(&p, &v)) {
if (n & 0x1) { if (n & 0x1) {
xs *s1 = xs_strip(xs_crop(xs_dup(v), v[1] == '/' ? 2 : 1, -1)); xs *s1 = xs_strip_i(xs_crop_i(xs_dup(v), v[1] == '/' ? 2 : 1, -1));
xs *l1 = xs_split_n(s1, " ", 1); xs *l1 = xs_split_n(s1, " ", 1);
xs *tag = xs_tolower(xs_dup(xs_list_get(l1, 0))); xs *tag = xs_tolower_i(xs_dup(xs_list_get(l1, 0)));
xs *s2 = NULL; xs *s2 = NULL;
int i; int i;

10
html.c
View File

@ -21,7 +21,7 @@ int login(snac *snac, char *headers)
if (auth && xs_startswith(auth, "Basic ")) { if (auth && xs_startswith(auth, "Basic ")) {
int sz; int sz;
xs *s1 = xs_crop(xs_dup(auth), 6, 0); xs *s1 = xs_crop_i(xs_dup(auth), 6, 0);
xs *s2 = xs_base64_dec(s1, &sz); xs *s2 = xs_base64_dec(s1, &sz);
xs *l1 = xs_split_n(s2, ":", 1); xs *l1 = xs_split_n(s2, ":", 1);
@ -108,11 +108,11 @@ d_char *html_actor_icon(snac *snac, d_char *os, char *actor,
s = xs_str_cat(s, "<br>\n&nbsp;\n"); s = xs_str_cat(s, "<br>\n&nbsp;\n");
} }
else { else {
xs *date_label = xs_crop(xs_dup(date), 0, 10); xs *date_label = xs_crop_i(xs_dup(date), 0, 10);
xs *date_title = xs_dup(date); xs *date_title = xs_dup(date);
if (!xs_is_null(udate)) { if (!xs_is_null(udate)) {
xs *sd = xs_crop(xs_dup(udate), 0, 10); xs *sd = xs_crop_i(xs_dup(udate), 0, 10);
date_label = xs_str_cat(date_label, " / "); date_label = xs_str_cat(date_label, " / ");
date_label = xs_str_cat(date_label, sd); date_label = xs_str_cat(date_label, sd);
@ -682,7 +682,7 @@ d_char *html_entry(snac *snac, d_char *os, char *msg, int local, int level, cons
c = xs_replace_i(c, "\r", ""); c = xs_replace_i(c, "\r", "");
while (xs_endswith(c, "<br><br>")) while (xs_endswith(c, "<br><br>"))
c = xs_crop(c, 0, -4); c = xs_crop_i(c, 0, -4);
c = xs_replace_i(c, "<br><br>", "<p>"); c = xs_replace_i(c, "<br><br>", "<p>");
@ -1045,7 +1045,7 @@ int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char *
/* rss extension? */ /* rss extension? */
if (xs_endswith(uid, ".rss")) { if (xs_endswith(uid, ".rss")) {
uid = xs_crop(uid, 0, -4); uid = xs_crop_i(uid, 0, -4);
p_path = ".rss"; p_path = ".rss";
} }
else else

10
http.c
View File

@ -123,19 +123,19 @@ static int _check_signature(snac *snac, char *req, char **err)
p = l; p = l;
while (xs_list_iter(&p, &v)) { while (xs_list_iter(&p, &v)) {
if (xs_startswith(v, "keyId")) if (xs_startswith(v, "keyId"))
keyId = xs_crop(xs_dup(v), 7, -1); keyId = xs_crop_i(xs_dup(v), 7, -1);
else else
if (xs_startswith(v, "headers")) if (xs_startswith(v, "headers"))
headers = xs_crop(xs_dup(v), 9, -1); headers = xs_crop_i(xs_dup(v), 9, -1);
else else
if (xs_startswith(v, "signature")) if (xs_startswith(v, "signature"))
signature = xs_crop(xs_dup(v), 11, -1); signature = xs_crop_i(xs_dup(v), 11, -1);
else else
if (xs_startswith(v, "created")) if (xs_startswith(v, "created"))
created = xs_crop(xs_dup(v), 9, -1); created = xs_crop_i(xs_dup(v), 9, -1);
else else
if (xs_startswith(v, "expires")) if (xs_startswith(v, "expires"))
expires = xs_crop(xs_dup(v), 9, -1); expires = xs_crop_i(xs_dup(v), 9, -1);
} }
} }

View File

@ -160,11 +160,11 @@ void httpd_connection(FILE *f)
/* crop the q_path from leading / and the prefix */ /* crop the q_path from leading / and the prefix */
if (xs_endswith(q_path, "/")) if (xs_endswith(q_path, "/"))
q_path = xs_crop(q_path, 0, -1); q_path = xs_crop_i(q_path, 0, -1);
p = xs_dict_get(srv_config, "prefix"); p = xs_dict_get(srv_config, "prefix");
if (xs_startswith(q_path, p)) if (xs_startswith(q_path, p))
q_path = xs_crop(q_path, strlen(p), 0); q_path = xs_crop_i(q_path, strlen(p), 0);
if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0) { if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0) {
/* cascade through */ /* cascade through */

16
utils.c
View File

@ -85,7 +85,7 @@ int initdb(const char *basedir)
if (basedir == NULL) { if (basedir == NULL) {
printf("Base directory:\n"); printf("Base directory:\n");
srv_basedir = xs_strip(xs_readline(stdin)); srv_basedir = xs_strip_i(xs_readline(stdin));
} }
else else
srv_basedir = xs_str_new(basedir); srv_basedir = xs_str_new(basedir);
@ -94,7 +94,7 @@ int initdb(const char *basedir)
return 1; return 1;
if (xs_endswith(srv_basedir, "/")) if (xs_endswith(srv_basedir, "/"))
srv_basedir = xs_crop(srv_basedir, 0, -1); srv_basedir = xs_crop_i(srv_basedir, 0, -1);
if (mtime(srv_basedir) != 0.0) { if (mtime(srv_basedir) != 0.0) {
printf("ERROR: directory '%s' must not exist\n", srv_basedir); printf("ERROR: directory '%s' must not exist\n", srv_basedir);
@ -108,14 +108,14 @@ int initdb(const char *basedir)
printf("Network address [%s]:\n", xs_dict_get(srv_config, "address")); printf("Network address [%s]:\n", xs_dict_get(srv_config, "address"));
{ {
xs *i = xs_strip(xs_readline(stdin)); xs *i = xs_strip_i(xs_readline(stdin));
if (*i) if (*i)
srv_config = xs_dict_set(srv_config, "address", i); srv_config = xs_dict_set(srv_config, "address", i);
} }
printf("Network port [%d]:\n", (int)xs_number_get(xs_dict_get(srv_config, "port"))); printf("Network port [%d]:\n", (int)xs_number_get(xs_dict_get(srv_config, "port")));
{ {
xs *i = xs_strip(xs_readline(stdin)); xs *i = xs_strip_i(xs_readline(stdin));
if (*i) { if (*i) {
xs *n = xs_number_new(atoi(i)); xs *n = xs_number_new(atoi(i));
srv_config = xs_dict_set(srv_config, "port", n); srv_config = xs_dict_set(srv_config, "port", n);
@ -124,7 +124,7 @@ int initdb(const char *basedir)
printf("Host name:\n"); printf("Host name:\n");
{ {
xs *i = xs_strip(xs_readline(stdin)); xs *i = xs_strip_i(xs_readline(stdin));
if (*i == '\0') if (*i == '\0')
return 1; return 1;
@ -133,11 +133,11 @@ int initdb(const char *basedir)
printf("URL prefix:\n"); printf("URL prefix:\n");
{ {
xs *i = xs_strip(xs_readline(stdin)); xs *i = xs_strip_i(xs_readline(stdin));
if (*i) { if (*i) {
if (xs_endswith(i, "/")) if (xs_endswith(i, "/"))
i = xs_crop(i, 0, -1); i = xs_crop_i(i, 0, -1);
srv_config = xs_dict_set(srv_config, "prefix", i); srv_config = xs_dict_set(srv_config, "prefix", i);
} }
@ -215,7 +215,7 @@ int adduser(const char *uid)
if (uid == NULL) { if (uid == NULL) {
printf("User id:\n"); printf("User id:\n");
uid = xs_strip(xs_readline(stdin)); uid = xs_strip_i(xs_readline(stdin));
} }
if (!validate_uid(uid)) { if (!validate_uid(uid)) {

View File

@ -33,7 +33,7 @@ int webfinger_request(char *qs, char **actor, char **user)
xs *s = xs_dup(qs); xs *s = xs_dup(qs);
if (xs_startswith(s, "@")) if (xs_startswith(s, "@"))
s = xs_crop(s, 1, 0); s = xs_crop_i(s, 1, 0);
l = xs_split_n(s, "@", 1); l = xs_split_n(s, "@", 1);
@ -141,7 +141,7 @@ int webfinger_get_handler(d_char *req, char *q_path,
/* strip a possible leading @ */ /* strip a possible leading @ */
if (xs_startswith(an, "@")) if (xs_startswith(an, "@"))
an = xs_crop(an, 1, 0); an = xs_crop_i(an, 1, 0);
l = xs_split_n(an, "@", 1); l = xs_split_n(an, "@", 1);

18
xs.h
View File

@ -60,11 +60,11 @@ d_char *xs_fmt(const char *fmt, ...);
int xs_str_in(const char *haystack, const char *needle); int xs_str_in(const char *haystack, const char *needle);
int xs_startswith(const char *str, const char *prefix); int xs_startswith(const char *str, const char *prefix);
int xs_endswith(const char *str, const char *postfix); int xs_endswith(const char *str, const char *postfix);
d_char *xs_crop(d_char *str, int start, int end); d_char *xs_crop_i(d_char *str, int start, int end);
d_char *xs_strip_chars(d_char *str, const char *chars); d_char *xs_strip_chars_i(d_char *str, const char *chars);
#define xs_strip(str) xs_strip_chars(str, " \r\n\t\v\f") #define xs_strip_i(str) xs_strip_chars_i(str, " \r\n\t\v\f")
d_char *xs_tolower(d_char *str); d_char *xs_tolower_i(d_char *str);
d_char *xs_str_prepend(d_char *str, const char *prefix); d_char *xs_str_prepend_i(d_char *str, const char *prefix);
d_char *xs_list_new(void); d_char *xs_list_new(void);
d_char *xs_list_append_m(d_char *list, const char *mem, int dsz); d_char *xs_list_append_m(d_char *list, const char *mem, int dsz);
#define xs_list_append(list, data) xs_list_append_m(list, data, xs_size(data)) #define xs_list_append(list, data) xs_list_append_m(list, data, xs_size(data))
@ -439,7 +439,7 @@ int xs_endswith(const char *str, const char *postfix)
} }
d_char *xs_crop(d_char *str, int start, int end) d_char *xs_crop_i(d_char *str, int start, int end)
/* crops the d_char to be only from start to end */ /* crops the d_char to be only from start to end */
{ {
int sz = strlen(str); int sz = strlen(str);
@ -457,7 +457,7 @@ d_char *xs_crop(d_char *str, int start, int end)
} }
d_char *xs_strip_chars(d_char *str, const char *chars) d_char *xs_strip_chars_i(d_char *str, const char *chars)
/* strips the string of chars from the start and the end */ /* strips the string of chars from the start and the end */
{ {
int n; int n;
@ -478,7 +478,7 @@ d_char *xs_strip_chars(d_char *str, const char *chars)
} }
d_char *xs_tolower(d_char *str) d_char *xs_tolower_i(d_char *str)
/* convert to lowercase */ /* convert to lowercase */
{ {
int n; int n;
@ -490,7 +490,7 @@ d_char *xs_tolower(d_char *str)
} }
d_char *xs_str_prepend(d_char *str, const char *prefix) d_char *xs_str_prepend_i(d_char *str, const char *prefix)
/* prepends prefix into string */ /* prepends prefix into string */
{ {
int sz = strlen(prefix); int sz = strlen(prefix);

View File

@ -21,13 +21,13 @@ static size_t _header_callback(char *buffer, size_t size,
/* get the line */ /* get the line */
l = xs_str_new(NULL); l = xs_str_new(NULL);
l = xs_append_m(l, buffer, size * nitems); l = xs_append_m(l, buffer, size * nitems);
l = xs_strip(l); l = xs_strip_i(l);
/* only the HTTP/x 200 line and the last one doesn't have ': ' */ /* only the HTTP/x 200 line and the last one doesn't have ': ' */
if (xs_str_in(l, ": ") != -1) { if (xs_str_in(l, ": ") != -1) {
xs *knv = xs_split_n(l, ": ", 1); xs *knv = xs_split_n(l, ": ", 1);
xs_tolower(xs_list_get(knv, 0)); xs_tolower_i(xs_list_get(knv, 0));
headers = xs_dict_set(headers, xs_list_get(knv, 0), xs_list_get(knv, 1)); headers = xs_dict_set(headers, xs_list_get(knv, 0), xs_list_get(knv, 1));
} }

View File

@ -185,7 +185,7 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
xs_socket_timeout(fileno(f), 2.0, 0.0); xs_socket_timeout(fileno(f), 2.0, 0.0);
/* read the first line and split it */ /* read the first line and split it */
l1 = xs_strip(xs_readline(f)); l1 = xs_strip_i(xs_readline(f));
l2 = xs_split(l1, " "); l2 = xs_split(l1, " ");
if (xs_list_len(l2) != 3) { if (xs_list_len(l2) != 3) {
@ -214,7 +214,7 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
for (;;) { for (;;) {
xs *l, *p = NULL; xs *l, *p = NULL;
l = xs_strip(xs_readline(f)); l = xs_strip_i(xs_readline(f));
/* done with the header? */ /* done with the header? */
if (strcmp(l, "") == 0) if (strcmp(l, "") == 0)
@ -224,7 +224,7 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
p = xs_split_n(l, ": ", 1); p = xs_split_n(l, ": ", 1);
if (xs_list_len(p) == 2) if (xs_list_len(p) == 2)
req = xs_dict_append(req, xs_tolower(xs_list_get(p, 0)), xs_list_get(p, 1)); req = xs_dict_append(req, xs_tolower_i(xs_list_get(p, 0)), xs_list_get(p, 1));
} }
xs_socket_timeout(fileno(f), 5.0, 0.0); xs_socket_timeout(fileno(f), 5.0, 0.0);

View File

@ -1 +1 @@
/* 38b055a19829fb788f1bb6917f76cc4830388e35 */ /* 41985e780c622a078118ea7c7143dc304758d958 */