-rw-r--r-- | include/.gitignore | 2 | ||||
-rw-r--r-- | include/Makefile.am | 6 | ||||
-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 |
7 files changed, 410 insertions, 0 deletions
diff --git a/include/.gitignore b/include/.gitignore new file mode 100644 index 0000000..3dda729 --- a/dev/null +++ b/include/.gitignore @@ -0,0 +1,2 @@ +Makefile.in +Makefile diff --git a/include/Makefile.am b/include/Makefile.am new file mode 100644 index 0000000..c1ec36e --- a/dev/null +++ b/include/Makefile.am @@ -0,0 +1,6 @@ +nobase_include_HEADERS = \ + kingate/cgi_gateway.h \ + kingate/cgi_interface.h \ + kingate/fastcgi.h \ + kingate/exception.h \ + kingate/util.h 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 @@ +#ifndef __KINGATE_CGI_GATEWAY_H +#define __KINGATE_CGI_GATEWAY_H + +#include <map> +#include "kingate/cgi_interface.h" + +/** + * @file + * @brief the cgi_gateway -- main interface to CGI. + */ + +namespace kingate { + using namespace std; + + /** + * The main class interfacing with the CGI environment. + */ + class cgi_gateway { + public: + /** + * The interface to CGI environment (e.g. fastcgi). + */ + cgi_interface& iface; + /** + * The type describing map holding parameters parsed from query string or input. + */ + typedef multimap<string,string> params_t; + /** + * The GET-passed parameters. + */ + params_t get; + /** + * The POST-passed parameters. + */ + params_t post; + /** + * Was the stdin content parsed? + */ + bool b_parsed_content; + + /** + * @param ci the interface to use. + */ + cgi_gateway(cgi_interface& ci); + + /** + * Check whether there is an 'environment' meta-variable with specific name + * passed to CGI. + * @param n variable name. + * @return true if yes. + * @see cgi_interface::has_meta() + * @see get_meta() + */ + bool has_meta(const string& n) const { return iface.has_meta(n); } + /** + * Retrieve the 'environment' meta-variable value. + * @param n variable name. + * @return variable contents. + * @see exception_notfound + * @see cgi_interface::get_meta() + */ + string get_meta(const string& n) const { return iface.get_meta(n); } + + /** + * fetch reference to the 'stdin' stream. + * @return the reference to the corresponding istream object. + * @see cgi_interface::in() + */ + istream& in() { return iface.in(); } + /** + * fetch reference to the 'stdout' stream. + * @return the reference to the corresponding ostream object. + * @see cgi_interface::out() + */ + ostream& out() { return iface.out(); } + /** + * fetch reference to the 'stderr' stream. + * @return the reference to the corresponding ostream object. + * @see cgi_interface::err() + */ + ostream& err() { return iface.err(); } + /** + * cast to the ostream -- fetches the reference to the 'stdout' + * stream. + * @see out() + */ + operator ostream& (void) { return out(); } + + /** + * Check to see whether the parameter was passed via GET. + * @param n the parameter name. + * @return true if yes. + */ + bool has_GET(const string& n) const; + /** + * Retrieve the parameter passed via GET. + * @param n the parameter name. + * @return the parameter contents. + * @see exception_notfound + */ + string get_GET(const string& n) const; + /** + * Check to see whether the parameter was passed via POST. + * @param n the parameter name. + * @return true if yes. + */ + bool has_POST(const string& n) const; + /** + * Retrieve the POST-parameter. + * @param n the parameter name. + * @return the parameter contents. + * @see exception_notfound + */ + string get_POST(const string& n) const; + /** + * Check to see whether the parameter was passed either via POST or + * GET. + * @param n the parameter name. + * @return true if yes. + */ + bool has_param(const string& n) const; + /** + * Retrieve the parameter passed either via POST or GET + * (GET-parameter takes precedence). + * @param n the parameter name. + * @return true if yes. + * @see exception_notfound. + */ + string get_param(const string& n) const; + + /** + * Retrieve the POST content-type (as passed via CONTENT_TYPE + * environment variable). + * @return the content type. + */ + const string& get_content_type() const; + /** + * Retrieve the POST content length (as passed via the + * CONTENT_LENGTH environment variable). + * @return the content length. + */ + unsigned long get_content_length() const; + + /** + * Check to see whether the content from stdin stream was parsed. + * @return true if yes. + */ + bool is_content_parsed() const { return b_parsed_content; } + private: + /** + * Parse the query string, putting the parameters into the map + * specified. + * @param q the query string. + * @param p destination parameters map. + */ + static void parse_query(string& q,params_t& p); + }; + +} + +#endif /* __KINGATE_CGI_GATEWAY_H */ +/* + * vim:set ft=cpp: + */ 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 @@ +#ifndef __KINGATE_CGI_INTERFACE_H +#define __KINGATE_CGI_INTERFACE_H + +#include <iostream> +#include <string> +#include <map> + +/** + * @file + * @brief the abstract base for various interfaces to CGI. + */ + +namespace kingate { + using namespace std; + + /** + * The abstract base class for interface to CGI subsystem. + */ + class cgi_interface { + public: + /** + * The type for map holding 'environment' meta-variables. + */ + typedef map<string,string> metavars_t; + /** + * The environment variables. + */ + metavars_t metavars; + + cgi_interface() { } + virtual ~cgi_interface() { } + + /** + * Check to see whether there is a particular 'environment' + * meta-variable passed. + * @param n the variable name. + * @return true if yes. + */ + bool has_meta(const string& n) const; + /** + * Retrieve the 'environment' variable. + * @param n the variable name. + * @return the variable contents. + * @see exception_notfound + */ + const string& get_meta(const string& n) const; + + /** + * Fetch reference to CGI 'stdout' stream. + * @return reference to the corresponding ostream object. + */ + virtual ostream& out() = 0; + /** + * Fetch reference to CGI 'stdin' stream. + * @return reference to the corresponding istream object. + */ + virtual istream& in() = 0; + /** + * Fetch reference to CGI 'stderr' stream. + * @return reference to the corresponding ostream object. + */ + virtual ostream& err() = 0; + }; + +} + +#endif /* __KINGATE_CGI_INTERFACE_H */ +/* + * vim:set ft=cpp: + */ 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 @@ +#ifndef __KINGATE_EXCEPTION_H +#define __KINGATE_EXCEPTION_H + +#include <stdexcept> +#include <konforka/exception.h> + +/** + * @file + * @brief The kingate-specific exceptions. + */ + +/** + * @brief the main kingate namespace. + */ +namespace kingate { + using namespace std; + + /** + * The base for kingate-specific exception. + */ + class exception : public konforka::exception { + public: + explicit exception(const string& w) + : konforka::exception(NOCODEPOINT,w) { } + exception(const string& fi,const string& fu,int l,const string &w) + : konforka::exception(fi,fu,l,w) { } + }; + + /** + * Thrown if the specified variable or parameter wasn't found. + */ + class exception_notfound : public exception { + public: + explicit exception_notfound(const string& w) + : exception(w) { } + exception_notfound(const string& fi,const string& fu,int l,const string& w) + : exception(fi,fu,l,w) { } + }; +} + +#endif /* __KINGATE_EXCEPTION_H */ +/* + * vim:set ft=cpp: + */ 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 @@ +#ifndef __KINGATE_FASTCGI_H +#define __KINGATE_FASTCGI_H + +#include "kingate/cgi_interface.h" +#include <fcgio.h> + +/** + * @file + * @brief the fastcgi-specific implementation. + */ + +namespace kingate { + + /** + * The fcgi listening socket. + */ + class fcgi_socket { + static bool _initialized; + public: + /** + * socket file descriptor. + */ + int sock; + + /** + * @param s the socket name. (if the socket starts with a colon, + * then it is interpreted as a tcp port number. + * @param bl backlog listen queue depth. + */ + fcgi_socket(const char* s,int bl); + /** + * @param s the file descriptor of preopened socket. + */ + fcgi_socket(int s); + ~fcgi_socket(); + }; + + /** + * The implementation of the interface to the FastCGI. + */ + class fcgi_interface : public cgi_interface { + public: + /** + * stdin fcgi streambuf. + */ + fcgi_streambuf sbin; + /** + * stdout fcgi streambuf. + */ + fcgi_streambuf sbout; + /** + * stderr fcgi streambuf. + */ + fcgi_streambuf sberr; + /** + * stdin istream. + */ + istream sin; + /** + * stdout ostream. + */ + ostream sout; + /** + * stderr ostream. + */ + ostream serr; + /** + * The FCGI request. + */ + FCGX_Request request; + + /** + * @param s the socked used for interfacing with the server. + * @param f the request flags (e.g. FCGI_FAIL_ON_INTR). + */ + fcgi_interface(fcgi_socket& s,int f=0); + virtual ~fcgi_interface(); + + /** + * @overload cgi_interface::in() + */ + istream& in() { return sin; } + /** + * @overload cgi_interface::out() + */ + ostream& out() { return sout; } + /** + * @overload cgi_interface::out() + */ + ostream& err() { return serr; } + }; + +} + +#endif /* __KINGATE_FASTCGI_H */ +/* + * vim:set ft=cpp: + */ 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 @@ +#ifndef __KINGATE_UTIL_H +#define __KINGATE_UTIL_H + +#include <string> + +namespace kingate { + using namespace std; + + /** + * Escape string for passing via URL. + * @param str string unescaped. + * @return the escaped string. + */ + string url_escape(const string& str); + /** + * Remove URL-encoding from the string. + * @param str the URL-encoded string. + * @return the unescaped string. + */ + string url_unescape(const string& str); +} + +#endif /* __KINGATE_UTIL_H */ +/* + * vim:set ft=cpp: + */ |