improving style: no functional changes

This commit is contained in:
Omar Polo 2020-10-03 18:25:31 +02:00
parent fab952e1d7
commit 4d4f0e19ac
No known key found for this signature in database
GPG Key ID: 35F98C96A1786F0D

49
gmid.c
View File

@ -35,7 +35,7 @@
#define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */ #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
/* large enough to hold a copy of a gemini URL and still have room */ /* large enough to hold a copy of a gemini URL and still have extra room */
#define PATHBUF (2048) #define PATHBUF (2048)
#define FILEBUF 1024 #define FILEBUF 1024
@ -93,7 +93,7 @@ url_trim(char *url)
s[1] = '\0'; s[1] = '\0';
if (s[2] != '\0') { if (s[2] != '\0') {
fprintf(stderr, "last byte of request isn't NULL\n"); fprintf(stderr, "the request was longer than 1024 bytes\n");
return 0; return 0;
} }
@ -131,18 +131,9 @@ adjust_path(char *path)
int int
path_isdir(char *path) path_isdir(char *path)
{ {
size_t len;
if (*path == '\0') if (*path == '\0')
return 1; return 1;
return path[strlen(path)-1] == '/';
len = strlen(path);
/* len >= 1 */
if (path[len-1] == '/')
return 1;
return 0;
} }
void void
@ -163,7 +154,7 @@ isdir(int fd)
if (fstat(fd, &sb) == -1) { if (fstat(fd, &sb) == -1) {
warn("fstat"); warn("fstat");
return 1; /* we'll fail later on anyway */ return 1; /* we'll probably fail later on anyway */
} }
return S_ISDIR(sb.st_mode); return S_ISDIR(sb.st_mode);
@ -183,7 +174,7 @@ send_file(char *path, struct tls *ctx)
bzero(fpath, sizeof(fpath)); bzero(fpath, sizeof(fpath));
if (*path != '.') if (*path != '.')
strlcpy(fpath, ".", PATHBUF); fpath[0] = '.';
strlcat(fpath, path, PATHBUF); strlcat(fpath, path, PATHBUF);
@ -266,8 +257,10 @@ handle(char *url, struct tls *ctx)
return; return;
} }
if ((path = url_start_of_request(url)) == NULL) if ((path = url_start_of_request(url)) == NULL) {
start_reply(ctx, BAD_REQUEST, "bad request");
return; return;
}
adjust_path(path); adjust_path(path);
@ -318,7 +311,6 @@ loop(struct tls *ctx, int sock)
socklen_t len; socklen_t len;
struct tls *clientctx; struct tls *clientctx;
char buf[GEMINI_URL_LEN]; char buf[GEMINI_URL_LEN];
ssize_t r;
for (;;) { for (;;) {
len = sizeof(client); len = sizeof(client);
@ -331,14 +323,14 @@ loop(struct tls *ctx, int sock)
} }
bzero(buf, GEMINI_URL_LEN); bzero(buf, GEMINI_URL_LEN);
if ((r = tls_read(clientctx, buf, sizeof(buf)-1)) == -1) { if (tls_read(clientctx, buf, sizeof(buf)-1) == -1) {
warnx("tls_read: %s", tls_error(clientctx)); warnx("tls_read: %s", tls_error(clientctx));
goto clienterr; goto clientend;
} }
handle(buf, clientctx); handle(buf, clientctx);
clienterr: clientend:
if (tls_close(clientctx) == -1) if (tls_close(clientctx) == -1)
warn("tls_close: client"); warn("tls_close: client");
tls_free(clientctx); tls_free(clientctx);
@ -360,8 +352,6 @@ main(int argc, char **argv)
const char *cert = "cert.pem", *key = "key.pem", *dir = "docs"; const char *cert = "cert.pem", *key = "key.pem", *dir = "docs";
struct tls *ctx = NULL; struct tls *ctx = NULL;
struct tls_config *conf; struct tls_config *conf;
void *m;
size_t mlen;
int sock, ch; int sock, ch;
while ((ch = getopt(argc, argv, "c:d:hk:")) != -1) { while ((ch = getopt(argc, argv, "c:d:hk:")) != -1) {
@ -395,15 +385,11 @@ main(int argc, char **argv)
TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1) TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
err(1, "tls_config_set_protocols"); err(1, "tls_config_set_protocols");
if ((m = tls_load_file(cert, &mlen, NULL)) == NULL) if (tls_config_set_cert_file(conf, cert) == -1)
err(1, "tls_load_file: %s", cert); err(1, "tls_config_set_cert_file: %s", cert);
if (tls_config_set_cert_mem(conf, m, mlen) == -1)
err(1, "tls_config_set_cert_mem: server certificate");
if ((m = tls_load_file(key, &mlen, NULL)) == NULL) if (tls_config_set_key_file(conf, key) == -1)
err(1, "tls_load_file: %s", key); err(1, "tls_config_set_key_file: %s", key);
if (tls_config_set_key_mem(conf, m, mlen) == -1)
err(1, "tls_config_set_cert_mem: server key");
if ((ctx = tls_server()) == NULL) if ((ctx = tls_server()) == NULL)
err(1, "tls_server"); err(1, "tls_server");
@ -424,11 +410,6 @@ main(int argc, char **argv)
loop(ctx, sock); loop(ctx, sock);
if (1) {
printf("why im I here?\n");
abort();
}
close(sock); close(sock);
tls_free(ctx); tls_free(ctx);
tls_config_free(conf); tls_config_free(conf);