refactor, metastuff
[detour.git] / pulser.c
1 #include "common.c"
2
3 void handle_connection(int s) {
4   dup2(s, 1);
5   dup2(s, 0);
6
7   char line_in[100];
8   if (!fgets(line_in, 100, stdin)) return;
9   char *p = line_in + strlen("GET /");
10
11   setbuf(stdout, NULL);
12   printf("HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<!--");
13
14   char *spaaaace = malloc(1024*64);
15   memset(spaaaace, ' ', 1024*64);
16   spaaaace[0] = 'A';
17
18   time_t t = round_up(real_seconds(), 4);
19   while (1) {
20     if (*p != '0' && *p != '1') return;
21     if (*p == '1') t+=2;
22     sleep_until(t);
23     if (fwrite(spaaaace, 1024, 64, stdout) <= 0) return;
24     t += (*p == '1') ? 2 : 4;
25     p++;
26   }
27 }
28
29 int main(void) {
30   int s = netopen_server(NULL, "4422", JH_TCP_HINTS);
31   while (1) {
32     int s_ = accept(s, NULL, NULL);
33     pid_t p = fork();
34     if (p == 0) {
35       handle_connection(s_);
36       return 0;
37     }
38     close(s_);
39   }
40 }