Skip to content

Commit 9633dae

Browse files
committed
rm hostport
1 parent 3469871 commit 9633dae

File tree

5 files changed

+70
-99
lines changed

5 files changed

+70
-99
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ jobs:
1515
curl -ssL https://magic.modular.com | bash
1616
source $HOME/.bash_profile
1717
magic run test
18-
magic run integration_test
19-
magic run integration_tests
18+
magic run integration_tests_py
19+
magic run integration_tests_external

lightbug_http/net.mojo

Lines changed: 46 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ trait Addr(Stringable, Representable, Writable, EqualityComparableCollectionElem
8787
fn __init__(out self, ip: String, port: UInt16):
8888
...
8989

90-
@implicit
91-
fn __init__(out self, host_port: HostPort):
92-
...
93-
9490
fn network(self) -> String:
9591
...
9692

@@ -117,13 +113,6 @@ struct NoTLSListener:
117113
fn __moveinit__(out self, owned existing: Self):
118114
self.socket = existing.socket^
119115

120-
# fn __del__(owned self):
121-
# logger.info("Listener cleaning up", self.socket)
122-
# try:
123-
# self.teardown()
124-
# except e:
125-
# logger.error("NoTLSListener.__del__: Failed to close connection: " + str(e))
126-
127116
fn accept(self) raises -> TCPConnection:
128117
return TCPConnection(self.socket.accept())
129118

@@ -152,7 +141,8 @@ struct ListenConfig:
152141
network in NetworkType.SUPPORTED_TYPES,
153142
"Unsupported network type for internet address resolution. Unix addresses are not supported yet.",
154143
]()
155-
var addr = TCPAddr(HostPort.from_string(address))
144+
var local = parse_address(address)
145+
var addr = TCPAddr(local[0], local[1])
156146
var socket: Socket[TCPAddr]
157147
try:
158148
socket = Socket[TCPAddr]()
@@ -215,13 +205,6 @@ struct TCPConnection(Connection):
215205
fn __moveinit__(inout self, owned existing: Self):
216206
self.socket = existing.socket^
217207

218-
# fn __del__(owned self):
219-
# logger.info("TCPConnection cleaning up", self.socket)
220-
# try:
221-
# self.teardown()
222-
# except e:
223-
# logger.error("TCPConnection.__del__: Failed to close connection: " + str(e))
224-
225208
fn read(self, mut buf: Bytes) raises -> Int:
226209
try:
227210
return self.socket.receive_into(buf)
@@ -409,12 +392,6 @@ struct TCPAddr(Addr):
409392
self.port = port
410393
self.zone = ""
411394

412-
@implicit
413-
fn __init__(out self, host_port: HostPort):
414-
self.ip = host_port.host
415-
self.port = host_port.port
416-
self.zone = ""
417-
418395
fn network(self) -> String:
419396
return NetworkType.tcp.value
420397

@@ -447,54 +424,56 @@ alias MissingPortError = Error("missing port in address")
447424
alias TooManyColonsError = Error("too many colons in address")
448425

449426

450-
@value
451-
struct HostPort:
452-
var host: String
453-
var port: UInt16
427+
fn parse_address(address: String) raises -> (String, UInt16):
428+
"""Parse an address string into a host and port.
454429
455-
@staticmethod
456-
fn from_string(address: String) raises -> HostPort:
457-
var colon_index = address.rfind(":")
458-
if colon_index == -1:
459-
raise MissingPortError
460-
461-
var host: String = ""
462-
var port: String = ""
463-
var j: Int = 0
464-
var k: Int = 0
430+
Args:
431+
address: The address string.
465432
466-
if address[0] == "[":
467-
var end_bracket_index = address.find("]")
468-
if end_bracket_index == -1:
469-
raise Error("missing ']' in address")
433+
Returns:
434+
A tuple containing the host and port.
435+
"""
436+
var colon_index = address.rfind(":")
437+
if colon_index == -1:
438+
raise MissingPortError
470439

471-
if end_bracket_index + 1 == len(address):
472-
raise MissingPortError
473-
elif end_bracket_index + 1 == colon_index:
474-
host = address[1:end_bracket_index]
475-
j = 1
476-
k = end_bracket_index + 1
477-
else:
478-
if address[end_bracket_index + 1] == ":":
479-
raise TooManyColonsError
480-
else:
481-
raise MissingPortError
482-
else:
483-
host = address[:colon_index]
484-
if host.find(":") != -1:
485-
raise TooManyColonsError
440+
var host: String = ""
441+
var port: String = ""
442+
var j: Int = 0
443+
var k: Int = 0
486444

