Skip to content

Commit

Permalink
Bugfix: Don't segfault on unknown http method
Browse files Browse the repository at this point in the history
Reported by Chakrit Wichian.
  • Loading branch information
ry committed Dec 31, 2009
1 parent 8fd46a3 commit 9553503
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions src/node_http.cc
Expand Up @@ -29,6 +29,23 @@ static Persistent<String> header_complete_symbol;
static Persistent<String> body_symbol;
static Persistent<String> eof_symbol;

static Persistent<String> delete_sym;
static Persistent<String> get_sym;
static Persistent<String> head_sym;
static Persistent<String> post_sym;
static Persistent<String> put_sym;
static Persistent<String> connect_sym;
static Persistent<String> options_sym;
static Persistent<String> trace_sym;
static Persistent<String> copy_sym;
static Persistent<String> lock_sym;
static Persistent<String> mkcol_sym;
static Persistent<String> move_sym;
static Persistent<String> propfind_sym;
static Persistent<String> proppatch_sym;
static Persistent<String> unlock_sym;
static Persistent<String> unknown_method_sym;

void
HTTPConnection::Initialize (Handle<Object> target)
{
Expand All @@ -53,6 +70,24 @@ HTTPConnection::Initialize (Handle<Object> 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<Value>
Expand Down Expand Up @@ -242,6 +277,28 @@ HTTPConnection::on_header_value (http_parser *parser, const char *buf, size_t le
return 0;
}

static inline Persistent<String>
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)
{
Expand All @@ -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
Expand Down

0 comments on commit 9553503

Please sign in to comment.