summaryrefslogtreecommitdiff
path: root/httpserve.c
blob: 946f4aec867c6a8c53d73b623e94d266f77b254d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*

    httpserve - Serve a directory over HTTP

    Copyright (C) 2024  Ian Cowburn (ianc@noddybox.co.uk)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>

#include <magic.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <libgen.h>
#include <glob.h>
#include <fcntl.h>

/* ---------------------------------------------------------------------------
   TYPES AND GLOBALS
   ---------------------------------------------------------------------------
*/
typedef struct
{
    int         fd;
    magic_t     magic;
} thread_arg_t;


/* ---------------------------------------------------------------------------
   BASE ROUTINES
   ---------------------------------------------------------------------------
*/
static char *Strdup(const char *p)
{
    char *ret = strdup(p);

    if (!ret)
    {
        perror("strdup");
        exit(EXIT_FAILURE);
    }

    return ret;
}

static void *Malloc(size_t s)
{
    void *ret = malloc(s);

    if (!ret)
    {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    return ret;
}

static void *Realloc(void *p, size_t s)
{
    void *ret = realloc(p, s);

    if (!ret)
    {
        perror("realloc");
        exit(EXIT_FAILURE);
    }

    return ret;
}

static char *PrintStream(char *stream, const char *format, ...)
{
    char buff[4096];
    va_list va;
    size_t stream_len;
    size_t buff_len;

    va_start(va, format);
    vsnprintf(buff, sizeof buff, format, va);
    va_end(va);

    buff_len = strlen(buff);

    if (stream)
    {
        stream_len = strlen(stream);
    }
    else
    {
        stream_len = 0;
    }

    stream = Realloc(stream, stream_len + buff_len + 1);

    strcpy(stream + stream_len, buff);

    return stream;
}


/* ---------------------------------------------------------------------------
   SOCKET ROUTINES
   ---------------------------------------------------------------------------
*/
static int Listen(int port)
{
    struct sockaddr_in addr;
    int fd;

    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
	perror("socket");
        return -1;
    }

    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port = htons(port);

    if (bind(fd, (void*)&addr, sizeof(addr)) == -1)
    {
        perror("bind");
	return -1;
    }

    if (listen(fd, 5) == -1)
    {
	perror("listen");
	return -1;
    }

    return fd;
}

static int Read(int fd, char *buff, size_t max)
{
    int rd;
    size_t tot = 0;

    while(tot < max)
    {
        rd = read(fd, buff + tot, max - tot);

        if (rd < 1)
        {
            return 0;
        }

        tot += rd;

        if (tot < max)
        {
            buff[tot] = 0;

            if (tot > 4 && buff[tot - 4] == '\r' && buff[tot - 3] == '\n' &&
                buff[tot - 2] == '\r' && buff[tot - 1] == '\n')
            {
                return 1;
            }
        }
    }

    fprintf(stderr, "HTTP request too big\n");
    return 0;
}


/* ---------------------------------------------------------------------------
   HTML ROUTINES
   ---------------------------------------------------------------------------
*/
static void GenerateListing(int fd, const char *protocol)
{
    glob_t gl;
    char *stream = NULL;
    int f;

    glob("*", 0, NULL, &gl);

    stream = PrintStream(stream, "<!DOCTYPE html>\n<html><head></head><body>");

    for(f = 0; f < gl.gl_pathc; f++)
    {
        stream = PrintStream(stream, "<a href=\"%s\">%s</a><br>",
                                        gl.gl_pathv[f], gl.gl_pathv[f]);
    }

    stream = PrintStream(stream, "</body></html>");

    dprintf(fd, "%s 200 OK\r\n"
                "Content-Type: text/html\r\n"
                "Content-Length: %lu\r\n"
                "\r\n"
                "%s", protocol, strlen(stream), stream);

    globfree(&gl);
    free(stream);
}

static void GenerateStatus(int fd,
                           int status,
                           const char *status_text,
                           const char *protocol)
{
    dprintf(fd, "%s %d %s\r\nContent-Length: 0\r\n\r\n",
                        protocol, status, status_text);
}

static void GenerateFile(int fd,
                         magic_t magic,
                         const char *path,
                         const char *protocol)
{
    char buff[4096];
    int file;
    off_t size;
    ssize_t len;

    file = open(path, O_RDONLY);

    if (file == -1)
    {
        perror("open");
        GenerateStatus(fd, 404, "Not found", protocol);
        return;
    }

    size = lseek(file, 0, SEEK_END);
    lseek(file, 0, SEEK_SET);

    dprintf(fd, "%s 200 OK\r\n"
                "Content-Type: %s\r\n"
                "Content-Length: %lu\r\n"
                "\r\n", protocol, magic_file(magic, path), (unsigned long)size);

    while((len = read(file, buff, sizeof buff)) > 0)
    {
        write(fd, buff, len);
    }

    close(file);
}

