Browse Source

end of boundary returned as error

flashmob 6 years ago
parent
commit
b715b24670
2 changed files with 37 additions and 19 deletions
  1. 27 16
      backends/s_mime.go
  2. 10 3
      backends/s_mime_test.go

+ 27 - 16
backends/s_mime.go

@@ -188,6 +188,7 @@ func (p *parser) peek() byte {
 
 // simulate a byte stream
 func (p *parser) inject(input ...[]byte) {
+	p.msgPos = 0
 	p.set(input[0])
 	p.pos = 0
 	p.ch = p.buf[0]
@@ -246,9 +247,12 @@ func (p *parser) boundary(contentBoundary string) (end bool, err error) {
 			// will wait until we get a new buffer
 
 			p.skip(i)
-			p.lastBoundaryPos = p.msgPos - 1 // - uint(len(boundary))
+			p.lastBoundaryPos = p.msgPos // - uint(len(boundary))
 			p.skip(len(boundary))
-			end = p.boundaryEnd()
+			end, err = p.boundaryEnd()
+			if err != nil {
+				return
+			}
 			p.transportPadding()
 			if p.ch != '\n' && p.ch != 0 {
 				err = errors.New("boundary new line expected")
@@ -286,8 +290,11 @@ func (p *parser) boundary(contentBoundary string) (end bool, err error) {
 					// advance the pointer
 					p.skip(len(boundary) - p.boundaryMatched)
 
-					p.lastBoundaryPos = p.msgPos - 1 //- uint(len(boundary))
-					end = p.boundaryEnd()
+					p.lastBoundaryPos = p.msgPos - uint(len(boundary))
+					end, err = p.boundaryEnd()
+					if err != nil {
+						return
+					}
 					p.transportPadding()
 					if p.ch != '\n' && p.ch != 0 {
 						err = errors.New("boundary new line expected")
@@ -302,13 +309,16 @@ func (p *parser) boundary(contentBoundary string) (end bool, err error) {
 }
 
 // is it the end of a boundary?
-func (p *parser) boundaryEnd() bool {
+func (p *parser) boundaryEnd() (result bool, err error) {
 	if p.ch == '-' && p.peek() == '-' {
 		p.next()
 		p.next()
-		return true
+		result = true
 	}
-	return false
+	if p.ch == 0 {
+		err = io.EOF
+	}
+	return
 }
 
 // *LWSP-char
@@ -816,23 +826,24 @@ func (p *parser) mime(parent *mimeHeader, depth string) (err error) {
 		p.addPart(&part, partID)
 
 		if p.isMulti(&part) {
+
 			err = p.multi(&part, partID)
-			part.endingPosBody = p.msgPos + 1 //p.lastBoundaryPos
+			part.endingPosBody = p.lastBoundaryPos
 			if err != nil {
 				break
 			}
 
-			return
+			//return
 		} else {
-			if end, bErr := p.boundary(parent.contentBoundary); bErr != nil {
-				return bErr
-			} else if end {
-				part.endingPosBody = p.lastBoundaryPos
-				return
-			}
-			part.endingPosBody = p.lastBoundaryPos
 
 		}
+		if end, bErr := p.boundary(parent.contentBoundary); bErr != nil {
+			return bErr
+		} else if end {
+			part.endingPosBody = p.lastBoundaryPos
+			return
+		}
+		part.endingPosBody = p.lastBoundaryPos
 		count++
 	}
 	return

+ 10 - 3
backends/s_mime_test.go

@@ -119,12 +119,14 @@ func TestBoundary(t *testing.T) {
 	part.contentBoundary = "-wololo-"
 
 	// in the middle of the string
-	p.inject([]byte("The quick brown fo---wololo-\nx jumped over the lazy dog"))
+	test := "The quick brown fo---wololo-\nx jumped over the lazy dog"
+	p.inject([]byte(test))
 
 	_, err = p.boundary(part.contentBoundary)
 	if err != nil {
 		t.Error(err)
 	}
+	fmt.Println(string(test[:p.lastBoundaryPos]))
 
 	// at the end (with the -- postfix)
 	p.inject([]byte("The quick brown fox jumped over the lazy dog---wololo---\n"))
@@ -144,6 +146,9 @@ func TestBoundary(t *testing.T) {
 	if err != nil {
 		t.Error(err)
 	}
+	all := []byte("The quick brown fox jumped ov---wololo---\ner the lazy dog")
+	//all[p.lastBoundaryPos] = 'X'
+	fmt.Println(string(all[:p.lastBoundaryPos]))
 	for c := p.next(); c != 0; c = p.next() {
 	} // drain
 	// the boundary with an additional buffer in between
@@ -357,7 +362,7 @@ Content-Disposition: attachment;
 ------_=_NextPart_001_01CBE273.65A0E7AA--`
 
 func TestNestedEmail(t *testing.T) {
-	//email = email2
+	email = email2
 	p.inject([]byte(email))
 
 	if err := p.mime(nil, ""); err != nil {
@@ -372,7 +377,9 @@ func TestNestedEmail(t *testing.T) {
 	fmt.Print(email)
 	//fmt.Println(strings.Index(email, "--D7F------------D7FD5A0B8AB9C65CCDBFA872--"))
 
-	//	fmt.Println(email[p.parts[5].startingPosBody:p.parts[5].endingPosBody])
+	//fmt.Println(email[p.parts[1].startingPosBody:p.parts[1].endingPosBody])
+	i := 6
+	fmt.Println("**********{" + email[p.parts[i].startingPosBody:p.parts[i].endingPosBody] + "}**********")
 }
 
 func replaceAtIndex(str string, replacement rune, index uint) string {