Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

24.5 updates #58

Merged
merged 22 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@
*.mojopkg
.DS_Store
.mojoenv
install_id
install_id

# pixi environments
.pixi
*.egg-info

# magic environments
.magic

# Rattler
output
2 changes: 0 additions & 2 deletions lightbug_http/.gitattributes

This file was deleted.

5 changes: 0 additions & 5 deletions lightbug_http/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion lightbug_http/error.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ from lightbug_http.io.bytes import bytes
@value
struct ErrorHandler:
fn Error(self) -> HTTPResponse:
return HTTPResponse(ResponseHeader(), bytes("TODO"))
return HTTPResponse(ResponseHeader(), "TODO".as_bytes_slice())
346 changes: 180 additions & 166 deletions lightbug_http/header.mojo

Large diffs are not rendered by default.

56 changes: 33 additions & 23 deletions lightbug_http/http.mojo
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
from time import now
from utils.string_slice import StringSlice
from external.morrow import Morrow
from utils import Span
# from external.morrow import Morrow
from gojo.strings.builder import StringBuilder
from gojo.bufio import Reader
from lightbug_http.uri import URI
from lightbug_http.io.bytes import Bytes, BytesView, bytes
from lightbug_http.io.bytes import Bytes, bytes
from lightbug_http.header import RequestHeader, ResponseHeader
from lightbug_http.io.sync import Duration
from lightbug_http.net import Addr, TCPAddr
from lightbug_http.strings import strHttp11, strHttp, strSlash, whitespace, rChar, nChar


alias OK_MESSAGE = String("OK").as_bytes()
alias NOT_FOUND_MESSAGE = String("Not Found").as_bytes()
alias TEXT_PLAIN_CONTENT_TYPE = String("text/plain").as_bytes()

trait Request:
fn __init__(inout self, uri: URI):
...
Expand Down Expand Up @@ -124,8 +130,8 @@ struct HTTPRequest(Request):
self.timeout = timeout
self.disable_redirect_path_normalization = disable_redirect_path_normalization

fn get_body_bytes(self) -> BytesView:
return BytesView(unsafe_ptr=self.body_raw.unsafe_ptr(), len=self.body_raw.size)
fn get_body_bytes(self) -> Span[UInt8, __lifetime_of(self)]:
return Span[UInt8, __lifetime_of(self)](self.body_raw)

fn set_body_bytes(inout self, body: Bytes) -> Self:
self.body_raw = body
Expand Down Expand Up @@ -190,8 +196,8 @@ struct HTTPResponse(Response):
fn __init__(inout self, body_bytes: Bytes):
self.header = ResponseHeader(
200,
bytes("OK"),
bytes("application/octet-stream"),
OK_MESSAGE,
"application/octet-stream".as_bytes_slice(),
thatstoasty marked this conversation as resolved.
Show resolved Hide resolved
)
self.stream_immediate_header_flush = False
self.stream_body = False
Expand All @@ -209,8 +215,8 @@ struct HTTPResponse(Response):
self.raddr = TCPAddr()
self.laddr = TCPAddr()

fn get_body_bytes(self) -> BytesView:
return BytesView(unsafe_ptr=self.body_raw.unsafe_ptr(), len=self.body_raw.size - 1)
fn get_body_bytes(self) -> Span[UInt8, __lifetime_of(self)]:
return Span[UInt8, __lifetime_of(self)](self.body_raw)

fn get_body(self) -> Bytes:
return self.body_raw
Expand All @@ -237,51 +243,50 @@ struct HTTPResponse(Response):
_ = r.discard(header_len)

var body_buf_result = r.peek(r.buffered())
var body_buf = body_buf_result[0]

_ = self.set_body_bytes(body_buf)
_ = self.set_body_bytes(body_buf_result[0])

fn OK(body: StringLiteral) -> HTTPResponse:
return HTTPResponse(
ResponseHeader(200, bytes("OK"), bytes("text/plain")), bytes(body, pop=False),
ResponseHeader(200, OK_MESSAGE, TEXT_PLAIN_CONTENT_TYPE), body.as_bytes_slice(),
)

fn OK(body: StringLiteral, content_type: String) -> HTTPResponse:
return HTTPResponse(
ResponseHeader(200, bytes("OK"), bytes(content_type)), bytes(body, pop=False),
ResponseHeader(200, OK_MESSAGE, content_type.as_bytes()), body.as_bytes_slice(),
)

fn OK(body: String) -> HTTPResponse:
return HTTPResponse(
ResponseHeader(200, bytes("OK"), bytes("text/plain")), bytes(body, pop=False),
ResponseHeader(200, OK_MESSAGE, TEXT_PLAIN_CONTENT_TYPE), body.as_bytes(),
)

