request_handler.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "wasm_app.h"
  17. static void url1_request_handler(request_t *request)
  18. {
  19. response_t response[1];
  20. attr_container_t *payload;
  21. printf("[resp] ### user resource 1 handler called\n");
  22. if (request->payload != NULL && request->fmt == FMT_ATTR_CONTAINER)
  23. attr_container_dump((attr_container_t *) request->payload);
  24. payload = attr_container_create("wasm app response payload");
  25. if (payload == NULL)
  26. return;
  27. attr_container_set_string(&payload, "key1", "value1");
  28. attr_container_set_string(&payload, "key2", "value2");
  29. make_response_for_request(request, response);
  30. set_response(response, CONTENT_2_05,
  31. FMT_ATTR_CONTAINER,
  32. (void *)payload,
  33. attr_container_get_serialize_length(payload));
  34. api_response_send(response);
  35. attr_container_destroy(payload);
  36. }
  37. static void url2_request_handler(request_t *request)
  38. {
  39. response_t response[1];
  40. make_response_for_request(request, response);
  41. set_response(response, DELETED_2_02, 0, NULL, 0);
  42. api_response_send(response);
  43. printf("### user resource 2 handler called\n");
  44. }
  45. void on_init()
  46. {
  47. /* register resource uri */
  48. api_register_resource_handler("/url1", url1_request_handler);
  49. api_register_resource_handler("/url2", url2_request_handler);
  50. }
  51. void on_destroy()
  52. {
  53. /* real destroy work including killing timer and closing sensor is
  54. accomplished in wasm app library version of on_destroy() */
  55. }