From a0bcc4779e7088d59e71a28a346cb3b364489d43 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Fri, 31 Aug 2012 16:53:46 +0200 Subject: [PATCH] Improvements to HTTP::Parser - now builds responses and requests by reference and has getUrl() method to get the URL without URI-parameters. --- lib/http_parser.cpp | 32 ++++++++++++++++++++------------ lib/http_parser.h | 6 ++++-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/lib/http_parser.cpp b/lib/http_parser.cpp index 2047be6d..526ff014 100644 --- a/lib/http_parser.cpp +++ b/lib/http_parser.cpp @@ -24,18 +24,18 @@ void HTTP::Parser::Clean(){ /// The request is build from internal variables set before this call is made. /// To be precise, method, url, protocol, headers and body are used. /// \return A string containing a valid HTTP 1.0 or 1.1 request, ready for sending. -std::string HTTP::Parser::BuildRequest(){ +std::string & HTTP::Parser::BuildRequest(){ /// \todo Include GET/POST variable parsing? std::map::iterator it; if (protocol.size() < 5 || protocol.substr(0, 4) != "HTTP"){protocol = "HTTP/1.0";} - std::string tmp = method+" "+url+" "+protocol+"\n"; + builder = method+" "+url+" "+protocol+"\n"; for (it=headers.begin(); it != headers.end(); it++){ if ((*it).first != "" && (*it).second != ""){ - tmp += (*it).first + ": " + (*it).second + "\n"; + builder += (*it).first + ": " + (*it).second + "\n"; } } - tmp += "\n" + body; - return tmp; + builder += "\n" + body; + return builder; } /// Returns a string containing a valid HTTP 1.0 or 1.1 response, ready for sending. @@ -44,21 +44,21 @@ std::string HTTP::Parser::BuildRequest(){ /// \param code The HTTP response code. Usually you want 200. /// \param message The HTTP response message. Usually you want "OK". /// \return A string containing a valid HTTP 1.0 or 1.1 response, ready for sending. -std::string HTTP::Parser::BuildResponse(std::string code, std::string message){ +std::string & HTTP::Parser::BuildResponse(std::string code, std::string message){ /// \todo Include GET/POST variable parsing? std::map::iterator it; if (protocol.size() < 5 || protocol.substr(0, 4) != "HTTP"){protocol = "HTTP/1.0";} - std::string tmp = protocol+" "+code+" "+message+"\n"; + builder = protocol+" "+code+" "+message+"\n"; for (it=headers.begin(); it != headers.end(); it++){ if ((*it).first != "" && (*it).second != ""){ if ((*it).first != "Content-Length" || (*it).second != "0"){ - tmp += (*it).first + ": " + (*it).second + "\n"; + builder += (*it).first + ": " + (*it).second + "\n"; } } } - tmp += "\n"; - tmp += body; - return tmp; + builder += "\n"; + builder += body; + return builder; } /// Trims any whitespace at the front or back of the string. @@ -86,6 +86,15 @@ void HTTP::Parser::SetBody(char * buffer, int len){ SetHeader("Content-Length", len); } +/// Returns header i, if set. +std::string HTTP::Parser::getUrl(){ + if (url.find('?') != std::string::npos){ + return url.substr(0, url.find('?')); + }else{ + return url; + } +} + /// Returns header i, if set. std::string HTTP::Parser::GetHeader(std::string i){return headers[i];} /// Returns POST variable i, if set. @@ -150,7 +159,6 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer){ protocol = tmpA; if (url.find('?') != std::string::npos){ parseVars(url.substr(url.find('?')+1)); //parse GET variables - url.resize(url.find('?')); } }else{seenReq = false;} }else{seenReq = false;} diff --git a/lib/http_parser.h b/lib/http_parser.h index 609e7d28..ae235a2e 100644 --- a/lib/http_parser.h +++ b/lib/http_parser.h @@ -16,13 +16,14 @@ namespace HTTP{ bool Read(std::string & strbuf); std::string GetHeader(std::string i); std::string GetVar(std::string i); + std::string getUrl(); void SetHeader(std::string i, std::string v); void SetHeader(std::string i, int v); void SetVar(std::string i, std::string v); void SetBody(std::string s); void SetBody(char * buffer, int len); - std::string BuildRequest(); - std::string BuildResponse(std::string code, std::string message); + std::string & BuildRequest(); + std::string & BuildResponse(std::string code, std::string message); void Chunkify(std::string & bodypart); void Clean(); static std::string urlunescape(const std::string & in); @@ -37,6 +38,7 @@ namespace HTTP{ bool seenReq; bool parse(std::string & HTTPbuffer); void parseVars(std::string data); + std::string builder; std::string read_buffer; std::map headers; std::map vars;