ry / ebb fork watch download tarball
public this repo is viewable by everyone
Description: web server
Homepage: http://ebb.rubyforge.org
Clone URL: git://github.com/ry/ebb.git
python-wsgi binding is working (to some extent)

it doesn't yet implement the full WSGI protocol but does handle the 
basics.
Probably does not work with threads but no matter because ebb does not 
support
response streaming yet anyway.
Ryan Dahl (author)
2 months ago
commit  9b05bbd9644cc6bbdd4f845196a715f21d91fbb2
tree    1c8e228362031077337ea044e521b56df8befb59
parent  98ceeb9fbacb61dd7dc61847d4c88f97420cddef
...
61
62
63
 
64
65
66
...
197
198
199
200
201
202
203
204
...
286
287
288
 
289
290
291
...
61
62
63
64
65
66
67
...
198
199
200
 
 
201
202
203
...
285
286
287
288
289
290
291
0
@@ -61,6 +61,7 @@ void env_add_const(ebb_client *client, int type, const char *value, int vlen)
0
   client->env_size += 1;
0
 }
0
 
0
+
0
 void http_field_cb(void *data, const char *field, size_t flen, const char *value, size_t vlen)
0
 {
0
   ebb_client *client = (ebb_client*)(data);
0
@@ -197,8 +198,6 @@ void* read_body_into_file(void *_client)
0
     written += r;
0
   }
0
   
0
- // g_debug("wrote request header to file. written: %d, content_length: %d", written, client->content_length);
0
-
0
   int bufsize = 5*1024;
0
   char buffer[bufsize];
0
   size_t received;
0
@@ -286,6 +285,7 @@ error:
0
   ebb_client_close(client);
0
 }
0
 
0
+
0
 void on_request(struct ev_loop *loop, ev_io *watcher, int revents)
0
 {
0
   ebb_server *server = (ebb_server*)(watcher->data);
...
15
16
17
18
19
20
 
 
 
21
22
23
...
39
40
41
42
 
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
45
46
...
84
85
86
87
 
88
89
90
...
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
...
155
156
157
158
159
160
161
162
 
 
 
 
163
164
165
166
167
168
169
170
171
 
172
173
174
175
176
177
...
183
184
185
 
 
 
 
 
 
 
186
187
188
...
198
199
200
 
201
202
...
15
16
17
 
 
 
18
19
20
21
22
23
...
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
...
134
135
136
 
137
138
139
140
...
149
150
151
 
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
 
 
 
 
168
 
169
170
171
172
173
174
175
176
 
 
177
178
179
180
181
182
183
184
185
186
 
187
188
189
190
191
192
193
194
195
196
197
198
199
 
200
201
 
202
203
204
205
...
235
236
237
 
 
 
 
 
238
239
240
241
242
243
244
245
246
 
 
 
 
247
248
 
 
249
250
251
...
257
258
259
260
261
262
263
264
265
266
267
268
269
...
279
280
281
282
283
284
0
@@ -15,9 +15,9 @@
0
  * one ebb_server per python VM instance.
0
  */
0
 static ebb_server server;
0
-struct ev_loop *loop = NULL;
0
-static PyObject *application = NULL;
0
-static PyObject *base_env = NULL;
0
+struct ev_loop *loop;
0
+static PyObject *application;
0
+static PyObject *base_env;
0
 static PyObject *global_http_prefix;
0
 static PyObject *global_request_method;
0
 static PyObject *global_request_uri;
0
@@ -39,8 +39,58 @@ static PyObject *global_http_host;
0
 typedef struct {
0
   PyObject_HEAD
0
   ebb_client *client;
0
-} ebb_Client;
0
+} py_client;
0
 