/* ---------------------------------------------------------------------------
   HTTP ROUTINES
   ---------------------------------------------------------------------------
*/
static void DecodeURL(char *dest, const char *url)
{
    while(*url)
    {
        char num[3]={0};

        switch(*url)
        {
            case '%':
                num[0] = url[1];
                num[1] = url[2];
                *dest++ = (char)strtol(num, NULL, 16);
                url += 3;
                break;

            case '+':
                *dest++ = ' ';
                url++;
                break;

            default:
                *dest++ = *url++;
                break;
        }
    }

    *dest = 0;
}

static int ParseRequest(char *request,
                        char **method,
                        char **path,
                        char **protocol,
                        int *close)
{
    char *tok[3] = {0};
    char *last;
    int f;

    *method = NULL;
    *path = NULL;
    *protocol = NULL;
    *close = 0;

    if ((tok[0] = strtok_r(request, " ", &last)) &&
        (tok[1] = strtok_r(NULL, " ", &last)) &&
        (tok[2] = strtok_r(NULL, " \r\n", &last)))
    {
        char *tmppath;

        *method = tok[0];
        *path = tok[1];
        *protocol = tok[2];

        /* This is slightly dodgy as hell, but should work
        */
        *close = (strstr(*protocol + strlen(*protocol) + 3,
                    "Connection: close\r\n") ||
                  strcmp(*protocol, "HTTP/1.0") == 0);

        tmppath = Strdup(*path);
        DecodeURL(*path, tmppath);
        free(tmppath);

        return 1;
    }

    return 0;
}


/* ---------------------------------------------------------------------------
   THREAD CODE
   ---------------------------------------------------------------------------
*/
static void *ThreadCode(void *p)
{
    char buff[4096];
    char *method = NULL;
    char *path = NULL;
    char *protocol = NULL;
    int close_connection = 0;
    thread_arg_t *args = p;

    while (Read(args->fd, buff, sizeof buff) && !close_connection)
    {
        if (ParseRequest(buff, &method, &path,
                            &protocol, &close_connection))
        {
            if (strcmp(method, "GET") == 0)
            {
                printf("%s %s %s\n", method, path, protocol);

                path = basename(path);

                if (strcmp(path, "/") == 0)
                {
                    printf("Generate listing for /\n");
                    GenerateListing(args->fd, protocol);
                }
                else if (access(path, R_OK) == 0)
                {
                    printf("Generating download for %s\n", path);
                    GenerateFile(args->fd, args->magic, path, protocol);
                }
                else
                {
                    printf("File not found - returning 404\n");
                    GenerateStatus(args->fd, 404, "Not found", protocol);
                }
            }
            else
            {
                printf("Method %s not supported\n", method);
                GenerateStatus(args->fd, 501, "Not implemented", protocol);
            }
        }
        else
        {
            printf("Bad request %s\n", method);
        }
    }

    printf("Connection closed on %d\n", args->fd);
    close(args->fd);

    return NULL;
}


/* ---------------------------------------------------------------------------
   MAIN
   ---------------------------------------------------------------------------
*/
int main(int argc, char *argv[])
{
    magic_t magic;
    int port = 80;
    int opt;
    int sock;
    const char *dir=".";

    while((opt = getopt(argc, argv, "p:d:")) != -1)
    {
        switch(opt)
        {
            case 'p':
                port = atoi(optarg);
                break;

            case 'd':
                dir = optarg;
                break;

            default:
                fprintf(stderr, "%s: usage %s [-p port] [-d dir]\n",
                            argv[0], argv[0]);
                return EXIT_FAILURE;
        }
    }

    magic = magic_open(MAGIC_MIME_TYPE);

    if (!magic)
    {
        fprintf(stderr, "%s: Failed to open magic library\n", argv[0]);
    }

    magic_load(magic, NULL);

    if ((sock = Listen(port)) == -1)
    {
        return EXIT_FAILURE;
    }

    chdir(dir);

    printf("Serving up %s on %d\n", dir, port);

    while(1)
    {
        pthread_t thread;
        thread_arg_t *thread_args;
        int fd;

        if ((fd = accept(sock, NULL, 0)) == -1)
        {
            perror("accept");
            close(sock);
            return EXIT_FAILURE;
        }

        printf("Accepted connection on %d\n", fd);

        thread_args = Malloc(sizeof *thread_args);
        thread_args->fd = fd;
        thread_args->magic = magic;

        pthread_create(&thread, NULL, ThreadCode, thread_args);
        pthread_detach(thread);
    }

    return EXIT_SUCCESS;
}

/*
vim: ai sw=4 ts=8 expandtab
*/