Skip to content

Commit

Permalink
Merge pull request #78 from saviorand/chore/update-24-6
Browse files Browse the repository at this point in the history
update to Mojo 24.6
  • Loading branch information
saviorand authored Dec 29, 2024
2 parents ea897f7 + a7ff98f commit 875657d
Show file tree
Hide file tree
Showing 21 changed files with 6,325 additions and 1,325 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Once you have a Mojo project set up locally,

```toml
[dependencies]
lightbug_http = ">=0.1.5"
lightbug_http = ">=0.1.6"
```

3. Run `magic install` at the root of your project, where `mojoproject.toml` is located
Expand Down
2 changes: 1 addition & 1 deletion bench.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main():

fn run_benchmark():
try:
var config = BenchConfig(warmup_iters=100)
var config = BenchConfig()
config.verbose_timing = True
config.tabular_view = True
var m = Bench(config)
Expand Down
5 changes: 2 additions & 3 deletions client.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ from lightbug_http.client import Client


fn test_request(inout client: Client) raises -> None:
var uri = URI.parse_raises("http://google.com")
var headers = Headers(Header("Host", "google.com"), Header("User-Agent", "curl/8.1.2"), Header("Accept", "*/*"))

var uri = URI.parse_raises("http://httpbin.org/status/404")
var headers = Headers(Header("Host", "httpbin.org"))
var request = HTTPRequest(uri, headers)
var response = client.do(request^)

