Skip to content

Commit 493a03f

Browse files
authored
feat: support https (#76)
feat: support https
1 parent 6b7d118 commit 493a03f

File tree

6 files changed

+29
-8
lines changed

6 files changed

+29
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ type UpYunConfig struct {
253253
Secret string // 表单上传密钥,已经弃用!
254254
Hosts map[string]string // 自定义 Hosts 映射关系
255255
UserAgent string // HTTP User-Agent 头,默认 "UPYUN Go SDK V2"
256+
UseHTTP bool // 默认使用https,若要使用http,则该字段值为true
256257
}
257258
```
258259

upyun/form.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ func (up *UpYun) FormUpload(config *FormUploadConfig) (*FormUploadResp, error) {
8484
formValues["authorization"] = up.MakeUnifiedAuth(sign)
8585
}
8686

87-
endpoint := up.doGetEndpoint("v0.api.upyun.com")
88-
url := fmt.Sprintf("http://%s/%s", endpoint, up.Bucket)
87+
endpoint := up.getEndpoint("v0.api.upyun.com")
88+
url := fmt.Sprintf("%s/%s", endpoint, up.Bucket)
8989
resp, err := up.doFormRequest(url, formValues)
9090
if err != nil {
9191
return nil, err

upyun/http.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package upyun
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
67
"net/http"
78
"os"
@@ -80,3 +81,21 @@ func (up *UpYun) doGetEndpoint(host string) string {
8081
}
8182
return host
8283
}
84+
85+
func (up *UpYun) getEndpoint(defaultHost string) string {
86+
value := up.Hosts["host"]
87+
if value == "" {
88+
value = defaultHost
89+
}
90+
s := strings.TrimSpace(value)
91+
s = strings.TrimPrefix(s, "http://")
92+
s = strings.TrimPrefix(s, "https://")
93+
if s == "" {
94+
s = defaultHost
95+
}
96+
scheme := "https://"
97+
if up.UseHTTP {
98+
scheme = "http://"
99+
}
100+
return fmt.Sprintf("%s%s", scheme, s)
101+
}

upyun/process.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ func (up *UpYun) doProcessRequest(method, uri string,
115115

116116
var resp *http.Response
117117
var err error
118-
endpoint := up.doGetEndpoint("p0.api.upyun.com")
119-
rawurl := fmt.Sprintf("http://%s%s", endpoint, uri)
118+
endpoint := up.getEndpoint("p0.api.upyun.com")
119+
rawurl := fmt.Sprintf("%s%s", endpoint, uri)
120120
switch method {
121121
case "GET":
122122
resp, err = up.doHTTPRequest(method, rawurl, headers, nil)
@@ -203,8 +203,8 @@ func (up *UpYun) doSyncProcessRequest(method, uri string, payload string) (map[s
203203

204204
var resp *http.Response
205205
var err error
206-
endpoint := up.doGetEndpoint("p1.api.upyun.com")
207-
rawurl := fmt.Sprintf("http://%s%s", endpoint, uri)
206+
endpoint := up.getEndpoint("p1.api.upyun.com")
207+
rawurl := fmt.Sprintf("%s%s", endpoint, uri)
208208
switch method {
209209
case "POST":
210210
resp, err = up.doHTTPRequest(method, rawurl, headers, strings.NewReader(payload))

upyun/rest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,8 +851,8 @@ func (up *UpYun) doRESTRequest(config *restReqConfig) (*http.Response, error) {
851851
})
852852
}
853853

854-
endpoint := up.doGetEndpoint("v0.api.upyun.com")
855-
url := fmt.Sprintf("http://%s%s", endpoint, escUri)
854+
endpoint := up.getEndpoint("v0.api.upyun.com")
855+
url := fmt.Sprintf("%s%s", endpoint, escUri)
856856

857857
resp, err := up.doHTTPRequest(config.method, url, headers, config.httpBody)
858858
if err != nil {

upyun/upyun.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type UpYunConfig struct {
2020
Secret string // deprecated
2121
Hosts map[string]string
2222
UserAgent string
23+
UseHTTP bool
2324
}
2425

2526
type UpYun struct {

0 commit comments

Comments
 (0)