/
server_http.go
155 lines (122 loc) · 3.36 KB
/
server_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
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
package main
import (
"errors"
"net/http"
"net/url"
"strings"
"github.com/ajg/form"
)
// ListenHTTP starts a new http (tcp) listener at specified listening address.
func (server *Server) ListenHTTP() error {
http.Handle("/", server)
return http.ListenAndServe(server.GetListenAddress(), nil)
}
// ServeHTTP is entrypoint of all HTTP connections with sould server.
func (server *Server) ServeHTTP(
response http.ResponseWriter, request *http.Request,
) {
defer func() {
err := recover()
if err != nil {
logger.Errorf("(http) PANIC: %s\n%#v\n%s", err, request, stack())
}
}()
logRequest(request)
var method = request.Method
switch {
case method == "POST":
pullRequest, err := ExtractPullRequest(
request.Form, server.insecureMode,
)
if err != nil {
logger.Error(err)
http.Error(response, err.Error(), http.StatusBadRequest)
return
}
logger.Info(pullRequest)
server.HandlePullRequest(response, pullRequest)
case method == "GET" &&
strings.TrimRight(request.URL.Path, "/") == "/x/status":
statusRequest := ExtractStatusRequest(request.URL)
logger.Info(statusRequest)
server.HandleStatusRequest(response, statusRequest)
case method == "GET":
tarRequest, err := ExtractTarRequest(request.URL)
if err != nil {
logger.Error(err)
http.Error(response, err.Error(), http.StatusBadRequest)
return
}
logger.Info(tarRequest)
server.HandleTarRequest(response, tarRequest)
default:
response.WriteHeader(http.StatusMethodNotAllowed)
logger.Errorf("unsupported method: %s", request.Method)
}
}
// ExtractPullRequest parses post form and creates new instance of PullRequest,
// if insecure is false (by default) then ExtractPullRequest will check that
// given mirror origin url is really url.
func ExtractPullRequest(
values url.Values, insecure bool,
) (*PullRequest, error) {
var request PullRequest
err := form.DecodeValues(&request, values)
if err != nil {
return nil, err
}
if request.Spoof {
values.Set("branch", values.Get("branch"))
values.Set("tag", values.Get("tag"))
}
err = validateValues(values)
if err != nil {
return nil, err
}
if !insecure && !isURL(request.MirrorOrigin) {
return nil, errors.New("field 'origin' has an invalid URL")
}
return &request, err
}
func validateValues(values url.Values) error {
for key := range values {
value := strings.TrimSpace(values.Get(key))
if value == "" {
return errors.New("field '" + key + "' has an empty value")
}
if strings.HasPrefix(value, "-") {
return errors.New("field '" + key + "' has an insecure value")
}
}
return nil
}
// ExtractTarRequest parses given URL and extracts request for downloading tar
// archive.
func ExtractTarRequest(url *url.URL) (TarRequest, error) {
request := TarRequest{
MirrorName: strings.Trim(url.Path, "/"),
Reference: strings.TrimSpace(url.Query().Get("ref")),
}
if request.MirrorName == "" {
return request, errors.New("field 'name' has an empty value")
}
if request.Reference == "" {
request.Reference = "master"
}
return request, nil
}
// ExtractStatusRequest returns instance of StatusRequest basing on specified
// URL.
func ExtractStatusRequest(url *url.URL) StatusRequest {
var format string
formatValue := url.Query().Get("format")
switch formatValue {
case "toml", "json":
format = formatValue
default:
format = "hierarchical"
}
return StatusRequest{
format: format,
}
}