thttp_parser_header_Sec_WebSocket_Version.rl 4.7 KB

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