httplib.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. //
  2. // httplib.h
  3. //
  4. // Copyright (c) 2012 Yuji Hirose. All rights reserved.
  5. // The Boost Software License 1.0
  6. //
  7. #ifndef _CPPHTTPLIB_HTTPSLIB_H_
  8. #define _CPPHTTPLIB_HTTPSLIB_H_
  9. #ifdef _WIN32
  10. #define _CRT_SECURE_NO_WARNINGS
  11. #define _CRT_NONSTDC_NO_DEPRECATE
  12. #ifndef SO_SYNCHRONOUS_NONALERT
  13. #define SO_SYNCHRONOUS_NONALERT 0x20;
  14. #endif
  15. #ifndef SO_OPENTYPE
  16. #define SO_OPENTYPE 0x7008
  17. #endif
  18. #ifndef snprintf
  19. #define snprintf _snprintf_s
  20. #endif
  21. #include <fcntl.h>
  22. #include <io.h>
  23. #include <winsock2.h>
  24. typedef SOCKET socket_t;
  25. #else
  26. #include <pthread.h>
  27. #include <unistd.h>
  28. #include <netdb.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31. #include <sys/socket.h>
  32. typedef int socket_t;
  33. #endif
  34. #include <functional>
  35. #include <map>
  36. #include <regex>
  37. #include <string>
  38. #include <assert.h>
  39. namespace httplib
  40. {
  41. typedef std::map<std::string, std::string> Map;
  42. typedef std::multimap<std::string, std::string> MultiMap;
  43. typedef std::smatch Match;
  44. struct Request {
  45. std::string method;
  46. std::string url;
  47. MultiMap headers;
  48. std::string body;
  49. Map params;
  50. Match matches;
  51. bool has_header(const char* key) const;
  52. std::string get_header_value(const char* key) const;
  53. void set_header(const char* key, const char* val);
  54. bool has_param(const char* key) const;
  55. };
  56. struct Response {
  57. int status;
  58. MultiMap headers;
  59. std::string body;
  60. bool has_header(const char* key) const;
  61. std::string get_header_value(const char* key) const;
  62. void set_header(const char* key, const char* val);
  63. void set_redirect(const char* url);
  64. void set_content(const std::string& s, const char* content_type);
  65. Response() : status(-1) {}
  66. };
  67. class Server {
  68. public:
  69. typedef std::function<void (const Request&, Response&)> Handler;
  70. typedef std::function<void (const Request&, const Response&)> Logger;
  71. Server();
  72. void get(const char* pattern, Handler handler);
  73. void post(const char* pattern, Handler handler);
  74. void set_error_handler(Handler handler);
  75. void set_logger(Logger logger);
  76. bool listen(const char* host, int port);
  77. void stop();
  78. private:
  79. typedef std::vector<std::pair<std::regex, Handler>> Handlers;
  80. void process_request(socket_t sock);
  81. bool read_request_line(FILE* fp, Request& req);
  82. bool routing(Request& req, Response& res);
  83. bool dispatch_request(Request& req, Response& res, Handlers& handlers);
  84. socket_t svr_sock_;
  85. Handlers get_handlers_;
  86. Handlers post_handlers_;
  87. Handler error_handler_;
  88. Logger logger_;
  89. };
  90. class Client {
  91. public:
  92. Client(const char* host, int port);
  93. std::shared_ptr<Response> get(const char* url);
  94. std::shared_ptr<Response> head(const char* url);
  95. std::shared_ptr<Response> post(const char* url, const std::string& body, const char* content_type);
  96. std::shared_ptr<Response> post(const char* url, const Map& params);
  97. bool send(const Request& req, Response& res);
  98. private:
  99. bool read_response_line(FILE* fp, Response& res);
  100. const std::string host_;
  101. const int port_;
  102. };
  103. // Implementation
  104. namespace detail {
  105. template <class Fn>
  106. void split(const char* b, const char* e, char d, Fn fn)
  107. {
  108. int i = 0;
  109. int beg = 0;
  110. while (e ? (b + i != e) : (b[i] != '\0')) {
  111. if (b[i] == d) {
  112. fn(&b[beg], &b[i]);
  113. beg = i + 1;
  114. }
  115. i++;
  116. }
  117. if (i) {
  118. fn(&b[beg], &b[i]);
  119. }
  120. }
  121. inline void get_flie_pointers(int fd, FILE*& fp_read, FILE*& fp_write)
  122. {
  123. #ifdef _WIN32
  124. int osfhandle = _open_osfhandle(fd, _O_RDONLY);
  125. fp_read = _fdopen(osfhandle, "rb");
  126. fp_write = _fdopen(osfhandle, "wb");
  127. #else
  128. fp_read = fdopen(fd, "rb");
  129. fp_write = fdopen(fd, "wb");
  130. #endif
  131. }
  132. template <typename Fn>
  133. socket_t create_socket(const char* host, int port, Fn fn)
  134. {
  135. #ifdef _WIN32
  136. int opt = SO_SYNCHRONOUS_NONALERT;
  137. setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char*)&opt, sizeof(opt));
  138. #endif
  139. // Create a socket
  140. auto sock = socket(AF_INET, SOCK_STREAM, 0);
  141. if (sock == -1) {
  142. return -1;
  143. }
  144. // Make 'reuse address' option available
  145. int yes = 1;
  146. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes));
  147. // Get a host entry info
  148. struct hostent* hp;
  149. if (!(hp = gethostbyname(host))) {
  150. return -1;
  151. }
  152. // Bind the socket to the given address
  153. struct sockaddr_in addr;
  154. memset(&addr, 0, sizeof(addr));
  155. memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
  156. addr.sin_family = AF_INET;
  157. addr.sin_port = htons(port);
  158. return fn(sock, addr);
  159. }
  160. inline socket_t create_server_socket(const char* host, int port)
  161. {
  162. return create_socket(host, port, [](socket_t sock, struct sockaddr_in& addr) -> socket_t {
  163. if (::bind(sock, (struct sockaddr*)&addr, sizeof(addr))) {
  164. return -1;
  165. }
  166. if (listen(sock, 5)) { // Listen through 5 channels
  167. return -1;
  168. }
  169. return sock;
  170. });
  171. }
  172. inline int shutdown_socket(socket_t sock)
  173. {
  174. #ifdef _WIN32
  175. return shutdown(sock, SD_BOTH);
  176. #else
  177. return shutdown(sock, SHUT_RDWR);
  178. #endif
  179. }
  180. inline int close_socket(socket_t sock)
  181. {
  182. #ifdef _WIN32
  183. return closesocket(sock);
  184. #else
  185. return close(sock);
  186. #endif
  187. }
  188. inline socket_t create_client_socket(const char* host, int port)
  189. {
  190. return create_socket(host, port, [](socket_t sock, struct sockaddr_in& addr) -> socket_t {
  191. if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_in))) {
  192. return -1;
  193. }
  194. return sock;
  195. });
  196. }
  197. inline const char* status_message(int status)
  198. {
  199. switch (status) {
  200. case 200: return "OK";
  201. case 400: return "Bad Request";
  202. case 404: return "Not Found";
  203. default:
  204. case 500: return "Internal Server Error";
  205. }
  206. }
  207. inline const char* get_header_value_text(const MultiMap& map, const char* key, const char* def)
  208. {
  209. auto it = map.find(key);
  210. if (it != map.end()) {
  211. return it->second.c_str();
  212. }
  213. return def;
  214. }
  215. inline int get_header_value_int(const MultiMap& map, const char* key, int def)
  216. {
  217. auto it = map.find(key);
  218. if (it != map.end()) {
  219. return std::atoi(it->second.c_str());
  220. }
  221. return def;
  222. }
  223. inline bool read_headers(FILE* fp, MultiMap& headers)
  224. {
  225. static std::regex re("(.+?): (.+?)\r\n");
  226. const auto BUFSIZ_HEADER = 2048;
  227. char buf[BUFSIZ_HEADER];
  228. for (;;) {
  229. if (!fgets(buf, BUFSIZ_HEADER, fp)) {
  230. return false;
  231. }
  232. if (!strcmp(buf, "\r\n")) {
  233. break;
  234. }
  235. std::cmatch m;
  236. if (std::regex_match(buf, m, re)) {
  237. auto key = std::string(m[1]);
  238. auto val = std::string(m[2]);
  239. headers.insert(std::make_pair(key, val));
  240. }
  241. }
  242. return true;
  243. }
  244. template <typename T>
  245. bool read_content(T& x, FILE* fp)
  246. {
  247. auto len = get_header_value_int(x.headers, "Content-Length", 0);
  248. if (len) {
  249. x.body.assign(len, 0);
  250. if (!fgets(&x.body[0], x.body.size() + 1, fp)) {
  251. return false;
  252. }
  253. }
  254. return true;
  255. }
  256. template <typename T>
  257. inline void write_headers(FILE* fp, const T& x)
  258. {
  259. fprintf(fp, "Connection: close\r\n");
  260. for (auto it = x.headers.begin(); it != x.headers.end(); ++it) {
  261. if (it->first != "Content-Type" && it->first != "Content-Length") {
  262. fprintf(fp, "%s: %s\r\n", it->first.c_str(), it->second.c_str());
  263. }
  264. }
  265. if (!x.body.empty()) {
  266. auto content_type = get_header_value_text(x.headers, "Content-Type", "text/plain");
  267. fprintf(fp, "Content-Type: %s\r\n", content_type);
  268. fprintf(fp, "Content-Length: %ld\r\n", x.body.size());
  269. }
  270. fprintf(fp, "\r\n");
  271. }
  272. inline void write_response(FILE* fp, const Request& req, const Response& res)
  273. {
  274. fprintf(fp, "HTTP/1.0 %d %s\r\n", res.status, status_message(res.status));
  275. write_headers(fp, res);
  276. if (!res.body.empty() && req.method != "HEAD") {
  277. fprintf(fp, "%s", res.body.c_str());
  278. }
  279. }
  280. inline std::string encode_url(const std::string& s)
  281. {
  282. std::string result;
  283. for (auto i = 0; s[i]; i++) {
  284. switch (s[i]) {
  285. case ' ': result += "+"; break;
  286. case '\'': result += "%27"; break;
  287. case ',': result += "%2C"; break;
  288. case ':': result += "%3A"; break;
  289. case ';': result += "%3B"; break;
  290. default:
  291. if (s[i] < 0) {
  292. result += '%';
  293. char hex[4];
  294. size_t len = snprintf(hex, sizeof(hex), "%02X", (unsigned char)s[i]);
  295. assert(len == 2);
  296. result.append(hex, len);
  297. } else {
  298. result += s[i];
  299. }
  300. break;
  301. }
  302. }
  303. return result;
  304. }
  305. inline bool is_hex(char c, int& v)
  306. {
  307. if (0x20 <= c && isdigit(c)) {
  308. v = c - '0';
  309. return true;
  310. } else if ('A' <= c && c <= 'F') {
  311. v = c - 'A' + 10;
  312. return true;
  313. } else if ('a' <= c && c <= 'f') {
  314. v = c - 'a' + 10;
  315. return true;
  316. }
  317. return false;
  318. }
  319. inline int from_hex_to_i(const std::string& s, int i, int cnt, int& val)
  320. {
  321. val = 0;
  322. for (; s[i] && cnt; i++, cnt--) {
  323. int v = 0;
  324. if (is_hex(s[i], v)) {
  325. val = val * 16 + v;
  326. } else {
  327. break;
  328. }
  329. }
  330. return --i;
  331. }
  332. size_t to_utf8(int code, char* buff)
  333. {
  334. if (code < 0x0080) {
  335. buff[0] = (code & 0x7F);
  336. return 1;
  337. } else if (code < 0x0800) {
  338. buff[0] = (0xC0 | ((code >> 6) & 0x1F));
  339. buff[1] = (0x80 | (code & 0x3F));
  340. return 2;
  341. } else if (code < 0xD800) {
  342. buff[0] = (0xE0 | ((code >> 12) & 0xF));
  343. buff[1] = (0x80 | ((code >> 6) & 0x3F));
  344. buff[2] = (0x80 | (code & 0x3F));
  345. return 3;
  346. } else if (code < 0xE000) { // D800 - DFFF is invalid...
  347. return 0;
  348. } else if (code < 0x10000) {
  349. buff[0] = (0xE0 | ((code >> 12) & 0xF));
  350. buff[1] = (0x80 | ((code >> 6) & 0x3F));
  351. buff[2] = (0x80 | (code & 0x3F));
  352. return 3;
  353. } else if (code < 0x110000) {
  354. buff[0] = (0xF0 | ((code >> 18) & 0x7));
  355. buff[1] = (0x80 | ((code >> 12) & 0x3F));
  356. buff[2] = (0x80 | ((code >> 6) & 0x3F));
  357. buff[3] = (0x80 | (code & 0x3F));
  358. return 4;
  359. }
  360. // NOTREACHED
  361. return 0;
  362. }
  363. inline std::string decode_url(const std::string& s)
  364. {
  365. std::string result;
  366. for (int i = 0; s[i]; i++) {
  367. if (s[i] == '%') {
  368. i++;
  369. assert(s[i]);
  370. if (s[i] == '%') {
  371. result += s[i];
  372. } else if (s[i] == 'u') {
  373. // Unicode
  374. i++;
  375. assert(s[i]);
  376. int val = 0;
  377. i = from_hex_to_i(s, i, 4, val);
  378. char buff[4];
  379. size_t len = to_utf8(val, buff);
  380. if (len > 0) {
  381. result.append(buff, len);
  382. }
  383. } else {
  384. // HEX
  385. int val = 0;
  386. i = from_hex_to_i(s, i, 2, val);
  387. result += val;
  388. }
  389. } else if (s[i] == '+') {
  390. result += ' ';
  391. } else {
  392. result += s[i];
  393. }
  394. }
  395. return result;
  396. }
  397. inline void write_request(FILE* fp, const Request& req)
  398. {
  399. auto url = encode_url(req.url);
  400. fprintf(fp, "%s %s HTTP/1.0\r\n", req.method.c_str(), url.c_str());
  401. write_headers(fp, req);
  402. if (!req.body.empty()) {
  403. if (req.has_header("application/x-www-form-urlencoded")) {
  404. fprintf(fp, "%s", encode_url(req.body).c_str());
  405. } else {
  406. fprintf(fp, "%s", req.body.c_str());
  407. }
  408. }
  409. }
  410. inline void parse_query_text(const std::string& s, Map& params)
  411. {
  412. split(&s[0], &s[s.size()], '&', [&](const char* b, const char* e) {
  413. std::string key;
  414. std::string val;
  415. split(b, e, '=', [&](const char* b, const char* e) {
  416. if (key.empty()) {
  417. key.assign(b, e);
  418. } else {
  419. val.assign(b, e);
  420. }
  421. });
  422. params[key] = val;
  423. });
  424. }
  425. #ifdef _WIN32
  426. class WSInit {
  427. public:
  428. WSInit::WSInit() {
  429. WSADATA wsaData;
  430. WSAStartup(0x0002, &wsaData);
  431. }
  432. WSInit::~WSInit() {
  433. WSACleanup();
  434. }
  435. };
  436. static WSInit wsinit_;
  437. #endif
  438. } // namespace detail
  439. // Request implementation
  440. inline bool Request::has_header(const char* key) const
  441. {
  442. return headers.find(key) != headers.end();
  443. }
  444. inline std::string Request::get_header_value(const char* key) const
  445. {
  446. return detail::get_header_value_text(headers, key, "");
  447. }
  448. inline void Request::set_header(const char* key, const char* val)
  449. {
  450. headers.insert(std::make_pair(key, val));
  451. }
  452. inline bool Request::has_param(const char* key) const
  453. {
  454. return params.find(key) != params.end();
  455. }
  456. // Response implementation
  457. inline bool Response::has_header(const char* key) const
  458. {
  459. return headers.find(key) != headers.end();
  460. }
  461. inline std::string Response::get_header_value(const char* key) const
  462. {
  463. return detail::get_header_value_text(headers, key, "");
  464. }
  465. inline void Response::set_header(const char* key, const char* val)
  466. {
  467. headers.insert(std::make_pair(key, val));
  468. }
  469. inline void Response::set_redirect(const char* url)
  470. {
  471. set_header("Location", url);
  472. status = 302;
  473. }
  474. inline void Response::set_content(const std::string& s, const char* content_type)
  475. {
  476. body = s;
  477. set_header("Content-Type", content_type);
  478. }
  479. // HTTP server implementation
  480. inline Server::Server()
  481. : svr_sock_(-1)
  482. {
  483. }
  484. inline void Server::get(const char* pattern, Handler handler)
  485. {
  486. get_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
  487. }
  488. inline void Server::post(const char* pattern, Handler handler)
  489. {
  490. post_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
  491. }
  492. inline void Server::set_error_handler(Handler handler)
  493. {
  494. error_handler_ = handler;
  495. }
  496. inline void Server::set_logger(Logger logger)
  497. {
  498. logger_ = logger;
  499. }
  500. inline bool Server::listen(const char* host, int port)
  501. {
  502. svr_sock_ = detail::create_server_socket(host, port);
  503. if (svr_sock_ == -1) {
  504. return false;
  505. }
  506. auto ret = true;
  507. for (;;) {
  508. socket_t sock = accept(svr_sock_, NULL, NULL);
  509. if (sock == -1) {
  510. if (svr_sock_ != -1) {
  511. detail::close_socket(svr_sock_);
  512. ret = false;
  513. } else {
  514. ; // The server socket was closed by user.
  515. }
  516. break;
  517. }
  518. // TODO: should be async
  519. process_request(sock);
  520. detail::shutdown_socket(sock);
  521. detail::close_socket(sock);
  522. }
  523. return ret;
  524. }
  525. inline void Server::stop()
  526. {
  527. detail::shutdown_socket(svr_sock_);
  528. detail::close_socket(svr_sock_);
  529. svr_sock_ = -1;
  530. }
  531. inline bool Server::read_request_line(FILE* fp, Request& req)
  532. {
  533. const auto BUFSIZ_REQUESTLINE = 2048;
  534. char buf[BUFSIZ_REQUESTLINE];
  535. if (!fgets(buf, BUFSIZ_REQUESTLINE, fp)) {
  536. return false;
  537. }
  538. static std::regex re("(GET|HEAD|POST) ([^?]+)(?:\\?(.+?))? HTTP/1\\.[01]\r\n");
  539. std::cmatch m;
  540. if (std::regex_match(buf, m, re)) {
  541. req.method = std::string(m[1]);
  542. req.url = detail::decode_url(m[2]);
  543. // Parse query text
  544. auto len = std::distance(m[3].first, m[3].second);
  545. if (len > 0) {
  546. detail::parse_query_text(detail::decode_url(m[3]), req.params);
  547. }
  548. return true;
  549. }
  550. return false;
  551. }
  552. inline bool Server::routing(Request& req, Response& res)
  553. {
  554. if (req.method == "GET" || req.method == "HEAD") {
  555. return dispatch_request(req, res, get_handlers_);
  556. } else if (req.method == "POST") {
  557. return dispatch_request(req, res, post_handlers_);
  558. }
  559. return false;
  560. }
  561. inline bool Server::dispatch_request(Request& req, Response& res, Handlers& handlers)
  562. {
  563. for (auto it = handlers.begin(); it != handlers.end(); ++it) {
  564. const auto& pattern = it->first;
  565. const auto& handler = it->second;
  566. if (std::regex_match(req.url, req.matches, pattern)) {
  567. handler(req, res);
  568. return true;
  569. }
  570. }
  571. return false;
  572. }
  573. inline void Server::process_request(socket_t sock)
  574. {
  575. FILE* fp_read;
  576. FILE* fp_write;
  577. detail::get_flie_pointers(sock, fp_read, fp_write);
  578. Request req;
  579. Response res;
  580. if (!read_request_line(fp_read, req) ||
  581. !detail::read_headers(fp_read, req.headers)) {
  582. return;
  583. }
  584. if (req.method == "POST") {
  585. if (!detail::read_content(req, fp_read)) {
  586. return;
  587. }
  588. if (req.get_header_value("Content-Type") == "application/x-www-form-urlencoded") {
  589. detail::parse_query_text(detail::decode_url(req.body), req.params);
  590. }
  591. }
  592. if (routing(req, res)) {
  593. if (res.status == -1) {
  594. res.status = 200;
  595. }
  596. } else {
  597. res.status = 404;
  598. }
  599. assert(res.status != -1);
  600. if (400 <= res.status && error_handler_) {
  601. error_handler_(req, res);
  602. }
  603. detail::write_response(fp_write, req, res);
  604. fflush(fp_write);
  605. if (logger_) {
  606. logger_(req, res);
  607. }
  608. }
  609. // HTTP client implementation
  610. inline Client::Client(const char* host, int port)
  611. : host_(host)
  612. , port_(port)
  613. {
  614. }
  615. inline bool Client::read_response_line(FILE* fp, Response& res)
  616. {
  617. const auto BUFSIZ_RESPONSELINE = 2048;
  618. char buf[BUFSIZ_RESPONSELINE];
  619. if (!fgets(buf, BUFSIZ_RESPONSELINE, fp)) {
  620. return false;
  621. }
  622. static std::regex re("HTTP/1\\.[01] (\\d+?) .+\r\n");
  623. std::cmatch m;
  624. if (std::regex_match(buf, m, re)) {
  625. res.status = std::atoi(std::string(m[1]).c_str());
  626. }
  627. return true;
  628. }
  629. inline bool Client::send(const Request& req, Response& res)
  630. {
  631. auto sock = detail::create_client_socket(host_.c_str(), port_);
  632. if (sock == -1) {
  633. return false;
  634. }
  635. FILE* fp_read;
  636. FILE* fp_write;
  637. detail::get_flie_pointers(sock, fp_read, fp_write);
  638. // Send request
  639. detail::write_request(fp_write, req);
  640. fflush(fp_write);
  641. // Receive response
  642. if (!read_response_line(fp_read, res) ||
  643. !detail::read_headers(fp_read, res.headers)) {
  644. return false;
  645. }
  646. if (req.method != "HEAD") {
  647. if (!detail::read_content(res, fp_read)) {
  648. return false;
  649. }
  650. }
  651. detail::shutdown_socket(sock);
  652. detail::close_socket(sock);
  653. return true;
  654. }
  655. inline std::shared_ptr<Response> Client::get(const char* url)
  656. {
  657. Request req;
  658. req.method = "GET";
  659. req.url = url;
  660. auto res = std::make_shared<Response>();
  661. return send(req, *res) ? res : nullptr;
  662. }
  663. inline std::shared_ptr<Response> Client::head(const char* url)
  664. {
  665. Request req;
  666. req.method = "HEAD";
  667. req.url = url;
  668. auto res = std::make_shared<Response>();
  669. return send(req, *res) ? res : nullptr;
  670. }
  671. inline std::shared_ptr<Response> Client::post(
  672. const char* url, const std::string& body, const char* content_type)
  673. {
  674. Request req;
  675. req.method = "POST";
  676. req.url = url;
  677. req.set_header("Content-Type", content_type);
  678. req.body = body;
  679. auto res = std::make_shared<Response>();
  680. return send(req, *res) ? res : nullptr;
  681. }
  682. inline std::shared_ptr<Response> Client::post(
  683. const char* url, const Map& params)
  684. {
  685. std::string query;
  686. for (auto it = params.begin(); it != params.end(); ++it) {
  687. if (it != params.begin()) {
  688. query += "&";
  689. }
  690. query += it->first;
  691. query += "=";
  692. query += it->second;
  693. }
  694. return post(url, query, "application/x-www-form-urlencoded");
  695. }
  696. } // namespace httplib
  697. #endif
  698. // vim: et ts=4 sw=4 cin cino={1s ff=unix