aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/PySide6/glue/qhttpserver.cpp
blob: b6e485e2cdcf97d32d2b55dc462194227f18b38e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

/*********************************************************************
 * INJECT CODE
 ********************************************************************/

// Note: Lambdas need to be inline, QTBUG-104481
// @snippet qhttpserver-route
QString rule = %CONVERTTOCPP[QString](%PYARG_1);
auto *callable = %PYARG_2;

bool cppResult = %CPPSELF.%FUNCTION_NAME(rule,
                                         [callable](const QHttpServerRequest &request) -> QString {
    Shiboken::GilState state;
    auto *requestPtr = &request;
    Shiboken::AutoDecRef arglist(PyTuple_New(1));
    PyTuple_SET_ITEM(arglist, 0,
                     %CONVERTTOPYTHON[QHttpServerRequest *](requestPtr));
    PyObject *ret = PyObject_CallObject(callable, arglist);
    if (PyErr_Occurred())
        PyErr_Print();
    if (ret == nullptr)
        return QString{};
    QString cppResult = %CONVERTTOCPP[QString](ret);
    return cppResult;
});

%PYARG_0 = %CONVERTTOPYTHON[bool](cppResult);
// @snippet qhttpserver-route

// @snippet qhttpserver-afterrequest
auto *callable = %PYARG_1;

%CPPSELF.%FUNCTION_NAME([callable](QHttpServerResponse &&response,
                                   const QHttpServerRequest &request) {
    Shiboken::GilState state;
    Shiboken::AutoDecRef arglist(PyTuple_New(2));
    auto *responsePtr = &response;
    auto *requestPtr = &request;
    PyTuple_SET_ITEM(arglist, 0,
                     %CONVERTTOPYTHON[QHttpServerResponse *](responsePtr));
    PyTuple_SET_ITEM(arglist, 1,
                     %CONVERTTOPYTHON[QHttpServerRequest *](requestPtr));
    PyObject_CallObject(callable, arglist);
    if (PyErr_Occurred())
        PyErr_Print();
    return std::move(response);
});
// @snippet qhttpserver-afterrequest