A Discrete-Event Network Simulator
API
http-header.h
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 DOTFEESA www.tk.etf.unsa.ba
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Emir Dervisevic <emir.dervisevic@etf.unsa.ba>
19  * Miralem Mehic <miralem.mehic@ieee.org>
20  */
21 
22 #ifndef HTTP_HEADER_H
23 #define HTTP_HEADER_H
24 
25 #include <ns3/header.h>
26 #include <ns3/nstime.h>
27 #include <string>
28 #include <algorithm>
29 
30 namespace ns3 {
31 
32 class Packet;
33 
56 class HttpHeader : public Header
57 {
58 public:
59 
60 
63  {
64  NOT_SET = 0,
65  MAIN_OBJECT = 1,
66  EMBEDDED_OBJECT = 2
67  };
68 
69  enum HttpMethod {
70  DELETE = 0,
71  GET = 1,
72  HEAD = 2,
73  PATCH = 3,
74  POST = 4,
75  PUT = 5
76  };
77 
79  {
80  Continue = 100,
81  SwitchingProtocol = 101,
82  Processing = 102,
83  EarlyHints = 103,
84 
85  Ok = 200,
86  Created = 201,
87  Accepted = 202,
89  NoContent = 204,
90  ResetContent = 205,
91  PartialContent = 206,
92  MultiStatus = 207,
93  AlreadyReported = 208,
94  ImUsed = 226,
95 
96  MultipleChoice = 300,
97  MovedPermanently = 301,
98  Found = 302,
99  SeeOther = 303,
100  NotModified = 304,
101  UseProxy = 305,
102  TemporaryRedirect = 307,
103  PermanentRedirect = 308,
104 
105  BadRequest = 400,
106  Unauthorized = 401,
107  PaymentRequired = 402,
108  Forbidden = 403,
109  NotFound = 404,
110  MethodNotAllowed = 405,
111  NotAcceptable = 406,
113  RequestTimeout = 408,
114  Conflict = 409,
115  Gone = 410,
116  LengthRequired = 411,
117  PreconditionFailed = 412,
118  PayloadTooLarge = 413,
119  UriTooLong = 414,
120  UnsupportedMediaType = 415,
121  RangeNotSatisfiable = 416,
122  ExpectationFailed = 417,
123  ImaTeapot = 418,
124  MisdirectedRequest = 421,
125  UnprocessableEntity = 422,
126  Locked = 423,
127  FailedDependency = 424,
128  TooEarly = 425,
129  UpgradeRequired = 426,
130  PreconditionRequired = 428,
131  TooManyRequests = 429,
134 
135  InternalServerError = 500,
136  NotImplemented = 501,
137  BadGateway = 502,
138  ServiceUnavailable = 503,
139  GatewayTimeout = 504,
141  VariantAlsoNegotiates = 506,
142  InsufficientStorage = 507,
143  LoopDetected = 508,
144  NotExtended = 510,
146  };
147 
150 
151  void CreateRequest(
152  const std::string& url,
153  const std::string& method,
154  const std::map<std::string, std::string>& parameters,
155  const std::vector<std::string>& headers = {}
156  );
157 
158  void CreateRequest(
159  const std::string& url,
160  const std::string& method = "GET",
161  const std::string& body = "",
162  const std::vector<std::string>& headers = {}
163  );
164 
165  void CreateRequest(
166  const std::string& url,
167  const std::string& method,
168  const std::vector<uint8_t>& body,
169  const std::vector<std::string>& headers
170  );
171 
173  const HttpHeader::HttpStatus status,
174  const std::map<std::string, std::string>& body,
175  const std::vector<std::string>& headers = {}
176  );
177 
178  void CreateResponse(
179  const HttpHeader::HttpStatus status,
180  const std::string& body = "",
181  const std::vector<std::string>& headers = {}
182  );
183 
184  void CreateResponse(
185  const HttpHeader::HttpStatus status,
186  const std::vector<uint8_t>& body,
187  const std::vector<std::string>& headers
188  );
189 
194  static TypeId GetTypeId ();
195 
196  // Inherited from ObjectBase base class.
197  virtual TypeId GetInstanceTypeId () const;
198 
199  // Inherited from Header base class.
200  virtual uint32_t GetSerializedSize () const;
201  virtual void Serialize (Buffer::Iterator start) const;
202  virtual uint32_t Deserialize (Buffer::Iterator start);
203  virtual void Print (std::ostream &os) const;
204 
208  std::string ToString () const;
209 
210  std::vector<uint8_t> GetPayload(){
211  return m_payload;
212  }
213 
214  std::string GetPayloadString(){
215  if(m_headers.size() == 0 && m_payload.size() == 0) ParseResponse();
216  std::string payload = std::string{m_payload.begin(), m_payload.end()};
217  return payload;
218  }
219 
223  uint32_t GetSize () const;
224 
228  void SetStatus (HttpStatus status);
229 
234 
235  std::string GetStatusString () const;
236 
240  void SetMethod (HttpMethod method);
241 
242  void SetMethod (const std::string& m);
243 
248 
252  std::string GetUri() const;
256  void SetUri(const std::string& url);
257 
261  std::string GetRequestUri() const;
262 
263  std::string urlEncode(const std::string& str);
264 
265  void ParseResponse();
266 
267  std::string GetHeaderString();
268 
269  void SetHeaderString(std::string input);
270 
271  uint32_t GetContentSize();
272 
273  uint32_t GetHeaderSize();
274 
275  bool IsFragmented(){
276  return m_fragmented;
277  }
278 
279 private:
280 
281  std::string m_scheme;
282  std::string m_domain;
283  std::string m_path;
284 
285  std::vector<std::string> m_headers;
286  std::vector<uint8_t> m_payload;
287  std::vector<uint8_t> m_allData; //headers + emptyLine + m_payload
288 
289  uint16_t m_method;
290  uint16_t m_status;
291  uint16_t m_contentType;
292  uint32_t m_contentLength;
293  uint32_t m_headerSize;
294 
296 
297  std::string m_uri;
298  std::string m_request_uri;
299 
300 }; // end of `class HttpHeader`
301 
302 
303 } // end of `namespace ns3`
304 
305 
306 #endif /* HTTP_HEADER_H */
iterator in a Buffer instance
Definition: buffer.h:100
uint16_t m_method
" Connection method field in integer format.
Definition: http-header.h:232
std::string m_uri
Definition: http-header.h:237
std::vector< uint8_t > m_payload
Definition: http-header.h:286
uint32_t GetSize() const
std::vector< uint8_t > GetPayload()
Definition: http-header.h:210
HttpHeader()
Creates an empty instance .
std::vector< std::string > m_headers
Definition: http-header.h:285
uint16_t m_contentType
" Content type field in integer format.
Definition: http-header.h:234
std::vector< uint8_t > m_allData
Definition: http-header.h:287
virtual void Print(std::ostream &os) const
std::string m_domain
Definition: http-header.h:282
uint32_t GetHeaderSize()
Definition: http-header.cc:576
uint32_t m_headerSize
Definition: http-header.h:293
std::string GetHeaderString()
Definition: http-header.cc:410
std::string GetRequestUri() const
uint32_t GetContentSize()
Definition: http-header.cc:581
std::string GetUri() const
std::string m_path
Definition: http-header.h:283
bool IsFragmented()
Definition: http-header.h:275
void CreateResponse(const HttpHeader::HttpStatus status, const std::map< std::string, std::string > &body, const std::vector< std::string > &headers={})
void SetStatus(HttpStatus status)
virtual uint32_t Deserialize(Buffer::Iterator start)
virtual TypeId GetInstanceTypeId() const
Get the most derived TypeId for this Object.
uint16_t m_status
" Connection status field in integer format.
Definition: http-header.h:233
std::string GetPayloadString()
Definition: http-header.h:214
virtual void Serialize(Buffer::Iterator start) const
@ NetworkAuthenticationRequired
Definition: http-header.h:166
@ ProxyAuthenticationRequired
Definition: http-header.h:133
@ RequestHeaderFieldsTooLarge
Definition: http-header.h:153
@ NonAuthoritativeInformation
Definition: http-header.h:109
@ UnavailableForLegalReasons
Definition: http-header.h:154
std::string GetStatusString() const
Definition: http-header.cc:999
void SetHeaderString(std::string input)
Definition: http-header.cc:421
@ HEAD
Http Method Head.
Definition: http-header.h:93
@ DELETE
Http Method Delete.
Definition: http-header.h:91
@ PUT
Http Method Put.
Definition: http-header.h:96
@ GET
Http Method GET.
Definition: http-header.h:92
@ PATCH
Http Method Patch.
Definition: http-header.h:94
@ POST
Http Method Post.
Definition: http-header.h:95
uint32_t m_contentLength
" Content length field (in bytes unit).
Definition: http-header.h:235
HttpStatus GetStatus() const
void CreateRequest(const std::string &url, const std::string &method, const std::map< std::string, std::string > &parameters, const std::vector< std::string > &headers={})
Definition: http-header.cc:44
std::string m_scheme
Definition: http-header.h:281
static TypeId GetTypeId()
Returns the object TypeId.
std::string m_request_uri
Definition: http-header.h:298
void SetUri(std::string uri)
Definition: http-header.cc:687
ContentType_t
The possible types of content (default = NOT_SET).
Definition: http-header.h:84
@ NOT_SET
Integer equivalent = 0.
Definition: http-header.h:85
@ MAIN_OBJECT
Integer equivalent = 1.
Definition: http-header.h:86
@ EMBEDDED_OBJECT
Integer equivalent = 2.
Definition: http-header.h:87
std::string ToString() const
virtual uint32_t GetSerializedSize() const
void SetMethod(HttpMethod method)
void ParseResponse()
Definition: http-header.cc:204
HttpMethod GetMethod() const
std::string urlEncode(const std::string &str)
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.