http_content.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef HV_HTTP_CONTENT_H_
  2. #define HV_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(const 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. // FormFile
  40. struct FormFile : public FormData {
  41. FormFile(const char* filename = NULL) {
  42. if (filename) {
  43. this->filename = filename;
  44. }
  45. }
  46. };
  47. // MultiPart
  48. // name => FormData
  49. typedef HV_MAP<std::string, FormData> MultiPart;
  50. #define DEFAULT_MULTIPART_BOUNDARY "----WebKitFormBoundary7MA4YWxkTrZu0gW"
  51. HV_EXPORT std::string dump_multipart(MultiPart& mp, const char* boundary = DEFAULT_MULTIPART_BOUNDARY);
  52. HV_EXPORT int parse_multipart(const std::string& str, MultiPart& mp, const char* boundary);
  53. // Json
  54. // https://github.com/nlohmann/json
  55. #include "json.hpp"
  56. namespace hv { // NOTE: Avoid conflict with jsoncpp
  57. using Json = nlohmann::json;
  58. // using Json = nlohmann::ordered_json;
  59. }
  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. #endif // HV_HTTP_CONTENT_H_