From 375dc4074bbdf83d5984b3c9c492f17b53744f34 Mon Sep 17 00:00:00 2001 From: xxx Date: Thu, 15 Dec 2022 14:36:25 +0800 Subject: [PATCH] feat: support https doc: support https [issue 72] issue: #72 Closes: #72 --- README.md | 1 + upyun/form.go | 4 ++-- upyun/http.go | 19 +++++++++++++++++++ upyun/process.go | 8 ++++---- upyun/rest.go | 4 ++-- upyun/upyun.go | 1 + 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1100edc..7fcd8a9 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,7 @@ type UpYunConfig struct { Secret string // 表单上传密钥,已经弃用! Hosts map[string]string // 自定义 Hosts 映射关系 UserAgent string // HTTP User-Agent 头,默认 "UPYUN Go SDK V2" + UseHTTP bool // 默认使用https,若要使用http,则该字段值为true } ``` diff --git a/upyun/form.go b/upyun/form.go index 7d50555..8fdf736 100644 --- a/upyun/form.go +++ b/upyun/form.go @@ -84,8 +84,8 @@ func (up *UpYun) FormUpload(config *FormUploadConfig) (*FormUploadResp, error) { formValues["authorization"] = up.MakeUnifiedAuth(sign) } - endpoint := up.doGetEndpoint("v0.api.upyun.com") - url := fmt.Sprintf("http://%s/%s", endpoint, up.Bucket) + endpoint := up.getEndpoint("v0.api.upyun.com") + url := fmt.Sprintf("%s/%s", endpoint, up.Bucket) resp, err := up.doFormRequest(url, formValues) if err != nil { return nil, err diff --git a/upyun/http.go b/upyun/http.go index 8347ea3..3d08fec 100644 --- a/upyun/http.go +++ b/upyun/http.go @@ -2,6 +2,7 @@ package upyun import ( "bytes" + "fmt" "io" "net/http" "os" @@ -80,3 +81,21 @@ func (up *UpYun) doGetEndpoint(host string) string { } return host } + +func (up *UpYun) getEndpoint(defaultHost string) string { + value := up.Hosts["host"] + if value == "" { + value = defaultHost + } + s := strings.TrimSpace(value) + s = strings.TrimPrefix(s, "http://") + s = strings.TrimPrefix(s, "https://") + if s == "" { + s = defaultHost + } + scheme := "https://" + if up.UseHTTP { + scheme = "http://" + } + return fmt.Sprintf("%s%s", scheme, s) +} diff --git a/upyun/process.go b/upyun/process.go index 8987bdb..63997fa 100644 --- a/upyun/process.go +++ b/upyun/process.go @@ -115,8 +115,8 @@ func (up *UpYun) doProcessRequest(method, uri string, var resp *http.Response var err error - endpoint := up.doGetEndpoint("p0.api.upyun.com") - rawurl := fmt.Sprintf("http://%s%s", endpoint, uri) + endpoint := up.getEndpoint("p0.api.upyun.com") + rawurl := fmt.Sprintf("%s%s", endpoint, uri) switch method { case "GET": resp, err = up.doHTTPRequest(method, rawurl, headers, nil) @@ -203,8 +203,8 @@ func (up *UpYun) doSyncProcessRequest(method, uri string, payload string) (map[s var resp *http.Response var err error - endpoint := up.doGetEndpoint("p1.api.upyun.com") - rawurl := fmt.Sprintf("http://%s%s", endpoint, uri) + endpoint := up.getEndpoint("p1.api.upyun.com") + rawurl := fmt.Sprintf("%s%s", endpoint, uri) switch method { case "POST": resp, err = up.doHTTPRequest(method, rawurl, headers, strings.NewReader(payload)) diff --git a/upyun/rest.go b/upyun/rest.go index 2e82ba1..fd92878 100644 --- a/upyun/rest.go +++ b/upyun/rest.go @@ -851,8 +851,8 @@ func (up *UpYun) doRESTRequest(config *restReqConfig) (*http.Response, error) { }) } - endpoint := up.doGetEndpoint("v0.api.upyun.com") - url := fmt.Sprintf("http://%s%s", endpoint, escUri) + endpoint := up.getEndpoint("v0.api.upyun.com") + url := fmt.Sprintf("%s%s", endpoint, escUri) resp, err := up.doHTTPRequest(config.method, url, headers, config.httpBody) if err != nil { diff --git a/upyun/upyun.go b/upyun/upyun.go index 9217f30..6743d01 100644 --- a/upyun/upyun.go +++ b/upyun/upyun.go @@ -20,6 +20,7 @@ type UpYunConfig struct { Secret string // deprecated Hosts map[string]string UserAgent string + UseHTTP bool } type UpYun struct {