deal with too small/long requests

This commit is contained in:
prx 2021-02-25 18:37:48 +01:00
parent 458592594e
commit efa1f639fc
1 changed files with 15 additions and 6 deletions

21
main.c
View File

@ -20,7 +20,7 @@
#include "utils.h" #include "utils.h"
#define GEMINI_PART 9 #define GEMINI_PART 9
#define GEMINI_REQUEST_MAX 1024 /* see https://gemini.circumlunar.space/docs/specification.html */ #define GEMINI_REQUEST_MAX 1024 /* https://gemini.circumlunar.space/docs/specification.html */
@ -358,19 +358,27 @@ main(int argc, char **argv)
*/ */
drop_privileges(user, chroot_dir); drop_privileges(user, chroot_dir);
size_t reqlen = 0;
/* /*
* read 1024 chars from stdin * read 1024 chars from stdin
* to get the request * to get the request
*/ */
if (fgets(request, GEMINI_REQUEST_MAX, stdin) == NULL) { reqlen = fread(request, 1, GEMINI_REQUEST_MAX, stdin);
status(59, "request is too long (1024 max)");
errlog("request is too long (1024 max): %s", request); /* now should end of file, or request is too long */
if (feof(stdin) == 0) {
status(59, "request too long");
errlog("request too long");
} }
/* remove \r\n at the end of string */ /* remove \r\n at the end of string */
pos = strchr(request, '\r'); pos = strstr(request, "\r\n");
if (pos != NULL) if (pos != NULL) {
*pos = '\0'; *pos = '\0';
} else {
status(59, "malformed request, must end with \r\n");
errlog("malformed request, must end with \r\n");
}
/* /*
* check if the beginning of the request starts with * check if the beginning of the request starts with
@ -378,6 +386,7 @@ main(int argc, char **argv)
*/ */
if (strncmp(request, "gemini://", GEMINI_PART) != 0) { if (strncmp(request, "gemini://", GEMINI_PART) != 0) {
/* error code url malformed */ /* error code url malformed */
status(59, "request malformed : must start with gemini://");
errlog("request «%s» doesn't match gemini://", errlog("request «%s» doesn't match gemini://",
request); request);
} }