Skip to content

Commit 8d89b4b

Browse files
authored
Merge pull request #261 from thomas-huegel/main
add SeeOther among common HTTP responses
2 parents 508f421 + ab7bf4a commit 8d89b4b

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

lightbug_http/__init__.mojo

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
from lightbug_http.http import HTTPRequest, HTTPResponse, OK, NotFound, StatusCode
1+
from lightbug_http.http import (
2+
HTTPRequest,
3+
HTTPResponse,
4+
OK,
5+
NotFound,
6+
SeeOther,
7+
StatusCode,
8+
)
29
from lightbug_http.uri import URI
310
from lightbug_http.header import Header, Headers, HeaderKey
411
from lightbug_http.cookie import Cookie, RequestCookieJar, ResponseCookieJar

lightbug_http/cookie/response_cookie_jar.mojo

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ struct ResponseCookieKey(KeyElement):
2424
return not (self == other)
2525

2626
fn __eq__(self: Self, other: Self) -> Bool:
27-
return self.name == other.name and self.domain == other.domain and self.path == other.path
27+
return (
28+
self.name == other.name
29+
and self.domain == other.domain
30+
and self.path == other.path
31+
)
2832

2933
fn __moveinit__(out self: Self, owned existing: Self):
3034
self.name = existing.name
@@ -41,7 +45,7 @@ struct ResponseCookieKey(KeyElement):
4145

4246

4347
@value
44-
struct ResponseCookieJar(Writable, Stringable, Copyable, Movable, Sized):
48+
struct ResponseCookieJar(Copyable, Movable, Sized, Stringable, Writable):
4549
var _inner: Dict[ResponseCookieKey, Cookie]
4650

4751
fn __init__(out self):
@@ -52,6 +56,11 @@ struct ResponseCookieJar(Writable, Stringable, Copyable, Movable, Sized):
5256
for cookie in cookies:
5357
self.set_cookie(cookie)
5458

59+
fn __init__(out self, cookies: List[Cookie]):
60+
self._inner = Dict[ResponseCookieKey, Cookie]()
61+
for cookie in cookies:
62+
self.set_cookie(cookie)
63+
5564
@always_inline
5665
fn __setitem__(mut self, key: ResponseCookieKey, value: Cookie):
5766
self._inner[key] = value
@@ -81,7 +90,9 @@ struct ResponseCookieJar(Writable, Stringable, Copyable, Movable, Sized):
8190

8291
@always_inline
8392
fn set_cookie(mut self, cookie: Cookie):
84-
self[ResponseCookieKey(cookie.name, cookie.domain, cookie.path)] = cookie
93+
self[
94+
ResponseCookieKey(cookie.name, cookie.domain, cookie.path)
95+
] = cookie
8596

8697
@always_inline
8798
fn empty(self) -> Bool:

lightbug_http/http/common_response.mojo

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ fn OK(body: Bytes, content_type: String = "text/plain") -> HTTPResponse:
1515
)
1616

1717

18-
fn OK(body: Bytes, content_type: String, content_encoding: String) -> HTTPResponse:
18+
fn OK(
19+
body: Bytes, content_type: String, content_encoding: String
20+
) -> HTTPResponse:
1921
return HTTPResponse(
2022
headers=Headers(
2123
Header(HeaderKey.CONTENT_TYPE, content_type),
@@ -25,27 +27,43 @@ fn OK(body: Bytes, content_type: String, content_encoding: String) -> HTTPRespon
2527
)
2628

2729

30+
fn SeeOther(
31+
location: String, content_type: String, owned cookies: List[Cookie] = []
32+
) -> HTTPResponse:
33+
return HTTPResponse(
34+
bytes("See Other"),
35+
cookies=ResponseCookieJar(cookies^),
36+
headers=Headers(
37+
Header(HeaderKey.LOCATION, location),
38+
Header(HeaderKey.CONTENT_TYPE, content_type),
39+
),
40+
status_code=303,
41+
status_text="See Other",
42+
)
43+
44+
45+
fn BadRequest() -> HTTPResponse:
46+
return HTTPResponse(
47+
bytes("Bad Request"),
48+
headers=Headers(Header(HeaderKey.CONTENT_TYPE, "text/plain")),
49+
status_code=400,
50+
status_text="Bad Request",
51+
)
52+
53+
2854
fn NotFound(path: String) -> HTTPResponse:
2955
return HTTPResponse(
56+
body_bytes=bytes("path " + path + " not found"),
57+
headers=Headers(Header(HeaderKey.CONTENT_TYPE, "text/plain")),
3058
status_code=404,
3159
status_text="Not Found",
32-
headers=Headers(Header(HeaderKey.CONTENT_TYPE, "text/plain")),
33-
body_bytes=bytes("path " + path + " not found"),
3460
)
3561

3662

3763
fn InternalError() -> HTTPResponse:
3864
return HTTPResponse(
3965
bytes("Failed to process request"),
40-
status_code=500,
4166
headers=Headers(Header(HeaderKey.CONTENT_TYPE, "text/plain")),
67+
status_code=500,
4268
status_text="Internal Server Error",
4369
)
44-
45-
fn BadRequest() -> HTTPResponse:
46-
return HTTPResponse(
47-
bytes("Bad Request"),
48-
status_code=400,
49-
headers=Headers(Header(HeaderKey.CONTENT_TYPE, "text/plain")),
50-
status_text="Bad Request",
51-
)

0 commit comments

Comments
 (0)