thttp_parser_header_Authorization.rl 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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_Authorization.c
  21. * @brief HTTP Authorization header.
  22. */
  23. #include "tinyhttp/headers/thttp_header_Authorization.h"
  24. #include "tinyhttp/parsers/thttp_parser_url.h"
  25. #include "tsk_debug.h"
  26. #include "tsk_memory.h"
  27. #include "tsk_time.h"
  28. #include <string.h>
  29. /***********************************
  30. * Ragel state machine.
  31. */
  32. %%{
  33. machine thttp_machine_parser_header_Authorization;
  34. # Includes
  35. include thttp_machine_utils "./ragel/thttp_machine_utils.rl";
  36. action tag{
  37. tag_start = p;
  38. }
  39. action is_digest{
  40. hdr_Authorization->scheme = tsk_strdup("Digest");
  41. }
  42. action is_basic{
  43. hdr_Authorization->scheme = tsk_strdup("Basic");
  44. }
  45. action is_auth{
  46. THTTP_HEADER(hdr_Authorization)->type = thttp_htype_Authorization;
  47. }
  48. action is_proxy{
  49. THTTP_HEADER(hdr_Authorization)->type = thttp_htype_Proxy_Authorization;
  50. }
  51. action parse_username{
  52. TSK_PARSER_SET_STRING(hdr_Authorization->username);
  53. tsk_strunquote(&hdr_Authorization->username);
  54. }
  55. action parse_realm{
  56. TSK_PARSER_SET_STRING(hdr_Authorization->realm);
  57. tsk_strunquote(&hdr_Authorization->realm);
  58. }
  59. action parse_nonce{
  60. TSK_PARSER_SET_STRING(hdr_Authorization->nonce);
  61. tsk_strunquote(&hdr_Authorization->nonce);
  62. }
  63. action parse_uri{
  64. TSK_PARSER_SET_STRING(hdr_Authorization->uri);
  65. }
  66. action parse_response{
  67. TSK_PARSER_SET_STRING(hdr_Authorization->response);
  68. tsk_strunquote(&hdr_Authorization->response);
  69. }
  70. action parse_algorithm{
  71. TSK_PARSER_SET_STRING(hdr_Authorization->algorithm);
  72. }
  73. action parse_cnonce{
  74. TSK_PARSER_SET_STRING(hdr_Authorization->cnonce);
  75. tsk_strunquote(&hdr_Authorization->cnonce);
  76. }
  77. action parse_opaque{
  78. TSK_PARSER_SET_STRING(hdr_Authorization->opaque);
  79. tsk_strunquote(&hdr_Authorization->opaque);
  80. }
  81. action parse_qop{
  82. TSK_PARSER_SET_STRING(hdr_Authorization->qop);
  83. //tsk_strunquote(&hdr_Authorization->qop);
  84. }
  85. action parse_nc{
  86. TSK_PARSER_SET_STRING(hdr_Authorization->nc);
  87. }
  88. action parse_param{
  89. TSK_PARSER_ADD_PARAM(THTTP_HEADER_PARAMS(hdr_Authorization));
  90. }
  91. action eob{
  92. }
  93. #FIXME: Only Digest (MD5, AKAv1-MD5 and AKAv2-MD5) is supported
  94. qop_value = "auth" | "auth-int" | token;
  95. other_response = (any+);
  96. auth_param = generic_param>tag %parse_param;
  97. username = "username"i EQUAL quoted_string>tag %parse_username;
  98. realm = "realm"i EQUAL quoted_string>tag %parse_realm;
  99. nonce = "nonce"i EQUAL quoted_string>tag %parse_nonce;
  100. digest_uri = "uri"i EQUAL LDQUOT <: (any*)>tag %parse_uri :> RDQUOT;
  101. #dresponse = "response"i EQUAL LDQUOT <: (LHEX{32})>tag %parse_response :> RDQUOT;
  102. dresponse = "response"i EQUAL quoted_string>tag %parse_response;
  103. algorithm = "algorithm"i EQUAL <:token>tag %parse_algorithm;
  104. cnonce = "cnonce"i EQUAL quoted_string>tag %parse_cnonce;
  105. opaque = "opaque"i EQUAL quoted_string>tag %parse_opaque;
  106. message_qop = "qop"i EQUAL qop_value>tag %parse_qop;
  107. nonce_count = "nc"i EQUAL (LHEX{8})>tag %parse_nc;
  108. dig_resp = (username | realm | nonce | digest_uri | dresponse | algorithm | cnonce | opaque | message_qop | nonce_count)@1 | auth_param@0;
  109. digest_response = dig_resp ( COMMA <:dig_resp )*;
  110. credentials = ( ("Digest"i%is_digest | "Basic"i%is_basic) LWS digest_response ) | other_response;
  111. Authorization = ("Authorization"i>is_auth | "Proxy-Authorization"i>is_proxy) HCOLON credentials;
  112. # Entry point
  113. main := Authorization :>CRLF @eob;
  114. }%%
  115. thttp_header_Authorization_t* thttp_header_authorization_create()
  116. {
  117. return tsk_object_new(thttp_header_Authorization_def_t);
  118. }
  119. int thttp_header_Authorization_tostring(const thttp_header_t* header, tsk_buffer_t* output)
  120. {
  121. if(header)
  122. {
  123. const thttp_header_Authorization_t *Authorization = (const thttp_header_Authorization_t*)header;
  124. if(Authorization && Authorization->scheme)
  125. {
  126. if(tsk_striequals(Authorization->scheme, "Basic")){
  127. return tsk_buffer_append_2(output, "%s %s",
  128. Authorization->scheme, Authorization->response);
  129. }
  130. else{
  131. return tsk_buffer_append_2(output, "%s %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
  132. Authorization->scheme,
  133. Authorization->username ? "username=\"" : "",
  134. Authorization->username ? Authorization->username : "",
  135. Authorization->username ? "\"" : "",
  136. Authorization->realm ? ",realm=\"" : "",
  137. Authorization->realm ? Authorization->realm : "",
  138. Authorization->realm ? "\"" : "",
  139. Authorization->nonce ? ",nonce=\"" : "",
  140. Authorization->nonce ? Authorization->nonce : "",
  141. Authorization->nonce ? "\"" : "",
  142. Authorization->uri ? ",uri=\"" : "",
  143. Authorization->uri ? Authorization->uri : "",
  144. Authorization->uri ? "\"" : "",
  145. Authorization->response ? ",response=\"" : "",
  146. Authorization->response ? Authorization->response : "",
  147. Authorization->response ? "\"" : "",
  148. Authorization->algorithm ? ",algorithm=" : "",
  149. Authorization->algorithm ? Authorization->algorithm : "",
  150. Authorization->cnonce ? ",cnonce=\"" : "",
  151. Authorization->cnonce ? Authorization->cnonce : "",
  152. Authorization->cnonce ? "\"" : "",
  153. Authorization->opaque ? ",opaque=\"" : "",
  154. Authorization->opaque ? Authorization->opaque : "",
  155. Authorization->opaque ? "\"" : "",
  156. Authorization->qop ? ",qop=" : "",
  157. Authorization->qop ? Authorization->qop : "",
  158. Authorization->nc ? ",nc=" : "",
  159. Authorization->nc ? Authorization->nc : ""
  160. );
  161. }
  162. }
  163. }
  164. return -1;
  165. }
  166. /**@ingroup thttp_header_group
  167. */
  168. thttp_header_Authorization_t *thttp_header_Authorization_parse(const char *data, tsk_size_t size)
  169. {
  170. int cs = 0;
  171. const char *p = data;
  172. const char *pe = p + size;
  173. const char *eof = pe;
  174. thttp_header_Authorization_t *hdr_Authorization = thttp_header_authorization_create();
  175. const char *tag_start = tsk_null;
  176. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  177. %%write data;
  178. (void)(eof);
  179. (void)(thttp_machine_parser_header_Authorization_first_final);
  180. (void)(thttp_machine_parser_header_Authorization_error);
  181. (void)(thttp_machine_parser_header_Authorization_en_main);
  182. %%write init;
  183. %%write exec;
  184. TSK_RAGEL_DISABLE_WARNINGS_END()
  185. if( cs < %%{ write first_final; }%% ){
  186. TSK_DEBUG_ERROR("Failed to parse Authorization header.");
  187. TSK_OBJECT_SAFE_FREE(hdr_Authorization);
  188. }
  189. return hdr_Authorization;
  190. }
  191. /**@ingroup thttp_header_group
  192. */
  193. thttp_header_Proxy_Authorization_t *thttp_header_Proxy_Authorization_parse(const char *data, tsk_size_t size)
  194. {
  195. return thttp_header_Authorization_parse(data, size);
  196. }
  197. //========================================================
  198. // Authorization header object definition
  199. //
  200. static tsk_object_t* thttp_header_Authorization_ctor(tsk_object_t *self, va_list * app)
  201. {
  202. thttp_header_Authorization_t *Authorization = self;
  203. if(Authorization){
  204. THTTP_HEADER(Authorization)->type = thttp_htype_Authorization;
  205. THTTP_HEADER(Authorization)->tostring = thttp_header_Authorization_tostring;
  206. }
  207. else{
  208. TSK_DEBUG_ERROR("Failed to create new Authorization header.");
  209. }
  210. return self;
  211. }
  212. static tsk_object_t* thttp_header_Authorization_dtor(tsk_object_t *self)
  213. {
  214. thttp_header_Authorization_t *Authorization = self;
  215. if(Authorization){
  216. TSK_FREE(Authorization->scheme);
  217. TSK_FREE(Authorization->username);
  218. TSK_FREE(Authorization->realm);
  219. TSK_FREE(Authorization->nonce);
  220. TSK_FREE(Authorization->uri);
  221. TSK_FREE(Authorization->response);
  222. TSK_FREE(Authorization->algorithm);
  223. TSK_FREE(Authorization->cnonce);
  224. TSK_FREE(Authorization->opaque);
  225. TSK_FREE(Authorization->qop);
  226. TSK_FREE(Authorization->nc);
  227. TSK_OBJECT_SAFE_FREE(THTTP_HEADER_PARAMS(Authorization));
  228. }
  229. else{
  230. TSK_DEBUG_ERROR("Null Authorization header.");
  231. }
  232. return self;
  233. }
  234. static const tsk_object_def_t thttp_header_Authorization_def_s =
  235. {
  236. sizeof(thttp_header_Authorization_t),
  237. thttp_header_Authorization_ctor,
  238. thttp_header_Authorization_dtor,
  239. tsk_null
  240. };
  241. const tsk_object_def_t *thttp_header_Authorization_def_t = &thttp_header_Authorization_def_s;