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
6064struct 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+
380388class HV_EXPORT HttpRequest : public HttpMessage {
381389public:
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 }
0 commit comments