From 9553503be5b9efa06cefd5c813fe7b861cba495b Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 30 Dec 2009 22:16:10 -0800 Subject: [PATCH] Bugfix: Don't segfault on unknown http method Reported by Chakrit Wichian. --- src/node_http.cc | 60 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/src/node_http.cc b/src/node_http.cc index 5ccc53bcbb..230b27200c 100644 --- a/src/node_http.cc +++ b/src/node_http.cc @@ -29,6 +29,23 @@ static Persistent header_complete_symbol; static Persistent body_symbol; static Persistent eof_symbol; +static Persistent delete_sym; +static Persistent get_sym; +static Persistent head_sym; +static Persistent post_sym; +static Persistent put_sym; +static Persistent connect_sym; +static Persistent options_sym; +static Persistent trace_sym; +static Persistent copy_sym; +static Persistent lock_sym; +static Persistent mkcol_sym; +static Persistent move_sym; +static Persistent propfind_sym; +static Persistent proppatch_sym; +static Persistent unlock_sym; +static Persistent unknown_method_sym; + void HTTPConnection::Initialize (Handle target) { @@ -53,6 +70,24 @@ HTTPConnection::Initialize (Handle target) status_code_symbol = NODE_PSYMBOL("statusCode"); http_version_symbol = NODE_PSYMBOL("httpVersion"); should_keep_alive_symbol = NODE_PSYMBOL("should_keep_alive"); + + + delete_sym = NODE_PSYMBOL("DELETE"); + get_sym = NODE_PSYMBOL("GET"); + head_sym = NODE_PSYMBOL("HEAD"); + post_sym = NODE_PSYMBOL("POST"); + put_sym = NODE_PSYMBOL("PUT"); + connect_sym = NODE_PSYMBOL("CONNECT"); + options_sym = NODE_PSYMBOL("OPTIONS"); + trace_sym = NODE_PSYMBOL("TRACE"); + copy_sym = NODE_PSYMBOL("COPY"); + lock_sym = NODE_PSYMBOL("LOCK"); + mkcol_sym = NODE_PSYMBOL("MKCOL"); + move_sym = NODE_PSYMBOL("MOVE"); + propfind_sym = NODE_PSYMBOL("PROPFIND"); + proppatch_sym = NODE_PSYMBOL("PROPPATCH"); + unlock_sym = NODE_PSYMBOL("UNLOCK"); + unknown_method_sym = NODE_PSYMBOL("UNKNOWN_METHOD"); } Handle @@ -242,6 +277,28 @@ HTTPConnection::on_header_value (http_parser *parser, const char *buf, size_t le return 0; } +static inline Persistent +method_to_str(enum http_method m) { + switch (m) { + case HTTP_DELETE: return delete_sym; + case HTTP_GET: return get_sym; + case HTTP_HEAD: return head_sym; + case HTTP_POST: return post_sym; + case HTTP_PUT: return put_sym; + case HTTP_CONNECT: return connect_sym; + case HTTP_OPTIONS: return options_sym; + case HTTP_TRACE: return trace_sym; + case HTTP_COPY: return copy_sym; + case HTTP_LOCK: return lock_sym; + case HTTP_MKCOL: return mkcol_sym; + case HTTP_MOVE: return move_sym; + case HTTP_PROPFIND: return propfind_sym; + case HTTP_PROPPATCH: return proppatch_sym; + case HTTP_UNLOCK: return unlock_sym; + default: return unknown_method_sym; + } +} + int HTTPConnection::on_headers_complete (http_parser *parser) { @@ -253,8 +310,7 @@ HTTPConnection::on_headers_complete (http_parser *parser) // METHOD if (connection->type_ == HTTP_REQUEST) { - message_info->Set(method_symbol, String::NewSymbol( - http_method_str(connection->parser_.method))); + message_info->Set(method_symbol, method_to_str(connection->parser_.method)); } // STATUS