nxt_python_asgi_websocket.c (1624:e46b1b422545) nxt_python_asgi_websocket.c (1681:542b5b8c0647)
1
2/*
3 * Copyright (C) NGINX, Inc.
4 */
5
6
7#include <python/nxt_python.h>
8

--- 84 unchanged lines hidden (view full) ---

93 .tp_iternext = nxt_py_asgi_next,
94 .tp_methods = nxt_py_asgi_websocket_methods,
95};
96
97static uint64_t nxt_py_asgi_ws_max_frame_size = 1024 * 1024;
98static uint64_t nxt_py_asgi_ws_max_buffer_size = 10 * 1024 * 1024;
99
100
1
2/*
3 * Copyright (C) NGINX, Inc.
4 */
5
6
7#include <python/nxt_python.h>
8

--- 84 unchanged lines hidden (view full) ---

93 .tp_iternext = nxt_py_asgi_next,
94 .tp_methods = nxt_py_asgi_websocket_methods,
95};
96
97static uint64_t nxt_py_asgi_ws_max_frame_size = 1024 * 1024;
98static uint64_t nxt_py_asgi_ws_max_buffer_size = 10 * 1024 * 1024;
99
100
101nxt_int_t
102nxt_py_asgi_websocket_init(nxt_task_t *task)
101int
102nxt_py_asgi_websocket_init(void)
103{
104 if (nxt_slow_path(PyType_Ready(&nxt_py_asgi_websocket_type) != 0)) {
103{
104 if (nxt_slow_path(PyType_Ready(&nxt_py_asgi_websocket_type) != 0)) {
105 nxt_alert(task,
105 nxt_unit_alert(NULL,
106 "Python failed to initialize the \"asgi_websocket\" type object");
106 "Python failed to initialize the \"asgi_websocket\" type object");
107 return NXT_ERROR;
107 return NXT_UNIT_ERROR;
108 }
109
108 }
109
110 return NXT_OK;
110 return NXT_UNIT_OK;
111}
112
113
114PyObject *
115nxt_py_asgi_websocket_create(nxt_unit_request_info_t *req)
116{
117 nxt_py_asgi_websocket_t *ws;
118

--- 13 unchanged lines hidden (view full) ---

132 return (PyObject *) ws;
133}
134
135
136static PyObject *
137nxt_py_asgi_websocket_receive(PyObject *self, PyObject *none)
138{
139 PyObject *future, *msg;
111}
112
113
114PyObject *
115nxt_py_asgi_websocket_create(nxt_unit_request_info_t *req)
116{
117 nxt_py_asgi_websocket_t *ws;
118

--- 13 unchanged lines hidden (view full) ---

132 return (PyObject *) ws;
133}
134
135
136static PyObject *
137nxt_py_asgi_websocket_receive(PyObject *self, PyObject *none)
138{
139 PyObject *future, *msg;
140 nxt_py_asgi_ctx_data_t *ctx_data;
140 nxt_py_asgi_websocket_t *ws;
141
142 ws = (nxt_py_asgi_websocket_t *) self;
143
144 nxt_unit_req_debug(ws->req, "asgi_websocket_receive");
145
146 /* If exception happened out of receive() call, raise it now. */
147 if (nxt_slow_path(ws->receive_exc_str != NULL)) {

--- 7 unchanged lines hidden (view full) ---

155 if (nxt_slow_path(ws->state == NXT_WS_CLOSED)) {
156 nxt_unit_req_error(ws->req,
157 "receive() called for closed WebSocket");
158
159 return PyErr_Format(PyExc_RuntimeError,
160 "WebSocket already closed");
161 }
162
141 nxt_py_asgi_websocket_t *ws;
142
143 ws = (nxt_py_asgi_websocket_t *) self;
144
145 nxt_unit_req_debug(ws->req, "asgi_websocket_receive");
146
147 /* If exception happened out of receive() call, raise it now. */
148 if (nxt_slow_path(ws->receive_exc_str != NULL)) {

--- 7 unchanged lines hidden (view full) ---

156 if (nxt_slow_path(ws->state == NXT_WS_CLOSED)) {
157 nxt_unit_req_error(ws->req,
158 "receive() called for closed WebSocket");
159
160 return PyErr_Format(PyExc_RuntimeError,
161 "WebSocket already closed");
162 }
163
163 future = PyObject_CallObject(nxt_py_loop_create_future, NULL);
164 ctx_data = ws->req->ctx->data;
165
166 future = PyObject_CallObject(ctx_data->loop_create_future, NULL);
164 if (nxt_slow_path(future == NULL)) {
165 nxt_unit_req_alert(ws->req, "Python failed to create Future object");
166 nxt_python_print_exception();
167
168 return PyErr_Format(PyExc_RuntimeError,
169 "failed to create Future object");
170 }
171
172 if (nxt_slow_path(ws->state == NXT_WS_INIT)) {
173 ws->state = NXT_WS_CONNECT;
174
175 msg = nxt_py_asgi_new_msg(ws->req, nxt_py_websocket_connect_str);
176
167 if (nxt_slow_path(future == NULL)) {
168 nxt_unit_req_alert(ws->req, "Python failed to create Future object");
169 nxt_python_print_exception();
170
171 return PyErr_Format(PyExc_RuntimeError,
172 "failed to create Future object");
173 }
174
175 if (nxt_slow_path(ws->state == NXT_WS_INIT)) {
176 ws->state = NXT_WS_CONNECT;
177
178 msg = nxt_py_asgi_new_msg(ws->req, nxt_py_websocket_connect_str);
179
177 return nxt_py_asgi_set_result_soon(ws->req, future, msg);
180 return nxt_py_asgi_set_result_soon(ws->req, ctx_data, future, msg);
178 }
179
180 if (ws->pending_fins > 0) {
181 msg = nxt_py_asgi_websocket_pop_msg(ws, NULL);
182
181 }
182
183 if (ws->pending_fins > 0) {
184 msg = nxt_py_asgi_websocket_pop_msg(ws, NULL);
185
183 return nxt_py_asgi_set_result_soon(ws->req, future, msg);
186 return nxt_py_asgi_set_result_soon(ws->req, ctx_data, future, msg);
184 }
185
186 if (nxt_slow_path(ws->state == NXT_WS_DISCONNECTED)) {
187 msg = nxt_py_asgi_websocket_disconnect_msg(ws);
188
187 }
188
189 if (nxt_slow_path(ws->state == NXT_WS_DISCONNECTED)) {
190 msg = nxt_py_asgi_websocket_disconnect_msg(ws);
191
189 return nxt_py_asgi_set_result_soon(ws->req, future, msg);
192 return nxt_py_asgi_set_result_soon(ws->req, ctx_data, future, msg);
190 }
191
192 ws->receive_future = future;
193 Py_INCREF(ws->receive_future);
194
195 return future;
196}
197

--- 887 unchanged lines hidden ---
193 }
194
195 ws->receive_future = future;
196 Py_INCREF(ws->receive_future);
197
198 return future;
199}
200

--- 887 unchanged lines hidden ---