ksgi

Форк
0
/
test-debug-read.c 
139 строк · 2.8 Кб
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 <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

34
static char log[32] = "/tmp/test-debug-read.XXXXXXXXXX";
35

36
static int
37
parent(CURL *curl)
38
{
39
	const char	*data = "foo=ba\t\rr";
40

41
	curl_easy_setopt(curl, CURLOPT_URL, 
42
		"http://localhost:17123/");
43
	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
44
	return curl_easy_perform(curl) == CURLE_OK;
45
}
46

47
static int
48
child(void)
49
{
50
	struct kreq	 r;
51
	const char 	*page = "index";
52
	enum kcgi_err	 er;
53

54
	if (!kutil_openlog(log))
55
		return 0;
56

57
	er = khttp_parsex
58
		(&r, ksuffixmap, kmimetypes, KMIME__MAX, 
59
		 NULL, 0, &page, 1, KMIME_TEXT_HTML,
60
		 0, NULL, NULL, KREQ_DEBUG_READ_BODY, NULL);
61
	if (er != KCGI_OK)
62
		return 0;
63

64
	khttp_head(&r, kresps[KRESP_STATUS], 
65
		"%s", khttps[KHTTP_200]);
66
	khttp_head(&r, kresps[KRESP_CONTENT_TYPE], 
67
		"%s", kmimetypes[KMIME_TEXT_HTML]);
68
	khttp_body(&r);
69
	khttp_free(&r);
70
	return 1;
71
}
72

73
int
74
main(int argc, char *argv[])
75
{
76
	int		 fd = -1, rc = 1;
77
	FILE		*f = NULL;
78
	char		*line = NULL;
79
	const char	*cp;
80
	size_t		 linesize = 0, lineno = 0;
81
	ssize_t		 linelen;
82
	struct log_line	 log_line;
83

84
	if ((fd = mkstemp(log)) == -1)
85
		err(1, "%s", log);
86

87
	if (!regress_cgi(parent, child))
88
		goto out;
89

90
	if ((f = fdopen(fd, "r")) == NULL) {
91
		warn("%s", log);
92
		goto out;
93
	}
94

95
	for (;;) {
96
		if ((linelen = getline(&line, &linesize, f)) == -1)
97
			break;
98
		if (!log_line_parse(line, &log_line))
99
			goto out;
100
		if (strcmp(log_line.level, "INFO"))
101
			continue;
102

103
		/* Extract after pid-rx. */
104

105
		if ((cp = strchr(log_line.umsg, ':')) == NULL)
106
			goto out;
107
		cp++;
108
		while (*cp == ' ')
109
			cp++;
110

111
		/* Look line-by-line. */
112

113
		switch (lineno++) {
114
		case 0:
115
			if (strcmp(cp, "foo=ba\\t\\rr\n"))
116
				goto out;
117
			break;
118
		case 1:
119
			/* Ignore. */
120
			break;
121
		default:
122
			goto out;
123
		}
124
	}
125

126
	if (ferror(f) || lineno != 2)
127
		goto out;
128

129
	rc = 0;
130
out:
131
	if (f != NULL)
132
		fclose(f);
133
	else if (fd != -1)
134
		close(fd);
135

136
	free(line);
137
	unlink(log);
138
	return rc;
139
}
140

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

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

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

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