@@ -3,7 +3,11 @@ package cos
33import (
44 "context"
55 "encoding/xml"
6+ "fmt"
7+ "github.com/clbanning/mxj"
68 "net/http"
9+ "strconv"
10+ "strings"
711)
812
913type BucketPutOriginOptions struct {
@@ -35,9 +39,10 @@ type BucketOriginParameter struct {
3539
3640type BucketOriginHttpHeader struct {
3741 // 目前还不支持 FollowAllHeaders
38- // FollowAllHeaders bool `xml:"FollowAllHeaders,omitempty"`
39- NewHttpHeaders []OriginHttpHeader `xml:"NewHttpHeaders>Header,omitempty"`
40- FollowHttpHeaders []OriginHttpHeader `xml:"FollowHttpHeaders>Header,omitempty"`
42+ FollowAllHeaders bool `xml:"FollowAllHeaders,omitempty"`
43+ NewHttpHeaders []OriginHttpHeader `xml:"NewHttpHeaders>Header,omitempty"`
44+ FollowHttpHeaders []OriginHttpHeader `xml:"FollowHttpHeaders>Header,omitempty"`
45+ ForbidFollowHeaders []OriginHttpHeader `xml:"ForbidFollowHeaders>Header,omitempty"`
4146}
4247
4348type OriginHttpHeader struct {
@@ -46,17 +51,109 @@ type OriginHttpHeader struct {
4651}
4752
4853type BucketOriginInfo struct {
49- HostInfo string `xml:"HostInfo>HostName ,omitempty"`
54+ HostInfo * BucketOriginHostInfo `xml:"HostInfo,omitempty"`
5055 FileInfo * BucketOriginFileInfo `xml:"FileInfo,omitempty"`
5156}
57+
58+ type BucketOriginHostInfo struct {
59+ HostName string
60+ Weight int64
61+ StandbyHostName_N []string
62+ }
63+
5264type BucketOriginFileInfo struct {
53- PrefixDirective bool `xml:"PrefixDirective,omitempty"`
54- Prefix string `xml:"Prefix,omitempty"`
55- Suffix string `xml:"Suffix,omitempty"`
65+ PrefixConfiguration * OriginPrefixConfiguration `xml:"PrefixConfiguration,omitempty"`
66+ SuffixConfiguration * OriginSuffixConfiguration `xml:"SuffixConfiguration,omitempty"`
67+ FixedFileConfiguration * OriginFixedFileConfiguration `xml:"FixedFileConfiguration,omitempty"`
68+ }
69+
70+ type OriginPrefixConfiguration struct {
71+ Prefix string `xml:"Prefix,omitempty"`
72+ }
73+
74+ type OriginSuffixConfiguration struct {
75+ Suffix string `xml:"Suffix,omitempty"`
76+ }
77+
78+ type OriginFixedFileConfiguration struct {
79+ FixedFilePath string `xml:"FixedFilePath,omitempty"`
5680}
5781
5882type BucketGetOriginResult BucketPutOriginOptions
5983
84+ func (this * BucketOriginHostInfo ) MarshalXML (e * xml.Encoder , start xml.StartElement ) error {
85+ if this == nil {
86+ return nil
87+ }
88+ err := e .EncodeToken (start )
89+ if err != nil {
90+ return err
91+ }
92+ if this .HostName != "" {
93+ err = e .EncodeElement (this .HostName , xml.StartElement {Name : xml.Name {Local : "HostName" }})
94+ if err != nil {
95+ return err
96+ }
97+ }
98+ if this .Weight != 0 {
99+ err = e .EncodeElement (this .Weight , xml.StartElement {Name : xml.Name {Local : "Weight" }})
100+ if err != nil {
101+ return err
102+ }
103+ }
104+ for index , standByHostName := range this .StandbyHostName_N {
105+ err = e .EncodeElement (standByHostName , xml.StartElement {Name : xml.Name {Local : fmt .Sprintf ("StandbyHostName_%v" , index + 1 )}})
106+ if err != nil {
107+ return err
108+ }
109+ }
110+ return e .EncodeToken (xml.EndElement {Name : start .Name })
111+ }
112+
113+ func (this * BucketOriginHostInfo ) UnmarshalXML (d * xml.Decoder , start xml.StartElement ) error {
114+ var val struct {
115+ XMLName xml.Name
116+ Inner []byte `xml:",innerxml"`
117+ }
118+ err := d .DecodeElement (& val , & start )
119+ if err != nil {
120+ return err
121+ }
122+ str := "<HostInfo>" + string (val .Inner ) + "</HostInfo>"
123+ myMxjMap , err := mxj .NewMapXml ([]byte (str ))
124+ if err != nil {
125+ return err
126+ }
127+ myMap , ok := myMxjMap ["HostInfo" ].(map [string ]interface {})
128+ if ! ok {
129+ return fmt .Errorf ("XML HostInfo Parse failed" )
130+ }
131+
132+ var total int
133+ for key , value := range myMap {
134+ if key == "HostName" {
135+ this .HostName = value .(string )
136+ }
137+ if key == "Weight" {
138+ v := value .(string )
139+ this .Weight , err = strconv .ParseInt (v , 10 , 64 )
140+ if err != nil {
141+ return err
142+ }
143+ }
144+ if strings .HasPrefix (key , "StandbyHostName_" ) {
145+ total ++
146+ }
147+ }
148+ // 按顺序执行
149+ for i := 1 ; i <= total ; i ++ {
150+ key := fmt .Sprintf ("StandbyHostName_%v" , i )
151+ this .StandbyHostName_N = append (this .StandbyHostName_N , myMap [key ].(string ))
152+ }
153+
154+ return nil
155+ }
156+
60157func (s * BucketService ) PutOrigin (ctx context.Context , opt * BucketPutOriginOptions ) (* Response , error ) {
61158 sendOpt := & sendOptions {
62159 baseURL : s .client .BaseURL .BucketURL ,
0 commit comments