physfshttpd.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 <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 8080
  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 char *txt200 =
  66. "HTTP/1.0 200 OK\n"
  67. "Connection: close\n"
  68. "Content-Type: text/plain; charset=utf-8\n"
  69. "\n";
  70. static int writeAll(const int fd, const void *buf, const size_t len)
  71. {
  72. return (write(fd, buf, len) == len);
  73. } /* writeAll */
  74. static void feed_file_http(const char *ipstr, int sock, const char *fname)
  75. {
  76. PHYSFS_File *in = PHYSFS_openRead(fname);
  77. printf("%s: requested [%s].\n", ipstr, fname);
  78. if (in == NULL)
  79. {
  80. printf("%s: Can't open [%s]: %s.\n",
  81. ipstr, fname, PHYSFS_getLastError());
  82. if (!writeAll(sock, txt404, strlen(txt404)))
  83. printf("%s: Write error to socket.\n", ipstr);
  84. return;
  85. } /* if */
  86. if (writeAll(sock, txt200, strlen(txt200)))
  87. {
  88. do
  89. {
  90. char buffer[1024];
  91. PHYSFS_sint64 br = PHYSFS_readBytes(in, buffer, sizeof (buffer));
  92. if (br == -1)
  93. {
  94. printf("%s: Read error: %s.\n", ipstr, PHYSFS_getLastError());
  95. break;
  96. } /* if */
  97. else if (!writeAll(sock, buffer, (size_t) br))
  98. {
  99. printf("%s: Write error to socket.\n", ipstr);
  100. break;
  101. } /* else if */
  102. } while (!PHYSFS_eof(in));
  103. } /* if */
  104. PHYSFS_close(in);
  105. } /* feed_file_http */
  106. static void *do_http(void *_args)
  107. {
  108. http_args *args = (http_args *) _args;
  109. char ipstr[128];
  110. char buffer[512];
  111. char *ptr;
  112. strncpy(ipstr, inet_ntoa(((struct sockaddr_in *) args->addr)->sin_addr),
  113. sizeof (ipstr));
  114. ipstr[sizeof (ipstr) - 1] = '\0';
  115. printf("%s: connected.\n", ipstr);
  116. read(args->sock, buffer, sizeof (buffer));
  117. buffer[sizeof (buffer) - 1] = '\0';
  118. ptr = strchr(buffer, '\n');
  119. if (!ptr)
  120. printf("%s: potentially bogus request.\n", ipstr);
  121. else
  122. {
  123. *ptr = '\0';
  124. ptr = strchr(buffer, '\r');
  125. if (ptr != NULL)
  126. *ptr = '\0';
  127. if ((toupper(buffer[0]) == 'G') &&
  128. (toupper(buffer[1]) == 'E') &&
  129. (toupper(buffer[2]) == 'T') &&
  130. (toupper(buffer[3]) == ' ') &&
  131. (toupper(buffer[4]) == '/'))
  132. {
  133. ptr = strchr(buffer + 5, ' ');
  134. if (ptr != NULL)
  135. *ptr = '\0';
  136. feed_file_http(ipstr, args->sock, buffer + 4);
  137. } /* if */
  138. } /* else */
  139. /* !!! FIXME: Time the transfer. */
  140. printf("%s: closing connection.\n", ipstr);
  141. close(args->sock);
  142. free(args->addr);
  143. free(args);
  144. return NULL;
  145. } /* do_http */
  146. static void serve_http_request(int sock, struct sockaddr *addr,
  147. socklen_t addrlen)
  148. {
  149. http_args *args = (http_args *) malloc(sizeof (http_args));
  150. if (args == NULL)
  151. {
  152. printf("out of memory.\n");
  153. return;
  154. } /* if */
  155. args->addr = (struct sockaddr *) malloc(addrlen);
  156. if (args->addr == NULL)
  157. {
  158. free(args);
  159. printf("out of memory.\n");
  160. return;
  161. } /* if */
  162. args->sock = sock;
  163. args->addrlen = addrlen;
  164. memcpy(args->addr, addr, addrlen);
  165. /* !!! FIXME: optionally spin a thread... */
  166. do_http((void *) args);
  167. } /* server_http_request */
  168. static int create_listen_socket(short portnum)
  169. {
  170. int retval = -1;
  171. int protocol = 0; /* pray this is right. */
  172. #ifndef LACKING_PROTOENT
  173. struct protoent *prot;
  174. setprotoent(0);
  175. prot = getprotobyname("tcp");
  176. if (prot != NULL)
  177. protocol = prot->p_proto;
  178. #endif
  179. retval = socket(PF_INET, SOCK_STREAM, protocol);
  180. if (retval >= 0)
  181. {
  182. struct sockaddr_in addr;
  183. addr.sin_family = AF_INET;
  184. addr.sin_port = htons(portnum);
  185. addr.sin_addr.s_addr = INADDR_ANY;
  186. if ((bind(retval, (struct sockaddr *) &addr, (socklen_t) sizeof (addr)) == -1) ||
  187. (listen(retval, 5) == -1))
  188. {
  189. close(retval);
  190. retval = -1;
  191. } /* if */
  192. } /* if */
  193. return retval;
  194. } /* create_listen_socket */
  195. static int listensocket = -1;
  196. void at_exit_cleanup(void)
  197. {
  198. /*
  199. * !!! FIXME: If thread support, signal threads to terminate and
  200. * !!! FIXME: wait for them to clean up.
  201. */
  202. if (listensocket >= 0)
  203. close(listensocket);
  204. if (!PHYSFS_deinit())
  205. printf("PHYSFS_deinit() failed: %s\n", PHYSFS_getLastError());
  206. } /* at_exit_cleanup */
  207. int main(int argc, char **argv)
  208. {
  209. int i;
  210. int portnum = DEFAULT_PORTNUM;
  211. setbuf(stdout, NULL);
  212. setbuf(stderr, NULL);
  213. #ifndef LACKING_SIGNALS
  214. /* I'm not sure if this qualifies as a cheap trick... */
  215. signal(SIGTERM, exit);
  216. signal(SIGINT, exit);
  217. signal(SIGFPE, exit);
  218. signal(SIGSEGV, exit);
  219. signal(SIGPIPE, exit);
  220. signal(SIGILL, exit);
  221. #endif
  222. if (argc == 1)
  223. {
  224. printf("USAGE: %s <archive1> [archive2 [... archiveN]]\n", argv[0]);
  225. return 42;
  226. } /* if */
  227. if (!PHYSFS_init(argv[0]))
  228. {
  229. printf("PHYSFS_init() failed: %s\n", PHYSFS_getLastError());
  230. return 42;
  231. } /* if */
  232. /* normally, this is bad practice, but oh well. */
  233. atexit(at_exit_cleanup);
  234. for (i = 1; i < argc; i++)
  235. {
  236. if (!PHYSFS_mount(argv[i], NULL, 1))
  237. printf(" WARNING: failed to add [%s] to search path.\n", argv[i]);
  238. } /* else */
  239. listensocket = create_listen_socket(portnum);
  240. if (listensocket < 0)
  241. {
  242. printf("listen socket failed to create.\n");
  243. return 42;
  244. } /* if */
  245. while (1) /* infinite loop for now. */
  246. {
  247. struct sockaddr addr;
  248. socklen_t len;
  249. int s = accept(listensocket, &addr, &len);
  250. if (s < 0)
  251. {
  252. printf("accept() failed: %s\n", strerror(errno));
  253. close(listensocket);
  254. return 42;
  255. } /* if */
  256. serve_http_request(s, &addr, len);
  257. } /* while */
  258. return 0;
  259. } /* main */
  260. /* end of physfshttpd.c ... */