Browse code

b/f:core: handle AmMimeBody boundary correctly in addParts

Stefan Sayer authored on 28/04/2014 15:30:55
Showing 2 changed files
... ...
@@ -4,6 +4,7 @@
4 4
 #include "sip/defs.h"
5 5
 
6 6
 #include "log.h"
7
+#include "AmUtils.h"
7 8
 
8 9
 #include <memory>
9 10
 using std::auto_ptr;
... ...
@@ -112,6 +113,22 @@ void AmContentType::clearParams()
112 113
   }
113 114
 }
114 115
 
116
+void AmContentType::resetBoundary()
117
+{
118
+  Params::iterator it = params.begin();
119
+  while (it != params.end()) {
120
+    Params::iterator l_it = it;
121
+    it++;
122
+    if ((*l_it)->type == Param::BOUNDARY)
123
+      delete *l_it;
124
+      params.erase(l_it);
125
+  }
126
+
127
+  params.push_back(new Param(BOUNDARY_str, int2hex(get_random())));
128
+  params.back()->type = Param::BOUNDARY;
129
+  mp_boundary = params.back();
130
+}
131
+
115 132
 void AmMimeBody::clearParts()
116 133
 {
117 134
   while(!parts.empty()){
... ...
@@ -574,12 +591,15 @@ void AmMimeBody::convertToMultipart()
574 591
 {
575 592
   AmContentType n_ct;
576 593
   n_ct.parse(MULTIPART_MIXED); // never fails
594
+  n_ct.resetBoundary();
577 595
 
578 596
   AmMimeBody* n_part = new AmMimeBody(*this);
579 597
   n_part->ct = ct;
580 598
 
581 599
   parts.push_back(n_part);
582 600
   ct = n_ct;
601
+
602
+  content_len = 0;
583 603
 }
584 604
 
585 605
 void AmContentType::setType(const string& t)
... ...
@@ -745,10 +765,14 @@ void AmMimeBody::print(string& buf) const
745 765
     buf += string((const char*)payload,content_len);
746 766
   }
747 767
   else {
768
+
769
+    // if (ct.mp_boundary == NULL)
770
+    //   ct.resetBoundary(); 
771
+
748 772
     for(Parts::const_iterator it = parts.begin();
749 773
 	it != parts.end(); ++it) {
750 774
 
751
-      buf += "--" + ct.mp_boundary->value + CRLF;
775
+      buf += "--" + (ct.mp_boundary != NULL ? ct.mp_boundary->value : string("") ) + CRLF;
752 776
       buf += SIP_HDR_CONTENT_TYPE COLSP + (*it)->getCTHdr() + CRLF;
753 777
       buf += (*it)->hdrs + CRLF;
754 778
       (*it)->print(buf);
... ...
@@ -756,7 +780,7 @@ void AmMimeBody::print(string& buf) const
756 780
     }
757 781
 
758 782
     if(!parts.empty()) {
759
-      buf += "--" + ct.mp_boundary->value + "--" CRLF;
783
+      buf += "--" + (ct.mp_boundary != NULL ? ct.mp_boundary->value : string("") ) + "--" CRLF;
760 784
     }
761 785
   }
762 786
 }
... ...
@@ -63,6 +63,9 @@ struct AmContentType
63 63
 
64 64
   /** Clear and free param list */
65 65
   void clearParams();
66
+
67
+  /** set a random boundary string */
68
+  void resetBoundary();
66 69
 };
67 70
 
68 71
 class AmMimeBody