Skip to content

Commit

Permalink
Fix case when curl comman contains escaped linebreak
Browse files Browse the repository at this point in the history
  • Loading branch information
hongquan committed Aug 9, 2021
1 parent 3d169da commit 0968172
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 47 deletions.
9 changes: 8 additions & 1 deletion curlipie/pie.py
Expand Up @@ -13,6 +13,7 @@


REGEX_SINGLE_OPT = re.compile(r'-\w$')
REGEX_SHELL_LINEBREAK = re.compile(r'\\\s+')
logger = logging.getLogger(__name__)


Expand All @@ -37,10 +38,16 @@ def join_previous_arg(cmds: List[str], name: str):
cmds.append(f'-{name}')


def clean_curl(cmd: str):
''' Remove slash-escaped newlines and normal newlines from curl command.'''
stripped = REGEX_SHELL_LINEBREAK.sub(' ', cmd)
return ' '.join(stripped.splitlines())


def curl_to_httpie(cmd: str, long_option: bool = False) -> ConversionResult:
# The cmd can be multiline string, with escape symbols, shlex doesn't support it, so
# we should convert it to one-line first.
oneline = ''.join(cmd.splitlines())
oneline = clean_curl(cmd)
try:
cargs = shlex.split(oneline)
except ValueError as e:
Expand Down
44 changes: 22 additions & 22 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "curlipie"
version = "0.8.0"
version = "0.8.1"
description = "Library to convert cURL command line to HTTPie"
authors = ["Nguyễn Hồng Quân <ng.hong.quan@gmail.com>"]
maintainers = [
Expand Down
23 changes: 0 additions & 23 deletions requirements.txt
@@ -1,43 +1,20 @@
aiofiles==0.7.0; python_version >= "3.6" and python_version < "4.0"
asgiref==3.3.4; python_version >= "3.6"
atomicwrites==1.4.0; python_version >= "3.5" and python_full_version < "3.0.0" and sys_platform == "win32" or sys_platform == "win32" and python_version >= "3.5" and python_full_version >= "3.4.0"
attrs==21.2.0; python_version >= "3.5" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.5"
click==8.0.1; python_version >= "3.6"
colorama==0.4.4; python_version >= "3.6" and python_full_version < "3.0.0" and sys_platform == "win32" and platform_system == "Windows" or sys_platform == "win32" and python_version >= "3.6" and python_full_version >= "3.5.0" and platform_system == "Windows"
dataclasses==0.8; python_version >= "3.6" and python_version < "3.7" and python_full_version >= "3.6.1"
devtools==0.5.1; python_version >= "3.5"
fastapi==0.65.2; python_version >= "3.6"
first==2.0.2
flake8==3.9.2; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0"
h11==0.12.0; python_version >= "3.6"
http-constants==0.4.0; python_full_version >= "3.6.0"
idna==3.2; python_version >= "3.6"
importlib-metadata==2.1.1; python_version >= "3.6" and python_full_version < "3.0.0" and python_version < "3.8" or python_version >= "3.6" and python_version < "3.8" and python_full_version >= "3.5.0"
jinja2==3.0.1; python_version >= "3.6"
kiss-headers==2.3.0; python_full_version >= "3.6.0"
logbook==1.5.3
markupsafe==2.0.1; python_version >= "3.6"
mccabe==0.6.1; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0"
more-itertools==8.8.0; python_version >= "3.5"
multidict==4.7.6; python_version >= "3.5"
mypy-extensions==0.4.3
orjson==2.6.8; python_version >= "3.7"
packaging==20.9; python_version >= "3.5" and python_full_version < "3.0.0" or python_full_version >= "3.4.0" and python_version >= "3.5"
pluggy==0.13.1; python_version >= "3.5" and python_full_version < "3.0.0" or python_full_version >= "3.4.0" and python_version >= "3.5"
py==1.10.0; python_version >= "3.5" and python_full_version < "3.0.0" or python_full_version >= "3.4.0" and python_version >= "3.5"
pycodestyle==2.7.0; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0"
pydantic==1.8.2; python_full_version >= "3.6.1"
pyflakes==2.3.1; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0"
pyparsing==2.4.7; python_version >= "3.5" and python_full_version < "3.0.0" or python_full_version >= "3.4.0" and python_version >= "3.5"
pytest-flake8==1.0.7
pytest==5.4.3; python_version >= "3.5"
python-rapidjson==0.9.4; python_version ~= "3.6"
single-version==1.5.1; python_version >= "3.6" and python_version < "4.0"
starlette==0.14.2; python_version >= "3.6"
typed-argument-parser==1.6.3
typing-extensions==3.10.0.0; python_version < "3.8" and python_version >= "3.6" and python_full_version >= "3.6.1"
typing-inspect==0.6.0
uvicorn==0.14.0
wcwidth==0.2.5; python_version >= "3.5"
yarl==1.6.3; python_version >= "3.6"
zipp==3.4.1; python_version >= "3.6" and python_full_version < "3.0.0" and python_version < "3.8" or python_version >= "3.6" and python_version < "3.8" and python_full_version >= "3.5.0"
8 changes: 8 additions & 0 deletions tests/test_curlipie.py
Expand Up @@ -98,6 +98,14 @@ def test_multi_line():
"""boarding_floor=1 destination_floor=9 elevator_bank_number=3""")


def test_escaped_linebreak():
curl = r"""curl -H 'Content-Type: application/json' \
-X POST http://127.0.0.1:5984/demo \
-d '{"company": "Example, Inc."}'"""
httpie = curl_to_httpie(curl).httpie
assert httpie == "http 127.0.0.1:5984/demo company='Example, Inc.'"


def test_json_with_quote_escape():
curl = ('curl --url http://localhost:3000/posts -H "Content-Type: application/json;charset=UTF-8" '
r'-d "{\"title\":\"murat\",\"author\":\"öner\"}"')
Expand Down

1 comment on commit 0968172

@vercel
Copy link

@vercel vercel bot commented on 0968172 Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.