Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
http-server
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cython-plus
http-server
Commits
94593fe6
Commit
94593fe6
authored
Nov 10, 2021
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add httpserver.pyx demo
parent
4d4e9c9b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
0 deletions
+78
-0
src/httpserver.pyx
src/httpserver.pyx
+78
-0
No files found.
src/httpserver.pyx
0 → 100644
View file @
94593fe6
from
stdlib.string
cimport
Str
from
stdlib.format
cimport
format
from
stdlib.socket
cimport
*
from
stdlib.http
cimport
HTTPRequest
from
stdlib.url
cimport
URI
from
libc.stdio
cimport
puts
,
printf
def
main
():
cdef
Str
recv
,
body
,
resp
cdef
size_t
length
cdef
Str
CRLFCRLF
=
Str
(
"
\
r
\
n
\
r
\
n
"
)
with
nogil
:
a
=
getaddrinfo
(
NULL
,
Str
(
"3490"
),
AF_UNSPEC
,
SOCK_STREAM
,
0
,
AI_PASSIVE
)[
0
]
s
=
socket
(
a
.
family
,
a
.
socktype
,
a
.
protocol
)
s
.
setsockopt
(
SO_REUSEADDR
,
1
)
s
.
bind
(
a
.
sockaddr
)
s
.
listen
(
5
)
printf
(
"listening on http://%s:3490
\
n
"
,
Str
.
to_c_str
(
a
.
sockaddr
.
to_string
()))
loop
=
True
while
loop
:
with
gil
:
try
:
with
nogil
:
s1
=
s
.
accept
()
except
OSError
as
e
:
print
(
e
)
continue
recv
=
s1
.
recvuntil
(
CRLFCRLF
,
1024
)
printf
(
'Received:
\
n
==========
\
n
%s
\
n
==========
\
n
\
n
'
,
Str
.
to_c_str
(
recv
))
request
=
HTTPRequest
(
recv
)
if
request
.
ok
:
status
=
Str
(
"HTTP/1.0 200 OK"
)
content_length_key
=
Str
(
'Content-Length'
)
if
content_length_key
in
request
.
headers
:
content_length_value
=
request
.
headers
[
content_length_key
]
length
=
content_length_value
.
__int__
()
body
=
recv
.
substr
(
request
.
_pos
)
while
body
.
__len__
()
<
length
:
remaining
=
length
-
body
.
__len__
()
body
=
s1
.
recvinto
(
consume
body
,
remaining
)
printf
(
'Received body:
\
n
==========
\
n
%s
\
n
==========
\
n
\
n
'
,
Str
.
to_c_str
(
body
))
resp
=
format
(
"{}
\
r
\
n
Content-Length: {}{}{}"
,
status
,
length
,
CRLFCRLF
,
body
)
if
body
==
Str
(
"quit"
):
loop
=
False
else
:
resp
=
status
+
CRLFCRLF
else
:
if
recv
==
Str
(
'quit'
):
loop
=
False
status
=
Str
(
"HTTP/1.0 418 I'M A TEAPOT"
)
resp
=
status
+
CRLFCRLF
printf
(
"Sending:
\
n
==========
\
n
%s
\
n
==========
\
n
\
n
\
n
"
,
Str
.
to_c_str
(
resp
))
s1
.
sendall
(
resp
)
s1
.
shutdown
(
SHUT_WR
)
s1
.
close
()
s
.
close
()
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment