public
Fork of ry/ebb
Description: web server
Homepage: http://ebb.rubyforge.org
Clone URL: git://github.com/gnosek/ebb.git
Add HTTP_CONNECTION to parser in prep for Keep-Alive feature
Ryan Dahl (author)
Sun Mar 23 09:25:38 -0700 2008
commit  36baa35d6b295fda668249dfeb0923e4a8108675
tree    f38652acb0c798770b0889d55dbc1702d3dd1cc5
parent  d478f914fae8664eadf84f7a2fe438ab0dc99d62
...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
...
287
288
289
290
 
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
 
310
311
312
313
314
315
 
316
317
318
 
 
319
320
321
...
340
341
342
343
 
344
345
346
347
 
 
 
 
 
 
 
348
349
 
350
351
352
...
384
385
386
387
 
 
 
 
 
 
 
 
 
388
389
390
...
407
408
409
 
410
411
412
...
80
81
82
 
83
84
 
 
 
 
 
 
 
 
85
86
87
...
278
279
280
 
281
282
283
 
 
 
 
 
284
285
 
 
 
 
 
 
 
 
 
 
286
287
288
289
290
291
 
292
293
 
 
294
295
296
297
298
...
317
318
319
 
320
321
322
323
 
324
325
326
327
328
329
330
331
332
333
334
335
336
...
368
369
370
 
371
372
373
374
375
376
377
378
379
380
381
382
...
399
400
401
402
403
404
405
0
@@ -80,17 +80,8 @@ void on_element(void *data, int type, const char *at, size_t length)
0
 static void dispatch(ebb_client *client)
0
 {
0
   ebb_server *server = client->server;
0
-
0
   if(client->open == FALSE)
0
     return;
0
-
0
- /* Set the env variables */
0
- if(server->port) {
0
- env_add_const(client, MONGREL_SERVER_PORT
0
- , server->port
0
- , strlen(server->port)
0
- );
0
- }
0
   client->in_use = TRUE;
0
   server->request_cb(client, server->request_cb_data);
0
 }
0
@@ -287,35 +278,21 @@ error:
0
 }
0
 
0
 
0
-static client_init(ebb_server *server, ebb_client *client)
0
+static client_init(ebb_client *client, int fd)
0
 {
0
   assert(client->in_use == FALSE);
0
-#ifdef DEBUG
0
- /* does ragel fuck up if request buffer isn't null? */
0
- for(i=0; i< EBB_BUFFERSIZE; i++)
0
- client->request_buffer[i] = 'A';
0
-#endif
0
   
0
   client->open = TRUE;
0
- client->server = server;
0
-
0
- /* DO SOCKET STUFF */
0
- socklen_t len;
0
- client->fd = accept(server->fd, (struct sockaddr*)&(server->sockaddr), &len);
0
- if(client->fd < 0) {
0
- perror("accept()");
0
- client->open = FALSE;
0
- return;
0
- }
0
+ client->fd = fd;
0
   
0
   int flags = fcntl(client->fd, F_GETFL, 0);
0
   assert(0 <= fcntl(client->fd, F_SETFL, flags | O_NONBLOCK));
0
   
0
   /* INITIALIZE http_parser */
0
- http_parser_init(&(client->parser));
0
+ http_parser_init(&client->parser);
0
   client->parser.data = client;
0
- client->parser.http_field = http_field_cb;
0
- client->parser.on_element = on_element;
0
+ client->parser.http_field = http_field_cb;
0
+ client->parser.on_element = on_element;
0
   
0
   /* OTHER */
0
   client->env_size = 0;
0
@@ -340,13 +317,20 @@ static client_init(ebb_server *server, ebb_client *client)
0
   client->read_watcher.data = client;
0
   ev_init(&client->read_watcher, on_client_readable);
0
   ev_io_set(&client->read_watcher, client->fd, EV_READ | EV_ERROR);
0
- ev_io_start(server->loop, &client->read_watcher);
0
+ ev_io_start(client->server->loop, &client->read_watcher);
0
   
0
   client->timeout_watcher.data = client;
0
   ev_timer_init(&client->timeout_watcher, on_timeout, EBB_TIMEOUT, EBB_TIMEOUT);
0
- ev_timer_start(server->loop, &client->timeout_watcher);
0
+ ev_timer_start(client->server->loop, &client->timeout_watcher);
0
+}
0
+
0
+
0
+static client_reinit(ebb_client *client)
0
+{
0
+ client_init(client, client->fd);
0
 }
