summaryrefslogtreecommitdiffhomepage
path: root/dev/examples/cgi/cgi.cc
blob: b4c0b34a4b2392a2166cde349ee5dfd188c744be (plain)
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
/* 
  cgi example
  written by Amlal El Mahrouss.
  licensed under the MIT license
 */

#include <lib/utility/cgi.hpp>
#include <fstream>
#include <sstream>
#include <string>

const std::string g_not_found = R"(
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>error | snu-lib</title>
  <style>
    body {
      font-family: monospace;
      background: white;
      color: black;
      margin: 2em;
    }
    h1 {
      font-size: 1.5em;
    }
    hr {
      border: none;
      border-top: 1px solid #ccc;
    }
    table {
      width: 100%;
      border-collapse: collapse;
    }
    td {
      padding: 0.2em 0.5em;
    }
    td:first-child {
      white-space: nowrap;
    }
    a {
      color: blue;
      text-decoration: none;
    }
    a:hover {
      text-decoration: underline;
    }
  </style>
</head>
<body>
  <h1>Uh Oh!</h1>
  <address>No index file was found in this directory.</address>
  <hr>
  <table>
    <tr><th>Name</th></tr>
    <tr><td colspan="3"><hr></td></tr>

    <tr>
      <td><a href="javascript:window.location.reload();">Refresh</a></td>
    </tr>

    <tr><td colspan="3"><hr></td></tr>
  </table>
  <address>snu's Common Gateway Server.</address>
</body>
</html>
)";

/* finally test it */
/* @brief this stub loads a 'index.html' or returns an error message if not found. */
int main(int argc, char** argv)
{
	// ... let's assume we serve data.

	snu::cgi::cgi_writer writer;
	std::stringstream	  ss_file;

	std::ifstream fp("index.html");

	if (fp.good())
		ss_file << fp.rdbuf();
	else
		ss_file << g_not_found;

	fp.close();

	writer.eval_html(ss_file);

	return 0;
}