summaryrefslogtreecommitdiffstats
path: root/tinyMSRP/ragel/tmsrp_parser_message.rl
blob: 4961158492c818f24dadd38055ded11441888b15 (plain)
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
/*
* Copyright (C) 2009-2015 Mamadou DIOP.
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO 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 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO 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 DOUBANGO.
*
*/
/**@file tmsrp_machine_message.rl
 * @brief Ragel file.
 */
#include "tinymsrp/parsers/tmsrp_parser_message.h"

#include "tinymsrp/headers/tmsrp_header_Dummy.h"
#include "tinymsrp/headers/tmsrp_header_Expires.h"
#include "tinymsrp/headers/tmsrp_header_Max-Expires.h"
#include "tinymsrp/headers/tmsrp_header_Min-Expires.h"
#include "tinymsrp/headers/tmsrp_header_Use-Path.h"
#include "tinymsrp/headers/tmsrp_header_WWW-Authenticate.h"

#include "tsk_string.h"
#include "tsk_memory.h"
#include "tsk_debug.h"

static tsk_bool_t parse_payload(tmsrp_message_t* msrp_msg, const char* tag_start, const char** p, const char* pe, tsk_bool_t* payload_parsed);
static void set_payload(tmsrp_message_t* msrp_msg, const void* ptr, tsk_size_t len);