0
+static PyObject *
0
+py_start_response(py_client *self, PyObject *args, PyObject *kw);
0
+
0
+
0
+static PyMethodDef client_methods[] =
0
+ { {"start_response" , (PyCFunction)py_start_response, METH_VARARGS, NULL }
0
+ // , {"write" , (PyCFunction)write, METH_VARARGS, NULL }
0
+ , {NULL, NULL, 0, NULL}
0
+ };
0
+
0
+static PyTypeObject py_client_t =
0
+ { ob_refcnt: 1
0
+ , tp_name: "ebb.Client"
0
+ , tp_doc: "a wrapper around ebb_client"
0
+ , tp_basicsize: sizeof(py_client)
0
+ , tp_flags: Py_TPFLAGS_DEFAULT
0
+ , tp_methods: client_methods
0
+ };
0
+
0
+static PyObject *
0
+py_start_response(py_client *self, PyObject *args, PyObject *kw)
0
+{
0
+ PyObject *response_headers;
0
+ char *status_string;
0
+ int status;
0
+
0
+ if(!PyArg_ParseTuple(args, "sO", &status_string, &response_headers))
0
+ return NULL;
0
+
0
+ /* do this goofy split(' ') operation. wsgi is such a terrible api. */
0
+ status = 100 * (status_string[0] - '0')
0
+ + 10 * (status_string[1] - '0')
0
+ + 1 * (status_string[2] - '0');
0
+ assert(0 <= status && status < 1000);
0
+
0
+ ebb_client_write_status(self->client, status, status_string+4);
0
+
0
+ PyObject *iterator = PyObject_GetIter(response_headers);
0
+ PyObject *header_pair;
0
+ char *field, *value;
0
+ while(header_pair = PyIter_Next(iterator)) {
0
+ if(!PyArg_ParseTuple(header_pair, "ss", &field, &value))
0
+ return NULL;
0
+ ebb_client_write_header(self->client, field, value);
0
+ Py_DECREF(header_pair);
0
+ }
0
+ ebb_client_write(self->client, "\r\n", 2);
0
+
0
+ Py_RETURN_NONE;
0
+}
0
 
0
 static PyObject* env_field(struct ebb_env_item *item)
0
 {
0
@@ -84,7 +134,7 @@ static PyObject* env_value(struct ebb_env_item *item)
0
 }
0
 
0
 
0
-static PyObject* client_env(ebb_client *client)
0
+static PyObject* py_client_env(ebb_client *client)
0
 {
0
   PyObject *env = PyDict_Copy(base_env);
0
   int i;
0
@@ -99,27 +149,57 @@ static PyObject* client_env(ebb_client *client)
0
   return env;
0
 }
0
 
0
-const char *test_response = "Hello World!\r\n";
0
+static py_client* py_client_new(ebb_client *client)
0
+{
0
+ py_client *self = PyObject_New(py_client, &py_client_t);
0
+ if(self == NULL) return NULL;
0
+ self->client = client;
0
+
0
+ //if(0 < PyObject_SetAttrString((PyObject*)self, "environ", py_client_env(client)))
0
+ // return NULL;
0
+
0
+ return self;
0
+}
0
+
0
+const char *test_response = "test_response defined in ebb_python.c\r\n";
0
 
0
 void request_cb(ebb_client *client, void *ignore)
0
 {
0
- PyObject *env = client_env(client);
0
- PyObject *start_response = Py_None;
0
- PyObject *arglist;
0
- PyObject *result;
0
   
0
- printf("hello world\n");
0
+ PyObject *environ, *start_response;
0
+
0
+ py_client *pclient = py_client_new(client);
0
+ assert(pclient != NULL);
0
+ //environ = PyObject_GetAttrString((PyObject*)pclient, "environ");
0
+ environ = py_client_env(client);
0
+ assert(environ != NULL);
0
   
0
- arglist = Py_BuildValue("OO", env, start_response);
0
- result = PyEval_CallObject(application, arglist);
0
+ start_response = PyObject_GetAttrString((PyObject*)pclient, "start_response");
0
+ assert(start_response != NULL);
0
+
0
+ PyObject *arglist = Py_BuildValue("OO", environ, start_response);
0
+ assert(arglist != NULL);
0
+ assert(application != NULL);
0
+ PyObject *body = PyEval_CallObject(application, arglist);
0
+ assert(body != NULL);
0
   
0
   Py_DECREF(arglist);
0
- Py_DECREF(env);
0
+ Py_DECREF(environ);
0
+
0
+
0
+ PyObject *iterator = PyObject_GetIter(body);
0
+ PyObject *body_item;
0
+ while (body_item = PyIter_Next(iterator)) {
0
+ char *body_string = PyString_AsString(body_item);
0
+ int body_length = PyString_Size(body_item);
0
+ /* Todo support streaming! */
0
+ ebb_client_write(pclient->client, body_string, body_length);
0
+ Py_DECREF(body_item);
0
+ }
0
   
0
- ebb_client_write(client, test_response, strlen(test_response));
0
   ebb_client_finished(client);
0
   
0
- printf("finished calling\n");
0
+ Py_DECREF(pclient);
0
 }
