Extracted HTTP::parseVars function from HTTP::Parser, for easier reuse.
This commit is contained in:
parent
5b2ab2453f
commit
6d33ba2cbd
2 changed files with 16 additions and 9 deletions
|
@ -447,7 +447,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
||||||
tmpA.erase(0, f + 1);
|
tmpA.erase(0, f + 1);
|
||||||
method = tmpA;
|
method = 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), vars); //parse GET variables
|
||||||
url.erase(url.find('?'));
|
url.erase(url.find('?'));
|
||||||
}
|
}
|
||||||
url = Encodings::URL::decode(url);
|
url = Encodings::URL::decode(url);
|
||||||
|
@ -463,7 +463,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
||||||
tmpA.erase(0, f + 1);
|
tmpA.erase(0, f + 1);
|
||||||
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), vars); //parse GET variables
|
||||||
url.erase(url.find('?'));
|
url.erase(url.find('?'));
|
||||||
}
|
}
|
||||||
url = Encodings::URL::decode(url);
|
url = Encodings::URL::decode(url);
|
||||||
|
@ -508,7 +508,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
||||||
HTTPbuffer.erase(0, toappend);
|
HTTPbuffer.erase(0, toappend);
|
||||||
}
|
}
|
||||||
if (length == body.length()) {
|
if (length == body.length()) {
|
||||||
parseVars(body); //parse POST variables
|
parseVars(body, vars); //parse POST variables
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -560,12 +560,12 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
||||||
return false; //empty input
|
return false; //empty input
|
||||||
} //HTTPReader::parse
|
} //HTTPReader::parse
|
||||||
|
|
||||||
/// Parses GET or POST-style variable data.
|
///HTTP variable parser to std::map<std::string, std::string> structure.
|
||||||
/// Saves to internal variable structure using HTTP::Parser::SetVar.
|
///Reads variables from data, decodes and stores them to storage.
|
||||||
void HTTP::Parser::parseVars(std::string data) {
|
void HTTP::parseVars(const std::string & data, std::map<std::string, std::string> & storage) {
|
||||||
std::string varname;
|
std::string varname;
|
||||||
std::string varval;
|
std::string varval;
|
||||||
// position where a part start (e.g. after &)
|
// position where a part starts (e.g. after &)
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
while (pos < data.length()) {
|
while (pos < data.length()) {
|
||||||
size_t nextpos = data.find('&', pos);
|
size_t nextpos = data.find('&', pos);
|
||||||
|
@ -582,7 +582,9 @@ void HTTP::Parser::parseVars(std::string data) {
|
||||||
varname = data.substr(pos, nextpos - pos);
|
varname = data.substr(pos, nextpos - pos);
|
||||||
varval.clear();
|
varval.clear();
|
||||||
}
|
}
|
||||||
SetVar(Encodings::URL::decode(varname), Encodings::URL::decode(varval));
|
if (varname.size()){
|
||||||
|
storage[Encodings::URL::decode(varname)] = Encodings::URL::decode(varval);
|
||||||
|
}
|
||||||
if (nextpos == std::string::npos) {
|
if (nextpos == std::string::npos) {
|
||||||
// in case the string is gigantic
|
// in case the string is gigantic
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -10,6 +10,12 @@
|
||||||
|
|
||||||
/// Holds all HTTP processing related code.
|
/// Holds all HTTP processing related code.
|
||||||
namespace HTTP {
|
namespace HTTP {
|
||||||
|
|
||||||
|
///HTTP variable parser to std::map<std::string, std::string> structure.
|
||||||
|
///Reads variables from data, decodes and stores them to storage.
|
||||||
|
void parseVars(const std::string & data, std::map<std::string, std::string> & storage);
|
||||||
|
|
||||||
|
|
||||||
/// Simple class for reading and writing HTTP 1.0 and 1.1.
|
/// Simple class for reading and writing HTTP 1.0 and 1.1.
|
||||||
class Parser {
|
class Parser {
|
||||||
public:
|
public:
|
||||||
|
@ -56,7 +62,6 @@ namespace HTTP {
|
||||||
bool getChunks;
|
bool getChunks;
|
||||||
unsigned int doingChunk;
|
unsigned int doingChunk;
|
||||||
bool parse(std::string & HTTPbuffer);
|
bool parse(std::string & HTTPbuffer);
|
||||||
void parseVars(std::string data);
|
|
||||||
std::string builder;
|
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;
|
||||||
|
|
Loading…
Add table
Reference in a new issue