-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
85 lines (73 loc) · 1.78 KB
/
request.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
package main
import (
"encoding/base64"
"errors"
"fmt"
"github.com/nu7hatch/gouuid"
"log"
"net/url"
"time"
)
const (
// Fulfillment is a basic request
Fulfillment = "fulfill"
// ErrorGettingPayload is used in error strings
ErrorGettingPayload = "error getting payload"
)
// Request is an instance of an incoming http request that has been processed
// into the constitute parts.
type Request struct {
*RequestPayload
Time int64
Type string
UUID string
}
// RequestPayload are the fundamental pieces of the request with Value being
// extracted from the payload.
type RequestPayload struct {
RemoteAddr string
UserAgent string
Value string
}
// NewRequest takes the payload data and creates a proper request
// with a unique identifier and time filled into a `Request`.
func NewRequest(data *RequestPayload, eventType string) *Request {
now := time.Now()
u, _ := uuid.NewV4()
event := &Request{
RequestPayload: data,
Time: now.Unix(),
Type: eventType,
UUID: u.String(),
}
return event
}
func getPayloadFromQuery(query url.Values, event string) (payload *RequestPayload, err error) {
val, ok := query["p"]
if !ok {
return nil, errors.New(ErrorGettingPayload)
}
decrypted, err := decryptURLBase64(encryptBlock, val[0])
if nil != err {
log.Print("decrypt failed: ", err)
return nil, err
}
payload = &RequestPayload{}
switch event {
case Fulfillment:
payload.Value = string(decrypted)
default:
err := fmt.Errorf("Unknown event: %s", event)
log.Println(err)
return nil, err
}
return payload, err
}
func generateRequestPayload(link string) (result string, err error) {
data, err := encrypt(encryptBlock, []byte(link))
if nil != err {
return "", err
}
result = base64.URLEncoding.EncodeToString(data)
return result, err
}