PcapPlusPlus
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 #include <map>
8 
10 
15 namespace pcpp
16 {
17 
22  {
31  };
32 
33  // some popular HTTP fields
34 
36 #define PCPP_HTTP_HOST_FIELD "Host"
37 
38 #define PCPP_HTTP_CONNECTION_FIELD "Connection"
39 
40 #define PCPP_HTTP_USER_AGENT_FIELD "User-Agent"
41 
42 #define PCPP_HTTP_REFERER_FIELD "Referer"
43 
44 #define PCPP_HTTP_ACCEPT_FIELD "Accept"
45 
46 #define PCPP_HTTP_ACCEPT_ENCODING_FIELD "Accept-Encoding"
47 
48 #define PCPP_HTTP_ACCEPT_LANGUAGE_FIELD "Accept-Language"
49 
50 #define PCPP_HTTP_COOKIE_FIELD "Cookie"
51 
52 #define PCPP_HTTP_CONTENT_LENGTH_FIELD "Content-Length"
53 
54 #define PCPP_HTTP_CONTENT_ENCODING_FIELD "Content-Encoding"
55 
56 #define PCPP_HTTP_CONTENT_TYPE_FIELD "Content-Type"
57 
58 #define PCPP_HTTP_TRANSFER_ENCODING_FIELD "Transfer-Encoding"
59 
60 #define PCPP_HTTP_SERVER_FIELD "Server"
61 
62 
63 
64  // -------- Class HttpMessage -----------------
65 
66 
72  {
73  public:
74 
75  virtual ~HttpMessage() {}
76 
80  static const std::map<uint16_t, bool>* getHTTPPortMap();
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) {}
93  HttpMessage() : TextBasedProtocolMessage() {}
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() { return ':'; }
99  bool spacesAllowedBetweenHeaderFieldNameAndValue() { return true; }
100  };
101 
102 
103 
104 
105  class HttpRequestFirstLine;
106 
107 
108 
109 
110  // -------- Class HttpRequestLayer -----------------
111 
125  {
126  friend class HttpRequestFirstLine;
127  public:
132  {
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 
191  inline HttpRequestFirstLine* getFirstLine() { return m_FirstLine; }
192 
200  std::string getUrl();
201 
202  // implement Layer's abstract methods
203  std::string toString();
204 
205  private:
206  HttpRequestFirstLine* m_FirstLine;
207  };
208 
209 
210 
211 
212 
213  // -------- Class HttpResponseLayer -----------------
214 
215  class HttpResponseFirstLine;
216 
217 
231  {
232  friend class HttpResponseFirstLine;
233  public:
238  {
401  };
402 
403 
410  HttpResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
411 
421  HttpResponseLayer(HttpVersion version, HttpResponseLayer::HttpResponseStatusCode statusCode, std::string statusCodeString = "");
422 
423  virtual ~HttpResponseLayer();
424 
430  HttpResponseLayer(const HttpResponseLayer& other);
431 
438 
442  inline HttpResponseFirstLine* getFirstLine() { return m_FirstLine; }
443 
456  HeaderField* setContentLength(int contentLength, const std::string prevFieldName = "");
457 
463  int getContentLength();
464 
465  // implement Layer's abstract methods
466 
467  std::string toString();
468 
469  private:
470  HttpResponseFirstLine* m_FirstLine;
471 
472  };
473 
474 
475 
476 
477 
478  // -------- Class HttpRequestFirstLine -----------------
479 
492  {
493  friend class HttpRequestLayer;
494  public:
498  inline HttpRequestLayer::HttpMethod getMethod() { return m_Method; }
499 
505  bool setMethod(HttpRequestLayer::HttpMethod newMethod);
506 
510  std::string getUri();
511 
517  bool setUri(std::string newUri);
518 
522  inline HttpVersion getVersion() { return m_Version; }
523 
529  void setVersion(HttpVersion newVersion);
530 
537  static HttpRequestLayer::HttpMethod parseMethod(char* data, size_t dataLen);
538 
542  inline int getSize() { return m_FirstLineEndOffset; }
543 
550  inline bool isComplete() { return m_IsComplete; }
551 
558  class HttpRequestFirstLineException : public std::exception
559  {
560  public:
561  ~HttpRequestFirstLineException() throw() {}
562  void setMessage(std::string message) { m_Message = message; }
563  virtual const char* what() const throw()
564  {
565  return m_Message.c_str();
566  }
567  private:
568  std::string m_Message;
569  };
570  private:
572  HttpRequestFirstLine(HttpRequestLayer* httpRequest, HttpRequestLayer::HttpMethod method, HttpVersion version, std::string uri = "/");
573  //throw(HttpRequestFirstLineException); // Deprecated in C++17
574 
575  void parseVersion();
576 
577  HttpRequestLayer* m_HttpRequest;
579  HttpVersion m_Version;
580  int m_VersionOffset;
581  int m_UriOffset;
582  int m_FirstLineEndOffset;
583  bool m_IsComplete;
584  HttpRequestFirstLineException m_Exception;
585  };
586 
587 
588 
589 
590 
591  // -------- Class HttpResponseFirstLine -----------------
592 
604  {
605  friend class HttpResponseLayer;
606  public:
611 
615  int getStatusCodeAsInt();
616 
620  std::string getStatusCodeString();
621 
628  bool setStatusCode(HttpResponseLayer::HttpResponseStatusCode newStatusCode, std::string statusCodeString = "");
629 
633  inline HttpVersion getVersion() { return m_Version; }
634 
640  void setVersion(HttpVersion newVersion);
641 
648  static HttpResponseLayer::HttpResponseStatusCode parseStatusCode(char* data, size_t dataLen);
649 
653  inline int getSize() { return m_FirstLineEndOffset; }
654 
661  inline bool isComplete() { return m_IsComplete; }
662 
669  class HttpResponseFirstLineException : public std::exception
670  {
671  public:
672  ~HttpResponseFirstLineException() throw() {}
673  void setMessage(std::string message) { m_Message = message; }
674  virtual const char* what() const throw()
675  {
676  return m_Message.c_str();
677  }
678  private:
679  std::string m_Message;
680  };
681 
682  private:
684  HttpResponseFirstLine(HttpResponseLayer* httpResponse, HttpVersion version, HttpResponseLayer::HttpResponseStatusCode statusCode, std::string statusCodeString = "");
685 
686  static HttpVersion parseVersion(char* data, size_t dataLen);
687  static HttpResponseLayer::HttpResponseStatusCode validateStatusCode(char* data, size_t dataLen, HttpResponseLayer::HttpResponseStatusCode potentialCode);
688 
689 
690  HttpResponseLayer* m_HttpResponse;
691  HttpVersion m_Version;
693  int m_FirstLineEndOffset;
694  bool m_IsComplete;
695  HttpResponseFirstLineException m_Exception;
696  };
697 
698 } // namespace pcpp
699 
700 #endif /* PACKETPP_HTTP_LAYER */
Definition: HttpLayer.h:28
Definition: HttpLayer.h:146
Definition: HttpLayer.h:276
Definition: HttpLayer.h:328
std::string toString()
HttpResponseLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: TextBasedProtocol.h:104
Definition: HttpLayer.h:26
std::string toString()
Definition: HttpLayer.h:144
Definition: HttpLayer.h:124
static const std::map< uint16_t, bool > * getHTTPPortMap()
static HttpRequestLayer::HttpMethod parseMethod(char *data, size_t dataLen)
Definition: HttpLayer.h:270
void setVersion(HttpVersion newVersion)
Definition: Packet.h:26
Definition: HttpLayer.h:248
Definition: ProtocolType.h:223
The main namespace for the PcapPlusPlus lib.
Definition: HttpLayer.h:292
Definition: TextBasedProtocol.h:31
Definition: HttpLayer.h:134
Definition: HttpLayer.h:264
HttpVersion getVersion()
Definition: HttpLayer.h:633
Definition: HttpLayer.h:491
Definition: HttpLayer.h:142
Definition: HttpLayer.h:354
Definition: HttpLayer.h:71
Definition: HttpLayer.h:138
HttpRequestLayer::HttpMethod getMethod()
Definition: HttpLayer.h:498
OsiModelLayer
Definition: ProtocolType.h:208
Definition: HttpLayer.h:30
HeaderField * setContentLength(int contentLength, const std::string prevFieldName="")
Definition: HttpLayer.h:302
Definition: HttpLayer.h:603
Definition: HttpLayer.h:148
bool isComplete()
Definition: HttpLayer.h:661
void setVersion(HttpVersion newVersion)
Definition: HttpLayer.h:246
Definition: HttpLayer.h:140
bool setStatusCode(HttpResponseLayer::HttpResponseStatusCode newStatusCode, std::string statusCodeString="")
std::string getUrl()
Definition: HttpLayer.h:240
bool setUri(std::string newUri)
int getSize()
Definition: HttpLayer.h:653
HttpMethod
Definition: HttpLayer.h:131
Definition: Layer.h:70
HttpResponseStatusCode
Definition: HttpLayer.h:237
HttpVersion
Definition: HttpLayer.h:21
HttpRequestLayer & operator=(const HttpRequestLayer &other)
HttpResponseFirstLine * getFirstLine()
Definition: HttpLayer.h:442
HttpVersion getVersion()
Definition: HttpLayer.h:522
virtual HeaderField * addField(const std::string &fieldName, const std::string &fieldValue)
Definition: HttpLayer.h:272
Definition: HttpLayer.h:304
std::string getStatusCodeString()
Definition: HttpLayer.h:360
bool setMethod(HttpRequestLayer::HttpMethod newMethod)
Definition: HttpLayer.h:150
Definition: HttpLayer.h:348
Definition: HttpLayer.h:324
HttpRequestFirstLine * getFirstLine()
Definition: HttpLayer.h:191
HttpResponseLayer & operator=(const HttpResponseLayer &other)
virtual HeaderField * insertField(HeaderField *prevField, const std::string &fieldName, const std::string &fieldValue)
Definition: HttpLayer.h:250
HttpResponseLayer::HttpResponseStatusCode getStatusCode()
Definition: HttpLayer.h:610
OsiModelLayer getOsiModelLayer()
Definition: HttpLayer.h:89
Definition: HttpLayer.h:24
static HttpResponseLayer::HttpResponseStatusCode parseStatusCode(char *data, size_t dataLen)
bool isComplete()
Definition: HttpLayer.h:550
HttpRequestLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: HttpLayer.h:230
int getSize()
Definition: HttpLayer.h:542
Definition: HttpLayer.h:136