about summary refs log tree commit diff
path: root/requestHandler.c
blob: 94e0aecb2b30d4259f3ee4605efd29f4de81a79b (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
#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 const char *ok               = "HTTP/1.0 200 OK\r\n";
static const char *notfound         = "HTTP/1.0 404 Not found\r\n";
static const char *forbidden        = "HTTP/1.0 403 Forbidden\r\n";
static const char *intserverror     = "HTTP/1.0 500 Internal server error\r\n";
static const char *not_implemented  = "HTTP/1.0 501 Not implemented\r\n";

static const char *content_length   = "Content-Length: %d\r\n";
static const char *text_plain       = "Content-Type: text/plain\r\n";
static const char *image_gif        = "Content-Type: image/gif\r\n";

static const char *end              = "\r\n";

static const char *root = "\
CPTC (https://github.com/nakidai/cptc)\n\
--------------------------------------\n\
PetTheCord but rewritten in the C cuz some people was annoying me about that a\n\
lot.\n\
\n\
Paths:\n\
/           Show this text\n\
/<UID>.*    Return a gif that pets given UID\n";

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 buffer[512], filenamebuf[64];
    ssize_t received = recv(fd, buffer, sizeof(buffer), 0);
    FILE *fp;

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

    method = buffer[0];
    if (method != CPTC_GET && method != CPTC_HEAD)
    {
        send(fd, not_implemented, strlen(not_implemented), 0);
        return;
    }

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

    if (strlen(path) == 1)
    {
        char *length = malloc(sizeof(*length) * 32);
        snprintf(length, 32, content_length, strlen(root));
        send(fd, ok, strlen(ok), 0);
        send(fd, text_plain, strlen(text_plain), 0);
        send(fd, length, strlen(length), 0);
        send(fd, end, strlen(end), 0);
        send(fd, root, strlen(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)
        {
            send(fd, forbidden, strlen(forbidden), 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()");
            send(fd, intserverror, strlen(intserverror), 0);
            free(in);
            free(length);
            return;
        }
        fseek(fp, 0, SEEK_END);
        snprintf(length, 32, content_length, ftell(fp));
        fseek(fp, 0, SEEK_SET);

        send(fd, ok, strlen(ok), 0);
        send(fd, image_gif, strlen(image_gif), 0);
        send(fd, length, strlen(length), 0);
        send(fd, end, strlen(end), 0);
        while ((ch = getc(fp)) >= 0)
        {
            chbuf = ch;
            send(fd, &chbuf, 1, 0);
        }

        fclose(fp);
        free(in);
        free(length);
    }

    send(fd, notfound, strlen(notfound), 0);
}