Such style. (Code style unification)
This commit is contained in:
parent
57bcd8f25c
commit
8c01ec8897
57 changed files with 6548 additions and 6437 deletions
|
@ -6,13 +6,13 @@
|
|||
|
||||
/// This constructor creates an empty HTTP::Parser, ready for use for either reading or writing.
|
||||
/// All this constructor does is call HTTP::Parser::Clean().
|
||||
HTTP::Parser::Parser(){
|
||||
HTTP::Parser::Parser() {
|
||||
headerOnly = false;
|
||||
Clean();
|
||||
}
|
||||
|
||||
/// Completely re-initializes the HTTP::Parser, leaving it ready for either reading or writing usage.
|
||||
void HTTP::Parser::Clean(){
|
||||
void HTTP::Parser::Clean() {
|
||||
seenHeaders = false;
|
||||
seenReq = false;
|
||||
getChunks = false;
|
||||
|
@ -30,16 +30,16 @@ 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<std::string, std::string>::iterator it;
|
||||
if (protocol.size() < 5 || protocol[4] != '/'){
|
||||
if (protocol.size() < 5 || protocol[4] != '/') {
|
||||
protocol = "HTTP/1.0";
|
||||
}
|
||||
builder = method + " " + url + " " + protocol + "\r\n";
|
||||
for (it = headers.begin(); it != headers.end(); it++){
|
||||
if (( *it).first != "" && ( *it).second != ""){
|
||||
builder += ( *it).first + ": " + ( *it).second + "\r\n";
|
||||
for (it = headers.begin(); it != headers.end(); it++) {
|
||||
if ((*it).first != "" && (*it).second != "") {
|
||||
builder += (*it).first + ": " + (*it).second + "\r\n";
|
||||
}
|
||||
}
|
||||
builder += "\r\n" + body;
|
||||
|
@ -49,17 +49,17 @@ std::string & HTTP::Parser::BuildRequest(){
|
|||
/// Creates and sends a valid HTTP 1.0 or 1.1 request.
|
||||
/// The request is build from internal variables set before this call is made.
|
||||
/// To be precise, method, url, protocol, headers and body are used.
|
||||
void HTTP::Parser::SendRequest(Socket::Connection & conn){
|
||||
void HTTP::Parser::SendRequest(Socket::Connection & conn) {
|
||||
/// \todo Include GET/POST variable parsing?
|
||||
std::map<std::string, std::string>::iterator it;
|
||||
if (protocol.size() < 5 || protocol[4] != '/'){
|
||||
if (protocol.size() < 5 || protocol[4] != '/') {
|
||||
protocol = "HTTP/1.0";
|
||||
}
|
||||
builder = method + " " + url + " " + protocol + "\r\n";
|
||||
conn.SendNow(builder);
|
||||
for (it = headers.begin(); it != headers.end(); it++){
|
||||
if (( *it).first != "" && ( *it).second != ""){
|
||||
builder = ( *it).first + ": " + ( *it).second + "\r\n";
|
||||
for (it = headers.begin(); it != headers.end(); it++) {
|
||||
if ((*it).first != "" && (*it).second != "") {
|
||||
builder = (*it).first + ": " + (*it).second + "\r\n";
|
||||
conn.SendNow(builder);
|
||||
}
|
||||
}
|
||||
|
@ -73,17 +73,17 @@ void HTTP::Parser::SendRequest(Socket::Connection & conn){
|
|||
/// \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<std::string, std::string>::iterator it;
|
||||
if (protocol.size() < 5 || protocol[4] != '/'){
|
||||
if (protocol.size() < 5 || protocol[4] != '/') {
|
||||
protocol = "HTTP/1.0";
|
||||
}
|
||||
builder = protocol + " " + code + " " + message + "\r\n";
|
||||
for (it = headers.begin(); it != headers.end(); it++){
|
||||
if (( *it).first != "" && ( *it).second != ""){
|
||||
if (( *it).first != "Content-Length" || ( *it).second != "0"){
|
||||
builder += ( *it).first + ": " + ( *it).second + "\r\n";
|
||||
for (it = headers.begin(); it != headers.end(); it++) {
|
||||
if ((*it).first != "" && (*it).second != "") {
|
||||
if ((*it).first != "Content-Length" || (*it).second != "0") {
|
||||
builder += (*it).first + ": " + (*it).second + "\r\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -99,18 +99,18 @@ std::string & HTTP::Parser::BuildResponse(std::string code, std::string message)
|
|||
/// \param code The HTTP response code. Usually you want 200.
|
||||
/// \param message The HTTP response message. Usually you want "OK".
|
||||
/// \param conn The Socket::Connection to send the response over.
|
||||
void HTTP::Parser::SendResponse(std::string code, std::string message, Socket::Connection & conn){
|
||||
void HTTP::Parser::SendResponse(std::string code, std::string message, Socket::Connection & conn) {
|
||||
/// \todo Include GET/POST variable parsing?
|
||||
std::map<std::string, std::string>::iterator it;
|
||||
if (protocol.size() < 5 || protocol[4] != '/'){
|
||||
if (protocol.size() < 5 || protocol[4] != '/') {
|
||||
protocol = "HTTP/1.0";
|
||||
}
|
||||
builder = protocol + " " + code + " " + message + "\r\n";
|
||||
conn.SendNow(builder);
|
||||
for (it = headers.begin(); it != headers.end(); it++){
|
||||
if (( *it).first != "" && ( *it).second != ""){
|
||||
if (( *it).first != "Content-Length" || ( *it).second != "0"){
|
||||
builder = ( *it).first + ": " + ( *it).second + "\r\n";
|
||||
for (it = headers.begin(); it != headers.end(); it++) {
|
||||
if ((*it).first != "" && (*it).second != "") {
|
||||
if ((*it).first != "Content-Length" || (*it).second != "0") {
|
||||
builder = (*it).first + ": " + (*it).second + "\r\n";
|
||||
conn.SendNow(builder);
|
||||
}
|
||||
}
|
||||
|
@ -126,12 +126,12 @@ void HTTP::Parser::SendResponse(std::string code, std::string message, Socket::C
|
|||
/// \param message The HTTP response message. Usually you want "OK".
|
||||
/// \param request The HTTP request to respond to.
|
||||
/// \param conn The connection to send over.
|
||||
void HTTP::Parser::StartResponse(std::string code, std::string message, HTTP::Parser & request, Socket::Connection & conn){
|
||||
void HTTP::Parser::StartResponse(std::string code, std::string message, HTTP::Parser & request, Socket::Connection & conn) {
|
||||
protocol = request.protocol;
|
||||
body = "";
|
||||
if (protocol == "HTTP/1.1"){
|
||||
if (protocol == "HTTP/1.1") {
|
||||
SetHeader("Transfer-Encoding", "chunked");
|
||||
}else{
|
||||
} else {
|
||||
SetBody("");
|
||||
}
|
||||
SendResponse(code, message, conn);
|
||||
|
@ -143,7 +143,7 @@ void HTTP::Parser::StartResponse(std::string code, std::string message, HTTP::Pa
|
|||
/// This call simply calls StartResponse("200", "OK", request, conn)
|
||||
/// \param request The HTTP request to respond to.
|
||||
/// \param conn The connection to send over.
|
||||
void HTTP::Parser::StartResponse(HTTP::Parser & request, Socket::Connection & conn){
|
||||
void HTTP::Parser::StartResponse(HTTP::Parser & request, Socket::Connection & conn) {
|
||||
StartResponse("200", "OK", request, conn);
|
||||
}
|
||||
|
||||
|
@ -152,29 +152,29 @@ void HTTP::Parser::StartResponse(HTTP::Parser & request, Socket::Connection & co
|
|||
/// - Retrieve all the body from the 'from' Socket::Connection.
|
||||
/// - Forward those contents as-is to the 'to' Socket::Connection.
|
||||
/// It blocks until completed or either of the connections reaches an error state.
|
||||
void HTTP::Parser::Proxy(Socket::Connection & from, Socket::Connection & to){
|
||||
void HTTP::Parser::Proxy(Socket::Connection & from, Socket::Connection & to) {
|
||||
SendResponse(url, method, to);
|
||||
if (getChunks){
|
||||
if (getChunks) {
|
||||
unsigned int proxyingChunk = 0;
|
||||
while (to.connected() && from.connected()){
|
||||
if ((from.Received().size() && (from.Received().size() > 1 || *(from.Received().get().rbegin()) == '\n')) || from.spool()){
|
||||
if (proxyingChunk){
|
||||
while (proxyingChunk && from.Received().size()){
|
||||
while (to.connected() && from.connected()) {
|
||||
if ((from.Received().size() && (from.Received().size() > 1 || *(from.Received().get().rbegin()) == '\n')) || from.spool()) {
|
||||
if (proxyingChunk) {
|
||||
while (proxyingChunk && from.Received().size()) {
|
||||
unsigned int toappend = from.Received().get().size();
|
||||
if (toappend > proxyingChunk){
|
||||
if (toappend > proxyingChunk) {
|
||||
toappend = proxyingChunk;
|
||||
to.SendNow(from.Received().get().c_str(), toappend);
|
||||
from.Received().get().erase(0, toappend);
|
||||
}else{
|
||||
} else {
|
||||
to.SendNow(from.Received().get());
|
||||
from.Received().get().clear();
|
||||
}
|
||||
proxyingChunk -= toappend;
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
//Make sure the received data ends in a newline (\n).
|
||||
if ( *(from.Received().get().rbegin()) != '\n'){
|
||||
if (from.Received().size() > 1){
|
||||
if (*(from.Received().get().rbegin()) != '\n') {
|
||||
if (from.Received().size() > 1) {
|
||||
//make a copy of the first part
|
||||
std::string tmp = from.Received().get();
|
||||
//clear the first part, wiping it from the partlist
|
||||
|
@ -182,26 +182,26 @@ void HTTP::Parser::Proxy(Socket::Connection & from, Socket::Connection & to){
|
|||
from.Received().size();
|
||||
//take the now first (was second) part, insert the stored part in front of it
|
||||
from.Received().get().insert(0, tmp);
|
||||
}else{
|
||||
} else {
|
||||
Util::sleep(100);
|
||||
}
|
||||
if ( *(from.Received().get().rbegin()) != '\n'){
|
||||
if (*(from.Received().get().rbegin()) != '\n') {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
//forward the size and any empty lines
|
||||
to.SendNow(from.Received().get());
|
||||
|
||||
|
||||
std::string tmpA = from.Received().get().substr(0, from.Received().get().size() - 1);
|
||||
while (tmpA.find('\r') != std::string::npos){
|
||||
while (tmpA.find('\r') != std::string::npos) {
|
||||
tmpA.erase(tmpA.find('\r'));
|
||||
}
|
||||
unsigned int chunkLen = 0;
|
||||
if ( !tmpA.empty()){
|
||||
for (unsigned int i = 0; i < tmpA.size(); ++i){
|
||||
if (!tmpA.empty()) {
|
||||
for (unsigned int i = 0; i < tmpA.size(); ++i) {
|
||||
chunkLen = (chunkLen << 4) | unhex(tmpA[i]);
|
||||
}
|
||||
if (chunkLen == 0){
|
||||
if (chunkLen == 0) {
|
||||
getChunks = false;
|
||||
to.SendNow("\r\n", 2);
|
||||
return;
|
||||
|
@ -210,24 +210,24 @@ void HTTP::Parser::Proxy(Socket::Connection & from, Socket::Connection & to){
|
|||
}
|
||||
from.Received().get().clear();
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
Util::sleep(100);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
unsigned int bodyLen = length;
|
||||
while (bodyLen > 0 && to.connected() && from.connected()){
|
||||
if (from.Received().size() || from.spool()){
|
||||
if (from.Received().get().size() <= bodyLen){
|
||||
while (bodyLen > 0 && to.connected() && from.connected()) {
|
||||
if (from.Received().size() || from.spool()) {
|
||||
if (from.Received().get().size() <= bodyLen) {
|
||||
to.SendNow(from.Received().get());
|
||||
bodyLen -= from.Received().get().size();
|
||||
from.Received().get().clear();
|
||||
}else{
|
||||
} else {
|
||||
to.SendNow(from.Received().get().c_str(), bodyLen);
|
||||
from.Received().get().erase(0, bodyLen);
|
||||
bodyLen = 0;
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
Util::sleep(100);
|
||||
}
|
||||
}
|
||||
|
@ -237,19 +237,19 @@ void HTTP::Parser::Proxy(Socket::Connection & from, Socket::Connection & to){
|
|||
/// Trims any whitespace at the front or back of the string.
|
||||
/// Used when getting/setting headers.
|
||||
/// \param s The string to trim. The string itself will be changed, not returned.
|
||||
void HTTP::Parser::Trim(std::string & s){
|
||||
void HTTP::Parser::Trim(std::string & s) {
|
||||
size_t startpos = s.find_first_not_of(" \t");
|
||||
size_t endpos = s.find_last_not_of(" \t");
|
||||
if ((std::string::npos == startpos) || (std::string::npos == endpos)){
|
||||
if ((std::string::npos == startpos) || (std::string::npos == endpos)) {
|
||||
s = "";
|
||||
}else{
|
||||
} else {
|
||||
s = s.substr(startpos, endpos - startpos + 1);
|
||||
}
|
||||
}
|
||||
|
||||
/// Function that sets the body of a response or request, along with the correct Content-Length header.
|
||||
/// \param s The string to set the body to.
|
||||
void HTTP::Parser::SetBody(std::string s){
|
||||
void HTTP::Parser::SetBody(std::string s) {
|
||||
body = s;
|
||||
SetHeader("Content-Length", s.length());
|
||||
}
|
||||
|
@ -257,39 +257,39 @@ void HTTP::Parser::SetBody(std::string s){
|
|||
/// Function that sets the body of a response or request, along with the correct Content-Length header.
|
||||
/// \param buffer The buffer data to set the body to.
|
||||
/// \param len Length of the buffer data.
|
||||
void HTTP::Parser::SetBody(char * buffer, int len){
|
||||
void HTTP::Parser::SetBody(char * buffer, int len) {
|
||||
body = "";
|
||||
body.append(buffer, len);
|
||||
SetHeader("Content-Length", len);
|
||||
}
|
||||
|
||||
/// Returns header i, if set.
|
||||
std::string HTTP::Parser::getUrl(){
|
||||
if (url.find('?') != std::string::npos){
|
||||
std::string HTTP::Parser::getUrl() {
|
||||
if (url.find('?') != std::string::npos) {
|
||||
return url.substr(0, url.find('?'));
|
||||
}else{
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns header i, if set.
|
||||
std::string HTTP::Parser::GetHeader(std::string i){
|
||||
std::string HTTP::Parser::GetHeader(std::string i) {
|
||||
return headers[i];
|
||||
}
|
||||
/// Returns POST variable i, if set.
|
||||
std::string HTTP::Parser::GetVar(std::string i){
|
||||
std::string HTTP::Parser::GetVar(std::string i) {
|
||||
return vars[i];
|
||||
}
|
||||
|
||||
/// Sets header i to string value v.
|
||||
void HTTP::Parser::SetHeader(std::string i, std::string v){
|
||||
void HTTP::Parser::SetHeader(std::string i, std::string v) {
|
||||
Trim(i);
|
||||
Trim(v);
|
||||
headers[i] = v;
|
||||
}
|
||||
|
||||
/// Sets header i to integer value v.
|
||||
void HTTP::Parser::SetHeader(std::string i, int v){
|
||||
void HTTP::Parser::SetHeader(std::string i, int v) {
|
||||
Trim(i);
|
||||
char val[23]; //ints are never bigger than 22 chars as decimal
|
||||
sprintf(val, "%i", v);
|
||||
|
@ -297,11 +297,11 @@ void HTTP::Parser::SetHeader(std::string i, int v){
|
|||
}
|
||||
|
||||
/// Sets POST variable i to string value v.
|
||||
void HTTP::Parser::SetVar(std::string i, std::string v){
|
||||
void HTTP::Parser::SetVar(std::string i, std::string v) {
|
||||
Trim(i);
|
||||
Trim(v);
|
||||
//only set if there is actually a key
|
||||
if ( !i.empty()){
|
||||
if (!i.empty()) {
|
||||
vars[i] = v;
|
||||
}
|
||||
}
|
||||
|
@ -311,10 +311,10 @@ void HTTP::Parser::SetVar(std::string i, std::string v){
|
|||
/// If not, as much as can be interpreted is removed and false returned.
|
||||
/// \param conn The socket to read from.
|
||||
/// \return True if a whole request or response was read, false otherwise.
|
||||
bool HTTP::Parser::Read(Socket::Connection & conn){
|
||||
bool HTTP::Parser::Read(Socket::Connection & conn) {
|
||||
//Make sure the received data ends in a newline (\n).
|
||||
while ((!seenHeaders || (getChunks && !doingChunk)) && *(conn.Received().get().rbegin()) != '\n'){
|
||||
if (conn.Received().size() > 1){
|
||||
while ((!seenHeaders || (getChunks && !doingChunk)) && *(conn.Received().get().rbegin()) != '\n') {
|
||||
if (conn.Received().size() > 1) {
|
||||
//make a copy of the first part
|
||||
std::string tmp = conn.Received().get();
|
||||
//clear the first part, wiping it from the partlist
|
||||
|
@ -322,16 +322,16 @@ bool HTTP::Parser::Read(Socket::Connection & conn){
|
|||
conn.Received().size();
|
||||
//take the now first (was second) part, insert the stored part in front of it
|
||||
conn.Received().get().insert(0, tmp);
|
||||
}else{
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//if a parse succeeds, simply return true
|
||||
if (parse(conn.Received().get())){
|
||||
if (parse(conn.Received().get())) {
|
||||
return true;
|
||||
}
|
||||
//otherwise, if we have parts left, call ourselves recursively
|
||||
if (conn.Received().size()){
|
||||
if (conn.Received().size()) {
|
||||
return Read(conn);
|
||||
}
|
||||
return false;
|
||||
|
@ -342,7 +342,7 @@ bool HTTP::Parser::Read(Socket::Connection & conn){
|
|||
/// If not, as much as can be interpreted is removed and false returned.
|
||||
/// \param strbuf The buffer to read from.
|
||||
/// \return True if a whole request or response was read, false otherwise.
|
||||
bool HTTP::Parser::Read(std::string & strbuf){
|
||||
bool HTTP::Parser::Read(std::string & strbuf) {
|
||||
return parse(strbuf);
|
||||
} //HTTPReader::Read
|
||||
|
||||
|
@ -351,74 +351,74 @@ bool HTTP::Parser::Read(std::string & strbuf){
|
|||
/// from the data buffer.
|
||||
/// \param HTTPbuffer The data buffer to read from.
|
||||
/// \return True on success, false otherwise.
|
||||
bool HTTP::Parser::parse(std::string & HTTPbuffer){
|
||||
bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
||||
size_t f;
|
||||
std::string tmpA, tmpB, tmpC;
|
||||
/// \todo Make this not resize HTTPbuffer in parts, but read all at once and then remove the entire request, like doxygen claims it does?
|
||||
while ( !HTTPbuffer.empty()){
|
||||
if ( !seenHeaders){
|
||||
while (!HTTPbuffer.empty()) {
|
||||
if (!seenHeaders) {
|
||||
f = HTTPbuffer.find('\n');
|
||||
if (f == std::string::npos) return false;
|
||||
tmpA = HTTPbuffer.substr(0, f);
|
||||
if (f + 1 == HTTPbuffer.size()){
|
||||
if (f + 1 == HTTPbuffer.size()) {
|
||||
HTTPbuffer.clear();
|
||||
}else{
|
||||
} else {
|
||||
HTTPbuffer.erase(0, f + 1);
|
||||
}
|
||||
while (tmpA.find('\r') != std::string::npos){
|
||||
while (tmpA.find('\r') != std::string::npos) {
|
||||
tmpA.erase(tmpA.find('\r'));
|
||||
}
|
||||
if ( !seenReq){
|
||||
if (!seenReq) {
|
||||
seenReq = true;
|
||||
f = tmpA.find(' ');
|
||||
if (f != std::string::npos){
|
||||
if (tmpA.substr(0, 4) == "HTTP"){
|
||||
if (f != std::string::npos) {
|
||||
if (tmpA.substr(0, 4) == "HTTP") {
|
||||
protocol = tmpA.substr(0, f);
|
||||
tmpA.erase(0, f + 1);
|
||||
f = tmpA.find(' ');
|
||||
if (f != std::string::npos){
|
||||
if (f != std::string::npos) {
|
||||
url = tmpA.substr(0, f);
|
||||
tmpA.erase(0, f + 1);
|
||||
method = tmpA;
|
||||
if (url.find('?') != std::string::npos){
|
||||
if (url.find('?') != std::string::npos) {
|
||||
parseVars(url.substr(url.find('?') + 1)); //parse GET variables
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
seenReq = false;
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
method = tmpA.substr(0, f);
|
||||
tmpA.erase(0, f + 1);
|
||||
f = tmpA.find(' ');
|
||||
if (f != std::string::npos){
|
||||
if (f != std::string::npos) {
|
||||
url = 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
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
seenReq = false;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
seenReq = false;
|
||||
}
|
||||
}else{
|
||||
if (tmpA.size() == 0){
|
||||
} else {
|
||||
if (tmpA.size() == 0) {
|
||||
seenHeaders = true;
|
||||
body.clear();
|
||||
if (GetHeader("Content-Length") != ""){
|
||||
if (GetHeader("Content-Length") != "") {
|
||||
length = atoi(GetHeader("Content-Length").c_str());
|
||||
if (body.capacity() < length){
|
||||
if (body.capacity() < length) {
|
||||
body.reserve(length);
|
||||
}
|
||||
}
|
||||
if (GetHeader("Transfer-Encoding") == "chunked"){
|
||||
if (GetHeader("Transfer-Encoding") == "chunked") {
|
||||
getChunks = true;
|
||||
doingChunk = 0;
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
f = tmpA.find(':');
|
||||
if (f == std::string::npos) continue;
|
||||
tmpB = tmpA.substr(0, f);
|
||||
|
@ -427,61 +427,61 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer){
|
|||
}
|
||||
}
|
||||
}
|
||||
if (seenHeaders){
|
||||
if (length > 0){
|
||||
if (headerOnly){
|
||||
if (seenHeaders) {
|
||||
if (length > 0) {
|
||||
if (headerOnly) {
|
||||
return true;
|
||||
}
|
||||
unsigned int toappend = length - body.length();
|
||||
if (toappend > 0){
|
||||
if (toappend > 0) {
|
||||
body.append(HTTPbuffer, 0, toappend);
|
||||
HTTPbuffer.erase(0, toappend);
|
||||
}
|
||||
if (length == body.length()){
|
||||
if (length == body.length()) {
|
||||
parseVars(body); //parse POST variables
|
||||
return true;
|
||||
}else{
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}else{
|
||||
if (getChunks){
|
||||
if (headerOnly){
|
||||
} else {
|
||||
if (getChunks) {
|
||||
if (headerOnly) {
|
||||
return true;
|
||||
}
|
||||
if (doingChunk){
|
||||
if (doingChunk) {
|
||||
unsigned int toappend = HTTPbuffer.size();
|
||||
if (toappend > doingChunk){
|
||||
if (toappend > doingChunk) {
|
||||
toappend = doingChunk;
|
||||
}
|
||||
body.append(HTTPbuffer, 0, toappend);
|
||||
HTTPbuffer.erase(0, toappend);
|
||||
doingChunk -= toappend;
|
||||
}else{
|
||||
} else {
|
||||
f = HTTPbuffer.find('\n');
|
||||
if (f == std::string::npos) return false;
|
||||
tmpA = HTTPbuffer.substr(0, f);
|
||||
while (tmpA.find('\r') != std::string::npos){
|
||||
while (tmpA.find('\r') != std::string::npos) {
|
||||
tmpA.erase(tmpA.find('\r'));
|
||||
}
|
||||
unsigned int chunkLen = 0;
|
||||
if ( !tmpA.empty()){
|
||||
for (unsigned int i = 0; i < tmpA.size(); ++i){
|
||||
if (!tmpA.empty()) {
|
||||
for (unsigned int i = 0; i < tmpA.size(); ++i) {
|
||||
chunkLen = (chunkLen << 4) | unhex(tmpA[i]);
|
||||
}
|
||||
if (chunkLen == 0){
|
||||
if (chunkLen == 0) {
|
||||
getChunks = false;
|
||||
return true;
|
||||
}
|
||||
doingChunk = chunkLen;
|
||||
}
|
||||
if (f + 1 == HTTPbuffer.size()){
|
||||
if (f + 1 == HTTPbuffer.size()) {
|
||||
HTTPbuffer.clear();
|
||||
}else{
|
||||
} else {
|
||||
HTTPbuffer.erase(0, f + 1);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}else{
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -492,28 +492,28 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer){
|
|||
|
||||
/// Parses GET or POST-style variable data.
|
||||
/// Saves to internal variable structure using HTTP::Parser::SetVar.
|
||||
void HTTP::Parser::parseVars(std::string data){
|
||||
void HTTP::Parser::parseVars(std::string data) {
|
||||
std::string varname;
|
||||
std::string varval;
|
||||
// position where a part start (e.g. after &)
|
||||
size_t pos = 0;
|
||||
while (pos < data.length()){
|
||||
while (pos < data.length()) {
|
||||
size_t nextpos = data.find('&', pos);
|
||||
if (nextpos == std::string::npos){
|
||||
if (nextpos == std::string::npos) {
|
||||
nextpos = data.length();
|
||||
}
|
||||
size_t eq_pos = data.find('=', pos);
|
||||
if (eq_pos < nextpos){
|
||||
if (eq_pos < nextpos) {
|
||||
// there is a key and value
|
||||
varname = data.substr(pos, eq_pos - pos);
|
||||
varval = data.substr(eq_pos + 1, nextpos - eq_pos - 1);
|
||||
}else{
|
||||
} else {
|
||||
// no value, only a key
|
||||
varname = data.substr(pos, nextpos - pos);
|
||||
varval.clear();
|
||||
}
|
||||
SetVar(urlunescape(varname), urlunescape(varval));
|
||||
if (nextpos == std::string::npos){
|
||||
if (nextpos == std::string::npos) {
|
||||
// in case the string is gigantic
|
||||
break;
|
||||
}
|
||||
|
@ -525,7 +525,7 @@ void HTTP::Parser::parseVars(std::string data){
|
|||
/// Sends a string in chunked format if protocol is HTTP/1.1, sends as-is otherwise.
|
||||
/// \param bodypart The data to send.
|
||||
/// \param conn The connection to use for sending.
|
||||
void HTTP::Parser::Chunkify(std::string & bodypart, Socket::Connection & conn){
|
||||
void HTTP::Parser::Chunkify(std::string & bodypart, Socket::Connection & conn) {
|
||||
Chunkify(bodypart.c_str(), bodypart.size(), conn);
|
||||
}
|
||||
|
||||
|
@ -533,8 +533,8 @@ void HTTP::Parser::Chunkify(std::string & bodypart, Socket::Connection & conn){
|
|||
/// \param data The data to send.
|
||||
/// \param size The size of the data to send.
|
||||
/// \param conn The connection to use for sending.
|
||||
void HTTP::Parser::Chunkify(const char * data, unsigned int size, Socket::Connection & conn){
|
||||
if (protocol == "HTTP/1.1"){
|
||||
void HTTP::Parser::Chunkify(const char * data, unsigned int size, Socket::Connection & conn) {
|
||||
if (protocol == "HTTP/1.1") {
|
||||
char len[10];
|
||||
int sizelen = snprintf(len, 10, "%x\r\n", size);
|
||||
//prepend the chunk size and \r\n
|
||||
|
@ -543,39 +543,39 @@ void HTTP::Parser::Chunkify(const char * data, unsigned int size, Socket::Connec
|
|||
conn.SendNow(data, size);
|
||||
//append \r\n
|
||||
conn.SendNow("\r\n", 2);
|
||||
if ( !size){
|
||||
if (!size) {
|
||||
//append \r\n again if this was the end of the file (required by chunked transfer encoding according to spec)
|
||||
conn.SendNow("\r\n", 2);
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
//just send the chunk itself
|
||||
conn.SendNow(data, size);
|
||||
//close the connection if this was the end of the file
|
||||
if ( !size){
|
||||
if (!size) {
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Unescapes URLencoded std::string data.
|
||||
std::string HTTP::Parser::urlunescape(const std::string & in){
|
||||
std::string HTTP::Parser::urlunescape(const std::string & in) {
|
||||
std::string out;
|
||||
for (unsigned int i = 0; i < in.length(); ++i){
|
||||
if (in[i] == '%'){
|
||||
for (unsigned int i = 0; i < in.length(); ++i) {
|
||||
if (in[i] == '%') {
|
||||
char tmp = 0;
|
||||
++i;
|
||||
if (i < in.length()){
|
||||
if (i < in.length()) {
|
||||
tmp = unhex(in[i]) << 4;
|
||||
}
|
||||
++i;
|
||||
if (i < in.length()){
|
||||
if (i < in.length()) {
|
||||
tmp += unhex(in[i]);
|
||||
}
|
||||
out += tmp;
|
||||
}else{
|
||||
if (in[i] == '+'){
|
||||
} else {
|
||||
if (in[i] == '+') {
|
||||
out += ' ';
|
||||
}else{
|
||||
} else {
|
||||
out += in[i];
|
||||
}
|
||||
}
|
||||
|
@ -585,19 +585,19 @@ std::string HTTP::Parser::urlunescape(const std::string & in){
|
|||
|
||||
/// Helper function for urlunescape.
|
||||
/// Takes a single char input and outputs its integer hex value.
|
||||
int HTTP::Parser::unhex(char c){
|
||||
int HTTP::Parser::unhex(char c) {
|
||||
return (c >= '0' && c <= '9' ? c - '0' : c >= 'A' && c <= 'F' ? c - 'A' + 10 : c - 'a' + 10);
|
||||
}
|
||||
|
||||
/// URLencodes std::string data.
|
||||
std::string HTTP::Parser::urlencode(const std::string &c){
|
||||
std::string HTTP::Parser::urlencode(const std::string & c) {
|
||||
std::string escaped = "";
|
||||
int max = c.length();
|
||||
for (int i = 0; i < max; i++){
|
||||
for (int i = 0; i < max; i++) {
|
||||
if (('0' <= c[i] && c[i] <= '9') || ('a' <= c[i] && c[i] <= 'z') || ('A' <= c[i] && c[i] <= 'Z')
|
||||
|| (c[i] == '~' || c[i] == '!' || c[i] == '*' || c[i] == '(' || c[i] == ')' || c[i] == '\'')){
|
||||
escaped.append( &c[i], 1);
|
||||
}else{
|
||||
|| (c[i] == '~' || c[i] == '!' || c[i] == '*' || c[i] == '(' || c[i] == ')' || c[i] == '\'')) {
|
||||
escaped.append(&c[i], 1);
|
||||
} else {
|
||||
escaped.append("%");
|
||||
escaped.append(hex(c[i]));
|
||||
}
|
||||
|
@ -607,7 +607,7 @@ std::string HTTP::Parser::urlencode(const std::string &c){
|
|||
|
||||
/// Helper function for urlescape.
|
||||
/// Encodes a character as two hex digits.
|
||||
std::string HTTP::Parser::hex(char dec){
|
||||
std::string HTTP::Parser::hex(char dec) {
|
||||
char dig1 = (dec & 0xF0) >> 4;
|
||||
char dig2 = (dec & 0x0F);
|
||||
if (dig1 <= 9) dig1 += 48;
|
||||
|
@ -615,7 +615,7 @@ std::string HTTP::Parser::hex(char dec){
|
|||
if (dig2 <= 9) dig2 += 48;
|
||||
if (10 <= dig2 && dig2 <= 15) dig2 += 97 - 10;
|
||||
std::string r;
|
||||
r.append( &dig1, 1);
|
||||
r.append( &dig2, 1);
|
||||
r.append(&dig1, 1);
|
||||
r.append(&dig2, 1);
|
||||
return r;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue