PcapPlusPlus  19.12
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() const { return ':'; }
99  bool spacesAllowedBetweenHeaderFieldNameAndValue() const { 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  HttpRequestFirstLine* getFirstLine() const { return m_FirstLine; }
192 
200  std::string getUrl() const;
201 
202  // implement Layer's abstract methods
203  std::string toString() const;
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  HttpResponseFirstLine* getFirstLine() const { return m_FirstLine; }
443 
456  HeaderField* setContentLength(int contentLength, const std::string prevFieldName = "");
457 
463  int getContentLength() const;
464 
465  // implement Layer's abstract methods
466 
467  std::string toString() const;
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  HttpRequestLayer::HttpMethod getMethod() const { return m_Method; }
499 
505  bool setMethod(HttpRequestLayer::HttpMethod newMethod);
506 
510  std::string getUri() const;
511 
517  bool setUri(std::string newUri);
518 
522  HttpVersion getVersion() const { return m_Version; }
523 
529  void setVersion(HttpVersion newVersion);
530 
537  static HttpRequestLayer::HttpMethod parseMethod(char* data, size_t dataLen);
538 
542  int getSize() const { return m_FirstLineEndOffset; }
543 
550  bool isComplete() const { 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() const;
616 
620  std::string getStatusCodeString() const;
621 
628  bool setStatusCode(HttpResponseLayer::HttpResponseStatusCode newStatusCode, std::string statusCodeString = "");
629 
633  HttpVersion getVersion() const { return m_Version; }
634 
640  void setVersion(HttpVersion newVersion);
641 
648  static HttpResponseLayer::HttpResponseStatusCode parseStatusCode(char* data, size_t dataLen);
649 
653  int getSize() const { return m_FirstLineEndOffset; }
654 
661  bool isComplete() const { 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 */
pcpp::OsiModelLayer
OsiModelLayer
Definition: ProtocolType.h:224
pcpp::HttpResponseLayer::Http417ExpectationFailed
Definition: HttpLayer.h:318
pcpp::HttpResponseLayer::Http200OK
Definition: HttpLayer.h:246
pcpp::HttpResponseLayer::Http201Created
Definition: HttpLayer.h:248
pcpp::Packet
Definition: Packet.h:26
pcpp::HttpResponseLayer::Http306SwitchProxy
Definition: HttpLayer.h:278
pcpp::HttpResponseLayer::Http300MultipleChoices
Definition: HttpLayer.h:266
pcpp::HttpResponseLayer::Http521WebServerIsDown
Definition: HttpLayer.h:388
pcpp::HttpResponseLayer::Http412PreconditionFailed
Definition: HttpLayer.h:308
pcpp::OsiModelApplicationLayer
Definition: ProtocolType.h:239
pcpp::HttpRequestFirstLine
Definition: HttpLayer.h:491
pcpp::HttpResponseLayer::Http402PaymentRequired
Definition: HttpLayer.h:288
pcpp::HttpResponseLayer::Http423Locked
Definition: HttpLayer.h:328
pcpp::HttpResponseLayer::Http416RequestedRangeNotSatisfiable
Definition: HttpLayer.h:316
pcpp::HttpResponseLayer::Http405MethodNotAllowed
Definition: HttpLayer.h:294
pcpp::HttpRequestLayer::HttpGET
Definition: HttpLayer.h:134
pcpp::HttpRequestFirstLine::getVersion
HttpVersion getVersion() const
Definition: HttpLayer.h:522
pcpp::HttpResponseLayer::Http506VariantAlsoNegotiates
Definition: HttpLayer.h:374
pcpp::HttpResponseLayer::Http499
Definition: HttpLayer.h:360
pcpp::HttpRequestFirstLine::parseMethod
static HttpRequestLayer::HttpMethod parseMethod(char *data, size_t dataLen)
pcpp::HttpResponseLayer::Http418Imateapot
Definition: HttpLayer.h:320
pcpp::HttpResponseLayer::Http202Accepted
Definition: HttpLayer.h:250
pcpp::HttpRequestLayer::HttpRequestLayer
HttpRequestLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
pcpp::HttpResponseLayer::HttpResponseLayer
HttpResponseLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
pcpp::HttpResponseLayer::Http522ConnectionTimedOut
Definition: HttpLayer.h:390
pcpp::HttpResponseLayer::Http203NonAuthoritativeInformation
Definition: HttpLayer.h:252
pcpp::HttpRequestFirstLine::isComplete
bool isComplete() const
Definition: HttpLayer.h:550
pcpp::HttpRequestLayer::getUrl
std::string getUrl() const
pcpp::HttpResponseLayer::Http431RequestHeaderFieldsTooLarge
Definition: HttpLayer.h:338
pcpp::HttpResponseLayer::Http414RequestURITooLong
Definition: HttpLayer.h:312
pcpp::HttpResponseLayer
Definition: HttpLayer.h:230
pcpp::OneDotZero
Definition: HttpLayer.h:26
pcpp::HttpResponseLayer::Http419AuthenticationTimeout
Definition: HttpLayer.h:322
pcpp::HttpRequestLayer::HttpMethod
HttpMethod
Definition: HttpLayer.h:131
pcpp::HttpResponseLayer::Http403Forbidden
Definition: HttpLayer.h:290
pcpp::HttpRequestLayer::operator=
HttpRequestLayer & operator=(const HttpRequestLayer &other)
pcpp::HttpResponseLayer::Http451
Definition: HttpLayer.h:348
pcpp::HttpResponseLayer::Http206PartialContent
Definition: HttpLayer.h:258
pcpp::HttpResponseLayer::Http100Continue
Definition: HttpLayer.h:240
pcpp::HttpResponseLayer::Http444NoResponse
Definition: HttpLayer.h:342
pcpp::HttpResponseLayer::http205ResetContent
Definition: HttpLayer.h:256
pcpp::HttpResponseLayer::Http401Unauthorized
Definition: HttpLayer.h:286
pcpp::HttpRequestFirstLine::setMethod
bool setMethod(HttpRequestLayer::HttpMethod newMethod)
pcpp::HttpResponseFirstLine::getStatusCodeString
std::string getStatusCodeString() const
pcpp::HttpRequestLayer::HttpHEAD
Definition: HttpLayer.h:136
pcpp::HttpResponseLayer::Http406NotAcceptable
Definition: HttpLayer.h:296
pcpp::HttpResponseFirstLine::getStatusCode
HttpResponseLayer::HttpResponseStatusCode getStatusCode() const
Definition: HttpLayer.h:610
pcpp::HttpResponseLayer::Http307TemporaryRedirect
Definition: HttpLayer.h:280
pcpp::HttpResponseLayer::Http498TokenExpiredInvalid
Definition: HttpLayer.h:358
pcpp::HttpResponseLayer::Http505HTTPVersionNotSupported
Definition: HttpLayer.h:372
pcpp::HttpRequestLayer::HttpPOST
Definition: HttpLayer.h:138
pcpp::HttpMessage::addField
virtual HeaderField * addField(const std::string &fieldName, const std::string &fieldValue)
pcpp::HttpRequestFirstLine::setVersion
void setVersion(HttpVersion newVersion)
pcpp::HttpResponseLayer::Http409Conflict
Definition: HttpLayer.h:302
pcpp::HttpResponseLayer::setContentLength
HeaderField * setContentLength(int contentLength, const std::string prevFieldName="")
pcpp::HttpRequestLayer::getFirstLine
HttpRequestFirstLine * getFirstLine() const
Definition: HttpLayer.h:191
pcpp::HttpResponseLayer::Http415UnsupportedMediaType
Definition: HttpLayer.h:314
pcpp::HttpResponseLayer::Http428PreconditionRequired
Definition: HttpLayer.h:334
pcpp::HttpVersionUnknown
Definition: HttpLayer.h:30
pcpp::HttpResponseLayer::Http503ServiceUnavailable
Definition: HttpLayer.h:368
pcpp::HttpMessage::getOsiModelLayer
OsiModelLayer getOsiModelLayer() const
Definition: HttpLayer.h:89
pcpp::HttpRequestLayer::HttpMethodUnknown
Definition: HttpLayer.h:152
pcpp::HttpRequestFirstLine::setUri
bool setUri(std::string newUri)
pcpp::HttpRequestFirstLine::getMethod
HttpRequestLayer::HttpMethod getMethod() const
Definition: HttpLayer.h:498
pcpp::HttpResponseLayer::Http420
Definition: HttpLayer.h:324
pcpp::HttpResponseLayer::Http301MovedPermanently
Definition: HttpLayer.h:268
pcpp::HttpMessage
Definition: HttpLayer.h:71
pcpp::HttpResponseLayer::Http226IMUsed
Definition: HttpLayer.h:264
pcpp::HttpResponseLayer::Http511NetworkAuthenticationRequired
Definition: HttpLayer.h:384
pcpp::HttpResponseLayer::Http495CertError
Definition: HttpLayer.h:352
pcpp::HttpResponseLayer::Http408RequestTimeout
Definition: HttpLayer.h:300
pcpp::HttpRequestLayer::HttpPATCH
Definition: HttpLayer.h:150
pcpp::HttpResponseFirstLine::HttpResponseFirstLineException
Definition: HttpLayer.h:669
pcpp::HttpResponseLayer::Http510NotExtended
Definition: HttpLayer.h:382
pcpp::HttpRequestFirstLine::HttpRequestFirstLineException
Definition: HttpLayer.h:558
pcpp::HttpResponseLayer::Http502BadGateway
Definition: HttpLayer.h:366
pcpp::HttpResponseFirstLine
Definition: HttpLayer.h:603
pcpp::HttpResponseLayer::Http413RequestEntityTooLarge
Definition: HttpLayer.h:310
pcpp::HttpResponseLayer::toString
std::string toString() const
pcpp::HttpResponseLayer::Http410Gone
Definition: HttpLayer.h:304
pcpp::HttpResponseLayer::Http422UnprocessableEntity
Definition: HttpLayer.h:326
pcpp::HttpResponseLayer::Http497HTTPtoHTTPS
Definition: HttpLayer.h:356
pcpp::HttpResponseLayer::getFirstLine
HttpResponseFirstLine * getFirstLine() const
Definition: HttpLayer.h:442
pcpp::HttpResponseLayer::Http599NetworkConnectTimeoutError
Definition: HttpLayer.h:398
pcpp::HttpResponseFirstLine::getVersion
HttpVersion getVersion() const
Definition: HttpLayer.h:633
pcpp::HttpResponseLayer::getContentLength
int getContentLength() const
pcpp::OneDotOne
Definition: HttpLayer.h:28
pcpp::HttpResponseLayer::Http302
Definition: HttpLayer.h:270
pcpp::HttpResponseLayer::Http523ProxyDeclinedRequest
Definition: HttpLayer.h:392
pcpp::HttpResponseLayer::Http501NotImplemented
Definition: HttpLayer.h:364
pcpp::TextBasedProtocolMessage
Definition: TextBasedProtocol.h:106
pcpp::HttpResponseLayer::Http411LengthRequired
Definition: HttpLayer.h:306
pcpp::HttpRequestFirstLine::getUri
std::string getUri() const
pcpp::HttpResponseLayer::Http400BadRequest
Definition: HttpLayer.h:284
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::HttpResponseLayer::Http440LoginTimeout
Definition: HttpLayer.h:340
pcpp::HttpResponseLayer::Http304NotModified
Definition: HttpLayer.h:274
pcpp::HttpResponseFirstLine::setVersion
void setVersion(HttpVersion newVersion)
pcpp::HttpResponseLayer::Http509BandwidthLimitExceeded
Definition: HttpLayer.h:380
pcpp::HttpRequestLayer::HttpCONNECT
Definition: HttpLayer.h:148
pcpp::HttpResponseLayer::Http598NetworkReadTimeoutError
Definition: HttpLayer.h:396
pcpp::HttpRequestLayer::toString
std::string toString() const
pcpp::HttpResponseFirstLine::parseStatusCode
static HttpResponseLayer::HttpResponseStatusCode parseStatusCode(char *data, size_t dataLen)
pcpp::HttpResponseLayer::Http204NoContent
Definition: HttpLayer.h:254
pcpp::HttpResponseLayer::Http303SeeOther
Definition: HttpLayer.h:272
pcpp::HttpResponseFirstLine::getSize
int getSize() const
Definition: HttpLayer.h:653
pcpp::HttpResponseLayer::HttpStatusCodeUnknown
Definition: HttpLayer.h:400
pcpp::HttpResponseFirstLine::setStatusCode
bool setStatusCode(HttpResponseLayer::HttpResponseStatusCode newStatusCode, std::string statusCodeString="")
pcpp::HttpResponseLayer::Http407ProxyAuthenticationRequired
Definition: HttpLayer.h:298
pcpp::Layer
Definition: Layer.h:70
pcpp::HttpResponseLayer::Http207MultiStatus
Definition: HttpLayer.h:260
pcpp::HttpResponseLayer::Http508LoopDetected
Definition: HttpLayer.h:378
pcpp::HeaderField
Definition: TextBasedProtocol.h:31
pcpp::HttpResponseFirstLine::isComplete
bool isComplete() const
Definition: HttpLayer.h:661
pcpp::HttpResponseFirstLine::getStatusCodeAsInt
int getStatusCodeAsInt() const
pcpp::HttpResponseLayer::Http524aTimeoutOccurred
Definition: HttpLayer.h:394
pcpp::HttpResponseLayer::Http507InsufficientStorage
Definition: HttpLayer.h:376
pcpp::HttpResponseLayer::Http208AlreadyReported
Definition: HttpLayer.h:262
pcpp::HttpRequestFirstLine::getSize
int getSize() const
Definition: HttpLayer.h:542
pcpp::HttpRequestLayer::HttpDELETE
Definition: HttpLayer.h:142
pcpp::HttpResponseLayer::Http450BlockedByWindowsParentalControls
Definition: HttpLayer.h:346
pcpp::HttpResponseLayer::Http101SwitchingProtocols
Definition: HttpLayer.h:242
pcpp::HttpResponseLayer::Http520OriginError
Definition: HttpLayer.h:386
TextBasedProtocol.h
pcpp::HttpRequestLayer
Definition: HttpLayer.h:124
pcpp::HttpResponseLayer::Http424FailedDependency
Definition: HttpLayer.h:330
pcpp::HttpResponseLayer::HttpResponseStatusCode
HttpResponseStatusCode
Definition: HttpLayer.h:237
pcpp::HttpResponseLayer::Http494RequestHeaderTooLarge
Definition: HttpLayer.h:350
pcpp::HttpResponseLayer::Http496NoCert
Definition: HttpLayer.h:354
pcpp::HttpResponseLayer::Http404NotFound
Definition: HttpLayer.h:292
pcpp::HttpResponseLayer::Http102Processing
Definition: HttpLayer.h:244
pcpp::HttpResponseLayer::Http426UpgradeRequired
Definition: HttpLayer.h:332
pcpp::HttpMessage::getHTTPPortMap
static const std::map< uint16_t, bool > * getHTTPPortMap()
pcpp::HttpResponseLayer::Http500InternalServerError
Definition: HttpLayer.h:362
pcpp::HttpResponseLayer::Http305UseProxy
Definition: HttpLayer.h:276
pcpp::HttpRequestLayer::HttpOPTIONS
Definition: HttpLayer.h:146
pcpp::HttpRequestLayer::HttpTRACE
Definition: HttpLayer.h:144
pcpp::HttpResponseLayer::Http449RetryWith
Definition: HttpLayer.h:344
pcpp::HttpResponseLayer::operator=
HttpResponseLayer & operator=(const HttpResponseLayer &other)
pcpp::ZeroDotNine
Definition: HttpLayer.h:24
pcpp::HttpResponseLayer::Http429TooManyRequests
Definition: HttpLayer.h:336
pcpp::HttpVersion
HttpVersion
Definition: HttpLayer.h:21
pcpp::HttpResponseLayer::Http308PermanentRedirect
Definition: HttpLayer.h:282
pcpp::HttpRequestLayer::HttpPUT
Definition: HttpLayer.h:140
pcpp::HttpResponseLayer::Http504GatewayTimeout
Definition: HttpLayer.h:370
pcpp::HttpMessage::insertField
virtual HeaderField * insertField(HeaderField *prevField, const std::string &fieldName, const std::string &fieldValue)