param_parsing.mustache 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. {{!
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. }}
  18. {{!
  19. * Snippet for decoding parameters into an _args struct.
  20. }}
  21. {{#has_query_parameters}}
  22. for (i = get_params; i; i = i->next) {
  23. {{#query_parameters}}
  24. if (strcmp(i->name, "{{name}}") == 0) {
  25. {{^allow_multiple}}
  26. args.{{c_name}} = {{c_convert}}(i->value);
  27. {{/allow_multiple}}
  28. {{#allow_multiple}}
  29. /* Parse comma separated list */
  30. char *vals[MAX_VALS];
  31. size_t j;
  32. args.{{c_name}}_parse = ast_strdup(i->value);
  33. if (!args.{{c_name}}_parse) {
  34. ast_ari_response_alloc_failed(response);
  35. goto fin;
  36. }
  37. if (strlen(args.{{c_name}}_parse) == 0) {
  38. /* ast_app_separate_args can't handle "" */
  39. args.{{c_name}}_count = 1;
  40. vals[0] = args.{{c_name}}_parse;
  41. } else {
  42. args.{{c_name}}_count = ast_app_separate_args(
  43. args.{{c_name}}_parse, ',', vals,
  44. ARRAY_LEN(vals));
  45. }
  46. if (args.{{c_name}}_count == 0) {
  47. ast_ari_response_alloc_failed(response);
  48. goto fin;
  49. }
  50. if (args.{{c_name}}_count >= MAX_VALS) {
  51. ast_ari_response_error(response, 400,
  52. "Bad Request",
  53. "Too many values for {{c_name}}");
  54. goto fin;
  55. }
  56. args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count);
  57. if (!args.{{c_name}}) {
  58. ast_ari_response_alloc_failed(response);
  59. goto fin;
  60. }
  61. for (j = 0; j < args.{{c_name}}_count; ++j) {
  62. args.{{c_name}}[j] = {{c_convert}}(vals[j]);
  63. }
  64. {{/allow_multiple}}
  65. } else
  66. {{/query_parameters}}
  67. {}
  68. }
  69. {{/has_query_parameters}}
  70. {{#has_path_parameters}}
  71. for (i = path_vars; i; i = i->next) {
  72. {{#path_parameters}}
  73. if (strcmp(i->name, "{{name}}") == 0) {
  74. args.{{c_name}} = {{c_convert}}(i->value);
  75. } else
  76. {{/path_parameters}}
  77. {}
  78. }
  79. {{/has_path_parameters}}
  80. {{^is_websocket}}
  81. {{#parse_body}}
  82. /* Look for a JSON request entity */
  83. body = ast_http_get_json(ser, headers);
  84. if (!body) {
  85. switch (errno) {
  86. case EFBIG:
  87. ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
  88. goto fin;
  89. case ENOMEM:
  90. ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
  91. goto fin;
  92. case EIO:
  93. ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
  94. goto fin;
  95. }
  96. }
  97. {{#body_parameter}}
  98. args.{{c_name}} = body;
  99. {{/body_parameter}}
  100. {{^body_parameter}}
  101. if (ast_ari_{{c_name}}_{{c_nickname}}_parse_body(body, &args)) {
  102. ast_ari_response_alloc_failed(response);
  103. goto fin;
  104. }
  105. {{/body_parameter}}
  106. {{/parse_body}}
  107. {{/is_websocket}}