Skip to content

Commit

Permalink
Merge pull request #204 from czasg/fix/keepalive
Browse files Browse the repository at this point in the history
fix: 修复keepalive错误
  • Loading branch information
czasg authored Dec 12, 2023
2 parents ba63ffd + a153d4f commit 278f5c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
5 changes: 3 additions & 2 deletions pywss/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,10 @@ def openapi(

def handler_request(self, request: socket.socket, address: tuple) -> None:
log = self.log
keepalive = True
try:
rfd = request.makefile("rb", -1)
while True:
while keepalive:
http_method, http_url, http_version, err = parse_request_line(rfd)
if err:
request.sendall(b"HTTP/1.1 400 BadRequest\r\n")
Expand All @@ -650,7 +651,7 @@ def handler_request(self, request: socket.socket, address: tuple) -> None:
return
# check keep alive
if http_headers.get(HeaderConnection, "").lower() == "close":
break
keepalive = False
# full match
handlers = self.full_match_routes.get(method_route, None)
# parser match
Expand Down
12 changes: 5 additions & 7 deletions pywss/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ def __init__(self, app):
self.method = None
self.path = None
self.body = None
self.headers = dict()
self.headers = {
"Pywss-Http-Test": "0.0.1",
"Connection": "close",
}
self.app.build()

def request(self, method, route, headers=None, json=None, data=""):
self.method = method
self.path = route
self.set_header("Pywss-Http-Test", "0.0.1")
self.set_headers(headers or {})
if json:
self.set_headers({"Content-Type": "application/json"})
Expand All @@ -58,10 +60,7 @@ def request(self, method, route, headers=None, json=None, data=""):
return self.build()

def set_header(self, k, v):
header = []
for key in k.split("-"):
header.append(key[0].upper() + key[1:].lower())
self.headers["-".join(header)] = v
self.headers[k.title()] = v
return self

def set_headers(self, headers):
Expand All @@ -81,7 +80,6 @@ def build(self) -> HttpTestResponse:
s, c = socket.socketpair()
with s, c:
c.sendall(req_message.encode())
c.sendall("GET / HTTP/1.1\r\nConnection: close\r\n\r\n".encode())
self.app.handler_request(s, None)
resp = c.makefile("rb", -1)
return HttpTestResponse(resp.readlines())
Expand Down

0 comments on commit 278f5c8

Please sign in to comment.