Skip to content

Commit

Permalink
🗓 Jul 1, 2023 2:36:36 PM
Browse files Browse the repository at this point in the history
💚 build steps added/updated
🚀 update to_url_encoding to encode regular chars also
🧪 tests added/updated
  • Loading branch information
securisec committed Jul 1, 2023
1 parent a589ff7 commit 7634be3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests_multi_os.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
- name: Test with bandit
run: |
bandit --recursive chepy/ --ignore-nosec --skip B101,B413,B303,B310,B112,B304,B320,B410,B404,B608,B311
bandit --recursive chepy/ --ignore-nosec --skip B101,B413,B303,B310,B112,B304,B320,B410,B404,B608,B311,B324
# - name: Test docs
# if: ${{ !env.ACT }} && contains(matrix.os, 'ubuntu')
Expand Down
13 changes: 11 additions & 2 deletions chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,14 +747,15 @@ def from_hexdump(self) -> DataFormatT:
return self

@ChepyDecorators.call_stack
def to_url_encoding(self, safe: str = "") -> DataFormatT:
def to_url_encoding(self, safe: str = "", all_chars: bool = False) -> DataFormatT:
"""URL encode
Encodes problematic characters into percent-encoding,
a format supported by URIs/URLs.
Args:
safe (str, optional): String of characters that will not be encoded, by default ""
all_chars (bool, optional): Encode all characters including safe characters
Returns:
Chepy: The Chepy object.
Expand All @@ -765,7 +766,15 @@ def to_url_encoding(self, safe: str = "") -> DataFormatT:
>>> Chepy("https://google.com/?lol=some data&a=1").to_url_encoding(safe="/:").o
"https://google.com/%3Flol%3Dsome+data%26a%3D1"
"""
self.state = _urllib_quote_plus(self._convert_to_str(), safe=safe)
data = self._convert_to_str()

def encode_all(string):
return "".join("%{0:0>2x}".format(ord(char)) for char in string)

if all_chars:
self.state = encode_all(data)
else:
self.state = _urllib_quote_plus(data, safe=safe)
return self

@ChepyDecorators.call_stack
Expand Down
4 changes: 2 additions & 2 deletions chepy/modules/dataformat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class DataFormat(ChepyCore):
def str_from_hexdump(self: DataFormatT) -> DataFormatT: ...
def to_hexdump(self: DataFormatT) -> DataFormatT: ...
def from_hexdump(self: DataFormatT) -> DataFormatT: ...
def from_url_encoding(self: DataFormatT, safe: str=...) -> DataFormatT: ...
def to_url_encoding(self: DataFormatT) -> DataFormatT: ...
def from_url_encoding(self: DataFormatT) -> DataFormatT: ...
def to_url_encoding(self: DataFormatT, safe: str=..., all_chars: bool=...) -> DataFormatT: ...
def bytearray_to_str(self: DataFormatT, encoding: str=..., errors: str=...) -> DataFormatT: ...
def str_to_list(self: DataFormatT) -> DataFormatT: ...
def str_to_dict(self: DataFormatT) -> DataFormatT: ...
Expand Down
1 change: 1 addition & 0 deletions tests/test_dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def test_to_url_encoding():
Chepy("https://google.com/?lol=some data&a=1").to_url_encoding(safe="/:").o
== "https://google.com/%3Flol%3Dsome+data%26a%3D1"
)
assert Chepy("a").to_url_encoding(all_chars=True).to_url_encoding().o == "%2561"


def test_from_url_encoding():
Expand Down

0 comments on commit 7634be3

Please sign in to comment.