File: python-http-header.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (438 lines) | stat: -rw-r--r-- 11,518 bytes parent folder | download | duplicates (2)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * Copyright (c) 2020 One Identity
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include "python-http-header.h"
#include "python-helpers.h"
#include "python-types.h"

#include "driver.h"
#include "str-utils.h"
#include "list-adt.h"

#include "modules/http/http-signals.h"

#define PYTHON_HTTP_HEADER_PLUGIN "python-http-header"

struct _PythonHttpHeaderPlugin
{
  LogDriverPlugin super;
  PythonBinding binding;

  gboolean mark_errors_as_critical;

  struct
  {
    PyObject *class;
    PyObject *instance;
    PyObject *get_headers;
    PyObject *on_http_response_received;
  } py;
};

PythonBinding *
python_http_header_get_binding(PythonHttpHeaderPlugin *self)
{
  return &self->binding;
}

void
python_http_header_set_mark_errors_as_critical(PythonHttpHeaderPlugin *self, gboolean enable)
{
  self->mark_errors_as_critical = enable;
}

static gboolean
_py_append_pylist_to_list(PyObject *py_list, GList **list)
{
  const gchar *str;
  PyObject *py_str;

  if (!py_list)
    {
      msg_debug("Trying to append a NULL-valued PyList to GList");
      goto exit;
    }

  if (!PyList_Check(py_list))
    {
      msg_debug("PyList_Check failed when trying to append PyList to GList.");
      goto exit;
    }

  Py_ssize_t len = PyList_Size(py_list);
  for (int i = 0; i < len; i++)
    {
      py_str = PyList_GetItem(py_list, i); // Borrowed reference
      if (!py_bytes_or_string_to_string(py_str, &str))
        {
          msg_debug("py_bytes_or_string_to_string failed when trying to append PyList to GList");
          goto exit;
        }

      *list = g_list_append(*list, g_strdup(str));
    }

  return TRUE;

exit:
  return FALSE;
}

static void
_py_append_str_to_pylist(gconstpointer data, gpointer user_data)
{
  PyObject *py_str = py_string_from_string((gchar *) data, -1);
  if (!py_str)
    {
      gchar buf[256];

      msg_error("Error creating Python String object from C string",
                evt_tag_str("exception", _py_format_exception_text(buf, sizeof(buf))));
      _py_finish_exception_handling();

      goto exit;
    }

  PyObject *py_list = (PyObject *) user_data;
  if (PyList_Append(py_list, py_str) != 0)
    {
      gchar buf[256];

      msg_error("Error adding new item to Python List",
                evt_tag_str("exception", _py_format_exception_text(buf, sizeof(buf))));
      _py_finish_exception_handling();
    }

exit:
  Py_XDECREF(py_str); // Even if append() succeeds, it doesn't steal the reference to py_str.
}

static PyObject *
_py_convert_list_to_pylist(List *list)
{
  PyObject *py_list = PyList_New(0);
  g_assert(py_list);

  if (list)
    list_foreach(list, _py_append_str_to_pylist, py_list);

  return py_list;
}

static gboolean
_py_attach_class(PythonHttpHeaderPlugin *self)
{
  self->py.class = _py_resolve_qualified_name(self->binding.class);
  if (!self->py.class)
    {
      gchar buf[256];

      msg_error("Error looking up Python class",
                evt_tag_str("class", self->binding.class),
                evt_tag_str("exception", _py_format_exception_text(buf, sizeof(buf))));
      _py_finish_exception_handling();

      return FALSE;
    }
  return TRUE;
}

static gboolean
_py_instantiate_class(PythonHttpHeaderPlugin *self)
{
  PyObject *py_options_dict = python_options_create_py_dict(self->binding.options);
  if (!py_options_dict)
    return FALSE;

  gboolean result = FALSE;
  self->py.instance = _py_invoke_function(self->py.class, py_options_dict, self->binding.class, self->super.name);
  if (!self->py.instance)
    {
      gchar buf[256];

      msg_error("Error instantiating Python class",
                evt_tag_str("class", self->binding.class),
                evt_tag_str("exception", _py_format_exception_text(buf, sizeof(buf))));
      _py_finish_exception_handling();

      goto exit;
    }
  result = TRUE;

exit:
  Py_XDECREF(py_options_dict);
  return result;
}

