Skip to content

Commit 8e4ab84

Browse files
committed
Fix bugs
1 parent 6633fd9 commit 8e4ab84

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

moxt/cclient.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ CHttpResponse CClient::doRequest(const std::string &path,
6666
client->set_header_container(&resHeaders);
6767
}
6868

69-
client->append_header("Host", host);
70-
client->append_header("User-Agent", "xt/1.0.1");
69+
// client->append_header("Host", host);
70+
// client->append_header("User-Agent", "xt/1.0.1");
7171

7272
// logd("--------------header--------------");
7373
for (auto &a : headers) {
@@ -121,8 +121,9 @@ CHttpResponse CClient::doRequest(const std::string &path,
121121

122122
if (ret != 200) {
123123
logw("connect to server failed. http response code: {}", ret);
124-
return CHttpResponse{
125-
500, fmt::format("请求失败 status={} body={}", ret, writer.string)};
124+
return CHttpResponse{500,
125+
fmt::format("Request failed status={} body={}",
126+
ret, writer.string)};
126127
}
127128

128129
return CHttpResponse{static_cast<uint64_t>(ret), writer.string};
@@ -188,8 +189,9 @@ CClient::doRequestTest(const std::string &path, photon::net::http::Verb verb,
188189

189190
if (ret != 200) {
190191
logw("connect to server failed. http response code: {}", ret);
191-
return CHttpResponse{
192-
500, fmt::format("请求失败 status={} body={}", ret, writer.string)};
192+
return CHttpResponse{500,
193+
fmt::format("Request failed status={} body={}",
194+
ret, writer.string)};
193195
}
194196

195197
return CHttpResponse{static_cast<uint64_t>(ret), writer.string};
@@ -209,7 +211,7 @@ SEQ_FUNC void seq_cclient_free(CClient *client) {
209211
SEQ_FUNC int64_t seq_cclient_do_request(
210212
CClient *client, const char *path, size_t path_len, int64_t verb,
211213
std::map<std::string, std::string> *headers, const char *body,
212-
size_t body_len, char *res, size_t *n, bool verbose) {
214+
size_t body_len, char *res, size_t res_len, size_t *n, bool verbose) {
213215
// logd("seq_cclient_do_request");
214216
std::string reqeustPath = std::string(path, path_len);
215217
std::string body_(body, body_len);
@@ -228,7 +230,14 @@ SEQ_FUNC int64_t seq_cclient_do_request(
228230
auto result = client->doRequest(reqeustPath, v, *headers, body_, verbose);
229231
// logd("seq_cclient_do_request success");
230232
auto &res_body = result.body;
231-
memcpy(res, res_body.c_str(), res_body.length());
232-
*n = res_body.length();
233+
if (res_body.length() < res_len) {
234+
memcpy(res, res_body.c_str(), res_body.length());
235+
*n = res_body.length();
236+
} else {
237+
memcpy(res, res_body.c_str(), res_len);
238+
*n = res_len;
239+
logw("response body is too long. baseUrl={}, path={}",
240+
client->getBaseUrl(), reqeustPath);
241+
}
233242
return result.status_code;
234243
}

moxt/cclient.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class CClient {
3232

3333
~CClient();
3434

35+
// Get baseUrl
36+
string getBaseUrl() { return baseUrl; }
37+
3538
CHttpResponse doRequest(const std::string &path,
3639
photon::net::http::Verb verb,
3740
std::map<std::string, std::string> &headers,
@@ -59,6 +62,6 @@ SEQ_FUNC void seq_cclient_free(CClient *client);
5962
SEQ_FUNC int64_t seq_cclient_do_request(
6063
CClient *client, const char *path, size_t path_len, int64_t verb,
6164
std::map<std::string, std::string> *headers, const char *body,
62-
size_t body_len, char *res, size_t *n, bool verbose);
65+
size_t body_len, char *res, size_t res_len, size_t *n, bool verbose);
6366

6467
#endif

0 commit comments

Comments
 (0)