Skip to content

Commit

Permalink
feat: support https (#76)
Browse files Browse the repository at this point in the history
feat: support https
lingqi7 authored Dec 16, 2022
1 parent 6b7d118 commit 493a03f
Showing 6 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
}
```

4 changes: 2 additions & 2 deletions upyun/form.go
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions upyun/http.go
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 4 additions & 4 deletions upyun/process.go
Original file line number Diff line number Diff line change
@@ -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))
4 changes: 2 additions & 2 deletions upyun/rest.go
Original file line number Diff line number Diff line change
@@ -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 {
1 change: 1 addition & 0 deletions upyun/upyun.go
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ type UpYunConfig struct {
Secret string // deprecated
Hosts map[string]string
UserAgent string
UseHTTP bool
}

type UpYun struct {

0 comments on commit 493a03f

Please sign in to comment.