-rw-r--r-- | include/kingate/cgi_gateway.h | 164 | ||||
-rw-r--r-- | include/kingate/cgi_interface.h | 70 | ||||
-rw-r--r-- | include/kingate/exception.h | 44 | ||||
-rw-r--r-- | include/kingate/fastcgi.h | 98 | ||||
-rw-r--r-- | include/kingate/util.h | 26 |
5 files changed, 402 insertions, 0 deletions
diff --git a/include/kingate/cgi_gateway.h b/include/kingate/cgi_gateway.h new file mode 100644 index 0000000..f683580 --- a/dev/null +++ b/include/kingate/cgi_gateway.h | |||
@@ -0,0 +1,164 @@ | |||
1 | #ifndef __KINGATE_CGI_GATEWAY_H | ||
2 | #define __KINGATE_CGI_GATEWAY_H | ||
3 | |||
4 | #include <map> | ||
5 | #include "kingate/cgi_interface.h" | ||
6 | |||
7 | /** | ||
8 | * @file | ||
9 | * @brief the cgi_gateway -- main interface to CGI. | ||
10 | */ | ||
11 | |||
12 | namespace kingate { | ||
13 | using namespace std; | ||
14 | |||
15 | /** | ||
16 | * The main class interfacing with the CGI environment. | ||
17 | */ | ||
18 | class cgi_gateway { | ||
19 | public: | ||
20 | /** | ||
21 | * The interface to CGI environment (e.g. fastcgi). | ||
22 | */ | ||
23 | cgi_interface& iface; | ||
24 | /** | ||
25 | * The type describing map holding parameters parsed from query string or input. | ||
26 | */ | ||
27 | typedef multimap<string,string> params_t; | ||
28 | /** | ||
29 | * The GET-passed parameters. | ||
30 | */ | ||
31 | params_t get; | ||
32 | /** | ||
33 | * The POST-passed parameters. | ||
34 | */ | ||
35 | params_t post; | ||
36 | /** | ||
37 | * Was the stdin content parsed? | ||
38 | */ | ||
39 | bool b_parsed_content; | ||
40 | |||
41 | /** | ||
42 | * @param ci the interface to use. | ||
43 | */ | ||
44 | cgi_gateway(cgi_interface& ci); | ||
45 | |||
46 | /** | ||
47 | * Check whether there is an 'environment' meta-variable with specific name | ||
48 | * passed to CGI. | ||
49 | * @param n variable name. | ||
50 | * @return true if yes. | ||
51 | * @see cgi_interface::has_meta() | ||
52 | * @see get_meta() | ||
53 | */ | ||
54 | bool has_meta(const string& n) const { return iface.has_meta(n); } | ||
55 | /** | ||
56 | * Retrieve the 'environment' meta-variable value. | ||
57 | * @param n variable name. | ||
58 | * @return variable contents. | ||
59 | * @see exception_notfound | ||
60 | * @see cgi_interface::get_meta() | ||
61 | */ | ||
62 | string get_meta(const string& n) const { return iface.get_meta(n); } | ||
63 | |||
64 | /** | ||
65 | * fetch reference to the 'stdin' stream. | ||
66 | * @return the reference to the corresponding istream object. | ||
67 | * @see cgi_interface::in() | ||
68 | */ | ||
69 | istream& in() { return iface.in(); } | ||
70 | /** | ||
71 | * fetch reference to the 'stdout' stream. | ||
72 | * @return the reference to the corresponding ostream object. | ||
73 | * @see cgi_interface::out() | ||
74 | */ | ||
75 | ostream& out() { return iface.out(); } | ||
76 | /** | ||
77 | * fetch reference to the 'stderr' stream. | ||
78 | * @return the reference to the corresponding ostream object. | ||
79 | * @see cgi_interface::err() | ||
80 | */ | ||
81 | ostream& err() { return iface.err(); } | ||
82 | /** | ||
83 | * cast to the ostream -- fetches the reference to the 'stdout' | ||
84 | * stream. | ||
85 | * @see out() | ||
86 | */ | ||
87 | operator ostream& (void) { return out(); } | ||
88 | |||
89 | /** | ||
90 | * Check to see whether the parameter was passed via GET. | ||
91 | * @param n the parameter name. | ||
92 | * @return true if yes. | ||
93 | */ | ||
94 | bool has_GET(const string& n) const; | ||
95 | /** | ||
96 | * Retrieve the parameter passed via GET. | ||
97 | * @param n the parameter name. | ||
98 | * @return the parameter contents. | ||
99 | * @see exception_notfound | ||
100 | */ | ||
101 | string get_GET(const string& n) const; | ||
102 | /** | ||
103 | * Check to see whether the parameter was passed via POST. | ||
104 | * @param n the parameter name. | ||
105 | * @return true if yes. | ||
106 | */ | ||
107 | bool has_POST(const string& n) const; | ||
108 | /** | ||
109 | * Retrieve the POST-parameter. | ||
110 | * @param n the parameter name. | ||
111 | * @return the parameter contents. | ||
112 | * @see exception_notfound | ||
113 | */ | ||
114 | string get_POST(const string& n) const; | ||
115 | /** | ||
116 | * Check to see whether the parameter was passed either via POST or | ||
117 | * GET. | ||
118 | * @param n the parameter name. | ||
119 | * @return true if yes. | ||
120 | */ | ||
121 | bool has_param(const string& n) const; | ||
122 | /** | ||
123 | * Retrieve the parameter passed either via POST or GET | ||
124 | * (GET-parameter takes precedence). | ||
125 | * @param n the parameter name. | ||
126 | * @return true if yes. | ||
127 | * @see exception_notfound. | ||
128 | */ | ||
129 | string get_param(const string& n) const; | ||
130 | |||
131 | /** | ||
132 | * Retrieve the POST content-type (as passed via CONTENT_TYPE | ||
133 | * environment variable). | ||
134 | * @return the content type. | ||
135 | */ | ||
136 | const string& get_content_type() const; | ||
137 | /** | ||
138 | * Retrieve the POST content length (as passed via the | ||
139 | * CONTENT_LENGTH environment variable). | ||
140 | * @return the content length. | ||
141 | */ | ||
142 | unsigned long get_content_length() const; | ||
143 | |||
144 | /** | ||
145 | * Check to see whether the content from stdin stream was parsed. | ||
146 | * @return true if yes. | ||
147 | */ | ||
148 | bool is_content_parsed() const { return b_parsed_content; } | ||
149 | private: | ||
150 | /** | ||
151 | * Parse the query string, putting the parameters into the map | ||
152 | * specified. | ||
153 | * @param q the query string. | ||
154 | * @param p destination parameters map. | ||
155 | */ | ||
156 | static void parse_query(string& q,params_t& p); | ||
157 | }; | ||
158 | |||
159 | } | ||
160 | |||
161 | #endif /* __KINGATE_CGI_GATEWAY_H */ | ||
162 | /* | ||
163 | * vim:set ft=cpp: | ||
164 | */ | ||
diff --git a/include/kingate/cgi_interface.h b/include/kingate/cgi_interface.h new file mode 100644 index 0000000..84ea6dd --- a/dev/null +++ b/include/kingate/cgi_interface.h | |||
@@ -0,0 +1,70 @@ | |||
1 | #ifndef __KINGATE_CGI_INTERFACE_H | ||
2 | #define __KINGATE_CGI_INTERFACE_H | ||
3 | |||
4 | #include <iostream> | ||
5 | #include <string> | ||
6 | #include <map> | ||
7 | |||
8 | /** | ||
9 | * @file | ||
10 | * @brief the abstract base for various interfaces to CGI. | ||
11 | */ | ||
12 | |||
13 | namespace kingate { | ||
14 | using namespace std; | ||
15 | |||
16 | /** | ||
17 | * The abstract base class for interface to CGI subsystem. | ||
18 | */ | ||
19 | class cgi_interface { | ||
20 | public: | ||
21 | /** | ||
22 | * The type for map holding 'environment' meta-variables. | ||
23 | */ | ||
24 | typedef map<string,string> metavars_t; | ||
25 | /** | ||
26 | * The environment variables. | ||
27 | */ | ||
28 | metavars_t metavars; | ||
29 | |||
30 | cgi_interface() { } | ||
31 | virtual ~cgi_interface() { } | ||
32 | |||
33 | /** | ||
34 | * Check to see whether there is a particular 'environment' | ||
35 | * meta-variable passed. | ||
36 | * @param n the variable name. | ||
37 | * @return true if yes. | ||
38 | */ | ||
39 | bool has_meta(const string& n) const; | ||
40 | /** | ||
41 | * Retrieve the 'environment' variable. | ||
42 | * @param n the variable name. | ||
43 | * @return the variable contents. | ||
44 | * @see exception_notfound | ||
45 | */ | ||
46 | const string& get_meta(const string& n) const; | ||
47 | |||
48 | /** | ||
49 | * Fetch reference to CGI 'stdout' stream. | ||
50 | * @return reference to the corresponding ostream object. | ||
51 | */ | ||
52 | virtual ostream& out() = 0; | ||
53 | /** | ||
54 | * Fetch reference to CGI 'stdin' stream. | ||
55 | * @return reference to the corresponding istream object. | ||
56 | */ | ||
57 | virtual istream& in() = 0; | ||
58 | /** | ||
59 | * Fetch reference to CGI 'stderr' stream. | ||
60 | * @return reference to the corresponding ostream object. | ||
61 | */ | ||
62 | virtual ostream& err() = 0; | ||
63 | }; | ||
64 | |||
65 | } | ||
66 | |||
67 | #endif /* __KINGATE_CGI_INTERFACE_H */ | ||
68 | /* | ||
69 | * vim:set ft=cpp: | ||
70 | */ | ||
diff --git a/include/kingate/exception.h b/include/kingate/exception.h new file mode 100644 index 0000000..6ebb361 --- a/dev/null +++ b/include/kingate/exception.h | |||
@@ -0,0 +1,44 @@ | |||
1 | #ifndef __KINGATE_EXCEPTION_H | ||
2 | #define __KINGATE_EXCEPTION_H | ||
3 | |||
4 | #include <stdexcept> | ||
5 | #include <konforka/exception.h> | ||
6 | |||
7 | /** | ||
8 | * @file | ||
9 | * @brief The kingate-specific exceptions. | ||
10 | */ | ||
11 | |||
12 | /** | ||
13 | * @brief the main kingate namespace. | ||
14 | */ | ||
15 | namespace kingate { | ||
16 | using namespace std; | ||
17 | |||
18 | /** | ||
19 | * The base for kingate-specific exception. | ||
20 | */ | ||
21 | class exception : public konforka::exception { | ||
22 | public: | ||
23 | explicit exception(const string& w) | ||
24 | : konforka::exception(NOCODEPOINT,w) { } | ||
25 | exception(const string& fi,const string& fu,int l,const string &w) | ||
26 | : konforka::exception(fi,fu,l,w) { } | ||
27 | }; | ||
28 | |||
29 | /** | ||
30 | * Thrown if the specified variable or parameter wasn't found. | ||
31 | */ | ||
32 | class exception_notfound : public exception { | ||
33 | public: | ||
34 | explicit exception_notfound(const string& w) | ||
35 | : exception(w) { } | ||
36 | exception_notfound(const string& fi,const string& fu,int l,const string& w) | ||
37 | : exception(fi,fu,l,w) { } | ||
38 | }; | ||
39 | } | ||
40 | |||
41 | #endif /* __KINGATE_EXCEPTION_H */ | ||
42 | /* | ||
43 | * vim:set ft=cpp: | ||
44 | */ | ||
diff --git a/include/kingate/fastcgi.h b/include/kingate/fastcgi.h new file mode 100644 index 0000000..fd293b9 --- a/dev/null +++ b/include/kingate/fastcgi.h | |||
@@ -0,0 +1,98 @@ | |||
1 | #ifndef __KINGATE_FASTCGI_H | ||
2 | #define __KINGATE_FASTCGI_H | ||
3 | |||
4 | #include "kingate/cgi_interface.h" | ||
5 | #include <fcgio.h> | ||
6 | |||
7 | /** | ||
8 | * @file | ||
9 | * @brief the fastcgi-specific implementation. | ||
10 | */ | ||
11 | |||
12 | namespace kingate { | ||
13 | |||
14 | /** | ||
15 | * The fcgi listening socket. | ||
16 | */ | ||
17 | class fcgi_socket { | ||
18 | static bool _initialized; | ||
19 | public: | ||
20 | /** | ||
21 | * socket file descriptor. | ||
22 | */ | ||
23 | int sock; | ||
24 | |||
25 | /** | ||
26 | * @param s the socket name. (if the socket starts with a colon, | ||
27 | * then it is interpreted as a tcp port number. | ||
28 | * @param bl backlog listen queue depth. | ||
29 | */ | ||
30 | fcgi_socket(const char* s,int bl); | ||
31 | /** | ||
32 | * @param s the file descriptor of preopened socket. | ||
33 | */ | ||
34 | fcgi_socket(int s); | ||
35 | ~fcgi_socket(); | ||
36 | }; | ||
37 | |||
38 | /** | ||
39 | * The implementation of the interface to the FastCGI. | ||
40 | */ | ||
41 | class fcgi_interface : public cgi_interface { | ||
42 | public: | ||
43 | /** | ||
44 | * stdin fcgi streambuf. | ||
45 | */ | ||
46 | fcgi_streambuf sbin; | ||
47 | /** | ||
48 | * stdout fcgi streambuf. | ||
49 | */ | ||
50 | fcgi_streambuf sbout; | ||
51 | /** | ||
52 | * stderr fcgi streambuf. | ||
53 | */ | ||
54 | fcgi_streambuf sberr; | ||
55 | /** | ||
56 | * stdin istream. | ||
57 | */ | ||
58 | istream sin; | ||
59 | /** | ||
60 | * stdout ostream. | ||
61 | */ | ||
62 | ostream sout; | ||
63 | /** | ||
64 | * stderr ostream. | ||
65 | */ | ||
66 | ostream serr; | ||
67 | /** | ||
68 | * The FCGI request. | ||
69 | */ | ||
70 | FCGX_Request request; | ||
71 | |||
72 | /** | ||
73 | * @param s the socked used for interfacing with the server. | ||
74 | * @param f the request flags (e.g. FCGI_FAIL_ON_INTR). | ||
75 | */ | ||
76 | fcgi_interface(fcgi_socket& s,int f=0); | ||
77 | virtual ~fcgi_interface(); | ||
78 | |||
79 | /** | ||
80 | * @overload cgi_interface::in() | ||
81 | */ | ||
82 | istream& in() { return sin; } | ||
83 | /** | ||
84 | * @overload cgi_interface::out() | ||
85 | */ | ||
86 | ostream& out() { return sout; } | ||
87 | /** | ||
88 | * @overload cgi_interface::out() | ||
89 | */ | ||
90 | ostream& err() { return serr; } | ||
91 | }; | ||
92 | |||
93 | } | ||
94 | |||
95 | #endif /* __KINGATE_FASTCGI_H */ | ||
96 | /* | ||
97 | * vim:set ft=cpp: | ||
98 | */ | ||
diff --git a/include/kingate/util.h b/include/kingate/util.h new file mode 100644 index 0000000..4b0dca8 --- a/dev/null +++ b/include/kingate/util.h | |||
@@ -0,0 +1,26 @@ | |||
1 | #ifndef __KINGATE_UTIL_H | ||
2 | #define __KINGATE_UTIL_H | ||
3 | |||
4 | #include <string> | ||
5 | |||
6 | namespace kingate { | ||
7 | using namespace std; | ||
8 | |||
9 | /** | ||
10 | * Escape string for passing via URL. | ||
11 | * @param str string unescaped. | ||
12 | * @return the escaped string. | ||
13 | */ | ||
14 | string url_escape(const string& str); | ||
15 | /** | ||
16 | * Remove URL-encoding from the string. | ||
17 | * @param str the URL-encoded string. | ||
18 | * @return the unescaped string. | ||
19 | */ | ||
20 | string url_unescape(const string& str); | ||
21 | } | ||
22 | |||
23 | #endif /* __KINGATE_UTIL_H */ | ||
24 | /* | ||
25 | * vim:set ft=cpp: | ||
26 | */ | ||