浏览代码

Merge pull request #562 from marcoafo/master

Adds static method to get post data from CivetServer
bel2125 7 年之前
父节点
当前提交
952827581d
共有 2 个文件被更改,包括 31 次插入0 次删除
  1. 12 0
      include/CivetServer.h
  2. 19 0
      src/CivetServer.cpp

+ 12 - 0
include/CivetServer.h

@@ -450,6 +450,18 @@ class CIVETWEB_API CivetServer
 	                     size_t occurrence = 0);
 
 	/**
+	 * getPostData(struct mg_connection *)
+	 *
+	 * Returns response body from a request made as POST. Since the
+	 * connections map is protected, it can't be directly accessed.
+	 * This uses string to store post data to handle big posts.
+	 *
+	 * @param conn - connection from which post data will be read
+	 * @return Post data (empty if not avaliable).
+	 */
+     static std::string getPostData(struct mg_connection *conn);
+     
+	/**
 	 * urlDecode(const std::string &, std::string &, bool)
 	 *
 	 * @param src - string to be decoded

+ 19 - 0
src/CivetServer.cpp

@@ -546,6 +546,25 @@ CivetServer::getParam(const char *data,
 	return false;
 }
 
+std::string
+CivetServer::getPostData(struct mg_connection *conn)
+{
+	const struct mg_request_info *ri = mg_get_request_info(conn);
+	assert(ri != NULL);
+	CivetServer *me = (CivetServer *)(ri->user_data);
+	assert(me != NULL);
+	mg_lock_context(me->context);
+	CivetConnection &conobj = me->connections[conn];
+	mg_lock_connection(conn);
+	mg_unlock_context(me->context);
+	std::string postdata;
+	postdata.resize(conobj.postDataLen);
+	memcpy(&postdata[0],conobj.postData,conobj.postDataLen);
+	postdata += '\0';
+	mg_unlock_connection(conn);
+	return postdata;
+}
+
 void
 CivetServer::urlEncode(const char *src, std::string &dst, bool append)
 {