1 /*	$Id$ */
2 /*
3  * Copyright (c) 2015, 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 <getopt.h>
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <string.h>
22 
23 #include "kcgi.h"
24 
25 int
main(void)26 main(void)
27 {
28 	struct kreq	 req;
29 	struct kfcgi	*fcgi;
30 	enum kcgi_err	 er;
31 
32 	if (KCGI_OK != khttp_fcgi_init(&fcgi, NULL, 0, NULL, 0, 0))
33 		return 0;
34 
35 	for (;;) {
36 		er = khttp_fcgi_parse(fcgi, &req);
37 		if (KCGI_EXIT == er) {
38 			fprintf(stderr, "khttp_fcgi_parse: terminate\n");
39 			break;
40 		} else if (KCGI_OK != er) {
41 			fprintf(stderr, "khttp_fcgi_parse: error: %d\n", er);
42 			break;
43 		}
44 
45 		er = khttp_head(&req, kresps[KRESP_STATUS],
46 			"%s", khttps[KHTTP_200]);
47 		if (KCGI_HUP == er) {
48 			fprintf(stderr, "khttp_head: interrupt\n");
49 			khttp_free(&req);
50 			continue;
51 		} else if (KCGI_OK != er) {
52 			fprintf(stderr, "khttp_head: error: %d\n", er);
53 			khttp_free(&req);
54 			break;
55 		}
56 
57 		er = khttp_head(&req, kresps[KRESP_CONTENT_TYPE],
58 			"%s", kmimetypes[req.mime]);
59 		if (KCGI_HUP == er) {
60 			fprintf(stderr, "khttp_head: interrupt\n");
61 			khttp_free(&req);
62 			continue;
63 		} else if (KCGI_OK != er) {
64 			fprintf(stderr, "khttp_head: error: %d\n", er);
65 			khttp_free(&req);
66 			break;
67 		}
68 
69 		er = khttp_body(&req);
70 		if (KCGI_HUP == er) {
71 			fprintf(stderr, "khttp_body: interrupt\n");
72 			khttp_free(&req);
73 			continue;
74 		} else if (KCGI_OK != er) {
75 			fprintf(stderr, "khttp_body: error: %d\n", er);
76 			khttp_free(&req);
77 			break;
78 		}
79 
80 		er = khttp_puts(&req, "Hello, world!\n");
81 		if (KCGI_HUP == er) {
82 			fprintf(stderr, "khttp_puts: interrupt\n");
83 			khttp_free(&req);
84 			continue;
85 		} else if (KCGI_OK != er) {
86 			fprintf(stderr, "khttp_puts: error: %d\n", er);
87 			khttp_free(&req);
88 			break;
89 		}
90 
91 		khttp_free(&req);
92 	}
93 
94 	khttp_fcgi_free(fcgi);
95 	return 0;
96 }
97