ksgi
1/* $Id$ */
2/*
3* Copyright (c) 2020 Kristaps Dzonsons <kristaps@bsd.lv>
4*
5* Permission to use, copy, modify, and distribute this software for any
6* purpose with or without fee is hereby granted, provided that the above
7* copyright notice and this permission notice appear in all copies.
8*
9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*/
17#include "../config.h"18
19#if HAVE_ERR20# include <err.h>21#endif22#include <stdarg.h>23#include <stdint.h>24#include <stdio.h>25#include <stdlib.h>26#include <string.h>27#include <unistd.h>28
29#include <curl/curl.h>30
31#include "../kcgi.h"32#include "regress.h"33
34static char log[32] = "/tmp/test-debug-read.XXXXXXXXXX";35
36static int37parent(CURL *curl)38{
39const char *data = "foo=ba\t\rr";40
41curl_easy_setopt(curl, CURLOPT_URL,42"http://localhost:17123/");43curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);44return curl_easy_perform(curl) == CURLE_OK;45}
46
47static int48child(void)49{
50struct kreq r;51const char *page = "index";52enum kcgi_err er;53
54if (!kutil_openlog(log))55return 0;56
57er = khttp_parsex58(&r, ksuffixmap, kmimetypes, KMIME__MAX,59NULL, 0, &page, 1, KMIME_TEXT_HTML,600, NULL, NULL, KREQ_DEBUG_READ_BODY, NULL);61if (er != KCGI_OK)62return 0;63
64khttp_head(&r, kresps[KRESP_STATUS],65"%s", khttps[KHTTP_200]);66khttp_head(&r, kresps[KRESP_CONTENT_TYPE],67"%s", kmimetypes[KMIME_TEXT_HTML]);68khttp_body(&r);69khttp_free(&r);70return 1;71}
72
73int
74main(int argc, char *argv[])75{
76int fd = -1, rc = 1;77FILE *f = NULL;78char *line = NULL;79const char *cp;80size_t linesize = 0, lineno = 0;81ssize_t linelen;82struct log_line log_line;83
84if ((fd = mkstemp(log)) == -1)85err(1, "%s", log);86
87if (!regress_cgi(parent, child))88goto out;89
90if ((f = fdopen(fd, "r")) == NULL) {91warn("%s", log);92goto out;93}94
95for (;;) {96if ((linelen = getline(&line, &linesize, f)) == -1)97break;98if (!log_line_parse(line, &log_line))99goto out;100if (strcmp(log_line.level, "INFO"))101continue;102
103/* Extract after pid-rx. */104
105if ((cp = strchr(log_line.umsg, ':')) == NULL)106goto out;107cp++;108while (*cp == ' ')109cp++;110
111/* Look line-by-line. */112
113switch (lineno++) {114case 0:115if (strcmp(cp, "foo=ba\\t\\rr\n"))116goto out;117break;118case 1:119/* Ignore. */120break;121default:122goto out;123}124}125
126if (ferror(f) || lineno != 2)127goto out;128
129rc = 0;130out:131if (f != NULL)132fclose(f);133else if (fd != -1)134close(fd);135
136free(line);137unlink(log);138return rc;139}
140