uhttpd.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * uhttpd - Tiny single-threaded httpd - Main header
  3. *
  4. * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
  5. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #ifndef __UHTTPD_H
  20. #define __UHTTPD_H
  21. #include <netinet/in.h>
  22. #include <limits.h>
  23. #include <dirent.h>
  24. #include <libubox/list.h>
  25. #include <libubox/uloop.h>
  26. #include <libubox/ustream.h>
  27. #include <libubox/blob.h>
  28. #include "utils.h"
  29. #define UH_LIMIT_CLIENTS 64
  30. #define UH_LIMIT_HEADERS 64
  31. #define UH_LIMIT_MSGHEAD 4096
  32. struct config {
  33. char docroot[PATH_MAX];
  34. char *realm;
  35. char *file;
  36. char *error_handler;
  37. int no_symlinks;
  38. int no_dirlists;
  39. int network_timeout;
  40. int rfc1918_filter;
  41. int tcp_keepalive;
  42. int max_requests;
  43. int http_keepalive;
  44. };
  45. struct auth_realm {
  46. struct list_head list;
  47. char path[PATH_MAX];
  48. char user[32];
  49. char pass[128];
  50. };
  51. enum http_method {
  52. UH_HTTP_MSG_GET,
  53. UH_HTTP_MSG_POST,
  54. UH_HTTP_MSG_HEAD,
  55. };
  56. enum http_version {
  57. UH_HTTP_VER_0_9,
  58. UH_HTTP_VER_1_0,
  59. UH_HTTP_VER_1_1,
  60. };
  61. struct http_request {
  62. enum http_method method;
  63. enum http_version version;
  64. int redirect_status;
  65. char *url;
  66. struct auth_realm *realm;
  67. };
  68. struct http_response {
  69. int statuscode;
  70. char *statusmsg;
  71. char *headers[UH_LIMIT_HEADERS];
  72. };
  73. enum client_state {
  74. CLIENT_STATE_INIT,
  75. CLIENT_STATE_HEADER,
  76. CLIENT_STATE_DATA,
  77. CLIENT_STATE_DONE,
  78. CLIENT_STATE_CLOSE,
  79. };
  80. struct client {
  81. struct list_head list;
  82. int id;
  83. struct ustream *us;
  84. struct ustream_fd sfd;
  85. #ifdef HAVE_TLS
  86. struct ustream_ssl stream_ssl;
  87. #endif
  88. struct uloop_fd rpipe;
  89. struct uloop_fd wpipe;
  90. struct uloop_process proc;
  91. struct uloop_timeout timeout;
  92. bool (*cb)(struct client *);
  93. void *priv;
  94. enum client_state state;
  95. struct http_request request;
  96. struct http_response response;
  97. struct sockaddr_in6 servaddr;
  98. struct sockaddr_in6 peeraddr;
  99. struct blob_buf hdr;
  100. void (*dispatch_write_cb)(struct client *cl);
  101. void (*dispatch_free)(struct client *cl);
  102. union {
  103. struct {
  104. struct blob_attr **hdr;
  105. int fd;
  106. } file;
  107. } data;
  108. };
  109. extern int n_clients;
  110. extern struct config conf;
  111. void uh_index_add(const char *filename);
  112. void uh_accept_client(int fd);
  113. void uh_unblock_listeners(void);
  114. void uh_setup_listeners(void);
  115. int uh_socket_bind(const char *host, const char *port, bool tls);
  116. bool uh_use_chunked(struct client *cl);
  117. void uh_chunk_write(struct client *cl, const void *data, int len);
  118. void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
  119. void __printf(2, 3)
  120. uh_chunk_printf(struct client *cl, const char *format, ...);
  121. void uh_chunk_eof(struct client *cl);
  122. void uh_request_done(struct client *cl);
  123. void uh_http_header(struct client *cl, int code, const char *summary);
  124. void __printf(4, 5)
  125. uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
  126. void uh_handle_file_request(struct client *cl);
  127. void uh_auth_add(const char *path, const char *user, const char *pass);
  128. #endif