author | Michael Krelin <hacker@klever.net> | 2005-07-04 22:54:09 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2005-07-04 22:54:09 (UTC) |
commit | c5d7daf77e265b53951e1e2b09c51e2fba5e93dc (patch) (side-by-side diff) | |
tree | 2337ff96d85d8de6d25fbaed75eb9c0d71a1625d | |
parent | c8bcbfb951eec5fe14dac0b14f4faaf4a9f9f229 (diff) | |
download | kingate-c5d7daf77e265b53951e1e2b09c51e2fba5e93dc.zip kingate-c5d7daf77e265b53951e1e2b09c51e2fba5e93dc.tar.gz kingate-c5d7daf77e265b53951e1e2b09c51e2fba5e93dc.tar.bz2 |
1. awkward yet working support for POST file upload (requires mimetic)
2. support for NOT handling/parsing POSTed data (including application/x-www-form-urlencoded)
-rw-r--r-- | configure.ac | 16 | ||||
-rw-r--r-- | include/kingate/cgi_gateway.h | 57 | ||||
-rw-r--r-- | kingate.pc.in | 4 | ||||
-rw-r--r-- | src/cgi_gateway.cc | 118 |
4 files changed, 183 insertions, 12 deletions
diff --git a/configure.ac b/configure.ac index b3141f1..b51d64d 100644 --- a/configure.ac +++ b/configure.ac @@ -2,3 +2,3 @@ AC_INIT([kingate], [0.0.1], [kingate-bugs@klever.net]) AC_CONFIG_SRCDIR([include/kingate/cgi_gateway.h]) -AC_CONFIG_HEADER([config.h]) +AC_CONFIG_HEADERS([config.h]) AM_INIT_AUTOMAKE([dist-bzip2]) @@ -62,2 +62,16 @@ fi +HAVE_MIMETIC=false +AC_LANG_PUSH(C++) + AC_CHECK_LIB([mimetic],[main],[ + MIMETIC_LIBS=-lmimetic + HAVE_MIMETIC=true + ] + ) +AC_LANG_POP(C++) +AC_SUBST([MIMETIC_LIBS]) +AC_SUBST([MIMETIC_CFLAGS]) +if ${HAVE_MIMETIC} ; then + AC_DEFINE([HAVE_MIMETIC],,[defined in presence of mimetic]) +fi + AC_CONFIG_FILES([ diff --git a/include/kingate/cgi_gateway.h b/include/kingate/cgi_gateway.h index f20d72b..a26b0ae 100644 --- a/include/kingate/cgi_gateway.h +++ b/include/kingate/cgi_gateway.h @@ -45,2 +45,33 @@ namespace kingate { /** + * Abstract base class for retrieving posted files. + */ + class basic_file_t { + public: + /** + * Retrieve file name. + * @return filename + */ + virtual const string& filename() const = 0; + /** + * Retrieve file content type. + * @return content type + */ + virtual const string& content_type() const = 0; + /** + * Retrieve file contents. + * @return reference to the stream for accessing file content. + */ + virtual istream& content() = 0; + virtual ~basic_file_t(); + }; + typedef basic_file_t *file_t; + /** + * The map holding information pertaining to files uploaded via post. + */ + typedef multimap<string,file_t> files_t; + /** + * Files uploaded via post + */ + files_t files; + /** * Cookies passed. @@ -56,3 +87,9 @@ namespace kingate { */ - cgi_gateway(cgi_interface& ci); + cgi_gateway(cgi_interface& ci,bool parsebody = true); + virtual ~cgi_gateway() throw(); + + /** + * Parse request body. + */ + void parse_request_body(); @@ -138,3 +175,3 @@ namespace kingate { * @param n the parameter name. - * @return true if yes. + * @return the parameter contents. * @see exception_notfound. @@ -142,2 +179,16 @@ namespace kingate { const string& get_param(const string& n) const; + /** + * Check to see whether the file was uploaded in the request body. + * @param n the parameter name. + * @return true if yes. + */ + bool has_file(const string& n) const; + /** + * Retrieve the file uploaded in the request body. + * @param n the parameter name. + * @return the file. + * @see exception_notfound. + */ + const file_t get_file(const string& n) const; + file_t get_file(const string& n); @@ -179,3 +230,3 @@ namespace kingate { */ - unsigned long cgi_gateway::content_length() const; + unsigned long content_length() const; /** diff --git a/kingate.pc.in b/kingate.pc.in index 671faac..05cfe1d 100644 --- a/kingate.pc.in +++ b/kingate.pc.in @@ -9,3 +9,3 @@ Version: @VERSION@ Requires: konforka -Libs: -L${libdir} -lkingate -Cflags: -I${includedir} +Libs: -L${libdir} -lkingate @MIMETIC_LIBS@ +Cflags: -I${includedir} @MIMETIC_CFLAGS@ diff --git a/src/cgi_gateway.cc b/src/cgi_gateway.cc index ab48f78..1706679 100644 --- a/src/cgi_gateway.cc +++ b/src/cgi_gateway.cc @@ -2,2 +2,3 @@ #include <ctype.h> +#include <sstream> #include "kingate/cgi_gateway.h" @@ -5,4 +6,36 @@ #include "kingate/exception.h" +#include "config.h" +#ifdef HAVE_MIMETIC +# include <mimetic/mimeentity.h> +# include <mimetic/parser/itparser.h> +#endif /* HAVE_MIMETIC */ namespace kingate { + using mimetic::MimeEntity; + +#ifdef HAVE_MIMETIC + struct TornMimeEntity : public MimeEntity { + typedef istreambuf_iterator<char> it_type; + typedef it_type::iterator_category it_cat; + struct IParser : public mimetic::IteratorParser<it_type,it_cat> { + typedef mimetic::IteratorParser<it_type,it_cat> BT; + IParser(MimeEntity& me) + : BT::IteratorParser<it_type,it_cat>(me) { } + void loadHeader(it_type bit,it_type eit) { + m_bit = bit; m_eit = eit; + BT::loadHeader(); + } + void loadBody(it_type bit,it_type eit) { + m_bit = bit; m_eit = eit; + BT::loadBody(); + } + }; + void load(istream& hs,istream& bs,int mask=0) { + IParser prs(*this); + prs.iMask(mask); + prs.loadHeader(it_type(hs),it_type()); + prs.loadBody(it_type(bs),it_type()); + } + }; +#endif /* HAVE_MIMETIC */ @@ -10,3 +43,18 @@ namespace kingate { - cgi_gateway::cgi_gateway(cgi_interface& ci) + cgi_gateway::basic_file_t::~basic_file_t() { } + + class string_file_t : public cgi_gateway::basic_file_t { + public: + string _file_name; + string _content_type; + stringstream _content; + + string_file_t(const string& fn,const string& ct,const string& s) + : _file_name(fn), _content_type(ct), _content(s,ios::in) { } + const string& filename() const { return _file_name; } + const string& content_type() const { return _content_type; } + istream& content() { return _content; } + }; + + cgi_gateway::cgi_gateway(cgi_interface& ci,bool parsebody) : iface(ci), b_parsed_content(false) { @@ -17,4 +65,24 @@ namespace kingate { }catch(exception_notfound& enf) { } + if(parsebody) + parse_request_body(); + // Parse cookies + try { + cookies.parse_cookies(get_meta("HTTP_COOKIE")); + }catch(exception_notfound& enf) { } + } + + cgi_gateway::~cgi_gateway() throw() { + for(files_t::iterator i=files.begin();i!=files.end();++i) + delete i->second; + files.clear(); + } + + void cgi_gateway::parse_request_body() { + if(b_parsed_content) + throw konforka::exception(CODEPOINT,"request body is already parsed"); // Fetch POST content - if(!strcasecmp(content_type().c_str(),"application/x-www-form-urlencoded")) { + if(!strncasecmp( + content_type().c_str(), + "application/x-www-form-urlencoded", + sizeof("application/x-www-form-urlencoded")-1) ) { unsigned long cl = content_length(); @@ -29,6 +97,29 @@ namespace kingate { } - // Parse cookies - try { - cookies.parse_cookies(get_meta("HTTP_COOKIE")); - }catch(exception_notfound& enf) { } +#ifdef HAVE_MIMETIC + else if(!strncasecmp( + content_type().c_str(), + "multipart/form-data", + sizeof("multipart/form-data")-1) ) { + stringstream h; + h + << "Content-Type: " << content_type() << "\r\n" + << "Content-Length: " << content_length() << "\r\n\n"; + TornMimeEntity me; + me.load(h,iface.in(),0); + mimetic::MimeEntityList& parts = me.body().parts(); + for(mimetic::MimeEntityList::iterator i=parts.begin();i!=parts.end();++i) { + MimeEntity *p = *i; + const mimetic::ContentDisposition& cd = p->header().contentDisposition(); + string n = cd.param("name"); + string fn = cd.param("filename"); + if(fn.empty()) { + post.insert(params_t::value_type(n,p->body())); + }else{ + const mimetic::ContentType& ct = p->header().contentType(); + files.insert(files_t::value_type(n,new string_file_t(fn,ct.str(),p->body()))); + } + } + b_parsed_content = true; + } +#endif /* HAVE_MIMETIC */ } @@ -65,2 +156,17 @@ namespace kingate { } + bool cgi_gateway::has_file(const string& n) const { + return files.find(n) != files.end(); + } + const cgi_gateway::file_t cgi_gateway::get_file(const string& n) const { + files_t::const_iterator i = files.find(n); + if(i==files.end()) + throw exception_notfound(CODEPOINT,"no such parameter"); + return i->second; + } + cgi_gateway::file_t cgi_gateway::get_file(const string& n) { + files_t::const_iterator i = files.find(n); + if(i==files.end()) + throw exception_notfound(CODEPOINT,"no such parameter"); + return i->second; + } |