Skip to content

Commit

Permalink
Merge pull request #246 from Lightbug-HQ/fix/docker-crash
Browse files Browse the repository at this point in the history
Fix server crashing with large headers
  • Loading branch information
saviorand authored Feb 23, 2025
2 parents 150928b + 1c2c54d commit 80724a2
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 3,819 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ The only hard dependency for `lightbug_http` is Mojo.
Learn how to get up and running with Mojo on the [Modular website](https://www.modular.com/max/mojo).
Once you have a Mojo project set up locally,

1. Add the `modular-community` channel to your `mojoproject.toml`, e.g:
1. Add the `mojo-community` channel to your `mojoproject.toml`, e.g:

```toml
[project]
channels = ["conda-forge", "https://conda.modular.com/max", "https://repo.prefix.dev/modular-community"]
channels = ["conda-forge", "https://conda.modular.com/max", "https://repo.prefix.dev/mojo-community"]
```

2. Add `lightbug_http` as a dependency:

```toml
[dependencies]
lightbug_http = ">=0.1.12"
lightbug_http = ">=0.1.13"
```

3. Run `magic install` at the root of your project, where `mojoproject.toml` is located
Expand Down
78 changes: 39 additions & 39 deletions lightbug_http/_libc.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ fn htonl(hostlong: c_uint) -> c_uint:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/htonl.3p.html.
* Reference: https://man7.org/linux/man-pages/man3/htonl.3p.html .
"""
return external_call["htonl", c_uint, c_uint](hostlong)

Expand All @@ -373,7 +373,7 @@ fn htons(hostshort: c_ushort) -> c_ushort:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/htonl.3p.html.
* Reference: https://man7.org/linux/man-pages/man3/htonl.3p.html .
"""
return external_call["htons", c_ushort, c_ushort](hostshort)

Expand All @@ -393,7 +393,7 @@ fn ntohl(netlong: c_uint) -> c_uint:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/htonl.3p.html
* Reference: https://man7.org/linux/man-pages/man3/htonl.3p.html .
"""
return external_call["ntohl", c_uint, c_uint](netlong)

Expand All @@ -413,7 +413,7 @@ fn ntohs(netshort: c_ushort) -> c_ushort:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/htonl.3p.html
* Reference: https://man7.org/linux/man-pages/man3/htonl.3p.html .
"""
return external_call["ntohs", c_ushort, c_ushort](netshort)

Expand Down Expand Up @@ -441,7 +441,7 @@ fn _inet_ntop(
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/inet_ntop.3p.html.
* Reference: https://man7.org/linux/man-pages/man3/inet_ntop.3p.html .
"""
return external_call[
"inet_ntop",
Expand Down Expand Up @@ -479,7 +479,7 @@ fn inet_ntop[
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/inet_ntop.3p.html.
* Reference: https://man7.org/linux/man-pages/man3/inet_ntop.3p.html .
"""
constrained[
Int(address_family) in [AF_INET, AF_INET6], "Address family must be either INET_ADDRSTRLEN or INET6_ADDRSTRLEN."
Expand Down Expand Up @@ -537,7 +537,7 @@ fn _inet_pton(af: c_int, src: UnsafePointer[c_char], dst: UnsafePointer[c_void])
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/inet_ntop.3p.html
* Reference: https://man7.org/linux/man-pages/man3/inet_ntop.3p.html .
"""
return external_call[
"inet_pton",
Expand Down Expand Up @@ -570,7 +570,7 @@ fn inet_pton[address_family: Int32](src: UnsafePointer[c_char]) raises -> c_uint
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/inet_ntop.3p.html
* Reference: https://man7.org/linux/man-pages/man3/inet_ntop.3p.html .
* This function is valid for `AF_INET` and `AF_INET6`.
"""
constrained[Int(address_family) in [AF_INET, AF_INET6], "Address family must be either AF_INET or AF_INET6."]()
Expand Down Expand Up @@ -609,7 +609,7 @@ fn _socket(domain: c_int, type: c_int, protocol: c_int) -> c_int:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/socket.3p.html
* Reference: https://man7.org/linux/man-pages/man3/socket.3p.html .
"""
return external_call["socket", c_int, c_int, c_int, c_int](domain, type, protocol)

Expand Down Expand Up @@ -641,7 +641,7 @@ fn socket(domain: c_int, type: c_int, protocol: c_int) raises -> c_int:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/socket.3p.html
* Reference: https://man7.org/linux/man-pages/man3/socket.3p.html .
"""
var fd = _socket(domain, type, protocol)
if fd == -1:
Expand Down Expand Up @@ -707,7 +707,7 @@ fn _setsockopt[
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/setsockopt.3p.html
* Reference: https://man7.org/linux/man-pages/man3/setsockopt.3p.html .
"""
return external_call[
"setsockopt",
Expand Down Expand Up @@ -748,7 +748,7 @@ fn setsockopt(
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/setsockopt.3p.html
* Reference: https://man7.org/linux/man-pages/man3/setsockopt.3p.html .
"""
var result = _setsockopt(socket, level, option_name, Pointer.address_of(option_value), sizeof[Int]())
if result == -1:
Expand Down Expand Up @@ -796,7 +796,7 @@ fn _getsockopt[
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/setsockopt.3p.html
* Reference: https://man7.org/linux/man-pages/man3/setsockopt.3p.html .
"""
return external_call[
"getsockopt",
Expand Down Expand Up @@ -838,7 +838,7 @@ fn getsockopt(
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/getsockopt.3p.html
* Reference: https://man7.org/linux/man-pages/man3/getsockopt.3p.html .
"""
var option_value = stack_allocation[1, c_void]()
var option_len: socklen_t = sizeof[Int]()
Expand Down Expand Up @@ -882,7 +882,7 @@ fn _getsockname[
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/getsockname.3p.html
* Reference: https://man7.org/linux/man-pages/man3/getsockname.3p.html .
"""
return external_call[
"getsockname",
Expand Down Expand Up @@ -917,7 +917,7 @@ fn getsockname[
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/getsockname.3p.html
* Reference: https://man7.org/linux/man-pages/man3/getsockname.3p.html .
"""
var result = _getsockname(socket, address, address_len)
if result == -1:
Expand Down Expand Up @@ -957,7 +957,7 @@ fn _getpeername[
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man2/getpeername.2.html
* Reference: https://man7.org/linux/man-pages/man2/getpeername.2.html .
"""
return external_call[
"getpeername",
Expand Down Expand Up @@ -989,7 +989,7 @@ fn getpeername(file_descriptor: c_int) raises -> sockaddr_in:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man2/getpeername.2.html
* Reference: https://man7.org/linux/man-pages/man2/getpeername.2.html .
"""
var remote_address = stack_allocation[1, sockaddr]()
var result = _getpeername(file_descriptor, remote_address, Pointer.address_of(socklen_t(sizeof[sockaddr]())))
Expand Down Expand Up @@ -1033,7 +1033,7 @@ fn _bind[origin: MutableOrigin](socket: c_int, address: Pointer[sockaddr_in, ori
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/bind.3p.html
* Reference: https://man7.org/linux/man-pages/man3/bind.3p.html .
"""
return external_call["bind", c_int, c_int, Pointer[sockaddr_in, origin], socklen_t](socket, address, address_len)

Expand Down Expand Up @@ -1071,7 +1071,7 @@ fn bind(socket: c_int, mut address: sockaddr_in) raises:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/bind.3p.html
* Reference: https://man7.org/linux/man-pages/man3/bind.3p.html .
"""
var result = _bind(socket, Pointer.address_of(address), sizeof[sockaddr_in]())
if result == -1:
Expand Down Expand Up @@ -1129,7 +1129,7 @@ fn _listen(socket: c_int, backlog: c_int) -> c_int:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/listen.3p.html
* Reference: https://man7.org/linux/man-pages/man3/listen.3p.html .
"""
return external_call["listen", c_int, c_int, c_int](socket, backlog)

Expand All @@ -1154,7 +1154,7 @@ fn listen(socket: c_int, backlog: c_int) raises:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/listen.3p.html
* Reference: https://man7.org/linux/man-pages/man3/listen.3p.html .
"""
var result = _listen(socket, backlog)
if result == -1:
Expand Down Expand Up @@ -1190,7 +1190,7 @@ fn _accept[
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/accept.3p.html
* Reference: https://man7.org/linux/man-pages/man3/accept.3p.html .
"""
return external_call[
"accept", c_int, c_int, Pointer[sockaddr, address_origin], Pointer[socklen_t, len_origin] # FnName, RetType
Expand Down Expand Up @@ -1227,7 +1227,7 @@ fn accept(socket: c_int) raises -> c_int:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/accept.3p.html
* Reference: https://man7.org/linux/man-pages/man3/accept.3p.html .
"""
var remote_address = sockaddr()
var result = _accept(socket, Pointer.address_of(remote_address), Pointer.address_of(socklen_t(sizeof[socklen_t]())))
Expand Down Expand Up @@ -1293,7 +1293,7 @@ fn _connect[origin: Origin](socket: c_int, address: Pointer[sockaddr_in, origin]
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/connect.3p.html
* Reference: https://man7.org/linux/man-pages/man3/connect.3p.html .
"""
return external_call["connect", c_int](socket, address, address_len)

Expand Down Expand Up @@ -1328,7 +1328,7 @@ fn connect(socket: c_int, address: sockaddr_in) raises:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/connect.3p.html
* Reference: https://man7.org/linux/man-pages/man3/connect.3p.html .
"""
var result = _connect(socket, Pointer.address_of(address), sizeof[sockaddr_in]())
if result == -1:
Expand Down Expand Up @@ -1402,7 +1402,7 @@ fn _recv(
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/recv.3p.html
* Reference: https://man7.org/linux/man-pages/man3/recv.3p.html .
"""
return external_call[
"recv",
Expand Down Expand Up @@ -1437,7 +1437,7 @@ fn recv(
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/recv.3p.html
* Reference: https://man7.org/linux/man-pages/man3/recv.3p.html .
"""
var result = _recv(socket, buffer, length, flags)
if result == -1:
Expand Down Expand Up @@ -1504,7 +1504,7 @@ fn _recvfrom[
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/recvfrom.3p.html
* Reference: https://man7.org/linux/man-pages/man3/recvfrom.3p.html .
* Valid Flags:
* `MSG_PEEK`: Peeks at an incoming message. The data is treated as unread and the next recvfrom() or similar function shall still return this data.
* `MSG_OOB`: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific.
Expand Down Expand Up @@ -1550,7 +1550,7 @@ fn recvfrom(
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/recvfrom.3p.html
* Reference: https://man7.org/linux/man-pages/man3/recvfrom.3p.html .
* Valid Flags:
* `MSG_PEEK`: Peeks at an incoming message. The data is treated as unread and the next recvfrom() or similar function shall still return this data.
* `MSG_OOB`: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific.
Expand Down Expand Up @@ -1610,7 +1610,7 @@ fn _send(socket: c_int, buffer: UnsafePointer[c_void], length: c_size_t, flags:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/send.3p.html
* Reference: https://man7.org/linux/man-pages/man3/send.3p.html .
"""
return external_call["send", c_ssize_t](socket, buffer, length, flags)

Expand Down Expand Up @@ -1652,7 +1652,7 @@ fn send(socket: c_int, buffer: UnsafePointer[c_void], length: c_size_t, flags: c
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/send.3p.html
* Reference: https://man7.org/linux/man-pages/man3/send.3p.html .
"""
var result = _send(socket, buffer, length, flags)
if result == -1:
Expand Down Expand Up @@ -1746,7 +1746,7 @@ fn _sendto(
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/sendto.3p.html
* Reference: https://man7.org/linux/man-pages/man3/sendto.3p.html .
* Valid Flags:
* `MSG_EOR`: Terminates a record (if supported by the protocol).
* `MSG_OOB`: Sends out-of-band data on sockets that support out-of-band data. The significance and semantics of out-of-band data are protocol-specific.
Expand Down Expand Up @@ -1804,7 +1804,7 @@ fn sendto(
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/sendto.3p.html
* Reference: https://man7.org/linux/man-pages/man3/sendto.3p.html .
* Valid Flags:
* `MSG_EOR`: Terminates a record (if supported by the protocol).
* `MSG_OOB`: Sends out-of-band data on sockets that support out-of-band data. The significance and semantics of out-of-band data are protocol-specific.
Expand Down Expand Up @@ -1880,7 +1880,7 @@ fn _shutdown(socket: c_int, how: c_int) -> c_int:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/shutdown.3p.html
* Reference: https://man7.org/linux/man-pages/man3/shutdown.3p.html .
"""
return external_call["shutdown", c_int, c_int, c_int](socket, how)

Expand Down Expand Up @@ -1911,7 +1911,7 @@ fn shutdown(socket: c_int, how: c_int) raises:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/shutdown.3p.html
* Reference: https://man7.org/linux/man-pages/man3/shutdown.3p.html .
"""
var result = _shutdown(socket, how)
if result == -1:
Expand Down Expand Up @@ -1946,7 +1946,7 @@ fn gai_strerror(ecode: c_int) -> UnsafePointer[c_char]:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/gai_strerror.3p.html
* Reference: https://man7.org/linux/man-pages/man3/gai_strerror.3p.html .
"""
return external_call["gai_strerror", UnsafePointer[c_char], c_int](ecode)

Expand All @@ -1973,7 +1973,7 @@ fn _close(fildes: c_int) -> c_int:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/close.3p.html
* Reference: https://man7.org/linux/man-pages/man3/close.3p.html .
"""
return external_call["close", c_int, c_int](fildes)

Expand Down Expand Up @@ -2006,7 +2006,7 @@ fn close(file_descriptor: c_int) raises:
```
#### Notes:
* Reference: https://man7.org/linux/man-pages/man3/close.3p.html
* Reference: https://man7.org/linux/man-pages/man3/close.3p.html .
"""
if _close(file_descriptor) == -1:
var errno = get_errno()
Expand Down
4 changes: 3 additions & 1 deletion lightbug_http/cookie/cookie.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ struct Cookie(CollectionElement):
if v:
header_value.write(Cookie.SEPERATOR, Cookie.EXPIRES, Cookie.EQUAL, v.value())
if self.max_age:
header_value.write(Cookie.SEPERATOR, Cookie.MAX_AGE, Cookie.EQUAL, String(self.max_age.value().total_seconds))
header_value.write(
Cookie.SEPERATOR, Cookie.MAX_AGE, Cookie.EQUAL, String(self.max_age.value().total_seconds)
)
if self.domain:
header_value.write(Cookie.SEPERATOR, Cookie.DOMAIN, Cookie.EQUAL, self.domain.value())
if self.path:
Expand Down
1 change: 1 addition & 0 deletions lightbug_http/http/common_response.mojo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from lightbug_http.io.bytes import Bytes


fn OK(body: String, content_type: String = "text/plain") -> HTTPResponse:
return HTTPResponse(
headers=Headers(Header(HeaderKey.CONTENT_TYPE, content_type)),
Expand Down
Loading

0 comments on commit 80724a2

Please sign in to comment.