HttpRequest.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Container/ArrayPtr.h"
  6. #include "../Core/Mutex.h"
  7. #include "../Container/RefCounted.h"
  8. #include "../Core/Thread.h"
  9. #include "../IO/Deserializer.h"
  10. namespace Urho3D
  11. {
  12. /// HTTP connection state.
  13. enum HttpRequestState
  14. {
  15. HTTP_INITIALIZING = 0,
  16. HTTP_ERROR,
  17. HTTP_OPEN,
  18. HTTP_CLOSED
  19. };
  20. /// An HTTP connection with response data stream.
  21. class URHO3D_API HttpRequest : public RefCounted, public Deserializer, public Thread
  22. {
  23. public:
  24. /// Construct with parameters.
  25. HttpRequest(const String& url, const String& verb, const Vector<String>& headers, const String& postData);
  26. /// Destruct. Release the connection object.
  27. ~HttpRequest() override;
  28. /// Process the connection in the worker thread until closed.
  29. void ThreadFunction() override;
  30. /// 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.
  31. i32 Read(void* dest, i32 size) override;
  32. /// Set position from the beginning of the stream. Not supported.
  33. i64 Seek(i64 position) override;
  34. /// Return whether all response data has been read.
  35. bool IsEof() const override;
  36. /// Return URL used in the request.
  37. /// @property{get_url}
  38. const String& GetURL() const { return url_; }
  39. /// Return verb used in the request. Default GET if empty verb specified on construction.
  40. /// @property
  41. const String& GetVerb() const { return verb_; }
  42. /// Return error. Only non-empty in the error state.
  43. /// @property
  44. String GetError() const;
  45. /// Return connection state.
  46. /// @property
  47. HttpRequestState GetState() const;
  48. /// Return amount of bytes in the read buffer.
  49. /// @property
  50. i32 GetAvailableSize() const;
  51. /// Return whether connection is in the open state.
  52. /// @property
  53. bool IsOpen() const { return GetState() == HTTP_OPEN; }
  54. private:
  55. /// 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.
  56. Pair<i32, bool> CheckAvailableSizeAndEof() const;
  57. /// URL.
  58. String url_;
  59. /// Verb.
  60. String verb_;
  61. /// Error string. Empty if no error.
  62. String error_;
  63. /// Headers.
  64. Vector<String> headers_;
  65. /// POST data.
  66. String postData_;
  67. /// Connection state.
  68. HttpRequestState state_;
  69. /// Mutex for synchronizing the worker and the main thread.
  70. mutable Mutex mutex_;
  71. /// Read buffer for the worker thread.
  72. SharedArrayPtr<u8> httpReadBuffer_;
  73. /// Read buffer for the main thread.
  74. SharedArrayPtr<u8> readBuffer_;
  75. /// Read buffer read cursor.
  76. i32 readPosition_;
  77. /// Read buffer write cursor.
  78. i32 writePosition_;
  79. };
  80. }