Skip to content

Commit

Permalink
update HttpResponseLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
tigercosmos committed Sep 9, 2023
1 parent 2e1e887 commit 14ee3d2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
31 changes: 24 additions & 7 deletions Packet++/header/HttpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
#include <string>
#include <exception>

#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

/**
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
23 changes: 14 additions & 9 deletions Packet++/src/HttpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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())
{
Expand All @@ -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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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())
{
Expand All @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion Tests/Packet++Test/Tests/HttpTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit 14ee3d2

Please sign in to comment.