This commit is contained in:
Unbit
2013-12-14 07:45:38 +01:00
parent bb4be12fda
commit 4e260fa8a1
10 changed files with 182 additions and 44 deletions
+13 -8
View File
@@ -376,18 +376,22 @@ static int uwsgi_api_ready_fd(lua_State *L) {
static int uwsgi_api_websocket_handshake(lua_State *L) {
uint8_t argc = lua_gettop(L);
if (argc == 0) goto error;
const char *key = NULL, *origin = NULL;
size_t key_len = 0, origin_len = 0;
const char *key = NULL, *origin = NULL, *proto = NULL;
size_t key_len = 0, origin_len = 0, proto_len = 0;
key = lua_tolstring(L, 1, &key_len);
if (argc > 1) {
origin = lua_tolstring(L, 2, &origin_len);
if (argc > 0) {
key = lua_tolstring(L, 1, &key_len);
if (argc > 1) {
origin = lua_tolstring(L, 2, &origin_len);
if (argc > 2) {
proto = lua_tolstring(L, 3, &proto_len);
}
}
}
struct wsgi_request *wsgi_req = current_wsgi_req();
if (uwsgi_websocket_handshake(wsgi_req, (char *)key, key_len, (char *)origin, origin_len)) {
if (uwsgi_websocket_handshake(wsgi_req, (char *)key, key_len, (char *)origin, origin_len, (char *) proto, proto_len)) {
goto error;
}
@@ -786,7 +790,7 @@ static int uwsgi_lua_request(struct wsgi_request *wsgi_req) {
uwsgi_log("%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
lua_pushvalue(L, -1);
goto clear;
goto clear2;
}
//uwsgi_log("%d %s %s %s\n",i,lua_typename(L, lua_type(L, -3)), lua_typename(L, lua_type(L, -2)) , lua_typename(L, lua_type(L, -1)));
@@ -827,6 +831,7 @@ static int uwsgi_lua_request(struct wsgi_request *wsgi_req) {
}
clear:
lua_pop(L, 4);
clear2:
// set frequency
if (!ulua.gc_freq || uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].requests % ulua.gc_freq == 0) {
lua_gc(L, LUA_GCCOLLECT, 0);
+14 -6
View File
@@ -473,16 +473,24 @@ XS(XS_websocket_handshake) {
char *origin = NULL;
STRLEN origin_len = 0;
psgi_check_args(1);
key = SvPV(ST(0), key_len);
char *proto = NULL;
STRLEN proto_len = 0;
if (items > 1) {
origin = SvPV(ST(0), origin_len);
psgi_check_args(0);
if (items > 0) {
key = SvPV(ST(0), key_len);
if (items > 1) {
origin = SvPV(ST(1), origin_len);
if (items > 2) {
proto = SvPV(ST(2), proto_len);
}
}
}
struct wsgi_request *wsgi_req = current_wsgi_req();
if (uwsgi_websocket_handshake(wsgi_req, key, key_len, origin, origin_len)) {
if (uwsgi_websocket_handshake(wsgi_req, key, key_len, origin, origin_len, proto, proto_len)) {
croak("unable to complete websocket handshake");
}
+6 -3
View File
@@ -224,7 +224,7 @@ int async_add_fd_write(struct wsgi_request *, int, int);
int async_add_fd_read(struct wsgi_request *, int, int);
int uwsgi_connect(char *, int, int);
int uwsgi_websocket_handshake(struct wsgi_request *, char *, uint16_t, char *, uint16_t);
int uwsgi_websocket_handshake(struct wsgi_request *, char *, uint16_t, char *, uint16_t, char *, uint16_t);
int uwsgi_websocket_send(struct wsgi_request *, char *, size_t);
struct uwsgi_buffer *uwsgi_websocket_recv(struct wsgi_request *);
struct uwsgi_buffer *uwsgi_websocket_recv_nb(struct wsgi_request *);
@@ -839,9 +839,12 @@ uwsgi.websocket_recv_nb = uwsgi_pypy_websocket_recv_nb
"""
uwsgi.websocket_handshake(key, origin)
"""
def uwsgi_pypy_websocket_handshake(key, origin=''):
def uwsgi_pypy_websocket_handshake(key='', origin='', proto=''):
wsgi_req = uwsgi_pypy_current_wsgi_req();
if lib.uwsgi_websocket_handshake(wsgi_req, ffi.new('char[]', key), len(key), ffi.new('char[]',origin), len(origin)) < 0:
c_key = ffi.new('char[]', key)
c_origin = ffi.new('char[]', origin)
c_proto = ffi.new('char[]', proto)
if lib.uwsgi_websocket_handshake(wsgi_req, c_key, len(key), c_origin, len(origin), c_proto, len(proto)) < 0:
raise IOError("unable to complete websocket handshake")
uwsgi.websocket_handshake = uwsgi_pypy_websocket_handshake
+5 -2
View File
@@ -965,14 +965,17 @@ PyObject *py_uwsgi_websocket_handshake(PyObject * self, PyObject * args) {
char *origin = NULL;
Py_ssize_t origin_len = 0;
if (!PyArg_ParseTuple(args, "s#|s#:websocket_handshake", &key, &key_len, &origin, &origin_len)) {
char *proto = NULL;
Py_ssize_t proto_len = 0;
if (!PyArg_ParseTuple(args, "|s#s#s#:websocket_handshake", &key, &key_len, &origin, &origin_len, &proto, &proto_len)) {
return NULL;
}
struct wsgi_request *wsgi_req = py_current_wsgi_req();
UWSGI_RELEASE_GIL
int ret = uwsgi_websocket_handshake(wsgi_req, key, key_len, origin, origin_len);
int ret = uwsgi_websocket_handshake(wsgi_req, key, key_len, origin, origin_len, proto, proto_len);
UWSGI_GET_GIL
if (ret) {
+18 -17
View File
@@ -1048,25 +1048,26 @@ static VALUE uwsgi_ruby_websocket_handshake(int argc, VALUE *argv, VALUE *class)
struct wsgi_request *wsgi_req = current_wsgi_req();
if (argc < 1) {
rb_raise(rb_eRuntimeError, "you neeto specify a valid websocket key");
return Qnil;
char *key = NULL, *origin = NULL, *proto = NULL;
size_t key_len = 0, origin_len = 0, proto_len = 0;
if (argc > 0) {
Check_Type(argv[0], T_STRING);
key = RSTRING_PTR(argv[0]);
key_len = RSTRING_LEN(argv[0]);
if (argc > 1) {
Check_Type(argv[1], T_STRING);
origin = RSTRING_PTR(argv[1]);
origin_len = RSTRING_LEN(argv[1]);
if (argc > 2) {
Check_Type(argv[2], T_STRING);
proto = RSTRING_PTR(argv[2]);
proto_len = RSTRING_LEN(argv[2]);
}
}
}
Check_Type(argv[0], T_STRING);
char *key = RSTRING_PTR(argv[0]);
size_t key_len = RSTRING_LEN(argv[0]);
char *origin = NULL;
size_t origin_len = 0;
if (argc > 1) {
Check_Type(argv[1], T_STRING);
origin = RSTRING_PTR(argv[1]);
origin_len = RSTRING_LEN(argv[1]);
}
if (uwsgi_websocket_handshake(wsgi_req, key, key_len, origin, origin_len)) {
if (uwsgi_websocket_handshake(wsgi_req, key, key_len, origin, origin_len, proto, proto_len)) {
rb_raise(rb_eRuntimeError, "unable to complete websocket handshake");
}
return Qnil;