Fix bugs in header parsing

This commit is contained in:
Ian Kent 2014-04-26 12:16:57 +01:00
parent 73088ab400
commit 05b50913c1
2 changed files with 44 additions and 25 deletions

View file

@ -117,24 +117,35 @@ func PathFromString(path string) *Path {
func ContentFromString(data string) *Content { func ContentFromString(data string) *Content {
log.Printf("Parsing Content from string: '%s'", data) log.Printf("Parsing Content from string: '%s'", data)
x := strings.SplitN(data, "\r\n\r\n", 2) x := strings.SplitN(data, "\r\n\r\n", 2)
headers, body := x[0], x[1]
h := make(map[string][]string, 0) h := make(map[string][]string, 0)
hdrs := strings.Split(headers, "\r\n")
for _, hdr := range hdrs { if len(x) == 2 {
if(strings.Contains(hdr, ": ")) { headers, body := x[0], x[1]
y := strings.SplitN(hdr, ": ", 2) hdrs := strings.Split(headers, "\r\n")
key, value := y[0], y[1] var lastHdr = ""
// TODO multiple header fields for _, hdr := range hdrs {
h[key] = []string{value} if lastHdr != "" && strings.HasPrefix(hdr, " ") {
} else { h[lastHdr][len(h[lastHdr])-1] = h[lastHdr][len(h[lastHdr])-1] + hdr
log.Printf("Found invalid header: '%s'", hdr) } else if strings.Contains(hdr, ": ") {
y := strings.SplitN(hdr, ": ", 2)
key, value := y[0], y[1]
// TODO multiple header fields
h[key] = []string{value}
lastHdr = key
} else {
log.Printf("Found invalid header: '%s'", hdr)
}
}
return &Content{
Size: len(data),
Headers: h,
Body: body,
}
} else {
return &Content{
Size: len(data),
Headers: h,
Body: x[0],
} }
} }
return &Content{
Size: len(data),
Headers: h,
Body: body,
}
} }

View file

@ -33,10 +33,14 @@ mailhogApp.controller('MailCtrl', function ($scope, $http, $sce) {
if(message.MIME) { if(message.MIME) {
for(var p in message.MIME.Parts) { for(var p in message.MIME.Parts) {
if(message.MIME.Parts[p].Headers["Content-Type"][0] == "text/plain") { if("Content-Type" in message.MIME.Parts[p].Headers) {
part = message.MIME.Parts[p]; if(message.MIME.Parts[p].Headers["Content-Type"].length > 0) {
break; if(message.MIME.Parts[p].Headers["Content-Type"][0].match(/text\/plain;?.*/)) {
} part = message.MIME.Parts[p];
break;
}
}
}
} }
} }
@ -49,10 +53,14 @@ mailhogApp.controller('MailCtrl', function ($scope, $http, $sce) {
if(message.MIME) { if(message.MIME) {
for(var p in message.MIME.Parts) { for(var p in message.MIME.Parts) {
if(message.MIME.Parts[p].Headers["Content-Type"][0] == "text/html") { if("Content-Type" in message.MIME.Parts[p].Headers) {
part = message.MIME.Parts[p]; if(message.MIME.Parts[p].Headers["Content-Type"].length > 0) {
break; if(message.MIME.Parts[p].Headers["Content-Type"][0].match(/text\/html;?.*/)) {
} part = message.MIME.Parts[p];
break;
}
}
}
} }
} }