if (buffer.size() < chunk_size+2 || buffer.at(chunk_size+1) != '\n') return false; // expect newline after carriage return
crlf=2;
} else if (buffer.at(chunk_size) != '\n') return false;
- if (bodybuf.str().length() + chunk_size > maxbody) {
+ if (bodysize + chunk_size > maxbody) {
throw ParseError("Chunked body is too large");
}
std::string tmp = buffer.substr(0, chunk_size);
buffer.erase(buffer.begin(), buffer.begin()+chunk_size+crlf);
bodybuf << tmp;
+ bodysize += chunk_size;
chunk_size = 0;
if (buffer.size() == 0) break; // just in case
}
} else {
- if (bodybuf.str().length() + buffer.length() > maxbody)
+ if (bodysize + buffer.length() > maxbody) {
bodybuf << buffer.substr(0, maxbody - bodybuf.str().length());
- else
+ bodysize = maxbody;
+ }
+ else {
bodybuf << buffer;
+ bodysize += buffer.length();
+ }
buffer = "";
}
}
std::ostringstream bodybuf; //<! buffer for body
size_t maxbody; //<! maximum size of body
size_t minbody; //<! minimum size of body
- size_t headersize;
+ size_t headersize;
+ size_t bodysize;
bool hasBody; //<! are we expecting body
void keyValuePair(const std::string &keyvalue, std::string &key, std::string &value); //<! key value pair parser helper
hasBody = false;
buffer = "";
headersize = 0;
+ bodysize = 0;
this->target->initialize();
}; //<! Initialize the parser for target and clear state
bool feed(const std::string& somedata); //<! Feed data to the parser
bool ready() {
return (chunked == true && state == 3) || // if it's chunked we get end of data indication
(chunked == false && state > 1 &&
- (!hasBody ||
- (bodybuf.str().size() <= maxbody &&
- bodybuf.str().size() >= minbody)
- )
- );
+ (!hasBody || (bodysize <= maxbody && bodysize >= minbody)));
}; //<! whether we have received enough data
void finalize() {
bodybuf.flush();
if (ready()) {
+ std::string body = bodybuf.str();
strstr_map_t::iterator cpos = target->headers.find("content-type");
if (cpos != target->headers.end() && Utility::iequals(cpos->second, "application/x-www-form-urlencoded", 32)) {
- target->postvars = Utility::parseUrlParameters(bodybuf.str());
+ target->postvars = Utility::parseUrlParameters(body);
}
- target->body = bodybuf.str();
+ target->body = std::move(body);
}
bodybuf.str("");
+ bodysize = 0;
this->target = NULL;
}; //<! finalize and release target
};