1 From b7db370449cfe2b453c22b74a660701f78bd7ce3 Mon Sep 17 00:00:00 2001
2 From: Anatolii Lapytskyi <ala@allunite.com>
3 Date: Tue, 6 Sep 2022 15:59:27 +0200
4 Subject: [PATCH] Remove dependency on libxml2
9 SpeedTest.cpp | 136 ++++++++++++++-----------------------------------
11 4 files changed, 41 insertions(+), 107 deletions(-)
15 @@ -44,7 +44,6 @@ add_executable(SpeedTest ${SOURCE_FILES}
17 INCLUDE (CheckIncludeFiles)
18 find_package(CURL REQUIRED)
19 -find_package(LibXml2 REQUIRED)
22 find_package(OpenSSL REQUIRED)
23 @@ -52,7 +51,7 @@ else()
24 CHECK_INCLUDE_FILES("CommonCrypto/CommonDigest.h" HAVE_COMMON_DIGEST_H)
27 -include_directories(${CURL_INCLUDE_DIRS} ${LIBXML2_INCLUDE_DIR})
28 -target_link_libraries(SpeedTest ${CURL_LIBRARIES} ${LIBXML2_LIBRARIES} -lpthread ${OPENSSL_LIBRARIES})
29 +include_directories(${CURL_INCLUDE_DIRS})
30 +target_link_libraries(SpeedTest ${CURL_LIBRARIES} -lpthread ${OPENSSL_LIBRARIES})
32 install(TARGETS SpeedTest RUNTIME DESTINATION bin)
35 @@ -26,7 +26,6 @@ It supports the new (undocumented) raw T
43 @@ -40,7 +39,7 @@ $ make install
47 -$ sudo apt-get install build-essential libcurl4-openssl-dev libxml2-dev libssl-dev cmake
48 +$ sudo apt-get install build-essential libcurl4-openssl-dev libssl-dev cmake
49 $ git clone https://github.com/taganaka/SpeedTest
51 $ cmake -DCMAKE_BUILD_TYPE=Release .
52 @@ -50,7 +49,7 @@ $ sudo make install
56 -$ sudo zypper install cmake gcc-c++ libcurl-devel libxml2-devel libopenssl-devel git
57 +$ sudo zypper install cmake gcc-c++ libcurl-devel libopenssl-devel git
58 $ git clone https://github.com/taganaka/SpeedTest
60 $ cmake -DCMAKE_BUILD_TYPE=Release .
63 @@ -353,67 +353,16 @@ std::vector<std::string> SpeedTest::spli
67 -ServerInfo SpeedTest::processServerXMLNode(xmlTextReaderPtr reader) {
69 - auto name = xmlTextReaderConstName(reader);
70 - auto nodeName = std::string((char*)name);
72 - if (!name || nodeName != "server"){
73 - return ServerInfo();
76 - if (xmlTextReaderAttributeCount(reader) > 0){
77 - auto info = ServerInfo();
78 - auto server_url = xmlTextReaderGetAttribute(reader, BAD_CAST "url");
79 - auto server_lat = xmlTextReaderGetAttribute(reader, BAD_CAST "lat");
80 - auto server_lon = xmlTextReaderGetAttribute(reader, BAD_CAST "lon");
81 - auto server_name = xmlTextReaderGetAttribute(reader, BAD_CAST "name");
82 - auto server_county = xmlTextReaderGetAttribute(reader, BAD_CAST "country");
83 - auto server_cc = xmlTextReaderGetAttribute(reader, BAD_CAST "cc");
84 - auto server_host = xmlTextReaderGetAttribute(reader, BAD_CAST "host");
85 - auto server_id = xmlTextReaderGetAttribute(reader, BAD_CAST "id");
86 - auto server_sponsor = xmlTextReaderGetAttribute(reader, BAD_CAST "sponsor");
89 - info.name.append((char*)server_name);
92 - info.url.append((char*)server_url);
95 - info.country.append((char*)server_county);
98 - info.country_code.append((char*)server_cc);
101 - info.host.append((char*)server_host);
103 - if (server_sponsor)
104 - info.sponsor.append((char*)server_sponsor);
107 - info.id = std::atoi((char*)server_id);
110 - info.lat = std::stof((char*)server_lat);
113 - info.lon = std::stof((char*)server_lon);
115 - xmlFree(server_url);
116 - xmlFree(server_lat);
117 - xmlFree(server_lon);
118 - xmlFree(server_name);
119 - xmlFree(server_county);
120 - xmlFree(server_cc);
121 - xmlFree(server_host);
122 - xmlFree(server_id);
123 - xmlFree(server_sponsor);
127 - return ServerInfo();
128 +std::string getAttributeValue(const std::string& data, const size_t offset, const size_t max_pos, const std::string& attribute_name) {
129 + size_t pos = data.find(attribute_name + "=\"", offset);
130 + if (pos == std::string::npos)
132 + if (pos >= max_pos)
134 + size_t value_pos = pos + attribute_name.length() + 2;
135 + size_t end = data.find("\"", value_pos);
136 + std::string s = data.substr(pos + attribute_name.length() + 2, end - value_pos);
140 bool SpeedTest::fetchServers(const std::string& url, std::vector<ServerInfo>& target, int &http_code) {
141 @@ -441,53 +390,42 @@ bool SpeedTest::fetchServers(const std::
145 - size_t len = oss.str().length();
146 - auto *xmlbuff = (char*)calloc(len + 1, sizeof(char));
148 - std::cerr << "Unable to calloc" << std::endl;
150 + if (!SpeedTest::ipInfo(ipInfo)){
151 curl_easy_cleanup(curl);
152 + std::cerr << "OOPS!" <<std::endl;
155 - memcpy(xmlbuff, oss.str().c_str(), len);
158 - xmlTextReaderPtr reader = xmlReaderForMemory(xmlbuff, static_cast<int>(len), nullptr, nullptr, 0);
159 + std::string data = oss.str();
161 - if (reader != nullptr) {
163 - if (!SpeedTest::ipInfo(ipInfo)){
164 - curl_easy_cleanup(curl);
166 - xmlFreeTextReader(reader);
167 - std::cerr << "OOPS!" <<std::endl;
170 - auto ret = xmlTextReaderRead(reader);
172 - ServerInfo info = processServerXMLNode(reader);
173 - if (!info.url.empty()){
174 - info.distance = harversine(std::make_pair(ipInfo.lat, ipInfo.lon), std::make_pair(info.lat, info.lon));
175 - target.push_back(info);
177 - ret = xmlTextReaderRead(reader);
179 - xmlFreeTextReader(reader);
181 - curl_easy_cleanup(curl);
183 - std::cerr << "Failed to parse" << std::endl;
185 + const std::string server_tag_start = "<server ";
187 + size_t server_tag_begin = data.find(server_tag_start);
188 + while (server_tag_begin != std::string::npos) {
189 + size_t server_tag_end = data.find("/>", server_tag_begin);
191 + auto info = ServerInfo();
192 + info.name = getAttributeValue(data, server_tag_begin, server_tag_end, "name");
193 + info.url = getAttributeValue(data, server_tag_begin, server_tag_end, "url");
194 + info.country = getAttributeValue(data, server_tag_begin, server_tag_end, "country");
195 + info.country_code = getAttributeValue(data, server_tag_begin, server_tag_end, "cc");
196 + info.host = getAttributeValue(data, server_tag_begin, server_tag_end, "host");
197 + info.sponsor = getAttributeValue(data, server_tag_begin, server_tag_end, "sponsor");
198 + info.id = atoi(getAttributeValue(data, server_tag_begin, server_tag_end, "id").c_str());
199 + info.lat = std::stof(getAttributeValue(data, server_tag_begin, server_tag_end, "lat"));
200 + info.lon = std::stof(getAttributeValue(data, server_tag_begin, server_tag_end, "lon"));
202 + if (!info.url.empty()){
203 + info.distance = harversine(std::make_pair(ipInfo.lat, ipInfo.lon), std::make_pair(info.lat, info.lon));
204 + target.push_back(info);
207 - std::cerr << "Unable to initialize xml parser" << std::endl;
208 - curl_easy_cleanup(curl);
212 + server_tag_begin = data.find(server_tag_start, server_tag_begin + 1);
216 curl_easy_cleanup(curl);
218 - xmlCleanupParser();
219 std::sort(target.begin(), target.end(), [](const ServerInfo &a, const ServerInfo &b) -> bool {
220 return a.distance < b.distance;
226 #include "SpeedTestConfig.h"
227 #include "SpeedTestClient.h"
228 -#include <libxml/xmlreader.h>
229 #include <functional>
231 #include <curl/curl.h>
232 @@ -50,7 +49,6 @@ private:
233 const ServerInfo findBestServerWithin(const std::vector<ServerInfo>& serverList, long& latency, int sample_size = 5, std::function<void(bool)> cb = nullptr);
234 static CURL* curl_setup(CURL* curl = nullptr);
235 static size_t writeFunc(void* buf, size_t size, size_t nmemb, void* userp);
236 - static ServerInfo processServerXMLNode(xmlTextReaderPtr reader);
237 double execute(const ServerInfo &server, const TestConfig &config, const opFn &fnc, std::function<void(bool)> cb = nullptr);
238 template <typename T>
239 static T deg2rad(T n);