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 
12 namespace pcpp
13 {
16  {
25  };
26 
27  // some popular HTTP fields
28 
30 #define PCPP_HTTP_HOST_FIELD "Host"
32 #define PCPP_HTTP_CONNECTION_FIELD "Connection"
34 #define PCPP_HTTP_USER_AGENT_FIELD "User-Agent"
36 #define PCPP_HTTP_REFERER_FIELD "Referer"
38 #define PCPP_HTTP_ACCEPT_FIELD "Accept"
40 #define PCPP_HTTP_ACCEPT_ENCODING_FIELD "Accept-Encoding"
42 #define PCPP_HTTP_ACCEPT_LANGUAGE_FIELD "Accept-Language"
44 #define PCPP_HTTP_COOKIE_FIELD "Cookie"
46 #define PCPP_HTTP_CONTENT_LENGTH_FIELD "Content-Length"
48 #define PCPP_HTTP_CONTENT_ENCODING_FIELD "Content-Encoding"
50 #define PCPP_HTTP_CONTENT_TYPE_FIELD "Content-Type"
52 #define PCPP_HTTP_TRANSFER_ENCODING_FIELD "Transfer-Encoding"
54 #define PCPP_HTTP_SERVER_FIELD "Server"
55 
56  // -------- classes to be defined later -----------------
57 
58  class HttpRequestFirstLine;
59  class HttpResponseFirstLine;
60 
61  // -------- Class HttpMessage -----------------
62 
67  {
68  public:
69  ~HttpMessage() override = default;
70 
74  static bool isHttpPort(uint16_t port)
75  {
76  return port == 80 || port == 8080;
77  }
78 
79  // overridden methods
80 
81  HeaderField* addField(const std::string& fieldName, const std::string& fieldValue) override;
82  HeaderField* addField(const HeaderField& newField) override;
83  HeaderField* insertField(HeaderField* prevField, const std::string& fieldName,
84  const std::string& fieldValue) override;
85  HeaderField* insertField(HeaderField* prevField, const HeaderField& newField) override;
86 
88  {
90  }
91 
92  protected:
93  HttpMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType protocol)
94  : TextBasedProtocolMessage(data, dataLen, prevLayer, packet, protocol)
95  {}
96  HttpMessage() : TextBasedProtocolMessage()
97  {}
98  HttpMessage(const HttpMessage& other) : TextBasedProtocolMessage(other)
99  {}
100  HttpMessage& operator=(const HttpMessage& other)
101  {
102  TextBasedProtocolMessage::operator=(other);
103  return *this;
104  }
105 
106  // implementation of abstract methods
107  char getHeaderFieldNameValueSeparator() const override
108  {
109  return ':';
110  }
111  bool spacesAllowedBetweenHeaderFieldNameAndValue() const override
112  {
113  return true;
114  }
115  };
116 
117  // -------- Class HttpRequestLayer -----------------
118 
131  {
132  friend class HttpRequestFirstLine;
133 
134  public:
137  {
158  };
159 
165  HttpRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
166 
172  HttpRequestLayer(HttpMethod method, const std::string& uri, HttpVersion version);
173 
174  ~HttpRequestLayer() override;
175 
180 
186 
189  {
190  return m_FirstLine;
191  }
192 
198  std::string getUrl() const;
199 
200  // implement Layer's abstract methods
201  std::string toString() const override;
202 
203  private:
204  HttpRequestFirstLine* m_FirstLine;
205  };
206 
207  // -------- Class HttpResponseStatusCode -----------------
208 
212  {
213  public:
215  enum Value : int
216  {
226 
228  Http200OK = 200,
249 
255  Http302 = 302,
269 
291  Http410Gone = 410,
311  Http420 = 420,
344  Http451 = 451,
357  Http499 = 499,
358 
399 
400  // clang-format off
402  HttpStatus1xxCodeUnknown = 900001, // 1xx: Informational - Request received, continuing process
403  HttpStatus2xxCodeUnknown = 900002, // 2xx: Success - The action was successfully received, understood, and accepted
404  HttpStatus3xxCodeUnknown = 900003, // 3xx: Redirection - Further action must be taken in order to complete the request
405  HttpStatus4xxCodeUnknown = 900004, // 4xx: Client Error - The request contains bad syntax or cannot be fulfilled
406  HttpStatus5xxCodeUnknown = 900005, // 5xx: Server Error - The server failed to fulfill an apparently valid request
407  HttpStatusCodeUnknown = 999999, // other arbitrary number
408  // clang-format on
409  };
410 
411  HttpResponseStatusCode() = default;
412 
413  // cppcheck-suppress noExplicitConstructor
416  HttpResponseStatusCode(Value statusCode) : m_Value(statusCode)
417  {}
418 
422  explicit HttpResponseStatusCode(const int& statusCodeNumber, const std::string& statusMessage = "");
423 
427  explicit HttpResponseStatusCode(const Value& statusCode, const std::string& statusMessage);
428 
429  // Allow switch and comparisons.
430  operator Value() const
431  {
432  return m_Value;
433  }
434  // Prevent usage: if(httpResponseStatusCode)
435  explicit operator bool() const = delete;
436 
438  std::string toString() const
439  {
440  return std::to_string(m_Value);
441  }
442 
444  int toInt() const
445  {
446  return static_cast<int>(m_Value);
447  }
448 
450  std::string getMessage() const;
453  bool isUnsupportedCode() const
454  {
455  return m_Value > 599;
456  }
457 
458  private:
459  Value m_Value = HttpStatusCodeUnknown;
460  std::string m_CustomizedMessage;
461  };
462 
463  // -------- Class HttpResponseLayer -----------------
464 
476  {
477  friend class HttpResponseFirstLine;
478 
479  public:
480  // backward compatibility
482 
488  HttpResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
489 
498  PCPP_DEPRECATED("Use other constructors instead")
499  explicit HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode,
500  const std::string& statusCodeString);
501 
506  explicit HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode);
507 
508  ~HttpResponseLayer() override;
509 
514 
519  HttpResponseLayer& operator=(const HttpResponseLayer& other);
520 
523  {
524  return m_FirstLine;
525  }
526 
541  HeaderField* setContentLength(int contentLength, const std::string& prevFieldName = "");
542 
547  int getContentLength() const;
548 
549  // implement Layer's abstract methods
550 
551  std::string toString() const override;
552 
553  private:
554  HttpResponseFirstLine* m_FirstLine;
555  };
556 
557  // -------- Class HttpRequestFirstLine -----------------
558 
569  {
570  friend class HttpRequestLayer;
571 
572  public:
575  {
576  return m_Method;
577  }
578 
584 
587  std::string getUri() const;
588 
592  bool setUri(std::string newUri);
593 
596  {
597  return m_Version;
598  }
599 
603  void setVersion(HttpVersion newVersion);
604 
609  static HttpRequestLayer::HttpMethod parseMethod(const char* data, size_t dataLen);
610 
612  int getSize() const
613  {
614  return m_FirstLineEndOffset;
615  }
616 
621  bool isComplete() const
622  {
623  return m_IsComplete;
624  }
625 
630  class HttpRequestFirstLineException : public std::exception
631  {
632  public:
634  {}
635  void setMessage(const std::string& message)
636  {
637  m_Message = message;
638  }
639  virtual const char* what() const noexcept
640  {
641  return m_Message.c_str();
642  }
643 
644  private:
645  std::string m_Message;
646  };
647 
648  private:
651  const std::string& uri = "/");
652 
653  void parseVersion();
654 
655  HttpRequestLayer* m_HttpRequest;
657  HttpVersion m_Version;
658  int m_VersionOffset;
659  int m_UriOffset;
660  int m_FirstLineEndOffset;
661  bool m_IsComplete;
662  HttpRequestFirstLineException m_Exception;
663  };
664 
665  // -------- Class HttpResponseFirstLine -----------------
666 
677  {
678  friend class HttpResponseLayer;
679 
680  public:
683  {
684  return m_StatusCode;
685  }
686 
688  int getStatusCodeAsInt() const;
689 
691  std::string getStatusCodeString() const;
692 
700  PCPP_DEPRECATED("Use the other overload instead")
701  bool setStatusCode(const HttpResponseStatusCode& newStatusCode, const std::string& statusCodeString);
702 
706  bool setStatusCode(const HttpResponseStatusCode& newStatusCode);
707 
710  {
711  return m_Version;
712  }
713 
717  void setVersion(HttpVersion newVersion);
718 
723  static HttpResponseStatusCode parseStatusCode(const char* data, size_t dataLen);
724 
729  static HttpVersion parseVersion(const char* data, size_t dataLen);
730 
732  int getSize() const
733  {
734  return m_FirstLineEndOffset;
735  }
736 
741  bool isComplete() const
742  {
743  return m_IsComplete;
744  }
745 
751  class HttpResponseFirstLineException : public std::exception
752  {
753  public:
755  {}
756  void setMessage(const std::string& message)
757  {
758  m_Message = message;
759  }
760  virtual const char* what() const noexcept
761  {
762  return m_Message.c_str();
763  }
764 
765  private:
766  std::string m_Message;
767  };
768 
769  private:
771  HttpResponseFirstLine(HttpResponseLayer* httpResponse, HttpVersion version,
772  const HttpResponseStatusCode& statusCode);
773 
774  HttpResponseLayer* m_HttpResponse;
775  HttpVersion m_Version;
776  HttpResponseStatusCode m_StatusCode;
777  int m_FirstLineEndOffset;
778  bool m_IsComplete;
779  HttpResponseFirstLineException m_Exception;
780  };
781 
782 } // namespace pcpp
Definition: TextBasedProtocol.h:28
Definition: HttpLayer.h:67
OsiModelLayer getOsiModelLayer() const override
Definition: HttpLayer.h:87
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:74
HeaderField * insertField(HeaderField *prevField, const HeaderField &newField) override
HeaderField * addField(const std::string &fieldName, const std::string &fieldValue) override
Definition: HttpLayer.h:569
bool setMethod(HttpRequestLayer::HttpMethod newMethod)
int getSize() const
Definition: HttpLayer.h:612
HttpRequestLayer::HttpMethod getMethod() const
Definition: HttpLayer.h:574
bool isComplete() const
Definition: HttpLayer.h:621
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:595
Definition: HttpLayer.h:131
HttpRequestLayer & operator=(const HttpRequestLayer &other)
HttpMethod
HTTP request methods.
Definition: HttpLayer.h:137
@ HttpDELETE
DELETE.
Definition: HttpLayer.h:147
@ HttpCONNECT
CONNECT.
Definition: HttpLayer.h:153
@ HttpPOST
POST.
Definition: HttpLayer.h:143
@ HttpMethodUnknown
Unknown HTTP method.
Definition: HttpLayer.h:157
@ HttpTRACE
TRACE.
Definition: HttpLayer.h:149
@ HttpOPTIONS
OPTIONS.
Definition: HttpLayer.h:151
@ HttpHEAD
HEAD.
Definition: HttpLayer.h:141
@ HttpPATCH
PATCH.
Definition: HttpLayer.h:155
@ HttpGET
GET.
Definition: HttpLayer.h:139
@ HttpPUT
PUT.
Definition: HttpLayer.h:145
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:188
Definition: HttpLayer.h:677
bool isComplete() const
Definition: HttpLayer.h:741
HttpVersion getVersion() const
Definition: HttpLayer.h:709
static HttpVersion parseVersion(const char *data, size_t dataLen)
HttpResponseStatusCode getStatusCode() const
Definition: HttpLayer.h:682
std::string getStatusCodeString() const
bool setStatusCode(const HttpResponseStatusCode &newStatusCode, const std::string &statusCodeString)
void setVersion(HttpVersion newVersion)
int getSize() const
Definition: HttpLayer.h:732
static HttpResponseStatusCode parseStatusCode(const char *data, size_t dataLen)
Definition: HttpLayer.h:476
std::string toString() const override
HttpResponseLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
HttpResponseFirstLine * getFirstLine() const
Definition: HttpLayer.h:522
HeaderField * setContentLength(int contentLength, const std::string &prevFieldName="")
int getContentLength() const
The enum wrapper class of HTTP response status codes.
Definition: HttpLayer.h:212
int toInt() const
get status code number as int
Definition: HttpLayer.h:444
bool isUnsupportedCode() const
Definition: HttpLayer.h:453
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:438
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:216
@ Http507InsufficientStorage
507 Insufficient Storage
Definition: HttpLayer.h:374
@ Http301MovedPermanently
301 Moved Permanently
Definition: HttpLayer.h:253
@ Http306SwitchProxy
306 Switch Proxy
Definition: HttpLayer.h:263
@ Http499
499 (various messages)
Definition: HttpLayer.h:357
@ Http599NetworkConnectTimeoutError
599 Network connect timeout error
Definition: HttpLayer.h:398
@ Http410Gone
410 Gone
Definition: HttpLayer.h:291
@ Http100Continue
100 Continue
Definition: HttpLayer.h:218
@ Http502BadGateway
502 Bad Gateway
Definition: HttpLayer.h:364
@ Http411LengthRequired
411 Length Required
Definition: HttpLayer.h:293
@ Http407ProxyAuthenticationRequired
407 Proxy Authentication Required
Definition: HttpLayer.h:285
@ Http308PermanentRedirect
308 Permanent Redirect,
Definition: HttpLayer.h:267
@ Http205ResetContent
205 Reset Content
Definition: HttpLayer.h:238
@ Http300MultipleChoices
227-299 Unassigned
Definition: HttpLayer.h:251
@ Http402PaymentRequired
402 Payment Required
Definition: HttpLayer.h:275
@ Http207MultiStatus
207 Multi-Status
Definition: HttpLayer.h:242
@ Http420
420 (various messages)
Definition: HttpLayer.h:311
@ Http400BadRequest
309-399 Unassigned
Definition: HttpLayer.h:271
@ Http509BandwidthLimitExceeded
509 Bandwidth Limit Exceeded
Definition: HttpLayer.h:378
@ Http426UpgradeRequired
426 Upgrade Required
Definition: HttpLayer.h:323
@ Http494RequestHeaderTooLarge
Definition: HttpLayer.h:347
@ Http511NetworkAuthenticationRequired
511 Network Authentication Required
Definition: HttpLayer.h:382
@ Http598NetworkReadTimeoutError
Definition: HttpLayer.h:396
@ Http501NotImplemented
501 Not Implemented
Definition: HttpLayer.h:362
@ Http523ProxyDeclinedRequest
523 Proxy Declined Request
Definition: HttpLayer.h:391
@ Http421MisdirectedRequest
421 Misdirected Request
Definition: HttpLayer.h:313
@ Http404NotFound
404 Not Found
Definition: HttpLayer.h:279
@ Http418ImATeapot
418 I'm a teapot
Definition: HttpLayer.h:307
@ Http305UseProxy
305 Use Proxy
Definition: HttpLayer.h:261
@ Http226IMUsed
Definition: HttpLayer.h:247
@ Http498TokenExpiredInvalid
498 Token expired/invalid
Definition: HttpLayer.h:355
@ Http416RequestedRangeNotSatisfiable
416 Requested Range Not Satisfiable
Definition: HttpLayer.h:303
@ Http524aTimeoutOccurred
524 A timeout occurred
Definition: HttpLayer.h:393
@ Http444NoResponse
Definition: HttpLayer.h:337
@ Http414RequestURITooLong
414 Request-URI Too Long
Definition: HttpLayer.h:299
@ Http425TooEarly
425 Too Early
Definition: HttpLayer.h:321
@ Http401Unauthorized
401 Unauthorized
Definition: HttpLayer.h:273
@ Http429TooManyRequests
429 Too Many Requests
Definition: HttpLayer.h:328
@ Http450BlockedByWindowsParentalControls
450 Blocked by Windows Parental Controls
Definition: HttpLayer.h:342
@ Http204NoContent
204 No Content
Definition: HttpLayer.h:236
@ Http508LoopDetected
508 Loop Detected
Definition: HttpLayer.h:376
@ Http510NotExtended
510 Not Extended
Definition: HttpLayer.h:380
@ Http403Forbidden
403 Forbidden
Definition: HttpLayer.h:277
@ Http408RequestTimeout
408 Request Timeout
Definition: HttpLayer.h:287
@ Http440LoginTimeout
Definition: HttpLayer.h:334
@ Http500InternalServerError
500 Internal Server Error
Definition: HttpLayer.h:360
@ Http505HTTPVersionNotSupported
505 HTTP Version Not Supported
Definition: HttpLayer.h:370
@ Http206PartialContent
206 Partial Content
Definition: HttpLayer.h:240
@ Http520OriginError
Definition: HttpLayer.h:385
@ Http302
302 (various messages)
Definition: HttpLayer.h:255
@ Http409Conflict
409 Conflict
Definition: HttpLayer.h:289
@ Http419AuthenticationTimeout
419 Authentication Timeout
Definition: HttpLayer.h:309
@ Http406NotAcceptable
406 Not Acceptable
Definition: HttpLayer.h:283
@ Http451
451 (various messages)
Definition: HttpLayer.h:344
@ Http496NoCert
496 No Cert
Definition: HttpLayer.h:351
@ Http103EarlyHints
103 Early Hints
Definition: HttpLayer.h:224
@ Http202Accepted
202 Accepted
Definition: HttpLayer.h:232
@ Http522ConnectionTimedOut
522 Connection timed out
Definition: HttpLayer.h:389
@ Http203NonAuthoritativeInformation
203 Non-Authoritative Information
Definition: HttpLayer.h:234
@ Http304NotModified
304 Not Modified
Definition: HttpLayer.h:259
@ Http201Created
201 Created
Definition: HttpLayer.h:230
@ Http101SwitchingProtocols
101 Switching Protocols
Definition: HttpLayer.h:220
@ Http413RequestEntityTooLarge
413 RequestEntity Too Large
Definition: HttpLayer.h:297
@ Http495CertError
495 Cert Error
Definition: HttpLayer.h:349
@ Http497HTTPtoHTTPS
497 HTTP to HTTPS
Definition: HttpLayer.h:353
@ Http200OK
104-199 Unassigned
Definition: HttpLayer.h:228
@ Http431RequestHeaderFieldsTooLarge
Definition: HttpLayer.h:331
@ Http405MethodNotAllowed
405 Method Not Allowed
Definition: HttpLayer.h:281
@ Http449RetryWith
Definition: HttpLayer.h:340
@ Http506VariantAlsoNegotiates
506 Variant Also Negotiates
Definition: HttpLayer.h:372
@ Http503ServiceUnavailable
503 Service Unavailable
Definition: HttpLayer.h:366
@ Http102Processing
102 Processing
Definition: HttpLayer.h:222
@ HttpStatus1xxCodeUnknown
Unknown status code.
Definition: HttpLayer.h:402
@ Http521WebServerIsDown
521 Web server is down
Definition: HttpLayer.h:387
@ Http415UnsupportedMediaType
415 Unsupported Media Type
Definition: HttpLayer.h:301
@ Http504GatewayTimeout
504 Gateway Timeout
Definition: HttpLayer.h:368
@ Http412PreconditionFailed
412 Precondition Failed
Definition: HttpLayer.h:295
@ Http417ExpectationFailed
417 Expectation Failed
Definition: HttpLayer.h:305
@ Http428PreconditionRequired
Definition: HttpLayer.h:326
@ Http303SeeOther
303 See Other
Definition: HttpLayer.h:257
@ Http307TemporaryRedirect
307 Temporary Redirect
Definition: HttpLayer.h:265
@ Http424FailedDependency
424 Failed Dependency
Definition: HttpLayer.h:319
@ Http422UnprocessableEntity
422 Unprocessable Entity
Definition: HttpLayer.h:315
@ Http423Locked
423 Locked
Definition: HttpLayer.h:317
@ Http208AlreadyReported
208 Already Reported
Definition: HttpLayer.h:244
HttpResponseStatusCode(Value statusCode)
Construct HttpResponseStatusCode from Value enum.
Definition: HttpLayer.h:416
Definition: Layer.h:60
Definition: Packet.h:22
Definition: TextBasedProtocol.h:105
The main namespace for the PcapPlusPlus lib.
uint8_t ProtocolType
Definition: ProtocolType.h:13
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:225
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:239
HttpVersion
An enum for HTTP version.
Definition: HttpLayer.h:16
@ HttpVersionUnknown
Unknown HTTP version.
Definition: HttpLayer.h:24
@ ZeroDotNine
HTTP/0.9.
Definition: HttpLayer.h:18
@ OneDotZero
HTTP/1.0.
Definition: HttpLayer.h:20
@ OneDotOne
HTTP/1.1.
Definition: HttpLayer.h:22