1.4.5.2
This commit is contained in:
78
EdgeNode/internal/nodes/http_request_oss_plus.go
Normal file
78
EdgeNode/internal/nodes/http_request_oss_plus.go
Normal file
@@ -0,0 +1,78 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build plus
|
||||
|
||||
package nodes
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/oss"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// 请求OSS源站
|
||||
func (this *HTTPRequest) doOSSOrigin(origin *serverconfigs.OriginConfig) (resp *http.Response, goNext bool, errorCode string, ossBucketName string, err error) {
|
||||
if origin == nil || origin.OSS == nil {
|
||||
err = errors.New("'origin' or 'origin.OSS' should not be nil")
|
||||
return
|
||||
}
|
||||
|
||||
// 只支持少数方法
|
||||
var isHeadRequest = this.RawReq.Method != http.MethodGet &&
|
||||
this.RawReq.Method != http.MethodPost &&
|
||||
this.RawReq.Method != http.MethodPut
|
||||
|
||||
var rangeBytes = this.RawReq.Header.Get("Range")
|
||||
if isHeadRequest && len(rangeBytes) == 0 {
|
||||
resp, errorCode, ossBucketName, err = oss.SharedManager.Head(this.RawReq, this.ReqHost, origin.OSS)
|
||||
} else {
|
||||
if len(rangeBytes) > 0 {
|
||||
resp, errorCode, ossBucketName, err = oss.SharedManager.GetRange(this.RawReq, this.ReqHost, rangeBytes, origin.OSS)
|
||||
} else {
|
||||
resp, errorCode, ossBucketName, err = oss.SharedManager.Get(this.RawReq, this.ReqHost, origin.OSS)
|
||||
}
|
||||
}
|
||||
|
||||
if len(ossBucketName) == 0 {
|
||||
this.originAddr = origin.OSS.Type
|
||||
} else {
|
||||
this.originAddr = origin.OSS.Type + "/" + ossBucketName
|
||||
}
|
||||
this.tags = append(this.tags, "oss")
|
||||
|
||||
if err != nil {
|
||||
if oss.IsNotFound(err) {
|
||||
this.write404()
|
||||
return nil, false, errorCode, ossBucketName, nil
|
||||
}
|
||||
|
||||
if oss.IsTimeout(err) {
|
||||
this.writeCode(http.StatusGatewayTimeout, "Read object timeout.", "读取对象超时。")
|
||||
return nil, false, errorCode, ossBucketName, nil
|
||||
}
|
||||
|
||||
return nil, false, errorCode, ossBucketName, fmt.Errorf("OSS: [%s]: %s: %w", origin.OSS.Type, errorCode, err)
|
||||
}
|
||||
|
||||
if isHeadRequest {
|
||||
_ = resp.Body.Close()
|
||||
resp.Body = io.NopCloser(&bytes.Buffer{})
|
||||
}
|
||||
|
||||
// fix Content-Length
|
||||
if resp.Header == nil {
|
||||
resp.Header = http.Header{}
|
||||
}
|
||||
if resp.ContentLength > 0 {
|
||||
_, ok := resp.Header["Content-Length"]
|
||||
if !ok {
|
||||
resp.Header.Set("Content-Length", types.String(resp.ContentLength))
|
||||
}
|
||||
}
|
||||
|
||||
return resp, true, "", ossBucketName, nil
|
||||
}
|
||||
Reference in New Issue
Block a user