File reorganization
[ebb.git] / src / ebb.h
blob4ce56dc3cab5a76c43da4f2e4d5ed8fd85178281
1 /* Ebb Web Server
2 * Copyright (c) 2007 Ry Dahl <ry.d4hl@gmail.com>
3 * This software is released under the "MIT License". See README file for details.
4 */
5 #ifndef ebb_h
6 #define ebb_h
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <glib.h>
12 #define EV_STANDALONE 1
13 #include "ev.h"
15 #include "parser.h"
18 typedef struct ebb_server ebb_server;
19 typedef struct ebb_client ebb_client;
21 #define EBB_LOG_DOMAIN "Ebb"
22 #define ebb_error(str, ...) \
23 g_log(EBB_LOG_DOMAIN, G_LOG_LEVEL_ERROR, str, ## __VA_ARGS__);
24 #define ebb_warning(str, ...) \
25 g_log(EBB_LOG_DOMAIN, G_LOG_LEVEL_WARNING, str, ## __VA_ARGS__);
26 #define ebb_info(str, ...) \
27 g_log(EBB_LOG_DOMAIN, G_LOG_LEVEL_INFO, str, ## __VA_ARGS__);
28 #define ebb_debug(str, ...) \
29 g_log(EBB_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, str, ## __VA_ARGS__);
31 #define EBB_BUFFERSIZE (16*1024)
32 #define EBB_MAX_CLIENTS 200
33 #define EBB_TIMEOUT 30.0
34 #define EBB_MAX_ENV 100
35 #define EBB_TCP_COMMON \
36 unsigned open : 1; \
37 int fd; \
38 struct sockaddr_in sockaddr;
40 /*** Ebb Client ***/
42 typedef void (*ebb_client_cb)(ebb_client*);
44 void ebb_client_close(ebb_client*);
45 size_t ebb_client_read(ebb_client*, char *buffer, int length);
46 void ebb_client_write(ebb_client*, const char *data, int length);
47 void ebb_client_start_writing( ebb_client *client
48 , ebb_client_cb after_write_cb
50 /* User must free the GString returned from ebb_client_read_input */
51 GString* ebb_client_read_input(ebb_client *client, ssize_t size);
53 enum { EBB_REQUEST_METHOD
54 , EBB_REQUEST_URI
55 , EBB_FRAGMENT
56 , EBB_REQUEST_PATH
57 , EBB_QUERY_STRING
58 , EBB_HTTP_VERSION
59 , EBB_SERVER_NAME
60 , EBB_SERVER_PORT
61 , EBB_CONTENT_LENGTH
64 struct ebb_client {
65 EBB_TCP_COMMON
67 ebb_server *server;
68 http_parser parser;
70 char read_buffer[EBB_BUFFERSIZE];
71 size_t read;
72 ev_io read_watcher;
74 ev_io write_watcher;
75 GString *write_buffer;
76 size_t written;
77 ebb_client_cb after_write_cb;
79 void *data;
81 char upload_file_filename[200];
82 FILE *upload_file;
83 int content_length;
85 char *request_body;
86 int nread_from_body;
88 ev_timer timeout_watcher;
90 /* the ENV structure */
91 int env_size;
92 const char *env_fields[EBB_MAX_ENV];
93 int env_field_lengths[EBB_MAX_ENV];
94 const char *env_values[EBB_MAX_ENV];
95 int env_value_lengths[EBB_MAX_ENV];
98 /*** Ebb Server ***/
100 typedef void (*ebb_request_cb)(ebb_client*, void*);
102 ebb_server* ebb_server_alloc();
103 void ebb_server_init( ebb_server *server
104 , struct ev_loop *loop
105 , char *address
106 , int port
107 , ebb_request_cb request_cb
108 , void *request_cb_data
110 void ebb_server_free(ebb_server*);
111 void ebb_server_start(ebb_server*);
112 void ebb_server_stop(ebb_server*);
114 struct ebb_server {
115 EBB_TCP_COMMON
116 struct hostent *dns_info;
117 char *port;
118 char *address;
120 void *data;
122 void *request_cb_data;
123 ebb_request_cb request_cb;
125 ev_io *request_watcher;
126 struct ev_loop *loop;
128 ebb_client clients[EBB_MAX_CLIENTS];
131 #endif ebb_h