-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSipMessage.cpp
More file actions
373 lines (311 loc) · 11.1 KB
/
SipMessage.cpp
File metadata and controls
373 lines (311 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "SipMessage.hpp"
#include "SipDefines.hpp"
#include "SipUtility.hpp"
#include <sstream>
#include <boost/regex.hpp>
#include <queue>
using std::string;
using std::ostringstream;
using std::queue;
namespace Sip {
string SipMessage::ToString() const
{
ostringstream stream;
//TODO: Add some sanity checking while creating stream
//TODO: Maybe explicitly process via's first?
for ( vector<SipHeader>::const_iterator header = GetAllHeaders().begin(); header != GetAllHeaders().end(); ++header )
{
if ( strcasecmp( header->header_name.c_str(), "content-length" ) == 0 ) //This is calculated seperately and needs to be processed at the end
continue;
else if ( strcasecmp( header->header_name.c_str(), "via" ) == 0 ) //We handle via seperately because order is important, and we don't comma seperate multiple values, we put them on seperate lines
{
for ( vector<SipHeaderValue>::const_iterator value = header->shvs.begin(); value != header->shvs.end(); ++value )
{
stream << header->header_name << ": " << value->Value();
if ( value->HasTags() )
{
for( map<string, string>::const_iterator tag = value->Tags().begin(); tag != value->Tags().end(); ++tag )
{
stream << ";" << tag->first;
if ( tag->second != "" ) //Maybe completely omit tag if no value?!
stream << "=" << tag->second;
}
}
stream << "\r\n";
}
}
else //Process all other headers, seperating values with a comma SP combo
{
stream << header->header_name << ": ";
bool firstValue = true;
for ( vector<SipHeaderValue>::const_iterator value = header->shvs.begin(); value != header->shvs.end(); ++value )
{
if ( firstValue )
firstValue = false;
else
stream << ", ";
stream << value->Value();
if ( value->HasTags() )
{
for( map<string, string>::const_iterator tag = value->Tags().begin(); tag != value->Tags().end(); ++tag )
{
stream << ";" << tag->first << "=" << tag->second;
}
}
}
stream << "\r\n";
}
}
//Do content length header and body
if ( HasMessageBody() )
{
stream << "Content-Length: " << GetMessageBody().length() << "\r\n\r\n";
stream << GetMessageBody();
}
else
{
stream << "Content-Length: 0" << "\r\n\r\n";
}
return stream.str();
}
const string& SipMessage::GetOriginalRawMessage() const
{
return this->rawMessage;
}
const vector<SipHeaderValue>& SipMessage::GetHeaderValues( const string& headerName ) const throw( SipMessageException )
{
vector<SipHeader>::const_iterator it;
it = std::find( m_headers.begin(), m_headers.end(), headerName );
if ( it == m_headers.end() )
throw SipMessageException( string( "Header not present: " ) + headerName );
else
return it->shvs;
}
const vector<SipHeader>& SipMessage::GetAllHeaders() const throw()
{
return m_headers;
}
const string& SipMessage::GetMessageBody() const throw( SipMessageException )
{
if ( ! m_hasBody )
throw SipMessageException( string( "GetMessageBody: Message has no body" ) + rawMessage );
else
return this->messageBody;
}
string& SipMessage::ModifyMessageBody() throw ( SipMessageException ) {
if ( ! m_hasBody )
throw SipMessageException( string( "ModifyMessageBody: Message has no body" ) + rawMessage );
else
return this->messageBody;
}
bool SipMessage::HasHeader( const string& headerName ) const throw()
{
return ( std::find( m_headers.begin(), m_headers.end(), headerName ) != m_headers.end() );
}
bool SipMessage::HasMessageBody ( ) const throw()
{
return m_hasBody;
}
void SipMessage::SetMessageBody ( const string& body, const string& rtpMap ) throw( SipMessageException )
{
if ( body == "" || rtpMap == "" )
throw SipMessageException( "SipMessage::SetMessageBody: Bad arguments. Both are required." );
ostringstream lengthAsStringBuilder;
lengthAsStringBuilder << body.length();
vector<SipHeaderValue> headerValues;
headerValues.push_back( SipHeaderValue( rtpMap ) );
SetHeader( "content-type", headerValues );
headerValues.clear();
headerValues.push_back( SipHeaderValue( lengthAsStringBuilder.str() ) );
SetHeader( "content-length", headerValues );
messageBody = body;
m_hasBody = true;
}
vector<SipHeaderValue>& SipMessage::ModifyHeader( const string& headerName)
{
vector<SipHeader>::iterator header = std::find( m_headers.begin(), m_headers.end(), headerName );
if ( header == m_headers.end() ) {
m_headers.push_back( SipHeader() );
header = m_headers.end() - 1;
header->header_name = headerName;
}
return header->shvs;
}
void SipMessage::SetHeader( const string& headerName, const vector<SipHeaderValue>& values ) throw()
{
vector<SipHeader>::iterator header = std::find( m_headers.begin(), m_headers.end(), headerName );
if ( header == m_headers.end() ) {
m_headers.push_back( SipHeader() );
header = m_headers.end() - 1;
header->header_name = headerName;
}
header->shvs = values;
}
void SipMessage::SetHeader( const string& headerName, const string& value ) throw()
{
vector<SipHeaderValue> valueVector;
valueVector.push_back( SipHeaderValue( value ) );
SetHeader( headerName, valueVector );
}
void SipMessage::SetHeader( const string& headerName, const SipHeaderValue& value ) throw()
{
vector<SipHeaderValue> valueVector;
valueVector.push_back( value );
SetHeader( headerName, valueVector );
}
void SipMessage::PushHeader( const string& headerName, const vector<SipHeaderValue>& values ) throw()
{
vector<SipHeader>::iterator header = std::find( m_headers.begin(), m_headers.end(), headerName );
if ( header == m_headers.end() ) {
m_headers.push_back( SipHeader() );
header = m_headers.end() - 1;
header->header_name = headerName;
header->shvs = values;
}
else {
header->shvs.insert( header->shvs.end(), values.begin(), values.end() );
}
}
void SipMessage::PushHeader( const string& headerName, const string& value ) throw()
{
vector<SipHeaderValue> valueVector;
valueVector.push_back( SipHeaderValue( value ) );
PushHeader( headerName, valueVector );
}
void SipMessage::PushHeader( const string& headerName, const SipHeaderValue& value ) throw()
{
vector<SipHeaderValue> valueVector;
valueVector.push_back( value );
PushHeader( headerName, valueVector );
}
void SipMessage::DeleteHeader( const string& headerName ) throw() {
vector<SipHeader>::iterator header = std::find( m_headers.begin(), m_headers.end(), headerName );
if ( header != m_headers.end() ) {
m_headers.erase( header );
}
}
string SipMessage::MassageHeaderKey( string headerName ) const throw()
{
//transform(headerName.begin(), headerName.end(), headerName.begin(), (int(*)(int))tolower ); //Go lowercase
if ( HeaderConversions.HasKey( headerName ) )
headerName = HeaderConversions.GetCase( headerName );
return headerName;
}
void SipMessage::ProcessSipMessage( string::const_iterator start, string::const_iterator end ) throw( SipMessageException )
{
boost::match_results<std::string::const_iterator> regResults;
boost::regex headerExpression( "([\\w\\-]+)[ ]*:[ ]*(.+?)(?:(?:\\r\\n\\r\\n)|(?:\\r\\n(?=\\w)))" ); //(?s) is inline dotall modifier
//boost::regex headerExpression( "([^:]+)\\s*:\\s*(.+?\\r\\n(?:[ \\t]+.+?\\r\\n)*)" ); //Used to grab headers, including multiline headers.
boost::regex contentExpression( "\\r\\n\\r\\n(.+)" );
string key, rawValue, messageBody;
if ( boost::regex_search( start, end, regResults, contentExpression, boost::match_single_line ) == true )
{
messageBody = string( regResults[1].first, regResults[1].second );
end = regResults[1].first;
}
while ( boost::regex_search( start, end, regResults, headerExpression, boost::match_single_line ) == true )
{
key = MassageHeaderKey( string( regResults[1].first, regResults[1].second ) );
rawValue = string( regResults[2].first, regResults[2].second );
ProcessSipHeaderValues( key, rawValue );
start = regResults[0].second;
}
int contentLength = 0;
if ( this->HasHeader( "content-length" ) )
contentLength = atoi( GetHeaderValues("content-length")[0].Value().c_str() );
if ( contentLength > 0 ) //Process content
{
if ( messageBody.length() != (unsigned)contentLength )
throw SipMessageException( "Content-length != actual body length...message corrupt" );
m_hasBody = true;
this->messageBody = messageBody;
}
}
void SipMessage::ProcessSipHeaderValues( const string& headerName, string& rawString ) throw( SipMessageException )
{
boost::regex ws( "\\r\\n" );
//Remove all CRLF's
rawString = boost::regex_replace( rawString, ws, "" );
//Will be used later to see if we need special handling to ignore semicolons in a bracket 'fence' - <>
boost::regex semiURICheck( "^\\s*(?:\".*?\")?\\s*<" );
//Used to match with one or more tags
boost::regex tagsAreaMatch( "((?:;[^;\\?]+)*)(?:\\?.*)?$" );
boost::match_results<std::string::const_iterator> regResults;
queue<string> elements;
CSVSeperate( rawString, elements );
while ( !elements.empty() )
{
string value, rawTags;
string element = elements.front();
elements.pop();
//Ok, check if there is a uri that might so we can ignore inner semicolons
boost::cmatch matches;
if ( boost::regex_search( element.c_str(), matches, semiURICheck ) )
{ //Ok, lets weed out the area within the <>
//Find position of >. If not found, throw and exception.
//grab from > to first semicolon or end, that will be 'value'
//if a semicolon is found, everything including and after that will be raw tags.
string::size_type rightBracketPosition = element.find( '>', 0 );
if ( rightBracketPosition == string::npos )
throw SipMessageException( string( "Invalid data for (URI?) header: " ) + headerName );
if ( boost::regex_search( (char * ) (element.c_str() + rightBracketPosition), matches, tagsAreaMatch ) && string( matches[1].first, matches[1].second ) != "" )
{
rawTags = string( matches[1].first, matches[1].second );
value = string( element.c_str(), matches[1].first );
}
else
{ //No tags found, whole element is just a value
rawTags = "";
value = element;
}
}
else
{ //Should be safe to assume semis only used for tags
if ( boost::regex_search( element.c_str(), matches, tagsAreaMatch ) && string( matches[1].first, matches[1].second ) != "" )
{
rawTags = string( matches[1].first, matches[1].second );
value = string( element.c_str(), matches[1].first );
}
else
{
rawTags = "";
value = element;
}
}
//Trim whitespace from value
boost::regex trimWS( "^\\s+|\\s+$" );
value = boost::regex_replace( value, trimWS, "" );
// At this point, we need thave the value and rawTags seperate and cleaned up
if ( rawTags != "" )
{
map<string, string> tagMap;
Utility::FillTags( rawTags, tagMap );
PushHeader( headerName, SipHeaderValue( value, tagMap ) );
}
else
PushHeader( headerName, SipHeaderValue( value ) );
}
}
void SipMessage::CSVSeperate( const string& rawString, queue<string>& elements )
{
bool inQuotes = false;
string currentString;
for ( uint32_t i=0; i < rawString.length(); ++i )
{
if ( rawString[i] == '"' )
inQuotes = !inQuotes;
if ( !inQuotes && rawString[i] == ',' )
{
elements.push( currentString );
currentString = "";
}
else
currentString += rawString[i];
}
if ( currentString != "" )
elements.push( currentString );
}
//
// UTILITY
//
}; //namespace Sip