src/http/v3/ngx_http_v3.c - nginx source code

Functions defined

Source code


  1. /*
  2. * Copyright (C) Roman Arutyunyan
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>


  8. static void ngx_http_v3_keepalive_handler(ngx_event_t *ev);
  9. static void ngx_http_v3_cleanup_session(void *data);


  10. ngx_int_t
  11. ngx_http_v3_init_session(ngx_connection_t *c)
  12. {
  13.     ngx_pool_cleanup_t     *cln;
  14.     ngx_http_connection_t  *hc;
  15.     ngx_http_v3_session_t  *h3c;

  16.     hc = c->data;

  17.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 init session");

  18.     h3c = ngx_pcalloc(c->pool, sizeof(ngx_http_v3_session_t));
  19.     if (h3c == NULL) {
  20.         goto failed;
  21.     }

  22.     h3c->http_connection = hc;

  23.     ngx_queue_init(&h3c->blocked);

  24.     h3c->keepalive.log = c->log;
  25.     h3c->keepalive.data = c;
  26.     h3c->keepalive.handler = ngx_http_v3_keepalive_handler;

  27.     h3c->table.send_insert_count.log = c->log;
  28.     h3c->table.send_insert_count.data = c;
  29.     h3c->table.send_insert_count.handler = ngx_http_v3_inc_insert_count_handler;

  30.     cln = ngx_pool_cleanup_add(c->pool, 0);
  31.     if (cln == NULL) {
  32.         goto failed;
  33.     }

  34.     cln->handler = ngx_http_v3_cleanup_session;
  35.     cln->data = h3c;

  36.     c->data = h3c;

  37.     return NGX_OK;

  38. failed:

  39.     ngx_log_error(NGX_LOG_ERR, c->log, 0, "failed to create http3 session");
  40.     return NGX_ERROR;
  41. }


  42. static void
  43. ngx_http_v3_keepalive_handler(ngx_event_t *ev)
  44. {
  45.     ngx_connection_t  *c;

  46.     c = ev->data;

  47.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 keepalive handler");

  48.     ngx_http_v3_finalize_connection(c, NGX_HTTP_V3_ERR_NO_ERROR,
  49.                                     "keepalive timeout");
  50. }


  51. static void
  52. ngx_http_v3_cleanup_session(void *data)
  53. {
  54.     ngx_http_v3_session_t  *h3c = data;

  55.     ngx_http_v3_cleanup_table(h3c);

  56.     if (h3c->keepalive.timer_set) {
  57.         ngx_del_timer(&h3c->keepalive);
  58.     }

  59.     if (h3c->table.send_insert_count.posted) {
  60.         ngx_delete_posted_event(&h3c->table.send_insert_count);
  61.     }
  62. }


  63. ngx_int_t
  64. ngx_http_v3_check_flood(ngx_connection_t *c)
  65. {
  66.     ngx_http_v3_session_t  *h3c;

  67.     h3c = ngx_http_v3_get_session(c);

  68.     if (h3c->total_bytes / 8 > h3c->payload_bytes + 1048576) {
  69.         ngx_log_error(NGX_LOG_INFO, c->log, 0, "http3 flood detected");

  70.         ngx_http_v3_finalize_connection(c, NGX_HTTP_V3_ERR_NO_ERROR,
  71.                                         "HTTP/3 flood detected");
  72.         return NGX_ERROR;
  73.     }

  74.     return NGX_OK;
  75. }