HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_WebResponse.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: NET_WebResponse.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __NET_WEBRESPONSE_H__
13 #define __NET_WEBRESPONSE_H__
14 
15 #include "NET_API.h"
16 
17 #include "NET_HTTPDefines.h"
18 #include "NET_Time.h"
19 #include "NET_WebTypes.h"
20 
21 #include <UT/UT_ArrayStringMap.h>
22 #include <UT/UT_Optional.h>
23 #include <UT/UT_StringArray.h>
24 #include <UT/UT_StringHolder.h>
25 
26 class NET_NetworkCookie;
27 
28 // NOTE: Not a full list of http status codes
30 {
31  // Curl uses zero to indicate no status code received
33  // Info Codes
36  // Success Codes
37  NET_HTTPOk = 200,
44  // Redirect Codes
52  // Client Error Codes
63  NET_HTTPGone = 410,
78  // Server Error Codes
86 };
87 
88 /// Holds all common information between a client response and an object holding
89 /// the servers response to a client.
90 class NET_API NET_BaseWebResponse
91 {
92 public:
98  NET_HTTPStatusCode code,
99  const HeaderMap &headers,
100  const UT_StringHolder &data);
101  NET_BaseWebResponse(NET_HTTPStatusCode code, const HeaderMap &headers);
102 
103  explicit operator bool() const { return isSuccess(); }
104 
105  bool isSuccess() const { return myStatus == NET_HTTPOk; }
106 
107  bool isRequestNotFound() const { return myStatus == NET_HTTPNotFound; }
108  bool isBadRequest() const { return myStatus == NET_HTTPBadRequest; }
109  bool hasTimedout() const { return myStatus == NET_HTTPRequestTimeout; }
110 
111  UT_StringHolder headersAsString() const;
112 
113  // Does this response object have a specific option specified.
114  bool hasHeader(const UT_StringRef &header) const;
115 
116  UT_Optional<const UT_StringHolder &> getHeaderContentType();
117  UT_Optional<exint> getHeaderContentLength();
118 
119  bool hasContentType(const UT_StringRef &mime) const;
120 
121  NET_Time modTime() const;
122 
123  // Get the stock message based on the status code
124  static void stockMsgFromCode(NET_HTTPStatusCode code, UT_WorkBuffer &msg);
125 
126  bool hasJSONBody() const;
127  static bool isJSONBody(const HeaderMap &headers);
128 
129  // The status of the response
131  // The headers of the response
134  // The body of the response
136 };
137 
138 /// Response object used by a client from a response by a server.
139 class NET_API NET_HTTPResponse : public NET_BaseWebResponse
140 {
141 public:
142  struct NET_API Error
143  {
144  // This are all of the protocol independent error codes.
145  //
146  // The currently match curls error code number but do not make the
147  // assumption that will always be the case. Use a translate function if
148  // you need to go between the two.
149  enum Code
150  {
151  INTERNAL_ERROR = -1,
152  OK = 0,
238  OBSOLETE
239  };
240 
241  UT_StringHolder buildErrorMessage() const;
242  static Code translateErrorToCode(int err);
243  static UT_StringHolder codeToString(Code code);
244 
245  Code myErrorCode = Code::OK;
246  // Explenation of the error
248  // Extra details we provide about the error. Most of the time this error
249  // string will be empty. Its meant to give extra information to a
250  // standard error. (ie. cert error)
252  };
253 
258  NET_HTTPStatusCode code,
259  const HeaderMap &headers,
260  const UT_StringHolder &data);
261  NET_HTTPResponse(NET_HTTPStatusCode code, const HeaderMap &headers);
262 
263  void parseCookies(UT_Array<NET_NetworkCookie> &cookies);
264  bool errorReceivedNothing() const
265  {
266  return myError.myErrorCode == Error::GOT_NOTHING;
267  }
268  void clear();
269 
271 };
272 
273 /// Response object used for responding to request in the server.
274 class NET_API NET_WebResponse : public NET_BaseWebResponse
275 {
276 public:
278  : NET_BaseWebResponse(), myDeleteFile(false), myRanges(), myErrors()
279  {
280  }
282  : NET_BaseWebResponse(code), myDeleteFile(false), myRanges(), myErrors()
283  {
284  }
286  : NET_BaseWebResponse(code, data)
287  , myDeleteFile(false)
288  , myRanges()
289  , myErrors()
290  {
291  }
293  NET_HTTPStatusCode code,
294  const HeaderMap &headers,
295  const UT_StringHolder &data)
296  : NET_BaseWebResponse(code, headers, data)
297  , myDeleteFile(false)
298  , myRanges()
299  , myErrors()
300  {
301  }
303  : NET_BaseWebResponse(code, headers)
304  , myDeleteFile(false)
305  , myRanges()
306  , myErrors()
307  {
308  }
309 
311  : NET_BaseWebResponse(resp)
312  , myDeleteFile(resp.myDeleteFile)
313  , myFile(resp.myFile)
314  , myRanges(resp.myRanges)
315  , myErrors(resp.myErrors)
316  {
317  }
319  {
320  NET_BaseWebResponse::operator=(resp);
321  myDeleteFile = resp.myDeleteFile;
322  myFile = resp.myFile;
323  myRanges = resp.myRanges;
324  myErrors = resp.myErrors;
325  return *this;
326  }
328  : NET_BaseWebResponse(resp)
329  , myDeleteFile(resp.myDeleteFile)
330  , myFile(std::move(resp.myFile))
331  , myRanges(std::move(resp.myRanges))
332  , myErrors(std::move(resp.myErrors))
333  {
334  }
336  {
337  NET_BaseWebResponse::operator=(resp);
338  myDeleteFile = resp.myDeleteFile;
339  myFile = std::move(resp.myFile);
340  myRanges = std::move(resp.myRanges);
341  myErrors = std::move(resp.myErrors);
342  return *this;
343  }
344 
345  // Convience operators to forward a client response as a server response
347  : NET_BaseWebResponse(resp), myDeleteFile(false), myFile(), myErrors()
348  {
349  }
351  {
352  NET_BaseWebResponse::operator=(resp);
353  myFile.clear();
354  myDeleteFile = false;
355  myRanges.clear();
356  myErrors.clear();
357  return *this;
358  }
359 
360  void clear();
361 
362  bool isFile() const { return myFile.isstring(); }
363 
364  bool isChunked() const
365  {
366  auto it = myHeaders.find(HTTP_TRANSFER_ENCODING);
367  if (it == myHeaders.end())
368  return false;
369  return it->second.startsWith("chunked");
370  }
371 
375  // Error that you might want hidden
377 };
378 
379 #endif // __NET_WEBRESPONSE_H__
380 
Response object used for responding to request in the server.
UT_StringHolder myErrors
UT_StringHolder myData
NET_WebResponse(NET_HTTPStatusCode code, const UT_StringHolder &data)
bool isFile() const
UT_StringHolder myFile
NET_HTTPStatusCode
UT_StringArray myCookies
NET_RequestRangeList myRanges
hboost::optional< T > UT_Optional
Definition: UT_Optional.h:16
Response object used by a client from a response by a server.
#define HTTP_TRANSFER_ENCODING
bool isRequestNotFound() const
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
NET_WebResponse(const NET_WebResponse &resp)
UT_StringHolder myExtraDetails
UT_StringHolder myErrorMsg
NET_WebResponse(NET_WebResponse &&resp)
bool isSuccess() const
NET_WebResponse(NET_HTTPStatusCode code, const HeaderMap &headers, const UT_StringHolder &data)
NET_HTTPStatusCode myStatus
NET_WebResponse(NET_HTTPStatusCode code)
NET_WebResponse & operator=(const NET_HTTPResponse &resp)
NET_WebResponse(NET_HTTPStatusCode code, const HeaderMap &headers)
bool errorReceivedNothing() const
bool isChunked() const
NET_WebResponse & operator=(NET_WebResponse &&resp)
bool hasTimedout() const
bool isBadRequest() const
NET_WebResponse(const NET_HTTPResponse &resp)
NET_WebResponse & operator=(const NET_WebResponse &resp)