ksgi

Форк
0
/
test-html-simple.c 
141 строка · 3.3 Кб
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_ERR
20
# include <err.h>
21
#endif
22
#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&#x2014;5 minutes til midnight!&#x1f601;" \
37
	"</body></html>"
38

39
static size_t
40
bufcb(void *contents, size_t sz, size_t nm, void *dat)
41
{
42
	struct kcgi_buf	*buf = dat;
43

44
	if (kcgi_buf_write(contents, nm * sz, buf) != KCGI_OK)
45
		return 0;
46
	return nm * sz;
47
}
48

49
static int
50
parent(CURL *curl)
51
{
52
	struct kcgi_buf	 buf;
53
	int		 rc;
54

55
	memset(&buf, 0, sizeof(struct kcgi_buf));
56

57
	curl_easy_setopt(curl, CURLOPT_URL, 
58
		"http://localhost:17123/index.html");
59
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bufcb);
60
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
61
	if (curl_easy_perform(curl) != CURLE_OK) {
62
		warnx("curl_easy_perform");
63
		return 0;
64
	}
65
	if (!(rc = strcmp(buf.buf, EXPECT) == 0))
66
		warnx("content test failure: %s", buf.buf);
67
	free(buf.buf);
68
	return rc;
69
}
70

71
static int
72
child(void)
73
{
74
	struct kreq	 r;
75
	struct khtmlreq	 req;
76
	const char 	*page[] = { "index" };
77
	int		 rc = 0;
78

79
	if (khttp_parse(&r, NULL, 0, page, 1, 0) != KCGI_OK) {
80
		warnx("khttp_parse");
81
		return 0;
82
	} else if (r.page != 0 || r.mime != KMIME_TEXT_HTML)
83
		goto out;
84

85
	/* Setup. */
86

87
	if (khttp_head(&r, kresps[KRESP_STATUS],
88
	    "%s", khttps[KHTTP_200]) != KCGI_OK) {
89
		warnx("khttp_head");
90
		goto out;
91
	} else if (khttp_head(&r, kresps[KRESP_CONTENT_TYPE],
92
	    "%s", kmimetypes[r.mime]) != KCGI_OK) {
93
		warnx("khttp_head");
94
		goto out;
95
	} else if (khttp_body(&r) != KCGI_OK) {
96
		warnx("khttp_body");
97
		goto out;
98
	}
99

100
	if (khtml_open(&req, &r, 0) != KCGI_OK) {
101
		warnx("khtml_open");
102
		goto out;
103
	}
104

105
	kcgi_writer_disable(&r);
106
	
107
	if (khtml_elem(&req, KELEM_DOCTYPE) != KCGI_OK ||
108
	    khtml_elem(&req, KELEM_HTML) != KCGI_OK ||
109
	    khtml_elem(&req, KELEM_BODY) != KCGI_OK) {
110
		warnx("khtml_elem");
111
		goto out;
112
	}
113

114
	if (khtml_puts(&req, "Hi") != KCGI_OK ||
115
	    khtml_entity(&req, KENTITY_mdash) != KCGI_OK ||
116
	    khtml_int(&req, 5) != KCGI_OK ||
117
	    khtml_write(" minutes xxx", 9, &req) != KCGI_OK ||
118
	    khtml_printf(&req, "til %s!", "midnight") != KCGI_OK ||
119
	    khtml_ncr(&req, 0x1F601) != KCGI_OK) {
120
		warnx("khtml_puts");
121
		goto out;
122
	}
123
	
124
	if (khtml_close(&req) != KCGI_OK) {
125
		warnx("khtml_close");
126
		goto out;
127
	}
128

129
	rc = 1;
130
out:
131
	khttp_free(&r);
132
	return rc;
133
}
134

135
int
136
main(int argc, char *argv[])
137
{
138

139
	return regress_cgi(parent, child) ? 
140
		EXIT_SUCCESS : EXIT_FAILURE;
141
}
142

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.