Fortify HTTP parser/generator code.

This commit is contained in:
root 2012-08-06 05:35:11 +02:00
parent d1e2132879
commit 4641efb79d

View file

@ -27,11 +27,12 @@ void HTTP::Parser::Clean(){
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";}
std::string tmp = method+" "+url+" "+protocol+"\n"; std::string tmp = method+" "+url+" "+protocol+"\n";
for (it=headers.begin(); it != headers.end(); it++){ for (it=headers.begin(); it != headers.end(); it++){
tmp += (*it).first + ": " + (*it).second + "\n"; tmp += (*it).first + ": " + (*it).second + "\n";
} }
tmp += "\n" + body + "\n"; tmp += "\n" + body;
return tmp; return tmp;
} }
@ -44,6 +45,7 @@ std::string HTTP::Parser::BuildRequest(){
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";}
std::string tmp = protocol+" "+code+" "+message+"\n"; std::string tmp = protocol+" "+code+" "+message+"\n";
for (it=headers.begin(); it != headers.end(); it++){ for (it=headers.begin(); it != headers.end(); it++){
tmp += (*it).first + ": " + (*it).second + "\n"; tmp += (*it).first + ": " + (*it).second + "\n";
@ -134,14 +136,17 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer){
if (!seenReq){ if (!seenReq){
seenReq = true; seenReq = true;
f = tmpA.find(' '); f = tmpA.find(' ');
if (f != std::string::npos){method = tmpA.substr(0, f); tmpA.erase(0, f+1);} if (f != std::string::npos){
method = tmpA.substr(0, f); tmpA.erase(0, f+1);
f = tmpA.find(' '); f = tmpA.find(' ');
if (f != std::string::npos){url = tmpA.substr(0, f); tmpA.erase(0, f+1);} if (f != std::string::npos){
f = tmpA.find(' '); url = tmpA.substr(0, f); tmpA.erase(0, f+1);
if (f != std::string::npos){protocol = tmpA.substr(0, f); tmpA.erase(0, f+1);} 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
} }
}else{seenReq = false;}
}else{seenReq = false;}
}else{ }else{
if (tmpA.size() == 0){ if (tmpA.size() == 0){
seenHeaders = true; seenHeaders = true;