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[33] = "/tmp/test-debug-write.XXXXXXXXXX";35
36static int37parent(CURL *curl)38{
39
40curl_easy_setopt(curl, CURLOPT_URL,41"http://localhost:17123/");42return curl_easy_perform(curl) == CURLE_OK;43}
44
45static int46child(void)47{
48struct kreq r;49const char *page = "index";50enum kcgi_err er;51
52if (!kutil_openlog(log))53return 0;54
55er = khttp_parsex56(&r, ksuffixmap, kmimetypes, KMIME__MAX,57NULL, 0, &page, 1, KMIME_TEXT_HTML,580, NULL, NULL, KREQ_DEBUG_WRITE, NULL);59if (er != KCGI_OK)60return 0;61
62khttp_head(&r, kresps[KRESP_STATUS],63"%s", khttps[KHTTP_200]);64khttp_head(&r, kresps[KRESP_CONTENT_TYPE],65"%s", kmimetypes[KMIME_TEXT_HTML]);66khttp_body(&r);67khttp_puts(&r, "hello, world\n");68khttp_free(&r);69return 1;70}
71
72int
73main(int argc, char *argv[])74{
75int fd = -1, rc = 1;76FILE *f = NULL;77char *line = NULL;78const char *cp;79size_t linesize = 0, lineno = 0;80ssize_t linelen;81struct log_line log_line;82
83if ((fd = mkstemp(log)) == -1)84err(1, "%s", log);85
86if (!regress_cgi(parent, child))87goto out;88
89if ((f = fdopen(fd, "r")) == NULL) {90warn("%s", log);91goto out;92}93
94for (;;) {95if ((linelen = getline(&line, &linesize, f)) == -1)96break;97if (!log_line_parse(line, &log_line))98goto out;99if (strcmp(log_line.level, "INFO"))100continue;101
102/* Extract after pid-tx. */103
104if ((cp = strchr(log_line.umsg, ':')) == NULL)105goto out;106cp++;107while (*cp == ' ')108cp++;109
110/* Examine line-by-line. */111
112switch (lineno++) {113case 0:114if (strcmp(cp, "Status: 200 OK\\r\n"))115goto out;116break;117case 1:118if (strcmp(cp, "Content-Type: text/html\\r\n"))119goto out;120break;121case 2:122if (strcmp(cp, "\\r\n"))123goto out;124break;125case 3:126if (strcmp(cp, "hello, world\n"))127goto out;128break;129case 4:130/* Ignore. */131break;132default:133goto out;134}135}136
137if (ferror(f) || lineno != 5)138goto out;139
140rc = 0;141out:142if (f != NULL)143fclose(f);144else if (fd != -1)145close(fd);146
147free(line);148unlink(log);149return rc;150}
151