From ad2c8750819f7e236c0baaf7edf2affd50c9d0a0 Mon Sep 17 00:00:00 2001 From: David Troy Date: Sat, 1 Apr 2006 16:56:25 +0000 Subject: git-svn-id: http://svncommunity.digium.com/svn/astmanproxy/branches/1.20pre@34 f02b47b9-160a-0410-81a6-dc3441afb0ec --- src/xml.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 src/xml.c (limited to 'src/xml.c') diff --git a/src/xml.c b/src/xml.c new file mode 100644 index 0000000..0d6737b --- /dev/null +++ b/src/xml.c @@ -0,0 +1,163 @@ +/* Asterisk Manager Proxy + Copyright (c) 2005 David C. Troy + + This program is free software, distributed under the terms of + the GNU General Public License. + + XML I/O Handler +*/ + +#include "astmanproxy.h" + +#define XML_UNPARSED "UnparsedText" +#define XML_BEGIN_INPUT "" +#define XML_END_INPUT "" + +#define XML_SERVERTAG "AsteriskManagerOutput" +#define XML_PROXYTAG "AsteriskManagerProxyOutput" + +void xml_quote_string(char *s, char *o); +int ParseXMLInput(char *xb, struct message *m); + +int _read(struct mansession *s, struct message *m) { + + /* Note: No single line may be longer than MAX_LEN/s->inbuf, as per get_input */ + /* No XML Input may be longer than BUFSIZE */ + + char line[MAX_LEN], xmlbuf[BUFSIZE]; + int res; + + /* first let's read the whole xml block into our buffer */ + memset(xmlbuf, 0, sizeof xmlbuf); + for (;;) { + memset(line, 0, sizeof line); + res = get_input(s, line); + + if (res > 0) { + if (*line == '\0' ) { + break; + } else if (strlen(xmlbuf) < (BUFSIZE - strlen(line)) ) + strcat(xmlbuf, line); + } else if (res < 0) + return res; + } + + /* now, let's transform and copy into a standard message block */ + debugmsg("Got xml: %s", xmlbuf); + res = ParseXMLInput(xmlbuf, m); + + if (res < 0) + proxyerror_do(s, "Invalid XML Input"); + + /* Return res>0 to process block, return res<0 to kill client, res=0, continue */ + return res; +} + +void *setdoctag(char *tag, struct mansession *s) { + + /* if message came from a server, say so; otherwise it must be from proxy */ + /* right now there is no such thing as client<->client comms */ + if (s && s->server) + strcpy(tag, XML_SERVERTAG); + else + strcpy(tag, XML_PROXYTAG); + + return 0; +} + +int _write(struct mansession *s, struct message *m) { + int i; + char buf[BUFSIZE], outstring[MAX_LEN*3], xmlescaped[MAX_LEN*3], xmldoctag[MAX_LEN]; + char *dpos, *lpos; + + setdoctag(xmldoctag, m->session); + sprintf(buf, "<%s>\r\n", xmldoctag); + + pthread_mutex_lock(&s->lock); + write(s->fd, buf, strlen(buf)); + + for (i=0; ihdrcount; i++) { + memset(xmlescaped, 0, sizeof xmlescaped); + xml_quote_string(m->headers[i], xmlescaped); + lpos = xmlescaped; + dpos = strstr(lpos, ": "); + if (dpos) { + strcpy(outstring, " <"); + strncat(outstring, lpos, dpos-lpos); + strcat(outstring, " Value=\""); + strncat(outstring, dpos+2, strlen(dpos)-2); + strcat(outstring, "\"/>\r\n"); + } else + sprintf(outstring, " <%s Value=\"%s\"/>\r\n", XML_UNPARSED, lpos); + write(s->fd, outstring, strlen(outstring) ); + } + sprintf(buf, "\r\n\r\n", xmldoctag); + write(s->fd, buf, strlen(buf)); + pthread_mutex_unlock(&s->lock); + + return 0; +} + +int _autodisconnect() { + return 0; +} + + +/* Takes a single manager header line and converts xml entities */ +void xml_quote_string(char *s, char *o) { + + char *c; + c = s; + + do { + if (*c == '<') + strcat(o, "<"); + else if (*c == '>') + strcat(o, ">"); + else if (*c == '&') + strcat(o, "&"); + else if (*c == '"') + strcat(o, """); + else if (*c == '\n') + strcat(o, " "); + else + strncat(o, c, 1); + } while (*(c++)); + + return; +} + +int ParseXMLInput(char *xb, struct message *m) { + char *b, *e, *bt, *et, tag[MAX_LEN], *i; + int res = 0; + + /* just an empty block; go home */ + if ( !(*xb) ) + return 0; + + /* initialize message block */ + memset(m, 0, sizeof(struct message) ); + + b = strstr(xb, XML_BEGIN_INPUT); + e = strstr(xb, XML_END_INPUT); + if (b && e) { + bt = strstr((char *)(b + strlen(XML_BEGIN_INPUT) + 1), "<"); + while (bt < e) { + et = strstr(bt+1, "<"); + memset(tag, 0, sizeof tag); + strncpy(tag, bt, (et-bt) ); + bt = et; + + strncpy( m->headers[m->hdrcount], tag+1, strstr(tag+1," ")-(tag+1) ); + strcat(m->headers[m->hdrcount], ": "); + i = strstr(tag+1, "\"") + 1; + strncat( m->headers[m->hdrcount], i, strstr(i, "\"") - i ); + debugmsg("parsed: %s", m->headers[m->hdrcount]); + m->hdrcount++; + } + res = 1; + } else + res = -1; + + return res; +} -- cgit From 003236f73567a285b35fc8d571ac94fb0bc51278 Mon Sep 17 00:00:00 2001 From: David Troy Date: Sat, 1 Apr 2006 17:55:39 +0000 Subject: git-svn-id: http://svncommunity.digium.com/svn/astmanproxy/branches/1.20pre@50 f02b47b9-160a-0410-81a6-dc3441afb0ec --- src/xml.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/xml.c') diff --git a/src/xml.c b/src/xml.c index 0d6737b..2f3f9d0 100644 --- a/src/xml.c +++ b/src/xml.c @@ -74,7 +74,7 @@ int _write(struct mansession *s, struct message *m) { sprintf(buf, "<%s>\r\n", xmldoctag); pthread_mutex_lock(&s->lock); - write(s->fd, buf, strlen(buf)); + ast_carefulwrite(s->fd, buf, strlen(buf), s->writetimeout); for (i=0; ihdrcount; i++) { memset(xmlescaped, 0, sizeof xmlescaped); @@ -89,10 +89,10 @@ int _write(struct mansession *s, struct message *m) { strcat(outstring, "\"/>\r\n"); } else sprintf(outstring, " <%s Value=\"%s\"/>\r\n", XML_UNPARSED, lpos); - write(s->fd, outstring, strlen(outstring) ); + ast_carefulwrite(s->fd, outstring, strlen(outstring), s->writetimeout); } sprintf(buf, "\r\n\r\n", xmldoctag); - write(s->fd, buf, strlen(buf)); + ast_carefulwrite(s->fd, buf, strlen(buf), s->writetimeout); pthread_mutex_unlock(&s->lock); return 0; -- cgit From 2e59afd9c5cdfcc8fa3f7237668568e9c85a15a0 Mon Sep 17 00:00:00 2001 From: David Troy Date: Fri, 7 Apr 2006 17:27:01 +0000 Subject: git-svn-id: http://svncommunity.digium.com/svn/astmanproxy/branches/1.20pre@92 f02b47b9-160a-0410-81a6-dc3441afb0ec --- src/xml.c | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src/xml.c') diff --git a/src/xml.c b/src/xml.c index 2f3f9d0..72dc4fb 100644 --- a/src/xml.c +++ b/src/xml.c @@ -98,11 +98,6 @@ int _write(struct mansession *s, struct message *m) { return 0; } -int _autodisconnect() { - return 0; -} - - /* Takes a single manager header line and converts xml entities */ void xml_quote_string(char *s, char *o) { -- cgit