Skip to content

Commit 6100228

Browse files
committed
http: expose supported methods
Expose the list of supported HTTP methods as a property on the 'http' module object. Fixes nodejs#6422.
1 parent 61ccaf9 commit 6100228

5 files changed

Lines changed: 47 additions & 0 deletions

File tree

doc/api/http.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ list like the following:
4343
'Host', 'mysite.com',
4444
'accepT', '*/*' ]
4545

46+
## http.METHODS
47+
48+
* {Array}
49+
50+
A list of the HTTP methods that are supported by the parser.
51+
4652
## http.STATUS_CODES
4753

4854
* {Object}

lib/_http_common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ exports.debug = debug;
3333
exports.CRLF = '\r\n';
3434
exports.chunkExpression = /chunk/i;
3535
exports.continueExpression = /100-continue/i;
36+
exports.methods = HTTPParser.methods;
3637

3738
var kOnHeaders = HTTPParser.kOnHeaders | 0;
3839
var kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;

lib/http.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var IncomingMessage = exports.IncomingMessage = incoming.IncomingMessage;
2828

2929

3030
var common = require('_http_common');
31+
exports.METHODS = util._extend([], common.methods).sort();
3132
exports.parsers = common.parsers;
3233

3334

src/node_http_parser.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,13 @@ void InitHttpParser(Handle<Object> target,
598598
t->Set(FIXED_ONE_BYTE_STRING(node_isolate, "kOnMessageComplete"),
599599
Integer::NewFromUnsigned(kOnMessageComplete, node_isolate));
600600

601+
Local<Array> methods = Array::New();
602+
#define V(num, name, string) \
603+
methods->Set(num, FIXED_ONE_BYTE_STRING(node_isolate, #string));
604+
HTTP_METHOD_MAP(V)
605+
#undef V
606+
t->Set(FIXED_ONE_BYTE_STRING(node_isolate, "methods"), methods);
607+
601608
NODE_SET_PROTOTYPE_METHOD(t, "execute", Parser::Execute);
602609
NODE_SET_PROTOTYPE_METHOD(t, "finish", Parser::Finish);
603610
NODE_SET_PROTOTYPE_METHOD(t, "reinitialize", Parser::Reinitialize);

test/simple/test-http-methods.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var http = require('http');
25+
var util = require('util');
26+
27+
assert(Array.isArray(http.METHODS));
28+
assert(http.METHODS.length > 0);
29+
assert(http.METHODS.indexOf('GET') !== -1);
30+
assert(http.METHODS.indexOf('HEAD') !== -1);
31+
assert(http.METHODS.indexOf('POST') !== -1);
32+
assert.deepEqual(util._extend([], http.METHODS), http.METHODS.sort());

0 commit comments

Comments
 (0)