0
* one ebb_server per python VM instance.
0
static ebb_server server;
0
-struct ev_loop *loop = NULL;
0
-static PyObject *application = NULL;
0
-static PyObject *base_env = NULL;
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
+py_start_response(py_client *self, PyObject *args, PyObject *kw);
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
+static PyTypeObject py_client_t =
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
+py_start_response(py_client *self, PyObject *args, PyObject *kw)
0
+ PyObject *response_headers;
0
+ if(!PyArg_ParseTuple(args, "sO", &status_string, &response_headers))
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
+ ebb_client_write_status(self->client, status, status_string+4);
0
+ PyObject *iterator = PyObject_GetIter(response_headers);
0
+ PyObject *header_pair;
0
+ while(header_pair = PyIter_Next(iterator)) {
0
+ if(!PyArg_ParseTuple(header_pair, "ss", &field, &value))
0
+ ebb_client_write_header(self->client, field, value);
0
+ Py_DECREF(header_pair);
0
+ ebb_client_write(self->client, "\r\n", 2);
0
static PyObject* env_field(struct ebb_env_item *item)
0
@@ -84,7 +134,7 @@ static PyObject* env_value(struct ebb_env_item *item)
0
-static PyObject* client_env(ebb_client *client)
0
+static PyObject* py_client_env(ebb_client *client)
0
PyObject *env = PyDict_Copy(base_env);
0
@@ -99,27 +149,57 @@ static PyObject* client_env(ebb_client *client)
0
-const char *test_response = "Hello World!\r\n";
0
+static py_client* py_client_new(ebb_client *client)
0
+ py_client *self = PyObject_New(py_client, &py_client_t);
0
+ if(self == NULL) return NULL;
0
+ self->client = client;
0
+ //if(0 < PyObject_SetAttrString((PyObject*)self, "environ", py_client_env(client)))
0
+const char *test_response = "test_response defined in ebb_python.c\r\n";
0
void request_cb(ebb_client *client, void *ignore)
0
- PyObject *env = client_env(client);
0
- PyObject *start_response = Py_None;
0
- printf("hello world\n");
0
+ PyObject *environ, *start_response;
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
- 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
+ PyObject *arglist = Py_BuildValue("OO", environ, start_response);
0
+ assert(arglist != NULL);
0
+ assert(application != NULL);
0
+ PyObject *body = PyEval_CallObject(application, arglist);
0
+ PyObject *iterator = PyObject_GetIter(body);
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
- ebb_client_write(client, test_response, strlen(test_response));
0
ebb_client_finished(client);
0
- printf("finished calling\n");
0
@@ -155,23 +235,17 @@ static PyObject *stop_server(PyObject *self)
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
+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
PyMODINIT_FUNC initebb(void)
0
- ebb_server_init(&server, loop, request_cb, NULL);
0
+ PyObject *m = Py_InitModule("ebb", ebb_module_methods);
0
- m = Py_InitModule("ebb", Ebb_methods);
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
+ 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
#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
+ ebb_server_init(&server, loop, request_cb, NULL);
0
\ No newline at end of file
Comments
No one has commented yet.