thttp_parser_header_Sec_WebSocket_Protocol.rl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2010-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 thttp_header_Sec_WebSocket_Protocol.c
  21. * @brief WebSocket "Sec-WebSocket-Protocol" header.
  22. *
  23. */
  24. #include "tinyhttp/headers/thttp_header_Sec_WebSocket_Protocol.h"
  25. #include "tsk_debug.h"
  26. #include <string.h>
  27. %%{
  28. machine thttp_machine_parser_header_Sec_WebSocket_Protocol;
  29. # Includes
  30. include thttp_machine_utils "./ragel/thttp_machine_utils.rl";
  31. include thttp_machine_ws "./ragel/thttp_machine_ws.rl";
  32. action tag { tag_start = p; }
  33. action eob { }
  34. action add_protocol{
  35. TSK_PARSER_ADD_STRING(hdr_Sec_WebSocket_Protocol->values);
  36. }
  37. Sec_WebSocket_Protocol_Value = token>tag %add_protocol ( COMMA token>tag %add_protocol )*;
  38. # Entry point
  39. main := "Sec-WebSocket-Protocol"i SP* HCOLON SP*<: Sec_WebSocket_Protocol_Value :>CRLF @eob;
  40. }%%
  41. thttp_header_Sec_WebSocket_Protocol_t* thttp_header_Sec_WebSocket_Protocol_create(const char* protocol)
  42. {
  43. return tsk_object_new(THTTP_HEADER_SEC_WEBSOCKET_PROTOCOL_VA_ARGS(protocol));
  44. }
  45. thttp_header_Sec_WebSocket_Protocol_t* thttp_header_Sec_WebSocket_Protocol_create_null()
  46. {
  47. return thttp_header_Sec_WebSocket_Protocol_create(tsk_null);
  48. }
  49. int thttp_header_Sec_WebSocket_Protocol_tostring(const thttp_header_t* header, tsk_buffer_t* output)
  50. {
  51. if(header){
  52. const thttp_header_Sec_WebSocket_Protocol_t *Sec_WebSocket_Protocol = (const thttp_header_Sec_WebSocket_Protocol_t*)header;
  53. if(Sec_WebSocket_Protocol->values){
  54. const tsk_list_item_t* item;
  55. const char* str;
  56. tsk_list_foreach(item, Sec_WebSocket_Protocol->values){
  57. if((str = TSK_STRING_STR(item->data))){
  58. tsk_buffer_append(output, str, tsk_strlen(str));
  59. }
  60. }
  61. }
  62. return 0;
  63. }
  64. return -1;
  65. }
  66. thttp_header_Sec_WebSocket_Protocol_t *thttp_header_Sec_WebSocket_Protocol_parse(const char *data, tsk_size_t size)
  67. {
  68. int cs = 0;
  69. const char *p = data;
  70. const char *pe = p + size;
  71. const char *eof = pe;
  72. thttp_header_Sec_WebSocket_Protocol_t *hdr_Sec_WebSocket_Protocol = thttp_header_Sec_WebSocket_Protocol_create_null();
  73. const char *tag_start = tsk_null;
  74. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  75. %%write data;
  76. (void)(eof);
  77. (void)(thttp_machine_parser_header_Sec_WebSocket_Protocol_first_final);
  78. (void)(thttp_machine_parser_header_Sec_WebSocket_Protocol_error);
  79. (void)(thttp_machine_parser_header_Sec_WebSocket_Protocol_en_main);
  80. %%write init;
  81. %%write exec;
  82. TSK_RAGEL_DISABLE_WARNINGS_END()
  83. if( cs < %%{ write first_final; }%% ){
  84. TSK_DEBUG_ERROR("Failed to parse Sec-WebSocket-Protocol header.");
  85. TSK_OBJECT_SAFE_FREE(hdr_Sec_WebSocket_Protocol);
  86. }
  87. return hdr_Sec_WebSocket_Protocol;
  88. }
  89. //========================================================
  90. // Sec_WebSocket_Protocol header object definition
  91. //
  92. static tsk_object_t* thttp_header_Sec_WebSocket_Protocol_ctor(tsk_object_t *self, va_list * app)
  93. {
  94. thttp_header_Sec_WebSocket_Protocol_t *Sec_WebSocket_Protocol = self;
  95. if(Sec_WebSocket_Protocol){
  96. const char* protocol;
  97. Sec_WebSocket_Protocol->values = tsk_list_create();
  98. THTTP_HEADER(Sec_WebSocket_Protocol)->type = thttp_htype_Sec_WebSocket_Protocol;
  99. THTTP_HEADER(Sec_WebSocket_Protocol)->tostring = thttp_header_Sec_WebSocket_Protocol_tostring;
  100. if((protocol = va_arg(*app, const char*))){
  101. tsk_string_t* str_ver = tsk_string_create(protocol);
  102. if(str_ver){
  103. tsk_list_push_back_data(Sec_WebSocket_Protocol->values, ((void**) &str_ver));
  104. }
  105. }
  106. }
  107. else{
  108. TSK_DEBUG_ERROR("Failed to create new Sec-WebSocket-Protocol header.");
  109. }
  110. return self;
  111. }
  112. static tsk_object_t* thttp_header_Sec_WebSocket_Protocol_dtor(tsk_object_t *self)
  113. {
  114. thttp_header_Sec_WebSocket_Protocol_t *Sec_WebSocket_Protocol = self;
  115. if(Sec_WebSocket_Protocol){
  116. TSK_OBJECT_SAFE_FREE(Sec_WebSocket_Protocol->values);
  117. TSK_OBJECT_SAFE_FREE(THTTP_HEADER_PARAMS(Sec_WebSocket_Protocol));
  118. }
  119. else{
  120. TSK_DEBUG_ERROR("Null Sec_WebSocket_Protocol header.");
  121. }
  122. return self;
  123. }
  124. static const tsk_object_def_t thttp_header_Sec_WebSocket_Protocol_def_s =
  125. {
  126. sizeof(thttp_header_Sec_WebSocket_Protocol_t),
  127. thttp_header_Sec_WebSocket_Protocol_ctor,
  128. thttp_header_Sec_WebSocket_Protocol_dtor,
  129. tsk_null
  130. };
  131. const tsk_object_def_t *thttp_header_Sec_WebSocket_Protocol_def_t = &thttp_header_Sec_WebSocket_Protocol_def_s;