httpObject.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //-----------------------------------------------------------------------------
  23. // Copyright (c) 2017 The Platinum Team
  24. //
  25. // Permission is hereby granted, free of charge, to any person obtaining a copy
  26. // of this software and associated documentation files (the "Software"), to
  27. // deal in the Software without restriction, including without limitation the
  28. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  29. // sell copies of the Software, and to permit persons to whom the Software is
  30. // furnished to do so, subject to the following conditions:
  31. //
  32. // The above copyright notice and this permission notice shall be included in
  33. // all copies or substantial portions of the Software.
  34. //
  35. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  36. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  37. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  38. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  39. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  40. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  41. // DEALINGS IN THE SOFTWARE.
  42. //-----------------------------------------------------------------------------
  43. #ifndef _HTTPOBJECT_H_
  44. #define _HTTPOBJECT_H_
  45. #ifndef _TVECTOR_H_
  46. #include "core/util/tVector.h"
  47. #endif
  48. #ifndef _TCPOBJECT_H_
  49. #include "app/net/tcpObject.h"
  50. #endif
  51. #include <curl/curl.h>
  52. #include <string>
  53. #include <unordered_map>
  54. class HTTPObject : public SimObject
  55. {
  56. private:
  57. typedef SimObject Parent;
  58. protected:
  59. CURL *mCurl;
  60. U8 *mBuffer;
  61. U32 mBufferSize;
  62. U32 mBufferUsed;
  63. std::string mUrl;
  64. std::string mValues;
  65. bool mDownload;
  66. std::string mDownloadPath;
  67. curl_slist *mHeaders;
  68. std::unordered_map<std::string, std::string> mRecieveHeaders;
  69. static CURLM *gCurlMulti;
  70. static int gCurlMultiTotal;
  71. static std::unordered_map<CURL *, HTTPObject *> gCurlMap;
  72. static size_t writeCallback(char *buffer, size_t size, size_t nitems, HTTPObject *object);
  73. static size_t headerCallback(char *buffer, size_t size, size_t nitems, HTTPObject *object);
  74. bool ensureBuffer(U32 length);
  75. size_t processData(char *buffer, size_t size, size_t nitems);
  76. size_t processHeader(char *buffer, size_t size, size_t nitems);
  77. void start();
  78. void processLines();
  79. void finish(CURLcode errorCode);
  80. void onConnected();
  81. void onConnectFailed();
  82. void onLine(const std::string& line);
  83. void onDownload(const std::string& path);
  84. void onDownloadFailed(const std::string& path);
  85. void onDisconnect();
  86. public:
  87. HTTPObject();
  88. ~HTTPObject() override;
  89. void setOption(const std::string &option, const std::string &value);
  90. void setDownloadPath(const std::string &path);
  91. void addHeader(const std::string &name, const std::string &value);
  92. void get(const std::string &address, const std::string &uri, const std::string &query);
  93. void post(const std::string &address, const std::string &uri, const std::string &query, const std::string &data);
  94. static void init();
  95. static void process();
  96. static void shutdown();
  97. DECLARE_CONOBJECT(HTTPObject);
  98. };
  99. #endif // _H_HTTPOBJECT_