0
 
0
+
0
 static void on_request(struct ev_loop *loop, ev_io *watcher, int revents)
0
 {
0
   ebb_server *server = (ebb_server*)(watcher->data);
0
@@ -384,7 +368,15 @@ static void on_request(struct ev_loop *loop, ev_io *watcher, int revents)
0
   g_debug("%d open connections", count);
0
 #endif
0
   
0
- client_init(server, client);
0
+ /* DO SOCKET STUFF */
0
+ socklen_t len;
0
+ int client_fd = accept(server->fd, (struct sockaddr*)&(server->sockaddr), &len);
0
+ if(client_fd < 0) {
0
+ perror("accept()");
0
+ return;
0
+ }
0
+
0
+ client_init(client, client_fd);
0
 }
0
 
0
 
0
@@ -407,6 +399,7 @@ void ebb_server_init( ebb_server *server
0
     server->clients[i].response_buffer = g_string_new("");
0
     server->clients[i].open = FALSE;
0
     server->clients[i].in_use = FALSE;
0
+ server->clients[i].server = server;
0
   }
0
   
0
   server->request_cb = request_cb;
...
11
12
13
14
15
16
17
18
 
19
20
21
 
 
 
22
23
 
 
24
25
26
27
 
 
 
28
29
30
...
134
135
136
137
138
139
140
141
142
143
144
 
145
146
 
 
 
 
 
 
147
148
149
...
163
164
165
166
 
167
168
169
170
171
172
173
174
 
175
176
177
178
 
 
 
 
 
 
179
180
181
...
245
246
247
248
249
250
251
252
 
253
254
255
 
 
 
256
257
 
 
258
259
260
 
 
261
262
263
...
11
12
13
 
 
 
14
 
15
16
 
17
18
19
20
21
 
22
23
24
25
26
 
27
28
29
30
31
32
...
136
137
138
 
 
 
 
 
 
 
139
140
141
142
143
144
145
146
147
148
149
150
151
...
165
166
167
 
168
169
170
171
172
173
174
 
 
175
176
 
 
 
177
178
179
180
181
182
183
184
185
...
249
250
251
 
 
 
252
 
253
254
 
255
256
257
258
259
 
260
261
262
263
 
264
265
266
267
268
0
@@ -11,20 +11,22 @@
0
 #include <ev.h>
0
 
0
 static VALUE cClient;
0
-static VALUE global_http_prefix;
0
-static VALUE global_request_method;
0
-static VALUE global_request_uri;
0
 static VALUE global_fragment;
0
-static VALUE global_request_path;
0
+static VALUE global_path_info;
0
 static VALUE global_query_string;
0
-static VALUE global_http_version;
0
 static VALUE global_request_body;
0
+static VALUE global_request_method;
0
+static VALUE global_request_path;
0
+static VALUE global_request_uri;
0
 static VALUE global_server_port;
0
-static VALUE global_path_info;
0
+static VALUE global_http_accept;
0
+static VALUE global_http_connection;
0
 static VALUE global_http_content_length;
0
 static VALUE global_http_content_type;
0
 static VALUE global_http_content_type;
0
-static VALUE global_http_accept;
0
+static VALUE global_http_prefix;
0
+static VALUE global_http_version;
0
+
0
 
