about summary refs log tree commit diff
path: root/requestHandler.c
blob: 8e1dd18932de56ffd0789f0571ac176a7789d843 (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
#define _POSIX_C_SOURCE 200112L

#include "cptc.h"

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <sys/socket.h>
#include <sys/types.h>

#include <cpetpet.h>


static bool isnumber(const char *s)
{
    for (const char *cp = s; *cp != '\0' && *cp != '.'; ++cp)
        if (!isdigit(*cp))
            return false;
    return true;
}

static char *generate_filename(char *buf, size_t size, const char *ext)
{
    struct timespec tp;
    clock_gettime(CLOCK_REALTIME, &tp);
    snprintf(buf, size, "/tmp/%lld.%s", tp.tv_sec * 1000000000LL + tp.tv_nsec, ext);
    return buf;
}

void CPTC_requestHandler(int fd)
{
    enum CPTC_Method method;
    int ch;
    char *path, chbuf;
    char request[512];
    char filenamebuf[64];
    char response[512], *responseadd = response;
    ssize_t received = recv(fd, request, sizeof(request), 0);
    FILE *fp;

    memset(response, 0, sizeof(response));

    if (received == -1)
    {
        perror("recv()");
        return;
    }

    method = request[0];
    if (method != CPTC_GET && method != CPTC_HEAD)
    {
        strcpy(response, "HTTP/1.0 501 Not implemented\r\n");
        strcat(response, "\r\n");
        send(fd, response, strlen(response), 0);
        return;
    }

    path = strchr(request, ' ') + 1;
    *strchr(path, ' ') = '\0';
    puts(path);

    if (strlen(path) == 1)
    {
        char *length = malloc(sizeof(*length) * 32);
        snprintf(length, 32, "Content-Length: %ld\r\n", strlen(CPTC_root));
        strcpy(response, "HTTP/1.0 200 OK\r\n");
        strcat(response, "Content-Type: text/plain\r\n");
        strcat(response, length);
        strcat(response, "\r\n");
        send(fd, response, strlen(response), 0);
        send(fd, CPTC_root, strlen(CPTC_root), 0);
        free(length);
        return;
    }

    if (strchr(path, '.') && isnumber(path + 1))
    {
        char *length = malloc(sizeof(*length) * 32);

        *strchr(path, '.') = '\0';
        generate_filename(filenamebuf, sizeof(filenamebuf), "png");
        char *in = CPTC_downloadAvatar(atoll(path + 1), filenamebuf);
        if (!in)
        {
            strcpy(response, "HTTP/1.0 403 Forbidden\r\n");
            strcat(response, "\r\n");
            send(fd, response, strlen(response), 0);
            return;
        }
        generate_filename(filenamebuf, sizeof(filenamebuf), "gif");
        CPetPet(in, filenamebuf, 2);
        printf("From %s to %s\n", in, filenamebuf);

        fp = fopen(filenamebuf, "rb");
        if (!fp)
        {
            perror("fopen()");
            strcpy(response, "HTTP/1.0 500 Internal server error\r\n");
            strcat(response, "\r\n");
            send(fd, response, strlen(response), 0);
            free(in);
            free(length);
            return;
        }
        fseek(fp, 0, SEEK_END);
        snprintf(length, 32, "Content-Lnegth: %ld\r\n", ftell(fp));
        fseek(fp, 0, SEEK_SET);

        strcpy(response, "HTTP/1.0 200 OK\r\n");
        strcat(response, "Content-Type: image/gif\r\n");
        strcat(response, length);
        strcat(response, "\r\n");
        send(fd, response, strlen(response), 0);
        responseadd = response;
        while ((ch = getc(fp)) >= 0)
        {
            *responseadd++ = ch;
            if (responseadd == response + sizeof(response))
            {
                if (send(fd, response, sizeof(response), 0) < 0)
                {
                    perror("send()");
                    goto err_send;
                }
                responseadd = response;
            }
        }
        send(fd, response, responseadd - response, 0);

err_send:
        fclose(fp);

        remove(in);
        remove(filenamebuf);

        free(in);
        free(length);
        return;
    }

    strcpy(response, "HTTP/1.0 404 Not found");
    strcat(response, "\r\n");
    send(fd, response, strlen(response), 0);
}