Skip to content

Commit 685efdb

Browse files
we're calling WSGI now, slowly.
1 parent 2a411b3 commit 685efdb

2 files changed

Lines changed: 80 additions & 18 deletions

File tree

binding.cc

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,52 @@ class PythonObject : public ObjectWrap {
7171
NODEPY_ACCESSOR(ValueOf)
7272
static Handle<Value> ValueOf(const Arguments& args) {
7373
HandleScope scope;
74-
Handle<Object> obj = args.This();
75-
PythonObject* py_obj = ObjectWrap::Unwrap<PythonObject>(obj);
76-
PyObject* result = py_obj->GetValue();
77-
if(PyCallable_Check(result)) {
74+
return scope.Close(ValueOf(args.This()));
75+
}
76+
77+
static Handle<Value> ValueOf(const Handle<Object>& obj) {
78+
HandleScope scope;
79+
PythonObject* jspy = ObjectWrap::Unwrap<PythonObject>(obj);
80+
PyObject* py_obj = jspy->GetValue();
81+
if(PyCallable_Check(py_obj)) {
7882
Handle<FunctionTemplate> call = FunctionTemplate::New(Call);
7983
return scope.Close(call->GetFunction());
80-
} else if (PyNumber_Check(result)) {
81-
long long_result = PyLong_AsLong(result);
84+
} else if (PyNumber_Check(py_obj)) {
85+
long long_result = PyLong_AsLong(py_obj);
8286
return scope.Close(Integer::New(long_result));
87+
} else if (PySequence_Check(py_obj)) {
88+
int len = PySequence_Length(py_obj);
89+
Handle<Array> array = Array::New(len);
90+
for(int i = 0; i < len; ++i) {
91+
Handle<Object> jsobj = python_function_template_->GetFunction()->NewInstance();
92+
PyObject* py_obj_out = PySequence_GetItem(py_obj, i);
93+
PythonObject* obj_out = new PythonObject(py_obj_out);
94+
jsobj->SetInternalField(0, External::New(obj_out));
95+
array->Set(i, jsobj);
96+
}
97+
return scope.Close(array);
98+
} else if (PyMapping_Check(py_obj)) {
99+
int len = PyMapping_Length(py_obj);
100+
Handle<Object> object = Object::New();
101+
PyObject* keys = PyMapping_Keys(py_obj);
102+
PyObject* values = PyMapping_Values(py_obj);
103+
for(int i = 0; i < len; ++i) {
104+
PyObject *key = PySequence_GetItem(keys, i),
105+
*value = PySequence_GetItem(values, i),
106+
*key_as_string = PyObject_Str(key);
107+
char* cstr = PyString_AsString(key_as_string);
108+
109+
Handle<Object> jsobj = python_function_template_->GetFunction()->NewInstance();
110+
PythonObject* obj_out = new PythonObject(value);
111+
jsobj->SetInternalField(0, External::New(obj_out));
112+
object->Set(String::New(cstr), jsobj);
113+
114+
Py_XDECREF(key);
115+
Py_XDECREF(key_as_string);
116+
}
117+
Py_XDECREF(keys);
118+
Py_XDECREF(values);
119+
return scope.Close(object);
83120
}
84121
return Undefined();
85122
};
@@ -126,6 +163,8 @@ class PythonObject : public ObjectWrap {
126163
PyList_SET_ITEM(py_list, i, ConvertArgToPyObject(js_val));
127164
}
128165
return py_list;
166+
} else if(value->IsUndefined()) {
167+
return Py_None;
129168
}
130169
return NULL;
131170
}

wsgi.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ var sys = require('sys'),
22
puts = sys.puts,
33
binding = require('./binding'),
44
path_additions = require('./path_additions'),
5-
http = require('http');
5+
http = require('http'),
6+
url = require('url');
67

78
var sys = binding.import('sys');
89
var os = binding.import('os');
@@ -21,16 +22,38 @@ var wsgi_handler = django_wsgi.WSGIHandler.call()
2122
wsgi_handler.load_middleware();
2223

2324
http.createServer(function (req, res) {
24-
var wsgi_request = django_wsgi.WSGIRequest({
25-
'path':req.url,
26-
'REQUEST_PATH':req.url,
27-
'REQUEST_METHOD':'GET',
28-
'HTTP_HOST':'localhost:8000',
29-
});
30-
var response = wsgi_handler.get_response(wsgi_request);
25+
var path_and_query = url.parse(req.url);
26+
if(!path_and_query.pathname.match(/^\/media/)) {
27+
var wsgi_request = django_wsgi.WSGIRequest({
28+
'PATH_INFO':path_and_query.pathname,
29+
'QUERY_STRING':path_and_query.query,
30+
'HTTP_VERSION':req.httpVersion,
31+
'HTTP_ACCEPT':req.headers['http-accept'],
32+
'HTTP_ACCEPT_CHARSET':req.headers['http-accept-charset'],
33+
'HTTP_ACCEPT_ENCODING':req.headers['http-accept-encoding'],
34+
'HTTP_ACCEPT_LANGUAGE':req.headers['http-accept-language'],
35+
'HTTP_CACHE_CONTROL':req.headers['http-cache-control'],
36+
'REQUEST_METHOD':req.method,
37+
'HTTP_HOST':req.headers['http-host']
38+
});
39+
var response = wsgi_handler.get_response(wsgi_request),
40+
headers = response._headers.valueOf(),
41+
content = response.content.toString(),
42+
headers_out = {},
43+
status_code = response.status_code.valueOf();
3144

32-
res.writeHead(200, {'Content-Type':'text/html'});
33-
var content = response.content.toString();
34-
res.write(content);
35-
res.close();
45+
for(var i in headers) {
46+
var as_array = headers[i].valueOf();
47+
headers_out[as_array[0]] = as_array[1].toString();
48+
};
49+
puts(req.method + ' - ' + response.status_code.valueOf() + ' - ' + path_and_query.pathname + " - " + JSON.stringify(headers_out));
50+
res.writeHead(status_code, headers_out);
51+
res.write(content);
52+
res.close();
53+
} else {
54+
puts(req.method + ' - 404 - ' + path_and_query.pathname + " - {}");
55+
res.writeHead(404, {"Content-Type":"text/html"});
56+
res.write("<h1>sorry, no images.</h1>");
57+
res.close();
58+
}
3659
}).listen(8000);

0 commit comments

Comments
 (0)