-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtutorial5.xml
97 lines (95 loc) · 3.65 KB
/
tutorial5.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<article data-sblg-article="1" data-sblg-tags="tutorial" itemscope="itemscope" itemtype="http://schema.org/BlogPosting">
<header>
<h2 itemprop="name">
CGI for C++ applications
</h2>
<address itemprop="author"><a href="https://kristaps.bsd.lv">Kristaps Dzonsons</a></address>
<time itemprop="datePublished" datetime="2018-03-24">24 May, 2018</time>
</header>
<p>
<aside itemprop="about">
<span class="nm">kcgi</span> supports C++ callers just as easily as C.
In this brief tutorial I'll take you through compiling a C++ application that uses the <span class="nm">kcgi</span>
library.
</aside>
</p>
<h3>
Source Code
</h3>
<p>
The hard part about this example is doing anything demonstrable with C++.
So we'll do something very silly in just printing our HTTP output into the log file.
For the <span class="nm">kcgi</span> inclusion, we'll need all the usual header files.
</p>
<figure class="sample">
<pre class="prettyprint linenums">#include <sys/types.h> /* size_t, ssize_t */
#include <stdarg.h> /* va_list */
#include <stddef.h> /* NULL */
#include <stdint.h> /* int64_t */
#include <kcgi.h></pre>
</figure>
<p>
Next, we'll need our C++ bits.
This is make-work, but serves to illustrate…
</p>
<figure class="sample">
<pre class="prettyprint linenums">#include <iostream></pre>
</figure>
<p>
Now let's just jump directly into our main function.
It will do nothing more than print <q>Hello, world!</q> to the browser.
But it will also emit <q>Said hello!</q> into the error log.
See your web server configuration for where this will appear.
It's usually in the <span class="file">error.log</span> file.
On OpenBSD's default server, it's often in
<span class="file">/var/www/logs/error.log</span>.
</p>
<figure class="sample">
<pre class="prettyprint linenums">int
main(void) {
enum kcgi_err er;
struct kreq r;
const char *const pages[1] = { "index" };
/* Set up our main HTTP context. */
er = khttp_parse(&r, NULL, 0, pages, 1, 0);
if (er != KCGI_OK)
return 0;
khttp_head(&r, kresps[KRESP_STATUS],
"%s", khttps[KHTTP_200]);
khttp_head(&r, kresps[KRESP_CONTENT_TYPE],
"%s", kmimetypes[r.mime]);
khttp_body(&r);
khttp_puts(&r, "Hello, world!\n");
std::cerr << "Said hello!";
khttp_free(&r);
return 0;
}</pre>
</figure>
<p>
Nothing to it—looks like any of our C examples.
The difference is that we've used some C++ code to emit <q>Said hello!</q> to the error log.
The next part is how we can compile this code.
For that, we'll need to use a C++ compiler instead of the C compiler we've been using to date.
</p>
<figure class="sample">
<pre class="prettyprint lang-sh linenums">% c++ `pkg-config --cflags kcgi` -c -o tutorial5.o tutorial5.cc
% c++ -static -o tutorial5 tutorial5.cc `pkg-config --libs kcgi`</pre>
</figure>
<p>
Or just…
(and noting again the <code>-static</code>, which we need by being in our file-system jail)
</p>
<figure class="sample">
<pre class="prettyprint lang-sh linenums">% c++ -static `pkg-config --cflags --libs kcgi` -o tutorial5 tutorial5.cc</pre>
</figure>
<p>
Now you can install your compiled CGI script just like any CGI script.
See <a href="tutorial0.html">Getting Started with CGI in C</a> for these steps in detail.
All of these steps work for FastCGI, of course.
Enjoy!
(On non-OpenBSD systems, you'll probably need to use <code>sudo</code> instead of <code>doas</code>.)
</p>
<figure class="sample">
<pre class="prettyprint lang-sh linenums">% doas install -m 0555 tutorial5 /var/www/cgi-bin</pre>
</figure>
</article>