add notice about where devurandom.c is from
[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   if (!strncmp(line_in, "GET /", strlen("GET /"))) return;
10   char *p = line_in + strlen("GET /");
11
12   setbuf(stdout, NULL);
13   printf("HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<!--");
14
15   char *spaaaace = malloc(1024*64);
16   memset(spaaaace, ' ', 1024*64);
17   spaaaace[0] = 'A';
18
19   time_t t = round_up(real_seconds(), 4);
20   while (1) {
21     if (*p != '0' && *p != '1') return;
22     if (*p == '1') t+=2;
23     sleep_until(t);
24     if (fwrite(spaaaace, 1024, 64, stdout) <= 0) return;
25     t += (*p == '1') ? 2 : 4;
26     p++;
27   }
28 }
29
30 int main(void) {
31   int s = netopen_server(NULL, "4422", JH_TCP_HINTS);
32   while (1) {
33     int s_ = accept(s, NULL, NULL);
34     pid_t p = fork();
35     if (p == 0) {
36       handle_connection(s_);
37       return 0;
38     }
39     close(s_);
40   }
41 }