blob: 74190752db5b3915927d3d3bbc42769844bffd2d (
plain) (
blame)
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
|
package main
import (
"net/http"
"net/url"
"path"
"strings"
)
const (
errSrcInvalid = "source is not a parsable URL"
errTgtNotAccepted = "can not process webmentions for this target"
errInvalidScheme = "URL scheme is not HTTP(S)"
)
// endpoint is a webmention receiver.
type endpoint struct {
allowPrefix string // host (or host:port) and path prefix for the targets served by this endpoint
}
// ServeHTTP is http.Handler implementation.
func (ep endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
source, err := url.Parse(r.PostFormValue("source"))
if err != nil || source.Host == "" {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(errSrcInvalid))
return
}
if source.Scheme != "http" && source.Scheme != "https" {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(errInvalidScheme))
return
}
target, err := url.Parse(r.PostFormValue("target"))
if err != nil || !ep.targetAllowed(target) {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(errTgtNotAccepted))
return
}
if target.Scheme != "http" && target.Scheme != "https" {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(errInvalidScheme))
return
}
}
// targetAllowed shows whether ep can accept a webmention for the target.
func (ep endpoint) targetAllowed(target *url.URL) bool {
if !strings.HasSuffix(ep.allowPrefix, "/") {
ep.allowPrefix = ep.allowPrefix + "/"
}
tgt := path.Join(target.Host, target.Path)
if !strings.HasSuffix(tgt, "/") {
tgt = tgt + "/"
}
return strings.HasPrefix(tgt, ep.allowPrefix)
}
|