summaryrefslogtreecommitdiff
path: root/server/src/server.cc
diff options
context:
space:
mode:
authordeva <deva>2010-05-07 12:36:13 +0000
committerdeva <deva>2010-05-07 12:36:13 +0000
commit016e4ba553044edee32c2a20ee34463ad82106e8 (patch)
tree8fdde7232ee847ba39eb94449b18fd3de7bb4fb8 /server/src/server.cc
parent965e43178736e6635cf27410e6d73f4ec0fdced2 (diff)
Use connection object instead of struct and make xml parsing on-the-fly instead of collecting all data first.
Diffstat (limited to 'server/src/server.cc')
-rw-r--r--server/src/server.cc169
1 files changed, 26 insertions, 143 deletions
diff --git a/server/src/server.cc b/server/src/server.cc
index 4283a11..118db85 100644
--- a/server/src/server.cc
+++ b/server/src/server.cc
@@ -41,84 +41,8 @@
#include <microhttpd.h>
#include "configuration.h"
-#include "transaction.h"
-#include "transactionparser.h"
-#include "database.h"
+#include "connection.h"
#include "log.h"
-#include "environment.h"
-#include "transactionhandler.h"
-#include "connectionpool.h"
-#include "session.h"
-#include "xml_encode_decode.h"
-
-static std::string error_box(std::string message)
-{
- std::string errorbox =
- "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
- "<pracro version=\"1.0\">\n"
- " <error>" + message + "</error>\n"
- "</pracro>\n";
- return errorbox;
-}
-
-static std::string handleConnection(const std::string &data,
- Environment &env,
- std::string &sessionid,
- bool sessioncommit)
-{
- std::string res;
-
- Session *session = NULL;
- if(sessionid == "") {
- session = env.sessions.newSession();
- } else {
- session = env.sessions.session(sessionid);
- if(session == NULL) session = env.sessions.newSession();
- }
-
- if(session == NULL) {
- PRACRO_ERR(server, "New session could not be created.");
- return error_box(xml_encode("New session could not be created."));
- }
-
- sessionid = session->id();
-
- {
- SessionAutolock lock(*session);
-
- if(data.length()) {
- Transaction transaction;
- TransactionParser parser(&transaction);
-
- if(!parser.parse(data.c_str(), data.length())) {
- PRACRO_ERR(server, "Failed to parse data!\n");
- PRACRO_ERR(server, "DATA:[[%s]]\n", data.c_str());
- res = error_box(xml_encode("XML Parse error."));
- } else {
- res = handleTransaction(transaction, env, *session);
- }
- }
-
- }
-
- if(sessioncommit) {
- session->commit();
- env.sessions.deleteSession(session->id());
- }
-
- return res;
-}
-
-struct condata {
- std::map<std::string, std::string> headers;
- std::string data;
- size_t data_size;
- std::string url;
- std::string method;
- std::string version;
-};
-
-//static std::map<struct MHD_Connection *, struct condata> condata;
static int handle_request_callback(void *cls,
struct MHD_Connection *con,
@@ -129,92 +53,51 @@ static int handle_request_callback(void *cls,
unsigned int *data_size,
void **con_cls)
{
+ int ret = MHD_YES;
+
+ Connection *connection = (Connection*)*con_cls;
+
PRACRO_DEBUG(httpd, "handle_request_callback con:%p condata:%p\n",
con, *con_cls);
// Test for new connection
- if(*con_cls == NULL) {
- PRACRO_DEBUG(httpd,
- "handle_request(url=\"%s\", method=\"%s\","
- " version=\"%s\", data_size=\"%d\")\n",
- url, method, version, *data_size);
-
- struct condata *cd = new struct condata;
- cd->url = url;
- cd->method = method;
- cd->version = version;
- cd->data_size = 0;
- cd->data = "";
-
+ if(connection == NULL) {
+ std::string sessionid;
const char *sid = MHD_lookup_connection_value(con, MHD_HEADER_KIND,
"SessionID");
- if(sid) cd->headers["SessionID"] = sid;
+ if(sid) sessionid = sid;
const char *scm = MHD_lookup_connection_value(con, MHD_HEADER_KIND,
"SessionCommit");
- if(scm) cd->headers["SessionCommit"] = scm;
-
- const char *csz = MHD_lookup_connection_value(con, MHD_HEADER_KIND,
- "Content-Length");
- if(csz) {
- cd->headers["Content-Length"] = csz;
- cd->data_size = atol(csz);
- PRACRO_DEBUG(httpd, "Content-Length: %s (%d)\n", csz, cd->data_size);
- }
-
- *con_cls = cd;
+ Environment *env = (Environment *)cls;
+ connection = new Connection(*env, sessionid, scm != NULL);
+ *con_cls = connection;
}
- struct condata *cd = (struct condata*)*con_cls;
- cd->data.append(data, *data_size);
-
- int ret = MHD_YES;
+ if(!connection) return MHD_NO;
- PRACRO_DEBUG(httpd, "Waiting for %d bytes of data. Got %d (total %d)\n",
- cd->data_size, *data_size, cd->data.length());
-
- if(cd->data.length() >= cd->data_size) {
-
- Environment *env = (Environment *)cls;
-
- const char *sid = MHD_lookup_connection_value(con, MHD_HEADER_KIND,
- "SessionID");
- std::string sessionid;
- if(sid) sessionid = sid;
-
- const char *sessioncommit =
- MHD_lookup_connection_value(con, MHD_HEADER_KIND, "SessionCommit");
-
- PRACRO_DEBUG(httpd, "Starting to handle SessionID: %s%s\n",
- sessionid.c_str(),
- sessioncommit != NULL?" COMMIT":"");
-
- PRACRO_DEBUG(httpd, "Data: [%s]\n", cd->data.c_str());
-
- std::string reply =
- handleConnection(cd->data, *env, sessionid, sessioncommit != NULL);
+ if(connection->handle(data, *data_size)) {
+ std::string response = connection->getResponse();
+ PRACRO_DEBUG(httpd, "Sending response: [[%s]]\n", response.c_str());
+
struct MHD_Response *rsp =
- MHD_create_response_from_data(reply.length(), (char*)reply.c_str(),
- MHD_NO, MHD_YES);
+ MHD_create_response_from_data(response.size(),
+ (void*)response.data(),
+ MHD_NO, // must free
+ MHD_YES); // must copy
+
MHD_add_response_header(rsp, MHD_HTTP_HEADER_CONTENT_TYPE,
"text/plain; charset=UTF-8");
- if(sessionid != "") {
- MHD_add_response_header(rsp, "SessionID", sessionid.c_str());
- }
+ MHD_add_response_header(rsp, "SessionID",
+ connection->getSessionID().c_str());
ret = MHD_queue_response(con, MHD_HTTP_OK, rsp);
MHD_destroy_response(rsp);
- PRACRO_DEBUG(httpd, "Finished handling SessionID: %s%s\n",
- sessionid.c_str(),
- sessioncommit != NULL?" COMMIT":"");
-
-
- delete cd;
+ delete connection;
*con_cls = NULL;
-
}
*data_size = 0;
@@ -231,8 +114,8 @@ void requestCompletedCallback(void *cls,
// If connection was interrupted prematurely delete the content data here.
if(*con_cls) {
- struct condata *cd = (struct condata*)*con_cls;
- delete cd;
+ Connection *connection = (Connection*)*con_cls;
+ delete connection;
*con_cls = NULL;
}
}