summaryrefslogtreecommitdiffstats
path: root/src/core.h
blob: 76cffa280ca5cb75fc20f2cb9fc618434f421197 (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
#ifndef _TIP_CORE_H
#define _TIP_CORE_H

#include <ev.h>
#include "list.h"
#include <stdbool.h>
#include <netinet/in.h>

#define TIP_MSG_REQUEST_MAXLEN	 131072

extern const char *root;
#define DEFAULT_MAX_CLIENTS 3
extern int max_clients;
extern int num_clients;
extern bool redirect;
/* max_client logic only applies for files larger than 1024 bytes. */
#define FILE_SIZE_THRESHOLD 1024ULL

enum tip_client_state {
	TIP_CLIENT_PENDING		= 0,
	TIP_CLIENT_RECEIVING_HEADER,
	TIP_CLIENT_RECEIVING_PAYLOAD,
	TIP_CLIENT_PROCESSING_REQUEST,
	TIP_CLIENT_PROCESSING_REQUEST_2,
	TIP_CLIENT_PROCESSING_REQUEST_3,
	TIP_CLIENT_CLOSE_WAIT,
};

struct tip_client {
	struct list_head	list;
	struct ev_io		io;
	struct ev_timer		timer;
	struct sockaddr_in	addr;
	enum tip_client_state	state;
	char			buf[TIP_MSG_REQUEST_MAXLEN];
	unsigned int		buf_len;
	unsigned int		msg_len;
	int			content_length;
	char			auth_token[64];

	/* for file serving. */
	const char		*uri;
	const char		*path;
	off_t			size;
	int			fd;
	off_t			offset;

	/* for redirection. */
	bool			redirect;
	struct sockaddr_in	redirect_addr;
	bool			allow_redirect;
};

static inline int tip_client_socket(const struct tip_client *cli)
{
	return cli->io.fd;
}

static inline bool tip_client_large_file(const struct tip_client *cli)
{
	return cli->size > FILE_SIZE_THRESHOLD;
}

void tip_client_pending(struct tip_client *cli);
bool tip_client_redirect(struct tip_client *cli);

extern struct ev_loop *tip_main_loop;

int tip_socket_server_init(const char *port);
void tip_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events);

int tip_client_state_process_payload(struct tip_client *cli);
int tip_client_state_process_payload_reply(struct tip_client *cli);
int tip_client_state_process_payload_bulk(struct tip_client *cli);

enum tip_http_method {
	TIP_METHOD_GET	= 0,
	TIP_METHOD_POST,
	TIP_METHOD_NO_HTTP
};

struct tip_client_redirect {
	struct list_head	list;
	struct sockaddr_in	addr;
	const char		*uri;
	struct ev_timer		timer;
};

#endif