jabber/xstream.c

Go to the documentation of this file.
00001 /*
00002  * $Id: xstream.c 2 2005-06-13 16:47:24Z bogdan_iancu $
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  *
00018  *  Jabber
00019  *  Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
00020  */
00021 
00022 #include "xode.h"
00023 
00024 /* xode_stream is a way to have a consistent method of handling incoming XML Stream based events... it doesn't handle the generation of an XML Stream, but provides some facilities to help do that */
00025 
00026 
00027 static void _xode_put_expatattribs(xode owner, const char** atts)
00028 {
00029     int i = 0;
00030     if (atts == NULL) return;
00031     while (atts[i] != '\0')
00032     {
00033         xode_put_attrib(owner, atts[i], atts[i+1]);
00034         i += 2;
00035     }
00036 }
00037 
00038 /******* internal expat callbacks *********/
00039 static void _xode_stream_startElement(xode_stream xs, const char* name, const char** atts)
00040 {
00041     xode_pool p;
00042 
00043     /* if xode_stream is bad, get outa here */
00044     if(xs->status > XODE_STREAM_NODE) return;
00045 
00046     if(xs->node == NULL)
00047     {
00048         p = xode_pool_heap(5*1024); /* 5k, typically 1-2k each plus copy of self and workspace */
00049         xs->node = xode_new_frompool(p,name);
00050         _xode_put_expatattribs(xs->node, atts);
00051 
00052         if(xs->status == XODE_STREAM_ROOT)
00053         {
00054             xs->status = XODE_STREAM_NODE; /* flag status that we're processing nodes now */
00055             (xs->f)(XODE_STREAM_ROOT, xs->node, xs->arg); /* send the root, f must free all nodes */
00056             xs->node = NULL;
00057         }
00058     }else{
00059         xs->node = xode_insert_tag(xs->node, name);
00060         _xode_put_expatattribs(xs->node, atts);
00061     }
00062 
00063     /* depth check */
00064     xs->depth++;
00065     if(xs->depth > XODE_STREAM_MAXDEPTH)
00066         xs->status = XODE_STREAM_ERROR;
00067 }
00068 
00069 
00070 static void _xode_stream_endElement(xode_stream xs, const char* name)
00071 {
00072     xode parent;
00073 
00074     /* if xode_stream is bad, get outa here */
00075     if(xs->status > XODE_STREAM_NODE) return;
00076 
00077     /* if it's already NULL we've received </stream>, tell the app and we're outta here */
00078     if(xs->node == NULL)
00079     {
00080         xs->status = XODE_STREAM_CLOSE;
00081         (xs->f)(XODE_STREAM_CLOSE, NULL, xs->arg);
00082     }else{
00083         parent = xode_get_parent(xs->node);
00084 
00085         /* we are the top-most node, feed to the app who is responsible to delete it */
00086         if(parent == NULL)
00087             (xs->f)(XODE_STREAM_NODE, xs->node, xs->arg);
00088 
00089         xs->node = parent;
00090     }
00091     xs->depth--;
00092 }
00093 
00094 
00095 static void _xode_stream_charData(xode_stream xs, const char *str, int len)
00096 {
00097     /* if xode_stream is bad, get outa here */
00098     if(xs->status > XODE_STREAM_NODE) return;
00099 
00100     if(xs->node == NULL)
00101     {
00102         /* we must be in the root of the stream where CDATA is irrelevant */
00103         return;
00104     }
00105 
00106     xode_insert_cdata(xs->node, str, len);
00107 }
00108 
00109 
00110 static void _xode_stream_cleanup(void *arg)
00111 {
00112     xode_stream xs = (xode_stream)arg;
00113 
00114     xode_free(xs->node); /* cleanup anything left over */
00115     XML_ParserFree(xs->parser);
00116 }
00117 
00118 
00119 /* creates a new xode_stream with given pool, xode_stream will be cleaned up w/ pool */
00120 xode_stream xode_stream_new(xode_pool p, xode_stream_onNode f, void *arg)
00121 {
00122     xode_stream newx;
00123 
00124     if(p == NULL || f == NULL)
00125     {
00126         fprintf(stderr,"Fatal Programming Error: xode_streamnew() was improperly called with NULL.\n");
00127         return NULL;
00128     }
00129 
00130     newx = xode_pool_malloco(p, sizeof(_xode_stream));
00131     newx->p = p;
00132     newx->f = f;
00133     newx->arg = arg;
00134 
00135     /* create expat parser and ensure cleanup */
00136     newx->parser = XML_ParserCreate(NULL);
00137     XML_SetUserData(newx->parser, (void *)newx);
00138     XML_SetElementHandler(newx->parser,
00139       (void (*)(void*, const char*, const char**))_xode_stream_startElement,
00140       (void (*)(void*, const char*))_xode_stream_endElement);
00141     XML_SetCharacterDataHandler(newx->parser, 
00142       (void (*)(void*, const char*, int))_xode_stream_charData);
00143     xode_pool_cleanup(p, _xode_stream_cleanup, (void *)newx);
00144 
00145     return newx;
00146 }
00147 
00148 /* attempts to parse the buff onto this stream firing events to the handler, returns the last known status */
00149 int xode_stream_eat(xode_stream xs, char *buff, int len)
00150 {
00151     char *err;
00152     xode xerr;
00153     static char maxerr[] = "maximum node size reached";
00154     static char deeperr[] = "maximum node depth reached";
00155 
00156     if(xs == NULL)
00157     {
00158         fprintf(stderr,"Fatal Programming Error: xode_streameat() was improperly called with NULL.\n");
00159         return XODE_STREAM_ERROR;
00160     }
00161 
00162     if(len == 0 || buff == NULL)
00163         return xs->status;
00164 
00165     if(len == -1) /* easy for hand-fed eat calls */
00166         len = strlen(buff);
00167 
00168     if(!XML_Parse(xs->parser, buff, len, 0))
00169     {
00170         err = (char *)XML_ErrorString(XML_GetErrorCode(xs->parser));
00171         xs->status = XODE_STREAM_ERROR;
00172     }else if(xode_pool_size(xode_get_pool(xs->node)) > XODE_STREAM_MAXNODE || xs->cdata_len > XODE_STREAM_MAXNODE){
00173         err = maxerr;
00174         xs->status = XODE_STREAM_ERROR;
00175     }else if(xs->status == XODE_STREAM_ERROR){ /* set within expat handlers */
00176         err = deeperr;
00177     }else{
00178         err = deeperr;
00179     }
00180 
00181     /* fire parsing error event, make a node containing the error string */
00182     if(xs->status == XODE_STREAM_ERROR)
00183     {
00184         xerr = xode_new("error");
00185         xode_insert_cdata(xerr,err,-1);
00186         (xs->f)(XODE_STREAM_ERROR, xerr, xs->arg);
00187     }
00188 
00189     return xs->status;
00190 }

Generated on Wed May 23 08:01:04 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6