ksgi
1/* $Id$ */
2/*
3* Copyright (c) 2018 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#include <stdarg.h>20#include <stdint.h>21#include <stdlib.h>22#include <string.h>23#include <unistd.h>24
25#include <curl/curl.h>26
27#include "../kcgi.h"28#include "../kcgijson.h"29#include "regress.h"30
31static size_t32bufcb(void *contents, size_t sz, size_t nm, void *dat)33{
34struct kcgi_buf *buf = dat;35
36if (KCGI_OK != kcgi_buf_write(contents, nm * sz, buf))37return 0;38return nm * sz;39}
40
41static int42parent(CURL *curl)43{
44struct kcgi_buf buf;45int rc;46
47memset(&buf, 0, sizeof(struct kcgi_buf));48
49curl_easy_setopt(curl, CURLOPT_URL,50"http://localhost:17123/index.json");51curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bufcb);52curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);53if (CURLE_OK != curl_easy_perform(curl))54return 0;55
56rc = 0 == strcmp(buf.buf, "{\"für\": \"für\"}");57free(buf.buf);58return rc;59}
60
61static int62child(void)63{
64struct kreq r;65struct kjsonreq req;66const char *page[] = { "index" };67int rc = 0;68
69if (KCGI_OK != khttp_parse(&r, NULL, 0, page, 1, 0))70return(0);71if (r.page)72goto out;73if (KMIME_APP_JSON != r.mime)74goto out;75
76rc = 1;77khttp_head(&r, kresps[KRESP_STATUS],78"%s", khttps[KHTTP_200]);79khttp_head(&r, kresps[KRESP_CONTENT_TYPE],80"%s", kmimetypes[r.mime]);81khttp_body(&r);82
83kjson_open(&req, &r);84kjson_obj_open(&req);85kjson_putstringp(&req, "für", "für");86kjson_obj_close(&req);87kjson_close(&req);88out:89khttp_free(&r);90return(rc);91}
92
93int
94main(int argc, char *argv[])95{
96
97return(regress_cgi(parent, child) ?98EXIT_SUCCESS : EXIT_FAILURE);99}
100