Expand Down
2 changes: 1 addition & 1 deletion integration_test_server.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct IntegerationTestService(HTTPService):
var p = req.uri.path
if p == "/redirect":
return HTTPResponse(
"get off my lawn".as_bytes_slice(),
"get off my lawn".as_bytes(),
headers=Headers(
Header(HeaderKey.LOCATION, "/rd-destination")
),
Expand Down
5 changes: 2 additions & 3 deletions lightbug_http/cookie/expiration.mojo
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
alias HTTP_DATE_FORMAT = "ddd, DD MMM YYYY HH:mm:ss ZZZ"
alias TZ_GMT = TimeZone(0, "GMT")

from small_time import SmallTime

alias HTTP_DATE_FORMAT = "ddd, DD MMM YYYY HH:mm:ss ZZZ"
alias TZ_GMT = TimeZone(0, "GMT")

@value
struct Expiration(CollectionElement):
Expand Down
4 changes: 2 additions & 2 deletions lightbug_http/cookie/request_cookie_jar.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ from lightbug_http.utils import ByteReader, ByteWriter, is_newline, is_space


@value
struct RequestCookieJar(Formattable, Stringable):
struct RequestCookieJar(Writable, Stringable):
var _inner: Dict[String, String]

fn __init__(inout self):
Expand Down Expand Up @@ -71,7 +71,7 @@ struct RequestCookieJar(Formattable, Stringable):
if header:
write_header(writer, header.value().key, header.value().value)

fn format_to(self, inout writer: Formatter):
fn write_to[T: Writer](self, inout writer: T):
var header = self.to_header()
if header:
write_header(writer, header.value().key, header.value().value)
Expand Down
4 changes: 2 additions & 2 deletions lightbug_http/cookie/response_cookie_jar.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct ResponseCookieKey(KeyElement):


@value
struct ResponseCookieJar(Formattable, Stringable):
struct ResponseCookieJar(Writable, Stringable):
var _inner: Dict[ResponseCookieKey, Cookie]

fn __init__(inout self):
Expand Down Expand Up @@ -99,7 +99,7 @@ struct ResponseCookieJar(Formattable, Stringable):
var v = cookie[].build_header_value()
write_header(writer, HeaderKey.SET_COOKIE, v)

fn format_to(self, inout writer: Formatter):
fn write_to[T: Writer](self, inout writer: T):
for cookie in self._inner.values():
var v = cookie[].build_header_value()
write_header(writer, HeaderKey.SET_COOKIE, v)
6 changes: 3 additions & 3 deletions lightbug_http/header.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Header:


@always_inline
fn write_header(inout writer: Formatter, key: String, value: String):
fn write_header[T: Writer](inout writer: T, key: String, value: String):
writer.write(key + ": ", value, lineBreak)


Expand All @@ -40,7 +40,7 @@ fn write_header(inout writer: ByteWriter, key: String, inout value: String):


@value
struct Headers(Formattable, Stringable):
struct Headers(Writable, Stringable):
"""Represents the header key/values in an http request/response.
Header keys are normalized to lowercase
Expand Down Expand Up @@ -108,7 +108,7 @@ struct Headers(Formattable, Stringable):
self._inner[k] = to_string(value^)
return (to_string(first^), to_string(second^), to_string(third^), cookies)

fn format_to(self, inout writer: Formatter):
fn write_to[T: Writer](self, inout writer: T):
for header in self._inner.items():
write_header(writer, header[].key, header[].value)

Expand Down
8 changes: 4 additions & 4 deletions lightbug_http/http/request.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ from lightbug_http.strings import (


@value
struct HTTPRequest(Formattable, Stringable):
struct HTTPRequest(Writable, Stringable):
var headers: Headers
var cookies: RequestCookieJar
var uri: URI
Expand Down Expand Up @@ -106,7 +106,7 @@ struct HTTPRequest(Formattable, Stringable):
r.consume(self.body_raw, content_length)
self.set_content_length(content_length)

fn format_to(self, inout writer: Formatter):
fn write_to[T: Writer](self, inout writer: T):
writer.write(self.method, whitespace)
path = self.uri.path if len(self.uri.path) > 1 else strSlash
if len(self.uri.query_string) > 0:
Expand All @@ -120,8 +120,8 @@ struct HTTPRequest(Formattable, Stringable):
lineBreak,
)

self.headers.format_to(writer)
self.cookies.format_to(writer)
self.headers.write_to(writer)
self.cookies.write_to(writer)
writer.write(lineBreak)
writer.write(to_string(self.body_raw))

Expand Down
8 changes: 4 additions & 4 deletions lightbug_http/http/response.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct StatusCode:


@value
struct HTTPResponse(Formattable, Stringable):
struct HTTPResponse(Writable, Stringable):
var headers: Headers
var cookies: ResponseCookieJar
var body_raw: Bytes
Expand Down Expand Up @@ -161,14 +161,14 @@ struct HTTPResponse(Formattable, Stringable):
self.set_content_length(self.content_length() + len(data))
self.body_raw += data

fn format_to(self, inout writer: Formatter):
fn write_to[T: Writer](self, inout writer: T):
writer.write(self.protocol, whitespace, self.status_code, whitespace, self.status_text, lineBreak)

if HeaderKey.SERVER not in self.headers:
writer.write("server: lightbug_http", lineBreak)

self.headers.format_to(writer)
self.cookies.format_to(writer)
self.headers.write_to(writer)
self.cookies.write_to(writer)

writer.write(lineBreak)
writer.write(to_string(self.body_raw))
Expand Down
6 changes: 3 additions & 3 deletions lightbug_http/io/bytes.mojo
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from python import PythonObject
from lightbug_http.strings import nChar, rChar, to_string
from memory import UnsafePointer

alias Byte = UInt8
alias Bytes = List[Byte, True]
alias Bytes = List[Byte]


@always_inline
Expand Down Expand Up @@ -56,5 +56,5 @@ struct UnsafeString:
self.len = l

fn to_string(self) -> String:
var s = String(self.data, self.len)
var s = String(ptr=self.data, length=self.len)
return s
6 changes: 3 additions & 3 deletions lightbug_http/libc.mojo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from utils import StaticTuple
from sys.ffi import external_call
from sys.info import sizeof
from memory import memcpy
from memory import memcpy, UnsafePointer
from lightbug_http.io.bytes import Bytes

alias IPPROTO_IPV6 = 41
Expand Down Expand Up @@ -97,7 +97,7 @@ fn to_char_ptr(s: Bytes) -> UnsafePointer[c_char]:


fn c_charptr_to_string(s: UnsafePointer[c_char]) -> String:
return String(s.bitcast[UInt8](), strlen(s))
return String(ptr=s.bitcast[UInt8](), length=strlen(s))


fn cftob(val: c_int) -> Bool:
Expand Down Expand Up @@ -631,7 +631,7 @@ fn accept(
](socket, address, address_len)


fn connect(socket: c_int, address: Reference[sockaddr], address_len: socklen_t) -> c_int:
fn connect(socket: c_int, address: Pointer[sockaddr], address_len: socklen_t) -> c_int:
"""Libc POSIX `connect` function
Reference: https://man7.org/linux/man-pages/man3/connect.3p.html
Fn signature: int connect(int socket, const struct sockaddr *address, socklen_t address_len).
Expand Down
Loading

0 comments on commit 875657d

Please sign in to comment.