#define TMSRP_MSG_PARSER_ADD_HEADER(name) \
	if((header = (tmsrp_header_t*)tmsrp_header_##name##_parse(tag_start, (p - tag_start)))){ \
		tmsrp_message_add_header(msrp_msg, header); \
		header = tsk_object_unref(header); \
	}
%%{
	machine tmsrp_machine_message;
	
	###########################################
	#	Includes
	###########################################
	include tmsrp_machine_utils "./ragel/tmsrp_machine_utils.rl";
	
	action tag{
		tag_start = p;
	}

	###########################################
	#	Actions
	###########################################
	action parse_Authentication_Info{
		//FIXME: TMSRP_MSG_PARSER_ADD_HEADER(Authentication_Info);
		TSK_DEBUG_WARN("Authentication_Info Not implemented");
	}
	
	action parse_Authorization{
		//FIXME: TMSRP_MSG_PARSER_ADD_HEADER(Authorization);
		TSK_DEBUG_WARN("Authorization Not implemented");
	}
	
	action parse_Byte_Range{
		TMSRP_MSG_PARSER_ADD_HEADER(Byte_Range);
	}
	
	action parse_Content_Type{
		TMSRP_MSG_PARSER_ADD_HEADER(Content_Type);
	}
	
	action parse_Expires{
		TMSRP_MSG_PARSER_ADD_HEADER(Expires);
	}
	
	action parse_Failure_Report{
		TMSRP_MSG_PARSER_ADD_HEADER(Failure_Report);
	}
	
	action parse_From_Path{
		TMSRP_MSG_PARSER_ADD_HEADER(From_Path);
	}
	
	action parse_Max_Expires{
		TMSRP_MSG_PARSER_ADD_HEADER(Max_Expires);
	}
	
	action parse_Message_ID{
		TMSRP_MSG_PARSER_ADD_HEADER(Message_ID);
	}
	
	action parse_Min_Expires{
		TMSRP_MSG_PARSER_ADD_HEADER(Min_Expires);
	}
	
	action parse_Status{
		TMSRP_MSG_PARSER_ADD_HEADER(Status);
	}
	
	action parse_Success_Report{
		TMSRP_MSG_PARSER_ADD_HEADER(Success_Report);
	}
	
	action parse_To_Path{
		TMSRP_MSG_PARSER_ADD_HEADER(To_Path);
	}
	
	action parse_Use_Path{
		TMSRP_MSG_PARSER_ADD_HEADER(Use_Path);
	}
	
	action parse_WWW_Authenticate{
		TMSRP_MSG_PARSER_ADD_HEADER(WWW_Authenticate);
	}
	
	action parse_Dummy{
		TMSRP_MSG_PARSER_ADD_HEADER(Dummy);
	}
	
	action parse_tid{
		TSK_PARSER_SET_STRING(msrp_msg->tid);
	}

	action parse_method{
		if(msrp_msg->type == tmsrp_unknown){
			msrp_msg->type = tmsrp_request;
			TSK_PARSER_SET_STRING(msrp_msg->line.request.method);
			 msrp_msg->line.request.type = tmsrp_request_get_type(msrp_msg->line.request.method);
		}
		else{
			//cs = %%{ write first_final; }%%;
			cs = tmsrp_machine_message_error;
			TSK_DEBUG_ERROR("Message type already defined.");
		}
	}


	action parse_status_code{
		if(msrp_msg->type == tmsrp_unknown){
			msrp_msg->type = tmsrp_response;
			TSK_PARSER_SET_INT(msrp_msg->line.response.status);
		}
		else{
			//cs = %%{ write first_final; }%%;
			cs = tmsrp_machine_message_error;
			TSK_DEBUG_ERROR("Message type already defined.");
		}
	}
	action parse_comment{
		TSK_PARSER_SET_STRING(msrp_msg->line.response.comment);
	}

	
	action try_parse_data{
		parse_payload(msrp_msg, tag_start, &p, pe, &payload_parsed); // will update "p"
	}

	action parse_data{
		// if the msrp message contain a valid content-type, then gob it otherwise continue until we reach the endline
		int len;
		if(parse_payload(msrp_msg, tag_start, &p, pe, &payload_parsed)){ // will update "p"
			// (This space left deliberately blank)
		}
		else if((len = (int)(p  - tag_start))>0){
			set_payload(msrp_msg, tag_start, (tsk_size_t)len);
		}
	}

	action parse_endtid{
		TSK_PARSER_SET_STRING(msrp_msg->end_line.tid);
	}

	action parse_cflag{
		if(tag_start){
			msrp_msg->end_line.cflag = *tag_start;
		}
		else msrp_msg->end_line.cflag = '#';
	}

	action outside_endline{
		*msg_size = (p - (const char*)input) + 1;
	}
	
	action into_endline{
		into_endline = tsk_true;
	}
	action endtid_match{
		( into_endline || (((pe-p) >7/*seven hyphens*/) && (msrp_msg->tid) && tsk_strniequals(msrp_msg->tid, (p+7), tsk_strlen(msrp_msg->tid))) )
	}


	###########################################
	#	Headers
	###########################################

	Authentication_Info = "Authentication-Info:"i SP any* :>CRLF %parse_Authentication_Info;
	Authorization		= "Authorization:"i SP any* :>CRLF %parse_Authorization;
	Byte_Range			= "Byte-Range:"i SP any* :>CRLF %parse_Byte_Range;
	Content_Type		= "Content-Type:"i SP any* :>CRLF %parse_Content_Type;
	Expires				= "Expires:"i SP any* :>CRLF %parse_Expires;
	Failure_Report		= "Failure-Report:"i SP any* :>CRLF %parse_Failure_Report ;
	From_Path			= "From-Path:"i SP any* :>CRLF %parse_From_Path ;
	Max_Expires			= "Max-Expires:"i SP any* :>CRLF %parse_Max_Expires;
	Message_ID			= "Message-ID:"i SP any* :>CRLF %parse_Message_ID;
	Min_Expires			= "Min-Expires:"i SP any* :>CRLF %parse_Min_Expires;
	Status				= "Status:"i SP any* :>CRLF %parse_Status;
	Success_Report		= "Success-Report:"i SP any* :>CRLF %parse_Success_Report;
	To_Path				= "To-Path:"i SP any* :>CRLF %parse_To_Path;
	Use_Path			= "Use-Path:"i SP any* :>CRLF %parse_Use_Path;
	WWW_Authenticate	= "WWW-Authenticate:"i SP any* :>CRLF %parse_WWW_Authenticate;
	
	Dummy				= hname ":" SP hval :>CRLF %parse_Dummy;
	
	header = (Authentication_Info | Authorization | Byte_Range | Content_Type | Expires | Failure_Report | From_Path | Max_Expires | Message_ID | Min_Expires | Status | Success_Report | To_Path | Use_Path | WWW_Authenticate)>tag @10 | (Dummy>tag) @0;

	#headers = To_Path From_Path ( header )*;
	headers = ( header )*;

	###########################################
	#	Utils
	###########################################
	transact_id	= ident;
	method = UPALPHA*;
	status_code	= DIGIT{3};
	comment	= utf8text;
	
	continuation_flag = "+" | "$" | "#";
	end_line = "-------" transact_id>tag %parse_endtid continuation_flag>tag %parse_cflag CRLF;

	Other_Mime_header = Dummy;

	data = any*;

	###########################################
	#	Request
	###########################################
	req_start = "MSRP" SP transact_id>tag %parse_tid SP method>tag %parse_method CRLF;
	#content_stuff = (Other_Mime_header)* CRLF data>tag %parse_data :>CRLF;
	content_stuff = data>tag >try_parse_data %parse_data;
	msrp_request = req_start headers>10 (CRLF content_stuff CRLF)?>5 :>end_line when endtid_match >into_endline;

	###########################################
	#	Response
	###########################################
	resp_start = "MSRP" SP transact_id>tag %parse_tid SP status_code>tag %parse_status_code (SP comment>tag %parse_comment)? CRLF;
	msrp_response = resp_start headers end_line;

	###########################################
	#	Message
	###########################################
	msrp_req_or_resp = (msrp_request | msrp_response)>1 @outside_endline any*>0;

	###########################################
	#	Entry Point
	###########################################
	main := msrp_req_or_resp;
}%%

TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
/* Ragel data */
%% write data;
TSK_RAGEL_DISABLE_WARNINGS_END()

tmsrp_message_t* tmsrp_message_parse(const void *input, tsk_size_t size)
{
	tsk_size_t msg_size;
	return tmsrp_message_parse_2(input, size, &msg_size);
}

tmsrp_message_t* tmsrp_message_parse_2(const void *input, tsk_size_t size, tsk_size_t* msg_size)
{
	tmsrp_message_t* msrp_msg = tsk_null;
	const char* tag_start = tsk_null;
	tmsrp_header_t* header = tsk_null;
	tsk_bool_t into_endline = tsk_false;
	tsk_bool_t payload_parsed = tsk_false;
	
	/* Ragel variables */
	int cs = 0;
	const char* p = input;
	const char* pe = p + size;
	const char* eof = tsk_null;

	(void)(eof);
	(void)(tmsrp_machine_message_first_final);
	(void)(tmsrp_machine_message_error);
	(void)(tmsrp_machine_message_en_main);

	*msg_size = 0;

	if(!input || !size){
		//TSK_DEBUG_ERROR("Null or empty buffer."); // --> very common case(stream): do not bother us...
		goto bail;
	}

	if(!(msrp_msg = tmsrp_message_create_null())){
		goto bail;
	}

	TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
	/* Ragel init */
	%% write init;

	/* Ragel execute */
	%% write exec;
	TSK_RAGEL_DISABLE_WARNINGS_END()

	/* Check result */
	if( cs < %%{ write first_final; }%% ){
		//TSK_DEBUG_ERROR("Failed to parse MSRP message."); --> very common case(stream): do not bother us...
		TSK_OBJECT_SAFE_FREE(msrp_msg);
		goto bail;
	}
	
bail:
	return msrp_msg;
}

static tsk_bool_t parse_payload(tmsrp_message_t* msrp_msg, const char* tag_start, const char** p, const char* pe, tsk_bool_t* payload_parsed)
{
	int64_t payload_len, endline_len;
	tsk_bool_t can_parse_payload;

	if(*payload_parsed){
		TSK_DEBUG_INFO("payload already parsed");
		return tsk_true;
	}

	if(pe && p && *p && msrp_msg && (can_parse_payload = TMSRP_HEADER_BYTE_RANGE_IS_VALID(msrp_msg->ByteRange))){
		payload_len = (msrp_msg->ByteRange->end - msrp_msg->ByteRange->start) + 1;
		endline_len = 2/*CRLF*/ + 7/*hyphens*/ + tsk_strlen(msrp_msg->tid) + 2/*CRLF*/;
		can_parse_payload = (pe - tag_start) > (payload_len + endline_len);
		if(can_parse_payload){
			set_payload(msrp_msg, tag_start, (tsk_size_t)payload_len);
			*p = ((tag_start + payload_len) - 1);
			*payload_parsed = tsk_true;
			return tsk_true;
		}
	}
	return tsk_false;
}

static void set_payload(tmsrp_message_t* msrp_msg, const void* ptr, tsk_size_t len)
{
	if(msrp_msg->Content){
		tsk_buffer_cleanup(msrp_msg->Content);
		tsk_buffer_append(msrp_msg->Content, ptr, len);
	}
	else{
		msrp_msg->Content = tsk_buffer_create(ptr, len);
	}
}
OpenPOWER on IntegriCloud