2004-12-24 18:15:34 +00:00
|
|
|
/**
|
|
|
|
* util.cpp
|
|
|
|
* Yet Another SIP Stack
|
|
|
|
* This file is part of the YATE Project http://YATE.null.ro
|
|
|
|
*
|
|
|
|
* Yet Another Telephony Engine - a fully featured software PBX and IVR
|
2006-05-27 15:08:43 +00:00
|
|
|
* Copyright (C) 2004-2006 Null Team
|
2004-12-24 18:15:34 +00:00
|
|
|
*
|
|
|
|
* 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
|
2006-05-27 15:08:43 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-12-24 18:15:34 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-28 22:46:59 +00:00
|
|
|
#include "util.h"
|
2004-12-24 18:15:34 +00:00
|
|
|
|
2004-12-29 04:02:55 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2004-12-24 18:15:34 +00:00
|
|
|
namespace TelEngine {
|
|
|
|
|
2004-12-29 04:02:55 +00:00
|
|
|
static const char* compactForms[] = {
|
2005-09-16 10:37:57 +00:00
|
|
|
"a", "Accept-Contact",
|
2007-06-04 14:05:57 +00:00
|
|
|
"u", "Allow-Events",
|
2004-12-29 04:02:55 +00:00
|
|
|
"i", "Call-ID",
|
|
|
|
"m", "Contact",
|
|
|
|
"e", "Content-Encoding",
|
|
|
|
"l", "Content-Length",
|
|
|
|
"c", "Content-Type",
|
2007-06-04 14:05:57 +00:00
|
|
|
"o", "Event",
|
2004-12-29 04:02:55 +00:00
|
|
|
"f", "From",
|
2007-06-04 14:05:57 +00:00
|
|
|
"y", "Identity",
|
|
|
|
"n", "Identity-Info",
|
2005-08-18 13:02:10 +00:00
|
|
|
"r", "Refer-To",
|
2005-09-16 10:37:57 +00:00
|
|
|
"b", "Referred-By",
|
|
|
|
"j", "Reject-Contact",
|
|
|
|
"d", "Request-Disposition",
|
|
|
|
"x", "Session-Expires",
|
2004-12-29 04:02:55 +00:00
|
|
|
"s", "Subject",
|
|
|
|
"k", "Supported",
|
|
|
|
"t", "To",
|
|
|
|
"v", "Via",
|
|
|
|
0 };
|
|
|
|
|
|
|
|
// Utility function, returns an uncompacted header name
|
|
|
|
const char* uncompactForm(const char* header)
|
|
|
|
{
|
|
|
|
if (header && header[0] && !header[1]) {
|
|
|
|
char c = header[0];
|
|
|
|
const char **p = compactForms;
|
|
|
|
for (; *p; p += 2)
|
|
|
|
if (**p == c)
|
|
|
|
return *++p;
|
|
|
|
}
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Utility function, returns a compacted header name
|
|
|
|
const char* compactForm(const char* header)
|
|
|
|
{
|
|
|
|
if (header && *header) {
|
|
|
|
const char **p = compactForms;
|
|
|
|
for (; *p; p += 2)
|
|
|
|
if (!::strcasecmp(p[1],header))
|
|
|
|
return p[0];
|
|
|
|
}
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
2005-06-03 12:23:12 +00:00
|
|
|
// Utility function, puts quotes around a string
|
|
|
|
void addQuotes(String& str)
|
|
|
|
{
|
|
|
|
str.trimBlanks();
|
|
|
|
int l = str.length();
|
|
|
|
if ((l < 2) || (str[0] != '"') || (str[l-1] != '"'))
|
|
|
|
str = "\"" + str + "\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Utility function, removes quotes around a string
|
|
|
|
void delQuotes(String& str)
|
|
|
|
{
|
|
|
|
str.trimBlanks();
|
|
|
|
int l = str.length();
|
|
|
|
if ((l >= 2) && (str[0] == '"') && (str[l-1] == '"')) {
|
|
|
|
str = str.substr(1,l-2);
|
|
|
|
str.trimBlanks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-12 18:58:17 +00:00
|
|
|
// Utility function, puts quotes around a string
|
|
|
|
String quote(const String& str)
|
|
|
|
{
|
|
|
|
String tmp(str);
|
|
|
|
addQuotes(tmp);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2006-07-25 22:23:54 +00:00
|
|
|
// Utility function to find a separator not in "quotes" or inside <uri>
|
|
|
|
int findSep(const char* str, char sep, int offs)
|
|
|
|
{
|
|
|
|
if (!(str && sep))
|
|
|
|
return -1;
|
|
|
|
str += offs;
|
|
|
|
bool inQ = false;
|
|
|
|
bool inU = false;
|
2006-08-15 14:39:33 +00:00
|
|
|
char c;
|
2006-10-13 11:30:13 +00:00
|
|
|
for (; (c = *str++) ; offs++) {
|
2006-07-25 22:23:54 +00:00
|
|
|
if (inQ) {
|
|
|
|
if (c == '"')
|
|
|
|
inQ = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (inU) {
|
|
|
|
if (c == '>')
|
|
|
|
inU = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == sep)
|
|
|
|
return offs;
|
|
|
|
switch (c) {
|
|
|
|
case '"':
|
|
|
|
inQ = true;
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
inU = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-12-24 18:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vi: set ts=8 sw=4 sts=4 noet: */
|