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,
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,
82  Processing = 102,
83  EarlyHints = 103,
84 
85  Ok = 200,
86  Created = 201,
87  Accepted = 202,
89  NoContent = 204,
90  ResetContent = 205,
92  MultiStatus = 207,
94  ImUsed = 226,
95 
98  Found = 302,
99  SeeOther = 303,
100  NotModified = 304,
101  UseProxy = 305,
104 
105  BadRequest = 400,
108  Forbidden = 403,
109  NotFound = 404,
114  Conflict = 409,
115  Gone = 410,
119  UriTooLong = 414,
123  ImaTeapot = 418,
126  Locked = 423,
128  TooEarly = 425,
134 
137  BadGateway = 502,
144  NotExtended = 510,
146  };
147 
149  HttpHeader ();
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 
233  HttpStatus GetStatus () const;
234 
235  std::string GetStatusString () const;
236 
240  void SetMethod (HttpMethod method);
241 
242  void SetMethod (const std::string& m);
243 
247  HttpMethod GetMethod () const;
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 */
Protocol header serialization and deserialization.
Definition: header.h:43
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
Header used by web browsing applications to transmit information about content type,...
Definition: http-header.h:57
uint16_t m_method
" Connection method field in integer format.
Definition: http-header.h:289
std::string m_uri
Definition: http-header.h:297
std::vector< uint8_t > m_payload
Definition: http-header.h:286
virtual void Serialize(Buffer::Iterator start) const
Definition: http-header.cc:501
uint32_t GetSize() const
Definition: http-header.cc:587
std::vector< uint8_t > GetPayload()
Definition: http-header.h:210
HttpHeader()
Creates an empty instance .
Definition: http-header.cc:33
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:291
virtual void Print(std::ostream &os) const
Definition: http-header.cc:557
HttpStatus GetStatus() const
Definition: http-header.cc:795
std::vector< uint8_t > m_allData
Definition: http-header.h:287
std::string m_domain
Definition: http-header.h:282
uint32_t GetHeaderSize()
Definition: http-header.cc:576
virtual TypeId GetInstanceTypeId() const
Get the most derived TypeId for this Object.
Definition: http-header.cc:489
virtual uint32_t GetSerializedSize() const
Definition: http-header.cc:495
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
static TypeId GetTypeId()
Returns the object TypeId.
Definition: http-header.cc:479
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)
Definition: http-header.cc:593
uint16_t m_status
" Connection status field in integer format.
Definition: http-header.h:290
std::string GetPayloadString()
Definition: http-header.h:214
@ NetworkAuthenticationRequired
Definition: http-header.h:145
@ RequestHeaderFieldsTooLarge
Definition: http-header.h:132
@ ProxyAuthenticationRequired
Definition: http-header.h:112
@ UnavailableForLegalReasons
Definition: http-header.h:133
@ NonAuthoritativeInformation
Definition: http-header.h:88
std::string GetStatusString() const
Definition: http-header.cc:999
HttpMethod GetMethod() const
void SetHeaderString(std::string input)
Definition: http-header.cc:421
@ POST
Http Method Post.
Definition: http-header.h:74
@ HEAD
Http Method Head.
Definition: http-header.h:72
@ PUT
Http Method Put.
Definition: http-header.h:75
@ PATCH
Http Method Patch.
Definition: http-header.h:73
@ GET
Http Method GET.
Definition: http-header.h:71
@ DELETE
Http Method Delete.
Definition: http-header.h:70
uint32_t m_contentLength
" Content length field (in bytes unit).
Definition: http-header.h:292
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
std::string m_request_uri
Definition: http-header.h:298
ContentType_t
The possible types of content (default = NOT_SET).
Definition: http-header.h:63
@ EMBEDDED_OBJECT
Integer equivalent = 2.
Definition: http-header.h:66
@ NOT_SET
Integer equivalent = 0.
Definition: http-header.h:64
@ MAIN_OBJECT
Integer equivalent = 1.
Definition: http-header.h:65
std::string ToString() const
Definition: http-header.cc:567
void SetUri(const std::string &url)
void SetMethod(HttpMethod method)
void ParseResponse()
Definition: http-header.cc:204
std::string urlEncode(const std::string &str)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
def start()
Definition: core.py:1853