uWSGI-pypy

view uwsgi_pypy.c @ 0:7bbae6efc126

first public commit
author roberto@debian32
date mer ago 17 11:28:14 2011 +0200 (2011-08-17)
parents
children
line source
1 #include <Python.h>
2 #include <uwsgi.h>
4 extern struct uwsgi_server uwsgi;
5 extern char **environ;
7 PyObject *callable;
8 PyObject *callable_args;
9 PyObject *envs;
11 PyObject *u_log(PyObject *self, PyObject *args) {
13 char *message = NULL;
15 if (!PyArg_ParseTuple(args, "s", &message)) return NULL;
17 uwsgi_log("%s\n", message);
19 uwsgi_log("numproc = %d\n", uwsgi.numproc);
21 Py_INCREF(Py_None);
22 return Py_None;
24 }
26 PyObject *u_init(PyObject *self, PyObject *args) {
28 char **argv;
29 int i;
30 size_t size;
31 PyObject *py_argv = NULL;
33 if (PyTuple_Size(args) < 1) goto clear;
35 py_argv = PyTuple_GetItem(args, 0);
37 uwsgi_log("args size = %d\n", PyTuple_Size(py_argv));
39 size = PyTuple_Size(py_argv) + 2;
41 argv = uwsgi_malloc(sizeof(char *) * size);
43 memset(argv, 0, sizeof(char *) * size);
45 // will be overwritten
46 argv[0] = "uwsgi";
47 for(i=0;i<PyTuple_Size(py_argv);i++) {
48 argv[i+1] = PyString_AsString( PyTuple_GetItem(py_argv, i) );
49 }
51 PyObject *main_module = PyImport_ImportModule("__main__");
52 PyObject *main_dict = PyModule_GetDict(main_module);
54 callable = PyDict_GetItemString(main_dict, "pypy_wsgi_adapter");
55 Py_INCREF(callable);
57 envs = PyDict_New();
58 Py_INCREF(envs);
59 callable_args = PyTuple_New(1);
60 Py_INCREF(callable_args);
62 PyTuple_SetItem(callable_args, 0 , envs);
64 uwsgi_log("pypy callable = %p\n", callable);
66 uwsgi_init(size-1, argv, environ);
68 clear:
69 Py_INCREF(Py_None);
70 return Py_None;
71 }
73 PyObject *u_send(PyObject *self, PyObject *args) {
74 char *body = NULL;
75 size_t size = 0;
76 struct wsgi_request *wsgi_req = current_wsgi_req();
78 if (!PyArg_ParseTuple(args, "s#", &body, &size)) return NULL;
80 wsgi_req->response_size += wsgi_req->socket->proto_write(wsgi_req, body, size);
82 Py_INCREF(Py_None);
83 return Py_None;
84 }
86 int pypy_uwsgi_request(struct wsgi_request *wsgi_req) {
88 int i;
90 /* Standard WSGI request */
91 if (!wsgi_req->uh.pktsize) {
92 uwsgi_log( "Empty pypy request. skip.\n");
93 return -1;
94 }
96 char *ptrbuf = wsgi_req->buffer;
97 char *bufferend = ptrbuf + wsgi_req->uh.pktsize;
99 if (uwsgi_simple_parse_vars(wsgi_req, ptrbuf, bufferend)) {
100 return -1;
103 PyObject *pydictkey, *pydictvalue;
105 for (i = 0; i < wsgi_req->var_cnt; i += 2) {
106 #ifdef UWSGI_DEBUG
107 uwsgi_debug("%.*s: %.*s\n", wsgi_req->hvec[i].iov_len, wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i+1].iov_len, wsgi_req->hvec[i+1].iov_base);
108 #endif
109 pydictkey = PyString_FromStringAndSize(wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i].iov_len);
110 pydictvalue = PyString_FromStringAndSize(wsgi_req->hvec[i + 1].iov_base, wsgi_req->hvec[i + 1].iov_len);
111 PyDict_SetItem(envs, pydictkey, pydictvalue);
115 PyObject *ret = PyEval_CallObject(callable, callable_args);
117 if (!ret) {
118 PyErr_Print();
121 return 0;
124 PyMethodDef methods[] = {
125 {"init", u_init, METH_VARARGS, "initalize the uWSGI engine"},
126 {"send", u_send, METH_VARARGS, "send data via uWSGI socket"},
127 {"log", u_log, METH_VARARGS, "uWSGI log"},
128 {NULL, NULL, 0, NULL}
129 };
131 PyMODINIT_FUNC
132 inituwsgi_pypy()
134 (void) Py_InitModule("uwsgi_pypy", methods);