mirror of
https://gitlab.com/ric_harvey/MailHog.git
synced 2024-11-27 16:24:04 +00:00
Fix bugs in header parsing
This commit is contained in:
parent
73088ab400
commit
05b50913c1
2 changed files with 44 additions and 25 deletions
|
@ -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)
|
||||||
|
|
||||||
|
if len(x) == 2 {
|
||||||
|
headers, body := x[0], x[1]
|
||||||
hdrs := strings.Split(headers, "\r\n")
|
hdrs := strings.Split(headers, "\r\n")
|
||||||
|
var lastHdr = ""
|
||||||
for _, hdr := range hdrs {
|
for _, hdr := range hdrs {
|
||||||
if(strings.Contains(hdr, ": ")) {
|
if lastHdr != "" && strings.HasPrefix(hdr, " ") {
|
||||||
|
h[lastHdr][len(h[lastHdr])-1] = h[lastHdr][len(h[lastHdr])-1] + hdr
|
||||||
|
} else if strings.Contains(hdr, ": ") {
|
||||||
y := strings.SplitN(hdr, ": ", 2)
|
y := strings.SplitN(hdr, ": ", 2)
|
||||||
key, value := y[0], y[1]
|
key, value := y[0], y[1]
|
||||||
// TODO multiple header fields
|
// TODO multiple header fields
|
||||||
h[key] = []string{value}
|
h[key] = []string{value}
|
||||||
|
lastHdr = key
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Found invalid header: '%s'", hdr)
|
log.Printf("Found invalid header: '%s'", hdr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Content{
|
return &Content{
|
||||||
Size: len(data),
|
Size: len(data),
|
||||||
Headers: h,
|
Headers: h,
|
||||||
Body: body,
|
Body: body,
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return &Content{
|
||||||
|
Size: len(data),
|
||||||
|
Headers: h,
|
||||||
|
Body: x[0],
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,12 +33,16 @@ 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) {
|
||||||
|
if(message.MIME.Parts[p].Headers["Content-Type"].length > 0) {
|
||||||
|
if(message.MIME.Parts[p].Headers["Content-Type"][0].match(/text\/plain;?.*/)) {
|
||||||
part = message.MIME.Parts[p];
|
part = message.MIME.Parts[p];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(!part) part = message.Content;
|
if(!part) part = message.Content;
|
||||||
|
|
||||||
|
@ -49,12 +53,16 @@ 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) {
|
||||||
|
if(message.MIME.Parts[p].Headers["Content-Type"].length > 0) {
|
||||||
|
if(message.MIME.Parts[p].Headers["Content-Type"][0].match(/text\/html;?.*/)) {
|
||||||
part = message.MIME.Parts[p];
|
part = message.MIME.Parts[p];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(!part) part = message.Content;
|
if(!part) part = message.Content;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue