forked from pierrre/imageserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
112 lines (102 loc) · 2.89 KB
/
http.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
// Package http provides a imageserver.Server implementation that gets the Image from an HTTP URL.
package http
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/Destinia/imageserver"
imageserver_source "github.com/Destinia/imageserver/source"
)
// Server is a imageserver.Server implementation that gets the Image from an HTTP URL.
//
// It parses the "source" param as URL, then do a GET request.
// It returns an error if the HTTP status code is not 200 (OK).
type Server struct {
// Client is an optional HTTP client.
// http.DefaultClient is used by default.
Client *http.Client
// Identify identifies the Image format.
// By default, it uses IdentifyHeader().
Identify func(resp *http.Response, data []byte) (format string, err error)
}
// Get implements imageserver.Server.
func (srv *Server) Get(params imageserver.Params) (*imageserver.Image, error) {
resp, err := srv.doRequest(params)
if err != nil {
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()
data, err := loadData(resp)
if err != nil {
return nil, err
}
format, err := srv.identify(resp, data)
if err != nil {
return nil, err
}
return &imageserver.Image{
Format: format,
Data: data,
}, nil
}
func (srv *Server) doRequest(params imageserver.Params) (*http.Response, error) {
src, err := params.GetString(imageserver_source.Param)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", src, nil)
if err != nil {
return nil, newSourceError(err.Error())
}
c := srv.Client
if c == nil {
c = http.DefaultClient
}
response, err := c.Do(req)
if err != nil {
return nil, newSourceError(err.Error())
}
return response, nil
}
func loadData(resp *http.Response) ([]byte, error) {
if resp.StatusCode != http.StatusOK {
return nil, newSourceError(fmt.Sprintf("HTTP status code %d while downloading", resp.StatusCode))
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, newSourceError(fmt.Sprintf("error while downloading: %s", err))
}
return data, nil
}
func (srv *Server) identify(resp *http.Response, data []byte) (format string, err error) {
idf := srv.Identify
if idf == nil {
idf = IdentifyHeader
}
format, err = idf(resp, data)
if err != nil {
return "", newSourceError(fmt.Sprintf("unable to identify image format: %s", err.Error()))
}
return format, nil
}
func newSourceError(msg string) error {
return &imageserver.ParamError{
Param: imageserver_source.Param,
Message: msg,
}
}
// IdentifyHeader identifies the Image format with the "Content-Type" header.
func IdentifyHeader(resp *http.Response, data []byte) (format string, err error) {
ct := resp.Header.Get("Content-Type")
if ct == "" {
return "", fmt.Errorf("no HTTP \"Content-Type\" header")
}
const pref = "image/"
if !strings.HasPrefix(ct, pref) {
return "", fmt.Errorf("HTTP \"Content-Type\" header does not begin with \"%s\": %s", pref, ct)
}
return ct[len(pref):], nil
}