ksgi

Форк
0
/
test-html-null-strings.c 
137 строк · 3.1 Кб
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></body></html>"
36

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

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

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

53
	memset(&buf, 0, sizeof(struct kcgi_buf));
54

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

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

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

83
	/* Setup. */
84

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

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

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

112
	if (khtml_puts(&req, NULL) != KCGI_OK ||
113
	    khtml_printf(&req, NULL) != KCGI_OK ||
114
	    khtml_write(NULL, 9, &req) != KCGI_OK ||
115
	    khtml_write("abcdef", 0, &req) != KCGI_OK) {
116
		warnx("khtml_puts/write");
117
		goto out;
118
	}
119
	
120
	if (khtml_close(&req) != KCGI_OK) {
121
		warnx("khtml_close");
122
		goto out;
123
	}
124

125
	rc = 1;
126
out:
127
	khttp_free(&r);
128
	return rc;
129
}
130

131
int
132
main(int argc, char *argv[])
133
{
134

135
	return regress_cgi(parent, child) ? 
136
		EXIT_SUCCESS : EXIT_FAILURE;
137
}
138

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

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

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

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