http_content.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef HV_HTTP_CONTENT_H_
  2. #define HV_HTTP_CONTENT_H_
  3. #include "hexport.h"
  4. #include "hstring.h"
  5. // NOTE: WITHOUT_HTTP_CONTENT
  6. // ndk-r10e no std::to_string and can't compile modern json.hpp
  7. #ifndef WITHOUT_HTTP_CONTENT
  8. #include "json.hpp" // https://github.com/nlohmann/json
  9. #endif
  10. BEGIN_NAMESPACE_HV
  11. // QueryParams
  12. using QueryParams = hv::KeyValue;
  13. HV_EXPORT std::string dump_query_params(const QueryParams& query_params);
  14. HV_EXPORT int parse_query_params(const char* query_string, QueryParams& query_params);
  15. #ifndef WITHOUT_HTTP_CONTENT
  16. /**************multipart/form-data*************************************
  17. --boundary
  18. Content-Disposition: form-data; name="user"
  19. content
  20. --boundary
  21. Content-Disposition: form-data; name="avatar"; filename="user.jpg"
  22. Content-Type: image/jpeg
  23. content
  24. --boundary--
  25. ***********************************************************************/
  26. // FormData
  27. struct FormData {
  28. std::string filename;
  29. std::string content;
  30. FormData(const char* content = NULL, const char* filename = NULL) {
  31. if (content) {
  32. this->content = content;
  33. }
  34. if (filename) {
  35. this->filename = filename;
  36. }
  37. }
  38. template<typename T>
  39. FormData(T num) {
  40. content = hv::to_string(num);
  41. }
  42. };
  43. // FormFile
  44. struct FormFile : public FormData {
  45. FormFile(const char* filename = NULL) {
  46. if (filename) {
  47. this->filename = filename;
  48. }
  49. }
  50. };
  51. // MultiPart
  52. // name => FormData
  53. typedef HV_MAP<std::string, FormData> MultiPart;
  54. #define DEFAULT_MULTIPART_BOUNDARY "----WebKitFormBoundary7MA4YWxkTrZu0gW"
  55. HV_EXPORT std::string dump_multipart(MultiPart& mp, const char* boundary = DEFAULT_MULTIPART_BOUNDARY);
  56. HV_EXPORT int parse_multipart(const std::string& str, MultiPart& mp, const char* boundary);
  57. // Json
  58. using Json = nlohmann::json;
  59. // using Json = nlohmann::ordered_json;
  60. HV_EXPORT std::string dump_json(const hv::Json& json, int indent = -1);
  61. HV_EXPORT int parse_json(const char* str, hv::Json& json, std::string& errmsg);
  62. #endif
  63. END_NAMESPACE_HV
  64. #endif // HV_HTTP_CONTENT_H_