extras/physfshttpd.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 11 Jul 2002 21:37:30 +0000
changeset 335 1d4720c018c3
parent 206 b4663929d1ae
child 576 5da65f8e9a50
permissions -rw-r--r--
FIXME cleanups.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
206
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * This is a quick and dirty HTTP server that uses PhysicsFS to retrieve
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *  files. It is not robust at all, probably buggy, and definitely poorly
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 *  designed. It's just meant to show that it can be done.
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 * Basically, you compile this code, and run it:
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *   ./physfshttpd archive1.zip archive2.zip /path/to/a/real/dir etc...
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 *
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 * The files are appended in order to the PhysicsFS search path, and when
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 *  a client request comes it, it looks for the file in said search path.
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 *
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 * My goal was to make this work in less than 300 lines of C, so again, it's
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 *  not to be used for any serious purpose. Patches to make this application
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 *  suck less will be readily and gratefully accepted.
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
 *
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
 * Command line I used to build this on Linux:
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
 *  gcc -Wall -Werror -g -o bin/physfshttpd extras/physfshttpd.c -lphysfs
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
 *
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
 * License: this code is public domain. I make no warranty that it is useful,
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
 *  correct, harmless, or environmentally safe.
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
 *
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
 * This particular file may be used however you like, including copying it
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 *  verbatim into a closed-source project, exploiting it commercially, and
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
 *  removing any trace of my name from the source (although I hope you won't
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
 *  do that). I welcome enhancements and corrections to this file, but I do
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
 *  not require you to send me patches if you make changes.
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
 *
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
 * Unless otherwise stated, the rest of PhysicsFS falls under the GNU Lesser
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
 *  General Public License: http://www.gnu.org/licenses/lgpl.txt
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
 *
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
 *  This file was written by Ryan C. Gordon. (icculus@clutteredmind.org).
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
 */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
