http_content.h 1.9 KB

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