@@ -46,34 +46,38 @@ UrlComponents parse_url(const std::string& url) {
4646std::string SendHttpPostRequest (const char * url, const char * data)
4747{
4848 if (!url || url[0 ] == ' \0 ' || !data)
49- return " " ;
49+ return " [ERROR] Bad hostname or request " ;
5050
5151 UrlComponents uril = parse_url (url);
5252 try
5353 {
5454 httplib::Client client (uril.host );
5555
56+ client.set_follow_location (true );
57+ client.set_read_timeout (20 , 0 );
58+ client.set_write_timeout (20 , 0 );
59+ client.set_connection_timeout (10 , 0 );
60+
5661 auto res = client.Post (uril.path , data, " text/plain" );
5762
5863 if (res)
5964 {
60- if (res->status == httplib::MovedPermanently_301 || res->status == httplib::PermanentRedirect_308)
61- {
62- client.set_follow_location (true );
63- res = client.Post (uril.path , data, " text/plain" );
64- }
65-
66- if (res && res->status == httplib::StatusCode::OK_200 )
65+ if (res->status == httplib::StatusCode::OK_200 )
6766 {
6867 DownStatus = 1 ;
6968 return res->body ;
7069 }
70+ else
71+ {
72+ auto err = res.error ();
73+ DownStatus = -1 ;
74+ return " [ERROR] HTTP error: " + httplib::to_string (err);
75+ }
7176 }
72- if (res)
77+ else
7378 {
74- auto err = res.error ();
7579 DownStatus = -1 ;
76- return " [POST] HTTP error: " + httplib::to_string (err). substr ( 0 , 64 ) ;
80+ return " [ERROR] No response " ;
7781 }
7882 }
7983 catch (std::exception& ex)
@@ -86,43 +90,43 @@ std::string SendHttpPostRequest(const char* url, const char* data)
8690
8791 }
8892 DownStatus = -2 ;
89- return " " ;
93+ return " [ERROR] Unhandled error " ;
9094}
9195
9296std::string SendHttpGetRequest (const char * host, const char * path)
9397{
9498 if (!host || host[0 ] == ' \0 ' || !path)
95- return " " ;
99+ return " [ERROR] Bad hostname or request " ;
96100
97101 try
98102 {
99103 httplib::Client client (host);
100104
101105 auto res = client.Get (path);
102106
103- client.set_read_timeout (5 , 0 );
104- client.set_write_timeout (5 , 0 );
105- client.set_connection_timeout (5 , 0 );
107+ client.set_follow_location (true );
108+ client.set_read_timeout (20 , 0 );
109+ client.set_write_timeout (20 , 0 );
110+ client.set_connection_timeout (10 , 0 );
106111
107112 if (res)
108113 {
109- if (res->status == httplib::MovedPermanently_301 || res->status == httplib::PermanentRedirect_308)
110- {
111- client.set_follow_location (true );
112- res = client.Get (path);
113- }
114-
115- if (res && res->status == httplib::StatusCode::OK_200 )
114+ if (res->status == httplib::StatusCode::OK_200 )
116115 {
117116 DownStatus = 1 ;
118117 return res->body ;
119118 }
119+ else
120+ {
121+ auto err = res.error ();
122+ DownStatus = -1 ;
123+ return " [ERROR] HTTP error: " + httplib::to_string (err);
124+ }
120125 }
121- if (res)
126+ else
122127 {
123- auto err = res.error ();
124128 DownStatus = -1 ;
125- return " [GET] HTTP error: " + httplib::to_string (err). substr ( 0 , 64 ) ;
129+ return " [ERROR] No response " ;
126130 }
127131 }
128132 catch (std::exception& ex)
@@ -136,7 +140,7 @@ std::string SendHttpGetRequest(const char* host, const char* path)
136140 }
137141
138142 DownStatus = -2 ;
139- return " " ;
143+ return " [ERROR] Unhandled error " ;
140144}
141145
142146
@@ -189,7 +193,7 @@ void DownloadNewMapToFile(const char* szUrl, const char* filepath)
189193 if (res)
190194 {
191195 auto err = res.error ();
192- LatestDownloadedString = " [MAP DOWNLOAD ] HTTP error: " + httplib::to_string (err).substr (0 , 64 );
196+ LatestDownloadedString = " [ERROR ] HTTP error: " + httplib::to_string (err).substr (0 , 64 );
193197 DownStatus = -1 ;
194198 DownProgress = 0 ;
195199 }
@@ -205,7 +209,7 @@ int __stdcall SendGetRequest(const char* url, const char* path)
205209{
206210 if (!avaiableNow)
207211 return 0 ;
208- LatestDownloadedString = " Error: you need wait response!" ;
212+ LatestDownloadedString = " [ERROR] you need wait response!" ;
209213 avaiableNow = false ;
210214 DownProgress = 0 ;
211215 DownStatus = 0 ;
@@ -233,7 +237,7 @@ int __stdcall SendPostRequest(const char* url, const char* request)
233237{
234238 if (!avaiableNow)
235239 return 0 ;
236- LatestDownloadedString = " Error: you need wait response!" ;
240+ LatestDownloadedString = " [ERROR] You need wait response!" ;
237241 avaiableNow = false ;
238242 DownProgress = 0 ;
239243 DownStatus = 0 ;
@@ -258,7 +262,7 @@ int __stdcall SendPostRequestEx(const char* url, const char* path, const char*
258262{
259263 if (!avaiableNow)
260264 return 0 ;
261- LatestDownloadedString = " Error: you need wait response!" ;
265+ LatestDownloadedString = " [ERROR] You need wait response!" ;
262266 avaiableNow = false ;
263267 DownProgress = 0 ;
264268 DownStatus = 0 ;
@@ -310,7 +314,20 @@ const char* __stdcall GetLatestDownloadedString(int)
310314 LatestDownloadedString = LatestDownloadedString.substr (0 , 1023 );
311315 if (SetInfoObjDebugVal)
312316 {
313- PrintText ((" Recv data:" + LatestDownloadedString.substr (0 ,100 )).c_str ());
317+ const std::string& str = LatestDownloadedString;
318+ size_t length = str.length ();
319+ size_t pos = 0 ;
320+ size_t chunkSize = 100 ;
321+
322+ while (pos < length)
323+ {
324+ std::string chunk = str.substr (pos, chunkSize);
325+ if (pos)
326+ PrintText ((" Recv data:" + chunk).c_str ());
327+ else
328+ PrintText (chunk.c_str ());
329+ pos += chunkSize;
330+ }
314331 }
315332 return LatestDownloadedString.c_str ();
316333}
0 commit comments