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 
81  static bool isHttpPort(uint16_t port) { return port == 80 || port == 8080; }
82 
83  // overriden methods
84 
85  virtual HeaderField* addField(const std::string& fieldName, const std::string& fieldValue);
86  virtual HeaderField* addField(const HeaderField& newField);
87  virtual HeaderField* insertField(HeaderField* prevField, const std::string& fieldName, const std::string& fieldValue);
88  virtual HeaderField* insertField(HeaderField* prevField, const HeaderField& newField);
89 
91 
92  protected:
93  HttpMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : TextBasedProtocolMessage(data, dataLen, prevLayer, packet) {}
95  HttpMessage(const HttpMessage& other) : TextBasedProtocolMessage(other) {}
96  HttpMessage& operator=(const HttpMessage& other) { TextBasedProtocolMessage::operator=(other); return *this; }
97 
98  // implementation of abstract methods
99  char getHeaderFieldNameValueSeparator() const { return ':'; }
100  bool spacesAllowedBetweenHeaderFieldNameAndValue() const { return true; }
101  };
102 
103 
104 
105 
106  class HttpRequestFirstLine;
107 
108 
109 
110  // -------- Class HttpRequestLayer -----------------
111 
125  {
126  friend class HttpRequestFirstLine;
127  public:
132  {
152  HttpMethodUnknown
153  };
154 
161  HttpRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
162 
170  HttpRequestLayer(HttpMethod method, std::string uri, HttpVersion version);
171 
172  virtual ~HttpRequestLayer();
173 
179  HttpRequestLayer(const HttpRequestLayer& other);
180 
187  HttpRequestLayer& operator=(const HttpRequestLayer& other);
188 
192  HttpRequestFirstLine* getFirstLine() const { return m_FirstLine; }
193 
201  std::string getUrl() const;
202 
203  // implement Layer's abstract methods
204  std::string toString() const;
205 
206  private:
207  HttpRequestFirstLine* m_FirstLine;
208  };
209 
210 
211 
212 
213 
214  // -------- Class HttpResponseLayer -----------------
215 
216  class HttpResponseFirstLine;
217 
218 
232  {
233  friend class HttpResponseFirstLine;
234  public:
239  {
401  HttpStatusCodeUnknown
402  };
403 
404 
411  HttpResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
412 
422  HttpResponseLayer(HttpVersion version, HttpResponseLayer::HttpResponseStatusCode statusCode, std::string statusCodeString = "");
423 
424  virtual ~HttpResponseLayer();
425 
431  HttpResponseLayer(const HttpResponseLayer& other);
432 
439  HttpResponseLayer& operator=(const HttpResponseLayer& other);
440 
444  HttpResponseFirstLine* getFirstLine() const { return m_FirstLine; }
445 
458  HeaderField* setContentLength(int contentLength, const std::string prevFieldName = "");
459 
465  int getContentLength() const;
466 
467  // implement Layer's abstract methods
468 
469  std::string toString() const;
470 
471  private:
472  HttpResponseFirstLine* m_FirstLine;
473 
474  };
475 
476 
477 
478 
479 
480  // -------- Class HttpRequestFirstLine -----------------
481 
494  {
495  friend class HttpRequestLayer;
496  public:
500  HttpRequestLayer::HttpMethod getMethod() const { return m_Method; }
501 
507  bool setMethod(HttpRequestLayer::HttpMethod newMethod);
508 
512  std::string getUri() const;
513 
519  bool setUri(std::string newUri);
520 
524  HttpVersion getVersion() const { return m_Version; }
525 
531  void setVersion(HttpVersion newVersion);
532 
539  static HttpRequestLayer::HttpMethod parseMethod(char* data, size_t dataLen);
540 
544  int getSize() const { return m_FirstLineEndOffset; }
545 
552  bool isComplete() const { return m_IsComplete; }
553 
560  class HttpRequestFirstLineException : public std::exception
561  {
562  public:
563  ~HttpRequestFirstLineException() throw() {}
564  void setMessage(std::string message) { m_Message = message; }
565  virtual const char* what() const throw()
566  {
567  return m_Message.c_str();
568  }
569  private:
570  std::string m_Message;
571  };
572  private:
574  HttpRequestFirstLine(HttpRequestLayer* httpRequest, HttpRequestLayer::HttpMethod method, HttpVersion version, std::string uri = "/");
575  //throw(HttpRequestFirstLineException); // Deprecated in C++17
576 
577  void parseVersion();
578 
579  HttpRequestLayer* m_HttpRequest;
581  HttpVersion m_Version;
582  int m_VersionOffset;
583  int m_UriOffset;
584  int m_FirstLineEndOffset;
585  bool m_IsComplete;
586  HttpRequestFirstLineException m_Exception;
587  };
588 
589 
590 
591 
592 
593  // -------- Class HttpResponseFirstLine -----------------
594 
606  {
607  friend class HttpResponseLayer;
608  public:
613 
617  int getStatusCodeAsInt() const;
618 
622  std::string getStatusCodeString() const;
623 
631  bool setStatusCode(HttpResponseLayer::HttpResponseStatusCode newStatusCode, std::string statusCodeString = "");
632 
636  HttpVersion getVersion() const { return m_Version; }
637 
643  void setVersion(HttpVersion newVersion);
644 
651  static HttpResponseLayer::HttpResponseStatusCode parseStatusCode(char* data, size_t dataLen);
652 
656  int getSize() const { return m_FirstLineEndOffset; }
657 
664  bool isComplete() const { return m_IsComplete; }
665 
672  class HttpResponseFirstLineException : public std::exception
673  {
674  public:
675  ~HttpResponseFirstLineException() throw() {}
676  void setMessage(std::string message) { m_Message = message; }
677  virtual const char* what() const throw()
678  {
679  return m_Message.c_str();
680  }
681  private:
682  std::string m_Message;
683  };
684 
685  private:
687  HttpResponseFirstLine(HttpResponseLayer* httpResponse, HttpVersion version, HttpResponseLayer::HttpResponseStatusCode statusCode, std::string statusCodeString = "");
688 
689  static HttpVersion parseVersion(char* data, size_t dataLen);
690  static HttpResponseLayer::HttpResponseStatusCode validateStatusCode(char* data, size_t dataLen, HttpResponseLayer::HttpResponseStatusCode potentialCode);
691 
692 
693  HttpResponseLayer* m_HttpResponse;
694  HttpVersion m_Version;
696  int m_FirstLineEndOffset;
697  bool m_IsComplete;
698  HttpResponseFirstLineException m_Exception;
699  };
700 
701 } // namespace pcpp
702 
703 #endif /* PACKETPP_HTTP_LAYER */
The main namespace for the PcapPlusPlus lib.
HttpVersion getVersion() const
Definition: HttpLayer.h:524
OsiModelLayer
Definition: ProtocolType.h:258
Definition: HttpLayer.h:124
Definition: HttpLayer.h:146
int getSize() const
Definition: HttpLayer.h:544
Definition: HttpLayer.h:325
Definition: HttpLayer.h:148
Definition: HttpLayer.h:150
Definition: ProtocolType.h:273
HttpResponseLayer::HttpResponseStatusCode getStatusCode() const
Definition: HttpLayer.h:612
Definition: Layer.h:70
Definition: HttpLayer.h:305
Definition: HttpLayer.h:25
virtual HeaderField * addField(const std::string &fieldName, const std::string &fieldValue)
Definition: HttpLayer.h:271
Definition: Packet.h:26
HttpResponseFirstLine * getFirstLine() const
Definition: HttpLayer.h:444
Definition: HttpLayer.h:138
bool isComplete() const
Definition: HttpLayer.h:552
OsiModelLayer getOsiModelLayer() const
Definition: HttpLayer.h:90
HttpRequestLayer::HttpMethod getMethod() const
Definition: HttpLayer.h:500
HttpVersion getVersion() const
Definition: HttpLayer.h:636
HttpRequestFirstLine * getFirstLine() const
Definition: HttpLayer.h:192
Definition: HttpLayer.h:231
Definition: TextBasedProtocol.h:115
Definition: HttpLayer.h:136
Definition: HttpLayer.h:265
Definition: HttpLayer.h:361
Definition: HttpLayer.h:29
HttpResponseStatusCode
Definition: HttpLayer.h:238
Definition: HttpLayer.h:247
HttpMethod
Definition: HttpLayer.h:131
Definition: HttpLayer.h:70
Definition: HttpLayer.h:355
Definition: HttpLayer.h:329
Definition: HttpLayer.h:605
Definition: HttpLayer.h:303
bool isComplete() const
Definition: HttpLayer.h:664
Definition: HttpLayer.h:273
virtual HeaderField * insertField(HeaderField *prevField, const std::string &fieldName, const std::string &fieldValue)
Definition: HttpLayer.h:134
Definition: HttpLayer.h:144
Definition: HttpLayer.h:27
Definition: HttpLayer.h:293
Definition: HttpLayer.h:349
Definition: HttpLayer.h:493
virtual std::string toString() const =0
static bool isHttpPort(uint16_t port)
Definition: HttpLayer.h:81
Definition: HttpLayer.h:249
Definition: HttpLayer.h:241
Definition: HttpLayer.h:142
Definition: HttpLayer.h:23
Definition: TextBasedProtocol.h:31
Definition: HttpLayer.h:140
Definition: HttpLayer.h:277
Definition: HttpLayer.h:251
HttpVersion
Definition: HttpLayer.h:20
int getSize() const
Definition: HttpLayer.h:656