Loading src/http.cpp +83 −83 Changes for src/http.cpp: 83 added lines, 83 removed lines. Original line number Diff line number Diff line Loading @@ -180,7 +180,6 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq std::stringstream host; host <<_url.getHost() << ":" << _url.getPort(); libhttppp::HttpResponse res; nreq.setHeaderData("host")->push_back(host.str()); nreq.setRequestType(GETREQUEST); if(nreq.getRequestURL().empty()) Loading Loading @@ -210,6 +209,8 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq throw he; } libhttppp::HttpResponse res; hsize=res.parse(data.data.buf,recv); recv-=++hsize; Loading Loading @@ -313,8 +314,6 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre std::stringstream host; host <<_url.getHost() << ":" << _url.getPort(); libhttppp::HttpResponse res; nreq.setHeaderData("host")->push_back(host.str()); nreq.setRequestType(POSTREQUEST); if(nreq.getRequestURL().empty()) Loading Loading @@ -354,6 +353,7 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre throw he; } libhttppp::HttpResponse res; hsize=res.parse(data.data.buf,recv); recv-=++hsize; Loading Loading @@ -934,114 +934,112 @@ void libhttppp::HttpResponse::send(netplus::con &curconnection,const std::vector size_t libhttppp::HttpResponse::parse(const char *data, size_t inlen) { HTTPException excep; if(inlen <9){ if (inlen < 16) { throw excep[HTTPException::Error] << "HttpResponse header too small aborting!"; } size_t v=5; const char* header_end_marker = "\r\n\r\n"; const char* end_ptr = data + inlen - 4; // Check up to the last 4 bytes const char* header_end_pos = nullptr; if(memcmp(data,"HTTP/",5)==0){ while(v<inlen){ if(data[v]==' ') for (const char* p = data; p < end_ptr; ++p) { if (std::equal(p, p + 4, header_end_marker)) { header_end_pos = p; break; ++v; } }else{ throw excep[HTTPException::Error] << "HttpResponse no Version Tag found !"; } std::copy(data,data+v,std::begin(_Version)); if (header_end_pos == nullptr) { throw excep[HTTPException::Error] << "HttpHeader end not found!"; } size_t ve=++v; size_t helen = header_end_pos - data; size_t bytes_consumed = helen + 4; while(ve<inlen){ if(data[ve]=='\r' || data[ve]=='\n') break; ++ve; }; std::string header_str(data, helen); size_t line_end = header_str.find("\r\n"); --ve; if (line_end == std::string::npos) { throw excep[HTTPException::Error] << "Can't find end of Status Line!"; } std::copy(data+v,data+ve,std::back_inserter(_State)); std::string status_line = header_str.substr(0, line_end); ++ve; size_t v_start = 0; size_t v_end = status_line.find(' ', v_start); size_t helen=inlen; if (v_end == std::string::npos || status_line.substr(0, 5) != "HTTP/") { throw excep[HTTPException::Error] << "HttpResponse no Version Tag found or malformed Status Line!"; } bool hend=false; _Version = status_line.substr(v_start, v_end - v_start); for(size_t cur=ve; cur<inlen; ++cur){ switch(data[cur]){ case '\r': break; case '\n': if(hend){ helen=cur; goto HEADERENDFOUND; } hend=true; break; default: hend=false; break; } v_start = v_end + 1; v_end = status_line.find(' ', v_start); if (v_end == std::string::npos) { v_end = status_line.length(); } throw excep[HTTPException::Error] << "HttpHeader end not found!"; std::string status_code_str = status_line.substr(v_start, v_end - v_start); v_start = v_end + 1; std::string reason_phrase; if (v_start < status_line.length()) { reason_phrase = status_line.substr(v_start); } else { reason_phrase = ""; // Empty Reason Phrase } HEADERENDFOUND: _State = reason_phrase; /*parse the http header fields*/ size_t lrow=0,delimeter=0,startkeypos=0; size_t cursor = line_end + 2; // Start after Status Line's \r\n for(size_t pos=ve; pos< helen; ++pos){ if(delimeter==0 && data[pos]==':'){ delimeter=pos; while (cursor < helen) { size_t next_line = header_str.find("\r\n", cursor); if (next_line == std::string::npos) { next_line = helen; } if( data[pos]=='\r' || (data[pos-1]!='\r' && data[pos]=='\n') ){ if(delimeter>lrow && delimeter!=0){ if(pos>=0 && pos <= helen){ std::string key; std::copy(data+startkeypos,data+delimeter,back_inserter(key)); for (size_t it = 0; it < key.length(); ++it) { if(isalpha(key[it])) key[it] = (char)tolower(key[it]); std::string hline = header_str.substr(cursor, next_line - cursor); cursor = next_line + 2; if (hline.empty()) continue; size_t colon = hline.find(':'); if (colon == std::string::npos) { continue; } ++delimeter; while(data[delimeter]==' '){ ++delimeter; }; if(pos > 0 && pos <=helen){ std::string value; std::copy(data+delimeter,data+pos,std::back_inserter(value)); std::string key = hline.substr(0, colon); std::string value = (colon + 1 < hline.size()) ? hline.substr(colon + 1) : ""; HeaderData *ncontent=setHeaderData(key.c_str()); auto ltrim_inplace = [](std::string& s) { /* ... defined previously ... */ }; auto rtrim_inplace = [](std::string& s) { /* ... defined previously ... */ }; auto trim_inplace = [&](std::string& s) { rtrim_inplace(s); ltrim_inplace(s); }; size_t oldiv=0,iv=0; trim_inplace(key); trim_inplace(value); while(iv<value.length()){ if(value[iv]==';'){ ncontent->push_back(value.substr(oldiv,iv-oldiv)); while(value[++iv]==' '){ if(iv==value.length()) break; }; oldiv=iv; } ++iv; } ncontent->push_back(value.substr(oldiv,value.length()-oldiv)); } } std::transform(key.begin(), key.end(), key.begin(), [](unsigned char c){ return static_cast<char>(std::tolower(c)); }); HeaderData *ncontent = setHeaderData(key.c_str()); size_t oldiv = 0; size_t iv = 0; while ((iv = value.find(',', oldiv)) != std::string::npos) { std::string token = value.substr(oldiv, iv - oldiv); trim_inplace(token); // Trim token ncontent->push_back(token); oldiv = iv + 1; if (oldiv < value.length() && value[oldiv] == ' ') { oldiv++; } delimeter=0; lrow=pos; startkeypos=lrow+2; }else if(delimeter==0 && data[pos]==':'){ delimeter=pos; } std::string final_token = value.substr(oldiv); trim_inplace(final_token); ncontent->push_back(final_token); } _ContentLength=getHeaderData("content-length"); Loading @@ -1049,7 +1047,7 @@ size_t libhttppp::HttpResponse::parse(const char *data,size_t inlen){ _ContentType=getHeaderData("content-type"); _TransferEncoding=getHeaderData("transfer-encoding"); return helen; return bytes_consumed; } void libhttppp::HttpResponse::setTransferEncoding(const std ::string &enc){ Loading Loading @@ -1299,6 +1297,8 @@ void libhttppp::HttpRequest::printHeader(std::string &buffer){ buffer.append(_RequestVersion); buffer.append("\r\n"); std::cout << buffer << std::endl; for(HeaderData *curdat=getfirstHeaderData(); curdat; curdat=curdat->nextHeaderData()){ buffer.append(curdat->getkey()); buffer.append(": "); Loading Loading
src/http.cpp +83 −83 Changes for src/http.cpp: 83 added lines, 83 removed lines. Original line number Diff line number Diff line Loading @@ -180,7 +180,6 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq std::stringstream host; host <<_url.getHost() << ":" << _url.getPort(); libhttppp::HttpResponse res; nreq.setHeaderData("host")->push_back(host.str()); nreq.setRequestType(GETREQUEST); if(nreq.getRequestURL().empty()) Loading Loading @@ -210,6 +209,8 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq throw he; } libhttppp::HttpResponse res; hsize=res.parse(data.data.buf,recv); recv-=++hsize; Loading Loading @@ -313,8 +314,6 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre std::stringstream host; host <<_url.getHost() << ":" << _url.getPort(); libhttppp::HttpResponse res; nreq.setHeaderData("host")->push_back(host.str()); nreq.setRequestType(POSTREQUEST); if(nreq.getRequestURL().empty()) Loading Loading @@ -354,6 +353,7 @@ const std::vector<char> &libhttppp::HttpClient::Post(libhttppp::HttpRequest &nre throw he; } libhttppp::HttpResponse res; hsize=res.parse(data.data.buf,recv); recv-=++hsize; Loading Loading @@ -934,114 +934,112 @@ void libhttppp::HttpResponse::send(netplus::con &curconnection,const std::vector size_t libhttppp::HttpResponse::parse(const char *data, size_t inlen) { HTTPException excep; if(inlen <9){ if (inlen < 16) { throw excep[HTTPException::Error] << "HttpResponse header too small aborting!"; } size_t v=5; const char* header_end_marker = "\r\n\r\n"; const char* end_ptr = data + inlen - 4; // Check up to the last 4 bytes const char* header_end_pos = nullptr; if(memcmp(data,"HTTP/",5)==0){ while(v<inlen){ if(data[v]==' ') for (const char* p = data; p < end_ptr; ++p) { if (std::equal(p, p + 4, header_end_marker)) { header_end_pos = p; break; ++v; } }else{ throw excep[HTTPException::Error] << "HttpResponse no Version Tag found !"; } std::copy(data,data+v,std::begin(_Version)); if (header_end_pos == nullptr) { throw excep[HTTPException::Error] << "HttpHeader end not found!"; } size_t ve=++v; size_t helen = header_end_pos - data; size_t bytes_consumed = helen + 4; while(ve<inlen){ if(data[ve]=='\r' || data[ve]=='\n') break; ++ve; }; std::string header_str(data, helen); size_t line_end = header_str.find("\r\n"); --ve; if (line_end == std::string::npos) { throw excep[HTTPException::Error] << "Can't find end of Status Line!"; } std::copy(data+v,data+ve,std::back_inserter(_State)); std::string status_line = header_str.substr(0, line_end); ++ve; size_t v_start = 0; size_t v_end = status_line.find(' ', v_start); size_t helen=inlen; if (v_end == std::string::npos || status_line.substr(0, 5) != "HTTP/") { throw excep[HTTPException::Error] << "HttpResponse no Version Tag found or malformed Status Line!"; } bool hend=false; _Version = status_line.substr(v_start, v_end - v_start); for(size_t cur=ve; cur<inlen; ++cur){ switch(data[cur]){ case '\r': break; case '\n': if(hend){ helen=cur; goto HEADERENDFOUND; } hend=true; break; default: hend=false; break; } v_start = v_end + 1; v_end = status_line.find(' ', v_start); if (v_end == std::string::npos) { v_end = status_line.length(); } throw excep[HTTPException::Error] << "HttpHeader end not found!"; std::string status_code_str = status_line.substr(v_start, v_end - v_start); v_start = v_end + 1; std::string reason_phrase; if (v_start < status_line.length()) { reason_phrase = status_line.substr(v_start); } else { reason_phrase = ""; // Empty Reason Phrase } HEADERENDFOUND: _State = reason_phrase; /*parse the http header fields*/ size_t lrow=0,delimeter=0,startkeypos=0; size_t cursor = line_end + 2; // Start after Status Line's \r\n for(size_t pos=ve; pos< helen; ++pos){ if(delimeter==0 && data[pos]==':'){ delimeter=pos; while (cursor < helen) { size_t next_line = header_str.find("\r\n", cursor); if (next_line == std::string::npos) { next_line = helen; } if( data[pos]=='\r' || (data[pos-1]!='\r' && data[pos]=='\n') ){ if(delimeter>lrow && delimeter!=0){ if(pos>=0 && pos <= helen){ std::string key; std::copy(data+startkeypos,data+delimeter,back_inserter(key)); for (size_t it = 0; it < key.length(); ++it) { if(isalpha(key[it])) key[it] = (char)tolower(key[it]); std::string hline = header_str.substr(cursor, next_line - cursor); cursor = next_line + 2; if (hline.empty()) continue; size_t colon = hline.find(':'); if (colon == std::string::npos) { continue; } ++delimeter; while(data[delimeter]==' '){ ++delimeter; }; if(pos > 0 && pos <=helen){ std::string value; std::copy(data+delimeter,data+pos,std::back_inserter(value)); std::string key = hline.substr(0, colon); std::string value = (colon + 1 < hline.size()) ? hline.substr(colon + 1) : ""; HeaderData *ncontent=setHeaderData(key.c_str()); auto ltrim_inplace = [](std::string& s) { /* ... defined previously ... */ }; auto rtrim_inplace = [](std::string& s) { /* ... defined previously ... */ }; auto trim_inplace = [&](std::string& s) { rtrim_inplace(s); ltrim_inplace(s); }; size_t oldiv=0,iv=0; trim_inplace(key); trim_inplace(value); while(iv<value.length()){ if(value[iv]==';'){ ncontent->push_back(value.substr(oldiv,iv-oldiv)); while(value[++iv]==' '){ if(iv==value.length()) break; }; oldiv=iv; } ++iv; } ncontent->push_back(value.substr(oldiv,value.length()-oldiv)); } } std::transform(key.begin(), key.end(), key.begin(), [](unsigned char c){ return static_cast<char>(std::tolower(c)); }); HeaderData *ncontent = setHeaderData(key.c_str()); size_t oldiv = 0; size_t iv = 0; while ((iv = value.find(',', oldiv)) != std::string::npos) { std::string token = value.substr(oldiv, iv - oldiv); trim_inplace(token); // Trim token ncontent->push_back(token); oldiv = iv + 1; if (oldiv < value.length() && value[oldiv] == ' ') { oldiv++; } delimeter=0; lrow=pos; startkeypos=lrow+2; }else if(delimeter==0 && data[pos]==':'){ delimeter=pos; } std::string final_token = value.substr(oldiv); trim_inplace(final_token); ncontent->push_back(final_token); } _ContentLength=getHeaderData("content-length"); Loading @@ -1049,7 +1047,7 @@ size_t libhttppp::HttpResponse::parse(const char *data,size_t inlen){ _ContentType=getHeaderData("content-type"); _TransferEncoding=getHeaderData("transfer-encoding"); return helen; return bytes_consumed; } void libhttppp::HttpResponse::setTransferEncoding(const std ::string &enc){ Loading Loading @@ -1299,6 +1297,8 @@ void libhttppp::HttpRequest::printHeader(std::string &buffer){ buffer.append(_RequestVersion); buffer.append("\r\n"); std::cout << buffer << std::endl; for(HeaderData *curdat=getfirstHeaderData(); curdat; curdat=curdat->nextHeaderData()){ buffer.append(curdat->getkey()); buffer.append(": "); Loading