487-
if address[j:].find("[") != -1:
488-
raise Error("unexpected '[' in address")
489-
if address[k:].find("]") != -1:
490-
raise Error("unexpected ']' in address")
445+
if address[0] == "[":
446+
var end_bracket_index = address.find("]")
447+
if end_bracket_index == -1:
448+
raise Error("missing ']' in address")
491449

492-
port = address[colon_index + 1 :]
493-
if port == "":
450+
if end_bracket_index + 1 == len(address):
494451
raise MissingPortError
495-
if host == "":
496-
raise Error("missing host")
497-
return HostPort(host, int(port))
452+
elif end_bracket_index + 1 == colon_index:
453+
host = address[1:end_bracket_index]
454+
j = 1
455+
k = end_bracket_index + 1
456+
else:
457+
if address[end_bracket_index + 1] == ":":
458+
raise TooManyColonsError
459+
else:
460+
raise MissingPortError
461+
else:
462+
host = address[:colon_index]
463+
if host.find(":") != -1:
464+
raise TooManyColonsError
465+
466+
if address[j:].find("[") != -1:
467+
raise Error("unexpected '[' in address")
468+
if address[k:].find("]") != -1:
469+
raise Error("unexpected ']' in address")
470+
471+
port = address[colon_index + 1 :]
472+
if port == "":
473+
raise MissingPortError
474+
if host == "":
475+
raise Error("missing host")
476+
return host, UInt16(int(port))
498477

499478

500479
fn binary_port_to_int(port: UInt16) -> Int:

