tmsrp_parser_uri.rl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2009-2015 Mamadou DIOP.
  3. *
  4. * This file is part of Open Source Doubango Framework.
  5. *
  6. * DOUBANGO is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DOUBANGO is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with DOUBANGO.
  18. *
  19. */
  20. /**@file tmsrp_header_Dummy.c
  21. * @brief MSRP Dummy header.
  22. */
  23. #include "tinymsrp/parsers/tmsrp_parser_uri.h"
  24. #include "tsk_debug.h"
  25. #include "tsk_memory.h"
  26. #include "tsk_string.h"
  27. #include <string.h>
  28. /***********************************
  29. * Ragel state machine.
  30. */
  31. %%{
  32. machine tmsrp_machine_parser_uri;
  33. # Includes
  34. include tmsrp_machine_utils "./ragel/tmsrp_machine_utils.rl";
  35. action tag{
  36. tag_start = p;
  37. }
  38. #/* Sets HOST type */
  39. action is_ipv4 { uri->authority.host_type = tmsrp_host_ipv4; }
  40. action is_ipv6 { uri->authority.host_type = tmsrp_host_ipv6; }
  41. action is_hostname { uri->authority.host_type = tmsrp_host_hostname; }
  42. action parse_scheme{
  43. TSK_PARSER_SET_STRING(uri->scheme);
  44. }
  45. action parse_userinfo{
  46. TSK_PARSER_SET_STRING(uri->authority.userinfo);
  47. }
  48. action parse_host{
  49. TSK_PARSER_SET_STRING(uri->authority.host);
  50. if(uri->authority.host_type == tmsrp_host_ipv6){
  51. tsk_strunquote_2(&uri->authority.host, '[', ']');
  52. }
  53. }
  54. action parse_port{
  55. TSK_PARSER_SET_INT(uri->authority.port);
  56. }
  57. action parse_session_id{
  58. TSK_PARSER_SET_STRING(uri->session_id);
  59. }
  60. action parse_transport{
  61. TSK_PARSER_SET_STRING(uri->transport);
  62. }
  63. action parse_param{
  64. TSK_PARSER_ADD_PARAM(uri->params);
  65. }
  66. #//MSRP-URI = msrp-scheme "://" authority ["/" session-id] ";" transport *( ";" URI-parameter)
  67. #//msrp-scheme = "msrp" / "msrps"
  68. #//session-id = 1*( unreserved / "+" / "=" / "/" )
  69. #//transport = "tcp" / 1*ALPHANUM
  70. #//URI-parameter = token ["=" token]
  71. #//authority = [ userinfo "@" ] host [ ":" port ]
  72. msrp_scheme = ("msrp" | "msrps")>tag %parse_scheme;
  73. userinfo = (unreserved | pct_encoded | sub_delims | ":")* >tag %parse_userinfo;
  74. myhost = ((IPv6reference >is_ipv6)>2 | (IPv4address >is_ipv4)>1 | (hostname >is_hostname)>0)>tag %parse_host;
  75. authority = (userinfo "@")? myhost (":" port>tag %parse_port)?;
  76. session_id = (unreserved | "+" | "=" | "/")+ >tag %parse_session_id;
  77. transport = ("tcp" | alphanum*)>tag %parse_transport;
  78. URI_parameter = (token ("=" token)?)>tag %parse_param;
  79. URI = msrp_scheme "://" authority ("/" session_id)? ";" transport ( ";" URI_parameter)*;
  80. # Entry point
  81. main := URI;
  82. }%%
  83. tmsrp_uri_t *tmsrp_uri_parse(const char *data, tsk_size_t size)
  84. {
  85. int cs = 0;
  86. const char *p = data;
  87. const char *pe = p + size;
  88. const char *eof = pe;
  89. tmsrp_uri_t *uri = tmsrp_uri_create_null();
  90. const char *tag_start = tsk_null;
  91. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  92. %%write data;
  93. (void)(eof);
  94. (void)(tmsrp_machine_parser_uri_first_final);
  95. (void)(tmsrp_machine_parser_uri_error);
  96. (void)(tmsrp_machine_parser_uri_en_main);
  97. %%write init;
  98. %%write exec;
  99. TSK_RAGEL_DISABLE_WARNINGS_END()
  100. if( cs < %%{ write first_final; }%% ){
  101. TSK_DEBUG_ERROR("Failed to parse MSRP/MSRPS header.");
  102. TSK_OBJECT_SAFE_FREE(uri);
  103. }
  104. return uri;
  105. }