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.
6 * Basically, you compile this code, and run it:
7 * ./physfshttpd archive1.zip archive2.zip /path/to/a/real/dir etc...
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.
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.
16 * Command line I used to build this on Linux:
17 * gcc -Wall -Werror -g -o bin/physfshttpd extras/physfshttpd.c -lphysfs
19 * License: this code is public domain. I make no warranty that it is useful,
20 * correct, harmless, or environmentally safe.
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
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.
32 * This file was written by Ryan C. Gordon. (icculus@icculus.org).
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
46 #ifndef LACKING_SIGNALS
50 #ifndef LACKING_PROTOENT
57 #define DEFAULT_PORTNUM 6667
62 struct sockaddr *addr;
68 "HTTP/1.0 404 Not Found\n"
70 "Content-Type: text/html; charset=utf-8\n"
72 "<html><head><title>404 Not Found</title></head>\n"
73 "<body>Can't find that.</body></html>\n\n";
76 static void feed_file_http(const char *ipstr, int sock, const char *fname)
78 PHYSFS_File *in = PHYSFS_openRead(fname);
80 printf("%s: requested [%s].\n", ipstr, fname);
83 printf("%s: Can't open [%s]: %s.\n",
84 ipstr, fname, PHYSFS_getLastError());
85 write(sock, txt404, strlen(txt404)); /* !!! FIXME: Check retval */
91 PHYSFS_sint64 br = PHYSFS_read(in, buffer, 1, sizeof (buffer));
94 printf("%s: Read error: %s.\n", ipstr, PHYSFS_getLastError());
98 write(sock, buffer, (int) br); /* !!! FIXME: CHECK THIS RETVAL! */
99 } while (!PHYSFS_eof(in));
103 } /* feed_file_http */
106 static void *do_http(void *_args)
108 http_args *args = (http_args *) _args;
112 strncpy(ipstr, inet_ntoa(((struct sockaddr_in *) args->addr)->sin_addr),
114 ipstr[sizeof (ipstr) - 1] = '\0';
116 printf("%s: connected.\n", ipstr);
117 read(args->sock, buffer, sizeof (buffer));
118 buffer[sizeof (buffer) - 1] = '\0';
119 ptr = strchr(buffer, '\n');
121 printf("%s: potentially bogus request.\n", ipstr);
125 ptr = strchr(buffer, '\r');
129 if ((toupper(buffer[0]) == 'G') &&
130 (toupper(buffer[1]) == 'E') &&
131 (toupper(buffer[2]) == 'T') &&
132 (toupper(buffer[3]) == ' ') &&
133 (toupper(buffer[4]) == '/'))
135 ptr = strchr(buffer + 5, ' ');
138 feed_file_http(ipstr, args->sock, buffer + 4);
142 /* !!! FIXME: Time the transfer. */
143 printf("%s: closing connection.\n", ipstr);
151 static void serve_http_request(int sock, struct sockaddr *addr,
154 http_args *args = (http_args *) malloc(sizeof (http_args));
157 printf("out of memory.\n");
160 args->addr = (struct sockaddr *) malloc(addrlen);
161 if (args->addr == NULL)
164 printf("out of memory.\n");
169 args->addrlen = addrlen;
170 memcpy(args->addr, addr, addrlen);
172 /* !!! FIXME: optionally spin a thread... */
173 do_http((void *) args);
174 } /* server_http_request */
177 static int create_listen_socket(short portnum)
180 int protocol = 0; /* pray this is right. */
182 #ifndef LACKING_PROTOENT
183 struct protoent *prot;
185 prot = getprotobyname("tcp");
187 protocol = prot->p_proto;
190 retval = socket(PF_INET, SOCK_STREAM, protocol);
193 struct sockaddr_in addr;
194 addr.sin_family = AF_INET;
195 addr.sin_port = htons(portnum);
196 addr.sin_addr.s_addr = INADDR_ANY;
197 if ((bind(retval, &addr, (socklen_t) sizeof (addr)) == -1) ||
198 (listen(retval, 5) == -1))
206 } /* create_listen_socket */
209 static int listensocket = -1;
211 void at_exit_cleanup(void)
214 * !!! FIXME: If thread support, signal threads to terminate and
215 * !!! FIXME: wait for them to clean up.
218 if (listensocket >= 0)
221 if (!PHYSFS_deinit())
222 printf("PHYSFS_deinit() failed: %s\n", PHYSFS_getLastError());
223 } /* at_exit_cleanup */
226 int main(int argc, char **argv)
229 int portnum = DEFAULT_PORTNUM;
231 setbuf(stdout, NULL);
232 setbuf(stderr, NULL);
234 #ifndef LACKING_SIGNALS
235 /* I'm not sure if this qualifies as a cheap trick... */
236 signal(SIGTERM, exit);
237 signal(SIGINT, exit);
238 signal(SIGFPE, exit);
239 signal(SIGSEGV, exit);
240 signal(SIGPIPE, exit);
241 signal(SIGILL, exit);
246 printf("USAGE: %s <archive1> [archive2 [... archiveN]]\n", argv[0]);
250 if (!PHYSFS_init(argv[0]))
252 printf("PHYSFS_init() failed: %s\n", PHYSFS_getLastError());
256 /* normally, this is bad practice, but oh well. */
257 atexit(at_exit_cleanup);
259 for (i = 1; i < argc; i++)
261 if (!PHYSFS_addToSearchPath(argv[i], 1))
262 printf(" WARNING: failed to add [%s] to search path.\n", argv[i]);
265 listensocket = create_listen_socket(portnum);
266 if (listensocket < 0)
268 printf("listen socket failed to create.\n");
272 while (1) /* infinite loop for now. */
274 struct sockaddr addr;
276 int s = accept(listensocket, &addr, &len);
279 printf("accept() failed: %s\n", strerror(errno));
284 serve_http_request(s, &addr, len);
290 /* end of physfshttpd.c ... */