206
|
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 |
|
|
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 |
|
|
45 |
#ifndef LACKING_SIGNALS
|
|
46 |
#include <signal.h>
|
|
47 |
#endif
|
|
48 |
|
|
49 |
#ifndef LACKING_PROTOENT
|
|
50 |
#include <netdb.h>
|
|
51 |
#endif
|
|
52 |
|
|
53 |
#include "physfs.h"
|
|
54 |
|
|
55 |
|
|
56 |
#define DEFAULT_PORTNUM 6667
|
|
57 |
|
|
58 |
typedef struct
|
|
59 |
{
|
|
60 |
int sock;
|
|
61 |
struct sockaddr *addr;
|
|
62 |
socklen_t addrlen;
|
|
63 |
} http_args;
|
|
64 |
|
|
65 |
|
|
66 |
static char *txt404 =
|
|
67 |
"HTTP/1.0 404 Not Found\n"
|
|
68 |
"Connection: close\n"
|
|
69 |
"Content-type: text/html\n"
|
|
70 |
"\n"
|
|
71 |
"<html><head><title>404 Not Found</title></head>\n"
|
|
72 |
"<body>Can't find that.</body></html>\n\n";
|
|
73 |
|
|
74 |
|
|
75 |
static void feed_file_http(const char *ipstr, int sock, const char *fname)
|
|
76 |
{
|
|
77 |
PHYSFS_file *in = PHYSFS_openRead(fname);
|
|
78 |
char buffer[1024];
|
|
79 |
printf("%s: requested [%s].\n", ipstr, fname);
|
|
80 |
if (in == NULL)
|
|
81 |
{
|
|
82 |
printf("%s: Can't open [%s]: %s.\n",
|
|
83 |
ipstr, fname, PHYSFS_getLastError());
|
335
|
84 |
write(sock, txt404, strlen(txt404)); /* !!! FIXME: Check retval */
|
206
|
85 |
} /* if */
|
|
86 |
else
|
|
87 |
{
|
|
88 |
do
|
|
89 |
{
|
|
90 |
PHYSFS_sint64 br = PHYSFS_read(in, buffer, 1, sizeof (buffer));
|
|
91 |
if (br == -1)
|
|
92 |
{
|
|
93 |
printf("%s: Read error: %s.\n", ipstr, PHYSFS_getLastError());
|
|
94 |
break;
|
|
95 |
} /* if */
|
|
96 |
|
335
|
97 |
write(sock, buffer, (int) br); /* !!! FIXME: CHECK THIS RETVAL! */
|
206
|
98 |
} while (!PHYSFS_eof(in));
|
|
99 |
|
|
100 |
PHYSFS_close(in);
|
|
101 |
} /* else */
|
|
102 |
} /* feed_file_http */
|
|
103 |
|
|
104 |
|
|
105 |
static void *do_http(void *_args)
|
|
106 |
{
|
|
107 |
http_args *args = (http_args *) _args;
|
|
108 |
char ipstr[128];
|
|
109 |
char buffer[512];
|
|
110 |
char *ptr;
|
|
111 |
strncpy(ipstr, inet_ntoa(((struct sockaddr_in *) args->addr)->sin_addr),
|
|
112 |
sizeof (ipstr));
|
|
113 |
ipstr[sizeof (ipstr) - 1] = '\0';
|
|
114 |
|
|
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 |
|
|
128 |
if ((toupper(buffer[0]) == 'G') &&
|
|
129 |
(toupper(buffer[1]) == 'E') &&
|
|
130 |
(toupper(buffer[2]) == 'T') &&
|
|
131 |
(toupper(buffer[3]) == ' ') &&
|
|
132 |
(toupper(buffer[4]) == '/'))
|
|
133 |
{
|
|
134 |
ptr = strchr(buffer + 5, ' ');
|
|
135 |
if (ptr != NULL)
|
|
136 |
*ptr = '\0';
|
|
137 |
feed_file_http(ipstr, args->sock, buffer + 4);
|
|
138 |
} /* if */
|
|
139 |
} /* else */
|
|
140 |
|
|
141 |
/* !!! FIXME: Time the transfer. */
|
|
142 |
printf("%s: closing connection.\n", ipstr);
|
|
143 |
close(args->sock);
|
|
144 |
free(args->addr);
|
|
145 |
free(args);
|
|
146 |
return(NULL);
|
|
147 |
} /* do_http */
|
|
148 |
|
|
149 |
|
|
150 |
static void serve_http_request(int sock, struct sockaddr *addr,
|
|
151 |
socklen_t addrlen)
|
|
152 |
{
|
|
153 |
http_args *args = (http_args *) malloc(sizeof (http_args));
|
|
154 |
if (args == NULL)
|
|
155 |
{
|
|
156 |
printf("out of memory.\n");
|
|
157 |
return;
|
|
158 |
} // if
|
|
159 |
args->addr = (struct sockaddr *) malloc(addrlen);
|
|
160 |
if (args->addr == NULL)
|
|
161 |
{
|
|
162 |
free(args);
|
|
163 |
printf("out of memory.\n");
|
|
164 |
return;
|
|
165 |
} // if
|
|
166 |
|
|
167 |
args->sock = sock;
|
|
168 |
args->addrlen = addrlen;
|
|
169 |
memcpy(args->addr, addr, addrlen);
|
|
170 |
|
|
171 |
/* !!! FIXME: optionally spin a thread... */
|
|
172 |
do_http((void *) args);
|
|
173 |
} /* server_http_request */
|
|
174 |
|
|
175 |
|
|
176 |
static int create_listen_socket(short portnum)
|
|
177 |
{
|
|
178 |
int retval = -1;
|
|
179 |
int protocol = 0; /* pray this is right. */
|
|
180 |
|
|
181 |
#ifndef LACKING_PROTOENT
|
|
182 |
struct protoent *prot;
|
|
183 |
setprotoent(0);
|
|
184 |
prot = getprotobyname("tcp");
|
|
185 |
if (prot != NULL)
|
|
186 |
protocol = prot->p_proto;
|
|
187 |
#endif
|
|
188 |
|
|
189 |
retval = socket(PF_INET, SOCK_STREAM, protocol);
|
|
190 |
if (retval >= 0)
|
|
191 |
{
|
|
192 |
struct sockaddr_in addr;
|
|
193 |
addr.sin_family = AF_INET;
|
|
194 |
addr.sin_port = htons(portnum);
|
|
195 |
addr.sin_addr.s_addr = INADDR_ANY;
|
|
196 |
if ((bind(retval, &addr, (socklen_t) sizeof (addr)) == -1) ||
|
|
197 |
(listen(retval, 5) == -1))
|
|
198 |
{
|
|
199 |
close(retval);
|
|
200 |
retval = -1;
|
|
201 |
} /* if */
|
|
202 |
} /* if */
|
|
203 |
|
|
204 |
return(retval);
|
|
205 |
} /* create_listen_socket */
|
|
206 |
|
|
207 |
|
|
208 |
static int listensocket = -1;
|
|
209 |
|
|
210 |
void at_exit_cleanup(void)
|
|
211 |
{
|
|
212 |
/*
|
|
213 |
* !!! FIXME: If thread support, signal threads to terminate and
|
|
214 |
* !!! FIXME: wait for them to clean up.
|
|
215 |
*/
|
|
216 |
|
|
217 |
if (listensocket >= 0)
|
|
218 |
close(listensocket);
|
|
219 |
|
|
220 |
if (!PHYSFS_deinit())
|
|
221 |
printf("PHYSFS_deinit() failed: %s\n", PHYSFS_getLastError());
|
|
222 |
} /* at_exit_cleanup */
|
|
223 |
|
|
224 |
|
|
225 |
int main(int argc, char **argv)
|
|
226 |
{
|
|
227 |
int i;
|
|
228 |
int portnum = DEFAULT_PORTNUM;
|
|
229 |
|
|
230 |
setbuf(stdout, NULL);
|
|
231 |
setbuf(stderr, NULL);
|
|
232 |
|
|
233 |
#ifndef LACKING_SIGNALS
|
|
234 |
/* I'm not sure if this qualifies as a cheap trick... */
|
|
235 |
signal(SIGTERM, exit);
|
|
236 |
signal(SIGINT, exit);
|
|
237 |
signal(SIGFPE, exit);
|
|
238 |
signal(SIGSEGV, exit);
|
|
239 |
signal(SIGPIPE, exit);
|
|
240 |
signal(SIGILL, exit);
|
|
241 |
#endif
|
|
242 |
|
|
243 |
if (argc == 1)
|
|
244 |
{
|
|
245 |
printf("USAGE: %s <archive1> [archive2 [... archiveN]]\n", argv[0]);
|
|
246 |
return(42);
|
|
247 |
} /* if */
|
|
248 |
|
|
249 |
if (!PHYSFS_init(argv[0]))
|
|
250 |
{
|
|
251 |
printf("PHYSFS_init() failed: %s\n", PHYSFS_getLastError());
|
|
252 |
return(42);
|
|
253 |
} /* if */
|
|
254 |
|
|
255 |
/* normally, this is bad practice, but oh well. */
|
|
256 |
atexit(at_exit_cleanup);
|
|
257 |
|
|
258 |
for (i = 1; i < argc; i++)
|
|
259 |
{
|
|
260 |
if (!PHYSFS_addToSearchPath(argv[i], 1))
|
|
261 |
printf(" WARNING: failed to add [%s] to search path.\n", argv[i]);
|
|
262 |
} /* else */
|
|
263 |
|
|
264 |
listensocket = create_listen_socket(portnum);
|
|
265 |
if (listensocket < 0)
|
|
266 |
{
|
|
267 |
printf("listen socket failed to create.\n");
|
|
268 |
return(42);
|
|
269 |
} /* if */
|
|
270 |
|
|
271 |
while (1) /* infinite loop for now. */
|
|
272 |
{
|
|
273 |
struct sockaddr addr;
|
|
274 |
socklen_t len;
|
|
275 |
int s = accept(listensocket, &addr, &len);
|
|
276 |
if (s < 0)
|
|
277 |
{
|
|
278 |
printf("accept() failed: %s\n", strerror(errno));
|
|
279 |
close(listensocket);
|
|
280 |
return(42);
|
|
281 |
} /* if */
|
|
282 |
|
|
283 |
serve_http_request(s, &addr, len);
|
|
284 |
} /* while */
|
|
285 |
|
|
286 |
return(0);
|
|
287 |
} /* main */
|
|
288 |
|
|
289 |
/* end of physfshttpd.c ... */
|
|
290 |
|