http.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright (c) 2016 Red Hat, Inc.
  4. * Author: Nathaniel McCallum <npmccallum@redhat.com>
  5. *
  6. * This program 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. * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #pragma once
  20. #include <sys/types.h>
  21. #include <regex.h>
  22. #ifdef USE_LLHTTP
  23. #include <llhttp.h>
  24. typedef llhttp_method_t http_method_t;
  25. typedef llhttp_status_t http_status_t;
  26. typedef llhttp_settings_t http_settings_t;
  27. typedef llhttp_t http_parser_t;
  28. #define tang_http_parser_init(parser, settings) llhttp_init(parser, HTTP_REQUEST, settings)
  29. #define tang_http_parser_errno(parser) parser.error
  30. #define tang_http_errno_description(parser, errno) llhttp_get_error_reason(parser)
  31. #define tang_http_parser_resume(parser) llhttp_resume(parser)
  32. #else
  33. /* Legacy http-parser. */
  34. #include <http_parser.h>
  35. typedef enum http_method http_method_t;
  36. typedef enum http_status http_status_t;
  37. typedef http_parser_settings http_settings_t;
  38. typedef struct http_parser http_parser_t;
  39. #define tang_http_parser_init(parser, settings) http_parser_init(parser, HTTP_REQUEST)
  40. #define tang_http_parser_errno(parser) parser.http_errno
  41. #define tang_http_errno_description(parser, errno) http_errno_description(errno)
  42. #define tang_http_parser_resume(parser) http_parser_pause(parser, 0)
  43. #endif /* USE_LLHTTP */
  44. struct http_dispatch {
  45. int (*func)(http_method_t method, const char *path,
  46. const char *body, regmatch_t matches[], void *misc);
  47. uint64_t methods;
  48. size_t nmatches;
  49. const char *re;
  50. };
  51. struct http_request {
  52. int status;
  53. char path[1024 * 4];
  54. char body[1024 * 64];
  55. };
  56. struct http_state {
  57. const struct http_dispatch *dispatch;
  58. struct http_request req;
  59. void *misc;
  60. };
  61. extern const http_settings_t http_settings;
  62. int __attribute__ ((format(printf, 4, 5)))
  63. http_reply(const char *file, int line,
  64. http_status_t code, const char *fmt, ...);
  65. #define http_reply(code, ...) \
  66. http_reply(__FILE__, __LINE__, code, __VA_ARGS__)