WebRequest.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include "../IO/BufferQueue.h"
  28. namespace asio
  29. {
  30. class io_service;
  31. }
  32. #ifndef EMSCRIPTEN
  33. extern "C"
  34. {
  35. typedef void CURLM;
  36. }
  37. #endif
  38. namespace Atomic
  39. {
  40. /// HTTP connection state
  41. enum WebRequestState
  42. {
  43. WR_INITIALIZING = 0,
  44. WR_ERROR,
  45. WR_OPEN,
  46. WR_CLOSED
  47. };
  48. struct WebRequestInternalState;
  49. /// An HTTP(S) connection with response data stream that uses curl internally
  50. class ATOMIC_API WebRequest : public Object
  51. {
  52. friend class Web;
  53. ATOMIC_OBJECT(WebRequest, Object)
  54. public:
  55. /// Construct with parameters.
  56. WebRequest(Context* context, const String& verb, const String& url, double requestContentSize = 0.0);
  57. /// Destruct. Release the connection.
  58. ~WebRequest();
  59. /// Return URL used in the request.
  60. const String& GetURL() const;
  61. /// Return error. Only non-empty in the error state.
  62. String GetError() const;
  63. /// Return connection state.
  64. WebRequestState GetState() const;
  65. /// Get the HTTP verb for this request.
  66. String GetVerb() const;
  67. /// Abort the WebRequest.
  68. void Abort();
  69. /// Return whether connection is in the open state.
  70. bool IsOpen() const { return GetState() == WR_OPEN; }
  71. /// Return whether E_WEBREQUESTDOWNLOADCHUNK event will be fired or if only E_WEBREQUESTCOMPLETE will be.
  72. bool HasDownloadChunkEvent();
  73. /// Set an HTTP request header (only works before Send has been called).
  74. void SetRequestHeader(const String& header, const String& value);
  75. /// Start sending the request.
  76. void Send();
  77. /// Get an HTTP response header.
  78. StringVector GetResponseHeaderKeys();
  79. /// Get an HTTP response header.
  80. String GetResponseHeader(const String& header);
  81. /// Get all HTTP response headers. Using GetResponseHeaderKeys() and GetResponseHeader() is more efficient than using this function.
  82. String GetAllResponseHeaders();
  83. /// Set post data for request
  84. void SetPostData(const String& postData);
  85. /// Get the download buffer queue
  86. SharedPtr<BufferQueue> GetDownloadBufferQueue();
  87. private:
  88. #ifndef EMSCRIPTEN
  89. void setup(asio::io_service *service, CURLM* curlm);
  90. static void internalNotify(WebRequest *wr, int code);
  91. #endif
  92. WebRequestInternalState* is_;
  93. };
  94. }