From 2b47e672ee4aad5b48d7314eec69438e3e66a2f4 Mon Sep 17 00:00:00 2001 From: marian Date: Mon, 14 Jan 2008 10:00:39 +0000 Subject: [PATCH] Added flag to know when a multipart last boundary was found. git-svn-id: http://voip.null.ro/svn/yate@1622 acf43c95-373e-0410-b603-e72c3f656dc1 --- engine/Mime.cpp | 20 +++++++++++++++----- yatemime.h | 3 ++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/engine/Mime.cpp b/engine/Mime.cpp index b28e2ddd..20b92ff4 100644 --- a/engine/Mime.cpp +++ b/engine/Mime.cpp @@ -601,17 +601,20 @@ void MimeMultipartBody::parse(const char* buf, int len) if (!(buf && len > 0 && getBoundary(boundary))) return; + bool endBody; // Find first boundary: ignore the data before it - findBoundary(buf,len,boundary.c_str(),boundary.length()); + findBoundary(buf,len,boundary.c_str(),boundary.length(),endBody); // Parse for bodies XDebug(DebugInfo,"Start parsing boundary=%s len=%d [%p]", boundary.c_str() + 4,len,this); while (len > 0) { + if (endBody) + break; // Find next boundary. Get the length of data before it // 'start' will point to the beginning of an enclosed body const char* start = buf; - int l = findBoundary(buf,len,boundary.c_str(),boundary.length()); + int l = findBoundary(buf,len,boundary.c_str(),boundary.length(),endBody); XDebug(DebugInfo,"Found %d length body (remain=%d) [%p]",l,len,this); if (l <= 0) continue; @@ -671,7 +674,8 @@ void MimeMultipartBody::parse(const char* buf, int len) XDebug(DebugInfo,"Added ('%s',%p) with %u additional headers [%p]", cType->c_str(),body,body->headers().count(),this); } - XDebug(DebugInfo,"End parsing boundary=%s [%p]",boundary.c_str() + 4,this); + XDebug(DebugInfo,"End parsing boundary=%s%s [%p]",boundary.c_str() + 4, + (endBody && len > 0)?" .Found garbage after body":"",this); } // Parse input buffer for first body boundary or data end @@ -680,11 +684,14 @@ void MimeMultipartBody::parse(const char* buf, int len) // buffer was reached // Return the length of data before the found boundary int MimeMultipartBody::findBoundary(const char*& buf, int& len, - const char* boundary, unsigned int bLen) + const char* boundary, unsigned int bLen, bool& endBody) { - if (len <= 0) + if (len <= 0) { + endBody = true; return 0; + } + endBody = false; unsigned int l = len; int bodyLen = 0; @@ -713,6 +720,7 @@ int MimeMultipartBody::findBoundary(const char*& buf, int& len, if (l > 2 && buf[0] == '-' && buf[1] == '-') { buf += 2; l -= 2; + endBody = true; } // Skip until the end of line or data for (; l; buf++, l--) @@ -725,6 +733,8 @@ int MimeMultipartBody::findBoundary(const char*& buf, int& len, } len = l; + if (!len) + endBody = true; return bodyLen; } diff --git a/yatemime.h b/yatemime.h index ac88fb73..0a1040a9 100644 --- a/yatemime.h +++ b/yatemime.h @@ -513,9 +513,10 @@ protected: private: // Parse input buffer for first body boundary or data end // Advance buffer pass the boundary line and decrease the buffer length + // Set endBody to true if the last boundary was found // Return the length of data before the found boundary int findBoundary(const char*& buf, int& len, - const char* boundary, unsigned int bLen); + const char* boundary, unsigned int bLen, bool& endBody); // Build a boundary string to be used when parsing or building body // Remove quotes if present. Remove trailing blanks // Insert CRLF and boundary marks ('--') before parameter