thttp_parser_header_Sec_WebSocket_Key.rl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_Key.c
  21. * @brief WebSocket "Sec-WebSocket-Key" header.
  22. */
  23. #include "tinyhttp/headers/thttp_header_Sec_WebSocket_Key.h"
  24. #include "tsk_debug.h"
  25. #include <string.h>
  26. %%{
  27. machine thttp_machine_parser_header_Sec_WebSocket_Key;
  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 parse_key{
  34. TSK_PARSER_SET_STRING(hdr_Sec_WebSocket_Key->value);
  35. }
  36. # Entry point
  37. main := "Sec-WebSocket-Key"i SP* HCOLON SP*<: Sec_WebSocket_Key>tag %parse_key :>CRLF @eob;
  38. }%%
  39. thttp_header_Sec_WebSocket_Key_t* thttp_header_Sec_WebSocket_Key_create(const char* value)
  40. {
  41. return tsk_object_new(THTTP_HEADER_SEC_WEBSOCKET_KEY_VA_ARGS(value));
  42. }
  43. thttp_header_Sec_WebSocket_Key_t* thttp_header_Sec_WebSocket_Key_create_null()
  44. {
  45. return thttp_header_Sec_WebSocket_Key_create(tsk_null);
  46. }
  47. int thttp_header_Sec_WebSocket_Key_tostring(const thttp_header_t* header, tsk_buffer_t* output)
  48. {
  49. if(header){
  50. const thttp_header_Sec_WebSocket_Key_t *Sec_WebSocket_Key = (const thttp_header_Sec_WebSocket_Key_t*)header;
  51. if(Sec_WebSocket_Key->value){
  52. return tsk_buffer_append(output, Sec_WebSocket_Key->value, tsk_strlen(Sec_WebSocket_Key->value));
  53. }
  54. return 0;
  55. }
  56. return -1;
  57. }
  58. thttp_header_Sec_WebSocket_Key_t *thttp_header_Sec_WebSocket_Key_parse(const char *data, tsk_size_t size)
  59. {
  60. int cs = 0;
  61. const char *p = data;
  62. const char *pe = p + size;
  63. const char *eof = pe;
  64. thttp_header_Sec_WebSocket_Key_t *hdr_Sec_WebSocket_Key = thttp_header_Sec_WebSocket_Key_create_null();
  65. const char *tag_start = tsk_null;
  66. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  67. %%write data;
  68. (void)(eof);
  69. (void)(thttp_machine_parser_header_Sec_WebSocket_Key_first_final);
  70. (void)(thttp_machine_parser_header_Sec_WebSocket_Key_error);
  71. (void)(thttp_machine_parser_header_Sec_WebSocket_Key_en_main);
  72. %%write init;
  73. %%write exec;
  74. TSK_RAGEL_DISABLE_WARNINGS_END()
  75. if( cs < %%{ write first_final; }%% ){
  76. TSK_DEBUG_ERROR("Failed to parse Sec-WebSocket-Key header.");
  77. TSK_OBJECT_SAFE_FREE(hdr_Sec_WebSocket_Key);
  78. }
  79. return hdr_Sec_WebSocket_Key;
  80. }
  81. //========================================================
  82. // Sec_WebSocket_Key header object definition
  83. //
  84. static tsk_object_t* thttp_header_Sec_WebSocket_Key_ctor(tsk_object_t *self, va_list * app)
  85. {
  86. thttp_header_Sec_WebSocket_Key_t *Sec_WebSocket_Key = self;
  87. if(Sec_WebSocket_Key){
  88. THTTP_HEADER(Sec_WebSocket_Key)->type = thttp_htype_Sec_WebSocket_Key;
  89. THTTP_HEADER(Sec_WebSocket_Key)->tostring = thttp_header_Sec_WebSocket_Key_tostring;
  90. Sec_WebSocket_Key->value = tsk_strdup(va_arg(*app, const char*));
  91. }
  92. else{
  93. TSK_DEBUG_ERROR("Failed to create new Sec-WebSocket-Key header.");
  94. }
  95. return self;
  96. }
  97. static tsk_object_t* thttp_header_Sec_WebSocket_Key_dtor(tsk_object_t *self)
  98. {
  99. thttp_header_Sec_WebSocket_Key_t *Sec_WebSocket_Key = self;
  100. if(Sec_WebSocket_Key){
  101. TSK_FREE(Sec_WebSocket_Key->value);
  102. TSK_OBJECT_SAFE_FREE(THTTP_HEADER_PARAMS(Sec_WebSocket_Key));
  103. }
  104. else{
  105. TSK_DEBUG_ERROR("Null Sec_WebSocket_Key header.");
  106. }
  107. return self;
  108. }
  109. static const tsk_object_def_t thttp_header_Sec_WebSocket_Key_def_s =
  110. {
  111. sizeof(thttp_header_Sec_WebSocket_Key_t),
  112. thttp_header_Sec_WebSocket_Key_ctor,
  113. thttp_header_Sec_WebSocket_Key_dtor,
  114. tsk_null
  115. };
  116. const tsk_object_def_t *thttp_header_Sec_WebSocket_Key_def_t = &thttp_header_Sec_WebSocket_Key_def_s;