summaryrefslogtreecommitdiffabout
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--configure.ac16
-rw-r--r--include/kingate/cgi_gateway.h57
-rw-r--r--kingate.pc.in4
-rw-r--r--src/cgi_gateway.cc132
4 files changed, 190 insertions, 19 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,14 +65,4 @@ namespace kingate {
}catch(exception_notfound& enf) { }
- // Fetch POST content
- if(!strcasecmp(content_type().c_str(),"application/x-www-form-urlencoded")) {
- unsigned long cl = content_length();
- if(cl) {
- char * tmp = new char[cl];
- iface.in().read(tmp,cl);
- string qs(tmp,cl);
- delete tmp;
- parse_query(qs,post);
- }
- b_parsed_content = true;
- }
+ if(parsebody)
+ parse_request_body();
// Parse cookies
@@ -35,2 +73,55 @@ namespace kingate {
+ 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(!strncasecmp(
+ content_type().c_str(),
+ "application/x-www-form-urlencoded",
+ sizeof("application/x-www-form-urlencoded")-1) ) {
+ unsigned long cl = content_length();
+ if(cl) {
+ char * tmp = new char[cl];
+ iface.in().read(tmp,cl);
+ string qs(tmp,cl);
+ delete tmp;
+ parse_query(qs,post);
+ }
+ b_parsed_content = true;
+ }
+#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 */
+ }
+
bool cgi_gateway::has_GET(const string& n) const {
@@ -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;
+ }