WebRequest.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // Copyright (c) 2008-2015 the Urho3D project.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "../Core/Object.h"
  25. #include "../Container/Str.h"
  26. #include "../Container/ArrayPtr.h"
  27. namespace asio
  28. {
  29. class io_service;
  30. }
  31. #ifndef EMSCRIPTEN
  32. extern "C"
  33. {
  34. typedef void CURLM;
  35. }
  36. #endif
  37. namespace Atomic
  38. {
  39. /// HTTP connection state
  40. enum WebRequestState
  41. {
  42. HTTP_INITIALIZING = 0,
  43. HTTP_ERROR,
  44. HTTP_OPEN,
  45. HTTP_CLOSED
  46. };
  47. struct WebRequestInternalState;
  48. /// An HTTP(S) connection with response data stream that uses curl internally
  49. class ATOMIC_API WebRequest : public Object
  50. {
  51. friend class Web;
  52. ATOMIC_OBJECT(WebRequest, Object)
  53. public:
  54. /// Construct with parameters.
  55. WebRequest(Context* context, const String& verb, const String& url, double requestContentSize = 0.0);
  56. /// Destruct. Release the connection.
  57. ~WebRequest();
  58. /// Return URL used in the request.
  59. const String& GetURL() const;
  60. /// Return error. Only non-empty in the error state.
  61. String GetError() const;
  62. /// Return connection state.
  63. WebRequestState GetState() const;
  64. /// Get the HTTP verb for this request.
  65. String GetVerb() const;
  66. /// Abort the WebRequest.
  67. void Abort();
  68. /// Return whether connection is in the open state.
  69. bool IsOpen() const { return GetState() == HTTP_OPEN; }
  70. /// Return whether E_WEBREQUESTDOWNLOADCHUNK event will be fired or if only E_WEBREQUESTCOMPLETE will be.
  71. bool HasDownloadChunkEvent();
  72. /// Set an HTTP request header (only works before Send has been called).
  73. void SetRequestHeader(const String& header, const String& value);
  74. /// Start sending the request.
  75. void Send();
  76. /// Get an HTTP response header.
  77. StringVector GetResponseHeaderKeys();
  78. /// Get an HTTP response header.
  79. String GetResponseHeader(const String& header);
  80. /// Get all HTTP response headers. Using GetResponseHeaderKeys() and GetResponseHeader() is more efficient than using this function.
  81. String GetAllResponseHeaders();
  82. /// Set post data for request
  83. void SetPostData(const String& postData);
  84. /// Get the request response as a string
  85. const String& GetResponse();
  86. private:
  87. #ifndef EMSCRIPTEN
  88. void setup(asio::io_service *service, CURLM* curlm);
  89. static void internalNotify(WebRequest *wr, int code);
  90. #endif
  91. WebRequestInternalState* is_;
  92. };
  93. }