2022-09-21 16:09:16 +00:00
|
|
|
/* snac - A simple, minimalistic ActivityPub instance */
|
|
|
|
/* copyright (c) 2022 grunfink - MIT license */
|
|
|
|
|
|
|
|
#include "xs.h"
|
|
|
|
#include "xs_encdec.h"
|
|
|
|
#include "xs_json.h"
|
|
|
|
#include "xs_socket.h"
|
|
|
|
#include "xs_httpd.h"
|
|
|
|
|
|
|
|
#include "snac.h"
|
|
|
|
|
|
|
|
|
|
|
|
void httpd_connection(int rs)
|
|
|
|
/* the connection loop */
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
xs *req;
|
|
|
|
|
|
|
|
f = xs_socket_accept(rs);
|
|
|
|
|
|
|
|
req = xs_httpd_request(f);
|
|
|
|
|
2022-09-21 16:27:30 +00:00
|
|
|
{
|
|
|
|
xs *j = xs_json_dumps_pp(req, 4);
|
|
|
|
printf("%s\n", j);
|
|
|
|
}
|
|
|
|
|
2022-09-21 16:09:16 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void httpd(void)
|
|
|
|
/* starts the server */
|
|
|
|
{
|
|
|
|
char *address;
|
|
|
|
int port;
|
|
|
|
int rs;
|
|
|
|
|
|
|
|
address = xs_dict_get(srv_config, "address");
|
|
|
|
port = xs_number_get(xs_dict_get(srv_config, "port"));
|
|
|
|
|
|
|
|
if ((rs = xs_socket_server(address, port)) == -1) {
|
|
|
|
srv_log(xs_fmt("cannot bind socket to %s:%d", address, port));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-21 16:13:11 +00:00
|
|
|
srv_running = 1;
|
|
|
|
|
2022-09-21 16:09:16 +00:00
|
|
|
srv_log(xs_fmt("httpd start %s:%d", address, port));
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
httpd_connection(rs);
|
|
|
|
}
|
|
|
|
|
|
|
|
srv_log(xs_fmt("httpd stop %s:%d", address, port));
|
|
|
|
}
|