-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe45630
commit 82d1437
Showing
11 changed files
with
228 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from . import grep | ||
|
||
def get_key_value(cookie: str): | ||
""" | ||
Get a key-value pair from an HTTP cookie.\n | ||
Returns an empty key-value pair on failure. | ||
""" | ||
key = ""; value = "" | ||
if grep.search(r"^[^\=\;]+\=[^\=\;]+$|^[^\=\;]+\=$", cookie): | ||
key, value = cookie.split("=", 1) | ||
return key.strip(), value.strip() | ||
|
||
def format_key_value(key: str, value: str): | ||
""" | ||
Returns a key-value pair as a string. | ||
""" | ||
return f"{key}={value}" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from . import array | ||
|
||
import os | ||
|
||
__ENCODING = "ISO-8859-1" | ||
|
||
def validate(file: str): | ||
""" | ||
Validate a file.\n | ||
Success flag is 'True' if the 'file' exists and is a regular file, has a read permission and is not empty. | ||
""" | ||
success = False | ||
message = "" | ||
if not os.path.isfile(file): | ||
message = f"\"{file}\" does not exist" | ||
elif not os.access(file, os.R_OK): | ||
message = f"\"{file}\" does not have a read permission" | ||
elif not os.stat(file).st_size > 0: | ||
message = f"\"{file}\" is empty" | ||
else: | ||
success = True | ||
return success, message | ||
|
||
def read_array(file: str) -> list[str]: | ||
""" | ||
Read a file line by line, and append the lines to a list.\n | ||
Whitespace will be stripped from each line, and empty lines will be removed.\n | ||
Returns a unique list. | ||
""" | ||
tmp = [] | ||
with open(file, "r", encoding = __ENCODING) as stream: | ||
for line in stream: | ||
line = line.strip() | ||
if line: | ||
tmp.append(line) | ||
return array.unique(tmp) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import regex as re | ||
|
||
__FLAGS = re.MULTILINE | re.IGNORECASE | ||
|
||
def validate(query: str): | ||
""" | ||
Validate a regular expression. | ||
""" | ||
success = False | ||
message = "" | ||
try: | ||
re.compile(query) | ||
success = True | ||
except re.error: | ||
message = f"Invalid RegEx: {query}" | ||
return success, message | ||
|
||
def search(string: str, query: str): | ||
""" | ||
Check if there are any matches in a string using the specified RegEx pattern. | ||
""" | ||
return bool(re.search(query, string, flags = __FLAGS)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from . import grep | ||
|
||
def get_key_value(header: str): | ||
""" | ||
Get a key-value pair from an HTTP request header.\n | ||
Returns an empty key-value pair on failure. | ||
""" | ||
key = ""; value = "" | ||
if grep.search(r"^[^\:]+\:.+$", header): | ||
key, value = header.split(":", 1) | ||
elif grep.search(r"^[^\;]+\;$", header): | ||
key, value = header.split(";", 1) | ||
return key.strip(), value.strip() | ||
|
||
def format_key_value(key: str, value: str): | ||
""" | ||
Returns a key-value pair as a string. | ||
""" | ||
return f"{key}: {value}" if value else f"{key};" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import regex as re | ||
|
||
__SEP = "/" | ||
|
||
def replace_multiple_slashes(path: str): | ||
""" | ||
Replace multiple consecutive forward slashes with a single forward slash. | ||
For example, replace '//' with '/', etc. | ||
""" | ||
return re.sub(r"\/{2,}", __SEP, path) | ||
|
||
def prepend_slash(path: str): | ||
""" | ||
Append a single forward slash if one does not already exist. | ||
""" | ||
if not path.startswith(__SEP): | ||
path = __SEP + path | ||
return path |
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains 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
This file contains 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