PcapPlusPlus  21.05
HttpLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_HTTP_LAYER
2 #define PACKETPP_HTTP_LAYER
3 
4 #include "TextBasedProtocol.h"
5 #include <string>
6 #include <exception>
7 
9 
14 namespace pcpp
15 {
16 
21  {
30  };
31 
32  // some popular HTTP fields
33 
35 #define PCPP_HTTP_HOST_FIELD "Host"
36 
37 #define PCPP_HTTP_CONNECTION_FIELD "Connection"
38 
39 #define PCPP_HTTP_USER_AGENT_FIELD "User-Agent"
40 
41 #define PCPP_HTTP_REFERER_FIELD "Referer"
42 
43 #define PCPP_HTTP_ACCEPT_FIELD "Accept"
44 
45 #define PCPP_HTTP_ACCEPT_ENCODING_FIELD "Accept-Encoding"
46 
47 #define PCPP_HTTP_ACCEPT_LANGUAGE_FIELD "Accept-Language"
48 
49 #define PCPP_HTTP_COOKIE_FIELD "Cookie"
50 
51 #define PCPP_HTTP_CONTENT_LENGTH_FIELD "Content-Length"
52 
53 #define PCPP_HTTP_CONTENT_ENCODING_FIELD "Content-Encoding"
54 
55 #define PCPP_HTTP_CONTENT_TYPE_FIELD "Content-Type"
56 
57 #define PCPP_HTTP_TRANSFER_ENCODING_FIELD "Transfer-Encoding"
58 
59 #define PCPP_HTTP_SERVER_FIELD "Server"
60 
61 
62 
63  // -------- Class HttpMessage -----------------
64 
65 
71  {
72  public:
73 
74  virtual ~HttpMessage() {}
75 
80  static bool isHttpPort(uint16_t port) { return port == 80 || port == 8080; }
81 
82  // overriden methods
83 
84  virtual HeaderField* addField(const std::string& fieldName, const std::string& fieldValue);
85  virtual HeaderField* addField(const HeaderField& newField);
86  virtual HeaderField* insertField(HeaderField* prevField, const std::string& fieldName, const std::string& fieldValue);
87  virtual HeaderField* insertField(HeaderField* prevField, const HeaderField& newField);
88 
90 
91  protected:
92  HttpMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : TextBasedProtocolMessage(data, dataLen, prevLayer, packet) {}
94  HttpMessage(const HttpMessage& other) : TextBasedProtocolMessage(other) {}
95  HttpMessage& operator=(const HttpMessage& other) { TextBasedProtocolMessage::operator=(other); return *this; }
96 
97  // implementation of abstract methods
98  char getHeaderFieldNameValueSeparator() const { return ':'; }
99  bool spacesAllowedBetweenHeaderFieldNameAndValue() const { return true; }
100  };
101 
102 
103 
104 
105  class HttpRequestFirstLine;
106 
107 
108 
109  // -------- Class HttpRequestLayer -----------------
110 
124  {
125  friend class HttpRequestFirstLine;
126  public:
131  {
151  HttpMethodUnknown
152  };
153 
160  HttpRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
161 
169  HttpRequestLayer(HttpMethod method, std::string uri, HttpVersion version);
170 
171  virtual ~HttpRequestLayer();
172 
178  HttpRequestLayer(const HttpRequestLayer& other);
179 
185  HttpRequestLayer& operator=(const HttpRequestLayer& other);
186 
190  HttpRequestFirstLine* getFirstLine() const { return m_FirstLine; }
191 
199  std::string getUrl() const;
200 
201  // implement Layer's abstract methods
202  std::string toString() const;
203 
204  private:
205  HttpRequestFirstLine* m_FirstLine;
206  };
207 
208 
209 
210 
211 
212  // -------- Class HttpResponseLayer -----------------
213 
214  class HttpResponseFirstLine;
215 
216 
230  {
231  friend class HttpResponseFirstLine;
232  public:
237  {
399  HttpStatusCodeUnknown
400  };
401 
402 
409  HttpResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
410 
420  HttpResponseLayer(HttpVersion version, HttpResponseLayer::HttpResponseStatusCode statusCode, std::string statusCodeString = "");
421 
422  virtual ~HttpResponseLayer();
423 
429  HttpResponseLayer(const HttpResponseLayer& other);
430 
436  HttpResponseLayer& operator=(const HttpResponseLayer& other);
437 
441  HttpResponseFirstLine* getFirstLine() const { return m_FirstLine; }
442 
455  HeaderField* setContentLength(int contentLength, const std::string prevFieldName = "");
456 
462  int getContentLength() const;
463 
464  // implement Layer's abstract methods
465 
466  std::string toString() const;
467 
468  private:
469  HttpResponseFirstLine* m_FirstLine;
470 
471  };
472 
473 
474 
475 
476 
477  // -------- Class HttpRequestFirstLine -----------------
478 
491  {
492  friend class HttpRequestLayer;
493  public:
497  HttpRequestLayer::HttpMethod getMethod() const { return m_Method; }
498 
504  bool setMethod(HttpRequestLayer::HttpMethod newMethod);
505 
509  std::string getUri() const;
510 
516  bool setUri(std::string newUri);
517 
521  HttpVersion getVersion() const { return m_Version; }
522 
528  void setVersion(HttpVersion newVersion);
529 
536  static HttpRequestLayer::HttpMethod parseMethod(char* data, size_t dataLen);
537 
541  int getSize() const { return m_FirstLineEndOffset; }
542 
549  bool isComplete() const { return m_IsComplete; }
550 
557  class HttpRequestFirstLineException : public std::exception
558  {
559  public:
560  ~HttpRequestFirstLineException() throw() {}
561  void setMessage(std::string message) { m_Message = message; }
562  virtual const char* what() const throw()
563  {
564  return m_Message.c_str();
565  }
566  private:
567  std::string m_Message;
568  };
569  private:
571  HttpRequestFirstLine(HttpRequestLayer* httpRequest, HttpRequestLayer::HttpMethod method, HttpVersion version, std::string uri = "/");
572  //throw(HttpRequestFirstLineException); // Deprecated in C++17
573 
574  void parseVersion();
575 
576  HttpRequestLayer* m_HttpRequest;
578  HttpVersion m_Version;
579  int m_VersionOffset;
580  int m_UriOffset;
581  int m_FirstLineEndOffset;
582  bool m_IsComplete;
583  HttpRequestFirstLineException m_Exception;
584  };
585 
586 
587 
588 
589 
590  // -------- Class HttpResponseFirstLine -----------------
591 
603  {
604  friend class HttpResponseLayer;
605  public:
610 
614  int getStatusCodeAsInt() const;
615 
619  std::string getStatusCodeString() const;
620 
627  bool setStatusCode(HttpResponseLayer::HttpResponseStatusCode newStatusCode, std::string statusCodeString = "");
628 
632  HttpVersion getVersion() const { return m_Version; }
633 
639  void setVersion(HttpVersion newVersion);
640 
647  static HttpResponseLayer::HttpResponseStatusCode parseStatusCode(char* data, size_t dataLen);
648 
652  int getSize() const { return m_FirstLineEndOffset; }
653 
660  bool isComplete() const { return m_IsComplete; }
661 
668  class HttpResponseFirstLineException : public std::exception
669  {
670  public:
671  ~HttpResponseFirstLineException() throw() {}
672  void setMessage(std::string message) { m_Message = message; }
673  virtual const char* what() const throw()
674  {
675  return m_Message.c_str();
676  }
677  private:
678  std::string m_Message;
679  };
680 
681  private:
683  HttpResponseFirstLine(HttpResponseLayer* httpResponse, HttpVersion version, HttpResponseLayer::HttpResponseStatusCode statusCode, std::string statusCodeString = "");
684 
685  static HttpVersion parseVersion(char* data, size_t dataLen);
686  static HttpResponseLayer::HttpResponseStatusCode validateStatusCode(char* data, size_t dataLen, HttpResponseLayer::HttpResponseStatusCode potentialCode);
687 
688 
689  HttpResponseLayer* m_HttpResponse;
690  HttpVersion m_Version;
692  int m_FirstLineEndOffset;
693  bool m_IsComplete;
694  HttpResponseFirstLineException m_Exception;
695  };
696 
697 } // namespace pcpp
698 
699 #endif /* PACKETPP_HTTP_LAYER */
The main namespace for the PcapPlusPlus lib.
HttpVersion getVersion() const
Definition: HttpLayer.h:521
OsiModelLayer
Definition: ProtocolType.h:253
Definition: HttpLayer.h:123
Definition: HttpLayer.h:145
int getSize() const
Definition: HttpLayer.h:541
Definition: HttpLayer.h:323
Definition: HttpLayer.h:147
Definition: HttpLayer.h:149
Definition: ProtocolType.h:268
HttpResponseLayer::HttpResponseStatusCode getStatusCode() const
Definition: HttpLayer.h:609
Definition: Layer.h:70
Definition: HttpLayer.h:303
Definition: HttpLayer.h:25
virtual HeaderField * addField(const std::string &fieldName, const std::string &fieldValue)
Definition: HttpLayer.h:269
Definition: Packet.h:26
HttpResponseFirstLine * getFirstLine() const
Definition: HttpLayer.h:441
Definition: HttpLayer.h:137
bool isComplete() const
Definition: HttpLayer.h:549
OsiModelLayer getOsiModelLayer() const
Definition: HttpLayer.h:89
HttpRequestLayer::HttpMethod getMethod() const
Definition: HttpLayer.h:497
HttpVersion getVersion() const
Definition: HttpLayer.h:632
HttpRequestFirstLine * getFirstLine() const
Definition: HttpLayer.h:190
Definition: HttpLayer.h:229
Definition: TextBasedProtocol.h:113
Definition: HttpLayer.h:135
Definition: HttpLayer.h:263
Definition: HttpLayer.h:359
Definition: HttpLayer.h:29
HttpResponseStatusCode
Definition: HttpLayer.h:236
Definition: HttpLayer.h:245
HttpMethod
Definition: HttpLayer.h:130
Definition: HttpLayer.h:70
Definition: HttpLayer.h:353
Definition: HttpLayer.h:327
Definition: HttpLayer.h:602
Definition: HttpLayer.h:301
bool isComplete() const
Definition: HttpLayer.h:660
Definition: HttpLayer.h:271
virtual HeaderField * insertField(HeaderField *prevField, const std::string &fieldName, const std::string &fieldValue)
Definition: HttpLayer.h:133
Definition: HttpLayer.h:143
Definition: HttpLayer.h:27
Definition: HttpLayer.h:291
Definition: HttpLayer.h:347
Definition: HttpLayer.h:490
virtual std::string toString() const =0
static bool isHttpPort(uint16_t port)
Definition: HttpLayer.h:80
Definition: HttpLayer.h:247
Definition: HttpLayer.h:239
Definition: HttpLayer.h:141
Definition: HttpLayer.h:23
Definition: TextBasedProtocol.h:31
Definition: HttpLayer.h:139
Definition: HttpLayer.h:275
Definition: HttpLayer.h:249
HttpVersion
Definition: HttpLayer.h:20
int getSize() const
Definition: HttpLayer.h:652