ensure errors msg are followed by \n, specify in a define the number of matches we need, remove bad structure init

This commit is contained in:
prx 2022-08-17 22:08:16 +02:00
parent be0f86df8d
commit 5063f3e95b
2 changed files with 8 additions and 17 deletions

22
vger.c
View File

@ -43,6 +43,7 @@ stop(const int r, const char *fmt, ...)
va_start(ap2, fmt);
vfprintf(stderr, fmt, ap2);
va_end(ap2);
fprintf(stderr, "\n");
}
exit(r);
@ -344,8 +345,8 @@ check_request(char *request)
*/
char *pos = NULL;
/* read 1024 +1 chars from stdin to get the request (1024 + \0) */
if (fgets(request, GEMINI_REQUEST_MAX, stdin) == NULL) {
/* EOF reached before reading anything */
if (feof(stdin)) {
@ -368,22 +369,9 @@ check_request(char *request)
/* remove \r\n at the end of string */
request[strcspn(request, "\r\n")] = '\0';
/*
* check if the beginning of the request starts with
* gemini://
*/
if (strncmp(request, "gemini://", GEMINI_PART) != 0) {
/* error code url malformed */
status(59, "request «%s» doesn't match gemini://", request);
stop(EXIT_FAILURE, "request «%s» doesn't match gemini://", request);
}
/* save request for logs */
estrlcpy(_request, request, sizeof(_request));
/* remove the gemini:// part */
memmove(request, request + GEMINI_PART, strlen(request) + 1 - GEMINI_PART);
/* remove all "/.." for safety reasons */
while ((pos = strstr(request, "/..")) != NULL)
memmove(request, pos + 3, strlen(pos) + 1 - 3); /* "/.." = 3 */
@ -508,11 +496,11 @@ get_query(char *path, char *query, size_t querysiz)
void
split_request(const char *request, char *hostname, char *path, char *query)
{
regex_t greg = {0}; /* compiled gemini regex */
regmatch_t *match = {0}; /* matches founds */
size_t nmatch = 4; /* number of substrings to look for */
size_t nmatch = SE_MAX; /* 3 "()" + 1 for whole match */
char buf[BUFSIZ] = {'\0'}; /* to handle error messages */
int ret = 0;
regex_t greg; /* compiled gemini regex */
regmatch_t match[SE_MAX]; /* matches founds */
ret = regcomp(&greg, _gemini_regex, REG_EXTENDED);
if (ret != 0) {

3
vger.h
View File

@ -11,6 +11,9 @@
*/
#define GEMINI_REQUEST_MAX 1025
/* max subexpression in regex : 3 + 1 */
#define SE_MAX 4
/* gemini_regex:
* =============
* ^gemini://+ : in case of gemini:///