PcapPlusPlus  Next
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  ~HttpMessage() override = default;
77 
83  static bool isHttpPort(uint16_t port)
84  {
85  return port == 80 || port == 8080;
86  }
87 
88  // overridden methods
89 
90  HeaderField* addField(const std::string& fieldName, const std::string& fieldValue) override;
91  HeaderField* addField(const HeaderField& newField) override;
92  HeaderField* insertField(HeaderField* prevField, const std::string& fieldName,
93  const std::string& fieldValue) override;
94  HeaderField* insertField(HeaderField* prevField, const HeaderField& newField) override;
95 
97  {
99  }
100 
101  protected:
102  HttpMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType protocol)
103  : TextBasedProtocolMessage(data, dataLen, prevLayer, packet, protocol)
104  {}
105  HttpMessage() : TextBasedProtocolMessage()
106  {}
107  HttpMessage(const HttpMessage& other) : TextBasedProtocolMessage(other)
108  {}
109  HttpMessage& operator=(const HttpMessage& other)
110  {
111  TextBasedProtocolMessage::operator=(other);
112  return *this;
113  }
114 
115  // implementation of abstract methods
116  char getHeaderFieldNameValueSeparator() const override
117  {
118  return ':';
119  }
120  bool spacesAllowedBetweenHeaderFieldNameAndValue() const override
121  {
122  return true;
123  }
124  };
125 
126  // -------- Class HttpRequestLayer -----------------
127 
141  {
142  friend class HttpRequestFirstLine;
143 
144  public:
149  {
170  };
171 
178  HttpRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
179 
187  HttpRequestLayer(HttpMethod method, const std::string& uri, HttpVersion version);
188 
189  ~HttpRequestLayer() override;
190 
197 
205 
210  {
211  return m_FirstLine;
212  }
213 
221  std::string getUrl() const;
222 
223  // implement Layer's abstract methods
224  std::string toString() const override;
225 
226  private:
227  HttpRequestFirstLine* m_FirstLine;
228  };
229 
230  // -------- Class HttpResponseStatusCode -----------------
231 
237  {
238  public:
242  enum Value : int
243  {
255  Http200OK = 200,
282  Http302 = 302,
318  Http410Gone = 410,
338  Http420 = 420,
371  Http451 = 451,
384  Http499 = 499,
385 
426 
427  // clang-format off
429  HttpStatus1xxCodeUnknown = 900001, // 1xx: Informational - Request received, continuing process
430  HttpStatus2xxCodeUnknown = 900002, // 2xx: Success - The action was successfully received, understood, and accepted
431  HttpStatus3xxCodeUnknown = 900003, // 3xx: Redirection - Further action must be taken in order to complete the request
432  HttpStatus4xxCodeUnknown = 900004, // 4xx: Client Error - The request contains bad syntax or cannot be fulfilled
433  HttpStatus5xxCodeUnknown = 900005, // 5xx: Server Error - The server failed to fulfill an apparently valid request
434  HttpStatusCodeUnknown = 999999, // other arbitrary number
435  // clang-format on
436  };
437 
438  HttpResponseStatusCode() = default;
439 
440  // cppcheck-suppress noExplicitConstructor
445  HttpResponseStatusCode(Value statusCode) : m_Value(statusCode)
446  {}
447 
453  explicit HttpResponseStatusCode(const int& statusCodeNumber, const std::string& statusMessage = "");
454 
460  explicit HttpResponseStatusCode(const Value& statusCode, const std::string& statusMessage);
461 
462  // Allow switch and comparisons.
463  operator Value() const
464  {
465  return m_Value;
466  }
467  // Prevent usage: if(httpResponseStatusCode)
468  explicit operator bool() const = delete;
469 
473  std::string toString() const
474  {
475  return std::to_string(m_Value);
476  }
477 
481  int toInt() const
482  {
483  return static_cast<int>(m_Value);
484  }
485 
489  std::string getMessage() const;
494  bool isUnsupportedCode() const
495  {
496  return m_Value > 599;
497  }
498 
499  private:
500  Value m_Value = HttpStatusCodeUnknown;
501  std::string m_CustomizedMessage;
502  };
503 
504  // -------- Class HttpResponseLayer -----------------
505 
519  {
520  friend class HttpResponseFirstLine;
521 
522  public:
523  // backward compatibility
525 
532  HttpResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
533 
544  PCPP_DEPRECATED("Use other constructors instead")
545  explicit HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode,
546  const std::string& statusCodeString);
547 
554  explicit HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode);
555 
556  ~HttpResponseLayer() override;
557 
564 
571  HttpResponseLayer& operator=(const HttpResponseLayer& other);
572 
577  {
578  return m_FirstLine;
579  }
580 
596  HeaderField* setContentLength(int contentLength, const std::string& prevFieldName = "");
597 
604  int getContentLength() const;
605 
606  // implement Layer's abstract methods
607 
608  std::string toString() const override;
609 
610  private:
611  HttpResponseFirstLine* m_FirstLine;
612  };
613 
614  // -------- Class HttpRequestFirstLine -----------------
615 
628  {
629  friend class HttpRequestLayer;
630 
631  public:
636  {
637  return m_Method;
638  }
639 
647 
652  std::string getUri() const;
653 
659  bool setUri(std::string newUri);
660 
665  {
666  return m_Version;
667  }
668 
674  void setVersion(HttpVersion newVersion);
675 
682  static HttpRequestLayer::HttpMethod parseMethod(const char* data, size_t dataLen);
683 
687  int getSize() const
688  {
689  return m_FirstLineEndOffset;
690  }
691 
698  bool isComplete() const
699  {
700  return m_IsComplete;
701  }
702 
709  class HttpRequestFirstLineException : public std::exception
710  {
711  public:
713  {}
714  void setMessage(const std::string& message)
715  {
716  m_Message = message;
717  }
718  virtual const char* what() const noexcept
719  {
720  return m_Message.c_str();
721  }
722 
723  private:
724  std::string m_Message;
725  };
726 
727  private:
730  const std::string& uri = "/");
731 
732  void parseVersion();
733 
734  HttpRequestLayer* m_HttpRequest;
736  HttpVersion m_Version;
737  int m_VersionOffset;
738  int m_UriOffset;
739  int m_FirstLineEndOffset;
740  bool m_IsComplete;
741  HttpRequestFirstLineException m_Exception;
742  };
743 
744  // -------- Class HttpResponseFirstLine -----------------
745 
758  {
759  friend class HttpResponseLayer;
760 
761  public:
766  {
767  return m_StatusCode;
768  }
769 
773  int getStatusCodeAsInt() const;
774 
778  std::string getStatusCodeString() const;
779 
789  PCPP_DEPRECATED("Use the other overload instead")
790  bool setStatusCode(const HttpResponseStatusCode& newStatusCode, const std::string& statusCodeString);
791 
797  bool setStatusCode(const HttpResponseStatusCode& newStatusCode);
798 
803  {
804  return m_Version;
805  }
806 
812  void setVersion(HttpVersion newVersion);
813 
820  static HttpResponseStatusCode parseStatusCode(const char* data, size_t dataLen);
821 
828  static HttpVersion parseVersion(const char* data, size_t dataLen);
829 
833  int getSize() const
834  {
835  return m_FirstLineEndOffset;
836  }
837 
844  bool isComplete() const
845  {
846  return m_IsComplete;
847  }
848 
856  class HttpResponseFirstLineException : public std::exception
857  {
858  public:
860  {}
861  void setMessage(const std::string& message)
862  {
863  m_Message = message;
864  }
865  virtual const char* what() const noexcept
866  {
867  return m_Message.c_str();
868  }
869 
870  private:
871  std::string m_Message;
872  };
873 
874  private:
876  HttpResponseFirstLine(HttpResponseLayer* httpResponse, HttpVersion version,
877  const HttpResponseStatusCode& statusCode);
878 
879  HttpResponseLayer* m_HttpResponse;
880  HttpVersion m_Version;
881  HttpResponseStatusCode m_StatusCode;
882  int m_FirstLineEndOffset;
883  bool m_IsComplete;
884  HttpResponseFirstLineException m_Exception;
885  };
886 
887 } // namespace pcpp
Definition: TextBasedProtocol.h:30
Definition: HttpLayer.h:74
OsiModelLayer getOsiModelLayer() const override
Definition: HttpLayer.h:96
HeaderField * insertField(HeaderField *prevField, const std::string &fieldName, const std::string &fieldValue) override
HeaderField * addField(const HeaderField &newField) override
static bool isHttpPort(uint16_t port)
Definition: HttpLayer.h:83
HeaderField * insertField(HeaderField *prevField, const HeaderField &newField) override
HeaderField * addField(const std::string &fieldName, const std::string &fieldValue) override
Definition: HttpLayer.h:628
bool setMethod(HttpRequestLayer::HttpMethod newMethod)
int getSize() const
Definition: HttpLayer.h:687
HttpRequestLayer::HttpMethod getMethod() const
Definition: HttpLayer.h:635
bool isComplete() const
Definition: HttpLayer.h:698
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:664
Definition: HttpLayer.h:141
HttpRequestLayer & operator=(const HttpRequestLayer &other)
HttpMethod
Definition: HttpLayer.h:149
@ HttpDELETE
Definition: HttpLayer.h:159
@ HttpCONNECT
Definition: HttpLayer.h:165
@ HttpPOST
Definition: HttpLayer.h:155
@ HttpMethodUnknown
Definition: HttpLayer.h:169
@ HttpTRACE
Definition: HttpLayer.h:161
@ HttpOPTIONS
Definition: HttpLayer.h:163
@ HttpHEAD
Definition: HttpLayer.h:153
@ HttpPATCH
Definition: HttpLayer.h:167
@ HttpGET
Definition: HttpLayer.h:151
@ HttpPUT
Definition: HttpLayer.h:157
HttpRequestLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
HttpRequestLayer(const HttpRequestLayer &other)
std::string toString() const override
HttpRequestLayer(HttpMethod method, const std::string &uri, HttpVersion version)
std::string getUrl() const
HttpRequestFirstLine * getFirstLine() const
Definition: HttpLayer.h:209
Definition: HttpLayer.h:758
bool isComplete() const
Definition: HttpLayer.h:844
HttpVersion getVersion() const
Definition: HttpLayer.h:802
static HttpVersion parseVersion(const char *data, size_t dataLen)
HttpResponseStatusCode getStatusCode() const
Definition: HttpLayer.h:765
std::string getStatusCodeString() const
bool setStatusCode(const HttpResponseStatusCode &newStatusCode, const std::string &statusCodeString)
void setVersion(HttpVersion newVersion)
int getSize() const
Definition: HttpLayer.h:833
static HttpResponseStatusCode parseStatusCode(const char *data, size_t dataLen)
Definition: HttpLayer.h:519
std::string toString() const override
HttpResponseLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
HttpResponseFirstLine * getFirstLine() const
Definition: HttpLayer.h:576
HeaderField * setContentLength(int contentLength, const std::string &prevFieldName="")
int getContentLength() const
The enum wrapper class of HTTP response status codes.
Definition: HttpLayer.h:237
int toInt() const
get status code number as int
Definition: HttpLayer.h:481
bool isUnsupportedCode() const
Definition: HttpLayer.h:494
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:473
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:243
@ Http507InsufficientStorage
Definition: HttpLayer.h:401
@ Http301MovedPermanently
Definition: HttpLayer.h:280
@ Http306SwitchProxy
Definition: HttpLayer.h:290
@ Http499
Definition: HttpLayer.h:384
@ Http599NetworkConnectTimeoutError
Definition: HttpLayer.h:425
@ Http410Gone
Definition: HttpLayer.h:318
@ Http100Continue
Definition: HttpLayer.h:245
@ Http502BadGateway
Definition: HttpLayer.h:391
@ Http411LengthRequired
Definition: HttpLayer.h:320
@ Http407ProxyAuthenticationRequired
Definition: HttpLayer.h:312
@ Http308PermanentRedirect
Definition: HttpLayer.h:294
@ Http205ResetContent
Definition: HttpLayer.h:265
@ Http300MultipleChoices
Definition: HttpLayer.h:278
@ Http402PaymentRequired
Definition: HttpLayer.h:302
@ Http207MultiStatus
Definition: HttpLayer.h:269
@ Http420
Definition: HttpLayer.h:338
@ Http400BadRequest
Definition: HttpLayer.h:298
@ Http509BandwidthLimitExceeded
Definition: HttpLayer.h:405
@ Http426UpgradeRequired
Definition: HttpLayer.h:350
@ Http494RequestHeaderTooLarge
Definition: HttpLayer.h:374
@ Http511NetworkAuthenticationRequired
Definition: HttpLayer.h:409
@ Http598NetworkReadTimeoutError
Definition: HttpLayer.h:423
@ Http501NotImplemented
Definition: HttpLayer.h:389
@ Http523ProxyDeclinedRequest
Definition: HttpLayer.h:418
@ Http421MisdirectedRequest
Definition: HttpLayer.h:340
@ Http404NotFound
Definition: HttpLayer.h:306
@ Http418ImATeapot
Definition: HttpLayer.h:334
@ Http305UseProxy
Definition: HttpLayer.h:288
@ Http226IMUsed
Definition: HttpLayer.h:274
@ Http498TokenExpiredInvalid
Definition: HttpLayer.h:382
@ Http416RequestedRangeNotSatisfiable
Definition: HttpLayer.h:330
@ Http524aTimeoutOccurred
Definition: HttpLayer.h:420
@ Http444NoResponse
Definition: HttpLayer.h:364
@ Http414RequestURITooLong
Definition: HttpLayer.h:326
@ Http425TooEarly
Definition: HttpLayer.h:348
@ Http401Unauthorized
Definition: HttpLayer.h:300
@ Http429TooManyRequests
Definition: HttpLayer.h:355
@ Http450BlockedByWindowsParentalControls
Definition: HttpLayer.h:369
@ Http204NoContent
Definition: HttpLayer.h:263
@ Http508LoopDetected
Definition: HttpLayer.h:403
@ Http510NotExtended
Definition: HttpLayer.h:407
@ Http403Forbidden
Definition: HttpLayer.h:304
@ Http408RequestTimeout
Definition: HttpLayer.h:314
@ Http440LoginTimeout
Definition: HttpLayer.h:361
@ Http500InternalServerError
Definition: HttpLayer.h:387
@ Http505HTTPVersionNotSupported
Definition: HttpLayer.h:397
@ Http206PartialContent
Definition: HttpLayer.h:267
@ Http520OriginError
Definition: HttpLayer.h:412
@ Http302
Definition: HttpLayer.h:282
@ Http409Conflict
Definition: HttpLayer.h:316
@ Http419AuthenticationTimeout
Definition: HttpLayer.h:336
@ Http406NotAcceptable
Definition: HttpLayer.h:310
@ Http451
Definition: HttpLayer.h:371
@ Http496NoCert
Definition: HttpLayer.h:378
@ Http103EarlyHints
Definition: HttpLayer.h:251
@ Http202Accepted
Definition: HttpLayer.h:259
@ Http522ConnectionTimedOut
Definition: HttpLayer.h:416
@ Http203NonAuthoritativeInformation
Definition: HttpLayer.h:261
@ Http304NotModified
Definition: HttpLayer.h:286
@ Http201Created
Definition: HttpLayer.h:257
@ Http101SwitchingProtocols
Definition: HttpLayer.h:247
@ Http413RequestEntityTooLarge
Definition: HttpLayer.h:324
@ Http495CertError
Definition: HttpLayer.h:376
@ Http497HTTPtoHTTPS
Definition: HttpLayer.h:380
@ Http200OK
Definition: HttpLayer.h:255
@ Http431RequestHeaderFieldsTooLarge
Definition: HttpLayer.h:358
@ Http405MethodNotAllowed
Definition: HttpLayer.h:308
@ Http449RetryWith
Definition: HttpLayer.h:367
@ Http506VariantAlsoNegotiates
Definition: HttpLayer.h:399
@ Http503ServiceUnavailable
Definition: HttpLayer.h:393
@ Http102Processing
Definition: HttpLayer.h:249
@ HttpStatus1xxCodeUnknown
Definition: HttpLayer.h:429
@ Http521WebServerIsDown
Definition: HttpLayer.h:414
@ Http415UnsupportedMediaType
Definition: HttpLayer.h:328
@ Http504GatewayTimeout
Definition: HttpLayer.h:395
@ Http412PreconditionFailed
Definition: HttpLayer.h:322
@ Http417ExpectationFailed
Definition: HttpLayer.h:332
@ Http428PreconditionRequired
Definition: HttpLayer.h:353
@ Http303SeeOther
Definition: HttpLayer.h:284
@ Http307TemporaryRedirect
Definition: HttpLayer.h:292
@ Http424FailedDependency
Definition: HttpLayer.h:346
@ Http422UnprocessableEntity
Definition: HttpLayer.h:342
@ Http423Locked
Definition: HttpLayer.h:344
@ Http208AlreadyReported
Definition: HttpLayer.h:271
HttpResponseStatusCode(Value statusCode)
Construct HttpResponseStatusCode from Value enum.
Definition: HttpLayer.h:445
Definition: Layer.h:69
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:364
@ OsiModelApplicationLayer
Definition: ProtocolType.h:378
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