Subversion Repositories Games.Descent

Rev

Blame | Last modification | View Log | Download | RSS feed

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