Separate success response into its own function to deduplicate.

This commit is contained in:
Qc Na 2023-03-10 17:55:04 +01:00
parent 2afaf5510b
commit 64c69b3a38
Signed by: qcna
GPG Key ID: 3C818AEF6447D77F
2 changed files with 13 additions and 12 deletions

24
vger.c
View File

@ -66,6 +66,15 @@ status(const int code, const char *fmt, ...)
_retcode = code; /* store return code for logs */
}
void
status_success(const char *file_mime)
{
if (strcmp(file_mime, "text/gemini") == 0)
status(20, "%s; %s", file_mime, lang);
else
status(20, "%s", file_mime);
}
int
uridecode(char *uri)
{
@ -180,7 +189,6 @@ ssize_t
display_file(const char *path, struct stat sb)
{
FILE *fd = NULL;
const char *file_mime;
size_t pathlen = strlen(path);
/*
@ -222,12 +230,8 @@ display_file(const char *path, struct stat sb)
return _datasiz;
}
/* TODO: Move this to its own function. */
file_mime = get_file_mime(path, default_mime);
if (strcmp(file_mime, "text/gemini") == 0)
status(20, "%s; %s", file_mime, lang);
else
status(20, "%s", file_mime);
/* Begin response (detect MIME from path name, disregarding the fact that we are a socket.) */
status_success(get_file_mime(path, default_mime));
/* Read data until the socket is closed. */
char buffer[BUFSIZ];
@ -250,11 +254,7 @@ display_file(const char *path, struct stat sb)
/* open the file requested */
if ((fd = fopen(path, "r")) != NULL) {
file_mime = get_file_mime(path, default_mime);
if (strcmp(file_mime, "text/gemini") == 0)
status(20, "%s; %s", file_mime, lang);
else
status(20, "%s", file_mime);
status_success(get_file_mime(path, default_mime));
_datasiz += print_file(fd);
fclose(fd); /* close file descriptor */

1
vger.h
View File

@ -50,6 +50,7 @@ void set_rootdir(const char *, const char *, const char *);
void remove_double_dot(char *);
void split_request(const char *, char *, char *, char *);
void status(const int, const char *, ...);
void status_success(const char *);
void stop(const int, const char *, ...);
int uridecode (char *);