1
0

physfshttpd.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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.
  27. *
  28. * Unless otherwise stated, the rest of PhysicsFS falls under the GNU Lesser
  29. * General Public License: http://www.gnu.org/licenses/lgpl.txt
  30. *
  31. * This file was written by Ryan C. Gordon. (icculus@clutteredmind.org).
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include <errno.h>
  38. #include <ctype.h>
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #ifndef LACKING_SIGNALS
  44. #include <signal.h>
  45. #endif
  46. #ifndef LACKING_PROTOENT
  47. #include <netdb.h>
  48. #endif
  49. #include "physfs.h"
  50. #define DEFAULT_PORTNUM 6667
  51. typedef struct
  52. {
  53. int sock;
  54. struct sockaddr *addr;
  55. socklen_t addrlen;
  56. } http_args;
  57. static char *txt404 =
  58. "HTTP/1.0 404 Not Found\n"
  59. "Connection: close\n"
  60. "Content-type: text/html\n"
  61. "\n"
  62. "<html><head><title>404 Not Found</title></head>\n"
  63. "<body>Can't find that.</body></html>\n\n";
  64. static void feed_file_http(const char *ipstr, int sock, const char *fname)
  65. {
  66. PHYSFS_file *in = PHYSFS_openRead(fname);
  67. char buffer[1024];
  68. printf("%s: requested [%s].\n", ipstr, fname);
  69. if (in == NULL)
  70. {
  71. printf("%s: Can't open [%s]: %s.\n",
  72. ipstr, fname, PHYSFS_getLastError());
  73. write(sock, txt404, strlen(txt404)); /* !!! Check retval */
  74. } /* if */
  75. else
  76. {
  77. do
  78. {
  79. PHYSFS_sint64 br = PHYSFS_read(in, buffer, 1, sizeof (buffer));
  80. if (br == -1)
  81. {
  82. printf("%s: Read error: %s.\n", ipstr, PHYSFS_getLastError());
  83. break;
  84. } /* if */
  85. write(sock, buffer, (int) br); /* !!! CHECK THIS RETVAL! */
  86. } while (!PHYSFS_eof(in));
  87. PHYSFS_close(in);
  88. } /* else */
  89. } /* feed_file_http */
  90. static void *do_http(void *_args)
  91. {
  92. http_args *args = (http_args *) _args;
  93. char ipstr[128];
  94. char buffer[512];
  95. char *ptr;
  96. strncpy(ipstr, inet_ntoa(((struct sockaddr_in *) args->addr)->sin_addr),
  97. sizeof (ipstr));
  98. ipstr[sizeof (ipstr) - 1] = '\0';
  99. printf("%s: connected.\n", ipstr);
  100. read(args->sock, buffer, sizeof (buffer));
  101. buffer[sizeof (buffer) - 1] = '\0';
  102. ptr = strchr(buffer, '\n');
  103. if (!ptr)
  104. printf("%s: potentially bogus request.\n", ipstr);
  105. else
  106. {
  107. *ptr = '\0';
  108. ptr = strchr(buffer, '\r');
  109. if (ptr != NULL)
  110. *ptr = '\0';
  111. if ((toupper(buffer[0]) == 'G') &&
  112. (toupper(buffer[1]) == 'E') &&
  113. (toupper(buffer[2]) == 'T') &&
  114. (toupper(buffer[3]) == ' ') &&
  115. (toupper(buffer[4]) == '/'))
  116. {
  117. ptr = strchr(buffer + 5, ' ');
  118. if (ptr != NULL)
  119. *ptr = '\0';
  120. feed_file_http(ipstr, args->sock, buffer + 4);
  121. } /* if */
  122. } /* else */
  123. /* !!! FIXME: Time the transfer. */
  124. printf("%s: closing connection.\n", ipstr);
  125. close(args->sock);
  126. free(args->addr);
  127. free(args);
  128. return(NULL);
  129. } /* do_http */
  130. static void serve_http_request(int sock, struct sockaddr *addr,
  131. socklen_t addrlen)
  132. {
  133. http_args *args = (http_args *) malloc(sizeof (http_args));
  134. if (args == NULL)
  135. {
  136. printf("out of memory.\n");
  137. return;
  138. } // if
  139. args->addr = (struct sockaddr *) malloc(addrlen);
  140. if (args->addr == NULL)
  141. {
  142. free(args);
  143. printf("out of memory.\n");
  144. return;
  145. } // if
  146. args->sock = sock;
  147. args->addrlen = addrlen;
  148. memcpy(args->addr, addr, addrlen);
  149. /* !!! FIXME: optionally spin a thread... */
  150. do_http((void *) args);
  151. } /* server_http_request */
  152. static int create_listen_socket(short portnum)
  153. {
  154. int retval = -1;
  155. int protocol = 0; /* pray this is right. */
  156. #ifndef LACKING_PROTOENT
  157. struct protoent *prot;
  158. setprotoent(0);
  159. prot = getprotobyname("tcp");
  160. if (prot != NULL)
  161. protocol = prot->p_proto;
  162. #endif
  163. retval = socket(PF_INET, SOCK_STREAM, protocol);
  164. if (retval >= 0)
  165. {
  166. struct sockaddr_in addr;
  167. addr.sin_family = AF_INET;
  168. addr.sin_port = htons(portnum);
  169. addr.sin_addr.s_addr = INADDR_ANY;
  170. if ((bind(retval, &addr, (socklen_t) sizeof (addr)) == -1) ||
  171. (listen(retval, 5) == -1))
  172. {
  173. close(retval);
  174. retval = -1;
  175. } /* if */
  176. } /* if */
  177. return(retval);
  178. } /* create_listen_socket */
  179. static int listensocket = -1;
  180. void at_exit_cleanup(void)
  181. {
  182. /*
  183. * !!! FIXME: If thread support, signal threads to terminate and
  184. * !!! FIXME: wait for them to clean up.
  185. */
  186. if (listensocket >= 0)
  187. close(listensocket);
  188. if (!PHYSFS_deinit())
  189. printf("PHYSFS_deinit() failed: %s\n", PHYSFS_getLastError());
  190. } /* at_exit_cleanup */
  191. int main(int argc, char **argv)
  192. {
  193. int i;
  194. int portnum = DEFAULT_PORTNUM;
  195. setbuf(stdout, NULL);
  196. setbuf(stderr, NULL);
  197. #ifndef LACKING_SIGNALS
  198. /* I'm not sure if this qualifies as a cheap trick... */
  199. signal(SIGTERM, exit);
  200. signal(SIGINT, exit);
  201. signal(SIGFPE, exit);
  202. signal(SIGSEGV, exit);
  203. signal(SIGPIPE, exit);
  204. signal(SIGILL, exit);
  205. #endif
  206. if (argc == 1)
  207. {
  208. printf("USAGE: %s <archive1> [archive2 [... archiveN]]\n", argv[0]);
  209. return(42);
  210. } /* if */
  211. if (!PHYSFS_init(argv[0]))
  212. {
  213. printf("PHYSFS_init() failed: %s\n", PHYSFS_getLastError());
  214. return(42);
  215. } /* if */
  216. /* normally, this is bad practice, but oh well. */
  217. atexit(at_exit_cleanup);
  218. for (i = 1; i < argc; i++)
  219. {
  220. if (!PHYSFS_addToSearchPath(argv[i], 1))
  221. printf(" WARNING: failed to add [%s] to search path.\n", argv[i]);
  222. } /* else */
  223. listensocket = create_listen_socket(portnum);
  224. if (listensocket < 0)
  225. {
  226. printf("listen socket failed to create.\n");
  227. return(42);
  228. } /* if */
  229. while (1) /* infinite loop for now. */
  230. {
  231. struct sockaddr addr;
  232. socklen_t len;
  233. int s = accept(listensocket, &addr, &len);
  234. if (s < 0)
  235. {
  236. printf("accept() failed: %s\n", strerror(errno));
  237. close(listensocket);
  238. return(42);
  239. } /* if */
  240. serve_http_request(s, &addr, len);
  241. } /* while */
  242. return(0);
  243. } /* main */
  244. /* end of physfshttpd.c ... */