uhttpd.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 <libubox/utils.h>
  29. #include "utils.h"
  30. #define UH_LIMIT_CLIENTS 64
  31. #define __enum_header(_name) HDR_##_name,
  32. #define __blobmsg_header(_name) [HDR_##_name] = { .name = #_name, .type = BLOBMSG_TYPE_STRING },
  33. struct client;
  34. struct config {
  35. const char *docroot;
  36. const char *realm;
  37. const char *file;
  38. const char *error_handler;
  39. const char *cgi_prefix;
  40. const char *cgi_path;
  41. int no_symlinks;
  42. int no_dirlists;
  43. int network_timeout;
  44. int rfc1918_filter;
  45. int tcp_keepalive;
  46. int max_requests;
  47. int http_keepalive;
  48. int script_timeout;
  49. };
  50. struct auth_realm {
  51. struct list_head list;
  52. const char *path;
  53. const char *user;
  54. const char *pass;
  55. };
  56. enum http_method {
  57. UH_HTTP_MSG_GET,
  58. UH_HTTP_MSG_POST,
  59. UH_HTTP_MSG_HEAD,
  60. };
  61. enum http_version {
  62. UH_HTTP_VER_0_9,
  63. UH_HTTP_VER_1_0,
  64. UH_HTTP_VER_1_1,
  65. };
  66. struct http_request {
  67. enum http_method method;
  68. enum http_version version;
  69. int redirect_status;
  70. const char *url;
  71. const struct auth_realm *realm;
  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 interpreter {
  81. struct list_head list;
  82. const char *path;
  83. const char *ext;
  84. };
  85. struct path_info {
  86. const char *root;
  87. const char *phys;
  88. const char *name;
  89. const char *info;
  90. const char *query;
  91. const char *auth;
  92. bool redirected;
  93. struct stat stat;
  94. const struct interpreter *ip;
  95. };
  96. struct env_var {
  97. const char *name;
  98. const char *value;
  99. };
  100. struct relay {
  101. struct ustream_fd sfd;
  102. struct uloop_process proc;
  103. struct client *cl;
  104. bool process_done;
  105. int ret;
  106. int header_ofs;
  107. void (*header_cb)(struct relay *r, const char *name, const char *value);
  108. void (*header_end)(struct relay *r);
  109. void (*close)(struct relay *r, int ret);
  110. };
  111. struct dispatch_handler {
  112. struct list_head list;
  113. bool (*check_url)(const char *url);
  114. bool (*check_path)(struct path_info *pi, const char *url);
  115. void (*handle_request)(struct client *cl, const char *url, struct path_info *pi);
  116. };
  117. struct uh_addr {
  118. uint8_t family;
  119. uint16_t port;
  120. union {
  121. struct in_addr in;
  122. struct in6_addr in6;
  123. };
  124. };
  125. struct client {
  126. struct list_head list;
  127. int id;
  128. struct ustream *us;
  129. struct ustream_fd sfd;
  130. #ifdef HAVE_TLS
  131. struct ustream_ssl stream_ssl;
  132. #endif
  133. struct uloop_timeout timeout;
  134. enum client_state state;
  135. struct http_request request;
  136. struct uh_addr srv_addr, peer_addr;
  137. struct blob_buf hdr;
  138. struct {
  139. void (*write_cb)(struct client *cl);
  140. void (*close_fds)(struct client *cl);
  141. void (*free)(struct client *cl);
  142. union {
  143. struct {
  144. struct blob_attr **hdr;
  145. int fd;
  146. } file;
  147. struct {
  148. struct blob_buf hdr;
  149. struct relay r;
  150. int status_code;
  151. char *status_msg;
  152. } proc;
  153. };
  154. } dispatch;
  155. };
  156. extern char uh_buf[4096];
  157. extern int n_clients;
  158. extern struct config conf;
  159. extern const char * const http_versions[];
  160. extern const char * const http_methods[];
  161. extern struct dispatch_handler cgi_dispatch;
  162. void uh_index_add(const char *filename);
  163. void uh_accept_client(int fd);
  164. void uh_unblock_listeners(void);
  165. void uh_setup_listeners(void);
  166. int uh_socket_bind(const char *host, const char *port, bool tls);
  167. bool uh_use_chunked(struct client *cl);
  168. void uh_chunk_write(struct client *cl, const void *data, int len);
  169. void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
  170. void __printf(2, 3)
  171. uh_chunk_printf(struct client *cl, const char *format, ...);
  172. void uh_chunk_eof(struct client *cl);
  173. void uh_request_done(struct client *cl);
  174. void uh_http_header(struct client *cl, int code, const char *summary);
  175. void __printf(4, 5)
  176. uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
  177. void uh_handle_request(struct client *cl);
  178. void uh_auth_add(const char *path, const char *user, const char *pass);
  179. bool uh_auth_check(struct client *cl, struct path_info *pi);
  180. void uh_close_listen_fds(void);
  181. void uh_close_fds(void);
  182. void uh_interpreter_add(const char *ext, const char *path);
  183. void uh_dispatch_add(struct dispatch_handler *d);
  184. void uh_relay_open(struct client *cl, struct relay *r, int fd, int pid);
  185. void uh_relay_close(struct relay *r, int ret);
  186. void uh_relay_free(struct relay *r);
  187. struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi);
  188. bool uh_create_process(struct client *cl, struct path_info *pi,
  189. void (*cb)(struct client *cl, struct path_info *pi, int fd));
  190. #endif