input.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2018 Markus Hennecke <markus-hennecke@markus-hennecke.de>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <sys/types.h>
  17. #include <err.h>
  18. #include <stdarg.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <kcgi.h>
  22. #include "../db.h"
  23. #include "../util.h"
  24. enum key {
  25. KEY_TEMP,
  26. KEY_HUMIDITY,
  27. KEY_PRESSURE,
  28. KEY_BATTERY,
  29. KEY_DEVICE,
  30. KEY_TIMESTAMP,
  31. KEY_ERRORMSG,
  32. KEY__MAX
  33. };
  34. static const struct kvalid keys[KEY__MAX] = {
  35. { kvalid_double, "temp" },
  36. { kvalid_double, "humidity" },
  37. { kvalid_double, "pressure" },
  38. { kvalid_double, "battery" },
  39. { validate_device, "device" },
  40. { validate_timestamp, "timestamp" },
  41. { kvalid_string, "errormsg" },
  42. };
  43. enum pg {
  44. PG_DATA,
  45. PG_ERROR,
  46. PG__MAX
  47. };
  48. static const char *pages[PG__MAX] = {
  49. "data",
  50. "error",
  51. };
  52. int
  53. main(void)
  54. {
  55. struct kreq r;
  56. enum kcgi_err er;
  57. if ((pledge("cpath getpw stdio proc wpath rpath dns inet",
  58. NULL)) == (-1))
  59. err(EXIT_FAILURE, "pledge");
  60. er = khttp_parse(&r, keys, KEY__MAX, pages, PG__MAX, PG_DATA);
  61. if (KCGI_OK != er)
  62. return EXIT_FAILURE;
  63. /*
  64. * Accept POST and GET...
  65. */
  66. if (KMETHOD_OPTIONS == r.method) {
  67. khttp_head(&r, kresps[KRESP_ALLOW], "OPTIONS POST GET");
  68. response(&r, KHTTP_200, r.mime, NULL);
  69. } else if ((KMETHOD_POST == r.method) || (KMETHOD_GET == r.method)) {
  70. switch (r.page) {
  71. case PG_DATA:
  72. {
  73. const char *temp = parameter(&r, KEY_TEMP);
  74. const char *humidity
  75. = parameter(&r, KEY_HUMIDITY);
  76. const char *pressure
  77. = parameter(&r, KEY_PRESSURE);
  78. const char *battery
  79. = parameter(&r, KEY_BATTERY);
  80. if (db_add_data(&r, temp, humidity, pressure,
  81. battery))
  82. response(&r, KHTTP_200,
  83. KMIME_TEXT_PLAIN, "1 success");
  84. else
  85. response(&r, KHTTP_503,
  86. KMIME_TEXT_PLAIN,
  87. "0 internal error");
  88. break;
  89. }
  90. case PG_ERROR:
  91. {
  92. const char *device = parameter(&r, KEY_DEVICE);
  93. const char *timestamp
  94. = parameter(&r, KEY_TIMESTAMP);
  95. const char *errormsg
  96. = parameter(&r, KEY_ERRORMSG);
  97. if (db_add_error(&r, device, timestamp,
  98. errormsg))
  99. response(&r, KHTTP_200,
  100. KMIME_TEXT_PLAIN, "1 success");
  101. else
  102. response(&r, KHTTP_503,
  103. KMIME_TEXT_PLAIN,
  104. "0 internal error");
  105. }
  106. break;
  107. default:
  108. break;
  109. }
  110. } else
  111. response(&r, KHTTP_404, r.mime, "0 page not found");
  112. khttp_free(&r);
  113. return EXIT_SUCCESS;
  114. }