/games/xmemory/

='sp2013' href='/~lantw44/cgit/sp2013/'>sp2013
Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogblamecommitdiffstats
path: root/hw4/cgiprog/file_reader.c
blob: a6f958fd3c07990986e0501774f8f0fc65ad28d3 (plain) (tree)
1
2
3
4
5
6
7
8
9
                               





                   




























































                                                                                                        
#define _POSIX_C_SOURCE 200809L
#define NL "\015\012"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main (int argc, char* argv[]) {
    char* qs = getenv ("QUERY_STRING");
    if (qs == NULL || *qs == '\0') {
        fputs (
            "HTTP/1.1 400 Bad Request" NL
            "Content-Type: text/html" NL NL
            "<html><head><title>400 Bad Request</title></head>" NL
            "<body><p>Query string is missing</p>"
            "</body></html>" NL, stdout);
        return 1;
    }

    if (strncmp (qs, "filename=", 9)) {
        fputs (
            "HTTP/1.1 400 Bad Request" NL
            "Content-Type: text/html" NL NL
            "<html><head><title>400 Bad Request</title></head>" NL
            "<body><p>Query string is not valid</p>"
            "</body></html>" NL, stdout);
        return 1;
    }

    char* fn = qs + 9;
    int fd = open (fn, O_RDONLY);
    if (fd < 0) {
        switch (errno) {
            case ENOENT:
                printf (