add url package

This commit is contained in:
Владимир Фёдоров 2025-07-21 03:10:02 +07:00
parent b897a4dfda
commit 78cf1e5e3f
3 changed files with 48 additions and 1 deletions

View File

@ -4,7 +4,8 @@ import (
"crypto/tls"
"errors"
"net"
"net/url"
"git.3crabs.ru/VLADIMIR/net/url"
)
type client struct{}

15
url/examples/main.go Normal file
View File

@ -0,0 +1,15 @@
package main
import (
"fmt"
"git.3crabs.ru/VLADIMIR/net/url"
)
func main() {
u, err := url.Parse("http://test.ru/")
if err != nil {
panic(err)
}
fmt.Printf("%+v", u)
}

31
url/url.go Normal file
View File

@ -0,0 +1,31 @@
package url
type URL struct {
Scheme string
Host string
Path string
}
func Parse(rawURL string) (*URL, error) {
schemeIndex := -1
hostStartIndex := -1
hostEndIndex := -1
for i := 0; i < len(rawURL); i++ {
if schemeIndex == -1 && rawURL[i] == ':' {
schemeIndex = i
hostStartIndex = i + 3
i += 3
continue
}
if rawURL[i] == '/' {
hostEndIndex = i
break
}
}
return &URL{
Scheme: rawURL[:schemeIndex],
Host: rawURL[hostStartIndex:hostEndIndex],
Path: rawURL[hostEndIndex:],
}, nil
}