Skip to content

Commit dc3e4a7

Browse files
committed
Add namespace hv
1 parent f72c592 commit dc3e4a7

File tree

9 files changed

+56
-42
lines changed

9 files changed

+56
-42
lines changed

http/HttpMessage.h

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
#include "httpdef.h"
4747
#include "http_content.h"
4848

49-
struct HNetAddr {
49+
namespace hv {
50+
51+
struct NetAddr {
5052
std::string ip;
5153
int port;
5254

@@ -55,6 +57,8 @@ struct HNetAddr {
5557
}
5658
};
5759

60+
}
61+
5862
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
5963
// Cookie: sessionid=1; domain=.example.com; path=/; max-age=86400; secure; httponly
6064
struct HV_EXPORT HttpCookie {
@@ -111,7 +115,7 @@ class HV_EXPORT HttpMessage {
111115
http_content_type content_type;
112116
#ifndef WITHOUT_HTTP_CONTENT
113117
hv::Json json; // APPLICATION_JSON
114-
MultiPart form; // MULTIPART_FORM_DATA
118+
hv::MultiPart form; // MULTIPART_FORM_DATA
115119
hv::KeyValue kv; // X_WWW_FORM_URLENCODED
116120

117121
// T=[bool, int, int64_t, float, double]
@@ -130,7 +134,7 @@ class HV_EXPORT HttpMessage {
130134
json[key] = value;
131135
break;
132136
case MULTIPART_FORM_DATA:
133-
form[key] = FormData(value);
137+
form[key] = hv::FormData(value);
134138
break;
135139
case X_WWW_FORM_URLENCODED:
136140
kv[key] = hv::to_string(value);
@@ -175,17 +179,17 @@ class HV_EXPORT HttpMessage {
175179
// Content-Type: multipart/form-data
176180
template<typename T>
177181
void SetFormData(const char* name, const T& t) {
178-
form[name] = FormData(t);
182+
form[name] = hv::FormData(t);
179183
}
180184
void SetFormFile(const char* name, const char* filepath) {
181-
form[name] = FormData(NULL, filepath);
185+
form[name] = hv::FormData(NULL, filepath);
182186
}
183187
int FormFile(const char* name, const char* filepath) {
184188
content_type = MULTIPART_FORM_DATA;
185-
form[name] = FormData(NULL, filepath);
189+
form[name] = hv::FormData(NULL, filepath);
186190
return 200;
187191
}
188-
const MultiPart& GetForm() {
192+
const hv::MultiPart& GetForm() {
189193
if (form.empty() && ContentType() == MULTIPART_FORM_DATA) {
190194
ParseBody();
191195
}
@@ -206,7 +210,7 @@ class HV_EXPORT HttpMessage {
206210
ParseBody();
207211
if (form.empty()) return HTTP_STATUS_BAD_REQUEST;
208212
}
209-
const FormData& formdata = form[name];
213+
const hv::FormData& formdata = form[name];
210214
if (formdata.content.empty()) {
211215
return HTTP_STATUS_BAD_REQUEST;
212216
}
@@ -376,7 +380,11 @@ class HV_EXPORT HttpMessage {
376380
}
377381
};
378382

379-
#define DEFAULT_USER_AGENT "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
383+
#define DEFAULT_HTTP_USER_AGENT "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
384+
#define DEFAULT_HTTP_TIMEOUT 60 // s
385+
#define DEFAULT_HTTP_FAIL_RETRY_COUNT 1
386+
#define DEFAULT_HTTP_FAIL_RETRY_DELAY 1000 // ms
387+
380388
class HV_EXPORT HttpRequest : public HttpMessage {
381389
public:
382390
http_method method;
@@ -387,27 +395,32 @@ class HV_EXPORT HttpRequest : public HttpMessage {
387395
std::string host;
388396
int port;
389397
std::string path;
390-
QueryParams query_params;
398+
hv::QueryParams query_params;
391399
// client_addr
392-
HNetAddr client_addr; // for http server save client addr of request
393-
int timeout; // for http client timeout
394-
unsigned redirect: 1; // for http_client redirect
395-
unsigned proxy : 1; // for http_client proxy
400+
hv::NetAddr client_addr; // for http server save client addr of request
401+
// for HttpClient
402+
int timeout;
403+
int retry_count; // just for AsyncHttpClient fail retry
404+
int retry_delay; // just for AsyncHttpClient fail retry
405+
unsigned redirect: 1;
406+
unsigned proxy : 1;
396407

397408
HttpRequest() : HttpMessage() {
398409
type = HTTP_REQUEST;
399410
Init();
400411
}
401412

402413
void Init() {
403-
headers["User-Agent"] = DEFAULT_USER_AGENT;
414+
headers["User-Agent"] = DEFAULT_HTTP_USER_AGENT;
404415
headers["Accept"] = "*/*";
405416
method = HTTP_GET;
406417
scheme = "http";
407418
host = "127.0.0.1";
408419
port = DEFAULT_HTTP_PORT;
409420
path = "/";
410-
timeout = 0;
421+
timeout = DEFAULT_HTTP_TIMEOUT;
422+
retry_count = DEFAULT_HTTP_FAIL_RETRY_COUNT;
423+
retry_delay = DEFAULT_HTTP_FAIL_RETRY_DELAY;
411424
redirect = 1;
412425
proxy = 0;
413426
}

http/client/AsyncHttpClient.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ int AsyncHttpClient::doTask(const HttpClientTaskPtr& task) {
9191
iter->second.remove(channel->fd());
9292
}
9393
const HttpClientTaskPtr& task = ctx->task;
94-
if (task && task->retry_cnt-- > 0) {
95-
if (task->retry_delay) {
94+
if (task && task->req && task->req->retry_count-- > 0) {
95+
if (task->req->retry_delay > 0) {
9696
// try again after delay
97-
setTimeout(ctx->task->retry_delay, [this, task](TimerID timerID){
97+
setTimeout(task->req->retry_delay, [this, task](TimerID timerID){
9898
hlogi("retry %s %s", http_method_str(task->req->method), task->req->url.c_str());
9999
sendInLoop(task);
100100
});

http/client/AsyncHttpClient.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
#include "HttpMessage.h"
1010
#include "HttpParser.h"
1111

12-
#define DEFAULT_FAIL_RETRY_COUNT 3
13-
#define DEFAULT_FAIL_RETRY_DELAY 1000 // ms
14-
1512
// async => keepalive => connect_pool
1613

1714
namespace hv {
@@ -55,10 +52,7 @@ class ConnPool {
5552
struct HttpClientTask {
5653
HttpRequestPtr req;
5754
HttpResponseCallback cb;
58-
59-
uint64_t start_time;
60-
int retry_cnt;
61-
int retry_delay;
55+
uint64_t start_time;
6256
};
6357
typedef std::shared_ptr<HttpClientTask> HttpClientTaskPtr;
6458

@@ -118,8 +112,9 @@ class AsyncHttpClient {
118112
task->req = req;
119113
task->cb = std::move(resp_cb);
120114
task->start_time = hloop_now_hrtime(loop_thread.hloop());
121-
task->retry_delay = DEFAULT_FAIL_RETRY_DELAY;
122-
task->retry_cnt = MIN(DEFAULT_FAIL_RETRY_COUNT, req->timeout * 1000 / task->retry_delay - 1);
115+
if (req->retry_count > 0 && req->retry_delay > 0) {
116+
req->retry_count = MIN(req->retry_count, req->timeout * 1000 / req->retry_delay - 1);
117+
}
123118
return send(task);
124119
}
125120

http/client/axios.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ HV_INLINE Request newRequestFromJson(const json& jreq) {
7171
}
7272
// params
7373
if (jreq.contains("params")) {
74-
req->query_params = jreq["params"].get<QueryParams>();
74+
req->query_params = jreq["params"].get<hv::QueryParams>();
7575
}
7676
// headers
7777
if (jreq.contains("headers")) {

http/client/http_client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ int main(int argc, char* argv[]) {
2727
}
2828
*/
2929

30-
#define DEFAULT_HTTP_TIMEOUT 60 // s
3130
typedef struct http_client_s http_client_t;
3231

3332
HV_EXPORT http_client_t* http_client_new(const char* host = NULL, int port = DEFAULT_HTTP_PORT, int https = 0);
3433
HV_EXPORT int http_client_close(http_client_t* cli);
3534
HV_EXPORT int http_client_del(http_client_t* cli);
3635
HV_EXPORT const char* http_client_strerror(int errcode);
3736

37+
// timeout: s
3838
HV_EXPORT int http_client_set_timeout(http_client_t* cli, int timeout);
3939

4040
// SSL/TLS

http/client/requests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ HV_INLINE Response patch(const char* url, const http_body& body = NoBody, const
103103
}
104104

105105
// delete is c++ keyword, we have to replace delete with Delete.
106-
HV_INLINE Response Delete(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders) {
107-
return request(HTTP_DELETE, url, body, headers);
106+
HV_INLINE Response Delete(const char* url, const http_headers& headers = DefaultHeaders) {
107+
return request(HTTP_DELETE, url, NoBody, headers);
108108
}
109109

110110
HV_INLINE int async(Request req, ResponseCallback resp_cb) {

http/http_content.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include <string.h>
66

7-
using namespace hv;
7+
BEGIN_NAMESPACE_HV
88

99
std::string dump_query_params(const QueryParams& query_params) {
1010
std::string query_string;
@@ -248,3 +248,5 @@ int parse_json(const char* str, hv::Json& json, std::string& errmsg) {
248248
return (json.is_discarded() || json.is_null()) ? -1 : 0;
249249
}
250250
#endif
251+
252+
END_NAMESPACE_HV

http/http_content.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
#include "hexport.h"
55
#include "hstring.h"
66

7+
// NOTE: WITHOUT_HTTP_CONTENT
8+
// ndk-r10e no std::to_string and can't compile modern json.hpp
9+
#ifndef WITHOUT_HTTP_CONTENT
10+
#include "json.hpp" // https://github.com/nlohmann/json
11+
#endif
12+
13+
BEGIN_NAMESPACE_HV
14+
715
// QueryParams
8-
typedef hv::KeyValue QueryParams;
16+
using QueryParams = hv::KeyValue;
917
HV_EXPORT std::string dump_query_params(const QueryParams& query_params);
1018
HV_EXPORT int parse_query_params(const char* query_string, QueryParams& query_params);
1119

12-
// NOTE: WITHOUT_HTTP_CONTENT
13-
// ndk-r10e no std::to_string and can't compile modern json.hpp
1420
#ifndef WITHOUT_HTTP_CONTENT
1521

1622
/**************multipart/form-data*************************************
@@ -60,15 +66,13 @@ HV_EXPORT std::string dump_multipart(MultiPart& mp, const char* boundary = DEFAU
6066
HV_EXPORT int parse_multipart(const std::string& str, MultiPart& mp, const char* boundary);
6167

6268
// Json
63-
// https://github.com/nlohmann/json
64-
#include "json.hpp"
65-
namespace hv { // NOTE: Avoid conflict with jsoncpp
6669
using Json = nlohmann::json;
6770
// using Json = nlohmann::ordered_json;
68-
}
6971

7072
HV_EXPORT std::string dump_json(const hv::Json& json, int indent = -1);
7173
HV_EXPORT int parse_json(const char* str, hv::Json& json, std::string& errmsg);
7274
#endif
7375

76+
END_NAMESPACE_HV
77+
7478
#endif // HV_HTTP_CONTENT_H_

http/server/HttpContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct HV_EXPORT HttpContext {
4444
return request->GetHeader(key, defvalue);
4545
}
4646

47-
const QueryParams& params() {
47+
const hv::QueryParams& params() {
4848
return request->query_params;
4949
}
5050

@@ -79,7 +79,7 @@ struct HV_EXPORT HttpContext {
7979
}
8080

8181
// Content-Type: multipart/form-data
82-
const MultiPart& form() {
82+
const hv::MultiPart& form() {
8383
return request->GetForm();
8484
}
8585
std::string form(const char* name, const std::string& defvalue = hv::empty_string) {

0 commit comments

Comments
 (0)