-
Notifications
You must be signed in to change notification settings - Fork 86
/
website.go
183 lines (168 loc) · 5.66 KB
/
website.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
package datatype
import (
"encoding/xml"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/dustin/go-humanize"
. "github.com/journeymidnight/yig/error"
"github.com/journeymidnight/yig/helper"
)
const (
MaxBucketWebsiteRulesCount = 100
MaxBucketWebsiteConfigurationSize = 20 * humanize.KiByte
)
type WebsiteConfiguration struct {
XMLName xml.Name `xml:"WebsiteConfiguration"`
Xmlns string `xml:"xmlns,attr,omitempty"`
RedirectAllRequestsTo *RedirectAllRequestsTo `xml:"RedirectAllRequestsTo,omitempty"`
IndexDocument *IndexDocument `xml:"IndexDocument,omitempty"`
ErrorDocument *ErrorDocument `xml:"ErrorDocument,omitempty"`
RoutingRules []RoutingRule `xml:"RoutingRules>RoutingRule,omitempty"`
}
type RedirectAllRequestsTo struct {
XMLName xml.Name `xml:"RedirectAllRequestsTo"`
HostName string `xml:"HostName"`
Protocol string `xml:"Protocol,omitempty"`
}
type IndexDocument struct {
XMLName xml.Name `xml:"IndexDocument"`
Suffix string `xml:"Suffix"`
}
type ErrorDocument struct {
XMLName xml.Name `xml:"ErrorDocument"`
Key string `xml:"Key"`
}
type RoutingRule struct {
XMLName xml.Name `xml:"RoutingRule"`
Condition *Condition `xml:"Condition,omitempty"`
Redirect *Redirect `xml:"Redirect"`
}
type Condition struct {
XMLName xml.Name `xml:"Condition"`
KeyPrefixEquals string `xml:"KeyPrefixEquals,omitempty"`
HttpErrorCodeReturnedEquals string `xml:"HttpErrorCodeReturnedEquals,omitempty"`
}
type Redirect struct {
XMLName xml.Name `xml:"Redirect"`
Protocol string `xml:"Protocol,omitempty"`
HostName string `xml:"HostName,omitempty"`
ReplaceKeyPrefixWith string `xml:"ReplaceKeyPrefixWith,omitempty"`
ReplaceKeyWith string `xml:"ReplaceKeyWith,omitempty"`
HttpRedirectCode string `xml:"HttpRedirectCode,omitempty"`
}
func (rr RoutingRule) Match(objectName string, errorCode string) bool {
var isKeyMatch, isErrorCodeMatch = true, true
if rr.Condition == nil {
return true
}
if rr.Condition.KeyPrefixEquals != "" {
isKeyMatch = strings.HasPrefix(objectName, rr.Condition.KeyPrefixEquals)
}
if rr.Condition.HttpErrorCodeReturnedEquals != "" {
isErrorCodeMatch = (errorCode == rr.Condition.HttpErrorCodeReturnedEquals)
}
return isKeyMatch && isErrorCodeMatch
}
func (rr RoutingRule) DoRedirect(w http.ResponseWriter, r *http.Request, objectName string) {
rd := rr.Redirect
if rd == nil {
return
}
protocol := rd.Protocol
if protocol == "" {
protocol = helper.Ternary(r.URL.Scheme == "", "http", r.URL.Scheme).(string)
}
hostName := rd.HostName
if hostName == "" {
hostName = r.Host
}
code := http.StatusFound
if rd.HttpRedirectCode != "" {
code, _ = strconv.Atoi(rd.HttpRedirectCode)
}
if rd.ReplaceKeyWith != "" {
objectName = rd.ReplaceKeyWith
} else if rd.ReplaceKeyPrefixWith != "" {
objectName = rd.ReplaceKeyPrefixWith + strings.TrimPrefix(objectName, rr.Condition.KeyPrefixEquals)
}
http.Redirect(w, r, protocol+"://"+hostName+"/"+objectName, code)
return
}
// Reference: https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/API/RESTBucketPUTwebsite.html
func (w *WebsiteConfiguration) Validate() (error error) {
if w.RedirectAllRequestsTo != nil {
if w.IndexDocument != nil || w.ErrorDocument != nil || w.RoutingRules != nil {
return ErrInvalidWebsiteConfiguration
}
protocol := w.RedirectAllRequestsTo.Protocol
if protocol != "" && protocol != "http" && protocol != "https" {
return ErrInvalidWebsiteRedirectProtocol
}
} else if w.IndexDocument != nil {
if w.IndexDocument.Suffix == "" || strings.Contains(w.IndexDocument.Suffix, "/") {
return ErrInvalidIndexDocumentSuffix
}
if w.ErrorDocument != nil && w.ErrorDocument.Key == "" {
return ErrInvalidErrorDocumentKey
}
if w.RoutingRules != nil {
if len(w.RoutingRules) == 0 {
return ErrMissingRoutingRuleInWebsiteRules
}
if len(w.RoutingRules) > MaxBucketWebsiteRulesCount {
return ErrExceededWebsiteRoutingRulesLimit
}
for _, r := range w.RoutingRules {
redirect := r.Redirect
if redirect == nil {
return ErrMissingRedirectInWebsiteRoutingRule
}
if redirect.Protocol == "" && redirect.HostName == "" && redirect.ReplaceKeyPrefixWith == "" &&
redirect.ReplaceKeyWith == "" && redirect.HttpRedirectCode == "" {
return ErrMissingRedirectElementInWebsiteRoutingRule
}
if redirect.ReplaceKeyPrefixWith != "" && redirect.ReplaceKeyWith != "" {
return ErrDuplicateKeyReplaceTagInWebsiteRoutingRule
}
protocol := redirect.Protocol
if protocol != "" && protocol != "http" && protocol != "https" {
return ErrInvalidWebsiteRedirectProtocol
}
if redirect.HttpRedirectCode != "" {
code, _ := strconv.Atoi(redirect.HttpRedirectCode)
if http.StatusText(code) == "" {
return ErrInvalidHttpRedirectCodeInWebsiteRoutingRule
}
}
}
}
} else {
return ErrInvalidWebsiteConfiguration
}
return
}
func ParseWebsiteConfig(reader io.Reader) (*WebsiteConfiguration, error) {
websiteConfig := new(WebsiteConfiguration)
websiteBuffer, err := ioutil.ReadAll(reader)
if err != nil {
helper.Logger.Error("Unable to read website config body:", err)
return nil, err
}
size := len(websiteBuffer)
if size > MaxBucketWebsiteConfigurationSize {
return nil, ErrEntityTooLarge
}
err = xml.Unmarshal(websiteBuffer, websiteConfig)
if err != nil {
helper.Logger.Error("Unable to parse website config XML body:", err)
return nil, ErrMalformedWebsiteConfiguration
}
err = websiteConfig.Validate()
if err != nil {
return nil, err
}
return websiteConfig, nil
}