0
 
0
 
0
@@ -155,23 +235,17 @@ static PyObject *stop_server(PyObject *self)
0
   Py_RETURN_NONE;
0
 }
0
 
0
-static PyMethodDef Ebb_methods[] =
0
- { {"start_server" , (PyCFunction)start_server, METH_VARARGS,
0
- "Listen on port supplied. Start the server event loop." }
0
- , {"stop_server" , (PyCFunction)stop_server, METH_NOARGS,
0
- "Unlisten." }
0
+
0
+static PyMethodDef ebb_module_methods[] =
0
+ { {"start_server" , (PyCFunction)start_server, METH_VARARGS, NULL }
0
+ , {"stop_server" , (PyCFunction)stop_server, METH_NOARGS, NULL }
0
   , {NULL, NULL, 0, NULL}
0
   };
0
 
0
 PyMODINIT_FUNC initebb(void)
0
 {
0
- ebb_server_init(&server, loop, request_cb, NULL);
0
-
0
-
0
- PyObject *m;
0
+ PyObject *m = Py_InitModule("ebb", ebb_module_methods);
0
   
0
- m = Py_InitModule("ebb", Ebb_methods);
0
-
0
   base_env = PyDict_New();
0
   PyDict_SetStringString(base_env, "SCRIPT_NAME", "");
0
   PyDict_SetStringString(base_env, "SERVER_SOFTWARE", "Ebb 0.0.4");
0
@@ -183,6 +257,13 @@ PyMODINIT_FUNC initebb(void)
0
   //PyDict_SetItemString(base_env, "wsgi.version", (0,1));
0
   //PyDict_SetItemString(base_env, "wsgi.errors", STDERR);
0
   
0
+
0
+ /* StartResponse */
0
+ py_client_t.tp_new = PyType_GenericNew;
0
+ if (PyType_Ready(&py_client_t) < 0) return;
0
+ Py_INCREF(&py_client_t);
0
+ PyModule_AddObject(m, "Client", (PyObject *)&py_client_t);
0
+
0
 #define DEF_GLOBAL(N, val) global_##N = PyString_FromString(val)
0
   DEF_GLOBAL(http_prefix, "HTTP_");
0
   DEF_GLOBAL(request_method, "REQUEST_METHOD");
0
@@ -198,4 +279,5 @@ PyMODINIT_FUNC initebb(void)
0
   DEF_GLOBAL(content_length, "CONTENT_LENGTH");
0
   DEF_GLOBAL(http_host, "HTTP_HOST");
0
   
0
+ ebb_server_init(&server, loop, request_cb, NULL);
0
 }
0
\ No newline at end of file
...
1
2
3
4
 
5
6
7
8
9
10
11
12
13
 
 
14
15
16
 
17
...
1
2
3
 
4
5
6
7
8
9
 
10
 
 
11
12
13
 
14
15
16
0
@@ -1,15 +1,14 @@
0
 import sys
0
 sys.path.append('/Users/ry/projects/ebb/build/lib.macosx-10.3-ppc-2.5')
0
 import ebb
0
-
0
+# os.path.abspath(__file__)
0
 print "hello"
0
 
0
 def simple_app(environ, start_response):
0
   """Simplest possible application object"""
0
   status = '200 OK'
0
- print repr(environ)
0
   response_headers = [('Content-type','text/plain')]
0
- #start_response(status, response_headers)
0
- return ['Hello world!\n']
0
+ start_response(status, response_headers)
0
+ return ["Hello world!\n", "Something else!", "blllllaaaaaaaah\n"]
0
 
0
-ebb.start_server(simple_app, 4000)
0
\ No newline at end of file
0
+ebb.start_server(simple_app, 4001)
0
\ No newline at end of file

Comments

    No one has commented yet.