add url package
This commit is contained in:
parent
b897a4dfda
commit
78cf1e5e3f
@ -4,7 +4,8 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
|
||||||
|
"git.3crabs.ru/VLADIMIR/net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
type client struct{}
|
type client struct{}
|
||||||
|
15
url/examples/main.go
Normal file
15
url/examples/main.go
Normal 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
31
url/url.go
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user