lightbug_http/socket.mojo

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ from lightbug_http.strings import NetworkType
4949
from lightbug_http.net import (
5050
Addr,
5151
TCPAddr,
52-
HostPort,
5352
default_buffer_size,
5453
binary_port_to_int,
5554
binary_ip_to_string,
@@ -75,8 +74,6 @@ struct Socket[AddrType: Addr, address_family: Int = AF_INET](Representable, Stri
7574

7675
var fd: Int32
7776
"""The file descriptor of the socket."""
78-
# var address_family: Int
79-
# """The address family of the socket."""
8077
var socket_type: Int32
8178
"""The socket type."""
8279
var protocol: Byte
@@ -94,7 +91,6 @@ struct Socket[AddrType: Addr, address_family: Int = AF_INET](Representable, Stri
9491
out self,
9592
local_address: AddrType = AddrType(),
9693
remote_address: AddrType = AddrType(),
97-
# address_family: Int = AF_INET,
9894
socket_type: Int32 = SOCK_STREAM,
9995
protocol: Byte = 0,
10096
) raises:
@@ -109,7 +105,6 @@ struct Socket[AddrType: Addr, address_family: Int = AF_INET](Representable, Stri
109105
Raises:
110106
Error: If the socket creation fails.
111107
"""
112-
# self.address_family = address_family
113108
self.socket_type = socket_type
114109
self.protocol = protocol
115110

@@ -152,7 +147,6 @@ struct Socket[AddrType: Addr, address_family: Int = AF_INET](Representable, Stri
152147
existing: The existing socket object to move the data from.
153148
"""
154149
self.fd = existing.fd
155-
# self.address_family = existing.address_family
156150
self.socket_type = existing.socket_type
157151
self.protocol = existing.protocol
158152
self._local_address = existing._local_address^
@@ -276,12 +270,12 @@ struct Socket[AddrType: Addr, address_family: Int = AF_INET](Representable, Stri
276270

277271
var new_socket = Socket(
278272
fd=new_socket_fd,
279-
# address_family=self.address_family,
280273
socket_type=self.socket_type,
281274
protocol=self.protocol,
282275
local_address=self.local_address(),
283276
)
284-
new_socket.set_remote_address(new_socket.get_peer_name())
277+
var peer = new_socket.get_peer_name()
278+
new_socket.set_remote_address(AddrType(peer[0], peer[1]))
285279
return new_socket^
286280

287281
fn listen(self, backlog: UInt = 0) raises:
@@ -336,9 +330,9 @@ struct Socket[AddrType: Addr, address_family: Int = AF_INET](Representable, Stri
336330
raise Error("Socket.bind: Binding socket failed.")
337331

338332
var local = self.get_sock_name()
339-
self._local_address = AddrType(local.host, int(local.port))
333+
self._local_address = AddrType(local[0], local[1])
340334

341-
fn get_sock_name(self) raises -> HostPort:
335+
fn get_sock_name(self) raises -> (String, UInt16):
342336
"""Return the address of the socket.
343337
344338
Returns:
@@ -363,12 +357,11 @@ struct Socket[AddrType: Addr, address_family: Int = AF_INET](Representable, Stri
363357
raise Error("get_sock_name: Failed to get address of local socket.")
364358

365359
var addr_in = local_address.bitcast[sockaddr_in]().take_pointee()
366-
return HostPort(
367-
host=binary_ip_to_string[AF_INET](addr_in.sin_addr.s_addr),
368-
port=binary_port_to_int(addr_in.sin_port),
360+
return binary_ip_to_string[address_family](addr_in.sin_addr.s_addr), UInt16(
361+
binary_port_to_int(addr_in.sin_port)
369362
)
370363

371-
fn get_peer_name(self) raises -> HostPort:
364+
fn get_peer_name(self) raises -> (String, UInt16):
372365
"""Return the address of the peer connected to the socket.
373366
374367
Returns:
@@ -388,9 +381,8 @@ struct Socket[AddrType: Addr, address_family: Int = AF_INET](Representable, Stri
388381
logger.error(e)
389382
raise Error("get_peer_name: Failed to get address of remote socket.")
390383

391-
return HostPort(
392-
host=binary_ip_to_string[AF_INET](addr_in.sin_addr.s_addr),
393-
port=binary_port_to_int(addr_in.sin_port),
384+
return binary_ip_to_string[address_family](addr_in.sin_addr.s_addr), UInt16(
385+
binary_port_to_int(addr_in.sin_port)
394386
)
395387

396388
fn get_socket_option(self, option_name: Int) raises -> Int:
@@ -454,7 +446,7 @@ struct Socket[AddrType: Addr, address_family: Int = AF_INET](Representable, Stri
454446
raise e
455447

456448
var remote = self.get_peer_name()
457-
self._remote_address = AddrType(remote.host, remote.port)
449+
self._remote_address = AddrType(remote[0], remote[1])
458450

459451
fn send(self, buffer: Span[Byte]) raises -> Int:
460452
if buffer[-1] == 0:

mojoproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ version = "0.1.8"
1010
build = { cmd = "rattler-build build --recipe recipes -c https://conda.modular.com/max -c conda-forge --skip-existing=all", env = {MODULAR_MOJO_IMPORT_PATH = "$CONDA_PREFIX/lib/mojo"} }
1111
publish = { cmd = "bash scripts/publish.sh", env = { PREFIX_API_KEY = "$PREFIX_API_KEY" } }
1212
test = { cmd = "magic run mojo test -I . tests/lightbug_http" }
13-
integration_test = { cmd = "bash scripts/integration_test.sh" }
14-
integration_tests = { cmd = "magic run mojo test -I . tests/integration" }
13+
integration_tests_py = { cmd = "bash scripts/integration_test.sh" }
14+
integration_tests_external = { cmd = "magic run mojo test -I . tests/integration" }
1515
bench = { cmd = "magic run mojo -I . benchmark/bench.mojo" }
1616
bench_server = { cmd = "bash scripts/bench_server.sh" }
1717
format = { cmd = "magic run mojo format -l 120 lightbug_http" }

tests/lightbug_http/test_host_port.mojo

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import testing
2-
from lightbug_http.net import join_host_port, HostPort, TCPAddr
2+
from lightbug_http.net import join_host_port, parse_address, TCPAddr
33
from lightbug_http.strings import NetworkType
44

55

66
def test_split_host_port():
77
# IPv4
8-
var hp = HostPort.from_string("127.0.0.1:8080")
9-
testing.assert_equal(hp.host, "127.0.0.1")
10-
testing.assert_equal(hp.port, 8080)
8+
var hp = parse_address("127.0.0.1:8080")
9+
testing.assert_equal(hp[0], "127.0.0.1")
10+
testing.assert_equal(hp[1], 8080)
1111

1212
# IPv6
13-
hp = HostPort.from_string("[::1]:8080")
14-
testing.assert_equal(hp.host, "::1")
15-
testing.assert_equal(hp.port, 8080)
13+
hp = parse_address("[::1]:8080")
14+
testing.assert_equal(hp[0], "::1")
15+
testing.assert_equal(hp[1], 8080)
1616

1717
# # TODO: IPv6 long form - Not supported yet.
18-
# hp = HostPort.from_string("0:0:0:0:0:0:0:1:8080")
19-
# testing.assert_equal(hp.host, "0:0:0:0:0:0:0:1")
20-
# testing.assert_equal(hp.port, 8080)
18+
# hp = parse_address("0:0:0:0:0:0:0:1:8080")
19+
# testing.assert_equal(hp[0], "0:0:0:0:0:0:0:1")
20+
# testing.assert_equal(hp[1], 8080)
2121

2222

2323
def test_join_host_port():

0 commit comments

Comments
 (0)