HttpRequest.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. /// \file
  23. #pragma once
  24. #include "../Container/ArrayPtr.h"
  25. #include "../Core/Mutex.h"
  26. #include "../Container/RefCounted.h"
  27. #include "../Core/Thread.h"
  28. #include "../IO/Deserializer.h"
  29. namespace Urho3D
  30. {
  31. /// HTTP connection state.
  32. enum HttpRequestState
  33. {
  34. HTTP_INITIALIZING = 0,
  35. HTTP_ERROR,
  36. HTTP_OPEN,
  37. HTTP_CLOSED
  38. };
  39. /// An HTTP connection with response data stream.
  40. class URHO3D_API HttpRequest : public RefCounted, public Deserializer, public Thread
  41. {
  42. #ifdef __EMSCRIPTEN__
  43. friend void JSResponse(intptr_t requester, intptr_t data, int length);
  44. #endif
  45. public:
  46. /// Construct with parameters.
  47. HttpRequest(const String& url, const String& verb, const Vector<String>& headers, const String& postData);
  48. /// Destruct. Release the connection object.
  49. ~HttpRequest() override;
  50. /// Process the connection in the worker thread until closed.
  51. void ThreadFunction() override;
  52. /// Read response data from the HTTP connection and return number of bytes actually read. While the connection is open, will block while trying to read the specified size. To avoid blocking, only read up to as many bytes as GetAvailableSize() returns.
  53. unsigned Read(void* dest, unsigned size) override;
  54. /// Set position from the beginning of the stream. Not supported.
  55. unsigned Seek(unsigned position) override;
  56. /// Return whether all response data has been read.
  57. bool IsEof() const override;
  58. /// Return URL used in the request.
  59. /// @property{get_url}
  60. const String& GetURL() const { return url_; }
  61. /// Return verb used in the request. Default GET if empty verb specified on construction.
  62. /// @property
  63. const String& GetVerb() const { return verb_; }
  64. /// Return error. Only non-empty in the error state.
  65. /// @property
  66. String GetError() const;
  67. /// Return connection state.
  68. /// @property
  69. HttpRequestState GetState() const;
  70. /// Return amount of bytes in the read buffer.
  71. /// @property
  72. unsigned GetAvailableSize() const;
  73. /// Return whether connection is in the open state.
  74. /// @property
  75. bool IsOpen() const { return GetState() == HTTP_OPEN; }
  76. private:
  77. /// Check for available read data in buffer and whether end has been reached. Must only be called when the mutex is held by the main thread.
  78. Pair<unsigned, bool> CheckAvailableSizeAndEof() const;
  79. /// URL.
  80. String url_;
  81. /// Verb.
  82. String verb_;
  83. /// Error string. Empty if no error.
  84. String error_;
  85. /// Headers.
  86. Vector<String> headers_;
  87. /// POST data.
  88. String postData_;
  89. /// Connection state.
  90. HttpRequestState state_;
  91. /// Mutex for synchronizing the worker and the main thread.
  92. mutable Mutex mutex_;
  93. /// Read buffer for the worker thread.
  94. SharedArrayPtr<unsigned char> httpReadBuffer_;
  95. /// Read buffer for the main thread.
  96. SharedArrayPtr<unsigned char> readBuffer_;
  97. /// Read buffer read cursor.
  98. unsigned readPosition_;
  99. /// Read buffer write cursor.
  100. unsigned writePosition_;
  101. };
  102. }