mirror of
https://github.com/lammertb/libhttp.git
synced 2025-07-29 21:01:13 +03:00
Issue #11: Method to get POST request parameters via C++ interface
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
/*
|
/* Copyright (c) 2013-2014 the Civetweb developers
|
||||||
* Copyright (c) 2013 No Face Press, LLC
|
* Copyright (c) 2013 No Face Press, LLC
|
||||||
* License http://opensource.org/licenses/mit-license.php MIT License
|
* License http://opensource.org/licenses/mit-license.php MIT License
|
||||||
*/
|
*/
|
||||||
@ -12,7 +12,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define DOCUMENT_ROOT "."
|
#define DOCUMENT_ROOT "."
|
||||||
#define PORT "8888"
|
#define PORT "8081"
|
||||||
#define EXAMPLE_URI "/example"
|
#define EXAMPLE_URI "/example"
|
||||||
#define EXIT_URI "/exit"
|
#define EXIT_URI "/exit"
|
||||||
bool exitNow = false;
|
bool exitNow = false;
|
||||||
@ -47,14 +47,27 @@ public:
|
|||||||
|
|
||||||
class AHandler: public CivetHandler
|
class AHandler: public CivetHandler
|
||||||
{
|
{
|
||||||
public:
|
private:
|
||||||
bool handleGet(CivetServer *server, struct mg_connection *conn) {
|
bool handleAll(const char * method, CivetServer *server, struct mg_connection *conn) {
|
||||||
|
std::string s = "";
|
||||||
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
|
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
|
||||||
mg_printf(conn, "<html><body>");
|
mg_printf(conn, "<html><body>");
|
||||||
mg_printf(conn, "<h2>This is the A handler!!!</h2>");
|
mg_printf(conn, "<h2>This is the A handler for \"%s\" !</h2>", method);
|
||||||
|
if (CivetServer::getParam(conn, "param", s)) {
|
||||||
|
mg_printf(conn, "<p>param set to %s</p>", s.c_str());
|
||||||
|
} else {
|
||||||
|
mg_printf(conn, "<p>param not set</p>");
|
||||||
|
}
|
||||||
mg_printf(conn, "</body></html>\n");
|
mg_printf(conn, "</body></html>\n");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
public:
|
||||||
|
bool handleGet(CivetServer *server, struct mg_connection *conn) {
|
||||||
|
return handleAll("GET", server, conn);
|
||||||
|
}
|
||||||
|
bool handlePost(CivetServer *server, struct mg_connection *conn) {
|
||||||
|
return handleAll("POST", server, conn);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class ABHandler: public CivetHandler
|
class ABHandler: public CivetHandler
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/*
|
/* Copyright (c) 2013-2014 the Civetweb developers
|
||||||
* Copyright (c) 2013 No Face Press, LLC
|
* Copyright (c) 2013 No Face Press, LLC
|
||||||
|
*
|
||||||
* License http://opensource.org/licenses/mit-license.php MIT License
|
* License http://opensource.org/licenses/mit-license.php MIT License
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -272,6 +273,7 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
|
|
||||||
struct mg_context *context;
|
struct mg_context *context;
|
||||||
|
char * postData;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
@ -285,6 +287,20 @@ private:
|
|||||||
*/
|
*/
|
||||||
static int requestHandler(struct mg_connection *conn, void *cbdata);
|
static int requestHandler(struct mg_connection *conn, void *cbdata);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* closeHandler(struct mg_connection *)
|
||||||
|
*
|
||||||
|
* Handles closing a request (internal handler)
|
||||||
|
*
|
||||||
|
* @param conn - the connection information
|
||||||
|
*/
|
||||||
|
static void closeHandler(struct mg_connection *conn);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores the user provided close handler
|
||||||
|
*/
|
||||||
|
void (*userCloseHandler)(struct mg_connection *conn);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* __cplusplus */
|
#endif /* __cplusplus */
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/*
|
/* Copyright (c) 2013-2014 the Civetweb developers
|
||||||
* Copyright (c) 2013 No Face Press, LLC
|
* Copyright (c) 2013 No Face Press, LLC
|
||||||
|
*
|
||||||
* License http://opensource.org/licenses/mit-license.php MIT License
|
* License http://opensource.org/licenses/mit-license.php MIT License
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -44,7 +45,10 @@ bool CivetHandler::handleDelete(CivetServer *server, struct mg_connection *conn)
|
|||||||
int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
|
int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
|
||||||
{
|
{
|
||||||
struct mg_request_info *request_info = mg_get_request_info(conn);
|
struct mg_request_info *request_info = mg_get_request_info(conn);
|
||||||
|
assert(request_info != NULL);
|
||||||
CivetServer *me = (CivetServer*) (request_info->user_data);
|
CivetServer *me = (CivetServer*) (request_info->user_data);
|
||||||
|
assert(me != NULL);
|
||||||
|
|
||||||
CivetHandler *handler = (CivetHandler *)cbdata;
|
CivetHandler *handler = (CivetHandler *)cbdata;
|
||||||
|
|
||||||
if (handler) {
|
if (handler) {
|
||||||
@ -60,22 +64,23 @@ int CivetServer::requestHandler(struct mg_connection *conn, void *cbdata)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return 0; // No handler found
|
return 0; // No handler found
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CivetServer::CivetServer(const char **options,
|
CivetServer::CivetServer(const char **options,
|
||||||
const struct mg_callbacks *_callbacks) :
|
const struct mg_callbacks *_callbacks) :
|
||||||
context(0)
|
context(0), postData(0)
|
||||||
{
|
{
|
||||||
|
struct mg_callbacks callbacks;
|
||||||
|
memset(&callbacks, 0, sizeof(callbacks));
|
||||||
if (_callbacks) {
|
if (_callbacks) {
|
||||||
context = mg_start(_callbacks, this, options);
|
callbacks = *_callbacks;
|
||||||
|
userCloseHandler = _callbacks->connection_close;
|
||||||
} else {
|
} else {
|
||||||
struct mg_callbacks callbacks;
|
userCloseHandler = NULL;
|
||||||
memset(&callbacks, 0, sizeof(callbacks));
|
|
||||||
context = mg_start(&callbacks, this, options);
|
|
||||||
}
|
}
|
||||||
|
callbacks.connection_close = closeHandler;
|
||||||
|
|
||||||
|
context = mg_start(&callbacks, this, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
CivetServer::~CivetServer()
|
CivetServer::~CivetServer()
|
||||||
@ -83,6 +88,20 @@ CivetServer::~CivetServer()
|
|||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CivetServer::closeHandler(struct mg_connection *conn)
|
||||||
|
{
|
||||||
|
struct mg_request_info *request_info = mg_get_request_info(conn);
|
||||||
|
assert(request_info != NULL);
|
||||||
|
CivetServer *me = (CivetServer*) (request_info->user_data);
|
||||||
|
assert(me != NULL);
|
||||||
|
|
||||||
|
if (me->userCloseHandler) me->userCloseHandler(conn);
|
||||||
|
if (me->postData) {
|
||||||
|
delete [] (me->postData);
|
||||||
|
me->postData = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void CivetServer::addHandler(const std::string &uri, CivetHandler *handler)
|
void CivetServer::addHandler(const std::string &uri, CivetHandler *handler)
|
||||||
{
|
{
|
||||||
mg_set_request_handler(context, uri.c_str(), requestHandler, handler);
|
mg_set_request_handler(context, uri.c_str(), requestHandler, handler);
|
||||||
@ -151,14 +170,33 @@ CivetServer::getParam(struct mg_connection *conn, const char *name,
|
|||||||
std::string &dst, size_t occurrence)
|
std::string &dst, size_t occurrence)
|
||||||
{
|
{
|
||||||
const char *formParams = NULL;
|
const char *formParams = NULL;
|
||||||
mg_request_info * ri = mg_get_request_info(conn);
|
struct mg_request_info *ri = mg_get_request_info(conn);
|
||||||
|
assert(ri != NULL);
|
||||||
|
CivetServer *me = (CivetServer*) (ri->user_data);
|
||||||
|
assert(me != NULL);
|
||||||
|
|
||||||
if (ri) {
|
if (me->postData != NULL) {
|
||||||
|
formParams = me->postData;
|
||||||
|
} else {
|
||||||
|
const char * con_len_str = mg_get_header(conn, "Content-Length");
|
||||||
|
if (con_len_str) {
|
||||||
|
unsigned long con_len = atoi(con_len_str);
|
||||||
|
if (con_len>0) {
|
||||||
|
me->postData = new char[con_len];
|
||||||
|
if (me->postData != NULL) {
|
||||||
|
/* NULL check is not required according to current C++ standard */
|
||||||
|
mg_read(conn, me->postData, con_len);
|
||||||
|
formParams = me->postData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (formParams == NULL) {
|
||||||
// get requests do store html <form> field values in the http query_string
|
// get requests do store html <form> field values in the http query_string
|
||||||
formParams = ri->query_string;
|
formParams = ri->query_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (formParams) {
|
if (formParams != NULL) {
|
||||||
return getParam(formParams, strlen(formParams), name, dst, occurrence);
|
return getParam(formParams, strlen(formParams), name, dst, occurrence);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user