main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * uhttpd - Tiny single-threaded httpd
  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. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <getopt.h>
  23. #include <errno.h>
  24. #include <netdb.h>
  25. #include <signal.h>
  26. #include <libubox/usock.h>
  27. #include "uhttpd.h"
  28. static int run_server(void)
  29. {
  30. uloop_init();
  31. uh_setup_listeners();
  32. uloop_run();
  33. return 0;
  34. }
  35. static void uh_config_parse(void)
  36. {
  37. const char *path = conf.file;
  38. FILE *c;
  39. char line[512];
  40. char *col1;
  41. char *col2;
  42. char *eol;
  43. if (!path)
  44. path = "/etc/httpd.conf";
  45. c = fopen(path, "r");
  46. if (!c)
  47. return;
  48. memset(line, 0, sizeof(line));
  49. while (fgets(line, sizeof(line) - 1, c)) {
  50. if ((line[0] == '/') && (strchr(line, ':') != NULL)) {
  51. if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
  52. !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
  53. !(eol = strchr(col2, '\n')) || (*eol++ = 0))
  54. continue;
  55. uh_auth_add(line, col1, col2);
  56. } else if (!strncmp(line, "I:", 2)) {
  57. if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
  58. !(eol = strchr(col1, '\n')) || (*eol++ = 0))
  59. continue;
  60. uh_index_add(strdup(col1));
  61. } else if (!strncmp(line, "E404:", 5)) {
  62. if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
  63. !(eol = strchr(col1, '\n')) || (*eol++ = 0))
  64. continue;
  65. conf.error_handler = strdup(col1);
  66. }
  67. #ifdef HAVE_CGI
  68. else if ((line[0] == '*') && (strchr(line, ':') != NULL)) {
  69. if (!(col1 = strchr(line, '*')) || (*col1++ = 0) ||
  70. !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
  71. !(eol = strchr(col2, '\n')) || (*eol++ = 0))
  72. continue;
  73. if (!uh_interpreter_add(col1, col2))
  74. fprintf(stderr,
  75. "Unable to add interpreter %s for extension %s: "
  76. "Out of memory\n", col2, col1
  77. );
  78. }
  79. #endif
  80. }
  81. fclose(c);
  82. }
  83. static void add_listener_arg(char *arg, bool tls)
  84. {
  85. char *host = NULL;
  86. char *port = arg;
  87. char *s;
  88. s = strrchr(arg, ':');
  89. if (s) {
  90. host = arg;
  91. port = s + 1;
  92. *s = 0;
  93. }
  94. uh_socket_bind(host, port, tls);
  95. }
  96. static int usage(const char *name)
  97. {
  98. fprintf(stderr, "Usage: %s -p <port>\n", name);
  99. return 1;
  100. }
  101. static void init_defaults(void)
  102. {
  103. conf.network_timeout = 30;
  104. conf.http_keepalive = 0; /* fixme */
  105. conf.max_requests = 3;
  106. uh_index_add("index.html");
  107. uh_index_add("index.htm");
  108. uh_index_add("default.html");
  109. uh_index_add("default.htm");
  110. }
  111. int main(int argc, char **argv)
  112. {
  113. int ch;
  114. init_defaults();
  115. signal(SIGPIPE, SIG_IGN);
  116. while ((ch = getopt(argc, argv, "sp:h:")) != -1) {
  117. bool tls = false;
  118. switch(ch) {
  119. case 's':
  120. tls = true;
  121. case 'p':
  122. add_listener_arg(optarg, tls);
  123. break;
  124. case 'h':
  125. /* docroot */
  126. if (!realpath(optarg, conf.docroot)) {
  127. fprintf(stderr, "Error: Invalid directory %s: %s\n",
  128. optarg, strerror(errno));
  129. exit(1);
  130. }
  131. break;
  132. default:
  133. return usage(argv[0]);
  134. }
  135. }
  136. uh_config_parse();
  137. return run_server();
  138. }