physfshttpd.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * This is a quick and dirty HTTP server that uses PhysicsFS to retrieve
  3. * files. It is not robust at all, probably buggy, and definitely poorly
  4. * designed. It's just meant to show that it can be done.
  5. *
  6. * Basically, you compile this code, and run it:
  7. * ./physfshttpd archive1.zip archive2.zip /path/to/a/real/dir etc...
  8. *
  9. * The files are appended in order to the PhysicsFS search path, and when
  10. * a client request comes it, it looks for the file in said search path.
  11. *
  12. * My goal was to make this work in less than 300 lines of C, so again, it's
  13. * not to be used for any serious purpose. Patches to make this application
  14. * suck less will be readily and gratefully accepted.
  15. *
  16. * Command line I used to build this on Linux:
  17. * gcc -Wall -Werror -g -o bin/physfshttpd extras/physfshttpd.c -lphysfs
  18. *
  19. * License: this code is public domain. I make no warranty that it is useful,
  20. * correct, harmless, or environmentally safe.
  21. *
  22. * This particular file may be used however you like, including copying it
  23. * verbatim into a closed-source project, exploiting it commercially, and
  24. * removing any trace of my name from the source (although I hope you won't
  25. * do that). I welcome enhancements and corrections to this file, but I do
  26. * not require you to send me patches if you make changes. This code has
  27. * NO WARRANTY.
  28. *
  29. * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
  30. * Please see LICENSE.txt in the root of the source tree.
  31. *
  32. * This file was written by Ryan C. Gordon. (icculus@icculus.org).
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include <errno.h>
  39. #include <ctype.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #ifndef LACKING_SIGNALS
  45. #include <signal.h>
  46. #endif
  47. #ifndef LACKING_PROTOENT
  48. #include <netdb.h>
  49. #endif
  50. #include "physfs.h"
  51. #define DEFAULT_PORTNUM 6667
  52. typedef struct
  53. {
  54. int sock;
  55. struct sockaddr *addr;
  56. socklen_t addrlen;
  57. } http_args;
  58. static char *txt404 =
  59. "HTTP/1.0 404 Not Found\n"
  60. "Connection: close\n"
  61. "Content-Type: text/html; charset=utf-8\n"
  62. "\n"
  63. "<html><head><title>404 Not Found</title></head>\n"
  64. "<body>Can't find that.</body></html>\n\n";
  65. static void feed_file_http(const char *ipstr, int sock, const char *fname)
  66. {
  67. PHYSFS_File *in = PHYSFS_openRead(fname);
  68. char buffer[1024];
  69. printf("%s: requested [%s].\n", ipstr, fname);
  70. if (in == NULL)
  71. {
  72. printf("%s: Can't open [%s]: %s.\n",
  73. ipstr, fname, PHYSFS_getLastError());
  74. write(sock, txt404, strlen(txt404)); /* !!! FIXME: Check retval */
  75. } /* if */
  76. else
  77. {
  78. do
  79. {
  80. PHYSFS_sint64 br = PHYSFS_read(in, buffer, 1, sizeof (buffer));
  81. if (br == -1)
  82. {
  83. printf("%s: Read error: %s.\n", ipstr, PHYSFS_getLastError());
  84. break;
  85. } /* if */
  86. write(sock, buffer, (int) br); /* !!! FIXME: CHECK THIS RETVAL! */
  87. } while (!PHYSFS_eof(in));
  88. PHYSFS_close(in);
  89. } /* else */
  90. } /* feed_file_http */
  91. static void *do_http(void *_args)
  92. {
  93. http_args *args = (http_args *) _args;
  94. char ipstr[128];
  95. char buffer[512];
  96. char *ptr;
  97. strncpy(ipstr, inet_ntoa(((struct sockaddr_in *) args->addr)->sin_addr),
  98. sizeof (ipstr));
  99. ipstr[sizeof (ipstr) - 1] = '\0';
  100. printf("%s: connected.\n", ipstr);
  101. read(args->sock, buffer, sizeof (buffer));
  102. buffer[sizeof (buffer) - 1] = '\0';
  103. ptr = strchr(buffer, '\n');
  104. if (!ptr)
  105. printf("%s: potentially bogus request.\n", ipstr);
  106. else
  107. {
  108. *ptr = '\0';
  109. ptr = strchr(buffer, '\r');
  110. if (ptr != NULL)
  111. *ptr = '\0';
  112. if ((toupper(buffer[0]) == 'G') &&
  113. (toupper(buffer[1]) == 'E') &&
  114. (toupper(buffer[2]) == 'T') &&
  115. (toupper(buffer[3]) == ' ') &&
  116. (toupper(buffer[4]) == '/'))
  117. {
  118. ptr = strchr(buffer + 5, ' ');
  119. if (ptr != NULL)
  120. *ptr = '\0';
  121. feed_file_http(ipstr, args->sock, buffer + 4);
  122. } /* if */
  123. } /* else */
  124. /* !!! FIXME: Time the transfer. */
  125. printf("%s: closing connection.\n", ipstr);
  126. close(args->sock);
  127. free(args->addr);
  128. free(args);
  129. return(NULL);
  130. } /* do_http */
  131. static void serve_http_request(int sock, struct sockaddr *addr,
  132. socklen_t addrlen)
  133. {
  134. http_args *args = (http_args *) malloc(sizeof (http_args));
  135. if (args == NULL)
  136. {
  137. printf("out of memory.\n");
  138. return;
  139. } /* if */
  140. args->addr = (struct sockaddr *) malloc(addrlen);
  141. if (args->addr == NULL)
  142. {
  143. free(args);
  144. printf("out of memory.\n");
  145. return;
  146. } /* if */
  147. args->sock = sock;
  148. args->addrlen = addrlen;
  149. memcpy(args->addr, addr, addrlen);
  150. /* !!! FIXME: optionally spin a thread... */
  151. do_http((void *) args);
  152. } /* server_http_request */
  153. static int create_listen_socket(short portnum)
  154. {
  155. int retval = -1;
  156. int protocol = 0; /* pray this is right. */
  157. #ifndef LACKING_PROTOENT
  158. struct protoent *prot;
  159. setprotoent(0);
  160. prot = getprotobyname("tcp");
  161. if (prot != NULL)
  162. protocol = prot->p_proto;
  163. #endif
  164. retval = socket(PF_INET, SOCK_STREAM, protocol);
  165. if (retval >= 0)
  166. {
  167. struct sockaddr_in addr;
  168. addr.sin_family = AF_INET;
  169. addr.sin_port = htons(portnum);
  170. addr.sin_addr.s_addr = INADDR_ANY;
  171. if ((bind(retval, &addr, (socklen_t) sizeof (addr)) == -1) ||
  172. (listen(retval, 5) == -1))
  173. {
  174. close(retval);
  175. retval = -1;
  176. } /* if */
  177. } /* if */
  178. return(retval);
  179. } /* create_listen_socket */
  180. static int listensocket = -1;
  181. void at_exit_cleanup(void)
  182. {
  183. /*
  184. * !!! FIXME: If thread support, signal threads to terminate and
  185. * !!! FIXME: wait for them to clean up.
  186. */
  187. if (listensocket >= 0)
  188. close(listensocket);
  189. if (!PHYSFS_deinit())
  190. printf("PHYSFS_deinit() failed: %s\n", PHYSFS_getLastError());
  191. } /* at_exit_cleanup */
  192. int main(int argc, char **argv)
  193. {
  194. int i;
  195. int portnum = DEFAULT_PORTNUM;
  196. setbuf(stdout, NULL);
  197. setbuf(stderr, NULL);
  198. #ifndef LACKING_SIGNALS
  199. /* I'm not sure if this qualifies as a cheap trick... */
  200. signal(SIGTERM, exit);
  201. signal(SIGINT, exit);
  202. signal(SIGFPE, exit);
  203. signal(SIGSEGV, exit);
  204. signal(SIGPIPE, exit);
  205. signal(SIGILL, exit);
  206. #endif
  207. if (argc == 1)
  208. {
  209. printf("USAGE: %s <archive1> [archive2 [... archiveN]]\n", argv[0]);
  210. return(42);
  211. } /* if */
  212. if (!PHYSFS_init(argv[0]))
  213. {
  214. printf("PHYSFS_init() failed: %s\n", PHYSFS_getLastError());
  215. return(42);
  216. } /* if */
  217. /* normally, this is bad practice, but oh well. */
  218. atexit(at_exit_cleanup);
  219. for (i = 1; i < argc; i++)
  220. {
  221. if (!PHYSFS_addToSearchPath(argv[i], 1))
  222. printf(" WARNING: failed to add [%s] to search path.\n", argv[i]);
  223. } /* else */
  224. listensocket = create_listen_socket(portnum);
  225. if (listensocket < 0)
  226. {
  227. printf("listen socket failed to create.\n");
  228. return(42);
  229. } /* if */
  230. while (1) /* infinite loop for now. */
  231. {
  232. struct sockaddr addr;
  233. socklen_t len;
  234. int s = accept(listensocket, &addr, &len);
  235. if (s < 0)
  236. {
  237. printf("accept() failed: %s\n", strerror(errno));
  238. close(listensocket);
  239. return(42);
  240. } /* if */
  241. serve_http_request(s, &addr, len);
  242. } /* while */
  243. return(0);
  244. } /* main */
  245. /* end of physfshttpd.c ... */