mirror of
https://github.com/tinygo-org/net.git
synced 2026-07-26 08:18:39 +00:00
Upgrade net package from Go 1.21.4 to Go 1.26.2
Backport upstream Go standard library changes to TinyGo's net package. Unmodified files (18) replaced directly from Go 1.26.2 source. Modified files (22) merged via 3-way diff, preserving all TinyGo netdev adaptations, TINYGO markers, and embedded-device constraints. Notable upstream changes included: - net/http: cookie handling improvements, fs enhancements, reverse proxy updates, chunked encoding fixes - net/http: ServeMux routing and pattern matching updates - net: IP parsing and MAC address handling improvements - Various doc link syntax modernization (e.g. [Dial], [Buffers]) TinyGo-only files (netdev.go, tlssock.go) unchanged. All TINYGO comment markers preserved.
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
// TINYGO: The following is copied from Go 1.21.4 official implementation.
|
||||
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// TINYGO: The following is copied from Go 1.26.2 official implementation.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
@@ -16,7 +14,8 @@ package net
|
||||
|
||||
import (
|
||||
"internal/bytealg"
|
||||
"internal/itoa"
|
||||
"internal/strconv"
|
||||
"internal/stringslite"
|
||||
"net/netip"
|
||||
)
|
||||
|
||||
@@ -40,7 +39,7 @@ type IP []byte
|
||||
// An IPMask is a bitmask that can be used to manipulate
|
||||
// IP addresses for IP addressing and routing.
|
||||
//
|
||||
// See type IPNet and func ParseCIDR for details.
|
||||
// See type [IPNet] and func [ParseCIDR] for details.
|
||||
type IPMask []byte
|
||||
|
||||
// An IPNet represents an IP network.
|
||||
@@ -74,9 +73,9 @@ func IPv4Mask(a, b, c, d byte) IPMask {
|
||||
return p
|
||||
}
|
||||
|
||||
// CIDRMask returns an IPMask consisting of 'ones' 1 bits
|
||||
// CIDRMask returns an [IPMask] consisting of 'ones' 1 bits
|
||||
// followed by 0s up to a total length of 'bits' bits.
|
||||
// For a mask of this form, CIDRMask is the inverse of IPMask.Size.
|
||||
// For a mask of this form, CIDRMask is the inverse of [IPMask.Size].
|
||||
func CIDRMask(ones, bits int) IPMask {
|
||||
if bits != 8*IPv4len && bits != 8*IPv6len {
|
||||
return nil
|
||||
@@ -302,11 +301,18 @@ func (ip IP) String() string {
|
||||
if len(ip) != IPv4len && len(ip) != IPv6len {
|
||||
return "?" + hexString(ip)
|
||||
}
|
||||
// If IPv4, use dotted notation.
|
||||
if p4 := ip.To4(); len(p4) == IPv4len {
|
||||
return netip.AddrFrom4([4]byte(p4)).String()
|
||||
|
||||
var buf []byte
|
||||
switch len(ip) {
|
||||
case IPv4len:
|
||||
const maxCap = len("255.255.255.255")
|
||||
buf = make([]byte, 0, maxCap)
|
||||
case IPv6len:
|
||||
const maxCap = len("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
|
||||
buf = make([]byte, 0, maxCap)
|
||||
}
|
||||
return netip.AddrFrom16([16]byte(ip)).String()
|
||||
buf = ip.appendTo(buf)
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
func hexString(b []byte) string {
|
||||
@@ -326,21 +332,45 @@ func ipEmptyString(ip IP) string {
|
||||
return ip.String()
|
||||
}
|
||||
|
||||
// MarshalText implements the encoding.TextMarshaler interface.
|
||||
// The encoding is the same as returned by String, with one exception:
|
||||
// When len(ip) is zero, it returns an empty slice.
|
||||
func (ip IP) MarshalText() ([]byte, error) {
|
||||
if len(ip) == 0 {
|
||||
return []byte(""), nil
|
||||
// appendTo appends the string representation of ip to b and returns the expanded b
|
||||
// If len(ip) != IPv4len or IPv6len, it appends nothing.
|
||||
func (ip IP) appendTo(b []byte) []byte {
|
||||
// If IPv4, use dotted notation.
|
||||
if p4 := ip.To4(); len(p4) == IPv4len {
|
||||
ip = p4
|
||||
}
|
||||
if len(ip) != IPv4len && len(ip) != IPv6len {
|
||||
return nil, &AddrError{Err: "invalid IP address", Addr: hexString(ip)}
|
||||
}
|
||||
return []byte(ip.String()), nil
|
||||
addr, _ := netip.AddrFromSlice(ip)
|
||||
return addr.AppendTo(b)
|
||||
}
|
||||
|
||||
// UnmarshalText implements the encoding.TextUnmarshaler interface.
|
||||
// The IP address is expected in a form accepted by ParseIP.
|
||||
// AppendText implements the [encoding.TextAppender] interface.
|
||||
// The encoding is the same as returned by [IP.String], with one exception:
|
||||
// When len(ip) is zero, it appends nothing.
|
||||
func (ip IP) AppendText(b []byte) ([]byte, error) {
|
||||
if len(ip) == 0 {
|
||||
return b, nil
|
||||
}
|
||||
if len(ip) != IPv4len && len(ip) != IPv6len {
|
||||
return b, &AddrError{Err: "invalid IP address", Addr: hexString(ip)}
|
||||
}
|
||||
|
||||
return ip.appendTo(b), nil
|
||||
}
|
||||
|
||||
// MarshalText implements the [encoding.TextMarshaler] interface.
|
||||
// The encoding is the same as returned by [IP.String], with one exception:
|
||||
// When len(ip) is zero, it returns an empty slice.
|
||||
func (ip IP) MarshalText() ([]byte, error) {
|
||||
// 24 is satisfied with all IPv4 addresses and short IPv6 addresses
|
||||
b, err := ip.AppendText(make([]byte, 0, 24))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// UnmarshalText implements the [encoding.TextUnmarshaler] interface.
|
||||
// The IP address is expected in a form accepted by [ParseIP].
|
||||
func (ip *IP) UnmarshalText(text []byte) error {
|
||||
if len(text) == 0 {
|
||||
*ip = nil
|
||||
@@ -485,14 +515,15 @@ func (n *IPNet) String() string {
|
||||
if l == -1 {
|
||||
return nn.String() + "/" + m.String()
|
||||
}
|
||||
return nn.String() + "/" + itoa.Uitoa(uint(l))
|
||||
return nn.String() + "/" + strconv.Itoa(l)
|
||||
}
|
||||
|
||||
// ParseIP parses s as an IP address, returning the result.
|
||||
// The string s can be in IPv4 dotted decimal ("192.0.2.1"), IPv6
|
||||
// ("2001:db8::68"), or IPv4-mapped IPv6 ("::ffff:192.0.2.1") form.
|
||||
// If s is not a valid textual representation of an IP address,
|
||||
// ParseIP returns nil.
|
||||
// ParseIP returns nil. The returned address is always 16 bytes,
|
||||
// IPv4 addresses are returned in IPv4-mapped IPv6 form.
|
||||
func ParseIP(s string) IP {
|
||||
if addr, valid := parseIP(s); valid {
|
||||
return IP(addr[:])
|
||||
@@ -517,11 +548,10 @@ func parseIP(s string) ([16]byte, bool) {
|
||||
// For example, ParseCIDR("192.0.2.1/24") returns the IP address
|
||||
// 192.0.2.1 and the network 192.0.2.0/24.
|
||||
func ParseCIDR(s string) (IP, *IPNet, error) {
|
||||
i := bytealg.IndexByteString(s, '/')
|
||||
if i < 0 {
|
||||
addr, mask, found := stringslite.Cut(s, "/")
|
||||
if !found {
|
||||
return nil, nil, &ParseError{Type: "CIDR address", Text: s}
|
||||
}
|
||||
addr, mask := s[:i], s[i+1:]
|
||||
|
||||
ipAddr, err := netip.ParseAddr(addr)
|
||||
if err != nil || ipAddr.Zone() != "" {
|
||||
|
||||
Reference in New Issue
Block a user