Browse Source

add EndingPos counters

flashmob 6 years ago
parent
commit
5fe9ede924
2 changed files with 24 additions and 16 deletions
  1. 8 4
      mail/mime/mime.go
  2. 16 12
      mail/mime/mime_test.go

+ 8 - 4
mail/mime/mime.go

@@ -98,9 +98,10 @@ type Part struct {
 	StartingPos uint
 	// StartingPosBody is the starting position of the body, after header \n\n
 	StartingPosBody uint
-	// EndingPos is the ending position for the part
+	// EndingPos is the ending position for the part, including the boundary line
 	EndingPos uint
-	// EndingPosBody is thr ending position for the body. Typically identical to EndingPos
+	// EndingPosBody is the ending position for the body, excluding boundary.
+	// I.e EndingPos - len(Boundary Line)
 	EndingPosBody uint
 
 	// Charset holds the character-set the part is encoded in, eg. us-ascii
@@ -827,6 +828,7 @@ func (p *Parser) mime(part *Part, cb string) (err error) {
 		defer func() {
 			if err != MaxNodesErr {
 				part.EndingPosBody = p.lastBoundaryPos
+				part.EndingPos = p.msgPos
 			} else {
 				// remove the unfinished node (edge case)
 				var parts []*Part
@@ -858,8 +860,8 @@ func (p *Parser) mime(part *Part, cb string) (err error) {
 		count++
 		p.addPart(subPart, subPartId)
 		err = p.mime(subPart, part.ContentBoundary)
-		subPart.EndingPosBody = p.msgPos
-		part.EndingPosBody = p.msgPos
+		subPart.EndingPosBody = p.lastBoundaryPos
+		subPart.EndingPos = p.msgPos
 		return
 	}
 	if ct != nil && ct.superType == multipart &&
@@ -884,6 +886,7 @@ func (p *Parser) mime(part *Part, cb string) (err error) {
 			} else if end {
 				// reached the terminating boundary (ends with double dash --)
 				subPart.EndingPosBody = p.lastBoundaryPos
+				subPart.EndingPos = p.msgPos
 				break
 			} else {
 				// process the part boundary
@@ -898,6 +901,7 @@ func (p *Parser) mime(part *Part, cb string) (err error) {
 					subPartId = part.Node + dot + strconv.Itoa(count)
 				} else {
 					subPart.EndingPosBody = p.lastBoundaryPos
+					subPart.EndingPos = p.msgPos
 					subPart, count = p.split(subPart, count)
 					p.addPart(subPart, subPartId)
 					err = p.mime(subPart, part.ContentBoundary)

+ 16 - 12
mail/mime/mime_test.go

@@ -535,20 +535,20 @@ Array
 
 email 2
 
-1  0  121  1763
-1.1  207  302  628
-1.1.1  343  428  445
-1.1.2  485  569  586
-1.2  668  730  1763
-1.2.1  730  959  1763
-1.2.1.1  1045  1140  1501
-1.2.1.1.1  1181  1281  1303
-1.2.1.1.2  1343  1442  1459
-1.2.1.2  1541  1703  1721
+1  0  121  1763  1803
+1.1  207  302  628  668
+1.1.1  343  428  445  485
+1.1.2  485  569  586  628
+1.2  668  730  1763  1803
+1.2.1  730  959  1721  1763
+1.2.1.1  1045  1140  1501  1541
+1.2.1.1.1  1181  1281  1303  1343
+1.2.1.1.2  1343  1442  1459  1501
+1.2.1.2  1541  1703  1721  1763
 */
 func TestNestedEmail(t *testing.T) {
 	p = NewMimeParser()
-	email = email2
+	email = email3
 	//email = strings.Replace(string(email), "\n", "\r\n", -1)
 	p.inject([]byte(email))
 
@@ -568,7 +568,11 @@ func TestNestedEmail(t *testing.T) {
 		//output = replaceAtIndex(output, '#', p.Parts[part].StartingPos)
 		//output = replaceAtIndex(output, '&', p.Parts[part].StartingPosBody)
 		//output = replaceAtIndex(output, '*', p.Parts[part].EndingPosBody)
-		fmt.Println(p.Parts[part].Node + "  " + strconv.Itoa(int(p.Parts[part].StartingPos)) + "  " + strconv.Itoa(int(p.Parts[part].StartingPosBody)) + "  " + strconv.Itoa(int(p.Parts[part].EndingPosBody)))
+		fmt.Println(p.Parts[part].Node +
+			"  " + strconv.Itoa(int(p.Parts[part].StartingPos)) +
+			"  " + strconv.Itoa(int(p.Parts[part].StartingPosBody)) +
+			"  " + strconv.Itoa(int(p.Parts[part].EndingPosBody)) +
+			"  " + strconv.Itoa(int(p.Parts[part].EndingPos)))
 	}
 	//fmt.Print(output)
 	//fmt.Println(strings.Index(output, "--D7F------------D7FD5A0B8AB9C65CCDBFA872--"))