blob: d80ba6a16bf4d7c659d1973efc8cb52cee990adc (
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
|
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"
)
// 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) {
_, err := url.Parse(r.PostFormValue("source"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errSrcInvalid))
}
target, err := url.Parse(r.PostFormValue("target"))
if err != nil || !ep.targetAllowed(target) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errTgtNotAccepted))
}
}
// 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)
}
|