ksgi

Форк
0
/
tutorial5.xml 
97 строк · 3.6 Кб
1
<article data-sblg-article="1" data-sblg-tags="tutorial" itemscope="itemscope" itemtype="http://schema.org/BlogPosting">
2
	<header>
3
		<h2 itemprop="name">
4
			CGI for C++ applications
5
		</h2>
6
		<address itemprop="author"><a href="https://kristaps.bsd.lv">Kristaps Dzonsons</a></address>
7
		<time itemprop="datePublished" datetime="2018-03-24">24 May, 2018</time>
8
	</header>
9
	<p>
10
		<aside itemprop="about">
11
			<span class="nm">kcgi</span> supports C++ callers just as easily as C.
12
			In this brief tutorial I'll take you through compiling a C++ application that uses the <span class="nm">kcgi</span>
13
			library.
14
		</aside>
15
	</p>
16
	<h3>
17
		Source Code
18
	</h3>
19
	<p>
20
		The hard part about this example is doing anything demonstrable with C++.
21
		So we'll do something very silly in just printing our HTTP output into the log file.
22
		For the <span class="nm">kcgi</span> inclusion, we'll need all the usual header files.
23
	</p>
24
	<figure class="sample">
25
		<pre class="prettyprint linenums">#include &lt;sys/types.h&gt; /* size_t, ssize_t */
26
#include &lt;stdarg.h&gt; /* va_list */
27
#include &lt;stddef.h&gt; /* NULL */
28
#include &lt;stdint.h&gt; /* int64_t */
29
#include &lt;kcgi.h&gt;</pre>
30
	</figure>
31
	<p>
32
		Next, we'll need our C++ bits.
33
		This is make-work, but serves to illustrate&#8230;
34
	</p>
35
	<figure class="sample">
36
		<pre class="prettyprint linenums">#include &lt;iostream&gt;</pre>
37
	</figure>
38
	<p>
39
		Now let's just jump directly into our main function.
40
		It will do nothing more than print <q>Hello, world!</q> to the browser.
41
		But it will also emit <q>Said hello!</q> into the error log.
42
		See your web server configuration for where this will appear.
43
		It's usually in the <span class="file">error.log</span> file.
44
		On OpenBSD's default server, it's often in 
45
		<span class="file">/var/www/logs/error.log</span>.
46
	</p>
47
	<figure class="sample">
48
		<pre class="prettyprint linenums">int 
49
main(void) {
50
    enum kcgi_err er;
51
    struct kreq r;
52
    const char *const pages[1] = { "index" };
53

54
    /* Set up our main HTTP context. */
55

56
    er = khttp_parse(&amp;r, NULL, 0, pages, 1, 0);
57
    if (er != KCGI_OK)
58
        return 0;
59
    khttp_head(&amp;r, kresps[KRESP_STATUS], 
60
        "%s", khttps[KHTTP_200]);
61
    khttp_head(&amp;r, kresps[KRESP_CONTENT_TYPE], 
62
        "%s", kmimetypes[r.mime]);
63
    khttp_body(&amp;r);
64
    khttp_puts(&amp;r, "Hello, world!\n");
65
    std::cerr &lt;&lt; "Said hello!";
66
    khttp_free(&amp;r);
67
    return 0;
68
}</pre>
69
	</figure>
70
	<p>
71
		Nothing to it&#8212;looks like any of our C examples.
72
		The difference is that we've used some C++ code to emit <q>Said hello!</q> to the error log.
73
		The next part is how we can compile this code.
74
		For that, we'll need to use a C++ compiler instead of the C compiler we've been using to date.
75
	</p>
76
	<figure class="sample">
77
		<pre class="prettyprint lang-sh linenums">% c++ `pkg-config --cflags kcgi` -c -o tutorial5.o tutorial5.cc
78
% c++ -static -o tutorial5 tutorial5.cc `pkg-config --libs kcgi`</pre>
79
	</figure>
80
	<p>
81
		Or just&#8230;
82
		(and noting again the <code>-static</code>, which we need by being in our file-system jail)
83
	</p>
84
	<figure class="sample">
85
		<pre class="prettyprint lang-sh linenums">% c++ -static `pkg-config --cflags --libs kcgi` -o tutorial5 tutorial5.cc</pre>
86
	</figure>
87
	<p>
88
		Now you can install your compiled CGI script just like any CGI script.
89
		See <a href="tutorial0.html">Getting Started with CGI in C</a> for these steps in detail.
90
		All of these steps work for FastCGI, of course.
91
		Enjoy!
92
		(On non-OpenBSD systems, you'll probably need to use <code>sudo</code> instead of <code>doas</code>.)
93
	</p>
94
	<figure class="sample">
95
		<pre class="prettyprint lang-sh linenums">% doas install -m 0555 tutorial5 /var/www/cgi-bin</pre>
96
	</figure>
97
</article>
98

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

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

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

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