Improvements to HTTP::Parser - now builds responses and requests by reference and has getUrl() method to get the URL without URI-parameters.
This commit is contained in:
parent
4a940f7514
commit
c6bb67c4b9
2 changed files with 24 additions and 14 deletions
|
@ -24,18 +24,18 @@ void HTTP::Parser::Clean(){
|
||||||
/// The request is build from internal variables set before this call is made.
|
/// The request is build from internal variables set before this call is made.
|
||||||
/// To be precise, method, url, protocol, headers and body are used.
|
/// 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.
|
/// \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?
|
/// \todo Include GET/POST variable parsing?
|
||||||
std::map<std::string, std::string>::iterator it;
|
std::map<std::string, std::string>::iterator it;
|
||||||
if (protocol.size() < 5 || protocol.substr(0, 4) != "HTTP"){protocol = "HTTP/1.0";}
|
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++){
|
for (it=headers.begin(); it != headers.end(); it++){
|
||||||
if ((*it).first != "" && (*it).second != ""){
|
if ((*it).first != "" && (*it).second != ""){
|
||||||
tmp += (*it).first + ": " + (*it).second + "\n";
|
builder += (*it).first + ": " + (*it).second + "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tmp += "\n" + body;
|
builder += "\n" + body;
|
||||||
return tmp;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a string containing a valid HTTP 1.0 or 1.1 response, ready for sending.
|
/// 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 code The HTTP response code. Usually you want 200.
|
||||||
/// \param message The HTTP response message. Usually you want "OK".
|
/// \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.
|
/// \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?
|
/// \todo Include GET/POST variable parsing?
|
||||||
std::map<std::string, std::string>::iterator it;
|
std::map<std::string, std::string>::iterator it;
|
||||||
if (protocol.size() < 5 || protocol.substr(0, 4) != "HTTP"){protocol = "HTTP/1.0";}
|
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++){
|
for (it=headers.begin(); it != headers.end(); it++){
|
||||||
if ((*it).first != "" && (*it).second != ""){
|
if ((*it).first != "" && (*it).second != ""){
|
||||||
if ((*it).first != "Content-Length" || (*it).second != "0"){
|
if ((*it).first != "Content-Length" || (*it).second != "0"){
|
||||||
tmp += (*it).first + ": " + (*it).second + "\n";
|
builder += (*it).first + ": " + (*it).second + "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tmp += "\n";
|
builder += "\n";
|
||||||
tmp += body;
|
builder += body;
|
||||||
return tmp;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trims any whitespace at the front or back of the string.
|
/// 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);
|
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.
|
/// Returns header i, if set.
|
||||||
std::string HTTP::Parser::GetHeader(std::string i){return headers[i];}
|
std::string HTTP::Parser::GetHeader(std::string i){return headers[i];}
|
||||||
/// Returns POST variable i, if set.
|
/// Returns POST variable i, if set.
|
||||||
|
@ -150,7 +159,6 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer){
|
||||||
protocol = tmpA;
|
protocol = tmpA;
|
||||||
if (url.find('?') != std::string::npos){
|
if (url.find('?') != std::string::npos){
|
||||||
parseVars(url.substr(url.find('?')+1)); //parse GET variables
|
parseVars(url.substr(url.find('?')+1)); //parse GET variables
|
||||||
url.resize(url.find('?'));
|
|
||||||
}
|
}
|
||||||
}else{seenReq = false;}
|
}else{seenReq = false;}
|
||||||
}else{seenReq = false;}
|
}else{seenReq = false;}
|
||||||
|
|
|
@ -16,13 +16,14 @@ namespace HTTP{
|
||||||
bool Read(std::string & strbuf);
|
bool Read(std::string & strbuf);
|
||||||
std::string GetHeader(std::string i);
|
std::string GetHeader(std::string i);
|
||||||
std::string GetVar(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, std::string v);
|
||||||
void SetHeader(std::string i, int v);
|
void SetHeader(std::string i, int v);
|
||||||
void SetVar(std::string i, std::string v);
|
void SetVar(std::string i, std::string v);
|
||||||
void SetBody(std::string s);
|
void SetBody(std::string s);
|
||||||
void SetBody(char * buffer, int len);
|
void SetBody(char * buffer, int len);
|
||||||
std::string BuildRequest();
|
std::string & BuildRequest();
|
||||||
std::string BuildResponse(std::string code, std::string message);
|
std::string & BuildResponse(std::string code, std::string message);
|
||||||
void Chunkify(std::string & bodypart);
|
void Chunkify(std::string & bodypart);
|
||||||
void Clean();
|
void Clean();
|
||||||
static std::string urlunescape(const std::string & in);
|
static std::string urlunescape(const std::string & in);
|
||||||
|
@ -37,6 +38,7 @@ namespace HTTP{
|
||||||
bool seenReq;
|
bool seenReq;
|
||||||
bool parse(std::string & HTTPbuffer);
|
bool parse(std::string & HTTPbuffer);
|
||||||
void parseVars(std::string data);
|
void parseVars(std::string data);
|
||||||
|
std::string builder;
|
||||||
std::string read_buffer;
|
std::string read_buffer;
|
||||||
std::map<std::string, std::string> headers;
|
std::map<std::string, std::string> headers;
|
||||||
std::map<std::string, std::string> vars;
|
std::map<std::string, std::string> vars;
|
||||||
|
|
Loading…
Add table
Reference in a new issue