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 <stdlib.h>25#include <string.h>26#include <unistd.h>27
28#include <curl/curl.h>29
30#include "../kcgi.h"31#include "../kcgihtml.h"32#include "regress.h"33
34#define EXPECT \35"<!DOCTYPE html><html><body>" \36"Hi—5 minutes til midnight!😁" \37"</body></html>"38
39static size_t40bufcb(void *contents, size_t sz, size_t nm, void *dat)41{
42struct kcgi_buf *buf = dat;43
44if (kcgi_buf_write(contents, nm * sz, buf) != KCGI_OK)45return 0;46return nm * sz;47}
48
49static int50parent(CURL *curl)51{
52struct kcgi_buf buf;53int rc;54
55memset(&buf, 0, sizeof(struct kcgi_buf));56
57curl_easy_setopt(curl, CURLOPT_URL,58"http://localhost:17123/index.html");59curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bufcb);60curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);61if (curl_easy_perform(curl) != CURLE_OK) {62warnx("curl_easy_perform");63return 0;64}65if (!(rc = strcmp(buf.buf, EXPECT) == 0))66warnx("content test failure: %s", buf.buf);67free(buf.buf);68return rc;69}
70
71static int72child(void)73{
74struct kreq r;75struct khtmlreq req;76const char *page[] = { "index" };77int rc = 0;78
79if (khttp_parse(&r, NULL, 0, page, 1, 0) != KCGI_OK) {80warnx("khttp_parse");81return 0;82} else if (r.page != 0 || r.mime != KMIME_TEXT_HTML)83goto out;84
85/* Setup. */86
87if (khttp_head(&r, kresps[KRESP_STATUS],88"%s", khttps[KHTTP_200]) != KCGI_OK) {89warnx("khttp_head");90goto out;91} else if (khttp_head(&r, kresps[KRESP_CONTENT_TYPE],92"%s", kmimetypes[r.mime]) != KCGI_OK) {93warnx("khttp_head");94goto out;95} else if (khttp_body(&r) != KCGI_OK) {96warnx("khttp_body");97goto out;98}99
100if (khtml_open(&req, &r, 0) != KCGI_OK) {101warnx("khtml_open");102goto out;103}104
105kcgi_writer_disable(&r);106
107if (khtml_elem(&req, KELEM_DOCTYPE) != KCGI_OK ||108khtml_elem(&req, KELEM_HTML) != KCGI_OK ||109khtml_elem(&req, KELEM_BODY) != KCGI_OK) {110warnx("khtml_elem");111goto out;112}113
114if (khtml_puts(&req, "Hi") != KCGI_OK ||115khtml_entity(&req, KENTITY_mdash) != KCGI_OK ||116khtml_int(&req, 5) != KCGI_OK ||117khtml_write(" minutes xxx", 9, &req) != KCGI_OK ||118khtml_printf(&req, "til %s!", "midnight") != KCGI_OK ||119khtml_ncr(&req, 0x1F601) != KCGI_OK) {120warnx("khtml_puts");121goto out;122}123
124if (khtml_close(&req) != KCGI_OK) {125warnx("khtml_close");126goto out;127}128
129rc = 1;130out:131khttp_free(&r);132return rc;133}
134
135int
136main(int argc, char *argv[])137{
138
139return regress_cgi(parent, child) ?140EXIT_SUCCESS : EXIT_FAILURE;141}
142