New function webfinger_request() (incomplete).

This commit is contained in:
default 2022-09-22 10:15:24 +02:00
parent 6e4294f8e8
commit 00a8929581
3 changed files with 47 additions and 2 deletions

View file

@ -21,4 +21,4 @@ httpd.o: httpd.c xs.h xs_io.h xs_encdec.h xs_json.h xs_socket.h \
main.o: main.c xs.h xs_encdec.h xs_json.h snac.h
snac.o: snac.c xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h \
xs_openssl.h xs_socket.h xs_httpd.h snac.h
webfinger.o: webfinger.c xs.h xs_encdec.h xs_json.h snac.h
webfinger.o: webfinger.c xs.h xs_encdec.h xs_json.h xs_curl.h snac.h

1
snac.h
View file

@ -70,5 +70,6 @@ d_char *http_signed_request(snac *snac, char *method, char *url,
void httpd(void);
void webfinger_request(char *qs, int *status, char **actor, char **user);
void webfinger_get_handler(d_char *req, char *q_path, int *status,
char **body, int *b_size, char **ctype);

View file

@ -4,9 +4,53 @@
#include "xs.h"
#include "xs_encdec.h"
#include "xs_json.h"
#include "xs_curl.h"
#include "snac.h"
void webfinger_request(char *qs, int *status, char **actor, char **user)
/* queries the webfinger for qs and fills the required fields */
{
xs *payload = NULL;
int p_size = 0;
xs *url = NULL;
xs *headers = xs_dict_new();
if (xs_startswith(qs, "https:/" "/")) {
/* actor query: pick the host */
xs *s = xs_replace(qs, "https:/" "/", "");
xs *l = xs_split_n(s, "/", 1);
url = xs_fmt("https:/" "/%s/.well-known/webfinger?resource=%s",
xs_list_get(l, 0), qs);
}
else {
/* it's a user */
xs *s = xs_dup(qs);
xs *l;
if (xs_startswith(s, "@"))
s = xs_crop(s, 1, 0);
l = xs_split_n(s, "@", 1);
if (xs_list_len(l) == 2) {
url = xs_fmt("https:/" "/%s/.well-known/webfinger?resource:acct:%s",
xs_list_get(l, 1), qs);
}
}
if (url == NULL) {
*status = 400;
return;
}
headers = xs_dict_append(headers, "accept", "application/json");
xs_http_request("GET", url, headers, NULL, 0, status, &payload, &p_size);
}
void webfinger_get_handler(d_char *req, char *q_path, int *status,
char **body, int *b_size, char **ctype)
/* serves webfinger queries */