PcapPlusPlus  24.09
HttpLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "DeprecationUtils.h"
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"
37 #define PCPP_HTTP_CONNECTION_FIELD "Connection"
39 #define PCPP_HTTP_USER_AGENT_FIELD "User-Agent"
41 #define PCPP_HTTP_REFERER_FIELD "Referer"
43 #define PCPP_HTTP_ACCEPT_FIELD "Accept"
45 #define PCPP_HTTP_ACCEPT_ENCODING_FIELD "Accept-Encoding"
47 #define PCPP_HTTP_ACCEPT_LANGUAGE_FIELD "Accept-Language"
49 #define PCPP_HTTP_COOKIE_FIELD "Cookie"
51 #define PCPP_HTTP_CONTENT_LENGTH_FIELD "Content-Length"
53 #define PCPP_HTTP_CONTENT_ENCODING_FIELD "Content-Encoding"
55 #define PCPP_HTTP_CONTENT_TYPE_FIELD "Content-Type"
57 #define PCPP_HTTP_TRANSFER_ENCODING_FIELD "Transfer-Encoding"
59 #define PCPP_HTTP_SERVER_FIELD "Server"
60 
61  // -------- classes to be defined later -----------------
62 
63  class HttpRequestFirstLine;
64  class HttpResponseFirstLine;
65 
66  // -------- Class HttpMessage -----------------
67 
74  {
75  public:
76  virtual ~HttpMessage()
77  {}
78 
84  static bool isHttpPort(uint16_t port)
85  {
86  return port == 80 || port == 8080;
87  }
88 
89  // overridden methods
90 
91  virtual HeaderField* addField(const std::string& fieldName, const std::string& fieldValue);
92  virtual HeaderField* addField(const HeaderField& newField);
93  virtual HeaderField* insertField(HeaderField* prevField, const std::string& fieldName,
94  const std::string& fieldValue);
95  virtual HeaderField* insertField(HeaderField* prevField, const HeaderField& newField);
96 
98  {
100  }
101 
102  protected:
103  HttpMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType protocol)
104  : TextBasedProtocolMessage(data, dataLen, prevLayer, packet, protocol)
105  {}
106  HttpMessage() : TextBasedProtocolMessage()
107  {}
108  HttpMessage(const HttpMessage& other) : TextBasedProtocolMessage(other)
109  {}
110  HttpMessage& operator=(const HttpMessage& other)
111  {
112  TextBasedProtocolMessage::operator=(other);
113  return *this;
114  }
115 
116  // implementation of abstract methods
117  char getHeaderFieldNameValueSeparator() const
118  {
119  return ':';
120  }
121  bool spacesAllowedBetweenHeaderFieldNameAndValue() const
122  {
123  return true;
124  }
125  };
126 
127  // -------- Class HttpRequestLayer -----------------
128 
142  {
143  friend class HttpRequestFirstLine;
144 
145  public:
150  {
171  };
172 
179  HttpRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
180 
188  HttpRequestLayer(HttpMethod method, const std::string& uri, HttpVersion version);
189 
190  virtual ~HttpRequestLayer();
191 
198 
206 
211  {
212  return m_FirstLine;
213  }
214 
222  std::string getUrl() const;
223 
224  // implement Layer's abstract methods
225  std::string toString() const;
226 
227  private:
228  HttpRequestFirstLine* m_FirstLine;
229  };
230 
231  // -------- Class HttpResponseStatusCode -----------------
232 
238  {
239  public:
243  enum Value : int
244  {
256  Http200OK = 200,
283  Http302 = 302,
319  Http410Gone = 410,
339  Http420 = 420,
372  Http451 = 451,
385  Http499 = 499,
386 
427 
428  // clang-format off
430  HttpStatus1xxCodeUnknown = 900001, // 1xx: Informational - Request received, continuing process
431  HttpStatus2xxCodeUnknown = 900002, // 2xx: Success - The action was successfully received, understood, and accepted
432  HttpStatus3xxCodeUnknown = 900003, // 3xx: Redirection - Further action must be taken in order to complete the request
433  HttpStatus4xxCodeUnknown = 900004, // 4xx: Client Error - The request contains bad syntax or cannot be fulfilled
434  HttpStatus5xxCodeUnknown = 900005, // 5xx: Server Error - The server failed to fulfill an apparently valid request
435  HttpStatusCodeUnknown = 999999, // other arbitrary number
436  // clang-format on
437  };
438 
439  HttpResponseStatusCode() = default;
440 
441  // cppcheck-suppress noExplicitConstructor
446  HttpResponseStatusCode(Value statusCode) : m_Value(statusCode)
447  {}
448 
454  explicit HttpResponseStatusCode(const int& statusCodeNumber, const std::string& statusMessage = "");
455 
461  explicit HttpResponseStatusCode(const Value& statusCode, const std::string& statusMessage);
462 
463  // Allow switch and comparisons.
464  operator Value() const
465  {
466  return m_Value;
467  }
468  // Prevent usage: if(httpResponseStatusCode)
469  explicit operator bool() const = delete;
470 
474  std::string toString() const
475  {
476  return std::to_string(m_Value);
477  }
478 
482  int toInt() const
483  {
484  return static_cast<int>(m_Value);
485  }
486 
490  std::string getMessage() const;
495  bool isUnsupportedCode() const
496  {
497  return m_Value > 599;
498  }
499 
500  private:
501  Value m_Value = HttpStatusCodeUnknown;
502  std::string m_CustomizedMessage;
503  };
504 
505  // -------- Class HttpResponseLayer -----------------
506 
520  {
521  friend class HttpResponseFirstLine;
522 
523  public:
524  // backward compatibility
526 
533  HttpResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
534 
545  PCPP_DEPRECATED("Use other constructors instead")
546  explicit HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode,
547  const std::string& statusCodeString);
548 
555  explicit HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode);
556 
557  virtual ~HttpResponseLayer();
558 
565 
572  HttpResponseLayer& operator=(const HttpResponseLayer& other);
573 
578  {
579  return m_FirstLine;
580  }
581 
597  HeaderField* setContentLength(int contentLength, const std::string& prevFieldName = "");
598 
605  int getContentLength() const;
606 
607  // implement Layer's abstract methods
608 
609  std::string toString() const;
610 
611  private:
612  HttpResponseFirstLine* m_FirstLine;
613  };
614 
615  // -------- Class HttpRequestFirstLine -----------------
616 
629  {
630  friend class HttpRequestLayer;
631 
632  public:
637  {
638  return m_Method;
639  }
640 
648 
653  std::string getUri() const;
654 
660  bool setUri(std::string newUri);
661 
666  {
667  return m_Version;
668  }
669 
675  void setVersion(HttpVersion newVersion);
676 
683  static HttpRequestLayer::HttpMethod parseMethod(const char* data, size_t dataLen);
684 
688  int getSize() const
689  {
690  return m_FirstLineEndOffset;
691  }
692 
699  bool isComplete() const
700  {
701  return m_IsComplete;
702  }
703 
710  class HttpRequestFirstLineException : public std::exception
711  {
712  public:
714  {}
715  void setMessage(const std::string& message)
716  {
717  m_Message = message;
718  }
719  virtual const char* what() const noexcept
720  {
721  return m_Message.c_str();
722  }
723 
724  private:
725  std::string m_Message;
726  };
727 
728  private:
731  const std::string& uri = "/");
732 
733  void parseVersion();
734 
735  HttpRequestLayer* m_HttpRequest;
737  HttpVersion m_Version;
738  int m_VersionOffset;
739  int m_UriOffset;
740  int m_FirstLineEndOffset;
741  bool m_IsComplete;
742  HttpRequestFirstLineException m_Exception;
743  };
744 
745  // -------- Class HttpResponseFirstLine -----------------
746 
759  {
760  friend class HttpResponseLayer;
761 
762  public:
767  {
768  return m_StatusCode;
769  }
770 
774  int getStatusCodeAsInt() const;
775 
779  std::string getStatusCodeString() const;
780 
790  PCPP_DEPRECATED("Use the other overload instead")
791  bool setStatusCode(const HttpResponseStatusCode& newStatusCode, const std::string& statusCodeString);
792 
798  bool setStatusCode(const HttpResponseStatusCode& newStatusCode);
799 
804  {
805  return m_Version;
806  }
807 
813  void setVersion(HttpVersion newVersion);
814 
821  static HttpResponseStatusCode parseStatusCode(const char* data, size_t dataLen);
822 
829  static HttpVersion parseVersion(const char* data, size_t dataLen);
830 
834  int getSize() const
835  {
836  return m_FirstLineEndOffset;
837  }
838 
845  bool isComplete() const
846  {
847  return m_IsComplete;
848  }
849 
857  class HttpResponseFirstLineException : public std::exception
858  {
859  public:
861  {}
862  void setMessage(const std::string& message)
863  {
864  m_Message = message;
865  }
866  virtual const char* what() const noexcept
867  {
868  return m_Message.c_str();
869  }
870 
871  private:
872  std::string m_Message;
873  };
874 
875  private:
877  HttpResponseFirstLine(HttpResponseLayer* httpResponse, HttpVersion version,
878  const HttpResponseStatusCode& statusCode);
879 
880  HttpResponseLayer* m_HttpResponse;
881  HttpVersion m_Version;
882  HttpResponseStatusCode m_StatusCode;
883  int m_FirstLineEndOffset;
884  bool m_IsComplete;
885  HttpResponseFirstLineException m_Exception;
886  };
887 
888 } // namespace pcpp
Definition: TextBasedProtocol.h:30
Definition: HttpLayer.h:74
virtual HeaderField * addField(const std::string &fieldName, const std::string &fieldValue)
virtual HeaderField * insertField(HeaderField *prevField, const HeaderField &newField)
OsiModelLayer getOsiModelLayer() const
Definition: HttpLayer.h:97
virtual HeaderField * addField(const HeaderField &newField)
static bool isHttpPort(uint16_t port)
Definition: HttpLayer.h:84
virtual HeaderField * insertField(HeaderField *prevField, const std::string &fieldName, const std::string &fieldValue)
Definition: HttpLayer.h:629
bool setMethod(HttpRequestLayer::HttpMethod newMethod)
int getSize() const
Definition: HttpLayer.h:688
HttpRequestLayer::HttpMethod getMethod() const
Definition: HttpLayer.h:636
bool isComplete() const
Definition: HttpLayer.h:699
bool setUri(std::string newUri)
std::string getUri() const
static HttpRequestLayer::HttpMethod parseMethod(const char *data, size_t dataLen)
void setVersion(HttpVersion newVersion)
HttpVersion getVersion() const
Definition: HttpLayer.h:665
Definition: HttpLayer.h:142
HttpRequestLayer & operator=(const HttpRequestLayer &other)
HttpMethod
Definition: HttpLayer.h:150
@ HttpDELETE
Definition: HttpLayer.h:160
@ HttpCONNECT
Definition: HttpLayer.h:166
@ HttpPOST
Definition: HttpLayer.h:156
@ HttpMethodUnknown
Definition: HttpLayer.h:170
@ HttpTRACE
Definition: HttpLayer.h:162
@ HttpOPTIONS
Definition: HttpLayer.h:164
@ HttpHEAD
Definition: HttpLayer.h:154
@ HttpPATCH
Definition: HttpLayer.h:168
@ HttpGET
Definition: HttpLayer.h:152
@ HttpPUT
Definition: HttpLayer.h:158
std::string toString() const
HttpRequestLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
HttpRequestLayer(const HttpRequestLayer &other)
HttpRequestLayer(HttpMethod method, const std::string &uri, HttpVersion version)
std::string getUrl() const
HttpRequestFirstLine * getFirstLine() const
Definition: HttpLayer.h:210
Definition: HttpLayer.h:759
bool isComplete() const
Definition: HttpLayer.h:845
HttpVersion getVersion() const
Definition: HttpLayer.h:803
static HttpVersion parseVersion(const char *data, size_t dataLen)
HttpResponseStatusCode getStatusCode() const
Definition: HttpLayer.h:766
std::string getStatusCodeString() const
bool setStatusCode(const HttpResponseStatusCode &newStatusCode, const std::string &statusCodeString)
void setVersion(HttpVersion newVersion)
int getSize() const
Definition: HttpLayer.h:834
static HttpResponseStatusCode parseStatusCode(const char *data, size_t dataLen)
Definition: HttpLayer.h:520
HttpResponseLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
HttpResponseFirstLine * getFirstLine() const
Definition: HttpLayer.h:577
std::string toString() const
HeaderField * setContentLength(int contentLength, const std::string &prevFieldName="")
int getContentLength() const
The enum wrapper class of HTTP response status codes.
Definition: HttpLayer.h:238
int toInt() const
get status code number as int
Definition: HttpLayer.h:482
bool isUnsupportedCode() const
Definition: HttpLayer.h:495
HttpResponseStatusCode(const int &statusCodeNumber, const std::string &statusMessage="")
Construct HttpResponseStatusCode from the code number and the customized message.
std::string toString() const
get status code number as string
Definition: HttpLayer.h:474
HttpResponseStatusCode(const Value &statusCode, const std::string &statusMessage)
Construct HttpResponseStatusCode from Value enum and the customized message.
std::string getMessage() const
get status code message, e.g. "OK", "Not Found"
Value
Define enum types and the corresponding int values.
Definition: HttpLayer.h:244
@ Http507InsufficientStorage
Definition: HttpLayer.h:402
@ Http301MovedPermanently
Definition: HttpLayer.h:281
@ Http306SwitchProxy
Definition: HttpLayer.h:291
@ Http499
Definition: HttpLayer.h:385
@ Http599NetworkConnectTimeoutError
Definition: HttpLayer.h:426
@ Http410Gone
Definition: HttpLayer.h:319
@ Http100Continue
Definition: HttpLayer.h:246
@ Http502BadGateway
Definition: HttpLayer.h:392
@ Http411LengthRequired
Definition: HttpLayer.h:321
@ Http407ProxyAuthenticationRequired
Definition: HttpLayer.h:313
@ Http308PermanentRedirect
Definition: HttpLayer.h:295
@ Http205ResetContent
Definition: HttpLayer.h:266
@ Http300MultipleChoices
Definition: HttpLayer.h:279
@ Http402PaymentRequired
Definition: HttpLayer.h:303
@ Http207MultiStatus
Definition: HttpLayer.h:270
@ Http420
Definition: HttpLayer.h:339
@ Http400BadRequest
Definition: HttpLayer.h:299
@ Http509BandwidthLimitExceeded
Definition: HttpLayer.h:406
@ Http426UpgradeRequired
Definition: HttpLayer.h:351
@ Http494RequestHeaderTooLarge
Definition: HttpLayer.h:375
@ Http511NetworkAuthenticationRequired
Definition: HttpLayer.h:410
@ Http598NetworkReadTimeoutError
Definition: HttpLayer.h:424
@ Http501NotImplemented
Definition: HttpLayer.h:390
@ Http523ProxyDeclinedRequest
Definition: HttpLayer.h:419
@ Http421MisdirectedRequest
Definition: HttpLayer.h:341
@ Http404NotFound
Definition: HttpLayer.h:307
@ Http418ImATeapot
Definition: HttpLayer.h:335
@ Http305UseProxy
Definition: HttpLayer.h:289
@ Http226IMUsed
Definition: HttpLayer.h:275
@ Http498TokenExpiredInvalid
Definition: HttpLayer.h:383
@ Http416RequestedRangeNotSatisfiable
Definition: HttpLayer.h:331
@ Http524aTimeoutOccurred
Definition: HttpLayer.h:421
@ Http444NoResponse
Definition: HttpLayer.h:365
@ Http414RequestURITooLong
Definition: HttpLayer.h:327
@ Http425TooEarly
Definition: HttpLayer.h:349
@ Http401Unauthorized
Definition: HttpLayer.h:301
@ Http429TooManyRequests
Definition: HttpLayer.h:356
@ Http450BlockedByWindowsParentalControls
Definition: HttpLayer.h:370
@ Http204NoContent
Definition: HttpLayer.h:264
@ Http508LoopDetected
Definition: HttpLayer.h:404
@ Http510NotExtended
Definition: HttpLayer.h:408
@ Http403Forbidden
Definition: HttpLayer.h:305
@ Http408RequestTimeout
Definition: HttpLayer.h:315
@ Http440LoginTimeout
Definition: HttpLayer.h:362
@ Http500InternalServerError
Definition: HttpLayer.h:388
@ Http505HTTPVersionNotSupported
Definition: HttpLayer.h:398
@ Http206PartialContent
Definition: HttpLayer.h:268
@ Http520OriginError
Definition: HttpLayer.h:413
@ Http302
Definition: HttpLayer.h:283
@ Http409Conflict
Definition: HttpLayer.h:317
@ Http419AuthenticationTimeout
Definition: HttpLayer.h:337
@ Http406NotAcceptable
Definition: HttpLayer.h:311
@ Http451
Definition: HttpLayer.h:372
@ Http496NoCert
Definition: HttpLayer.h:379
@ Http103EarlyHints
Definition: HttpLayer.h:252
@ Http202Accepted
Definition: HttpLayer.h:260
@ Http522ConnectionTimedOut
Definition: HttpLayer.h:417
@ Http203NonAuthoritativeInformation
Definition: HttpLayer.h:262
@ Http304NotModified
Definition: HttpLayer.h:287
@ Http201Created
Definition: HttpLayer.h:258
@ Http101SwitchingProtocols
Definition: HttpLayer.h:248
@ Http413RequestEntityTooLarge
Definition: HttpLayer.h:325
@ Http495CertError
Definition: HttpLayer.h:377
@ Http497HTTPtoHTTPS
Definition: HttpLayer.h:381
@ Http200OK
Definition: HttpLayer.h:256
@ Http431RequestHeaderFieldsTooLarge
Definition: HttpLayer.h:359
@ Http405MethodNotAllowed
Definition: HttpLayer.h:309
@ Http449RetryWith
Definition: HttpLayer.h:368
@ Http506VariantAlsoNegotiates
Definition: HttpLayer.h:400
@ Http503ServiceUnavailable
Definition: HttpLayer.h:394
@ Http102Processing
Definition: HttpLayer.h:250
@ HttpStatus1xxCodeUnknown
Definition: HttpLayer.h:430
@ Http521WebServerIsDown
Definition: HttpLayer.h:415
@ Http415UnsupportedMediaType
Definition: HttpLayer.h:329
@ Http504GatewayTimeout
Definition: HttpLayer.h:396
@ Http412PreconditionFailed
Definition: HttpLayer.h:323
@ Http417ExpectationFailed
Definition: HttpLayer.h:333
@ Http428PreconditionRequired
Definition: HttpLayer.h:354
@ Http303SeeOther
Definition: HttpLayer.h:285
@ Http307TemporaryRedirect
Definition: HttpLayer.h:293
@ Http424FailedDependency
Definition: HttpLayer.h:347
@ Http422UnprocessableEntity
Definition: HttpLayer.h:343
@ Http423Locked
Definition: HttpLayer.h:345
@ Http208AlreadyReported
Definition: HttpLayer.h:272
HttpResponseStatusCode(Value statusCode)
Construct HttpResponseStatusCode from Value enum.
Definition: HttpLayer.h:446
Definition: Layer.h:70
Definition: Packet.h:27
Definition: TextBasedProtocol.h:123
The main namespace for the PcapPlusPlus lib.
uint8_t ProtocolType
Definition: ProtocolType.h:17
OsiModelLayer
Definition: ProtocolType.h:354
@ OsiModelApplicationLayer
Definition: ProtocolType.h:368
HttpVersion
Definition: HttpLayer.h:21
@ HttpVersionUnknown
Definition: HttpLayer.h:29
@ ZeroDotNine
Definition: HttpLayer.h:23
@ OneDotZero
Definition: HttpLayer.h:25
@ OneDotOne
Definition: HttpLayer.h:27