PcapPlusPlus  23.09
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 
8 #ifndef PCPP_DEPRECATED
9 #if defined(__GNUC__) || defined(__clang__)
10 #define PCPP_DEPRECATED __attribute__((deprecated))
11 #elif defined(_MSC_VER)
12 #define PCPP_DEPRECATED __declspec(deprecated)
13 #else
14 #pragma message("WARNING: DEPRECATED feature is not implemented for this compiler")
15 #define PCPP_DEPRECATED
16 #endif
17 #endif
18 
20 
25 namespace pcpp
26 {
27 
32  {
41  };
42 
43  // some popular HTTP fields
44 
46 #define PCPP_HTTP_HOST_FIELD "Host"
47 
48 #define PCPP_HTTP_CONNECTION_FIELD "Connection"
49 
50 #define PCPP_HTTP_USER_AGENT_FIELD "User-Agent"
51 
52 #define PCPP_HTTP_REFERER_FIELD "Referer"
53 
54 #define PCPP_HTTP_ACCEPT_FIELD "Accept"
55 
56 #define PCPP_HTTP_ACCEPT_ENCODING_FIELD "Accept-Encoding"
57 
58 #define PCPP_HTTP_ACCEPT_LANGUAGE_FIELD "Accept-Language"
59 
60 #define PCPP_HTTP_COOKIE_FIELD "Cookie"
61 
62 #define PCPP_HTTP_CONTENT_LENGTH_FIELD "Content-Length"
63 
64 #define PCPP_HTTP_CONTENT_ENCODING_FIELD "Content-Encoding"
65 
66 #define PCPP_HTTP_CONTENT_TYPE_FIELD "Content-Type"
67 
68 #define PCPP_HTTP_TRANSFER_ENCODING_FIELD "Transfer-Encoding"
69 
70 #define PCPP_HTTP_SERVER_FIELD "Server"
71 
72 
73  // -------- classes to be defined later -----------------
74 
75 
76  class HttpRequestFirstLine;
77  class HttpResponseFirstLine;
78 
79 
80  // -------- Class HttpMessage -----------------
81 
82 
88  {
89  public:
90 
91  virtual ~HttpMessage() {}
92 
98  static bool isHttpPort(uint16_t port) { return port == 80 || port == 8080; }
99 
100  // overridden methods
101 
102  virtual HeaderField* addField(const std::string& fieldName, const std::string& fieldValue);
103  virtual HeaderField* addField(const HeaderField& newField);
104  virtual HeaderField* insertField(HeaderField* prevField, const std::string& fieldName, const std::string& fieldValue);
105  virtual HeaderField* insertField(HeaderField* prevField, const HeaderField& newField);
106 
108 
109  protected:
110  HttpMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : TextBasedProtocolMessage(data, dataLen, prevLayer, packet) {}
112  HttpMessage(const HttpMessage& other) : TextBasedProtocolMessage(other) {}
113  HttpMessage& operator=(const HttpMessage& other) { TextBasedProtocolMessage::operator=(other); return *this; }
114 
115  // implementation of abstract methods
116  char getHeaderFieldNameValueSeparator() const { return ':'; }
117  bool spacesAllowedBetweenHeaderFieldNameAndValue() const { return true; }
118  };
119 
120 
121  // -------- Class HttpRequestLayer -----------------
122 
136  {
137  friend class HttpRequestFirstLine;
138  public:
143  {
163  HttpMethodUnknown
164  };
165 
172  HttpRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
173 
181  HttpRequestLayer(HttpMethod method, const std::string& uri, HttpVersion version);
182 
183  virtual ~HttpRequestLayer();
184 
190  HttpRequestLayer(const HttpRequestLayer& other);
191 
198  HttpRequestLayer& operator=(const HttpRequestLayer& other);
199 
203  HttpRequestFirstLine* getFirstLine() const { return m_FirstLine; }
204 
212  std::string getUrl() const;
213 
214  // implement Layer's abstract methods
215  std::string toString() const;
216 
217  private:
218  HttpRequestFirstLine* m_FirstLine;
219  };
220 
221  // -------- Class HttpResponseStatusCode -----------------
222 
228  {
229  public:
233  enum Value: int
234  {
236  Http100Continue = 100,
238  Http101SwitchingProtocols = 101,
240  Http102Processing = 102,
242  Http103EarlyHints = 103,
246  Http200OK= 200,
248  Http201Created = 201,
250  Http202Accepted = 202,
252  Http203NonAuthoritativeInformation = 203,
254  Http204NoContent = 204,
256  Http205ResetContent = 205,
258  Http206PartialContent = 206,
260  Http207MultiStatus = 207,
262  Http208AlreadyReported = 208,
265  Http226IMUsed = 226,
269  Http300MultipleChoices = 300,
271  Http301MovedPermanently = 301,
273  Http302 = 302,
275  Http303SeeOther = 303,
277  Http304NotModified = 304,
279  Http305UseProxy = 305,
281  Http306SwitchProxy = 306,
283  Http307TemporaryRedirect = 307,
285  Http308PermanentRedirect = 308,
289  Http400BadRequest = 400,
291  Http401Unauthorized = 401,
293  Http402PaymentRequired = 402,
295  Http403Forbidden = 403,
297  Http404NotFound = 404,
299  Http405MethodNotAllowed = 405,
301  Http406NotAcceptable = 406,
303  Http407ProxyAuthenticationRequired = 407,
305  Http408RequestTimeout = 408,
307  Http409Conflict = 409,
309  Http410Gone = 410,
311  Http411LengthRequired = 411,
313  Http412PreconditionFailed = 412,
315  Http413RequestEntityTooLarge = 413,
317  Http414RequestURITooLong = 414,
319  Http415UnsupportedMediaType = 415,
321  Http416RequestedRangeNotSatisfiable = 416,
323  Http417ExpectationFailed = 417,
325  Http418ImATeapot = 418,
327  Http419AuthenticationTimeout = 419,
329  Http420 = 420,
331  Http421MisdirectedRequest = 421,
333  Http422UnprocessableEntity = 422,
335  Http423Locked = 423,
337  Http424FailedDependency = 424,
339  Http425TooEarly = 425,
341  Http426UpgradeRequired = 426,
344  Http428PreconditionRequired = 428,
346  Http429TooManyRequests = 429,
349  Http431RequestHeaderFieldsTooLarge = 431,
352  Http440LoginTimeout = 440,
355  Http444NoResponse = 444,
358  Http449RetryWith = 449,
360  Http450BlockedByWindowsParentalControls = 450,
362  Http451 = 451,
365  Http494RequestHeaderTooLarge = 494,
367  Http495CertError = 495,
369  Http496NoCert = 496,
371  Http497HTTPtoHTTPS = 497,
373  Http498TokenExpiredInvalid = 498,
375  Http499 = 499,
376 
378  Http500InternalServerError = 500,
380  Http501NotImplemented = 501,
382  Http502BadGateway = 502,
384  Http503ServiceUnavailable = 503,
386  Http504GatewayTimeout = 504,
388  Http505HTTPVersionNotSupported = 505,
390  Http506VariantAlsoNegotiates = 506,
392  Http507InsufficientStorage = 507,
394  Http508LoopDetected = 508,
396  Http509BandwidthLimitExceeded = 509,
398  Http510NotExtended = 510,
400  Http511NetworkAuthenticationRequired = 511,
403  Http520OriginError = 520,
405  Http521WebServerIsDown = 521,
407  Http522ConnectionTimedOut = 522,
409  Http523ProxyDeclinedRequest = 523,
411  Http524aTimeoutOccurred = 524,
414  Http598NetworkReadTimeoutError = 598,
416  Http599NetworkConnectTimeoutError = 599,
417 
419  HttpStatus1xxCodeUnknown = 900001, // 1xx: Informational - Request received, continuing process
420  HttpStatus2xxCodeUnknown = 900002, // 2xx: Success - The action was successfully received, understood, and accepted
421  HttpStatus3xxCodeUnknown = 900003, // 3xx: Redirection - Further action must be taken in order to complete the request
422  HttpStatus4xxCodeUnknown = 900004, // 4xx: Client Error - The request contains bad syntax or cannot be fulfilled
423  HttpStatus5xxCodeUnknown = 900005, // 5xx: Server Error - The server failed to fulfill an apparently valid request
424  HttpStatusCodeUnknown = 999999, // other arbitrary number
425  };
426 
427  HttpResponseStatusCode() = default;
428 
429  // cppcheck-suppress noExplicitConstructor
434  HttpResponseStatusCode(Value statusCode) : m_Value(statusCode) { }
435 
441  explicit HttpResponseStatusCode(const int &statusCodeNumber, const std::string& statusMessage = "");
442 
448  explicit HttpResponseStatusCode(const Value& statusCode, const std::string& statusMessage);
449 
450  // Allow switch and comparisons.
451  operator Value() const { return m_Value; }
452  // Prevent usage: if(httpResponseStatusCode)
453  explicit operator bool() const = delete;
454 
458  std::string toString() const
459  {
460  return std::to_string(m_Value);
461  }
462 
466  int toInt() const
467  {
468  return static_cast<int>(m_Value);
469  }
470 
474  std::string getMessage() const;
479  bool isUnsupportedCode() const
480  {
481  return m_Value > 599;
482  }
483 
484  private:
485  Value m_Value = HttpStatusCodeUnknown;
486  std::string m_CustomizedMessage;
487  };
488 
489  // -------- Class HttpResponseLayer -----------------
490 
504  {
505  friend class HttpResponseFirstLine;
506  public:
507  // backward compatibility
509 
516  HttpResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
517 
528  PCPP_DEPRECATED explicit HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode, const std::string& statusCodeString);
529 
536  explicit HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode);
537 
538  virtual ~HttpResponseLayer();
539 
545  HttpResponseLayer(const HttpResponseLayer& other);
546 
553  HttpResponseLayer& operator=(const HttpResponseLayer& other);
554 
558  HttpResponseFirstLine* getFirstLine() const { return m_FirstLine; }
559 
572  HeaderField* setContentLength(int contentLength, const std::string &prevFieldName = "");
573 
579  int getContentLength() const;
580 
581  // implement Layer's abstract methods
582 
583  std::string toString() const;
584 
585  private:
586  HttpResponseFirstLine* m_FirstLine;
587 
588  };
589 
590 
591 
592 
593 
594  // -------- Class HttpRequestFirstLine -----------------
595 
608  {
609  friend class HttpRequestLayer;
610  public:
614  HttpRequestLayer::HttpMethod getMethod() const { return m_Method; }
615 
621  bool setMethod(HttpRequestLayer::HttpMethod newMethod);
622 
626  std::string getUri() const;
627 
633  bool setUri(std::string newUri);
634 
638  HttpVersion getVersion() const { return m_Version; }
639 
645  void setVersion(HttpVersion newVersion);
646 
653  static HttpRequestLayer::HttpMethod parseMethod(const char* data, size_t dataLen);
654 
658  int getSize() const { return m_FirstLineEndOffset; }
659 
666  bool isComplete() const { return m_IsComplete; }
667 
674  class HttpRequestFirstLineException : public std::exception
675  {
676  public:
677  ~HttpRequestFirstLineException() throw() {}
678  void setMessage(const std::string &message) { m_Message = message; }
679  virtual const char* what() const throw()
680  {
681  return m_Message.c_str();
682  }
683  private:
684  std::string m_Message;
685  };
686  private:
688  HttpRequestFirstLine(HttpRequestLayer* httpRequest, HttpRequestLayer::HttpMethod method, HttpVersion version, const std::string& uri = "/");
689 
690  void parseVersion();
691 
692  HttpRequestLayer* m_HttpRequest;
694  HttpVersion m_Version;
695  int m_VersionOffset;
696  int m_UriOffset;
697  int m_FirstLineEndOffset;
698  bool m_IsComplete;
699  HttpRequestFirstLineException m_Exception;
700  };
701 
702 
703 
704 
705 
706  // -------- Class HttpResponseFirstLine -----------------
707 
719  {
720  friend class HttpResponseLayer;
721  public:
725  HttpResponseStatusCode getStatusCode() const { return m_StatusCode; }
726 
730  int getStatusCodeAsInt() const;
731 
735  std::string getStatusCodeString() const;
736 
745  PCPP_DEPRECATED bool setStatusCode(const HttpResponseStatusCode& newStatusCode, const std::string& statusCodeString);
746 
752  bool setStatusCode(const HttpResponseStatusCode& newStatusCode);
753 
757  HttpVersion getVersion() const { return m_Version; }
758 
764  void setVersion(HttpVersion newVersion);
765 
772  static HttpResponseStatusCode parseStatusCode(const char* data, size_t dataLen);
773 
780  static HttpVersion parseVersion(const char* data, size_t dataLen);
781 
785  int getSize() const { return m_FirstLineEndOffset; }
786 
793  bool isComplete() const { return m_IsComplete; }
794 
801  class HttpResponseFirstLineException : public std::exception
802  {
803  public:
804  ~HttpResponseFirstLineException() throw() {}
805  void setMessage(const std::string &message) { m_Message = message; }
806  virtual const char* what() const throw()
807  {
808  return m_Message.c_str();
809  }
810  private:
811  std::string m_Message;
812  };
813 
814  private:
816  HttpResponseFirstLine(HttpResponseLayer* httpResponse, HttpVersion version, const HttpResponseStatusCode& statusCode);
817 
818  HttpResponseLayer* m_HttpResponse;
819  HttpVersion m_Version;
820  HttpResponseStatusCode m_StatusCode;
821  int m_FirstLineEndOffset;
822  bool m_IsComplete;
823  HttpResponseFirstLineException m_Exception;
824  };
825 
826 } // namespace pcpp
827 
828 #endif /* PACKETPP_HTTP_LAYER */
The main namespace for the PcapPlusPlus lib.
HttpVersion getVersion() const
Definition: HttpLayer.h:638
OsiModelLayer
Definition: ProtocolType.h:333
Definition: HttpLayer.h:135
Definition: HttpLayer.h:157
int getSize() const
Definition: HttpLayer.h:658
HttpResponseStatusCode(Value statusCode)
Construct HttpResponseStatusCode from Value enum.
Definition: HttpLayer.h:434
Definition: HttpLayer.h:159
Definition: HttpLayer.h:161
Definition: ProtocolType.h:348
Definition: Layer.h:70
Definition: HttpLayer.h:36
virtual HeaderField * addField(const std::string &fieldName, const std::string &fieldValue)
Definition: Packet.h:26
HttpResponseFirstLine * getFirstLine() const
Definition: HttpLayer.h:558
Definition: HttpLayer.h:149
HttpResponseStatusCode getStatusCode() const
Definition: HttpLayer.h:725
bool isComplete() const
Definition: HttpLayer.h:666
OsiModelLayer getOsiModelLayer() const
Definition: HttpLayer.h:107
HttpRequestLayer::HttpMethod getMethod() const
Definition: HttpLayer.h:614
HttpVersion getVersion() const
Definition: HttpLayer.h:757
HttpRequestFirstLine * getFirstLine() const
Definition: HttpLayer.h:203
Definition: HttpLayer.h:503
Definition: TextBasedProtocol.h:115
Definition: HttpLayer.h:147
std::string toString() const
get status code number as string
Definition: HttpLayer.h:458
Definition: HttpLayer.h:40
The enum wrapper class of HTTP response status codes.
Definition: HttpLayer.h:227
HttpMethod
Definition: HttpLayer.h:142
Definition: HttpLayer.h:87
Definition: HttpLayer.h:718
bool isComplete() const
Definition: HttpLayer.h:793
virtual HeaderField * insertField(HeaderField *prevField, const std::string &fieldName, const std::string &fieldValue)
Definition: HttpLayer.h:145
Definition: HttpLayer.h:155
Definition: HttpLayer.h:38
Definition: HttpLayer.h:607
bool isUnsupportedCode() const
Definition: HttpLayer.h:479
Value
Define enum types and the corresponding int values.
Definition: HttpLayer.h:233
virtual std::string toString() const =0
static bool isHttpPort(uint16_t port)
Definition: HttpLayer.h:98
Definition: HttpLayer.h:153
Definition: HttpLayer.h:34
Definition: TextBasedProtocol.h:31
Definition: HttpLayer.h:151
int toInt() const
get status code number as int
Definition: HttpLayer.h:466
HttpVersion
Definition: HttpLayer.h:31
int getSize() const
Definition: HttpLayer.h:785