0
 /* You don't want to run more than one server per Ruby VM. Really
0
  * I'm making this explicit by not defining a Ebb::Server class but instead
0
@@ -134,16 +136,16 @@ VALUE env_field(struct ebb_env_item *item)
0
     return f;
0
   }
0
   switch(item->type) {
0
- case MONGREL_REQUEST_METHOD: return global_request_method;
0
- case MONGREL_REQUEST_URI: return global_request_uri;
0
- case MONGREL_FRAGMENT: return global_fragment;
0
- case MONGREL_REQUEST_PATH: return global_request_path;
0
- case MONGREL_QUERY_STRING: return global_query_string;
0
- case MONGREL_HTTP_VERSION: return global_http_version;
0
- case MONGREL_SERVER_PORT: return global_server_port;
0
     case MONGREL_ACCEPT: return global_http_accept;
0
+ case MONGREL_CONNECTION: return global_http_connection;
0
     case MONGREL_CONTENT_LENGTH: return global_http_content_length;
0
     case MONGREL_CONTENT_TYPE: return global_http_content_type;
0
+ case MONGREL_FRAGMENT: return global_fragment;
0
+ case MONGREL_HTTP_VERSION: return global_http_version;
0
+ case MONGREL_QUERY_STRING: return global_query_string;
0
+ case MONGREL_REQUEST_METHOD: return global_request_method;
0
+ case MONGREL_REQUEST_PATH: return global_request_path;
0
+ case MONGREL_REQUEST_URI: return global_request_uri;
0
   }
0
   fprintf(stderr, "Unknown environ type: %d", item->type);
0
   assert(FALSE);
0
@@ -163,19 +165,21 @@ VALUE env_value(struct ebb_env_item *item)
0
 VALUE client_env(VALUE _, VALUE rb_client)
0
 {
0
   ebb_client *client;
0
- VALUE field, value, hash = rb_hash_new();
0
+ VALUE field, value, env = rb_hash_new();
0
   int i;
0
   
0
   Data_Get_Struct(rb_client, ebb_client, client);
0
   for(i=0; i < client->env_size; i++) {
0
     field = env_field(&client->env[i]);
0
     value = env_value(&client->env[i]);
0
- rb_hash_aset(hash, field, value);
0
- //printf("(%s, %s)\n", StringValuePtr(field), StringValuePtr(value));
0
+ rb_hash_aset(env, field, value);
0
   }
0
- //printf("\n\n");
0
- rb_hash_aset(hash, global_path_info, rb_hash_aref(hash, global_request_path));
0
- return hash;
0
+
0
+ if(client->server->port)
0
+ rb_hash_aset(env, global_server_port, rb_str_new2(client->server->port));
0
+
0
+ rb_hash_aset(env, global_path_info, rb_hash_aref(env, global_request_path));
0
+ return env;
0
 }
0
 
0
 
0
@@ -245,19 +249,20 @@ void Init_ebb_ext()
0
   
0
   /** Defines global strings in the init method. */
0
 #define DEF_GLOBAL(N, val) global_##N = rb_obj_freeze(rb_str_new2(val)); rb_global_variable(&global_##N)
0
- DEF_GLOBAL(http_prefix, "HTTP_");
0
- DEF_GLOBAL(request_method, "REQUEST_METHOD");
0
- DEF_GLOBAL(request_uri, "REQUEST_URI");
0
   DEF_GLOBAL(fragment, "FRAGMENT");
0
- DEF_GLOBAL(request_path, "REQUEST_PATH");
0
+ DEF_GLOBAL(path_info, "PATH_INFO");
0
   DEF_GLOBAL(query_string, "QUERY_STRING");
0
- DEF_GLOBAL(http_version, "HTTP_VERSION");
0
   DEF_GLOBAL(request_body, "REQUEST_BODY");
0
+ DEF_GLOBAL(request_method, "REQUEST_METHOD");
0
+ DEF_GLOBAL(request_path, "REQUEST_PATH");
0
+ DEF_GLOBAL(request_uri, "REQUEST_URI");
0
   DEF_GLOBAL(server_port, "SERVER_PORT");
