Implemented core multipart support and support in the invite session (re #1070)
 - incoming multipart message will be handled automatically
 - for testing, enable HAVE_MULTIPART_TEST in pjsua_app.c


git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@3243 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjsip/include/pjsip-ua/sip_inv.h b/pjsip/include/pjsip-ua/sip_inv.h
index 122a148..309cded 100644
--- a/pjsip/include/pjsip-ua/sip_inv.h
+++ b/pjsip/include/pjsip-ua/sip_inv.h
@@ -382,6 +382,34 @@
 
 
 /**
+ * This structure represents SDP information in a pjsip_rx_data. Application
+ * retrieve this information by calling #pjsip_rdata_get_sdp_info(). This
+ * mechanism supports multipart message body.
+ */
+typedef struct pjsip_rdata_sdp_info
+{
+    /**
+     * Pointer and length of the text body in the incoming message. If
+     * the pointer is NULL, it means the message does not contain SDP
+     * body.
+     */
+    pj_str_t		 body;
+
+    /**
+     * This will contain non-zero if an invalid SDP body is found in the
+     * message.
+     */
+    pj_status_t		 sdp_err;
+
+    /**
+     * A parsed and validated SDP body.
+     */
+    pjmedia_sdp_session *sdp;
+
+} pjsip_rdata_sdp_info;
+
+
+/**
  * Initialize the invite usage module and register it to the endpoint. 
  * The callback argument contains pointer to functions to be called on 
  * occurences of events in invite sessions.
@@ -874,6 +902,21 @@
 					   pjmedia_sdp_session *sdp,
 					   pjsip_msg_body **p_body);
 
+/**
+ * Retrieve SDP information from an incoming message. Application should
+ * prefer to use this function rather than parsing the SDP manually since
+ * this function supports multipart message body.
+ *
+ * This function will only parse the SDP once, the first time it is called
+ * on the same message. Subsequent call on the same message will just pick
+ * up the already parsed SDP from the message.
+ *
+ * @param rdata		The incoming message.
+ *
+ * @return		The SDP info.
+ */
+PJ_DECL(pjsip_rdata_sdp_info*) pjsip_rdata_get_sdp_info(pjsip_rx_data *rdata);
+
 
 PJ_END_DECL
 
diff --git a/pjsip/include/pjsip.h b/pjsip/include/pjsip.h
index 4f74d2a..7108577 100644
--- a/pjsip/include/pjsip.h
+++ b/pjsip/include/pjsip.h
@@ -28,6 +28,7 @@
 #include <pjsip/sip_uri.h>
 #include <pjsip/sip_tel_uri.h>
 #include <pjsip/sip_msg.h>
+#include <pjsip/sip_multipart.h>
 #include <pjsip/sip_parser.h>
 
 /* Core */
diff --git a/pjsip/include/pjsip/sip_multipart.h b/pjsip/include/pjsip/sip_multipart.h
new file mode 100644
index 0000000..12a27ba
--- /dev/null
+++ b/pjsip/include/pjsip/sip_multipart.h
@@ -0,0 +1,179 @@
+/* $Id$ */
+/* 
+ * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
+ */
+#ifndef __PJSIP_SIP_MULTIPART_H__
+#define __PJSIP_SIP_MULTIPART_H__
+
+/**
+ * @file pjsip/sip_multipart.h
+ * @brief Multipart support.
+ */
+
+#include <pjsip/sip_msg.h>
+
+PJ_BEGIN_DECL
+
+/**
+ * @defgroup PJSIP_MULTIPART Multipart message bodies.
+ * @ingroup PJSIP_MSG
+ * @brief Support for multipart message bodies.
+ * @{
+ */
+
+/**
+ * This structure describes the individual body part inside a multipart
+ * message body. It mainly contains the message body itself and optional
+ * headers.
+ */
+typedef struct pjsip_multipart_part
+{
+    /**
+     * Standard list element.
+     */
+    PJ_DECL_LIST_MEMBER(struct pjsip_multipart_part);
+
+    /**
+     * Optional message headers.
+     */
+    pjsip_hdr		    hdr;
+
+    /**
+     * Pointer to the message body.
+     */
+    pjsip_msg_body	   *body;
+
+} pjsip_multipart_part;
+
+/**
+ * Create an empty multipart body.
+ *
+ * @param pool		Memory pool to allocate memory from.
+ * @param ctype		Optional MIME media type of the multipart
+ * 			bodies. If not specified, "multipart/mixed"
+ * 			will be used.
+ * @param boundary	Optional string to be set as part boundary.
+ * 			The boundary string excludes the leading
+ * 			hyphens. If this parameter is NULL or empty,
+ * 			a random boundary will be generated.
+ *
+ * @return		Multipart body instance with no part.
+ */
+PJ_DECL(pjsip_msg_body*) pjsip_multipart_create(pj_pool_t *pool,
+						const pjsip_media_type *ctype,
+						const pj_str_t *boundary);
+
+/**
+ * Create an empty multipart part.
+ *
+ * @param pool		The memory pool.
+ *
+ * @return		The multipart part.
+ */
+PJ_DECL(pjsip_multipart_part*) pjsip_multipart_create_part(pj_pool_t *pool);
+
+
+/**
+ * Perform a deep clone to a multipart part.
+ *
+ * @param pool		The memory pool.
+ * @param part		The part to be duplicated.
+ *
+ * @return		Copy of the multipart part.
+ */
+PJ_DECL(pjsip_multipart_part*)
+pjsip_multipart_clone_part(pj_pool_t *pool,
+			   const pjsip_multipart_part *part);
+
+/**
+ * Add a part into multipart bodies.
+ *
+ * @param pool		The memory pool.
+ * @param mp		The multipart bodies.
+ * @param part		The part to be added into the bodies.
+ *
+ * @return		PJ_SUCCESS on success.
+ */
+PJ_DECL(pj_status_t) pjsip_multipart_add_part(pj_pool_t *pool,
+					      pjsip_msg_body *mp,
+					      pjsip_multipart_part *part);
+
+/**
+ * Get the first part of multipart bodies.
+ *
+ * @param mp		The multipart bodies.
+ *
+ * @return		The first part, or NULL if the multipart
+ * 			bodies currently doesn't hold any elements.
+ */
+PJ_DECL(pjsip_multipart_part*)
+pjsip_multipart_get_first_part(const pjsip_msg_body *mp);
+
+/**
+ * Get the next part after the specified part.
+ *
+ * @param mp		The multipart bodies.
+ * @param part		The part.
+ *
+ * @return		The next part, or NULL if there is no other part after
+ * 			the part.
+ */
+PJ_DECL(pjsip_multipart_part*)
+pjsip_multipart_get_next_part(const pjsip_msg_body *mp,
+			      pjsip_multipart_part *part);
+
+/**
+ * Find a body inside multipart bodies which has the specified content type.
+ *
+ * @param mp		The multipart body.
+ * @param content_type	Content type to find.
+ * @param start		If specified, the search will begin at
+ * 			start->next. Otherwise it will begin at
+ * 			the first part in the multipart bodies.
+ *
+ * @return		The first part with the specified content type
+ * 			if found, or NULL.
+ */
+PJ_DECL(pjsip_multipart_part*)
+pjsip_multipart_find_part( const pjsip_msg_body *mp,
+			   const pjsip_media_type *content_type,
+			   const pjsip_multipart_part *start);
+
+/**
+ * Parse multipart message.
+ *
+ * @param pool		Memory pool.
+ * @param buf		Input buffer.
+ * @param len		The buffer length.
+ * @param ctype		Content type of the multipart body.
+ * @param options	Parsing options, must be zero for now.
+ *
+ * @return		Multipart message body.
+ */
+PJ_DECL(pjsip_msg_body*) pjsip_multipart_parse(pj_pool_t *pool,
+					       char *buf, pj_size_t len,
+					       const pjsip_media_type *ctype,
+					       unsigned options);
+
+/**
+ * @}  PJSIP_MULTIPART
+ */
+
+
+PJ_END_DECL
+
+#endif	/* __PJSIP_SIP_MULTIPART_H__ */
diff --git a/pjsip/include/pjsip/sip_parser.h b/pjsip/include/pjsip/sip_parser.h
index 872b87c..8634d3b 100644
--- a/pjsip/include/pjsip/sip_parser.h
+++ b/pjsip/include/pjsip/sip_parser.h
@@ -276,7 +276,7 @@
  *			lines, and two when an error happen the value can
  *			pinpoint the location of the error in the buffer.
  *
- * @return		The instance of the header if parsing was successfull,
+ * @return		The instance of the header if parsing was successful,
  *			or otherwise a NULL pointer will be returned.
  */
 PJ_DECL(void*) pjsip_parse_hdr( pj_pool_t *pool, const pj_str_t *hname,
@@ -287,21 +287,25 @@
  * Parse header line(s). Multiple headers can be parsed by this function.
  * When there are multiple headers, the headers MUST be separated by either
  * a newline (as in SIP message) or ampersand mark (as in URI). This separator
- * however is optional for the last header.
+ * is optional for the last header.
  *
- * @param pool		the pool.
- * @param input		the input text to parse, which must be NULL terminated.
- * @param size		the text length.
- * @param hlist		the header list to store the parsed headers. 
+ * @param pool		The pool.
+ * @param input		The input text to parse, which must be NULL terminated.
+ * @param size		The text length.
+ * @param hlist		The header list to store the parsed headers.
  *			This list must have been initialized before calling 
  *			this function.
+ * @param options	Specify 1 here to make parsing stop when error is
+ * 			encountered when parsing the header. Otherwise the
+ * 			error is silently ignored and parsing resumes to the
+ * 			next line.
  * @return		zero if successfull, or -1 if error is encountered. 
  *			Upon error, the \a hlist argument MAY contain 
  *			successfully parsed headers.
  */
-PJ_DECL(pj_status_t) pjsip_parse_headers( pj_pool_t *pool,
-					  char *input, pj_size_t size,
-					  pj_list *hlist );
+PJ_DECL(pj_status_t) pjsip_parse_headers( pj_pool_t *pool, char *input,
+				          pj_size_t size, pjsip_hdr *hlist,
+				          unsigned options);
 
 
 /**
diff --git a/pjsip/include/pjsua-lib/pjsua.h b/pjsip/include/pjsua-lib/pjsua.h
index add306b..7c770e0 100644
--- a/pjsip/include/pjsua-lib/pjsua.h
+++ b/pjsip/include/pjsua-lib/pjsua.h
@@ -1186,10 +1186,26 @@
     pj_str_t	content_type;
 
     /**
-     * Optional message body.
+     * Optional message body to be added to the message, only when the
+     * message doesn't have a body.
      */
     pj_str_t	msg_body;
 
+    /**
+     * Content type of the multipart body. If application wants to send
+     * multipart message bodies, it puts the parts in \a parts and set
+     * the content type in \a multipart_ctype. If the message already
+     * contains a body, the body will be added to the multipart bodies.
+     */
+    pjsip_media_type  multipart_ctype;
+
+    /**
+     * List of multipart parts. If application wants to send multipart
+     * message bodies, it puts the parts in \a parts and set the content
+     * type in \a multipart_ctype. If the message already contains a body,
+     * the body will be added to the multipart bodies.
+     */
+    pjsip_multipart_part multipart_parts;
 };