Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement HttpResponseStatusCode to string #1157

Closed
tigercosmos opened this issue Jul 10, 2023 · 10 comments · Fixed by #1180
Closed

implement HttpResponseStatusCode to string #1157

tigercosmos opened this issue Jul 10, 2023 · 10 comments · Fixed by #1180

Comments

@tigercosmos
Copy link
Collaborator

there is no toString() function for HttpResponseStatusCode

it would be nice to have:

HttpResponseStatusCode code = HttpResponseStatusCode::Http200OK;
code.toString(); // "HTTP 200 OK"

We already have something like StatusCodeEnumToString, however it's only in cpp code and the messages are not clear enough, e.g. Http200OK -> OK but it's better to have HTTP 200 OK

@seladb
Copy link
Owner

seladb commented Jul 10, 2023

HttpResponseStatusCode is an enum so if cannot have toString()...

However there is already such method: getStatusCodeString(). Here is how to use it:

pcpp::HttpResponseLayer* responseLayer = httpPacket.getLayerOfType<pcpp::HttpResponseLayer>();
std::cout << responseLayer->getFirstLine()->getStatusCodeString() << std::endl;

// "OK" will be printed

You can also get the status code as integer:

pcpp::HttpResponseLayer* responseLayer = httpPacket.getLayerOfType<pcpp::HttpResponseLayer>();
std::cout << responseLayer->getFirstLine()->getStatusCodeAsInt() << std::endl;

// "200" will be printed

@tigercosmos
Copy link
Collaborator Author

tigercosmos commented Jul 10, 2023

Thanks for your reply. Currently, getStatusCodeAsInt prints the code in HttpResponseFirstLine.
When there is a standalone status code, there is no function to print it out.

here is the use case:

we probably can have a method like

static int HttpResponseLayer::getStatusCodeAsInt(HttpResponseLayer::HttpResponseStatusCode code);
pcpp::HttpResponseLayer* responseLayer = httpPacket.getLayerOfType<pcpp::HttpResponseLayer>();
if(responseLayer->getStatusCode()  != HttpResponseStatusCode::Http200OK) {
   std::cerr << "got status code: " << responseLayer->getFirstLine()->getStatusCodeAsInt()
             << ", expect status code: " << HttpResponseLayer::getStatusCodeAsInt(HttpResponseStatusCode::Http200OK);
}

@tigercosmos
Copy link
Collaborator Author

@seladb any comments?

@seladb
Copy link
Owner

seladb commented Jul 15, 2023

@tigercosmos I'm sorry for the late reply

Maybe we can just add these status non-standard status codes? 🤔

@tigercosmos
Copy link
Collaborator Author

@seladb sorry, I don't understand, could you provide an example?

@seladb
Copy link
Owner

seladb commented Jul 19, 2023

sorry @tigercosmos I misread your previous comments.
If we want to add toString() to the HttpResponseStatusCode enum we probably need to create a wrapper class for it, similar to the suggestion here: https://stackoverflow.com/a/53284026

I think it's doable and will add some value. It can also use the existing structures in HttpLayer.cpp such as StatusCodeEnumToString and StatusCodeEnumToInt. Would you consider implementing it?

@tigercosmos
Copy link
Collaborator Author

sure, sounds interesting, let me do it

@seladb
Copy link
Owner

seladb commented Aug 10, 2023

@tigercosmos are you still planning to work on it?

@tigercosmos
Copy link
Collaborator Author

@seladb yes, sorry for the late. I will finish it in these days.

@seladb
Copy link
Owner

seladb commented Aug 27, 2023

Fixed by #1180 . Thank you so much @tigercosmos for adding this to PcapPlusPlus! 🙏

@seladb seladb closed this as completed Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants