2020-09-23 12:40:28 -04:00
|
|
|
#ifndef GMNISRV_SERVER
|
|
|
|
#define GMNISRV_SERVER
|
2020-09-26 14:45:17 -04:00
|
|
|
#include <assert.h>
|
2020-09-24 19:58:34 -04:00
|
|
|
#include <openssl/ssl.h>
|
2020-09-23 12:40:28 -04:00
|
|
|
#include <poll.h>
|
2020-09-26 13:39:37 -04:00
|
|
|
#include <time.h>
|
2020-09-23 14:19:28 -04:00
|
|
|
#include <stdbool.h>
|
2020-09-26 13:39:37 -04:00
|
|
|
#include "gemini.h"
|
|
|
|
#include "url.h"
|
2020-09-23 12:40:28 -04:00
|
|
|
|
2020-09-26 13:39:37 -04:00
|
|
|
struct gmnisrv_server;
|
|
|
|
|
|
|
|
enum response_state {
|
|
|
|
RESPOND_HEADER,
|
|
|
|
RESPOND_BODY,
|
|
|
|
};
|
2020-09-23 12:40:28 -04:00
|
|
|
|
|
|
|
struct gmnisrv_client {
|
2020-09-26 13:39:37 -04:00
|
|
|
struct gmnisrv_server *server;
|
|
|
|
struct timespec ctime;
|
2020-09-23 14:19:28 -04:00
|
|
|
struct sockaddr addr;
|
|
|
|
socklen_t addrlen;
|
2020-09-24 19:58:34 -04:00
|
|
|
int sockfd;
|
2020-09-26 13:39:37 -04:00
|
|
|
struct pollfd *pollfd;
|
2020-09-23 14:19:28 -04:00
|
|
|
|
2020-09-24 19:58:34 -04:00
|
|
|
SSL *ssl;
|
2020-09-26 13:39:37 -04:00
|
|
|
BIO *bio, *sbio;
|
2020-09-23 14:19:28 -04:00
|
|
|
|
2020-09-26 15:00:40 -04:00
|
|
|
char buf[4096];
|
|
|
|
static_assert(GEMINI_MAX_URL + 3 < 4096);
|
2020-09-26 13:39:37 -04:00
|
|
|
size_t bufix, bufln;
|
2020-09-24 19:58:34 -04:00
|
|
|
|
2020-09-26 13:39:37 -04:00
|
|
|
enum response_state state;
|
|
|
|
enum gemini_status status;
|
|
|
|
char *meta;
|
2020-09-26 15:51:28 -04:00
|
|
|
FILE *body;
|
2020-09-26 14:41:17 -04:00
|
|
|
size_t bbytes;
|
2020-09-26 13:39:37 -04:00
|
|
|
|
2020-09-24 19:58:34 -04:00
|
|
|
struct gmnisrv_host *host;
|
2020-09-26 13:39:37 -04:00
|
|
|
char *path;
|
2020-09-23 12:40:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct gmisrv_config;
|
|
|
|
|
|
|
|
struct gmnisrv_server {
|
|
|
|
struct gmnisrv_config *conf;
|
|
|
|
struct pollfd *fds;
|
|
|
|
nfds_t nfds, fdsz;
|
|
|
|
|
2020-09-23 14:19:28 -04:00
|
|
|
// nlisten is initialized once and does not change. The fds list starts
|
|
|
|
// with this many listening sockets, then has sockets for each active
|
|
|
|
// client, up to nfds.
|
2020-09-23 12:40:28 -04:00
|
|
|
size_t nlisten;
|
|
|
|
|
|
|
|
struct gmnisrv_client *clients;
|
|
|
|
size_t nclients, clientsz;
|
2020-09-23 14:19:28 -04:00
|
|
|
|
|
|
|
bool run;
|
2020-09-23 12:40:28 -04:00
|
|
|
};
|
|
|
|
|
2020-09-26 15:08:25 -04:00
|
|
|
// server.c
|
2020-09-23 12:40:28 -04:00
|
|
|
int server_init(struct gmnisrv_server *server, struct gmnisrv_config *conf);
|
|
|
|
void server_run(struct gmnisrv_server *server);
|
|
|
|
void server_finish(struct gmnisrv_server *server);
|
|
|
|
|
2020-09-26 15:08:25 -04:00
|
|
|
void disconnect_client(struct gmnisrv_server *server,
|
|
|
|
struct gmnisrv_client *client);
|
|
|
|
|
|
|
|
// serve.c
|
|
|
|
void serve_request(struct gmnisrv_client *client);
|
|
|
|
bool request_validate(struct gmnisrv_client *client, char **path);
|
|
|
|
void client_submit_response(struct gmnisrv_client *client,
|
2020-09-26 15:51:28 -04:00
|
|
|
enum gemini_status status, const char *meta, FILE *body);
|
2020-09-26 15:08:25 -04:00
|
|
|
void client_oom(struct gmnisrv_client *client);
|
|
|
|
|
2020-09-23 12:40:28 -04:00
|
|
|
#endif
|