tsip_parser_header_Int.jrl 3.15 KB
Newer Older
nextime's avatar
nextime committed
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
/*
* Copyright (C) 2012 Doubango Telecom <http://www.doubango.org>
* License: BSD
* This file is part of Open Source sipML5 solution <http://www.sipml5.org>
*/

var TSIP_HEADER_MAX_FORWARDS_DEFAULT = 70;

// Parse headers: 'Content-Length', 'Expires', 'Max-Forwards', 'Min-Expires', 'Min-SE', 'RSeq'
tsip_header_Int.prototype = Object.create(tsip_header.prototype);
tsip_header_Content_Length.prototype = Object.create(tsip_header_Int.prototype);
tsip_header_Expires.prototype = Object.create(tsip_header_Int.prototype);
tsip_header_Max_Forwards.prototype = Object.create(tsip_header_Int.prototype);
tsip_header_Min_Expires.prototype = Object.create(tsip_header_Int.prototype);
tsip_header_Min_SE.prototype = Object.create(tsip_header_Int.prototype);
tsip_header_RSeq.prototype = Object.create(tsip_header_Int.prototype);

%%{
	machine tsip_machine_parser_header_Int;

	# Includes
	include tsip_machine_utils "./tsip_machine_utils.jrl";
	
	action tag{
		i_tag_start = p;
	}

	action parse_value{
		if(o_hdr){
			o_hdr.i_value = tsk_ragel_parser_get_int(s_str, p, i_tag_start);
		}
	}

	action parse_param{
		if(o_hdr){
			tsk_ragel_add_param(s_str, p, i_tag_start, o_hdr.ao_params);
		}
	}
	
	action is_Content_Length { o_hdr = new tsip_header_Content_Length(); }
	action is_Expires { o_hdr = new tsip_header_Expires(); }
	action is_Max_Forwards { o_hdr = new tsip_header_Max_Forwards(); }
	action is_Min_Expires { o_hdr = new tsip_header_Min_Expires(); }
	action is_Min_SE { o_hdr = new tsip_header_Min_SE(); }
	action is_RSeq { o_hdr = new tsip_header_RSeq(); }

	Int =	(
				( "Content-Length"i | "l"i )%is_Content_Length |
				("Expires"i)%is_Expires |
				("Max-Forwards"i)%is_Max_Forwards |
				("Min-Expires"i)%is_Min_Expires |
				("Min-SE"i)%is_Min_SE |
				("RSeq"i)%is_RSeq
			)
			 HCOLON (DIGIT+)>tag %parse_value ( (SEMI | COMMA) generic_param>tag %parse_param )*;

	action eob{ }
	
	# Entry point
	main := Int :>CRLF @eob;

}%%

%%write data;

function tsip_header_Int(e_type, i_value){
    tsip_header.call(this, e_type);
    this.i_value = i_value;
}

tsip_header_Int.prototype.toString = function(){
    return typeof this.i_value == "undefined" ? null : this.i_value.toString();
};

tsip_header_Int.prototype.Parse = function(s_str){
    var cs = 0;
	var p = 0;
	var pe = s_str.length;
	var eof = pe;
	var data = tsk_buff_str2ib(s_str);
	var i_tag_start;	
	var o_hdr = null;
	
	%%write init;
	%%write exec;
	
	if( cs < %%{ write first_final; }%% ){
		tsk_utils_log_error("Failed to parse header: " + s_str);
		return null;
	}
	
	return o_hdr;
}

function tsip_header_Content_Length(i_value){ tsip_header_Int.call(this, tsip_header_type_e.Content_Length, i_value); }
function tsip_header_Expires(i_value){ tsip_header_Int.call(this, tsip_header_type_e.Expires, i_value); }
function tsip_header_Max_Forwards(i_value){ tsip_header_Int.call(this, tsip_header_type_e.Max_Forwards, i_value); }
function tsip_header_Min_Expires(i_value){ tsip_header_Int.call(this, tsip_header_type_e.Min_Expires, i_value); }
function tsip_header_Min_SE(i_value){ tsip_header_Int.call(this, tsip_header_type_e.Min_SE, i_value); }
function tsip_header_RSeq(i_value){ tsip_header_Int.call(this, tsip_header_type_e.RSeq, i_value); }