llcorehttpresponse.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * @file llcorehttpresponse.h
  3. * @brief Public-facing declarations for the HttpResponse class
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2012, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef _LLCORE_HTTP_RESPONSE_H_
  27. #define _LLCORE_HTTP_RESPONSE_H_
  28. #include <string>
  29. #include "llcorehttpcommon.h"
  30. #include "llcorehttpheaders.h"
  31. #include "llcorerefcounted.h"
  32. namespace LLCore
  33. {
  34. class BufferArray;
  35. // HttpResponse is instantiated by the library and handed to the caller during
  36. // callbacks to the handler. It supplies all the status, header and HTTP body
  37. // data the caller is interested in. Methods provide simple getters to return
  38. // individual pieces of the response.
  39. //
  40. // Typical usage will have the caller interrogate the object and return from
  41. // the handler callback and then simply returning. But instances are refcounted
  42. // and and callers can add a reference and hold onto the object after the
  43. // callback.
  44. //
  45. // Threading: Not intrinsically thread-safe.
  46. //
  47. // Allocation: Refcounted, heap only. Caller of the constructor is given a
  48. // refcount.
  49. class HttpResponse final : public LLCoreInt::RefCounted
  50. {
  51. protected:
  52. virtual ~HttpResponse(); // Use release()
  53. public:
  54. HttpResponse();
  55. HttpResponse(const HttpResponse&) = delete;
  56. void operator=(const HttpResponse&) = delete;
  57. // Returns the final status of the requested operation.
  58. LL_INLINE HttpStatus getStatus() const { return mStatus; }
  59. LL_INLINE void setStatus(const HttpStatus& s) { mStatus = s; }
  60. // Simple getter for the response body returned as a scatter/gather buffer.
  61. // If the operation doesn't produce data (such as the Null or StopThread
  62. // operations), this may be NULL.
  63. //
  64. // Caller can hold onto the response by incrementing the reference count of
  65. // the returned object.
  66. LL_INLINE BufferArray* getBody() const { return mBufferArray; }
  67. // Safely get the size of the body buffer. If the body buffer is missing
  68. // return 0 as the size.
  69. size_t getBodySize() const;
  70. // Set the response data in the instance. Will drop the reference count to
  71. // any existing data and increment the count of that passed in. It is legal
  72. // to set the data to NULL.
  73. void setBody(BufferArray* ba);
  74. // And a getter for the headers. And as with getResponse(), if headers are
  75. // not available because the operation doesn't produce any or delivery of
  76. // headers wasn't requested in the options, this will be NULL.
  77. //
  78. // Caller can hold onto the headers by incrementing the reference count of
  79. // the returned object.
  80. LL_INLINE HttpHeaders::ptr_t getHeaders() const { return mHeaders; }
  81. // Behaves like @see setResponse() but for header data.
  82. LL_INLINE void setHeaders(HttpHeaders::ptr_t& hdr) { mHeaders = hdr; }
  83. // If a 'Range:' header was used, these methods are involved in setting and
  84. // returning data about the actual response. If both @offset and @length
  85. // are returned as 0, we probably didn't get a Content-Range header in the
  86. // response. This occurs with various Capabilities-based services and the
  87. // caller is going to have to make assumptions on receipt of a 206 status.
  88. // The @full value may also be zero in cases of parsing problems or a wild-
  89. // carded length response.
  90. //
  91. // These values will not necessarily agree with the data in the body itself
  92. // (if present). The BufferArray object is authoritative for actual data
  93. // length.
  94. LL_INLINE void getRange(unsigned int* offset, unsigned int* length,
  95. unsigned int* full) const
  96. {
  97. *offset = mReplyOffset;
  98. *length = mReplyLength;
  99. *full = mReplyFullLength;
  100. }
  101. LL_INLINE void setRange(unsigned int offset, unsigned int length,
  102. unsigned int full_length)
  103. {
  104. mReplyOffset = offset;
  105. mReplyLength = length;
  106. mReplyFullLength = full_length;
  107. }
  108. LL_INLINE const std::string& getContentType() const { return mContentType; }
  109. LL_INLINE void setContentType(const std::string& t) { mContentType = t; }
  110. // Get and set retry attempt information on the request.
  111. LL_INLINE void getRetries(unsigned int* retries,
  112. unsigned int* retries_503) const
  113. {
  114. if (retries)
  115. {
  116. *retries = mRetries;
  117. }
  118. if (retries_503)
  119. {
  120. *retries_503 = m503Retries;
  121. }
  122. }
  123. LL_INLINE void setRetries(unsigned int retries, unsigned int retries_503)
  124. {
  125. mRetries = retries;
  126. m503Retries = retries_503;
  127. }
  128. LL_INLINE void setRequestURL(const std::string& u) { mRequestUrl = u; }
  129. LL_INLINE const std::string& getRequestURL() const { return mRequestUrl; }
  130. // Statistics for the HTTP
  131. struct TransferStats
  132. {
  133. typedef std::shared_ptr<TransferStats> ptr_t;
  134. TransferStats()
  135. : mSizeDownload(0.0),
  136. mTotalTime(0.0),
  137. mSpeedDownload(0.0)
  138. {
  139. }
  140. F64 mSizeDownload;
  141. F64 mTotalTime;
  142. F64 mSpeedDownload;
  143. };
  144. LL_INLINE void setTransferStats(TransferStats::ptr_t& stats)
  145. {
  146. mStats = stats;
  147. }
  148. LL_INLINE TransferStats::ptr_t getTransferStats() { return mStats; }
  149. protected:
  150. HttpStatus mStatus;
  151. unsigned int mReplyOffset;
  152. unsigned int mReplyLength;
  153. unsigned int mReplyFullLength;
  154. unsigned int mRetries;
  155. unsigned int m503Retries;
  156. BufferArray* mBufferArray;
  157. HttpHeaders::ptr_t mHeaders;
  158. std::string mContentType;
  159. std::string mRequestUrl;
  160. TransferStats::ptr_t mStats;
  161. };
  162. } // End namespace LLCore
  163. #endif // _LLCORE_HTTP_RESPONSE_H_