Skip to content

Commit 56a6ee6

Browse files
authored
Merge pull request #1 from Hootsworth/fix-localhost-method-parsing
fix(cli): prevent purely alphabetic hostnames like localhost from being parsed as HTTP method
2 parents 5b604c3 + 684066f commit 56a6ee6

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

httpie/cli/argparser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,10 @@ def _guess_method(self):
420420
self.args.method = HTTP_GET
421421

422422
# FIXME: False positive, e.g., "localhost" matches but is a valid URL.
423-
elif not re.match('^[a-zA-Z]+$', self.args.method):
423+
elif self.args.method.upper() not in {
424+
'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'CONNECT', 'TRACE',
425+
'PROPFIND', 'PROPPATCH', 'MKCOL', 'COPY', 'MOVE', 'LOCK', 'UNLOCK', 'PURGE'
426+
} or not re.match('^[a-zA-Z]+$', self.args.method):
424427
# Invoked as `http URL item+'. The URL is now in `args.method`
425428
# and the first ITEM is now incorrectly in `args.url`.
426429
try:

tests/test_cli.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ def test_dont_expand_full_ipv6_as_shorthand(self):
235235
)
236236
assert args.url == 'http://0000:0000:0000:0000:0000:0000:0000:0001'
237237

238+
def test_localhost_with_request_items_does_not_parse_as_method(self):
239+
args = parser.parse_args(
240+
args=['localhost', 'foo=bar'],
241+
env=MockEnvironment()
242+
)
243+
assert args.url == 'http://localhost'
244+
assert args.method == 'POST'
245+
assert args.request_items[0].key == 'foo'
246+
assert args.request_items[0].value == 'bar'
247+
238248

239249
class TestArgumentParser:
240250

0 commit comments

Comments
 (0)