-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive.go
72 lines (60 loc) · 2.2 KB
/
receive.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
package inbox
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/benpate/derp"
"github.com/benpate/hannibal/streams"
"github.com/benpate/re"
"github.com/rs/zerolog/log"
)
// ReceiveRequest reads an incoming HTTP request and returns a parsed and validated ActivityPub activity
func ReceiveRequest(request *http.Request, client streams.Client) (document streams.Document, err error) {
const location = "hannibal.pub.ReceiveRequest"
// Try to read the body from the request
body, err := re.ReadRequestBody(request)
if err != nil {
return streams.NilDocument(), derp.Wrap(err, location, "Error reading body from request")
}
// Debug if necessary
if canDebug() {
if canTrace() {
fmt.Println("")
fmt.Println("------------------------------------------")
fmt.Println("HANNIBAL: Received Request:")
fmt.Println(request.Method + " " + request.URL.String() + " " + request.Proto)
fmt.Println("Host: " + request.Host)
for key, value := range request.Header {
fmt.Println(key + ": " + strings.Join(value, ", "))
}
fmt.Println("")
fmt.Println(string(body))
fmt.Println("------------------------------------------")
fmt.Println("")
} else {
log.Debug().Str("url", request.URL.String()).Msg("Hannibal Inbox: Received Request")
}
}
// Try to retrieve the object from the buffer
document = streams.NilDocument(streams.WithClient(client))
if err := json.Unmarshal(body, &document); err != nil {
log.Err(err).Msg("Hannibal Inbox: Error Unmarshalling JSON")
return streams.NilDocument(), derp.Wrap(err, location, "Error unmarshalling JSON body into ActivityPub document")
}
// Validate the Actor and Public Key
if err := validateRequest(request, document); err != nil {
log.Err(err).Msg("Hannibal Inbox: Invalid Actor or Public Key")
return streams.NilDocument(), derp.Wrap(err, location, "Invalid Actor or Public Key", document.Value())
}
// Logging
if canDebug() {
log.Debug().Str("id", document.ID()).Msg("Hannibal Inbox: Activity Parsed")
if canTrace() {
rawJSON, _ := json.MarshalIndent(document.Value(), "", " ")
log.Trace().RawJSON("document", rawJSON).Send()
}
}
// Return the parsed document to the caller (vöïlä!)
return document, nil
}