1
0

HttpMessage.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. #ifndef HV_HTTP_MESSAGE_H_
  2. #define HV_HTTP_MESSAGE_H_
  3. /*
  4. * @class HttpMessage
  5. * HttpRequest extends HttpMessage
  6. * HttpResponse extends HttpMessage
  7. *
  8. * @member
  9. * request-line: GET / HTTP/1.1\r\n => method path
  10. * response-line: 200 OK\r\n => status_code
  11. * headers
  12. * body
  13. *
  14. * content, content_length, content_type
  15. * json, form, kv
  16. *
  17. * @function
  18. * Content, ContentLength, ContentType
  19. * ParseUrl, ParseBody
  20. * DumpUrl, DumpHeaders, DumpBody, Dump
  21. * GetJson, GetForm, GetUrlEncoded
  22. * GetHeader, GetParam, GetString, GetBool, GetInt, GetFloat
  23. * String, Data, File, Json, FormFile, SetFormData, SetUrlEncoded
  24. * Get, Set
  25. *
  26. * @example
  27. * examples/http_server_test.cpp
  28. * examples/http_client_test.cpp
  29. * examples/httpd
  30. *
  31. */
  32. #include <memory>
  33. #include <string>
  34. #include <map>
  35. #include <functional>
  36. #include "hexport.h"
  37. #include "hbase.h"
  38. #include "hstring.h"
  39. #include "hfile.h"
  40. #include "hpath.h"
  41. #include "httpdef.h"
  42. #include "http_content.h"
  43. struct HNetAddr {
  44. std::string ip;
  45. int port;
  46. std::string ipport() {
  47. return hv::asprintf("%s:%d", ip.c_str(), port);
  48. }
  49. };
  50. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
  51. // Cookie: sessionid=1; domain=.example.com; path=/; max-age=86400; secure; httponly
  52. struct HV_EXPORT HttpCookie {
  53. std::string name;
  54. std::string value;
  55. std::string domain;
  56. std::string path;
  57. std::string expires;
  58. int max_age;
  59. bool secure;
  60. bool httponly;
  61. enum SameSite {
  62. Default,
  63. Strict,
  64. Lax,
  65. None
  66. } samesite;
  67. HttpCookie() {
  68. max_age = 0;
  69. secure = false;
  70. httponly = false;
  71. samesite = Default;
  72. }
  73. bool parse(const std::string& str);
  74. std::string dump() const;
  75. };
  76. typedef std::map<std::string, std::string, hv::StringCaseLess> http_headers;
  77. typedef std::vector<HttpCookie> http_cookies;
  78. typedef std::string http_body;
  79. typedef std::function<void(const http_headers& headers)> http_head_cb;
  80. typedef std::function<void(const char* data, size_t size)> http_body_cb;
  81. typedef std::function<void(const char* data, size_t size)> http_chunked_cb;
  82. HV_EXPORT extern http_headers DefaultHeaders;
  83. HV_EXPORT extern http_body NoBody;
  84. class HV_EXPORT HttpMessage {
  85. public:
  86. static char s_date[32];
  87. int type;
  88. unsigned short http_major;
  89. unsigned short http_minor;
  90. http_headers headers;
  91. http_cookies cookies;
  92. http_body body;
  93. http_head_cb head_cb;
  94. http_body_cb body_cb;
  95. http_chunked_cb chunked_cb; // Transfer-Encoding: chunked
  96. // structured content
  97. void* content; // DATA_NO_COPY
  98. int content_length;
  99. http_content_type content_type;
  100. #ifndef WITHOUT_HTTP_CONTENT
  101. hv::Json json; // APPLICATION_JSON
  102. MultiPart form; // MULTIPART_FORM_DATA
  103. hv::KeyValue kv; // X_WWW_FORM_URLENCODED
  104. // T=[bool, int, int64_t, float, double]
  105. template<typename T>
  106. T Get(const char* key, T defvalue = 0);
  107. std::string GetString(const char* key, const std::string& = "");
  108. bool GetBool(const char* key, bool defvalue = 0);
  109. int64_t GetInt(const char* key, int64_t defvalue = 0);
  110. double GetFloat(const char* key, double defvalue = 0);
  111. template<typename T>
  112. void Set(const char* key, const T& value) {
  113. switch (ContentType()) {
  114. case APPLICATION_JSON:
  115. json[key] = value;
  116. break;
  117. case MULTIPART_FORM_DATA:
  118. form[key] = FormData(value);
  119. break;
  120. case X_WWW_FORM_URLENCODED:
  121. kv[key] = hv::to_string(value);
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. /*
  128. * @usage https://github.com/nlohmann/json
  129. *
  130. * null: Json(nullptr);
  131. * boolean: Json(true);
  132. * number: Json(123);
  133. * string: Json("hello");
  134. * object: Json(std::map<string, ValueType>);
  135. * Json(hv::Json::object({
  136. {"k1", "v1"},
  137. {"k2", "v2"}
  138. }));
  139. * array: Json(std::vector<ValueType>);
  140. Json(hv::Json::array(
  141. {1, 2, 3}
  142. ));
  143. */
  144. // Content-Type: application/json
  145. template<typename T>
  146. int Json(const T& t) {
  147. content_type = APPLICATION_JSON;
  148. json = t;
  149. return 200;
  150. }
  151. const hv::Json& GetJson() {
  152. if (json.empty() && ContentType() == APPLICATION_JSON) {
  153. ParseBody();
  154. }
  155. return json;
  156. }
  157. // Content-Type: multipart/form-data
  158. template<typename T>
  159. void SetFormData(const char* name, const T& t) {
  160. form[name] = FormData(t);
  161. }
  162. void SetFormFile(const char* name, const char* filepath) {
  163. form[name] = FormData(NULL, filepath);
  164. }
  165. int FormFile(const char* name, const char* filepath) {
  166. content_type = MULTIPART_FORM_DATA;
  167. form[name] = FormData(NULL, filepath);
  168. return 200;
  169. }
  170. const MultiPart& GetForm() {
  171. if (form.empty() && ContentType() == MULTIPART_FORM_DATA) {
  172. ParseBody();
  173. }
  174. return form;
  175. }
  176. std::string GetFormData(const char* name, const std::string& defvalue = hv::empty_string) {
  177. if (form.empty() && ContentType() == MULTIPART_FORM_DATA) {
  178. ParseBody();
  179. }
  180. auto iter = form.find(name);
  181. return iter == form.end() ? defvalue : iter->second.content;
  182. }
  183. int SaveFormFile(const char* name, const char* path) {
  184. if (ContentType() != MULTIPART_FORM_DATA) {
  185. return HTTP_STATUS_BAD_REQUEST;
  186. }
  187. if (form.empty()) {
  188. ParseBody();
  189. if (form.empty()) return HTTP_STATUS_BAD_REQUEST;
  190. }
  191. const FormData& formdata = form[name];
  192. if (formdata.content.empty()) {
  193. return HTTP_STATUS_BAD_REQUEST;
  194. }
  195. std::string filepath(path);
  196. if (HPath::isdir(path)) {
  197. filepath = HPath::join(filepath, formdata.filename);
  198. }
  199. HFile file;
  200. if (file.open(filepath.c_str(), "wb") != 0) {
  201. return HTTP_STATUS_INTERNAL_SERVER_ERROR;
  202. }
  203. file.write(formdata.content.data(), formdata.content.size());
  204. return 200;
  205. }
  206. // Content-Type: application/x-www-form-urlencoded
  207. template<typename T>
  208. void SetUrlEncoded(const char* key, const T& t) {
  209. kv[key] = hv::to_string(t);
  210. }
  211. const hv::KeyValue& GetUrlEncoded() {
  212. if (kv.empty() && ContentType() == X_WWW_FORM_URLENCODED) {
  213. ParseBody();
  214. }
  215. return kv;
  216. }
  217. std::string GetUrlEncoded(const char* key, const std::string& defvalue = hv::empty_string) {
  218. if (kv.empty() && ContentType() == X_WWW_FORM_URLENCODED) {
  219. ParseBody();
  220. }
  221. auto iter = kv.find(key);
  222. return iter == kv.end() ? defvalue : iter->second;
  223. }
  224. #endif
  225. HttpMessage() {
  226. type = HTTP_BOTH;
  227. Init();
  228. }
  229. virtual ~HttpMessage() {}
  230. void Init() {
  231. http_major = 1;
  232. http_minor = 1;
  233. content = NULL;
  234. content_length = 0;
  235. content_type = CONTENT_TYPE_NONE;
  236. }
  237. virtual void Reset() {
  238. Init();
  239. headers.clear();
  240. body.clear();
  241. #ifndef WITHOUT_HTTP_CONTENT
  242. json.clear();
  243. form.clear();
  244. kv.clear();
  245. #endif
  246. }
  247. // structured-content -> content_type <-> headers Content-Type
  248. void FillContentType();
  249. // body.size -> content_length <-> headers Content-Length
  250. void FillContentLength();
  251. bool IsChunked();
  252. bool IsKeepAlive();
  253. // headers
  254. void SetHeader(const char* key, const std::string& value) {
  255. headers[key] = value;
  256. }
  257. std::string GetHeader(const char* key, const std::string& defvalue = hv::empty_string) {
  258. auto iter = headers.find(key);
  259. return iter == headers.end() ? defvalue : iter->second;
  260. }
  261. // body
  262. void SetBody(const std::string& body) {
  263. this->body = body;
  264. }
  265. const std::string& Body() {
  266. return this->body;
  267. }
  268. // headers -> string
  269. void DumpHeaders(std::string& str);
  270. // structured content -> body
  271. void DumpBody();
  272. void DumpBody(std::string& str);
  273. // body -> structured content
  274. // @retval 0:succeed
  275. int ParseBody();
  276. virtual std::string Dump(bool is_dump_headers, bool is_dump_body);
  277. void* Content() {
  278. if (content == NULL && body.size() != 0) {
  279. content = (void*)body.data();
  280. }
  281. return content;
  282. }
  283. int ContentLength() {
  284. if (content_length == 0) {
  285. FillContentLength();
  286. }
  287. return content_length;
  288. }
  289. http_content_type ContentType() {
  290. if (content_type == CONTENT_TYPE_NONE) {
  291. FillContentType();
  292. }
  293. return content_type;
  294. }
  295. void AddCookie(const HttpCookie& cookie) {
  296. cookies.push_back(cookie);
  297. }
  298. int String(const std::string& str) {
  299. content_type = TEXT_PLAIN;
  300. body = str;
  301. return 200;
  302. }
  303. int Data(void* data, int len, bool nocopy = true) {
  304. content_type = APPLICATION_OCTET_STREAM;
  305. if (nocopy) {
  306. content = data;
  307. content_length = len;
  308. } else {
  309. content_length = body.size();
  310. body.resize(content_length + len);
  311. memcpy((void*)(body.data() + content_length), data, len);
  312. content_length += len;
  313. }
  314. return 200;
  315. }
  316. int File(const char* filepath) {
  317. HFile file;
  318. if (file.open(filepath, "rb") != 0) {
  319. return HTTP_STATUS_NOT_FOUND;
  320. }
  321. const char* suffix = hv_suffixname(filepath);
  322. if (suffix) {
  323. content_type = http_content_type_enum_by_suffix(suffix);
  324. }
  325. if (content_type == CONTENT_TYPE_NONE || content_type == CONTENT_TYPE_UNDEFINED) {
  326. content_type = APPLICATION_OCTET_STREAM;
  327. }
  328. file.readall(body);
  329. return 200;
  330. }
  331. int SaveFile(const char* filepath) {
  332. HFile file;
  333. if (file.open(filepath, "wb") != 0) {
  334. return HTTP_STATUS_NOT_FOUND;
  335. }
  336. file.write(body.data(), body.size());
  337. return 200;
  338. }
  339. };
  340. #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"
  341. class HV_EXPORT HttpRequest : public HttpMessage {
  342. public:
  343. http_method method;
  344. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  345. std::string url;
  346. // structured url
  347. std::string scheme;
  348. std::string host;
  349. int port;
  350. std::string path;
  351. QueryParams query_params;
  352. // client_addr
  353. HNetAddr client_addr; // for http server save client addr of request
  354. int timeout; // for http client timeout
  355. unsigned redirect: 1; // for http_client redirect
  356. unsigned proxy : 1; // for http_client proxy
  357. HttpRequest() : HttpMessage() {
  358. type = HTTP_REQUEST;
  359. Init();
  360. }
  361. void Init() {
  362. headers["User-Agent"] = DEFAULT_USER_AGENT;
  363. headers["Accept"] = "*/*";
  364. method = HTTP_GET;
  365. scheme = "http";
  366. host = "127.0.0.1";
  367. port = DEFAULT_HTTP_PORT;
  368. path = "/";
  369. timeout = 0;
  370. redirect = 1;
  371. proxy = 0;
  372. }
  373. virtual void Reset() {
  374. HttpMessage::Reset();
  375. Init();
  376. url.clear();
  377. query_params.clear();
  378. }
  379. virtual std::string Dump(bool is_dump_headers = true, bool is_dump_body = false);
  380. // method
  381. void SetMethod(const char* method) {
  382. this->method = http_method_enum(method);
  383. }
  384. const char* Method() {
  385. return http_method_str(method);
  386. }
  387. // scheme
  388. bool IsHttps() {
  389. return strncmp(scheme.c_str(), "https", 5) == 0 ||
  390. strncmp(url.c_str(), "https://", 8) == 0;
  391. }
  392. // url
  393. void SetUrl(const char* url) {
  394. this->url = url;
  395. }
  396. const std::string& Url() {
  397. return url;
  398. }
  399. // structed url -> url
  400. void DumpUrl();
  401. // url -> structed url
  402. void ParseUrl();
  403. // /path
  404. std::string Path() {
  405. const char* s = path.c_str();
  406. const char* e = s;
  407. while (*e && *e != '?' && *e != '#') ++e;
  408. return std::string(s, e);
  409. }
  410. // ?query_params
  411. template<typename T>
  412. void SetParam(const char* key, const T& t) {
  413. query_params[key] = hv::to_string(t);
  414. }
  415. std::string GetParam(const char* key, const std::string& defvalue = hv::empty_string) {
  416. auto iter = query_params.find(key);
  417. return iter == query_params.end() ? defvalue : iter->second;
  418. }
  419. // Host:
  420. std::string Host() {
  421. auto iter = headers.find("Host");
  422. return iter == headers.end() ? host : iter->second;
  423. }
  424. void FillHost(const char* host, int port = DEFAULT_HTTP_PORT);
  425. void SetHost(const char* host, int port = DEFAULT_HTTP_PORT);
  426. void SetProxy(const char* host, int port);
  427. // Range: bytes=0-4095
  428. void SetRange(long from = 0, long to = -1) {
  429. headers["Range"] = hv::asprintf("bytes=%ld-%ld", from, to);
  430. }
  431. bool GetRange(long& from, long& to) {
  432. auto iter = headers.find("Range");
  433. if (iter != headers.end()) {
  434. sscanf(iter->second.c_str(), "bytes=%ld-%ld", &from, &to);
  435. return true;
  436. }
  437. from = to = 0;
  438. return false;
  439. }
  440. // Cookie:
  441. void SetCookie(const HttpCookie& cookie) {
  442. headers["Cookie"] = cookie.dump();
  443. }
  444. bool GetCookie(HttpCookie& cookie) {
  445. std::string str = GetHeader("Cookie");
  446. if (str.empty()) return false;
  447. return cookie.parse(str);
  448. }
  449. };
  450. class HV_EXPORT HttpResponse : public HttpMessage {
  451. public:
  452. http_status status_code;
  453. const char* status_message() {
  454. return http_status_str(status_code);
  455. }
  456. HttpResponse() : HttpMessage() {
  457. type = HTTP_RESPONSE;
  458. Init();
  459. }
  460. void Init() {
  461. status_code = HTTP_STATUS_OK;
  462. }
  463. virtual void Reset() {
  464. HttpMessage::Reset();
  465. Init();
  466. }
  467. virtual std::string Dump(bool is_dump_headers = true, bool is_dump_body = false);
  468. // Content-Range: bytes 0-4095/10240000
  469. void SetRange(long from, long to, long total) {
  470. headers["Content-Range"] = hv::asprintf("bytes %ld-%ld/%ld", from, to, total);
  471. }
  472. bool GetRange(long& from, long& to, long& total) {
  473. auto iter = headers.find("Content-Range");
  474. if (iter != headers.end()) {
  475. sscanf(iter->second.c_str(), "bytes %ld-%ld/%ld", &from, &to, &total);
  476. return true;
  477. }
  478. from = to = total = 0;
  479. return false;
  480. }
  481. // Set-Cookie
  482. void SetCookie(const HttpCookie& cookie) {
  483. headers["Set-Cookie"] = cookie.dump();
  484. }
  485. bool GetCookie(HttpCookie& cookie) {
  486. std::string str = GetHeader("Set-Cookie");
  487. if (str.empty()) return false;
  488. return cookie.parse(str);
  489. }
  490. };
  491. typedef std::shared_ptr<HttpRequest> HttpRequestPtr;
  492. typedef std::shared_ptr<HttpResponse> HttpResponsePtr;
  493. typedef std::function<void(const HttpResponsePtr&)> HttpResponseCallback;
  494. #endif // HV_HTTP_MESSAGE_H_