#include <stdio.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
#include <stdlib.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
#include <string.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
#include <unistd.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
#include <errno.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
#include <ctype.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
#include <sys/types.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
#include <sys/socket.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
#include <netinet/in.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
#include <arpa/inet.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
#ifndef LACKING_SIGNALS
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
#include <signal.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
#endif
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
#ifndef LACKING_PROTOENT
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
#include <netdb.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
#endif
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
#include "physfs.h"
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
#define DEFAULT_PORTNUM  6667
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
typedef struct
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
{
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
    int sock;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    struct sockaddr *addr;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    socklen_t addrlen;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
} http_args;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
static char *txt404 =
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
"HTTP/1.0 404 Not Found\n"
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
"Connection: close\n"
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
"Content-type: text/html\n"
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
"\n"
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
"<html><head><title>404 Not Found</title></head>\n"
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
"<body>Can't find that.</body></html>\n\n";
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
static void feed_file_http(const char *ipstr, int sock, const char *fname)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
{
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    PHYSFS_file *in = PHYSFS_openRead(fname);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    char buffer[1024];
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    printf("%s: requested [%s].\n", ipstr, fname);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    if (in == NULL)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
        printf("%s: Can't open [%s]: %s.\n",
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
               ipstr, fname, PHYSFS_getLastError());
335
1d4720c018c3 FIXME cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
    84
        write(sock, txt404, strlen(txt404));  /* !!! FIXME: Check retval */
206
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
    else
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
        do
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
        {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
            PHYSFS_sint64 br = PHYSFS_read(in, buffer, 1, sizeof (buffer));
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
            if (br == -1)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
            {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
                printf("%s: Read error: %s.\n", ipstr, PHYSFS_getLastError());
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
                break;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
            } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
335
1d4720c018c3 FIXME cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
    97
            write(sock, buffer, (int) br);   /* !!! FIXME: CHECK THIS RETVAL! */
206
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
        } while (!PHYSFS_eof(in));
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
        PHYSFS_close(in);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    } /* else */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
} /* feed_file_http */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
static void *do_http(void *_args)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
{
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    http_args *args = (http_args *) _args;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
    char ipstr[128];
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    char buffer[512];
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    char *ptr;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
    strncpy(ipstr, inet_ntoa(((struct sockaddr_in *) args->addr)->sin_addr),
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
            sizeof (ipstr));
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    ipstr[sizeof (ipstr) - 1] = '\0';
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
    printf("%s: connected.\n", ipstr);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    read(args->sock, buffer, sizeof (buffer));
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
    buffer[sizeof (buffer) - 1] = '\0';
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    ptr = strchr(buffer, '\n');
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    if (!ptr)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
        printf("%s: potentially bogus request.\n", ipstr);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    else
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
        *ptr = '\0';
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
        ptr = strchr(buffer, '\r');
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
        if (ptr != NULL)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
            *ptr = '\0';
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
        if ((toupper(buffer[0]) == 'G') &&
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
            (toupper(buffer[1]) == 'E') &&
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
            (toupper(buffer[2]) == 'T') &&
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
            (toupper(buffer[3]) == ' ') &&
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
            (toupper(buffer[4]) == '/'))
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
        {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
            ptr = strchr(buffer + 5, ' ');
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
            if (ptr != NULL)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
                *ptr = '\0';
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
            feed_file_http(ipstr, args->sock, buffer + 4);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
        } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    } /* else */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    /* !!! FIXME: Time the transfer. */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    printf("%s: closing connection.\n", ipstr);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    close(args->sock);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    free(args->addr);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    free(args);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    return(NULL);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
} /* do_http */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
static void serve_http_request(int sock, struct sockaddr *addr,
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
                               socklen_t addrlen)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
{
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
    http_args *args = (http_args *) malloc(sizeof (http_args));
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    if (args == NULL)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
        printf("out of memory.\n");
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
        return;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    } // if
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    args->addr = (struct sockaddr *) malloc(addrlen);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    if (args->addr == NULL)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
        free(args);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
        printf("out of memory.\n");
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
        return;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
    } // if
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
    args->sock = sock;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    args->addrlen = addrlen;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    memcpy(args->addr, addr, addrlen);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
    /* !!! FIXME: optionally spin a thread... */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
    do_http((void *) args);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
} /* server_http_request */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
static int create_listen_socket(short portnum)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
{
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
    int retval = -1;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
    int protocol = 0;  /* pray this is right. */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
#ifndef LACKING_PROTOENT
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
    struct protoent *prot;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
    setprotoent(0);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
    prot = getprotobyname("tcp");
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
    if (prot != NULL)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
        protocol = prot->p_proto;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
#endif
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
    retval = socket(PF_INET, SOCK_STREAM, protocol);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    if (retval >= 0)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
        struct sockaddr_in addr;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
        addr.sin_family = AF_INET;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
        addr.sin_port = htons(portnum);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
        addr.sin_addr.s_addr = INADDR_ANY;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
        if ((bind(retval, &addr, (socklen_t) sizeof (addr)) == -1) ||
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
            (listen(retval, 5) == -1))
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
        {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
            close(retval);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
            retval = -1;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
        } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
    } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
    return(retval);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
} /* create_listen_socket */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
static int listensocket = -1;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
void at_exit_cleanup(void)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
{
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
    /*
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
     * !!! FIXME: If thread support, signal threads to terminate and
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
     * !!! FIXME:  wait for them to clean up.
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
     */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
    if (listensocket >= 0)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
        close(listensocket);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
    if (!PHYSFS_deinit())
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
        printf("PHYSFS_deinit() failed: %s\n", PHYSFS_getLastError());
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
} /* at_exit_cleanup */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
int main(int argc, char **argv)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
{
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    int i;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
    int portnum = DEFAULT_PORTNUM;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
    setbuf(stdout, NULL);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    setbuf(stderr, NULL);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
#ifndef LACKING_SIGNALS
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    /* I'm not sure if this qualifies as a cheap trick... */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    signal(SIGTERM, exit);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    signal(SIGINT, exit);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    signal(SIGFPE, exit);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    signal(SIGSEGV, exit);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
    signal(SIGPIPE, exit);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
    signal(SIGILL, exit);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
#endif
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
    if (argc == 1)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
        printf("USAGE: %s <archive1> [archive2 [... archiveN]]\n", argv[0]);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
        return(42);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
    } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
    if (!PHYSFS_init(argv[0]))
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
        printf("PHYSFS_init() failed: %s\n", PHYSFS_getLastError());
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
        return(42);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
    } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
    /* normally, this is bad practice, but oh well. */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
    atexit(at_exit_cleanup);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
    for (i = 1; i < argc; i++)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
        if (!PHYSFS_addToSearchPath(argv[i], 1))
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
            printf(" WARNING: failed to add [%s] to search path.\n", argv[i]);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
    } /* else */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
    listensocket = create_listen_socket(portnum);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    if (listensocket < 0)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
        printf("listen socket failed to create.\n");
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
        return(42);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
    } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
    while (1)  /* infinite loop for now. */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
        struct sockaddr addr;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
        socklen_t len;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
        int s = accept(listensocket, &addr, &len);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
        if (s < 0)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
        {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
            printf("accept() failed: %s\n", strerror(errno));
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
            close(listensocket);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
            return(42);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
        } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
        serve_http_request(s, &addr, len);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
    } /* while */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
    return(0);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
} /* main */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
/* end of physfshttpd.c ... */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290