httplib.h 19 KB

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