-
Notifications
You must be signed in to change notification settings - Fork 91
/
bucket_origin.go
188 lines (167 loc) · 5.28 KB
/
bucket_origin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package cos
import (
"context"
"encoding/xml"
"fmt"
"github.com/clbanning/mxj"
"net/http"
"strconv"
"strings"
)
type BucketPutOriginOptions struct {
XMLName xml.Name `xml:"OriginConfiguration"`
Rule []BucketOriginRule `xml:"OriginRule"`
}
type BucketOriginRule struct {
RulePriority int `xml:"RulePriority,omitempty"`
OriginType string `xml:"OriginType,omitempty"`
OriginCondition *BucketOriginCondition `xml:"OriginCondition,omitempty"`
OriginParameter *BucketOriginParameter `xml:"OriginParameter,omitempty"`
OriginInfo *BucketOriginInfo `xml:"OriginInfo,omitempty"`
}
type BucketOriginCondition struct {
HTTPStatusCode string `xml:"HTTPStatusCode,omitempty"`
Prefix string `xml:"Prefix,omitempty"`
}
type BucketOriginParameter struct {
Protocol string `xml:"Protocol,omitempty"`
FollowQueryString bool `xml:"FollowQueryString,omitempty"`
HttpHeader *BucketOriginHttpHeader `xml:"HttpHeader,omitempty"`
FollowRedirection bool `xml:"FollowRedirection,omitempty"`
HttpRedirectCode string `xml:"HttpRedirectCode,omitempty"`
CopyOriginData bool `xml:"CopyOriginData,omitempty"`
}
type BucketOriginHttpHeader struct {
// 目前还不支持 FollowAllHeaders
FollowAllHeaders bool `xml:"FollowAllHeaders,omitempty"`
NewHttpHeaders []OriginHttpHeader `xml:"NewHttpHeaders>Header,omitempty"`
FollowHttpHeaders []OriginHttpHeader `xml:"FollowHttpHeaders>Header,omitempty"`
ForbidFollowHeaders []OriginHttpHeader `xml:"ForbidFollowHeaders>Header,omitempty"`
}
type OriginHttpHeader struct {
Key string `xml:"Key,omitempty"`
Value string `xml:"Value,omitempty"`
}
type BucketOriginInfo struct {
HostInfo *BucketOriginHostInfo `xml:"HostInfo,omitempty"`
FileInfo *BucketOriginFileInfo `xml:"FileInfo,omitempty"`
}
type BucketOriginHostInfo struct {
HostName string
Weight int64
StandbyHostName_N []string
}
type BucketOriginFileInfo struct {
PrefixConfiguration *OriginPrefixConfiguration `xml:"PrefixConfiguration,omitempty"`
SuffixConfiguration *OriginSuffixConfiguration `xml:"SuffixConfiguration,omitempty"`
FixedFileConfiguration *OriginFixedFileConfiguration `xml:"FixedFileConfiguration,omitempty"`
}
type OriginPrefixConfiguration struct {
Prefix string `xml:"Prefix,omitempty"`
}
type OriginSuffixConfiguration struct {
Suffix string `xml:"Suffix,omitempty"`
}
type OriginFixedFileConfiguration struct {
FixedFilePath string `xml:"FixedFilePath,omitempty"`
}
type BucketGetOriginResult BucketPutOriginOptions
func (this *BucketOriginHostInfo) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if this == nil {
return nil
}
err := e.EncodeToken(start)
if err != nil {
return err
}
if this.HostName != "" {
err = e.EncodeElement(this.HostName, xml.StartElement{Name: xml.Name{Local: "HostName"}})
if err != nil {
return err
}
}
if this.Weight != 0 {
err = e.EncodeElement(this.Weight, xml.StartElement{Name: xml.Name{Local: "Weight"}})
if err != nil {
return err
}
}
for index, standByHostName := range this.StandbyHostName_N {
err = e.EncodeElement(standByHostName, xml.StartElement{Name: xml.Name{Local: fmt.Sprintf("StandbyHostName_%v", index+1)}})
if err != nil {
return err
}
}
return e.EncodeToken(xml.EndElement{Name: start.Name})
}
func (this *BucketOriginHostInfo) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var val struct {
XMLName xml.Name
Inner []byte `xml:",innerxml"`
}
err := d.DecodeElement(&val, &start)
if err != nil {
return err
}
str := "<HostInfo>" + string(val.Inner) + "</HostInfo>"
myMxjMap, err := mxj.NewMapXml([]byte(str))
if err != nil {
return err
}
myMap, ok := myMxjMap["HostInfo"].(map[string]interface{})
if !ok {
return fmt.Errorf("XML HostInfo Parse failed")
}
var total int
for key, value := range myMap {
if key == "HostName" {
this.HostName = value.(string)
}
if key == "Weight" {
v := value.(string)
this.Weight, err = strconv.ParseInt(v, 10, 64)
if err != nil {
return err
}
}
if strings.HasPrefix(key, "StandbyHostName_") {
total++
}
}
// 按顺序执行
for i := 1; i <= total; i++ {
key := fmt.Sprintf("StandbyHostName_%v", i)
this.StandbyHostName_N = append(this.StandbyHostName_N, myMap[key].(string))
}
return nil
}
func (s *BucketService) PutOrigin(ctx context.Context, opt *BucketPutOriginOptions) (*Response, error) {
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?origin",
method: http.MethodPut,
body: opt,
}
resp, err := s.client.doRetry(ctx, sendOpt)
return resp, err
}
func (s *BucketService) GetOrigin(ctx context.Context) (*BucketGetOriginResult, *Response, error) {
var res BucketGetOriginResult
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?origin",
method: http.MethodGet,
result: &res,
}
resp, err := s.client.doRetry(ctx, sendOpt)
return &res, resp, err
}
func (s *BucketService) DeleteOrigin(ctx context.Context) (*Response, error) {
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?origin",
method: http.MethodDelete,
}
resp, err := s.client.doRetry(ctx, sendOpt)
return resp, err
}