mirror of
https://github.com/lammertb/libhttp.git
synced 2025-12-22 04:02:04 +03:00
C++ Wrapper: Store post data per connection instead of per server
Fix #2 as proposed on June 16 2014 - see: See https://groups.google.com/forum/#!topic/civetweb/ri0mNRJ_5-4
This commit is contained in:
@@ -9,10 +9,11 @@
|
|||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
||||||
#include "civetweb.h"
|
#include "civetweb.h"
|
||||||
#include <vector>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class CivetServer; // forward declaration
|
// forward declaration
|
||||||
|
class CivetServer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic interface for a URI request handler. Handlers implementations
|
* Basic interface for a URI request handler. Handlers implementations
|
||||||
@@ -271,11 +272,24 @@ public:
|
|||||||
static void urlEncode(const char *src, size_t src_len, std::string &dst, bool append=false);
|
static void urlEncode(const char *src, size_t src_len, std::string &dst, bool append=false);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
class CivetConnection {
|
||||||
struct mg_context *context;
|
public:
|
||||||
char * postData;
|
char * postData;
|
||||||
unsigned long postDataLen;
|
unsigned long postDataLen;
|
||||||
|
|
||||||
|
CivetConnection() {
|
||||||
|
postData = NULL;
|
||||||
|
postDataLen = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
~CivetConnection() {
|
||||||
|
free(postData);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mg_context *context;
|
||||||
|
std::map<struct mg_connection *, class CivetConnection> connections;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* requestHandler(struct mg_connection *, void *cbdata)
|
* requestHandler(struct mg_connection *, void *cbdata)
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
|
|||||||
assert(request_info != NULL);
|
assert(request_info != NULL);
|
||||||
CivetServer *me = (CivetServer*) (request_info->user_data);
|
CivetServer *me = (CivetServer*) (request_info->user_data);
|
||||||
assert(me != NULL);
|
assert(me != NULL);
|
||||||
|
me->connections[conn] = CivetConnection();
|
||||||
|
|
||||||
CivetHandler *handler = (CivetHandler *)cbdata;
|
CivetHandler *handler = (CivetHandler *)cbdata;
|
||||||
|
|
||||||
@@ -68,7 +69,7 @@ int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
|
|||||||
|
|
||||||
CivetServer::CivetServer(const char **options,
|
CivetServer::CivetServer(const char **options,
|
||||||
const struct mg_callbacks *_callbacks) :
|
const struct mg_callbacks *_callbacks) :
|
||||||
context(0), postData(0)
|
context(0)
|
||||||
{
|
{
|
||||||
struct mg_callbacks callbacks;
|
struct mg_callbacks callbacks;
|
||||||
memset(&callbacks, 0, sizeof(callbacks));
|
memset(&callbacks, 0, sizeof(callbacks));
|
||||||
@@ -80,7 +81,6 @@ CivetServer::CivetServer(const char **options,
|
|||||||
userCloseHandler = NULL;
|
userCloseHandler = NULL;
|
||||||
}
|
}
|
||||||
callbacks.connection_close = closeHandler;
|
callbacks.connection_close = closeHandler;
|
||||||
postDataLen = 0;
|
|
||||||
context = mg_start(&callbacks, this, options);
|
context = mg_start(&callbacks, this, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,11 +97,7 @@ void CivetServer::closeHandler(struct mg_connection *conn)
|
|||||||
assert(me != NULL);
|
assert(me != NULL);
|
||||||
|
|
||||||
if (me->userCloseHandler) me->userCloseHandler(conn);
|
if (me->userCloseHandler) me->userCloseHandler(conn);
|
||||||
if (me->postData) {
|
me->connections.erase(conn);
|
||||||
free(me->postData);
|
|
||||||
me->postData = 0;
|
|
||||||
}
|
|
||||||
me->postDataLen = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CivetServer::addHandler(const std::string &uri, CivetHandler *handler)
|
void CivetServer::addHandler(const std::string &uri, CivetHandler *handler)
|
||||||
@@ -176,22 +172,23 @@ CivetServer::getParam(struct mg_connection *conn, const char *name,
|
|||||||
assert(ri != NULL);
|
assert(ri != NULL);
|
||||||
CivetServer *me = (CivetServer*) (ri->user_data);
|
CivetServer *me = (CivetServer*) (ri->user_data);
|
||||||
assert(me != NULL);
|
assert(me != NULL);
|
||||||
|
CivetConnection &conobj = me->connections[conn];
|
||||||
|
|
||||||
if (me->postData != NULL) {
|
if (conobj.postData != NULL) {
|
||||||
formParams = me->postData;
|
formParams = conobj.postData;
|
||||||
} else {
|
} else {
|
||||||
const char * con_len_str = mg_get_header(conn, "Content-Length");
|
const char * con_len_str = mg_get_header(conn, "Content-Length");
|
||||||
if (con_len_str) {
|
if (con_len_str) {
|
||||||
unsigned long con_len = atoi(con_len_str);
|
unsigned long con_len = atoi(con_len_str);
|
||||||
if (con_len>0) {
|
if (con_len>0) {
|
||||||
// Add one extra character for 0-termination of strings
|
// Add one extra character for 0-termination of strings
|
||||||
me->postData = (char*)malloc(con_len + 1);
|
conobj.postData = (char*)malloc(con_len + 1);
|
||||||
if (me->postData != NULL) {
|
if (conobj.postData != NULL) {
|
||||||
// malloc may fail for huge requests
|
// malloc may fail for huge requests
|
||||||
mg_read(conn, me->postData, con_len);
|
mg_read(conn, conobj.postData, con_len);
|
||||||
me->postData[con_len] = 0;
|
conobj.postData[con_len] = 0;
|
||||||
formParams = me->postData;
|
formParams = conobj.postData;
|
||||||
me->postDataLen = con_len;
|
conobj.postDataLen = con_len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user