FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-rest-api/src/parser.py at master · witer33/python-rest-api · GitHub
witer33
/
python-rest-api
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-rest-api
/
src
/
parser.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
91 lines (81 loc) · 3.05 KB
Breadcrumbs
python-rest-api
/
src
/
parser.py
Copy path
File metadata and controls
91 lines (81 loc) · 3.05 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Python Rest API - Pure-Python web server for Rest APIs.
# Copyright (C) 2020-2021 witer33 <https://github.com/witer33>
#
# This file is part of Python Rest API.
#
# Python Rest API is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Python Rest API is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Python Rest API. If not, see <http://www.gnu.org/licenses/>.
from
.
types
import
HttpRequest
class
Parser
:
def
__init__
(
self
,
request
:
str
):
self
.
request
=
request
self
.
pos
=
0
def
advance
(
self
):
self
.
pos
+=
1
if
len
(
self
.
request
)
>=
self
.
pos
:
return
self
.
request
[
self
.
pos
-
1
]
def
parse
(
self
):
char
=
self
.
advance
()
line
=
0
headers
=
{}
method_line
=
""
header_name
=
""
header_value
=
""
in_value
=
False
in_content
=
False
content
=
""
skip
=
False
while
char
:
if
line
!=
0
and
not
in_content
:
if
char
==
"
\r
"
:
self
.
advance
()
if
header_name
!=
""
:
header_name
=
header_name
.
lower
()
line
+=
1
in_value
=
False
headers
[
header_name
]
=
header_value
.
strip
()
header_value
=
""
header_name
=
""
else
:
in_content
=
True
elif
char
==
":"
and
not
in_value
:
in_value
=
True
elif
in_value
:
header_value
+=
char
else
:
header_name
+=
char
elif
in_content
:
content
+=
char
elif
char
==
"
\r
"
:
self
.
advance
()
line
+=
1
else
:
method_line
+=
char
char
=
self
.
advance
()
if
header_name
!=
""
and
not
in_content
:
header_name
=
header_name
.
lower
()
headers
[
header_name
]
=
header_value
.
strip
()
values
=
method_line
.
strip
().
split
(
" "
,
2
)
method
,
path
,
http_version
=
tuple
(
values
)
params
=
path
.
split
(
"?"
,
1
)
if
len
(
params
)
==
2
:
path
=
params
[
0
]
get_params
=
params
[
1
]
get_params_parsed
=
{}
for
param
in
get_params
.
split
(
"&"
):
name
,
value
=
tuple
(
param
.
split
(
"="
))
get_params_parsed
[
name
]
=
value
else
:
get_params_parsed
=
{}
http_version
=
float
(
http_version
.
split
(
"/"
,
1
)[
1
])
return
HttpRequest
(
method
,
path
,
http_version
,
headers
,
content
,
get_params_parsed
)
Back
|
FazBrowse Home
|
New Git URL