fix(cli): prevent purely alphabetic hostnames like localhost from bei…#1884
Closed
Hootsworth wants to merge 1 commit into
Closed
fix(cli): prevent purely alphabetic hostnames like localhost from bei…#1884Hootsworth wants to merge 1 commit into
Hootsworth wants to merge 1 commit into
Conversation
…ng parsed as HTTP method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a bug in HTTPie CLI where purely alphabetic hostnames (such as localhost or httpbin) are incorrectly parsed as the HTTP method when positional arguments are guessed.
When invoking http localhost headers=value, argparse allocates localhost to the optional method positional and headers=value to the required url positional. HTTPie’s _guess_method attempts to swap them but was previously limited to checking if the method is not purely alphabetic (not re.match('^[a-zA-Z]+$', self.args.method)). Since localhost is purely alphabetic, this swap was skipped.
This fix checks the parsed method against a set of standard/known HTTP methods. If it's purely alphabetic but not a standard method, we correctly identify it as a host/URL and swap the arguments.
Type of Change
Bug fix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)
Breaking change (fix or feature that would cause existing functionality to not work as expected)
Documentation update
Performance improvement
Code refactoring
Test improvements
Related Issues
Fixes localhost shorthand parsing false positive.
Changes Made
httpie/cli/argparser.py: Updated _guess_method to check the parsed method against standard HTTP methods before skipping the argument swap.
tests/test_cli.py: Added the test_localhost_with_request_items_does_not_parse_as_method unit test to TestLocalhostShorthand to verify the fix.
Testing
All existing tests pass
Added tests for new functionality
Tested on multiple platforms (if applicable)
Manual testing performed
Platforms Tested
macOS
Documentation
Code is self-documenting
Added/updated code comments
Updated README (if needed)
Updated API documentation (if needed)
Checklist
My code follows the project's style guidelines
I have performed a self-review of my code
I have made corresponding changes to the documentation
My changes generate no new warnings or errors
I have added tests that prove my fix is effective or that my feature works
New and existing unit tests pass locally with my changes
Any dependent changes have been merged and published
Screenshots/Videos
N/A