HttpBody.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
10 #include <Bentley/BeFile.h>
11 #include <Bentley/RefCounted.h>
12 #include <Bentley/ByteStream.h>
13 #include <Bentley/bmap.h>
14 #include <BeHttp/Http.h>
15 
17 
19 
20 typedef RefCountedPtr<HttpBody> HttpBodyPtr;
21 
22 /*--------------------------------------------------------------------------------------+
23 * @bsiclass Bentley Systems
24 +---------------+---------------+---------------+---------------+---------------+------*/
25 struct HttpBody : RefCountedBase
26 {
27 public:
28  // Open for Read/Write
29  virtual void Open() = 0;
30 
31  // Close and release resources
32  virtual void Close() = 0;
33 
34  // Set Read/Write position
35  virtual BentleyStatus SetPosition(uint64_t position) = 0;
36 
37  // Get current position
38  virtual BentleyStatus GetPosition(uint64_t& position) = 0;
39 
40  // Reset position and clear body contents
41  virtual BentleyStatus Reset() = 0;
42 
43  // Write contents of buffer, return bytes successfully written
44  virtual size_t Write(const char* buffer, size_t bufferSize) = 0;
45 
46  // Read maximum bytes of bufferSize to bufferOut
47  virtual size_t Read(char* bufferOut, size_t bufferSize) = 0;
48 
49  // Get length of contents
50  virtual uint64_t GetLength() = 0;
51 
52  virtual Utf8String AsString() const = 0;
53 };
54 
55 /*--------------------------------------------------------------------------------------+
56 * @bsiclass Bentley Systems
57 +---------------+---------------+---------------+---------------+---------------+------*/
58 typedef RefCountedPtr<struct HttpFileBody> HttpFileBodyPtr;
59 
61 {
62 private:
63  mutable BeFile m_file;
64  BeFileName m_filePath;
65 
66 private:
67  BentleyStatus OpenFile(BeFileAccess access = BeFileAccess::Read);
68  BentleyStatus CreateFile();
69  BentleyStatus PrepareForWrite();
70 
71  HttpFileBody(BeFileNameCR filePath) : m_filePath (filePath) {}
72  virtual ~HttpFileBody() {Close ();}
73 
74 public:
75  static HttpFileBodyPtr Create(BeFileNameCR filePath) {return new HttpFileBody(filePath);}
76  BeFileNameCR GetFilePath() const {return m_filePath;}
77 
78  BEHTTP_EXPORT void Open();
79  BEHTTP_EXPORT void Close();
80 
84  BEHTTP_EXPORT size_t Write(const char* buffer, size_t bufferSize);
85  BEHTTP_EXPORT size_t Read(char* bufferOut, size_t bufferSize);
87  BEHTTP_EXPORT Utf8String AsString() const;
88 };
89 
90 /*--------------------------------------------------------------------------------------+
91 * @bsiclass Bentley Systems
92 +---------------+---------------+---------------+---------------+---------------+------*/
93 typedef RefCountedPtr<struct HttpStringBody> HttpStringBodyPtr;
94 
96 {
97 protected:
99  std::shared_ptr<Utf8String> m_string;
100 
101  HttpStringBody(std::shared_ptr<Utf8String> content);
102  virtual ~HttpStringBody() {}
103 
104 public:
105  BEHTTP_EXPORT static HttpStringBodyPtr Create(Utf8StringCR content = "");
106  BEHTTP_EXPORT static HttpStringBodyPtr Create(std::shared_ptr<Utf8String> content);
107 
108  void Open() override {}
109  void Close() override {}
110 
111  BEHTTP_EXPORT BentleyStatus SetPosition(uint64_t position) override;
112  BentleyStatus GetPosition(uint64_t& position) override {position = m_position; return SUCCESS;}
113 
114  BentleyStatus Reset() override {m_string->clear(); m_position = 0; return SUCCESS;}
115  BEHTTP_EXPORT size_t Write(const char* buffer, size_t bufferSize) override;
116  BEHTTP_EXPORT size_t Read(char* bufferOut, size_t bufferSize) override;
117  uint64_t GetLength() override {return m_string->length();}
118 
119  Utf8String AsString() const override {return *m_string;}
120 };
121 
122 /*--------------------------------------------------------------------------------------+
123 * @bsiclass Bentley Systems
124 +---------------+---------------+---------------+---------------+---------------+------*/
125 typedef RefCountedPtr<struct HttpBinaryBody> HttpBinaryBodyPtr;
127  {
128  private:
129  uint64_t m_position = 0;
130  std::shared_ptr<bvector<char>> m_data;
131 
132  protected:
133  HttpBinaryBody(std::shared_ptr<bvector<char>> data);
134  virtual ~HttpBinaryBody() {}
135 
136  public:
137  BEHTTP_EXPORT static HttpBinaryBodyPtr Create(std::shared_ptr<bvector<char>> data);
138 
139  void Open() override {}
140  void Close() override {}
141 
142  BEHTTP_EXPORT BentleyStatus SetPosition(uint64_t position) override;
143  BentleyStatus GetPosition(uint64_t& position) override {position = m_position; return SUCCESS;}
144  BEHTTP_EXPORT BentleyStatus Reset() override;
145  BEHTTP_EXPORT size_t Write(const char* buffer, size_t bufferSize) override;
146  BEHTTP_EXPORT size_t Read(char* bufferOut, size_t bufferSize) override;
147  BEHTTP_EXPORT uint64_t GetLength() override;
148 
149  Utf8String AsString() const override {BeAssert(false && "Not supported"); return "";}
150  };
151 
152 /*--------------------------------------------------------------------------------------+
153 * @bsiclass Bentley Systems
154 +---------------+---------------+---------------+---------------+---------------+------*/
155 typedef RefCountedPtr<struct HttpByteStreamBody> HttpByteStreamBodyPtr;
157 {
158 protected:
160  ByteStream m_stream;
161 
162 public:
165 
166  void Open() override {}
167  void Close() override {}
168 
169  BEHTTP_EXPORT BentleyStatus SetPosition(uint64_t position) override;
170  BentleyStatus GetPosition(uint64_t& position) override {position = m_position; return SUCCESS;}
171  BentleyStatus Reset() override {m_stream.Clear(); m_position = 0; return SUCCESS;}
172  size_t Write(const char* buffer, size_t bufferSize) override {m_stream.Append((uint8_t const*)buffer, (uint32_t)bufferSize); m_position += bufferSize; return bufferSize;}
173  BEHTTP_EXPORT size_t Read(char* bufferOut, size_t bufferSize) override;
174  uint64_t GetLength() override {return m_stream.GetSize();}
175  ByteStream const& GetByteStream() const {return m_stream;}
176  Utf8String AsString() const override {return "";}
177 };
178 
179 typedef bmap<Utf8String, Utf8String> HttpMultipartProperties;
182 typedef RefCountedPtr<struct HttpMultipartBody> HttpMultipartBodyPtr;
183 
184 /*--------------------------------------------------------------------------------------+
185  Multipart body that can only be used as request body.
186 * @bsiclass Bentley Systems
187 +---------------+---------------+---------------+---------------+---------------+------*/
189 {
190 protected:
191  struct HttpMPart
192  {
193  Utf8String header;
196  };
197 
200  Utf8String m_boundary;
201  Utf8String m_boundaryFinish;
203 
205  virtual ~HttpMultipartBody() {}
206 
207 public:
208  BEHTTP_EXPORT static Utf8String DefaultBoundary();
209 
210  static HttpMultipartBodyPtr Create() {return new HttpMultipartBody(DefaultBoundary());}
211  static HttpMultipartBodyPtr Create(Utf8StringCR customBoundary) {return new HttpMultipartBody(DefaultBoundary());}
212 
213  // Add multipart content part with specified contentType and properties
214  void AddPart(HttpBodyPtr bodyPart, Utf8StringCR contentType) {AddPart(bodyPart, contentType, HttpMultipartProperties());}
215  BEHTTP_EXPORT void AddPart(HttpBodyPtr bodyPart, Utf8StringCR contentType, HttpMultipartPropertiesCR properties);
216 
217  // Get correct content type value for this content
218  BEHTTP_EXPORT Utf8String GetContentType();
219 
220  BEHTTP_EXPORT BeFileNameCR GetFilePath() const;
221 
222  BEHTTP_EXPORT void Open() override;
223  BEHTTP_EXPORT void Close() override;
224 
225  BEHTTP_EXPORT BentleyStatus SetPosition(uint64_t position) override;
226  BentleyStatus GetPosition(uint64_t& position) override {position = m_position; return SUCCESS;}
227  BEHTTP_EXPORT size_t Read(char* bufferOut, size_t bufferSize) override;
228  BEHTTP_EXPORT uint64_t GetLength() override;
229 
230  // Read only - will only reset position
231  BentleyStatus Reset() override{return SetPosition(0);}
232 
233  // Read only - disabled
234  size_t Write(const char* buffer, size_t bufferSize) override {BeAssert(false && "Write is not supported"); return 0;}
235  BEHTTP_EXPORT Utf8String AsString() const override;
236 };
237 
238 typedef RefCountedPtr<struct HttpRangeBody> HttpRangeBodyPtr;
239 
240 /*--------------------------------------------------------------------------------------+
241 * @bsiclass Bentley Systems
242 +---------------+---------------+---------------+---------------+---------------+------*/
244 {
245 private:
246  size_t TrimBufferSize(size_t bufferSize);
247  Utf8String ReadAllAsString();
248 
249 protected:
253 
254  BEHTTP_EXPORT HttpRangeBody(HttpBodyPtr body, uint64_t bytesFrom, uint64_t bytesTo);
255  virtual ~HttpRangeBody() {}
256 
257 public:
258  static HttpRangeBodyPtr Create(HttpBodyPtr body, uint64_t bytesFrom, uint64_t bytesTo) {return new HttpRangeBody(body, bytesFrom, bytesTo);}
259 
260  void Open() {m_body->Open(); m_body->SetPosition(m_bytesFrom);}
261  void Close() {m_body->Close();}
262 
263  BentleyStatus SetPosition(uint64_t position) {return m_body->SetPosition(m_bytesFrom + position);}
266  BEHTTP_EXPORT size_t Write(const char* buffer, size_t bufferSize);
267  BEHTTP_EXPORT size_t Read(char* bufferOut, size_t bufferSize);
268  uint64_t GetLength() {return m_bytesTo - m_bytesFrom + 1;}
269 
270  BEHTTP_EXPORT Utf8String AsString() const;
271 };
272 
273 
virtual BentleyStatus Reset()=0
void Close() override
Definition: HttpBody.h:109
bmap< Utf8String, Utf8String > HttpMultipartProperties
Definition: HttpBody.h:179
Definition: HttpBody.h:243
Definition: HttpBody.h:126
HttpBodyPtr m_body
Definition: HttpBody.h:250
uint64_t m_bytesFrom
Definition: HttpBody.h:251
struct Bentley::BeFileName const & BeFileNameCR
Definition: Bentley.h:242
static HttpFileBodyPtr Create(BeFileNameCR filePath)
Definition: HttpBody.h:75
BentleyStatus GetPosition(uint64_t &position) override
Definition: HttpBody.h:170
#define BEGIN_BENTLEY_HTTP_NAMESPACE
Definition: Http.h:17
BentleyStatus Reset() override
Definition: HttpBody.h:171
uint64_t GetLength() override
Definition: HttpBody.h:117
BentleyStatus GetPosition(uint64_t &position) override
Definition: HttpBody.h:112
Utf8String header
Definition: HttpBody.h:193
virtual ~HttpMultipartBody()
Definition: HttpBody.h:205
virtual BentleyStatus GetPosition(uint64_t &position)=0
bvector< HttpMPart > m_parts
Definition: HttpBody.h:202
uint64_t length
Definition: HttpBody.h:195
virtual uint64_t GetLength()=0
Definition: HttpBody.h:191
BentleyStatus SetPosition(uint64_t position)
Definition: HttpBody.h:263
virtual Utf8String AsString() const =0
size_t Write(const char *buffer, size_t bufferSize) override
Definition: HttpBody.h:234
BeFileAccess
The possible file access values for the Open method.
Definition: BeFile.h:42
BeFileNameCR GetFilePath() const
Definition: HttpBody.h:76
BentleyStatus GetPosition(uint64_t &position) override
Definition: HttpBody.h:226
virtual ~HttpStringBody()
Definition: HttpBody.h:102
void Close() override
Definition: HttpBody.h:167
Definition: HttpBody.h:60
uint64_t GetLength()
Definition: HttpBody.h:268
uint64_t m_bytesTo
Definition: HttpBody.h:252
struct Bentley::Utf8String const & Utf8StringCR
Definition: Bentley.h:241
BentleyStatus Reset() override
Definition: HttpBody.h:231
Definition: HttpBody.h:95
uint64_t m_totalPartsLength
Definition: HttpBody.h:199
RefCountedPtr< struct HttpRangeBody > HttpRangeBodyPtr
Definition: HttpBody.h:238
RefCountedPtr< struct HttpFileBody > HttpFileBodyPtr
Definition: HttpBody.h:58
virtual void Open()=0
#define BEHTTP_EXPORT
Definition: Http.h:14
Utf8String AsString() const override
Definition: HttpBody.h:149
static HttpByteStreamBodyPtr Create()
Definition: HttpBody.h:164
void Open() override
Definition: HttpBody.h:166
static HttpMultipartBodyPtr Create(Utf8StringCR customBoundary)
Definition: HttpBody.h:211
virtual ~HttpBinaryBody()
Definition: HttpBody.h:134
Utf8String AsString() const override
Definition: HttpBody.h:119
void Open() override
Definition: HttpBody.h:139
RefCountedPtr< HttpBody > HttpBodyPtr
Definition: HttpBody.h:20
RefCountedPtr< struct HttpStringBody > HttpStringBodyPtr
Definition: HttpBody.h:93
unsigned long long uint64_t
Definition: Bentley.r.h:95
BentleyStatus
Definition: Bentley.h:208
static HttpRangeBodyPtr Create(HttpBodyPtr body, uint64_t bytesFrom, uint64_t bytesTo)
Definition: HttpBody.h:258
Definition: HttpBody.h:25
ByteStream m_stream
Definition: HttpBody.h:160
virtual size_t Write(const char *buffer, size_t bufferSize)=0
size_t Write(const char *buffer, size_t bufferSize) override
Definition: HttpBody.h:172
A Bentley supplied implementation std::vector.
Definition: stdcxx/bvector.h:77
Utf8String m_boundary
Definition: HttpBody.h:200
uint64_t GetLength() override
Definition: HttpBody.h:174
virtual BentleyStatus SetPosition(uint64_t position)=0
void Open() override
Definition: HttpBody.h:108
std::shared_ptr< Utf8String > m_string
Definition: HttpBody.h:99
void Close() override
Definition: HttpBody.h:140
BentleyStatus Reset() override
Definition: HttpBody.h:114
HttpBodyPtr content
Definition: HttpBody.h:194
virtual ~HttpRangeBody()
Definition: HttpBody.h:255
RefCountedPtr< struct HttpBinaryBody > HttpBinaryBodyPtr
Definition: HttpBody.h:125
uint64_t m_position
Definition: HttpBody.h:198
void Close()
Definition: HttpBody.h:261
BentleyStatus GetPosition(uint64_t &position) override
Definition: HttpBody.h:143
Utf8String m_boundaryFinish
Definition: HttpBody.h:201
RefCountedPtr< struct HttpByteStreamBody > HttpByteStreamBodyPtr
Definition: HttpBody.h:155
HttpByteStreamBody()
Definition: HttpBody.h:163
ByteStream const & GetByteStream() const
Definition: HttpBody.h:175
#define END_BENTLEY_HTTP_NAMESPACE
Definition: Http.h:18
Definition: Bentley.h:210
RefCountedPtr< struct HttpMultipartBody > HttpMultipartBodyPtr
Definition: HttpBody.h:182
void AddPart(HttpBodyPtr bodyPart, Utf8StringCR contentType)
Definition: HttpBody.h:214
#define BeAssert(_Expression)
BeAssert performs the same function as the standard assert, plus it prevents the most common cases of...
Definition: BeAssert.h:56
void Open()
Definition: HttpBody.h:260
#define DEFINE_POINTER_SUFFIX_TYPEDEFS(_structname_)
Definition: Bentley.h:136
unsigned int uint32_t
Definition: Bentley.r.h:93
Provides Bentley implemented file management functions (Bentley/BeFile.h).
#define EXPORT_VTABLE_ATTRIBUTE
Definition: Bentley.h:67
HttpMultipartProperties & HttpMultipartPropertiesR
Definition: HttpBody.h:180
Utf8String AsString() const override
Definition: HttpBody.h:176
Definition: HttpBody.h:188
HttpMultipartProperties const & HttpMultipartPropertiesCR
Definition: HttpBody.h:181
uint64_t m_position
Definition: HttpBody.h:98
virtual void Close()=0
static HttpMultipartBodyPtr Create()
Definition: HttpBody.h:210
uint64_t m_position
Definition: HttpBody.h:159
unsigned char uint8_t
Definition: Bentley.r.h:89
virtual size_t Read(char *bufferOut, size_t bufferSize)=0
Definition: HttpBody.h:156

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.