fix tests, initialize all, remove useless var
This commit is contained in:
parent
f9dc956824
commit
d5cf84928e
1 changed files with 25 additions and 41 deletions
66
main.c
66
main.c
|
@ -130,9 +130,9 @@ status(const int code, const char *file_mime, const char *lang)
|
||||||
void
|
void
|
||||||
display_file(const char *path, const char *lang)
|
display_file(const char *path, const char *lang)
|
||||||
{
|
{
|
||||||
FILE *fd;
|
FILE *fd = NULL;
|
||||||
struct stat sb;
|
struct stat sb = {0};
|
||||||
ssize_t nread;
|
ssize_t nread = 0;
|
||||||
char *buffer[BUFSIZ];
|
char *buffer[BUFSIZ];
|
||||||
const char *file_mime;
|
const char *file_mime;
|
||||||
|
|
||||||
|
@ -155,7 +155,7 @@ display_file(const char *path, const char *lang)
|
||||||
/* read the file and write it to stdout */
|
/* read the file and write it to stdout */
|
||||||
while ((nread = fread(buffer, sizeof(char), sizeof(buffer), fd)) != 0)
|
while ((nread = fread(buffer, sizeof(char), sizeof(buffer), fd)) != 0)
|
||||||
fwrite(buffer, sizeof(char), nread, stdout);
|
fwrite(buffer, sizeof(char), nread, stdout);
|
||||||
fclose(fd);
|
goto closefd;
|
||||||
syslog(LOG_DAEMON, "path served %s", path);
|
syslog(LOG_DAEMON, "path served %s", path);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -163,6 +163,10 @@ err:
|
||||||
/* return an error code and no content */
|
/* return an error code and no content */
|
||||||
status(40, "text/gemini", lang);
|
status(40, "text/gemini", lang);
|
||||||
syslog(LOG_DAEMON, "path invalid %s", path);
|
syslog(LOG_DAEMON, "path invalid %s", path);
|
||||||
|
goto closefd;
|
||||||
|
|
||||||
|
closefd:
|
||||||
|
fclose(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -177,7 +181,6 @@ main(int argc, char **argv)
|
||||||
int virtualhost = 0;
|
int virtualhost = 0;
|
||||||
int option = 0;
|
int option = 0;
|
||||||
int chroot = 0;
|
int chroot = 0;
|
||||||
int start_with_gemini = 0;
|
|
||||||
char *pos = NULL;
|
char *pos = NULL;
|
||||||
|
|
||||||
while ((option = getopt(argc, argv, ":d:l:u:v")) != -1) {
|
while ((option = getopt(argc, argv, ":d:l:u:v")) != -1) {
|
||||||
|
@ -226,13 +229,10 @@ main(int argc, char **argv)
|
||||||
* check if the beginning of the request starts with
|
* check if the beginning of the request starts with
|
||||||
* gemini://
|
* gemini://
|
||||||
*/
|
*/
|
||||||
start_with_gemini = strncmp(request, "gemini://", GEMINI_PART);
|
if (strncmp(request, "gemini://", GEMINI_PART) != 0) {
|
||||||
|
|
||||||
/* the request must start with gemini:// */
|
|
||||||
if (start_with_gemini != 0) {
|
|
||||||
/* error code url malformed */
|
/* error code url malformed */
|
||||||
syslog(LOG_DAEMON, "request «%s» doesn't match gemini:// at index %i",
|
syslog(LOG_DAEMON, "request «%s» doesn't match gemini://",
|
||||||
request, start_with_gemini);
|
request);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
syslog(LOG_DAEMON, "request %s", request);
|
syslog(LOG_DAEMON, "request %s", request);
|
||||||
|
@ -248,43 +248,27 @@ main(int argc, char **argv)
|
||||||
|
|
||||||
if (pos != NULL) {
|
if (pos != NULL) {
|
||||||
/* if there is a / found */
|
/* if there is a / found */
|
||||||
int position = -1;
|
|
||||||
for (size_t i = 0; i < sizeof(request); i++) {
|
|
||||||
if (*pos == request[i]) {
|
|
||||||
position = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* separate hostname and uri */
|
/* separate hostname and uri */
|
||||||
if (position != -1) {
|
estrlcpy(file, pos, strlen(pos)+1);
|
||||||
/*estrlcpy(hostname, request - position + 1, sizeof(request - position + 1));*/
|
/* just keep hostname in request */
|
||||||
estrlcpy(file, request+position+1, sizeof(request+position+1));
|
pos[0] = '\0';
|
||||||
request[position +1] = '\0'; /* this is faster than strlcpy above*/
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* use a default file if no file are requested this
|
* use a default file if no file are requested this
|
||||||
* can happen in two cases gemini://hostname/
|
* can happen in two cases gemini://hostname/
|
||||||
* gemini://hostname/directory/
|
* gemini://hostname/directory/
|
||||||
*/
|
*/
|
||||||
if (strlen(file) == 0)
|
if (strlen(file) == 0)
|
||||||
estrlcpy(file, "/index.gmi", 11);
|
estrlcpy(file, "/index.gmi", 11);
|
||||||
if (file[strlen(file) - 1] == '/')
|
if (file[strlen(file) - 1] == '/')
|
||||||
estrlcat(file, "index.gmi", sizeof(file));
|
estrlcat(file, "index.gmi", sizeof(file));
|
||||||
|
|
||||||
} else {
|
|
||||||
syslog(LOG_DAEMON, "unknown situation after parsing query");
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
/*
|
/*
|
||||||
* there are no slash / in the request
|
* there are no slash / in the request
|
||||||
* -2 to remove \r\n
|
*/
|
||||||
*/
|
|
||||||
estrlcpy(hostname, request, sizeof(hostname));
|
|
||||||
estrlcpy(file, "/index.gmi", 11);
|
estrlcpy(file, "/index.gmi", 11);
|
||||||
}
|
}
|
||||||
|
estrlcpy(hostname, request, sizeof(hostname));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* if virtualhost feature is actived looking under the default path +
|
* if virtualhost feature is actived looking under the default path +
|
||||||
|
|
Loading…
Reference in a new issue