Files
imaginary_mirror/source.go
Peter Chung 4e6ed64134 feat(max-allowed-size): add new option max-allowed-size in bytes (#111)
* feat(max-allowed-size): add new option max-allowed-size in bytes

* fix(max-allowed-size): HEAD response handling

- consider 200~206 as valid HEAD response codes
- do not defer res.Body.Close() of HEAD request
2016-12-17 18:52:58 +00:00

53 lines
1.2 KiB
Go

package main
import (
"net/http"
"net/url"
)
type ImageSourceType string
type ImageSourceFactoryFunction func(*SourceConfig) ImageSource
type SourceConfig struct {
AuthForwarding bool
Authorization string
MountPath string
Type ImageSourceType
AllowedOrigings []*url.URL
MaxAllowedSize int
}
var imageSourceMap = make(map[ImageSourceType]ImageSource)
var imageSourceFactoryMap = make(map[ImageSourceType]ImageSourceFactoryFunction)
type ImageSource interface {
Matches(*http.Request) bool
GetImage(*http.Request) ([]byte, error)
}
func RegisterSource(sourceType ImageSourceType, factory ImageSourceFactoryFunction) {
imageSourceFactoryMap[sourceType] = factory
}
func LoadSources(o ServerOptions) {
for name, factory := range imageSourceFactoryMap {
imageSourceMap[name] = factory(&SourceConfig{
Type: name,
MountPath: o.Mount,
AuthForwarding: o.AuthForwarding,
Authorization: o.Authorization,
AllowedOrigings: o.AlloweOrigins,
MaxAllowedSize: o.MaxAllowedSize,
})
}
}
func MatchSource(req *http.Request) ImageSource {
for _, source := range imageSourceMap {
if source.Matches(req) {
return source
}
}
return nil
}