summaryrefslogtreecommitdiff
path: root/gopher.c
blob: c1cb2ebf77caa3f0f3187e9e85197f4fbe40b3e7 (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
#define _POSIX_C_SOURCE 200112L

#include <sys/socket.h>

#include <stdio.h>
#include <string.h>

#include "log.h"

static char *res = "1Entry 1	/entry1	localhost	3000\r\n\
1Entry 2	/entry2	localhost	3000\r\n\
1Entry 3	/entry3	localhost	3000\r\n\
.";

static char *res1 = "0SubEntry 1	/entry1	localhost	3000\r\n\
0SubEntry 2	/entry2	localhost	3000\r\n\
0SubEntry 3	/entry3	localhost	3000\r\n\
.";

static int 
sendall(int fd, const char *buf, size_t len)
{
	size_t sent = 0;
	ssize_t n;

	while (sent < len) {
		n = send(fd, buf + sent, len - sent, 0);
		if (n == -1) {
			perror("send");
			return -1;
		}
		sent += n;
	}

	return 0;
}

int 
handle_path(int fd, const char *path)
{
	const char *data;

	if (strcmp(path, "") == 0 || strcmp(path, "/") == 0) {
		data = res;
	} else if (strcmp(path, "/entry1") == 0) {
		data = res1;
	} else {
		data = "";
	}

	if (sendall(fd, data, strlen(data)) == -1) {
		logmsg(LOG_INFO, "failed to send data");
		return -1;
	}
	return 0;
}