Skip to content

Commit 9c57f2d

Browse files
committed
add security, remove reduments, fix routing bugs
1 parent f4fa41e commit 9c57f2d

File tree

13 files changed

+426
-113
lines changed

13 files changed

+426
-113
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
commit f4fa41e4ae4bab3a6f945fc1ebe3271713d71557
2+
Author: Alexeev Bronislav <[email protected]>
3+
Date: Fri May 23 01:49:04 2025 +0700
4+
5+
fix codestyle, add getversion script
6+
7+
commit 91e81b89489a176d2e880156ab2e54c04df18a42
8+
Author: Alexeev Bronislav <[email protected]>
9+
Date: Tue May 6 17:10:42 2025 +0700
10+
11+
feat: start add schemas support
12+
113
commit 6c22c1440b0454a8295c637fe1d07d7848d198c8
214
Author: Alexeev Bronislav <[email protected]>
315
Date: Sat May 3 13:11:31 2025 +0700

SECURITY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Use this section to tell people about which versions of your project are
66
currently being supported with security updates.
77

88
| Version | Supported |
9-
|---------| ------------------ |
9+
|---------|--------------------|
1010
| 0.7.17 | :white_check_mark: |
1111
| 0.7.16 | :white_check_mark: |
1212
| 0.7.15 | :white_check_mark: |
13-
| 0.7.14 | :white_check_mark: |
13+
| 0.7.14 | :x: |
1414
| 0.7.13 | :x: |
1515
| 0.7.12 | :x: |
1616
| 0.7.11 | :x: |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pyechonext.log

examples/simpleapi/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pyechonext.log

examples/simpleapi/app.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ def post(self, request, response, **kwargs):
3535
router = Router(prefix="/users")
3636

3737

38+
@echonext.route_page("/home")
39+
def home(request, response):
40+
return {"request": str(request), "response": str(response)}
41+
42+
3843
@router.route_page("/create", methods=["POST"], summary="create user")
3944
def create_user(request, response):
4045
return {"status": "user created", "username": request.POST.get("username")}

pyechonext/mvc/routes.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,8 @@ def __init__(self, urls: Optional[List[URL]] = None, prefix: Optional[str] = Non
120120
self.prefix = prefix
121121
self.urls = urls
122122
self.routes = {}
123-
124-
self._prepare_urls()
125-
126123
self._trie = PrefixTree()
124+
self._prepare_urls()
127125

128126
def route_page(
129127
self,
@@ -145,7 +143,7 @@ def route_page(
145143
if methods is None:
146144
methods = ["GET"]
147145

148-
def wrapper(handler: PageController | callable):
146+
def wrapper(handler: PageController | Callable):
149147
nonlocal page_path
150148

151149
page_path = (
@@ -166,6 +164,9 @@ def _prepare_urls(self):
166164
Prepare URLs (add to routes)
167165
"""
168166
for url in self.urls:
167+
self._trie.insert(
168+
url.path if self.prefix is None else f"{self.prefix}{url.path}"
169+
)
169170
self.routes[
170171
url.path if self.prefix is None else f"{self.prefix}{url.path}"
171172
] = _create_url_route(url)
@@ -241,6 +242,8 @@ def resolve(
241242

242243
paths = self._trie.starts_with(url)
243244

245+
print(f"get {url}: {self._trie.starts_with('')}")
246+
244247
for path in paths:
245248
route = self.routes.get(path)
246249

pyechonext/response.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from pyechonext.logging import logger
88
from pyechonext.request import Request
9+
from pyechonext.security.defence import Security
910

1011

1112
class Response:
@@ -85,26 +86,28 @@ def __init__(
8586
def __getattr__(self, item: Any) -> Union[Any, None]:
8687
"""Magic method for get attrs (from extra)
8788
88-
Args:
89-
item (Any): item key
89+
Args:
90+
item (Any): item key
9091
91-
Returns:
92-
Union[Any, None]: value
92+
Returns:
93+
Union[Any, None]: value
9394
"""
9495
return self.extra.get(item, None)
9596

9697
def _structuring_headers(self, environ: dict):
9798
"""Structure headers
9899
99-
Args:
100-
environ (dict): environ dictionary
100+
Args:
101+
environ (dict): environ dictionary
101102
"""
102103
headers = {
103104
"Host": environ.get("HTTP_HOST"),
104105
"Accept": environ.get("HTTP_ACCEPT"),
105106
"User-Agent": environ.get("HTTP_USER_AGENT"),
106107
}
107108

109+
headers.update(Security.get_security_headers())
110+
108111
for name, value in headers.items():
109112
self._headerslist.append((name, value))
110113

@@ -121,8 +124,8 @@ def _update_headers(self) -> None:
121124
def add_headers(self, headers: List[Tuple[str, str]]):
122125
"""Adds new headers
123126
124-
Args:
125-
headers (List[Tuple[str, str]]): new headers
127+
Args:
128+
headers (List[Tuple[str, str]]): new headers
126129
"""
127130
for header in headers:
128131
self._added_headers.append(header)
@@ -142,14 +145,14 @@ def _encode_body(self):
142145
def __call__(self, environ: dict, start_response: method) -> Iterable:
143146
"""Makes the response callable.
144147
145-
This method calling another methods for encode body, fill headers and starting response.
148+
This method calling another methods for encode body, fill headers and starting response.
146149
147-
Args:
148-
environ (dict): environ data
149-
start_response (method): start response method
150+
Args:
151+
environ (dict): environ data
152+
start_response (method): start response method
150153
151-
Returns:
152-
Iterable: iterable encoded body
154+
Returns:
155+
Iterable: iterable encoded body
153156
"""
154157
self._encode_body()
155158

@@ -168,8 +171,8 @@ def __call__(self, environ: dict, start_response: method) -> Iterable:
168171
def json(self) -> str | dict[Any, Any]:
169172
"""Get response body as JSON
170173
171-
Returns:
172-
dict: _description_
174+
Returns:
175+
dict: _description_
173176
"""
174177
if self.body:
175178
if isinstance(self.body, str):
@@ -185,7 +188,7 @@ def json(self) -> str | dict[Any, Any]:
185188
def __repr__(self) -> str:
186189
"""Returns a unambiguous string representation of the object (for debug...).
187190
188-
Returns:
189-
str: unambiguous string representation
191+
Returns:
192+
str: unambiguous string representation
190193
"""
191194
return f"<{self.__class__.__name__} at 0x{abs(id(self)):x} {self.status_code}>"

pyechonext/schemas/__init__.py

Whitespace-only changes.

pyechonext/schemas/fields.py

Lines changed: 0 additions & 88 deletions
This file was deleted.

pyechonext/schemas/models.py

Whitespace-only changes.

0 commit comments

Comments
 (0)