fn OK(body: String, content_type: String) -> HTTPResponse:
return HTTPResponse(
ResponseHeader(200, bytes("OK"), bytes(content_type)), bytes(body, pop=False),
ResponseHeader(200, OK_MESSAGE, content_type.as_bytes()), body.as_bytes(),
)

fn OK(body: Bytes) -> HTTPResponse:
return HTTPResponse(
ResponseHeader(200, bytes("OK"), bytes("text/plain")), body,
ResponseHeader(200, OK_MESSAGE, TEXT_PLAIN_CONTENT_TYPE), body,
)

fn OK(body: Bytes, content_type: String) -> HTTPResponse:
return HTTPResponse(
ResponseHeader(200, bytes("OK"), bytes(content_type)), body,
ResponseHeader(200, OK_MESSAGE, content_type.as_bytes()), body,
)

fn OK(body: Bytes, content_type: String, content_encoding: String) -> HTTPResponse:
return HTTPResponse(
ResponseHeader(200, bytes("OK"), bytes(content_type), bytes(content_encoding)), body,
ResponseHeader(200, OK_MESSAGE, content_type.as_bytes(), content_encoding.as_bytes()), body,
)

fn NotFound(path: String) -> HTTPResponse:
return HTTPResponse(
ResponseHeader(404, bytes("Not Found"), bytes("text/plain")), bytes("path " + path + " not found"),
ResponseHeader(404, NOT_FOUND_MESSAGE, TEXT_PLAIN_CONTENT_TYPE), ("path " + path + " not found").as_bytes(),
)

fn encode(req: HTTPRequest) raises -> StringSlice[is_mutable=False, lifetime=ImmutableStaticLifetime]:
fn encode(req: HTTPRequest) -> Bytes:
var builder = StringBuilder()

_ = builder.write(req.header.method())
Expand Down Expand Up @@ -329,10 +334,11 @@ fn encode(req: HTTPRequest) raises -> StringSlice[is_mutable=False, lifetime=Imm
if len(req.body_raw) > 0:
_ = builder.write(req.get_body_bytes())

return StringSlice[is_mutable=False, lifetime=ImmutableStaticLifetime](unsafe_from_utf8_ptr=builder.render().unsafe_ptr(), len=builder.__len__())
# TODO: Might want to avoid creating a string then copying the bytes
return str(builder).as_bytes()


fn encode(res: HTTPResponse) raises -> Bytes:
fn encode(res: HTTPResponse) -> Bytes:
# var current_time = String()
# try:
# current_time = Morrow.utcnow().__str__()
Expand Down Expand Up @@ -370,7 +376,7 @@ fn encode(res: HTTPResponse) raises -> Bytes:

if len(res.body_raw) > 0:
_ = builder.write_string("Content-Length: ")
_ = builder.write_string((len(res.body_raw) + 1).__str__())
_ = builder.write_string(str(len(res.body_raw)))
_ = builder.write_string(rChar)
_ = builder.write_string(nChar)
else:
Expand All @@ -397,9 +403,13 @@ fn encode(res: HTTPResponse) raises -> Bytes:
if len(res.body_raw) > 0:
_ = builder.write(res.get_body_bytes())

return builder.as_string_slice().as_bytes_slice()
# TODO: Might want to avoid creating a string then copying the bytes
return str(builder).as_bytes()


# TODO: Maybe remove this function? Not being used.
thatstoasty marked this conversation as resolved.
Show resolved Hide resolved
fn split_http_string(buf: Bytes) raises -> (String, String, String):

var request = String(buf)

var request_first_line_headers_body = request.split("\r\n\r\n")
Expand Down
6 changes: 2 additions & 4 deletions lightbug_http/io/bytes.mojo
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from python import PythonObject
from utils.span import Span
from lightbug_http.strings import nChar, rChar

alias Byte = UInt8
alias Bytes = List[Byte, True]
alias BytesView = Span[is_mutable=False, T=Byte, lifetime=ImmutableStaticLifetime]

fn bytes(s: StringLiteral, pop: Bool = True) -> Bytes:
var buf = String(s)._buffer
Expand Down Expand Up @@ -42,11 +40,11 @@ fn compare_case_insensitive(a: Bytes, b: Bytes) -> Bool:
return True

fn next_line(b: Bytes) raises -> (Bytes, Bytes):
var n_next = index_byte(b, bytes(nChar, pop=False)[0])
var n_next = index_byte(b, nChar.as_bytes_slice()[0])
if n_next < 0:
raise Error("next_line: newline not found")
var n = n_next
if n > 0 and (b[n-1] == bytes(rChar, pop=False)[0]):
if n > 0 and (b[n-1] == rChar.as_bytes_slice()[0]):
n -= 1
return (b[:n+1], b[n_next+1:])

Expand Down
Loading