extras/physfshttpd.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Mar 2007 10:10:28 +0000
changeset 809 116b8fe30371
parent 767 db29bf06d171
child 912 a68ff7aaf9a3
permissions -rw-r--r--
Renamed LICENSE to LICENSE.txt
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
576
5da65f8e9a50 Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    26
 *  not require you to send me patches if you make changes. This code has
5da65f8e9a50 Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    27
 *  NO WARRANTY.
206
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
 *
576
5da65f8e9a50 Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    29
 * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
809
116b8fe30371 Renamed LICENSE to LICENSE.txt
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
    30
 *  Please see LICENSE.txt in the root of the source tree.
206
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
 *
767
db29bf06d171 Changed my email address.
Ryan C. Gordon <icculus@icculus.org>
parents: 654
diff changeset
    32
 *  This file was written by Ryan C. Gordon. (icculus@icculus.org).
206
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
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
#include <stdio.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
#include <stdlib.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
#include <string.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
#include <unistd.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
#include <errno.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
#include <ctype.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
#include <sys/types.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
#include <sys/socket.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
#include <netinet/in.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
#include <arpa/inet.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
#ifndef LACKING_SIGNALS
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
#include <signal.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
#endif
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
#ifndef LACKING_PROTOENT
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
#include <netdb.h>
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
#endif
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
#include "physfs.h"
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
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
#define DEFAULT_PORTNUM  6667
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
typedef struct
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
{
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    int sock;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    struct sockaddr *addr;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    socklen_t addrlen;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
} http_args;
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
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
static char *txt404 =
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
"HTTP/1.0 404 Not Found\n"
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
"Connection: close\n"
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
"Content-type: text/html\n"
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
"\n"
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
"<html><head><title>404 Not Found</title></head>\n"
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
"<body>Can't find that.</body></html>\n\n";
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
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
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
    77
{
654
c0ae01de361d Changed PHYSFS_file to PHYSFS_File to match rest of API's naming
Ryan C. Gordon <icculus@icculus.org>
parents: 576
diff changeset
    78
    PHYSFS_File *in = PHYSFS_openRead(fname);
206
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    char buffer[1024];
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    printf("%s: requested [%s].\n", ipstr, fname);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    if (in == NULL)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
        printf("%s: Can't open [%s]: %s.\n",
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
               ipstr, fname, PHYSFS_getLastError());
335
1d4720c018c3 FIXME cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
    85
        write(sock, txt404, strlen(txt404));  /* !!! FIXME: Check retval */
206
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
    } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    else
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
        do
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
        {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
            PHYSFS_sint64 br = PHYSFS_read(in, buffer, 1, sizeof (buffer));
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
            if (br == -1)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
            {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
                printf("%s: Read error: %s.\n", ipstr, PHYSFS_getLastError());
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
                break;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
            } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
335
1d4720c018c3 FIXME cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
    98
            write(sock, buffer, (int) br);   /* !!! FIXME: CHECK THIS RETVAL! */
206
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
        } while (!PHYSFS_eof(in));
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
        PHYSFS_close(in);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    } /* else */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
} /* feed_file_http */
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
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
static void *do_http(void *_args)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
{
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
    http_args *args = (http_args *) _args;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    char ipstr[128];
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    char buffer[512];
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
    char *ptr;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    strncpy(ipstr, inet_ntoa(((struct sockaddr_in *) args->addr)->sin_addr),
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
            sizeof (ipstr));
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
    ipstr[sizeof (ipstr) - 1] = '\0';
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    printf("%s: connected.\n", ipstr);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
    read(args->sock, buffer, sizeof (buffer));
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    buffer[sizeof (buffer) - 1] = '\0';
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
    ptr = strchr(buffer, '\n');
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    if (!ptr)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
        printf("%s: potentially bogus request.\n", ipstr);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    else
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
        *ptr = '\0';
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
        ptr = strchr(buffer, '\r');
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
        if (ptr != NULL)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
            *ptr = '\0';
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
        if ((toupper(buffer[0]) == 'G') &&
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
            (toupper(buffer[1]) == 'E') &&
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
            (toupper(buffer[2]) == 'T') &&
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
            (toupper(buffer[3]) == ' ') &&
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
            (toupper(buffer[4]) == '/'))
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
        {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
            ptr = strchr(buffer + 5, ' ');
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
            if (ptr != NULL)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
                *ptr = '\0';
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
            feed_file_http(ipstr, args->sock, buffer + 4);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
        } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    } /* else */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    /* !!! FIXME: Time the transfer. */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    printf("%s: closing connection.\n", ipstr);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    close(args->sock);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    free(args->addr);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    free(args);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
    return(NULL);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
} /* do_http */
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
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
static void serve_http_request(int sock, struct sockaddr *addr,
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
                               socklen_t addrlen)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
{
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    http_args *args = (http_args *) malloc(sizeof (http_args));
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    if (args == NULL)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
        printf("out of memory.\n");
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
        return;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    } // if
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    args->addr = (struct sockaddr *) malloc(addrlen);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    if (args->addr == NULL)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
        free(args);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
        printf("out of memory.\n");
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
        return;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
    } // if
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    args->sock = sock;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    args->addrlen = addrlen;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
    memcpy(args->addr, addr, addrlen);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
    /* !!! FIXME: optionally spin a thread... */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
    do_http((void *) args);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
} /* server_http_request */
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
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
static int create_listen_socket(short portnum)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
{
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
    int retval = -1;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
    int protocol = 0;  /* pray this is right. */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
#ifndef LACKING_PROTOENT
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
    struct protoent *prot;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
    setprotoent(0);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
    prot = getprotobyname("tcp");
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
    if (prot != NULL)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
        protocol = prot->p_proto;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
#endif
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    retval = socket(PF_INET, SOCK_STREAM, protocol);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    if (retval >= 0)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
        struct sockaddr_in addr;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
        addr.sin_family = AF_INET;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
        addr.sin_port = htons(portnum);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
        addr.sin_addr.s_addr = INADDR_ANY;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
        if ((bind(retval, &addr, (socklen_t) sizeof (addr)) == -1) ||
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
            (listen(retval, 5) == -1))
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
        {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
            close(retval);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
            retval = -1;
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
    } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
    return(retval);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
} /* create_listen_socket */
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
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
static int listensocket = -1;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
void at_exit_cleanup(void)
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
    /*
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
     * !!! FIXME: If thread support, signal threads to terminate and
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
     * !!! FIXME:  wait for them to clean up.
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
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
    if (listensocket >= 0)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
        close(listensocket);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
    if (!PHYSFS_deinit())
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
        printf("PHYSFS_deinit() failed: %s\n", PHYSFS_getLastError());
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
} /* at_exit_cleanup */
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
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
int main(int argc, char **argv)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
{
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
    int i;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
    int portnum = DEFAULT_PORTNUM;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    setbuf(stdout, NULL);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
    setbuf(stderr, NULL);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
#ifndef LACKING_SIGNALS
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    /* I'm not sure if this qualifies as a cheap trick... */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    signal(SIGTERM, exit);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    signal(SIGINT, exit);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    signal(SIGFPE, exit);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
    signal(SIGSEGV, exit);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
    signal(SIGPIPE, exit);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
    signal(SIGILL, exit);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
#endif
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
    if (argc == 1)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
        printf("USAGE: %s <archive1> [archive2 [... archiveN]]\n", argv[0]);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
        return(42);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
    } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    if (!PHYSFS_init(argv[0]))
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
        printf("PHYSFS_init() failed: %s\n", PHYSFS_getLastError());
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
        return(42);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
    } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
    /* normally, this is bad practice, but oh well. */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
    atexit(at_exit_cleanup);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
    for (i = 1; i < argc; i++)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
        if (!PHYSFS_addToSearchPath(argv[i], 1))
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
            printf(" WARNING: failed to add [%s] to search path.\n", argv[i]);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
    } /* else */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    listensocket = create_listen_socket(portnum);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
    if (listensocket < 0)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
        printf("listen socket failed to create.\n");
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
        return(42);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
    } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
    while (1)  /* infinite loop for now. */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
    {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
        struct sockaddr addr;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
        socklen_t len;
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
        int s = accept(listensocket, &addr, &len);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
        if (s < 0)
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
        {
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
            printf("accept() failed: %s\n", strerror(errno));
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
            close(listensocket);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
            return(42);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
        } /* if */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
        serve_http_request(s, &addr, len);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
    } /* while */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
    return(0);
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
} /* main */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
/* end of physfshttpd.c ... */
b4663929d1ae Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291