0
- DEF_GLOBAL(path_info, "PATH_INFO");
0
+ DEF_GLOBAL(http_accept, "HTTP_ACCEPT");
0
+ DEF_GLOBAL(http_connection, "HTTP_CONNECTION");
0
   DEF_GLOBAL(http_content_length, "HTTP_CONTENT_LENGTH");
0
   DEF_GLOBAL(http_content_type, "HTTP_CONTENT_TYPE");
0
- DEF_GLOBAL(http_accept, "HTTP_ACCEPT");
0
+ DEF_GLOBAL(http_prefix, "HTTP_");
0
+ DEF_GLOBAL(http_version, "HTTP_VERSION");
0
   
0
   rb_define_singleton_method(mFFI, "server_process_connections", server_process_connections, 0);
0
   rb_define_singleton_method(mFFI, "server_listen_on_port", server_listen_on_port, 1);
...
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
...
13
14
15
 
 
 
 
 
 
16
17
18
19
 
 
 
20
21
22
23
24
25
26
27
28
29
30
31
 
 
32
33
34
0
@@ -13,25 +13,22 @@
0
 #endif
0
 
0
 
0
-enum { MONGREL_REQUEST_METHOD
0
- , MONGREL_REQUEST_URI
0
- , MONGREL_FRAGMENT
0
- , MONGREL_REQUEST_PATH
0
- , MONGREL_QUERY_STRING
0
- , MONGREL_HTTP_VERSION
0
+enum { MONGREL_ACCEPT
0
+ , MONGREL_CONNECTION
0
      , MONGREL_CONTENT_LENGTH
0
      , MONGREL_CONTENT_TYPE
0
- , MONGREL_ACCEPT
0
- /* below - not used in the parser but often used by users of parser */
0
- , MONGREL_SERVER_PORT
0
+ , MONGREL_FRAGMENT
0
+ , MONGREL_HTTP_VERSION
0
+ , MONGREL_QUERY_STRING
0
+ , MONGREL_REQUEST_PATH
0
+ , MONGREL_REQUEST_METHOD
0
+ , MONGREL_REQUEST_URI
0
      };
0
 
0
 typedef void (*element_cb)(void *data, const char *at, size_t length);
0
 typedef void (*field_cb)(void *data, const char *field, size_t flen, const char *value, size_t vlen);
0
 typedef void (*new_element_cb)(void *data, int type, const char *at, size_t length);
0
 
0
-
0
-
0
 typedef struct http_parser {
0
   int cs;
0
   int overflow_error;
...
42
43
44
45
 
 
 
 
46
47
48
...
149
150
151
 
152
153
154
...
42
43
44
 
45
46
47
48
49
50
51
...
152
153
154
155
156
157
158
0
@@ -42,7 +42,10 @@
0
     if(LEN(mark, fpc) > 1024) { parser->overflow_error = TRUE; fbreak; }
0
     parser->on_element(parser->data, MONGREL_ACCEPT, PTR_TO(mark), LEN(mark, fpc));
0
   }
0
-
0
+ action http_connection {
0
+ if(LEN(mark, fpc) > 1024) { parser->overflow_error = TRUE; fbreak; }
0
+ parser->on_element(parser->data, MONGREL_CONNECTION, PTR_TO(mark), LEN(mark, fpc));
0
+ }
0
   action http_content_length {
0
     if(LEN(mark, fpc) > 20) { parser->overflow_error = TRUE; fbreak; }
0
     set_content_length(parser, PTR_TO(mark), LEN(mark, fpc));
0
@@ -149,6 +152,7 @@
0
   field_value = any* >start_value %write_value;
0
   
0
   known_header = ( ("Accept:"i " "* (any* >mark %http_accept))
0
+ | ("Connection:"i " "* (any* >mark %http_connection))
0
                  | ("Content-Length:"i " "* (digit+ >mark %http_content_length))
0
                  | ("Content-Type:"i " "* (any* >mark %http_content_type))
0
                  ) :> CRLF;

Comments

    No one has commented yet.