physfshttpd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 in, 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 <stdarg.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include <errno.h>
  40. #include <ctype.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <netinet/in.h>
  44. #include <arpa/inet.h>
  45. #ifndef LACKING_SIGNALS
  46. #include <signal.h>
  47. #endif
  48. #ifndef LACKING_PROTOENT
  49. #include <netdb.h>
  50. #endif
  51. #include "physfs.h"
  52. #define DEFAULT_PORTNUM 8080
  53. typedef struct
  54. {
  55. int sock;
  56. struct sockaddr *addr;
  57. socklen_t addrlen;
  58. } http_args;
  59. #define txt404 \
  60. "HTTP/1.0 404 Not Found\n" \
  61. "Connection: close\n" \
  62. "Content-Type: text/html; charset=utf-8\n" \
  63. "\n" \
  64. "<html><head><title>404 Not Found</title></head>\n" \
  65. "<body>Can't find '%s'.</body></html>\n\n" \
  66. #define txt200 \
  67. "HTTP/1.0 200 OK\n" \
  68. "Connection: close\n" \
  69. "Content-Type: %s\n" \
  70. "\n"
  71. static const char *lastError(void)
  72. {
  73. return PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode());
  74. } /* lastError */
  75. static int writeAll(const char *ipstr, const int sock, void *buf, const size_t len)
  76. {
  77. if (write(sock, buf, len) != len)
  78. {
  79. printf("%s: Write error to socket.\n", ipstr);
  80. return 0;
  81. } /* if */
  82. return 1;
  83. } /* writeAll */
  84. static int writeString(const char *ipstr, const int sock, const char *fmt, ...)
  85. {
  86. /* none of this is robust against large strings or HTML escaping. */
  87. char buffer[1024];
  88. int len;
  89. va_list ap;
  90. va_start(ap, fmt);
  91. len = vsnprintf(buffer, sizeof (buffer), fmt, ap);
  92. va_end(ap);
  93. if (len < 0)
  94. {
  95. printf("uhoh, vsnprintf() failed!\n");
  96. return 0;
  97. } /* if */
  98. return writeAll(ipstr, sock, buffer, len);
  99. } /* writeString */
  100. static void feed_file_http(const char *ipstr, int sock, const char *fname)
  101. {
  102. PHYSFS_File *in = PHYSFS_openRead(fname);
  103. if (in == NULL)
  104. {
  105. printf("%s: Can't open [%s]: %s.\n", ipstr, fname, lastError());
  106. writeString(ipstr, sock, txt404, fname);
  107. return;
  108. } /* if */
  109. /* !!! FIXME: mimetype */
  110. if (writeString(ipstr, sock, txt200, "text/plain; charset=utf-8"))
  111. {
  112. do
  113. {
  114. char buffer[1024];
  115. PHYSFS_sint64 br = PHYSFS_readBytes(in, buffer, sizeof (buffer));
  116. if (br == -1)
  117. {
  118. printf("%s: Read error: %s.\n", ipstr, lastError());
  119. break;
  120. } /* if */
  121. else if (!writeAll(ipstr, sock, buffer, (size_t) br))
  122. {
  123. break;
  124. } /* else if */
  125. } while (!PHYSFS_eof(in));
  126. } /* if */
  127. PHYSFS_close(in);
  128. } /* feed_file_http */
  129. static void feed_dirlist_http(const char *ipstr, int sock,
  130. const char *dname, char **list)
  131. {
  132. int i;
  133. if (!writeString(ipstr, sock, txt200, "text/html; charset=utf-8"))
  134. return;
  135. else if (!writeString(ipstr, sock,
  136. "<html><head><title>Directory %s</title></head>"
  137. "<body><p><h1>Directory %s</h1></p><p><ul>\n",
  138. dname, dname))
  139. return;
  140. if (strcmp(dname, "/") == 0)
  141. dname = "";
  142. for (i = 0; list[i]; i++)
  143. {
  144. const char *fname = list[i];
  145. if (!writeString(ipstr, sock,
  146. "<li><a href='%s/%s'>%s</a></li>\n", dname, fname, fname))
  147. break;
  148. } /* for */
  149. writeString(ipstr, sock, "</ul></body></html>\n");
  150. } /* feed_dirlist_http */
  151. static void feed_dir_http(const char *ipstr, int sock, const char *dname)
  152. {
  153. char **list = PHYSFS_enumerateFiles(dname);
  154. if (list == NULL)
  155. {
  156. printf("%s: Can't enumerate directory [%s]: %s.\n",
  157. ipstr, dname, lastError());
  158. writeString(ipstr, sock, txt404, dname);
  159. return;
  160. } /* if */
  161. feed_dirlist_http(ipstr, sock, dname, list);
  162. PHYSFS_freeList(list);
  163. } /* feed_dir_http */
  164. static void feed_http_request(const char *ipstr, int sock, const char *fname)
  165. {
  166. PHYSFS_Stat statbuf;
  167. printf("%s: requested [%s].\n", ipstr, fname);
  168. if (!PHYSFS_stat(fname, &statbuf))
  169. {
  170. printf("%s: Can't stat [%s]: %s.\n", ipstr, fname, lastError());
  171. writeString(ipstr, sock, txt404, fname);
  172. return;
  173. } /* if */
  174. if (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY)
  175. feed_dir_http(ipstr, sock, fname);
  176. else
  177. feed_file_http(ipstr, sock, fname);
  178. } /* feed_http_request */
  179. static void *do_http(void *_args)
  180. {
  181. http_args *args = (http_args *) _args;
  182. char ipstr[128];
  183. char buffer[512];
  184. char *ptr;
  185. strncpy(ipstr, inet_ntoa(((struct sockaddr_in *) args->addr)->sin_addr),
  186. sizeof (ipstr));
  187. ipstr[sizeof (ipstr) - 1] = '\0';
  188. printf("%s: connected.\n", ipstr);
  189. read(args->sock, buffer, sizeof (buffer));
  190. buffer[sizeof (buffer) - 1] = '\0';
  191. ptr = strchr(buffer, '\n');
  192. if (!ptr)
  193. printf("%s: potentially bogus request.\n", ipstr);
  194. else
  195. {
  196. *ptr = '\0';
  197. ptr = strchr(buffer, '\r');
  198. if (ptr != NULL)
  199. *ptr = '\0';
  200. if ((toupper(buffer[0]) == 'G') &&
  201. (toupper(buffer[1]) == 'E') &&
  202. (toupper(buffer[2]) == 'T') &&
  203. (toupper(buffer[3]) == ' ') &&
  204. (toupper(buffer[4]) == '/'))
  205. {
  206. ptr = strchr(buffer + 5, ' ');
  207. if (ptr != NULL)
  208. *ptr = '\0';
  209. feed_http_request(ipstr, args->sock, buffer + 4);
  210. } /* if */
  211. } /* else */
  212. /* !!! FIXME: Time the transfer. */
  213. printf("%s: closing connection.\n", ipstr);
  214. close(args->sock);
  215. free(args->addr);
  216. free(args);
  217. return NULL;
  218. } /* do_http */
  219. static void serve_http_request(int sock, struct sockaddr *addr,
  220. socklen_t addrlen)
  221. {
  222. http_args *args = (http_args *) malloc(sizeof (http_args));
  223. if (args == NULL)
  224. {
  225. printf("out of memory.\n");
  226. return;
  227. } /* if */
  228. args->addr = (struct sockaddr *) malloc(addrlen);
  229. if (args->addr == NULL)
  230. {
  231. free(args);
  232. printf("out of memory.\n");
  233. return;
  234. } /* if */
  235. args->sock = sock;
  236. args->addrlen = addrlen;
  237. memcpy(args->addr, addr, addrlen);
  238. /* !!! FIXME: optionally spin a thread... */
  239. do_http((void *) args);
  240. } /* server_http_request */
  241. static int create_listen_socket(short portnum)
  242. {
  243. int retval = -1;
  244. int protocol = 0; /* pray this is right. */
  245. #ifndef LACKING_PROTOENT
  246. struct protoent *prot;
  247. setprotoent(0);
  248. prot = getprotobyname("tcp");
  249. if (prot != NULL)
  250. protocol = prot->p_proto;
  251. #endif
  252. retval = socket(PF_INET, SOCK_STREAM, protocol);
  253. if (retval >= 0)
  254. {
  255. struct sockaddr_in addr;
  256. addr.sin_family = AF_INET;
  257. addr.sin_port = htons(portnum);
  258. addr.sin_addr.s_addr = INADDR_ANY;
  259. if ((bind(retval, (struct sockaddr *) &addr, (socklen_t) sizeof (addr)) == -1) ||
  260. (listen(retval, 5) == -1))
  261. {
  262. close(retval);
  263. retval = -1;
  264. } /* if */
  265. } /* if */
  266. return retval;
  267. } /* create_listen_socket */
  268. static int listensocket = -1;
  269. void at_exit_cleanup(void)
  270. {
  271. /*
  272. * !!! FIXME: If thread support, signal threads to terminate and
  273. * !!! FIXME: wait for them to clean up.
  274. */
  275. if (listensocket >= 0)
  276. close(listensocket);
  277. if (!PHYSFS_deinit())
  278. printf("PHYSFS_deinit() failed: %s\n", lastError());
  279. } /* at_exit_cleanup */
  280. int main(int argc, char **argv)
  281. {
  282. int i;
  283. int portnum = DEFAULT_PORTNUM;
  284. setbuf(stdout, NULL);
  285. setbuf(stderr, NULL);
  286. #ifndef LACKING_SIGNALS
  287. /* I'm not sure if this qualifies as a cheap trick... */
  288. signal(SIGTERM, exit);
  289. signal(SIGINT, exit);
  290. signal(SIGFPE, exit);
  291. signal(SIGSEGV, exit);
  292. signal(SIGPIPE, exit);
  293. signal(SIGILL, exit);
  294. #endif
  295. if (argc == 1)
  296. {
  297. printf("USAGE: %s <archive1> [archive2 [... archiveN]]\n", argv[0]);
  298. return 42;
  299. } /* if */
  300. if (!PHYSFS_init(argv[0]))
  301. {
  302. printf("PHYSFS_init() failed: %s\n", lastError());
  303. return 42;
  304. } /* if */
  305. /* normally, this is bad practice, but oh well. */
  306. atexit(at_exit_cleanup);
  307. for (i = 1; i < argc; i++)
  308. {
  309. if (!PHYSFS_mount(argv[i], NULL, 1))
  310. printf(" WARNING: failed to add [%s] to search path.\n", argv[i]);
  311. } /* else */
  312. listensocket = create_listen_socket(portnum);
  313. if (listensocket < 0)
  314. {
  315. printf("listen socket failed to create.\n");
  316. return 42;
  317. } /* if */
  318. while (1) /* infinite loop for now. */
  319. {
  320. struct sockaddr addr;
  321. socklen_t len;
  322. int s = accept(listensocket, &addr, &len);
  323. if (s < 0)
  324. {
  325. printf("accept() failed: %s\n", strerror(errno));
  326. close(listensocket);
  327. return 42;
  328. } /* if */
  329. serve_http_request(s, &addr, len);
  330. } /* while */
  331. return 0;
  332. } /* main */
  333. /* end of physfshttpd.c ... */