static gboolean
_py_attach_get_headers(PythonHttpHeaderPlugin *self)
{
  self->py.get_headers = _py_get_attr_or_null(self->py.instance, "get_headers");
  if (!self->py.get_headers)
    {
      msg_error("Error initializing plugin, required method not found",
                evt_tag_str("class", self->binding.class),
                evt_tag_str("method", "get_headers"));
      return FALSE;
    }

  return TRUE;
}

static gboolean
_py_attach_on_http_response_received(PythonHttpHeaderPlugin *self)
{
  self->py.on_http_response_received = _py_get_attr_or_null(self->py.instance, "on_http_response_received");

  /*
   * on_http_response_received is an optional method
   * */
  return TRUE;
}

static gboolean
_py_attach_bindings(PythonHttpHeaderPlugin *self)
{
  return _py_attach_class(self) &&
         _py_instantiate_class(self) &&
         _py_attach_get_headers(self) &&
         _py_attach_on_http_response_received(self);
}

static void
_py_detach_bindings(PythonHttpHeaderPlugin *self)
{
  Py_CLEAR(self->py.class);
  Py_CLEAR(self->py.instance);
  Py_CLEAR(self->py.get_headers);
  Py_CLEAR(self->py.on_http_response_received);
}

static void
_append_str_to_list(gpointer data, gpointer user_data)
{
  List *list = (List *) user_data;
  list_append(list, data);
}

static HttpSlotResultType
_get_default_error_code(PythonHttpHeaderPlugin *self)
{
  if (self->mark_errors_as_critical)
    return HTTP_SLOT_CRITICAL_ERROR;

  return HTTP_SLOT_PLUGIN_ERROR;
}

static void
_append_headers(PythonHttpHeaderPlugin *self, HttpHeaderRequestSignalData *data)
{
  PyObject *py_list = NULL,
            *py_args = NULL,
             *py_ret_list = NULL;
  GList *headers = NULL;

  data->result = _get_default_error_code(self);

  // The GIL also guards non-Python plugin struct members from concurrent changes below.
  PyGILState_STATE gstate = PyGILState_Ensure();

  py_list = _py_convert_list_to_pylist(data->request_headers);
  if (!py_list)
    goto cleanup;

  py_args = Py_BuildValue("(sO)", data->request_body->str, py_list);
  if (!py_args)
    {
      gchar buf[256];

      msg_error("Error creating Python arguments",
                evt_tag_str("class", self->binding.class),
                evt_tag_str("exception", _py_format_exception_text(buf, sizeof(buf))));
      _py_finish_exception_handling();

      goto cleanup;
    }

  py_ret_list = _py_invoke_function_with_args(self->py.get_headers, py_args, self->binding.class, "_append_headers");
  if (!py_ret_list)
    {
      gchar buf[256];

      msg_error("Invalid response returned by Python call",
                evt_tag_str("class", self->binding.class),
                evt_tag_str("method", "get_headers"),
                evt_tag_str("exception", _py_format_exception_text(buf, sizeof(buf))));
      _py_finish_exception_handling();

      goto cleanup;
    }

  msg_debug("Python call returned valid response",
            evt_tag_str("class", self->binding.class),
            evt_tag_str("method", "get_headers"),
            evt_tag_str("return_type", py_ret_list->ob_type->tp_name));

  if (!_py_append_pylist_to_list(py_ret_list, &headers))
    {
      gchar buf[256];

      msg_error("Converting Python List failed",
                evt_tag_str("class", self->binding.class),
                evt_tag_str("method", "get_headers"),
                evt_tag_str("exception", _py_format_exception_text(buf, sizeof(buf))));
      _py_finish_exception_handling();
      goto cleanup;
    }

  data->result = HTTP_SLOT_SUCCESS;

cleanup:
  Py_XDECREF(py_args);
  Py_XDECREF(py_list);
  Py_XDECREF(py_ret_list);

  PyGILState_Release(gstate);

  if (headers)
    {
      g_list_foreach(headers, _append_str_to_list, data->request_headers);
      g_list_free_full(headers, g_free);
    }
}

