Skip to content

Commit 2ed7527

Browse files
ssambibnoordhuis
authored andcommitted
Add http_status_str() function.
Fixes: nodejs#371 PR-URL: nodejs#429 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent a7c2e86 commit 2ed7527

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

http_parser.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,16 @@ http_method_str (enum http_method m)
20962096
return ELEM_AT(method_strings, m, "<unknown>");
20972097
}
20982098

2099+
const char *
2100+
http_status_str (enum http_status s)
2101+
{
2102+
switch (s) {
2103+
#define XX(num, name, string) case HTTP_STATUS_##name: return #string;
2104+
HTTP_STATUS_MAP(XX)
2105+
#undef XX
2106+
default: return "<unknown>";
2107+
}
2108+
}
20992109

21002110
void
21012111
http_parser_init (http_parser *parser, enum http_parser_type t)

http_parser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ int http_should_keep_alive(const http_parser *parser);
407407
/* Returns a string version of the HTTP method. */
408408
const char *http_method_str(enum http_method m);
409409

410+
/* Returns a string version of the HTTP status code. */
411+
const char *http_status_str(enum http_status s);
412+
410413
/* Return a string name of the given error */
411414
const char *http_errno_name(enum http_errno err);
412415

test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3374,6 +3374,14 @@ test_method_str (void)
33743374
assert(0 == strcmp("<unknown>", http_method_str(1337)));
33753375
}
33763376

3377+
void
3378+
test_status_str (void)
3379+
{
3380+
assert(0 == strcmp("OK", http_status_str(HTTP_STATUS_OK)));
3381+
assert(0 == strcmp("Not Found", http_status_str(HTTP_STATUS_NOT_FOUND)));
3382+
assert(0 == strcmp("<unknown>", http_status_str(1337)));
3383+
}
3384+
33773385
void
33783386
test_message (const struct message *message)
33793387
{
@@ -4104,6 +4112,7 @@ main (void)
41044112
test_preserve_data();
41054113
test_parse_url();
41064114
test_method_str();
4115+
test_status_str();
41074116

41084117
//// NREAD
41094118
test_header_nread_value();

0 commit comments

Comments
 (0)