ksgi
/
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">4CGI 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.12In this brief tutorial I'll take you through compiling a C++ application that uses the <span class="nm">kcgi</span>13library.
14</aside>15</p>16<h3>17Source Code
18</h3>19<p>20The hard part about this example is doing anything demonstrable with C++.
21So we'll do something very silly in just printing our HTTP output into the log file.
22For 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 <sys/types.h> /* size_t, ssize_t */26#include <stdarg.h> /* va_list */27#include <stddef.h> /* NULL */28#include <stdint.h> /* int64_t */29#include <kcgi.h></pre>30</figure>31<p>32Next, we'll need our C++ bits.
33This is make-work, but serves to illustrate…34</p>35<figure class="sample">36<pre class="prettyprint linenums">#include <iostream></pre>37</figure>38<p>39Now let's just jump directly into our main function.
40It will do nothing more than print <q>Hello, world!</q> to the browser.41But it will also emit <q>Said hello!</q> into the error log.42See your web server configuration for where this will appear.
43It's usually in the <span class="file">error.log</span> file.44On 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">int49main(void) {
50enum kcgi_err er;
51struct kreq r;
52const char *const pages[1] = { "index" };
53
54/* Set up our main HTTP context. */
55
56er = khttp_parse(&r, NULL, 0, pages, 1, 0);57if (er != KCGI_OK)
58return 0;
59khttp_head(&r, kresps[KRESP_STATUS],60"%s", khttps[KHTTP_200]);
61khttp_head(&r, kresps[KRESP_CONTENT_TYPE],62"%s", kmimetypes[r.mime]);
63khttp_body(&r);64khttp_puts(&r, "Hello, world!\n");65std::cerr << "Said hello!";66khttp_free(&r);67return 0;
68}</pre>69</figure>70<p>71Nothing to it—looks like any of our C examples.72The difference is that we've used some C++ code to emit <q>Said hello!</q> to the error log.73The next part is how we can compile this code.
74For 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.cc78% c++ -static -o tutorial5 tutorial5.cc `pkg-config --libs kcgi`</pre>79</figure>80<p>81Or just…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>88Now you can install your compiled CGI script just like any CGI script.
89See <a href="tutorial0.html">Getting Started with CGI in C</a> for these steps in detail.90All of these steps work for FastCGI, of course.
91Enjoy!
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