11from memory import UnsafePointer
2+ from collections import Optional
23from sys.ffi import external_call, OpaquePointer
3- from lightbug_http.strings import NetworkType, to_string
4+ from lightbug_http.strings import to_string
45from 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
70133struct 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
333397fn 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
365436fn 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 ] == " [" :
0 commit comments