static void
_on_http_response_received(PythonHttpHeaderPlugin *self, HttpResponseReceivedSignalData *data)
{
  if (!self->py.on_http_response_received)
    return;

  PyGILState_STATE gstate = PyGILState_Ensure();
  {

    PyObject *py_arg = Py_BuildValue("i", data->http_code);
    if (!py_arg)
      {
        gchar buf[256];

        msg_error("Error creating Python argument",
                  evt_tag_str("class", self->binding.class),
                  evt_tag_str("exception", _py_format_exception_text(buf, sizeof(buf))));
        _py_finish_exception_handling();
        return;
      }

    _py_invoke_void_function(self->py.on_http_response_received, py_arg, self->binding.class, "_on_http_response_received");

    Py_XDECREF(py_arg);
  }
  PyGILState_Release(gstate);
}

static gboolean
_init(PythonHttpHeaderPlugin *self, LogDriver *driver)
{
  GlobalConfig *cfg = log_pipe_get_config(&driver->super);

  if (!python_binding_init(&self->binding, cfg, driver->id))
    return FALSE;

  PyGILState_STATE gstate = PyGILState_Ensure();

  if (!_py_attach_bindings(self))
    goto fail;

  PyGILState_Release(gstate);
  return TRUE;

fail:
  PyGILState_Release(gstate);
  return FALSE;
}

static gboolean
_attach(LogDriverPlugin *s, LogDriver *driver)
{
  PythonHttpHeaderPlugin *self = (PythonHttpHeaderPlugin *) s;

  if (!_init(self, driver))
    {
      msg_error("Plugin initialization failed",
                evt_tag_str("plugin", PYTHON_HTTP_HEADER_PLUGIN));

      return FALSE;
    }

  g_assert(s->signal_connector == NULL);
  s->signal_connector = signal_slot_connector_ref(driver->super.signal_slot_connector);

  CONNECT(s->signal_connector, signal_http_header_request, _append_headers, self);
  CONNECT(s->signal_connector, signal_http_response_received, _on_http_response_received, self);

  return TRUE;
}

static void
_detach(LogDriverPlugin *s, LogDriver *driver)
{
  PythonHttpHeaderPlugin *self = (PythonHttpHeaderPlugin *) s;

  DISCONNECT(s->signal_connector, signal_http_header_request, _append_headers, self);
  DISCONNECT(s->signal_connector, signal_http_response_received, _on_http_response_received, self);

  signal_slot_connector_unref(s->signal_connector);
  s->signal_connector = NULL;

  python_binding_deinit(&self->binding);
}

static void
_free(LogDriverPlugin *s)
{
  PythonHttpHeaderPlugin *self = (PythonHttpHeaderPlugin *) s;

  PyGILState_STATE gstate = PyGILState_Ensure();
  _py_detach_bindings(self);
  PyGILState_Release(gstate);

  python_binding_clear(&self->binding);

  log_driver_plugin_free_method(s);
}

PythonHttpHeaderPlugin *
python_http_header_new(void)
{
  PythonHttpHeaderPlugin *self = g_new0(PythonHttpHeaderPlugin, 1);

  log_driver_plugin_init_instance(&(self->super), PYTHON_HTTP_HEADER_PLUGIN);

  self->mark_errors_as_critical = TRUE;
  self->py.class = self->py.instance = self->py.get_headers = NULL;

  self->super.attach = _attach;
  self->super.detach = _detach;
  self->super.free_fn = _free;

  python_binding_init_instance(&self->binding);
  return self;
}