-
Notifications
You must be signed in to change notification settings - Fork 58
/
ebb.h
130 lines (106 loc) · 3.98 KB
/
ebb.h
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
/* libebb web server library
* Copyright 2008 ryah dahl, ry at tiny clouds punkt org
*
* This software may be distributed under the "MIT" license included in the
* README
*/
#ifndef EBB_H
#define EBB_H
/* remove this if you want to embed libebb without GNUTLS */
//#define HAVE_GNUTLS 1
#include <sys/socket.h>
#include <netinet/in.h>
#include <ev.h>
#ifdef HAVE_GNUTLS
# include <gnutls/gnutls.h>
# include "rbtree.h" /* for ebb_server.session_cache */
#endif
#include "ebb_request_parser.h"
#define EBB_MAX_CONNECTIONS 1024
#define EBB_DEFAULT_TIMEOUT 30.0
#define EBB_AGAIN 0
#define EBB_STOP 1
typedef struct ebb_buf ebb_buf;
typedef struct ebb_server ebb_server;
typedef struct ebb_connection ebb_connection;
typedef void (*ebb_after_write_cb) (ebb_connection *connection);
typedef void (*ebb_connection_cb)(ebb_connection *connection, void *data);
struct ebb_buf {
size_t written; /* private */
/* public */
char *base;
size_t len;
void (*on_release)(ebb_buf*);
void *data;
};
struct ebb_server {
int fd; /* ro */
struct sockaddr_in sockaddr; /* ro */
socklen_t socklen; /* ro */
char port[6]; /* ro */
struct ev_loop *loop; /* ro */
unsigned listening:1; /* ro */
unsigned secure:1; /* ro */
#ifdef HAVE_GNUTLS
gnutls_certificate_credentials_t credentials; /* private */
struct rbtree_t session_cache; /* private */
#endif
ev_io connection_watcher; /* private */
/* Public */
/* Allocates and initializes an ebb_connection. NULL by default. */
ebb_connection* (*new_connection) (ebb_server*, struct sockaddr_in*);
void *data;
};
struct ebb_connection {
int fd; /* ro */
struct sockaddr_in sockaddr; /* ro */
socklen_t socklen; /* ro */
ebb_server *server; /* ro */
char *ip; /* ro */
unsigned open:1; /* ro */
const char *to_write; /* ro */
size_t to_write_len; /* ro */
size_t written; /* ro */
ebb_after_write_cb after_write_cb; /* ro */
ebb_request_parser parser; /* private */
ev_io write_watcher; /* private */
ev_io read_watcher; /* private */
ev_timer timeout_watcher; /* private */
ev_timer goodbye_watcher; /* private */
#ifdef HAVE_GNUTLS
ev_io handshake_watcher; /* private */
gnutls_session_t session; /* private */
ev_io goodbye_tls_watcher; /* private */
#endif
/* Public */
ebb_request* (*new_request) (ebb_connection*);
/* The new_buf callback allocates and initializes an ebb_buf structure.
* By default this is set to a simple malloc() based callback which always
* returns 4 kilobyte bufs. Write over it with your own to use your own
* custom allocation
*
* new_buf is called each time there is data from a client connection to
* be read. See on_readable() in server.c to see exactly how this is used.
*/
ebb_buf* (*new_buf) (ebb_connection*);
/* Returns EBB_STOP or EBB_AGAIN
* NULL by default.
*/
int (*on_timeout) (ebb_connection*);
/* The connection was closed */
void (*on_close) (ebb_connection*);
void *data;
};
void ebb_server_init (ebb_server *server, struct ev_loop *loop);
#ifdef HAVE_GNUTLS
int ebb_server_set_secure (ebb_server *server, const char *cert_file,
const char *key_file);
#endif
int ebb_server_listen_on_port (ebb_server *server, const int port);
int ebb_server_listen_on_fd (ebb_server *server, const int sfd);
void ebb_server_unlisten (ebb_server *server);
void ebb_connection_init (ebb_connection *connection);
void ebb_connection_schedule_close (ebb_connection *);
void ebb_connection_reset_timeout (ebb_connection *connection);
int ebb_connection_write (ebb_connection *connection, const char *buf, size_t len, ebb_after_write_cb);
#endif