Loading src/http.cpp +65 −16 Changes for src/http.cpp: 65 added lines, 16 removed lines. Original line number Diff line number Diff line Loading @@ -53,36 +53,83 @@ libhttppp::HttpUrl::HttpUrl(){ _path="/"; } libhttppp::HttpUrl::HttpUrl(const std::string& url, bool http3): HttpUrl(url.c_str(),http3){ } libhttppp::HttpUrl::HttpUrl(const char* url, bool http3){ libhttppp::HttpUrl::HttpUrl(const std::string& url, bool http3) { HTTPException exp; if (http3) { _protocol = HttpProtocol::HTTP3; _port = 443; } else if (std::strncmp(url, "https://", 8) == 0) { _protocol = HTTPS; _port = 443; url += 8; } else if (std::strncmp(url, "http://", 7) == 0) { _protocol = HTTP; _port = 80; url += 7; } else if (std::strncmp(url, "https", 5) == 0) { _protocol = HTTPS; _port = 443; } else if (std::strncmp(url, "http", 4) == 0) { _protocol = HTTP; _port = 80; } else { exp[HTTPException::Error] << "Httpurl: protocol not supported!"; throw exp; } // Get the C-style pointer for string operations const char* c_url = url.c_str(); const char* p = c_url; if (http3) { _protocol = HttpProtocol::HTTP3; _port = 443; } // Update c_url pointer based on scheme matching else if (url.rfind("https://", 0) == 0) { // Using rfind for string prefix check _protocol = HTTPS; _port = 443; p += 8; // Advance pointer past "https://" } else if (url.rfind("http://", 0) == 0) { _protocol = HTTP; _port = 80; p += 7; // Advance pointer past "http://" } // Note: The "https" and "http" (without ://) checks are less robust but kept for compatibility else if (url.rfind("https", 0) == 0) { _protocol = HTTPS; _port = 443; } else if (url.rfind("http", 0) == 0) { _protocol = HTTP; _port = 80; } else { exp[HTTPException::Error] << "Httpurl: protocol not supported!"; throw exp; } _path = "/"; const char* p = url; const char* host_start = p; while (*p && *p != ':' && *p != '/') ++p; // Use the length of the original string to prevent reading past the end size_t url_len = url.length(); size_t offset = p - c_url; // Iterate through the remaining part of the string while (offset < url_len && *p && *p != ':' && *p != '/') { ++p; ++offset; } // Assign the host part _host.assign(host_start, p - host_start); if (*p == ':') { if (offset < url_len && *p == ':') { ++p; ++offset; const char* digits = p; while (*p && std::isdigit((unsigned char)*p)) ++p; // Iterate through the port digits while (offset < url_len && *p && std::isdigit((unsigned char)*p)) { ++p; ++offset; } if (p == digits || (p - digits) > 5) { exp[HTTPException::Error] << "Httpurl: Port range are Wrong !"; throw exp; exp[HTTPException::Error] << "Httpurl: Port range are Wrong !"; throw exp; } _port = std::atoi(std::string(digits, p - digits).c_str()); } if (*p == '/') _path.assign(p); // Check for the path part if (offset < url_len && *p == '/') { // Use string methods or assign from pointer // Here we use std::string::substr for clarity and safety with the remaining part of the URL _path = url.substr(p - c_url); } } libhttppp::HttpUrl::HttpUrl(const HttpUrl& src){ Loading Loading @@ -182,8 +229,10 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq nreq.setHeaderData("host")->push_back(host.str()); nreq.setRequestType(GETREQUEST); if(nreq.getRequestURL().empty()) nreq.setRequestURL(_url.getPath()); if(nreq.getRequestVersion().empty()) nreq.setRequestVersion(HTTPVERSION(1.1)); nreq.send(*_cltsock); Loading src/http.h +0 −1 Changes for src/http.h: 0 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -54,7 +54,6 @@ namespace libhttppp { }; HttpUrl(); HttpUrl(const char *url,bool http3=false); HttpUrl(const std::string &url,bool http3=false); HttpUrl(const HttpUrl &src); ~HttpUrl(); Loading Loading
src/http.cpp +65 −16 Changes for src/http.cpp: 65 added lines, 16 removed lines. Original line number Diff line number Diff line Loading @@ -53,36 +53,83 @@ libhttppp::HttpUrl::HttpUrl(){ _path="/"; } libhttppp::HttpUrl::HttpUrl(const std::string& url, bool http3): HttpUrl(url.c_str(),http3){ } libhttppp::HttpUrl::HttpUrl(const char* url, bool http3){ libhttppp::HttpUrl::HttpUrl(const std::string& url, bool http3) { HTTPException exp; if (http3) { _protocol = HttpProtocol::HTTP3; _port = 443; } else if (std::strncmp(url, "https://", 8) == 0) { _protocol = HTTPS; _port = 443; url += 8; } else if (std::strncmp(url, "http://", 7) == 0) { _protocol = HTTP; _port = 80; url += 7; } else if (std::strncmp(url, "https", 5) == 0) { _protocol = HTTPS; _port = 443; } else if (std::strncmp(url, "http", 4) == 0) { _protocol = HTTP; _port = 80; } else { exp[HTTPException::Error] << "Httpurl: protocol not supported!"; throw exp; } // Get the C-style pointer for string operations const char* c_url = url.c_str(); const char* p = c_url; if (http3) { _protocol = HttpProtocol::HTTP3; _port = 443; } // Update c_url pointer based on scheme matching else if (url.rfind("https://", 0) == 0) { // Using rfind for string prefix check _protocol = HTTPS; _port = 443; p += 8; // Advance pointer past "https://" } else if (url.rfind("http://", 0) == 0) { _protocol = HTTP; _port = 80; p += 7; // Advance pointer past "http://" } // Note: The "https" and "http" (without ://) checks are less robust but kept for compatibility else if (url.rfind("https", 0) == 0) { _protocol = HTTPS; _port = 443; } else if (url.rfind("http", 0) == 0) { _protocol = HTTP; _port = 80; } else { exp[HTTPException::Error] << "Httpurl: protocol not supported!"; throw exp; } _path = "/"; const char* p = url; const char* host_start = p; while (*p && *p != ':' && *p != '/') ++p; // Use the length of the original string to prevent reading past the end size_t url_len = url.length(); size_t offset = p - c_url; // Iterate through the remaining part of the string while (offset < url_len && *p && *p != ':' && *p != '/') { ++p; ++offset; } // Assign the host part _host.assign(host_start, p - host_start); if (*p == ':') { if (offset < url_len && *p == ':') { ++p; ++offset; const char* digits = p; while (*p && std::isdigit((unsigned char)*p)) ++p; // Iterate through the port digits while (offset < url_len && *p && std::isdigit((unsigned char)*p)) { ++p; ++offset; } if (p == digits || (p - digits) > 5) { exp[HTTPException::Error] << "Httpurl: Port range are Wrong !"; throw exp; exp[HTTPException::Error] << "Httpurl: Port range are Wrong !"; throw exp; } _port = std::atoi(std::string(digits, p - digits).c_str()); } if (*p == '/') _path.assign(p); // Check for the path part if (offset < url_len && *p == '/') { // Use string methods or assign from pointer // Here we use std::string::substr for clarity and safety with the remaining part of the URL _path = url.substr(p - c_url); } } libhttppp::HttpUrl::HttpUrl(const HttpUrl& src){ Loading Loading @@ -182,8 +229,10 @@ const std::vector<char> &libhttppp::HttpClient::Get(libhttppp::HttpRequest &nreq nreq.setHeaderData("host")->push_back(host.str()); nreq.setRequestType(GETREQUEST); if(nreq.getRequestURL().empty()) nreq.setRequestURL(_url.getPath()); if(nreq.getRequestVersion().empty()) nreq.setRequestVersion(HTTPVERSION(1.1)); nreq.send(*_cltsock); Loading
src/http.h +0 −1 Changes for src/http.h: 0 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -54,7 +54,6 @@ namespace libhttppp { }; HttpUrl(); HttpUrl(const char *url,bool http3=false); HttpUrl(const std::string &url,bool http3=false); HttpUrl(const HttpUrl &src); ~HttpUrl(); Loading