From 2ed7527795f511f7b1a0e1948a4f573ca5f9d456 Mon Sep 17 00:00:00 2001 From: Stefano Sambi Date: Wed, 23 May 2018 15:30:53 +0200 Subject: [PATCH] Add http_status_str() function. Fixes: https://github.com/nodejs/http-parser/issues/371 PR-URL: https://github.com/nodejs/http-parser/pull/429 Reviewed-By: Ben Noordhuis --- http_parser.c | 10 ++++++++++ http_parser.h | 3 +++ test.c | 9 +++++++++ 3 files changed, 22 insertions(+) diff --git a/http_parser.c b/http_parser.c index 908b8f1..eeacef5 100644 --- a/http_parser.c +++ b/http_parser.c @@ -2096,6 +2096,16 @@ http_method_str (enum http_method m) return ELEM_AT(method_strings, m, ""); } +const char * +http_status_str (enum http_status s) +{ + switch (s) { +#define XX(num, name, string) case HTTP_STATUS_##name: return #string; + HTTP_STATUS_MAP(XX) +#undef XX + default: return ""; + } +} void http_parser_init (http_parser *parser, enum http_parser_type t) diff --git a/http_parser.h b/http_parser.h index a0de71e..e894d7c 100644 --- a/http_parser.h +++ b/http_parser.h @@ -407,6 +407,9 @@ int http_should_keep_alive(const http_parser *parser); /* Returns a string version of the HTTP method. */ const char *http_method_str(enum http_method m); +/* Returns a string version of the HTTP status code. */ +const char *http_status_str(enum http_status s); + /* Return a string name of the given error */ const char *http_errno_name(enum http_errno err); diff --git a/test.c b/test.c index 5b97fec..dcf5486 100644 --- a/test.c +++ b/test.c @@ -3374,6 +3374,14 @@ test_method_str (void) assert(0 == strcmp("", http_method_str(1337))); } +void +test_status_str (void) +{ + assert(0 == strcmp("OK", http_status_str(HTTP_STATUS_OK))); + assert(0 == strcmp("Not Found", http_status_str(HTTP_STATUS_NOT_FOUND))); + assert(0 == strcmp("", http_status_str(1337))); +} + void test_message (const struct message *message) { @@ -4104,6 +4112,7 @@ main (void) test_preserve_data(); test_parse_url(); test_method_str(); + test_status_str(); //// NREAD test_header_nread_value();