diff --git a/Packet++/header/HttpLayer.h b/Packet++/header/HttpLayer.h index 1c47f8a61e..6e1dbb16a0 100644 --- a/Packet++/header/HttpLayer.h +++ b/Packet++/header/HttpLayer.h @@ -5,6 +5,17 @@ #include #include +#ifndef PCPP_DEPRECATED +#if defined(__GNUC__) || defined(__clang__) +#define PCPP_DEPRECATED __attribute__((deprecated)) +#elif defined(_MSC_VER) +#define PCPP_DEPRECATED __declspec(deprecated) +#else +#pragma message("WARNING: DEPRECATED feature is not implemented for this compiler") +#define PCPP_DEPRECATED +#endif +#endif + /// @file /** @@ -425,14 +436,14 @@ namespace pcpp /** * @brief Construct HttpResponseStatusCode from the code number and the customized message * @param[in] statusCodeNumber the status code in number, e.g. 200, 404 - * @param[in] statusMessage the status message, optional + * @param[in] statusMessage the status message, optional, leave empty to use a default message */ explicit HttpResponseStatusCode(const int &statusCodeNumber, const std::string& statusMessage = ""); /** * @brief Construct HttpResponseStatusCode from Value enum and the customized message * @param[in] statusCode the status code enum - * @param[in] statusMessage the status message, optional + * @param[in] statusMessage the customized status message, optional */ explicit HttpResponseStatusCode(const Value& statusCode, const std::string& statusMessage); @@ -513,7 +524,15 @@ namespace pcpp * But the user can set a non-default status code string and it will be written in the header first line. Empty string ("") means using the * default status code string */ - HttpResponseLayer(HttpVersion version, HttpResponseStatusCode statusCode, std::string statusCodeString = ""); + PCPP_DEPRECATED explicit HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode, const std::string& statusCodeString); + + /** + * A constructor that allocates a new HTTP response header with only the first line filled. Object will be created without further fields. + * The user can then add fields using addField() methods + * @param[in] version HTTP version to be used + * @param[in] statusCode Status code to be used + */ + explicit HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode); virtual ~HttpResponseLayer(); @@ -717,11 +736,9 @@ namespace pcpp /** * Set the status code * @param[in] newStatusCode The new status code to set - * @param[in] statusCodeString An optional parameter: set a non-default status code message (e.g "Bla Bla" instead of "Not Found"). If - * this parameter isn't supplied or supplied as empty string (""), the default message for the status code will be set * @return True if setting the status code was completed successfully, false otherwise */ - bool setStatusCode(HttpResponseStatusCode newStatusCode, std::string statusCodeString = ""); + bool setStatusCode(const HttpResponseStatusCode& newStatusCode); /** * @return The HTTP version @@ -785,7 +802,7 @@ namespace pcpp private: HttpResponseFirstLine(HttpResponseLayer* httpResponse); - HttpResponseFirstLine(HttpResponseLayer* httpResponse, HttpVersion version, HttpResponseStatusCode statusCode, std::string statusCodeString = ""); + HttpResponseFirstLine(HttpResponseLayer* httpResponse, HttpVersion version, const HttpResponseStatusCode& statusCode); HttpResponseLayer* m_HttpResponse; HttpVersion m_Version; diff --git a/Packet++/src/HttpLayer.cpp b/Packet++/src/HttpLayer.cpp index 17c5d68057..dd2c0e585f 100644 --- a/Packet++/src/HttpLayer.cpp +++ b/Packet++/src/HttpLayer.cpp @@ -697,10 +697,17 @@ HttpResponseLayer::HttpResponseLayer(uint8_t* data, size_t dataLen, Layer* prevL parseFields(); } -HttpResponseLayer::HttpResponseLayer(HttpVersion version, HttpResponseStatusCode statusCode, std::string statusCodeString) +HttpResponseLayer::HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode, const std::string& statusCodeString) { m_Protocol = HTTPResponse; - m_FirstLine = new HttpResponseFirstLine(this, version, statusCode, std::move(statusCodeString)); + m_FirstLine = new HttpResponseFirstLine(this, version, HttpResponseStatusCode(statusCode, statusCodeString)); + m_FieldsOffset = m_FirstLine->getSize(); +} + +HttpResponseLayer::HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode) +{ + m_Protocol = HTTPResponse; + m_FirstLine = new HttpResponseFirstLine(this, version, statusCode); m_FieldsOffset = m_FirstLine->getSize(); } @@ -802,7 +809,7 @@ std::string HttpResponseFirstLine::getStatusCodeString() const return ""; } -bool HttpResponseFirstLine::setStatusCode(HttpResponseStatusCode newStatusCode, std::string statusCodeString) +bool HttpResponseFirstLine::setStatusCode(const HttpResponseStatusCode& newStatusCode) { if (newStatusCode.isUnsupportedCode()) { @@ -812,10 +819,8 @@ bool HttpResponseFirstLine::setStatusCode(HttpResponseStatusCode newStatusCode, //extend or shorten layer - HttpResponseStatusCode newStatusCodeWithMessage(newStatusCode, statusCodeString); - size_t statusStringOffset = 13; - auto newStatusCodeMessage = newStatusCodeWithMessage.getMessage(); + auto newStatusCodeMessage = newStatusCode.getMessage(); int lengthDifference = newStatusCodeMessage.length() - getStatusCodeString().length(); if (lengthDifference > 0) @@ -845,7 +850,7 @@ bool HttpResponseFirstLine::setStatusCode(HttpResponseStatusCode newStatusCode, // change status code memcpy(m_HttpResponse->m_Data+9, newStatusCode.toString().c_str(), 3); - m_StatusCode = newStatusCodeWithMessage; + m_StatusCode = newStatusCode; m_FirstLineEndOffset += lengthDifference; @@ -944,7 +949,7 @@ HttpResponseFirstLine::HttpResponseFirstLine(HttpResponseLayer* httpResponse) : } -HttpResponseFirstLine::HttpResponseFirstLine(HttpResponseLayer* httpResponse, HttpVersion version, HttpResponseStatusCode statusCode, std::string statusCodeString) +HttpResponseFirstLine::HttpResponseFirstLine(HttpResponseLayer* httpResponse, HttpVersion version, const HttpResponseStatusCode& statusCode) { if (statusCode.isUnsupportedCode()) { @@ -960,7 +965,7 @@ HttpResponseFirstLine::HttpResponseFirstLine(HttpResponseLayer* httpResponse, H m_HttpResponse = httpResponse; - m_StatusCode = HttpResponseStatusCode(statusCode, statusCodeString); + m_StatusCode = statusCode; m_Version = version; std::string firstLine = "HTTP/" + VersionEnumToString[m_Version] + " " + m_StatusCode.toString() + " " + m_StatusCode.getMessage() + "\r\n"; diff --git a/Tests/Packet++Test/Tests/HttpTests.cpp b/Tests/Packet++Test/Tests/HttpTests.cpp index f739c336a5..9c7d10032b 100644 --- a/Tests/Packet++Test/Tests/HttpTests.cpp +++ b/Tests/Packet++Test/Tests/HttpTests.cpp @@ -475,7 +475,7 @@ PTF_TEST_CASE(HttpResponseLayerEditTest) PTF_ASSERT_BUF_COMPARE(expectedHttpResponse.c_str(), responseLayer->getData(), expectedHttpResponse.length()); - PTF_ASSERT_TRUE(responseLayer->getFirstLine()->setStatusCode(pcpp::HttpResponseStatusCode::Http413RequestEntityTooLarge, "This is a test")); + PTF_ASSERT_TRUE(responseLayer->getFirstLine()->setStatusCode(pcpp::HttpResponseStatusCode(pcpp::HttpResponseStatusCode::Http413RequestEntityTooLarge, "This is a test"))); PTF_ASSERT_EQUAL(responseLayer->getFirstLine()->getStatusCodeAsInt(), 413); PTF_ASSERT_EQUAL(responseLayer->getFirstLine()->getStatusCodeString(), "This is a test");