From 6cf4ec89f3b4b136e8a74284d26de35823f17436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Sat, 13 Aug 2022 09:33:54 +0800 Subject: [PATCH] - init --- .drone.yml | 17 +++++++++++++ .gitignore | 7 ++++++ LICENSE | 21 ++++++++++++++++ README.md | 17 +++++++++++++ const.go | 3 +++ go.mod | 3 +++ gourl.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ gourl_test.go | 12 +++++++++ 8 files changed, 150 insertions(+) create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 const.go create mode 100644 go.mod create mode 100644 gourl.go create mode 100644 gourl_test.go diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..c56c479 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,17 @@ +kind: pipeline +type: docker +name: clone + +steps: + - name: Test + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -v ./... + - name: Benchmark + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -bench=. -benchmem \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ee9f26 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.env +.git +.svn +.idea +.vscode +.log +gomod.sh \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ff84bbf --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 李光春 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..51334a1 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +

+Golang Url +

+ +📦 Golang Url + +[comment]: <> (go) +[![godoc](https://pkg.go.dev/badge/go.dtapp.net/gourl?status.svg)](https://pkg.go.dev/go.dtapp.net/gourl) +[![goproxy.cn](https://goproxy.cn/stats/go.dtapp.net/gourl/badges/download-count.svg)](https://goproxy.cn/stats/go.dtapp.net/gourl) +[![goreportcard.com](https://goreportcard.com/badge/go.dtapp.net/gourl)](https://goreportcard.com/report/go.dtapp.net/gourl) +[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/go.dtapp.net%2Fgourl) + +#### 安装 + +```go +go get -v -u go.dtapp.net/gourl +``` diff --git a/const.go b/const.go new file mode 100644 index 0000000..61bf62e --- /dev/null +++ b/const.go @@ -0,0 +1,3 @@ +package gourl + +const Version = "1.0.0" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..218b155 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module go.dtapp.net/gourl + +go 1.19 diff --git a/gourl.go b/gourl.go new file mode 100644 index 0000000..5e93b49 --- /dev/null +++ b/gourl.go @@ -0,0 +1,70 @@ +package gourl + +import ( + "net/url" + "strings" +) + +// ResponseUrlParse 返回参数 +type ResponseUrlParse struct { + Uri string `json:"uri"` // URI + Urn string `json:"urn"` // URN + Url string `json:"url"` // URL + Scheme string `json:"scheme"` // 协议 + Host string `json:"host"` // 主机 + Hostname string `json:"hostname"` // 主机名 + Port string `json:"port"` // 端口 + Path string `json:"path"` // 路径 + RawQuery string `json:"raw_query"` // 参数 ? + Fragment string `json:"fragment"` // 片段 # +} + +// UriParse 解析URl +func UriParse(input string) (resp ResponseUrlParse) { + parse, err := url.Parse(input) + if err != nil { + return + } + resp.Uri = input + resp.Urn = parse.Host + parse.Path + resp.Url = parse.Scheme + "://" + parse.Host + parse.Path + resp.Scheme = parse.Scheme + resp.Host = parse.Host + resp.Hostname = parse.Hostname() + resp.Port = parse.Port() + resp.Path = parse.Path + resp.RawQuery = parse.RawQuery + resp.Fragment = parse.Fragment + return +} + +// UriFilterExcludeQueryString 过滤掉url中的参数 +func UriFilterExcludeQueryString(uri string) string { + URL, _ := url.Parse(uri) + clearUri := strings.ReplaceAll(uri, URL.RawQuery, "") + clearUri = strings.TrimRight(clearUri, "?") + return strings.TrimRight(clearUri, "/") +} + +// LenCode 编码 +func LenCode(s string) string { + escape := url.QueryEscape(s) + return escape +} + +// DeCode 解码 +func DeCode(s string) string { + unescape, _ := url.QueryUnescape(s) + return unescape +} + +// ParseQuery 获取URL参数 https://studygolang.com/articles/2876 +func ParseQuery(s string) map[string][]string { + u, err := url.Parse(s) + if err != nil { + return nil + } + urlParam := u.RawQuery + m, _ := url.ParseQuery(urlParam) + return m +} diff --git a/gourl_test.go b/gourl_test.go new file mode 100644 index 0000000..40c4b96 --- /dev/null +++ b/gourl_test.go @@ -0,0 +1,12 @@ +package gourl + +import "testing" + +func TestUrlParse(t *testing.T) { + t.Logf("%+v", UriParse("https://api.dtapp.net/ip?ip=127.0.0.1#history")) + t.Logf("%+v", UriParse("https://test:abcd123@golangbyexample.com:8000/tutorials/intro?type=advance&compact=false#history")) + t.Logf("%+v", UriFilterExcludeQueryString("/")) + t.Logf("%+v", UriFilterExcludeQueryString("/favicon.ico")) + t.Logf("%+v", UriFilterExcludeQueryString("/ip")) + t.Logf("%+v", UriFilterExcludeQueryString("/ip?ip=127.0.0.1")) +}