forked from jerryharrison/sendgrid-go
/
inbound.go
205 lines (195 loc) · 5.22 KB
/
inbound.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"mime"
"mime/multipart"
"net/http"
"os"
"strings"
"github.com/sendgrid/rest"
)
type configuration struct {
Endpoint string `json:"endpoint"`
Port string `json:"port"`
}
func loadConfig(path string) configuration {
file, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal("Config File Missing. ", err)
}
var conf configuration
err = json.Unmarshal(file, &conf)
if err != nil {
log.Fatal("Config Parse Error: ", err)
}
return conf
}
func indexHandler(response http.ResponseWriter, request *http.Request) {
fmt.Fprintf(response, "%s", "Hello World")
}
func getBoundary(value string, contentType string) (string, *strings.Reader) {
body := strings.NewReader(value)
bodySplit := strings.Split(string(value), contentType)
scanner := bufio.NewScanner(strings.NewReader(bodySplit[1]))
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
break
}
boundary := lines[0][9:]
return boundary, body
}
func inboundHandler(response http.ResponseWriter, request *http.Request) {
mediaType, params, err := mime.ParseMediaType(request.Header.Get("Content-Type"))
if err != nil {
log.Fatal(err)
}
if strings.HasPrefix(mediaType, "multipart/") {
mr := multipart.NewReader(request.Body, params["boundary"])
parsedEmail := make(map[string]string)
emailHeader := make(map[string]string)
binaryFiles := make(map[string][]byte)
parsedRawEmail := make(map[string]string)
rawFiles := make(map[string]string)
for {
p, err := mr.NextPart()
// We have found an attachment with binary data
if err == nil && p.FileName() != "" {
contents, err := ioutil.ReadAll(p)
if err != nil {
log.Fatal(err)
}
binaryFiles[p.FileName()] = contents
}
if err == io.EOF {
// We have finished parsing, do something with the parsed data
for key, value := range parsedEmail {
fmt.Println("Key:", key, " Value:", value)
}
for key, value := range emailHeader {
fmt.Println("eKey:", key, " eValue:", value)
}
for key, value := range binaryFiles {
fmt.Println("bKey:", key, " bValue:", value)
}
for key, value := range parsedRawEmail {
fmt.Println("rKey:", key, " rValue:", value)
}
for key, value := range rawFiles {
fmt.Println("rfKey:", key, " rfValue:", value)
}
// SendGrid needs a 200 OK response to stop POSTing
response.WriteHeader(http.StatusOK)
return
}
if err != nil {
log.Fatal(err)
}
value, err := ioutil.ReadAll(p)
if err != nil {
log.Fatal(err)
}
header := p.Header.Get("Content-Disposition")
if strings.Contains(header, "filename") != true {
header = header[17 : len(header)-1]
parsedEmail[header] = string(value)
} else {
header = header[11:]
f := strings.Split(header, "=")
parsedEmail[f[1][1:len(f[1])-11]] = f[2][1 : len(f[2])-1]
}
if header == "headers" {
s := strings.Split(string(value), "\n")
var a []string
for _, v := range s {
t := strings.Split(string(v), ": ")
a = append(a, t...)
}
for i := 0; i < len(a)-1; i += 2 {
emailHeader[a[i]] = a[i+1]
}
}
// Since we have parsed the headers, we can delete the original
delete(parsedEmail, "headers")
// We have a raw message
if header == "email" {
boundary, body := getBoundary(string(value), "Content-Type: multipart/mixed; ")
raw := multipart.NewReader(body, boundary)
for {
next, err := raw.NextPart()
if err == io.EOF {
// We have finished parsing
break
}
value, err := ioutil.ReadAll(next)
if err != nil {
log.Fatal(err)
}
header := next.Header.Get("Content-Type")
// Parse the headers
if strings.Contains(header, "multipart/alternative") {
boundary, body := getBoundary(string(value), "Content-Type: multipart/alternative; ")
raw := multipart.NewReader(body, boundary)
for {
next, err := raw.NextPart()
if err == io.EOF {
// We have finished parsing
break
}
value, err := ioutil.ReadAll(next)
if err != nil {
log.Fatal(err)
}
header := next.Header.Get("Content-Type")
parsedRawEmail[header] = string(value)
}
} else {
// It's a base64 encoded attachment
rawFiles[header] = string(value)
}
}
}
// Since we've parsed this header, we can delete the original
delete(parsedEmail, "email")
}
}
}
func main() {
if len(os.Args) > 1 {
// Test Sender
path := os.Args[1]
host := os.Args[2]
file, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal("Check your Filepath. ", err)
}
Headers := make(map[string]string)
Headers["User-Agent"] = "SendGrid-Test"
Headers["Content-Type"] = "multipart/form-data; boundary=xYzZY"
method := rest.Post
request := rest.Request{
Method: method,
BaseURL: host,
Headers: Headers,
Body: file,
}
_, err = rest.API(request)
if err != nil {
log.Fatal("Check your Filepath. ", err)
}
} else {
conf := loadConfig("./conf.json")
http.HandleFunc("/", indexHandler)
http.HandleFunc(conf.Endpoint, inboundHandler)
port := os.Getenv("PORT")
if port == "" {
port = conf.Port
}
http.ListenAndServe(port, nil)
}
}