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);
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);

View file

@ -53,19 +53,19 @@ static d_char *format_line(const char *line)
if ((n & 0x1)) {
/* markup */
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);
s = xs_str_cat(s, s2);
}
else
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);
s = xs_str_cat(s, s2);
}
else
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);
s = xs_str_cat(s, s2);
}
@ -117,11 +117,11 @@ d_char *not_really_markdown(const char *content)
if (in_pre)
ss = xs_dup(v);
else
ss = xs_strip(format_line(v));
ss = xs_strip_i(format_line(v));
if (xs_startswith(ss, ">")) {
/* 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) {
s = xs_str_cat(s, "<blockquote>");
@ -184,9 +184,9 @@ d_char *sanitize(const char *content)
while (xs_list_iter(&p, &v)) {
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 *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;
int i;

10
html.c
View file

@ -21,7 +21,7 @@ int login(snac *snac, char *headers)
if (auth && xs_startswith(auth, "Basic ")) {
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 *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");
}
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);
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, 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", "");
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>");
@ -1045,7 +1045,7 @@ int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char *
/* rss extension? */
if (xs_endswith(uid, ".rss")) {
uid = xs_crop(uid, 0, -4);
uid = xs_crop_i(uid, 0, -4);
p_path = ".rss";
}
else

10
http.c
View file

@ -123,19 +123,19 @@ static int _check_signature(snac *snac, char *req, char **err)
p = l;
while (xs_list_iter(&p, &v)) {
if (xs_startswith(v, "keyId"))
keyId = xs_crop(xs_dup(v), 7, -1);
keyId = xs_crop_i(xs_dup(v), 7, -1);
else
if (xs_startswith(v, "headers"))
headers = xs_crop(xs_dup(v), 9, -1);
headers = xs_crop_i(xs_dup(v), 9, -1);
else
if (xs_startswith(v, "signature"))
signature = xs_crop(xs_dup(v), 11, -1);
signature = xs_crop_i(xs_dup(v), 11, -1);
else
if (xs_startswith(v, "created"))
created = xs_crop(xs_dup(v), 9, -1);
created = xs_crop_i(xs_dup(v), 9, -1);
else
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 */
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");
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) {
/* cascade through */

16
utils.c
View file

@ -85,7 +85,7 @@ int initdb(const char *basedir)
if (basedir == NULL) {
printf("Base directory:\n");
srv_basedir = xs_strip(xs_readline(stdin));
srv_basedir = xs_strip_i(xs_readline(stdin));
}
else
srv_basedir = xs_str_new(basedir);
@ -94,7 +94,7 @@ int initdb(const char *basedir)
return 1;
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) {
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"));
{
xs *i = xs_strip(xs_readline(stdin));
xs *i = xs_strip_i(xs_readline(stdin));
if (*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")));
{
xs *i = xs_strip(xs_readline(stdin));
xs *i = xs_strip_i(xs_readline(stdin));
if (*i) {
xs *n = xs_number_new(atoi(i));
srv_config = xs_dict_set(srv_config, "port", n);
@ -124,7 +124,7 @@ int initdb(const char *basedir)
printf("Host name:\n");
{
xs *i = xs_strip(xs_readline(stdin));
xs *i = xs_strip_i(xs_readline(stdin));
if (*i == '\0')
return 1;
@ -133,11 +133,11 @@ int initdb(const char *basedir)
printf("URL prefix:\n");
{
xs *i = xs_strip(xs_readline(stdin));
xs *i = xs_strip_i(xs_readline(stdin));
if (*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);
}
@ -215,7 +215,7 @@ int adduser(const char *uid)
if (uid == NULL) {
printf("User id:\n");
uid = xs_strip(xs_readline(stdin));
uid = xs_strip_i(xs_readline(stdin));
}
if (!validate_uid(uid)) {

View file

@ -33,7 +33,7 @@ int webfinger_request(char *qs, char **actor, char **user)
xs *s = xs_dup(qs);
if (xs_startswith(s, "@"))
s = xs_crop(s, 1, 0);
s = xs_crop_i(s, 1, 0);
l = xs_split_n(s, "@", 1);
@ -141,7 +141,7 @@ int webfinger_get_handler(d_char *req, char *q_path,
/* strip a possible leading @ */
if (xs_startswith(an, "@"))
an = xs_crop(an, 1, 0);
an = xs_crop_i(an, 1, 0);
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_startswith(const char *str, const char *prefix);
int xs_endswith(const char *str, const char *postfix);
d_char *xs_crop(d_char *str, int start, int end);
d_char *xs_strip_chars(d_char *str, const char *chars);
#define xs_strip(str) xs_strip_chars(str, " \r\n\t\v\f")
d_char *xs_tolower(d_char *str);
d_char *xs_str_prepend(d_char *str, const char *prefix);
d_char *xs_crop_i(d_char *str, int start, int end);
d_char *xs_strip_chars_i(d_char *str, const char *chars);
#define xs_strip_i(str) xs_strip_chars_i(str, " \r\n\t\v\f")
d_char *xs_tolower_i(d_char *str);
d_char *xs_str_prepend_i(d_char *str, const char *prefix);
d_char *xs_list_new(void);
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))
@ -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 */
{
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 */
{
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 */
{
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 */
{
int sz = strlen(prefix);

View file

@ -21,13 +21,13 @@ static size_t _header_callback(char *buffer, size_t size,
/* get the line */
l = xs_str_new(NULL);
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 ': ' */
if (xs_str_in(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));
}

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);
/* read the first line and split it */
l1 = xs_strip(xs_readline(f));
l1 = xs_strip_i(xs_readline(f));
l2 = xs_split(l1, " ");
if (xs_list_len(l2) != 3) {
@ -214,7 +214,7 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
for (;;) {
xs *l, *p = NULL;
l = xs_strip(xs_readline(f));
l = xs_strip_i(xs_readline(f));
/* done with the header? */
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);
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);

View file

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