libUTL++
HttpRequest.h
1 #pragma once
2 
4 
5 #include <libutl/Array.h>
6 #include <libutl/BinaryData.h>
7 #include <libutl/Stream.h>
8 #include <libutl/StringVars.h>
9 
11 
12 UTL_NS_BEGIN;
13 
15 
16 class HttpResponse;
17 
19 
31 
33 class HttpRequest : public Object
34 {
36 
37 public:
39  HttpRequest(const char* request)
40  : _request(request)
41  {
42  init();
43  }
44 
45  virtual int compare(const Object& rhs) const;
46 
47  virtual void copy(const Object& rhs);
48 
50  void
51  header(const char* name, const char* value)
52  {
53  header(name, String(value, false));
54  }
55 
57  void header(const char* name, const String& value);
58 
60  void body(BinaryData* body, const char* type);
61 
63  virtual HttpResponse* execute(Stream& stream) const;
64 
65 private:
66  void
67  init()
68  {
69  _headersHT.setOwner(false);
70  _body = nullptr;
71  }
72  void
73  deInit()
74  {
75  delete _body;
76  }
77 
78 private:
79  String _request;
80  Array _headersArray;
81  Hashtable _headersHT;
82  BinaryData* _body;
83 };
84 
86 
96 
98 class HttpResponse : public Object
99 {
100  friend class HttpRequest;
102 
103 public:
105  HttpResponse(uint16_t code, const char* text)
106  : _statusCode(code)
107  , _statusText(text)
108  {
109  _body = nullptr;
110  }
111 
112  virtual int compare(const Object& rhs) const;
113 
114  virtual void copy(const Object& rhs);
115 
117  const uint16_t
119  {
120  return _statusCode;
121  }
122 
124  const String&
126  {
127  return _statusText;
128  }
129 
131  const String&
132  header(const char* name) const
133  {
134  return header(String(name, false));
135  }
136 
138  const String& header(const String& name) const;
139 
141  const utl::Collection&
142  headers() const
143  {
144  return _headers;
145  }
146 
148  void printHeaders(Stream& os) const;
149 
151  const BinaryData*
152  body() const
153  {
154  return _body;
155  }
156 
157 private:
158  void
159  init()
160  {
161  _statusCode = 0;
162  _body = nullptr;
163  }
164  void
165  deInit()
166  {
167  delete _body;
168  }
169  void header(String* name, String* value);
170  void
171  body(BinaryData* body)
172  {
173  delete _body;
174  _body = body;
175  }
176 
177 private:
178  uint16_t _statusCode;
179  String _statusText;
180  Hashtable _headers;
181  BinaryData* _body;
182 };
183 
185 
186 UTL_NS_END;
Vector of bytes.
Definition: BinaryData.h:22
void deInit()
De-initialize UTL++.
HttpRequest(const char *request)
Constructor.
Definition: HttpRequest.h:39
HttpResponse(uint16_t code, const char *text)
Constructor.
Definition: HttpRequest.h:105
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
const utl::Collection & headers() const
Get the collection of headers.
Definition: HttpRequest.h:142
Character string.
Definition: String.h:31
HTTP request.
Definition: HttpRequest.h:33
SortedCollection that stores objects in an array.
Definition: Array.h:116
void header(const char *name, const char *value)
Add a unique header.
Definition: HttpRequest.h:51
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
const String & statusText()
Get the status text.
Definition: HttpRequest.h:125
const BinaryData * body() const
Get the response body.
Definition: HttpRequest.h:152
const String & header(const char *name) const
Get the value of the given header.
Definition: HttpRequest.h:132
Stream I/O abstraction.
Definition: Stream.h:68
unsigned short uint16_t
Unsigned 16-bit integer.
Definition: types.h:101
Chained hashing collection.
Definition: Hashtable.h:92
HTTP response.
Definition: HttpRequest.h:98
Root of UTL++ class hierarchy.
Definition: Object.h:52
int compare(bool lhs, bool rhs)
Compare two boolean values.
void init()
Initialize UTL++.
const uint16_t statusCode()
Get the status code.
Definition: HttpRequest.h:118
A collection of objects.
Definition: Collection.h:62