00001 /* ==================================================================== 00002 * The Kannel Software License, Version 1.0 00003 * 00004 * Copyright (c) 2001-2008 Kannel Group 00005 * Copyright (c) 1998-2001 WapIT Ltd. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * 1. Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * 00015 * 2. Redistributions in binary form must reproduce the above copyright 00016 * notice, this list of conditions and the following disclaimer in 00017 * the documentation and/or other materials provided with the 00018 * distribution. 00019 * 00020 * 3. The end-user documentation included with the redistribution, 00021 * if any, must include the following acknowledgment: 00022 * "This product includes software developed by the 00023 * Kannel Group (http://www.kannel.org/)." 00024 * Alternately, this acknowledgment may appear in the software itself, 00025 * if and wherever such third-party acknowledgments normally appear. 00026 * 00027 * 4. The names "Kannel" and "Kannel Group" must not be used to 00028 * endorse or promote products derived from this software without 00029 * prior written permission. For written permission, please 00030 * contact org@kannel.org. 00031 * 00032 * 5. Products derived from this software may not be called "Kannel", 00033 * nor may "Kannel" appear in their name, without prior written 00034 * permission of the Kannel Group. 00035 * 00036 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 00037 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00038 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00039 * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 00040 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 00041 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00042 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 00043 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00044 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00045 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00046 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00047 * ==================================================================== 00048 * 00049 * This software consists of voluntary contributions made by many 00050 * individuals on behalf of the Kannel Group. For more information on 00051 * the Kannel Group, please see <http://www.kannel.org/>. 00052 * 00053 * Portions of this software are based upon software originally written at 00054 * WapIT Ltd., Helsinki, Finland for the Kannel project. 00055 */ 00056 00057 /* 00058 * mime.h - Implement MIME multipart/related handling 00059 * 00060 * References: 00061 * RFC 2387 (The MIME Multipart/Related Content-type) 00062 * RFC 2025 (Multipurpose Internet Mail Extensions [MIME]) 00063 * 00064 * Here we implement generic parsing functions to handle MIME multipart/related 00065 * messages. Our representation is made by the struct MIMEEntity where each 00066 * entity of the MIME scope is hold. Every entity should at least have headers. 00067 * If the body is a "normal" content-type, then it is stored in Octstr *body. 00068 * if the body is a sub-MIME multipart/related again, then it's single entities 00069 * are parsed and chained into the List *multiparts. Hence items in the list 00070 * are of type MIMEEntity again. We result in a recursive linked represenation 00071 * of the MIME multipart/related structure. 00072 * 00073 * We know about two various multipart types: 00074 * multipart/mixed - where no "order" is associated among the entities 00075 * multipart/related - where the "start" parameter in the Content-Type defines 00076 * the semantically main processing entitiy in the set. 00077 * 00078 * In order to provide a mapping facility between the MIMEEntity representation 00079 * and an Octstr that holds the MIME document we have the two major functions 00080 * mime_octstr_to_entity() and mime_entity_to_octstr() that act as converters. 00081 * 00082 * Using the MIMEEntity representation structure it is easier to handle the 00083 * subsequent MIME entities in case we have a cubed structure in the document. 00084 * 00085 * Stipe Tolj <stolj@wapme.de> 00086 */ 00087 00088 #ifndef MIME_H 00089 #define MIME_H 00090 00091 #include "gwlib/gwlib.h" 00092 00093 00094 /* Define an generic MIME entity structure to be 00095 * used for MIME multipart parsing. */ 00096 typedef struct MIMEEntity MIMEEntity; 00097 00098 00099 /* create and destroy MIME multipart entity */ 00100 MIMEEntity *mime_entity_create(void); 00101 void mime_entity_destroy(MIMEEntity *e); 00102 00103 /* make a copy of a MIME object. */ 00104 MIMEEntity *mime_entity_duplicate(MIMEEntity *e); 00105 00106 00107 /* Replace top-level MIME headers: Old ones removed completetly */ 00108 void mime_replace_headers(MIMEEntity *e, List *headers); 00109 00110 00111 /* Get number of body parts. Returns 0 if this is not 00112 * a multipart object. 00113 */ 00114 int mime_entity_num_parts(MIMEEntity *e); 00115 00116 /* Append a new part to list of body parts. Copy is made*/ 00117 void mime_entity_add_part(MIMEEntity *e, MIMEEntity *part); 00118 00119 /* Get part i in list of body parts. Copy is made*/ 00120 MIMEEntity *mime_entity_get_part(MIMEEntity *e, int i); 00121 00122 /* Replace part i in list of body parts. Old one will be deleted */ 00123 void mime_entity_replace_part(MIMEEntity *e, int i, MIMEEntity *newpart); 00124 00125 /* Remove part i in list of body parts. */ 00126 void mime_entity_remove_part(MIMEEntity *e, int i); 00127 00128 /* Change body element of non-multipart entity. */ 00129 void mime_entity_set_body(MIMEEntity *e, Octstr *body); 00130 00131 /* Returns (copy of) the 'start' element of a multi-part entity. */ 00132 MIMEEntity *mime_multipart_start_elem(MIMEEntity *e); 00133 00134 /* 00135 * Parse the given Octstr that contains a normative text representation 00136 * of the MIME multipart document into our representation strucuture. 00137 * Will return parts of it in case an error occures in the recursive 00138 * call, otherwise will return NULL in case of a fatal error while parsing. 00139 */ 00140 MIMEEntity *mime_octstr_to_entity(Octstr *mime); 00141 00142 /* 00143 * Parse the given List (HTTP hedaer) and Octstr (HTTP body) and create 00144 * a MIME multipart representatin if possible. Return NULL if parsing fails. 00145 */ 00146 MIMEEntity *mime_http_to_entity(List *headers, Octstr *body); 00147 00148 /* 00149 * Convert a given MIME multipart representation structure to 00150 * it's Octstr counterpart and return it. Will return parts of it, 00151 * in case an error occures in a deeper level, otherwise a NULL 00152 * is return in case of a fatal error while convertion. 00153 */ 00154 Octstr *mime_entity_to_octstr(MIMEEntity *m); 00155 00156 /* 00157 * Take a MIME multipart representation and return the global header 00158 * for it. Return a pointer to a HTTP header List. 00159 */ 00160 List *mime_entity_headers(MIMEEntity *m); 00161 00162 /* 00163 * Take a MIME multipart representation and return the highest level 00164 * body encoded to an Octstr to tbe used as HTTP POST body. 00165 * Rerturns an Octstr with the body. 00166 */ 00167 Octstr *mime_entity_body(MIMEEntity *m); 00168 00169 /* 00170 * Dump the structure (hicharchical view) of the MIME representation 00171 * structure into our DEBUG log level facility. 00172 */ 00173 void mime_entity_dump(MIMEEntity *m); 00174 00175 00176 #endif