-rw-r--r-- | include/kingate/cgi_gateway.h | 164 |
1 files changed, 164 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 | */ | ||