ksgi

Форк
0
/
test-debug-write.c 
150 строк · 3.0 Кб
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[33] = "/tmp/test-debug-write.XXXXXXXXXX";
35

36
static int
37
parent(CURL *curl)
38
{
39

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

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

52
	if (!kutil_openlog(log))
53
		return 0;
54

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

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

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

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

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

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

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

102
		/* Extract after pid-tx. */
103

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

110
		/* Examine line-by-line. */
111

112
		switch (lineno++) {
113
		case 0:
114
			if (strcmp(cp, "Status: 200 OK\\r\n"))
115
				goto out;
116
			break;
117
		case 1:
118
			if (strcmp(cp, "Content-Type: text/html\\r\n"))
119
				goto out;
120
			break;
121
		case 2:
122
			if (strcmp(cp, "\\r\n"))
123
				goto out;
124
			break;
125
		case 3:
126
			if (strcmp(cp, "hello, world\n"))
127
				goto out;
128
			break;
129
		case 4:
130
			/* Ignore. */
131
			break;
132
		default:
133
			goto out;
134
		}
135
	}
136

137
	if (ferror(f) || lineno != 5)
138
		goto out;
139

140
	rc = 0;
141
out:
142
	if (f != NULL)
143
		fclose(f);
144
	else if (fd != -1)
145
		close(fd);
146

147
	free(line);
148
	unlink(log);
149
	return rc;
150
}
151

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

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

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

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