Skip to content

Commit 4a5d14a

Browse files
committed
move functions to networktype
1 parent a288456 commit 4a5d14a

File tree

8 files changed

+96
-110
lines changed

8 files changed

+96
-110
lines changed

lightbug_http/address.mojo

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from memory import UnsafePointer
2+
from collections import Optional
23
from sys.ffi import external_call, OpaquePointer
3-
from lightbug_http.strings import NetworkType, to_string
4+
from lightbug_http.strings import to_string
45
from lightbug_http._libc import (
56
c_int,
67
c_char,
@@ -66,6 +67,68 @@ trait AnAddrInfo:
6667
"""
6768
...
6869

70+
@value
71+
struct NetworkType(EqualityComparableCollectionElement):
72+
var value: String
73+
74+
alias empty = NetworkType("")
75+
alias tcp = NetworkType("tcp")
76+
alias tcp4 = NetworkType("tcp4")
77+
alias tcp6 = NetworkType("tcp6")
78+
alias udp = NetworkType("udp")
79+
alias udp4 = NetworkType("udp4")
80+
alias udp6 = NetworkType("udp6")
81+
alias ip = NetworkType("ip")
82+
alias ip4 = NetworkType("ip4")
83+
alias ip6 = NetworkType("ip6")
84+
alias unix = NetworkType("unix")
85+
86+
alias SUPPORTED_TYPES = [
87+
Self.tcp,
88+
Self.tcp4,
89+
Self.tcp6,
90+
Self.udp,
91+
Self.udp4,
92+
Self.udp6,
93+
Self.ip,
94+
Self.ip4,
95+
Self.ip6,
96+
]
97+
alias TCP_TYPES = [
98+
Self.tcp,
99+
Self.tcp4,
100+
Self.tcp6,
101+
]
102+
alias UDP_TYPES = [
103+
Self.udp,
104+
Self.udp4,
105+
Self.udp6,
106+
]
107+
alias IP_TYPES = [
108+
Self.ip,
109+
Self.ip4,
110+
Self.ip6,
111+
]
112+
113+
fn __eq__(self, other: NetworkType) -> Bool:
114+
return self.value == other.value
115+
116+
fn __ne__(self, other: NetworkType) -> Bool:
117+
return self.value != other.value
118+
119+
fn is_ip_protocol(self) -> Bool:
120+
"""Check if the network type is an IP protocol."""
121+
return self in (NetworkType.ip, NetworkType.ip4, NetworkType.ip6)
122+
123+
fn is_ipv4(self) -> Bool:
124+
"""Check if the network type is IPv4."""
125+
print("self.value:", self.value)
126+
return self in (NetworkType.tcp4, NetworkType.udp4, NetworkType.ip4)
127+
128+
fn is_ipv6(self) -> Bool:
129+
"""Check if the network type is IPv6."""
130+
return self in (NetworkType.tcp6, NetworkType.udp6, NetworkType.ip6)
131+
69132
@value
70133
struct TCPAddr[network: NetworkType = NetworkType.tcp4](Addr):
71134
alias _type = "TCPAddr"
@@ -324,10 +387,11 @@ fn resolve_localhost(host: String, network: NetworkType) -> String:
324387
if host != AddressConstants.LOCALHOST:
325388
return host
326389

327-
if is_ipv4(network):
390+
if network.is_ipv4():
328391
return AddressConstants.IPV4_LOCALHOST
329-
elif is_ipv6(network):
392+
elif network.is_ipv6():
330393
return AddressConstants.IPV6_LOCALHOST
394+
331395
return host
332396

333397
fn parse_ipv6_bracketed_address(address: String) raises -> (String, UInt16):
@@ -355,11 +419,18 @@ fn parse_ipv6_bracketed_address(address: String) raises -> (String, UInt16):
355419
UInt16(end_bracket_index + 1)
356420
)
357421

358-
fn validate_no_brackets(address: String, start_idx: Int, end_idx: Int = -1) raises:
422+
fn validate_no_brackets(address: String, start_idx: UInt16, end_idx: Optional[UInt16] = None) raises:
359423
"""Validate that the address segment contains no brackets."""
360-
if address[start_idx:end_idx].find("[") != -1:
424+
var segment: String
425+
426+
if end_idx is None:
427+
segment = address[int(start_idx):]
428+
else:
429+
segment = address[int(start_idx):int(end_idx.value())]
430+
431+
if segment.find("[") != -1:
361432
raise Error("unexpected '[' in address")
362-
if address[start_idx:end_idx].find("]") != -1:
433+
if segment.find("]") != -1:
363434
raise Error("unexpected ']' in address")
364435

365436
fn parse_port(port_str: String) raises -> UInt16:
@@ -383,7 +454,7 @@ fn parse_address(network: NetworkType, address: String) raises -> (String, UInt1
383454
Returns:
384455
Tuple containing the host and port
385456
"""
386-
if is_ip_protocol(network):
457+
if network.is_ip_protocol():
387458
var host = resolve_localhost(address, network)
388459
if host == AddressConstants.EMPTY:
389460
raise Error("missing host")
@@ -403,7 +474,7 @@ fn parse_address(network: NetworkType, address: String) raises -> (String, UInt1
403474
raise MissingPortError
404475

405476
var host: String
406-
var bracket_offset: Int = 0
477+
var bracket_offset: UInt16 = 0
407478

408479
# Handle IPv6 addresses
409480
if address[0] == "[":

lightbug_http/connection.mojo

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from time import sleep
22
from memory import Span
33
from sys.info import os_is_macos
4-
from lightbug_http.strings import NetworkType
4+
from lightbug_http.address import NetworkType
55
from lightbug_http.io.bytes import Bytes, bytes
66
from lightbug_http.io.sync import Duration
77
from lightbug_http.address import parse_address, TCPAddr, UDPAddr
@@ -51,7 +51,6 @@ trait Connection(Movable):
5151
fn remote_addr(self) -> TCPAddr:
5252
...
5353

54-
5554
struct NoTLSListener:
5655
"""A TCP listener that listens for incoming connections and can accept them."""
5756

lightbug_http/http/request.mojo

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ from lightbug_http.strings import (
1717
)
1818

1919

20+
@value
21+
struct RequestMethod:
22+
var value: String
23+
24+
alias get = RequestMethod("GET")
25+
alias post = RequestMethod("POST")
26+
alias put = RequestMethod("PUT")
27+
alias delete = RequestMethod("DELETE")
28+
alias head = RequestMethod("HEAD")
29+
alias patch = RequestMethod("PATCH")
30+
alias options = RequestMethod("OPTIONS")
31+
32+
2033
@value
2134
struct HTTPRequest(Writable, Stringable):
2235
var headers: Headers

lightbug_http/server.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from memory import Span
22
from lightbug_http.io.sync import Duration
33
from lightbug_http.io.bytes import Bytes, bytes, ByteReader
4-
from lightbug_http.strings import NetworkType
4+
from lightbug_http.address import NetworkType
55
from lightbug_http._logger import logger
66
from lightbug_http.connection import NoTLSListener, default_buffer_size, TCPConnection, ListenConfig
77
from lightbug_http.socket import Socket

lightbug_http/socket.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ from lightbug_http._libc import (
4545
ShutdownInvalidArgumentError,
4646
)
4747
from lightbug_http.io.bytes import Bytes
48-
from lightbug_http.strings import NetworkType
4948
from lightbug_http.address import (
49+
NetworkType,
5050
Addr,
5151
binary_port_to_int,
5252
binary_ip_to_string,

lightbug_http/strings.mojo

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -31,102 +31,6 @@ struct BytesConstant:
3131
alias nChar = byte(nChar)
3232

3333

34-
@value
35-
struct NetworkType(EqualityComparableCollectionElement):
36-
var value: String
37-
38-
alias empty = NetworkType("")
39-
alias tcp = NetworkType("tcp")
40-
alias tcp4 = NetworkType("tcp4")
41-
alias tcp6 = NetworkType("tcp6")
42-
alias udp = NetworkType("udp")
43-
alias udp4 = NetworkType("udp4")
44-
alias udp6 = NetworkType("udp6")
45-
alias ip = NetworkType("ip")
46-
alias ip4 = NetworkType("ip4")
47-
alias ip6 = NetworkType("ip6")
48-
alias unix = NetworkType("unix")
49-
50-
alias SUPPORTED_TYPES = [
51-
Self.tcp,
52-
Self.tcp4,
53-
Self.tcp6,
54-
Self.udp,
55-
Self.udp4,
56-
Self.udp6,
57-
Self.ip,
58-
Self.ip4,
59-
Self.ip6,
60-
]
61-
alias TCP_TYPES = [
62-
Self.tcp,
63-
Self.tcp4,
64-
Self.tcp6,
65-
]
66-
alias UDP_TYPES = [
67-
Self.udp,
68-
Self.udp4,
69-
Self.udp6,
70-
]
71-
alias IP_TYPES = [
72-
Self.ip,
73-
Self.ip4,
74-
Self.ip6,
75-
]
76-
77-
fn __eq__(self, other: NetworkType) -> Bool:
78-
return self.value == other.value
79-
80-
fn __ne__(self, other: NetworkType) -> Bool:
81-
return self.value != other.value
82-
83-
84-
@value
85-
struct ConnType:
86-
var value: String
87-
88-
alias empty = ConnType("")
89-
alias http = ConnType("http")
90-
alias websocket = ConnType("websocket")
91-
92-
93-
@value
94-
struct RequestMethod:
95-
var value: String
96-
97-
alias get = RequestMethod("GET")
98-
alias post = RequestMethod("POST")
99-
alias put = RequestMethod("PUT")
100-
alias delete = RequestMethod("DELETE")
101-
alias head = RequestMethod("HEAD")
102-
alias patch = RequestMethod("PATCH")
103-
alias options = RequestMethod("OPTIONS")
104-
105-
106-
@value
107-
struct CharSet:
108-
var value: String
109-
110-
alias utf8 = CharSet("utf-8")
111-
112-
113-
@value
114-
struct MediaType:
115-
var value: String
116-
117-
alias empty = MediaType("")
118-
alias plain = MediaType("text/plain")
119-
alias json = MediaType("application/json")
120-
121-
122-
@value
123-
struct Message:
124-
var type: String
125-
126-
alias empty = Message("")
127-
alias http_start = Message("http.response.start")
128-
129-
13034
fn to_string[T: Writable](value: T) -> String:
13135
return String.write(value)
13236

tests/integration/test_net.mojo

Whitespace-only changes.

tests/lightbug_http/test_host_port.mojo

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import testing
2-
from lightbug_http.address import join_host_port, parse_address, TCPAddr
3-
from lightbug_http.strings import NetworkType
2+
from lightbug_http.address import TCPAddr, NetworkType, join_host_port, parse_address
43

54

65
def test_split_host_port():

0 commit comments

Comments
 (0)