HttpRequest.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // Copyright (c) 2008-2017 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. #pragma once
  23. #include "../Container/ArrayPtr.h"
  24. #include "../Core/Mutex.h"
  25. #include "../Container/RefCounted.h"
  26. #include "../Core/Thread.h"
  27. #include "../IO/Deserializer.h"
  28. namespace Atomic
  29. {
  30. /// HTTP connection state
  31. enum HttpRequestState
  32. {
  33. HTTP_INITIALIZING = 0,
  34. HTTP_ERROR,
  35. HTTP_OPEN,
  36. HTTP_CLOSED
  37. };
  38. /// An HTTP connection with response data stream.
  39. class ATOMIC_API HttpRequest : public RefCounted, public Deserializer, public Thread
  40. {
  41. ATOMIC_REFCOUNTED(HttpRequest)
  42. public:
  43. /// Construct with parameters.
  44. HttpRequest(const String& url, const String& verb, const Vector<String>& headers, const String& postData);
  45. /// Destruct. Release the connection object.
  46. ~HttpRequest();
  47. /// Process the connection in the worker thread until closed.
  48. virtual void ThreadFunction();
  49. /// 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.
  50. virtual unsigned Read(void* dest, unsigned size);
  51. /// Set position from the beginning of the stream. Not supported.
  52. virtual unsigned Seek(unsigned position);
  53. /// Return whether all response data has been read.
  54. virtual bool IsEof() const;
  55. /// Return URL used in the request.
  56. const String& GetURL() const { return url_; }
  57. /// Return verb used in the request. Default GET if empty verb specified on construction.
  58. const String& GetVerb() const { return verb_; }
  59. /// Return error. Only non-empty in the error state.
  60. String GetError() const;
  61. /// Return connection state.
  62. HttpRequestState GetState() const;
  63. /// Return amount of bytes in the read buffer.
  64. unsigned GetAvailableSize() const;
  65. /// Return whether connection is in the open state.
  66. bool IsOpen() const { return GetState() == HTTP_OPEN; }
  67. private:
  68. /// 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.
  69. Pair<unsigned, bool> CheckAvailableSizeAndEof() const;
  70. /// URL.
  71. String url_;
  72. /// Verb.
  73. String verb_;
  74. /// Error string. Empty if no error.
  75. String error_;
  76. /// Headers.
  77. Vector<String> headers_;
  78. /// POST data.
  79. String postData_;
  80. /// Connection state.
  81. HttpRequestState state_;
  82. /// Mutex for synchronizing the worker and the main thread.
  83. mutable Mutex mutex_;
  84. /// Read buffer for the worker thread.
  85. SharedArrayPtr<unsigned char> httpReadBuffer_;
  86. /// Read buffer for the main thread.
  87. SharedArrayPtr<unsigned char> readBuffer_;
  88. /// Read buffer read cursor.
  89. unsigned readPosition_;
  90. /// Read buffer write cursor.
  91. unsigned writePosition_;
  92. };
  93. }