From d7c7c961e8c35c2d8587ec7f60aac5e309284a97 Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 13 Nov 2009 11:00:08 +0000 Subject: Added SessionIDs to http communication. Made SAXParser run on const char*. --- client/netcom.cc | 32 ++++++++++++++++++++++--- client/netcom.h | 1 + server/src/macroheaderparser.cc | 2 +- server/src/macroheaderparser.h | 2 +- server/src/macroparser.cc | 2 +- server/src/macroparser.h | 2 +- server/src/saxparser.cc | 6 ++--- server/src/saxparser.h | 4 ++-- server/src/server.cc | 48 ++++++++++++++++++++++++++++++-------- server/src/templateheaderparser.cc | 2 +- server/src/templateheaderparser.h | 2 +- server/src/templateparser.cc | 2 +- server/src/templateparser.h | 2 +- server/src/transactionparser.cc | 2 +- server/src/transactionparser.h | 2 +- 15 files changed, 83 insertions(+), 28 deletions(-) diff --git a/client/netcom.cc b/client/netcom.cc index be240e2..4126b02 100644 --- a/client/netcom.cc +++ b/client/netcom.cc @@ -62,12 +62,34 @@ NetCom::NetCom(QString host, quint16 port, QString user, QString cpr) #else http.setHost(host, QHttp::ConnectionModeHttp, port); #endif - + transfering = false; + + QHttpRequestHeader h("HTTP 1.1", "/"); + http.request(h, ""); + + transfering = true; + buffer = ""; + do { + qApp->processEvents(QEventLoop::WaitForMoreEvents); + } while(transfering); + + sessionid = http.lastResponse().value("SessionID"); + printf("SESSION ID: %s\n", sessionid.toStdString().c_str()); } NetCom::~NetCom() { + QHttpRequestHeader h("HTTP 1.1", "/"); + h.setValue("SessionCommit", ""); + h.setValue("SessionID", sessionid); + http.request(h, ""); + + transfering = true; + buffer = ""; + do { + qApp->processEvents(QEventLoop::WaitForMoreEvents); + } while(transfering); } QDomDocument NetCom::send(QString templ, QString macro, bool lockgui) @@ -93,7 +115,9 @@ QDomDocument NetCom::send(QString templ, QString macro, bool lockgui) printf("\nSending request:\n%s", doc.toString().toStdString().c_str()); - http.post("/", doc.toByteArray()); + QHttpRequestHeader h("HTTP 1.1", "/"); + h.setValue("SessionID", sessionid); + http.request(h, doc.toByteArray()); QDomDocument res_doc; transfering = true; @@ -151,7 +175,9 @@ void NetCom::send(QVector< Widget* > widgets, QString templ, QString macro, QStr printf("\nSending commit:\n%s", doc.toString().toStdString().c_str()); - http.post("/", doc.toByteArray()); + QHttpRequestHeader h("HTTP 1.1", "/"); + h.setValue("SessionID", sessionid); + http.request(h, doc.toByteArray()); transfering = true; buffer = ""; diff --git a/client/netcom.h b/client/netcom.h index f1a8766..718d5a6 100644 --- a/client/netcom.h +++ b/client/netcom.h @@ -64,6 +64,7 @@ private: QHttp http; QByteArray buffer; + QString sessionid; }; #endif/*__PRACRO_NETCOM_H__*/ diff --git a/server/src/macroheaderparser.cc b/server/src/macroheaderparser.cc index 6193e94..48ed381 100644 --- a/server/src/macroheaderparser.cc +++ b/server/src/macroheaderparser.cc @@ -121,7 +121,7 @@ int MacroHeaderParser::readData(char *data, size_t size) return r; } -void MacroHeaderParser::parseError(char *buf, size_t len, std::string error, int lineno) +void MacroHeaderParser::parseError(const char *buf, size_t len, std::string error, int lineno) { if(m) return; // Ignore "unclosed token" errors when the macro tag has been parsed. diff --git a/server/src/macroheaderparser.h b/server/src/macroheaderparser.h index 0bc58ed..a4bb8bc 100644 --- a/server/src/macroheaderparser.h +++ b/server/src/macroheaderparser.h @@ -65,7 +65,7 @@ public: /** * Overloaded parser callback method. */ - void parseError(char *buf, size_t len, std::string error, int lineno); + void parseError(const char *buf, size_t len, std::string error, int lineno); /** * Get a pointer to the parsed macro. diff --git a/server/src/macroparser.cc b/server/src/macroparser.cc index 0b0257e..863c52e 100644 --- a/server/src/macroparser.cc +++ b/server/src/macroparser.cc @@ -304,7 +304,7 @@ int MacroParser::readData(char *data, size_t size) return r; } -void MacroParser::parseError(char *buf, size_t len, std::string error, int lineno) +void MacroParser::parseError(const char *buf, size_t len, std::string error, int lineno) { PRACRO_ERR_LOG(macro, "MacroParser[%s] error at line %d: %s\n", file.c_str(), lineno, error.c_str()); PRACRO_ERR_LOG(macro, "\tBuffer %u bytes: [", len); diff --git a/server/src/macroparser.h b/server/src/macroparser.h index a283d26..a2a144c 100644 --- a/server/src/macroparser.h +++ b/server/src/macroparser.h @@ -51,7 +51,7 @@ public: void characterData(std::string &data); void startTag(std::string name, std::map< std::string, std::string> attributes); void endTag(std::string name); - void parseError(char *buf, size_t len, std::string error, int lineno); + void parseError(const char *buf, size_t len, std::string error, int lineno); /** * Get a pointer to the parsed macro. diff --git a/server/src/saxparser.cc b/server/src/saxparser.cc index 8455593..1f808b1 100644 --- a/server/src/saxparser.cc +++ b/server/src/saxparser.cc @@ -113,7 +113,7 @@ int SAXParser::parse() return 0; } -static bool iswhitespace(char *buf, size_t size) +static bool iswhitespace(const char *buf, size_t size) { for(size_t i = 0; i < size; i++) if(buf[i] != ' ' && buf[i] != '\n' && buf[i] != '\t' && buf[i] != '\r') @@ -121,7 +121,7 @@ static bool iswhitespace(char *buf, size_t size) return true; } -bool SAXParser::parse(char *data, size_t size) +bool SAXParser::parse(const char *data, size_t size) { PRACRO_DEBUG(sax, "parse %d bytes\n", size); @@ -151,7 +151,7 @@ bool SAXParser::parse(char *data, size_t size) return done; } -void SAXParser::parseError(char *buf, size_t len, std::string error, int lineno) +void SAXParser::parseError(const char *buf, size_t len, std::string error, int lineno) { fprintf(stderr, "SAXParser error at line %d: %s\n", lineno, error.c_str()); fprintf(stderr, "\tBuffer %u bytes: [", len); diff --git a/server/src/saxparser.h b/server/src/saxparser.h index 9f2faa2..265727f 100644 --- a/server/src/saxparser.h +++ b/server/src/saxparser.h @@ -95,7 +95,7 @@ public: * @param error A std::string containing the error message. * @param lineno An integer containing the line number on which the error occurred. */ - virtual void parseError(char *buf, size_t len, std::string error, int lineno); + virtual void parseError(const char *buf, size_t len, std::string error, int lineno); /** * Buffer parse method. @@ -107,7 +107,7 @@ public: * false otherwise. * @see bool parse(char *buf, size_t size) */ - bool parse(char *buf, size_t size); + bool parse(const char *buf, size_t size); /** * Get the number of bytes used from the last buffer. diff --git a/server/src/server.cc b/server/src/server.cc index 67ae74d..d1104c4 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -60,9 +60,12 @@ #include "templatelist.h" #include "versionstr.h" +typedef long long unsigned int sessionid_t; + struct conn_t { Database *db; - + sessionid_t sessionid; + bool commit; }; static std::string error_box(std::string message) @@ -88,7 +91,8 @@ public: }; -static std::string handleCommits(Transaction *transaction, Database &db, +static std::string handleCommits(sessionid_t sessiondid, + Transaction *transaction, Database &db, JournalWriter &journalwriter, MacroList ¯olist, TemplateList &templatelist) { @@ -125,7 +129,6 @@ static std::string handleCommits(Transaction *transaction, Database &db, static std::string handleRequest(Transaction *transaction, TCPSocket &pentominos_socket, Database &db, - JournalWriter &journalwriter, MacroList ¯olist, TemplateList &templatelist) { @@ -267,7 +270,8 @@ static std::string handleRequest(Transaction *transaction, return answer; } -static std::string handleTransaction(Transaction *transaction, +static std::string handleTransaction(sessionid_t sessionid, + Transaction *transaction, TCPSocket &pentominos_socket, Database &db, JournalWriter &journalwriter, @@ -279,15 +283,15 @@ static std::string handleTransaction(Transaction *transaction, answer += "\n"; try { - answer += handleCommits(transaction, db, journalwriter, macrolist, templatelist); + answer += handleCommits(sessionid, transaction, db, + journalwriter, macrolist, templatelist); } catch( std::exception &e ) { PRACRO_ERR(server, "Commit error: %s\n", e.what()); return error_box(xml_encode(e.what())); } try { - answer += handleRequest(transaction, pentominos_socket, db, journalwriter, - macrolist, templatelist); + answer += handleRequest(transaction, pentominos_socket, db, macrolist, templatelist); } catch( std::exception &e ) { PRACRO_ERR(server, "Request error: %s\n", e.what()); return error_box(xml_encode(e.what())); @@ -301,7 +305,7 @@ static std::string handleTransaction(Transaction *transaction, } -static std::string handleConnection(char *buf, size_t size, struct conn_t *conn) +static std::string handleConnection(const char *buf, size_t size, struct conn_t *conn) { if(size == 0) return error_box(xml_encode("Empty document received.")); @@ -324,7 +328,7 @@ static std::string handleConnection(char *buf, size_t size, struct conn_t *conn) if(parser.parse(buf, size)) { PRACRO_DEBUG(server, "Got complete XML document, %d bytes in current buffer.\n", size); - std::string res = handleTransaction(&transaction, pentominos_socket, + std::string res = handleTransaction(conn->sessionid, &transaction, pentominos_socket, *conn->db, journalwriter, macrolist, templatelist); journalwriter.commit(); @@ -335,6 +339,12 @@ static std::string handleConnection(char *buf, size_t size, struct conn_t *conn) return error_box(xml_encode("XML Parse error.")); } +static sessionid_t newSessionID() +{ + static volatile sessionid_t sessionid = 0; + return sessionid++; +} + static int handle_request(void *cls, struct MHD_Connection *con, const char *url, @@ -351,11 +361,29 @@ static int handle_request(void *cls, " version=\"%s\", data_size=\"%d\")\n", url, method, version, *data_size); - std::string reply = handleConnection((char*)data, *data_size, conn); + const char *sessionids = MHD_lookup_connection_value(con, MHD_HEADER_KIND, "SessionID"); + if(sessionids == NULL) conn->sessionid = newSessionID(); + else conn->sessionid = atoll(sessionids); + printf("SessionID: %llu\n", conn->sessionid); + + const char *session_commit = MHD_lookup_connection_value(con, MHD_HEADER_KIND, "SessionCommit"); + if(session_commit) { + printf("COMMIT: sessionid %llu\n", conn->sessionid); + conn->commit = true; + } else { + conn->commit = false; + } + + std::string reply = handleConnection(data, *data_size, conn); struct MHD_Response *rsp; rsp = MHD_create_response_from_data(reply.length(), (char*)reply.c_str(), MHD_NO, MHD_YES); MHD_add_response_header(rsp, MHD_HTTP_HEADER_CONTENT_TYPE, "text/plain; charset=UTF-8"); + + char idbuf[32]; + snprintf(idbuf, sizeof(idbuf), "%llu", conn->sessionid); + MHD_add_response_header(rsp, "SessionID", idbuf); + int ret = MHD_queue_response(con, MHD_HTTP_OK, rsp); MHD_destroy_response(rsp); diff --git a/server/src/templateheaderparser.cc b/server/src/templateheaderparser.cc index 0a68a6a..848b05c 100644 --- a/server/src/templateheaderparser.cc +++ b/server/src/templateheaderparser.cc @@ -121,7 +121,7 @@ int TemplateHeaderParser::readData(char *data, size_t size) return r; } -void TemplateHeaderParser::parseError(char *buf, size_t len, std::string error, int lineno) +void TemplateHeaderParser::parseError(const char *buf, size_t len, std::string error, int lineno) { if(t) return; // Ignore "unclosed token" errors when the template tag has been parsed. diff --git a/server/src/templateheaderparser.h b/server/src/templateheaderparser.h index e517f67..ce63180 100644 --- a/server/src/templateheaderparser.h +++ b/server/src/templateheaderparser.h @@ -65,7 +65,7 @@ public: /** * Overloaded parser callback method. */ - void parseError(char *buf, size_t len, std::string error, int lineno); + void parseError(const char *buf, size_t len, std::string error, int lineno); /** * Get a pointer to the parsed template. diff --git a/server/src/templateparser.cc b/server/src/templateparser.cc index 277a8bd..e6a0f2e 100644 --- a/server/src/templateparser.cc +++ b/server/src/templateparser.cc @@ -137,7 +137,7 @@ int TemplateParser::readData(char *data, size_t size) return r; } -void TemplateParser::parseError(char *buf, size_t len, std::string error, int lineno) +void TemplateParser::parseError(const char *buf, size_t len, std::string error, int lineno) { fprintf(stderr, "TemplateParser[%s] error at line %d: %s\n", file.c_str(), lineno, error.c_str()); fprintf(stderr, "\tBuffer %u bytes: [", len); diff --git a/server/src/templateparser.h b/server/src/templateparser.h index 61f4d0b..51e24a7 100644 --- a/server/src/templateparser.h +++ b/server/src/templateparser.h @@ -44,7 +44,7 @@ public: void characterData(std::string &data); void startTag(std::string name, std::map< std::string, std::string> attributes); void endTag(std::string name); - void parseError(char *buf, size_t len, std::string error, int lineno); + void parseError(const char *buf, size_t len, std::string error, int lineno); Template *getTemplate(); diff --git a/server/src/transactionparser.cc b/server/src/transactionparser.cc index d4f7c76..f1f79c6 100644 --- a/server/src/transactionparser.cc +++ b/server/src/transactionparser.cc @@ -99,7 +99,7 @@ void TransactionParser::startTag(std::string name, std::map< std::string, std::s } } -void TransactionParser::parseError(char *buf, size_t len, std::string error, int lineno) +void TransactionParser::parseError(const char *buf, size_t len, std::string error, int lineno) { PRACRO_ERR_LOG(transactionparser, "TransactionParser error at line %d: %s\n", lineno, error.c_str()); diff --git a/server/src/transactionparser.h b/server/src/transactionparser.h index 385e1a5..4d54a5a 100644 --- a/server/src/transactionparser.h +++ b/server/src/transactionparser.h @@ -57,7 +57,7 @@ public: * Parser error callback method. Unlike its parent class, this method throws * an exception. */ - void parseError(char *buf, size_t len, std::string error, int lineno); + void parseError(const char *buf, size_t len, std::string error, int lineno); private: Transaction *transaction; -- cgit v1.2.3