forked from Silverfish/proton-bridge
GODT-949: Fix section parsing issue
This commit is contained in:
@ -226,7 +226,7 @@ func buildPGPRFC822(kr *crypto.KeyRing, msg *pmapi.Message, opts JobOptions) ([]
|
||||
|
||||
sigs, err := msg.ExtractSignatures(kr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
log.WithError(err).WithField("id", msg.ID).Warn("Extract signature failed")
|
||||
}
|
||||
|
||||
if len(sigs) > 0 {
|
||||
@ -277,7 +277,7 @@ func buildPGPMIMEFallbackRFC822(msg *pmapi.Message, opts JobOptions) ([]byte, er
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func writeMultipartSignedRFC822(header message.Header, body []byte, sig pmapi.Signature) ([]byte, error) {
|
||||
func writeMultipartSignedRFC822(header message.Header, body []byte, sig pmapi.Signature) ([]byte, error) { //nolint[funlen]
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
header.SetContentType("multipart/signed", map[string]string{
|
||||
|
||||
@ -104,7 +104,7 @@ func (bs *BodyStructure) parseAllChildSections(r io.Reader, currentPath []int, s
|
||||
|
||||
// If multipart, call getAllParts, else read to count lines.
|
||||
if (strings.HasPrefix(mediaType, "multipart/") || mediaType == "message/rfc822") && params["boundary"] != "" {
|
||||
newPath := append(currentPath, 1)
|
||||
nextPath := getChildPath(currentPath)
|
||||
|
||||
var br *boundaryReader
|
||||
br, err = newBoundaryReader(bodyReader, params["boundary"])
|
||||
@ -121,9 +121,9 @@ func (bs *BodyStructure) parseAllChildSections(r io.Reader, currentPath []int, s
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
err = bs.parseAllChildSections(part, newPath, start)
|
||||
err = bs.parseAllChildSections(part, nextPath, start)
|
||||
part.Reset()
|
||||
newPath[len(newPath)-1]++
|
||||
nextPath[len(nextPath)-1]++
|
||||
}
|
||||
br.reader = nil
|
||||
|
||||
@ -152,7 +152,7 @@ func (bs *BodyStructure) parseAllChildSections(r io.Reader, currentPath []int, s
|
||||
(*bs)[path] = info
|
||||
|
||||
// Fix start of subsections.
|
||||
newPath := append(currentPath, 1)
|
||||
newPath := getChildPath(currentPath)
|
||||
shift := info.Size - info.BSize
|
||||
subInfo, err := bs.getInfo(newPath)
|
||||
|
||||
@ -197,6 +197,14 @@ func (bs *BodyStructure) parseAllChildSections(r io.Reader, currentPath []int, s
|
||||
return nil
|
||||
}
|
||||
|
||||
// getChildPath will return the first child path of parent path.
|
||||
// NOTE: Return value can be used to iterate over parts so it is necessary to
|
||||
// copy parrent values in order to not rewrite values in parent.
|
||||
func getChildPath(parent []int) []int {
|
||||
// append alloc inline is the fasted way to copy
|
||||
return append(append(make([]int, 0, len(parent)+1), parent...), 1)
|
||||
}
|
||||
|
||||
func stringPathFromInts(ints []int) (ret string) {
|
||||
for i, n := range ints {
|
||||
if i != 0 {
|
||||
|
||||
@ -98,17 +98,12 @@ func TestParseBodyStructurePGP(t *testing.T) {
|
||||
bs, err := NewBodyStructure(bytes.NewReader(b))
|
||||
require.NoError(t, err)
|
||||
|
||||
paths := []string{}
|
||||
haveStructure := map[string]string{}
|
||||
for path := range *bs {
|
||||
paths = append(paths, path)
|
||||
}
|
||||
sort.Strings(paths)
|
||||
|
||||
for _, path := range paths {
|
||||
require.Equal(t, expectedStructure[path], (*bs)[path].Header.Get("Content-Type"))
|
||||
haveStructure[path] = (*bs)[path].Header.Get("Content-Type")
|
||||
}
|
||||
|
||||
require.True(t, len(*bs) == len(expectedStructure), "Wrong number of sections expected %d but have %d", len(expectedStructure), len(*bs))
|
||||
require.Equal(t, expectedStructure, haveStructure)
|
||||
}
|
||||
|
||||
func TestGetSection(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user