src/http/ngx_http_spdy_module.c - nginx-1.7.10

Global variables defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Nginx, Inc.
  3. * Copyright (C) Valentin V. Bartenev
  4. */


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


  9. static ngx_int_t ngx_http_spdy_add_variables(ngx_conf_t *cf);

  10. static ngx_int_t ngx_http_spdy_variable(ngx_http_request_t *r,
  11.     ngx_http_variable_value_t *v, uintptr_t data);
  12. static ngx_int_t ngx_http_spdy_request_priority_variable(ngx_http_request_t *r,
  13.     ngx_http_variable_value_t *v, uintptr_t data);

  14. static ngx_int_t ngx_http_spdy_module_init(ngx_cycle_t *cycle);

  15. static void *ngx_http_spdy_create_main_conf(ngx_conf_t *cf);
  16. static char *ngx_http_spdy_init_main_conf(ngx_conf_t *cf, void *conf);
  17. static void *ngx_http_spdy_create_srv_conf(ngx_conf_t *cf);
  18. static char *ngx_http_spdy_merge_srv_conf(ngx_conf_t *cf, void *parent,
  19.     void *child);
  20. static void *ngx_http_spdy_create_loc_conf(ngx_conf_t *cf);
  21. static char *ngx_http_spdy_merge_loc_conf(ngx_conf_t *cf, void *parent,
  22.     void *child);

  23. static char *ngx_http_spdy_recv_buffer_size(ngx_conf_t *cf, void *post,
  24.     void *data);
  25. static char *ngx_http_spdy_pool_size(ngx_conf_t *cf, void *post, void *data);
  26. static char *ngx_http_spdy_streams_index_mask(ngx_conf_t *cf, void *post,
  27.     void *data);
  28. static char *ngx_http_spdy_chunk_size(ngx_conf_t *cf, void *post, void *data);


  29. static ngx_conf_num_bounds_t  ngx_http_spdy_headers_comp_bounds = {
  30.     ngx_conf_check_num_bounds, 0, 9
  31. };

  32. static ngx_conf_post_t  ngx_http_spdy_recv_buffer_size_post =
  33.     { ngx_http_spdy_recv_buffer_size };
  34. static ngx_conf_post_t  ngx_http_spdy_pool_size_post =
  35.     { ngx_http_spdy_pool_size };
  36. static ngx_conf_post_t  ngx_http_spdy_streams_index_mask_post =
  37.     { ngx_http_spdy_streams_index_mask };
  38. static ngx_conf_post_t  ngx_http_spdy_chunk_size_post =
  39.     { ngx_http_spdy_chunk_size };


  40. static ngx_command_t  ngx_http_spdy_commands[] = {

  41.     { ngx_string("spdy_recv_buffer_size"),
  42.       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
  43.       ngx_conf_set_size_slot,
  44.       NGX_HTTP_MAIN_CONF_OFFSET,
  45.       offsetof(ngx_http_spdy_main_conf_t, recv_buffer_size),
  46.       &ngx_http_spdy_recv_buffer_size_post },

  47.     { ngx_string("spdy_pool_size"),
  48.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  49.       ngx_conf_set_size_slot,
  50.       NGX_HTTP_SRV_CONF_OFFSET,
  51.       offsetof(ngx_http_spdy_srv_conf_t, pool_size),
  52.       &ngx_http_spdy_pool_size_post },

  53.     { ngx_string("spdy_max_concurrent_streams"),
  54.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  55.       ngx_conf_set_num_slot,
  56.       NGX_HTTP_SRV_CONF_OFFSET,
  57.       offsetof(ngx_http_spdy_srv_conf_t, concurrent_streams),
  58.       NULL },

  59.     { ngx_string("spdy_streams_index_size"),
  60.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  61.       ngx_conf_set_num_slot,
  62.       NGX_HTTP_SRV_CONF_OFFSET,
  63.       offsetof(ngx_http_spdy_srv_conf_t, streams_index_mask),
  64.       &ngx_http_spdy_streams_index_mask_post },

  65.     { ngx_string("spdy_recv_timeout"),
  66.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  67.       ngx_conf_set_msec_slot,
  68.       NGX_HTTP_SRV_CONF_OFFSET,
  69.       offsetof(ngx_http_spdy_srv_conf_t, recv_timeout),
  70.       NULL },

  71.     { ngx_string("spdy_keepalive_timeout"),
  72.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  73.       ngx_conf_set_msec_slot,
  74.       NGX_HTTP_SRV_CONF_OFFSET,
  75.       offsetof(ngx_http_spdy_srv_conf_t, keepalive_timeout),
  76.       NULL },

  77.     { ngx_string("spdy_headers_comp"),
  78.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
  79.       ngx_conf_set_num_slot,
  80.       NGX_HTTP_SRV_CONF_OFFSET,
  81.       offsetof(ngx_http_spdy_srv_conf_t, headers_comp),
  82.       &ngx_http_spdy_headers_comp_bounds },

  83.     { ngx_string("spdy_chunk_size"),
  84.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  85.       ngx_conf_set_size_slot,
  86.       NGX_HTTP_LOC_CONF_OFFSET,
  87.       offsetof(ngx_http_spdy_loc_conf_t, chunk_size),
  88.       &ngx_http_spdy_chunk_size_post },

  89.       ngx_null_command
  90. };


  91. static ngx_http_module_t  ngx_http_spdy_module_ctx = {
  92.     ngx_http_spdy_add_variables,           /* preconfiguration */
  93.     NULL,                                  /* postconfiguration */

  94.     ngx_http_spdy_create_main_conf,        /* create main configuration */
  95.     ngx_http_spdy_init_main_conf,          /* init main configuration */

  96.     ngx_http_spdy_create_srv_conf,         /* create server configuration */
  97.     ngx_http_spdy_merge_srv_conf,          /* merge server configuration */

  98.     ngx_http_spdy_create_loc_conf,         /* create location configuration */
  99.     ngx_http_spdy_merge_loc_conf           /* merge location configuration */
  100. };


  101. ngx_module_t  ngx_http_spdy_module = {
  102.     NGX_MODULE_V1,
  103.     &ngx_http_spdy_module_ctx,             /* module context */
  104.     ngx_http_spdy_commands,                /* module directives */
  105.     NGX_HTTP_MODULE,                       /* module type */
  106.     NULL,                                  /* init master */
  107.     ngx_http_spdy_module_init,             /* init module */
  108.     NULL,                                  /* init process */
  109.     NULL,                                  /* init thread */
  110.     NULL,                                  /* exit thread */
  111.     NULL,                                  /* exit process */
  112.     NULL,                                  /* exit master */
  113.     NGX_MODULE_V1_PADDING
  114. };


  115. static ngx_http_variable_t  ngx_http_spdy_vars[] = {

  116.     { ngx_string("spdy"), NULL,
  117.       ngx_http_spdy_variable, 0, 0, 0 },

  118.     { ngx_string("spdy_request_priority"), NULL,
  119.       ngx_http_spdy_request_priority_variable, 0, 0, 0 },

  120.     { ngx_null_string, NULL, NULL, 0, 0, 0 }
  121. };


  122. static ngx_int_t
  123. ngx_http_spdy_add_variables(ngx_conf_t *cf)
  124. {
  125.    ngx_http_variable_t  *var, *v;

  126.     for (v = ngx_http_spdy_vars; v->name.len; v++) {
  127.         var = ngx_http_add_variable(cf, &v->name, v->flags);
  128.         if (var == NULL) {
  129.             return NGX_ERROR;
  130.         }

  131.         var->get_handler = v->get_handler;
  132.         var->data = v->data;
  133.     }

  134.     return NGX_OK;
  135. }


  136. static ngx_int_t
  137. ngx_http_spdy_variable(ngx_http_request_t *r,
  138.     ngx_http_variable_value_t *v, uintptr_t data)
  139. {
  140.     if (r->spdy_stream) {
  141.         v->len = sizeof("3.1") - 1;
  142.         v->valid = 1;
  143.         v->no_cacheable = 0;
  144.         v->not_found = 0;
  145.         v->data = (u_char *) "3.1";

  146.         return NGX_OK;
  147.     }

  148.     *v = ngx_http_variable_null_value;

  149.     return NGX_OK;
  150. }


  151. static ngx_int_t
  152. ngx_http_spdy_request_priority_variable(ngx_http_request_t *r,
  153.     ngx_http_variable_value_t *v, uintptr_t data)
  154. {
  155.     if (r->spdy_stream) {
  156.         v->len = 1;
  157.         v->valid = 1;
  158.         v->no_cacheable = 0;
  159.         v->not_found = 0;

  160.         v->data = ngx_pnalloc(r->pool, 1);
  161.         if (v->data == NULL) {
  162.             return NGX_ERROR;
  163.         }

  164.         v->data[0] = '0' + (u_char) r->spdy_stream->priority;

  165.         return NGX_OK;
  166.     }

  167.     *v = ngx_http_variable_null_value;

  168.     return NGX_OK;
  169. }


  170. static ngx_int_t
  171. ngx_http_spdy_module_init(ngx_cycle_t *cycle)
  172. {
  173.     ngx_http_spdy_request_headers_init();

  174.     return NGX_OK;
  175. }


  176. static void *
  177. ngx_http_spdy_create_main_conf(ngx_conf_t *cf)
  178. {
  179.     ngx_http_spdy_main_conf_t  *smcf;

  180.     smcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_spdy_main_conf_t));
  181.     if (smcf == NULL) {
  182.         return NULL;
  183.     }

  184.     smcf->recv_buffer_size = NGX_CONF_UNSET_SIZE;

  185.     return smcf;
  186. }


  187. static char *
  188. ngx_http_spdy_init_main_conf(ngx_conf_t *cf, void *conf)
  189. {
  190.     ngx_http_spdy_main_conf_t *smcf = conf;

  191.     ngx_conf_init_size_value(smcf->recv_buffer_size, 256 * 1024);

  192.     return NGX_CONF_OK;
  193. }


  194. static void *
  195. ngx_http_spdy_create_srv_conf(ngx_conf_t *cf)
  196. {
  197.     ngx_http_spdy_srv_conf_t  *sscf;

  198.     sscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_spdy_srv_conf_t));
  199.     if (sscf == NULL) {
  200.         return NULL;
  201.     }

  202.     sscf->pool_size = NGX_CONF_UNSET_SIZE;

  203.     sscf->concurrent_streams = NGX_CONF_UNSET_UINT;
  204.     sscf->streams_index_mask = NGX_CONF_UNSET_UINT;

  205.     sscf->recv_timeout = NGX_CONF_UNSET_MSEC;
  206.     sscf->keepalive_timeout = NGX_CONF_UNSET_MSEC;

  207.     sscf->headers_comp = NGX_CONF_UNSET;

  208.     return sscf;
  209. }


  210. static char *
  211. ngx_http_spdy_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
  212. {
  213.     ngx_http_spdy_srv_conf_t *prev = parent;
  214.     ngx_http_spdy_srv_conf_t *conf = child;

  215.     ngx_conf_merge_size_value(conf->pool_size, prev->pool_size, 4096);

  216.     ngx_conf_merge_uint_value(conf->concurrent_streams,
  217.                               prev->concurrent_streams, 100);

  218.     ngx_conf_merge_uint_value(conf->streams_index_mask,
  219.                               prev->streams_index_mask, 32 - 1);

  220.     ngx_conf_merge_msec_value(conf->recv_timeout,
  221.                               prev->recv_timeout, 30000);
  222.     ngx_conf_merge_msec_value(conf->keepalive_timeout,
  223.                               prev->keepalive_timeout, 180000);

  224.     ngx_conf_merge_value(conf->headers_comp, prev->headers_comp, 0);

  225.     return NGX_CONF_OK;
  226. }


  227. static void *
  228. ngx_http_spdy_create_loc_conf(ngx_conf_t *cf)
  229. {
  230.     ngx_http_spdy_loc_conf_t  *slcf;

  231.     slcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_spdy_loc_conf_t));
  232.     if (slcf == NULL) {
  233.         return NULL;
  234.     }

  235.     slcf->chunk_size = NGX_CONF_UNSET_SIZE;

  236.     return slcf;
  237. }


  238. static char *
  239. ngx_http_spdy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
  240. {
  241.     ngx_http_spdy_loc_conf_t *prev = parent;
  242.     ngx_http_spdy_loc_conf_t *conf = child;

  243.     ngx_conf_merge_size_value(conf->chunk_size, prev->chunk_size, 8 * 1024);

  244.     return NGX_CONF_OK;
  245. }


  246. static char *
  247. ngx_http_spdy_recv_buffer_size(ngx_conf_t *cf, void *post, void *data)
  248. {
  249.     size_t *sp = data;

  250.     if (*sp <= 2 * NGX_SPDY_STATE_BUFFER_SIZE) {
  251.         return "value is too small";
  252.     }

  253.     return NGX_CONF_OK;
  254. }


  255. static char *
  256. ngx_http_spdy_pool_size(ngx_conf_t *cf, void *post, void *data)
  257. {
  258.     size_t *sp = data;

  259.     if (*sp < NGX_MIN_POOL_SIZE) {
  260.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  261.                            "the pool size must be no less than %uz",
  262.                            NGX_MIN_POOL_SIZE);
  263.         return NGX_CONF_ERROR;
  264.     }

  265.     if (*sp % NGX_POOL_ALIGNMENT) {
  266.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  267.                            "the pool size must be a multiple of %uz",
  268.                            NGX_POOL_ALIGNMENT);
  269.         return NGX_CONF_ERROR;
  270.     }

  271.     return NGX_CONF_OK;
  272. }


  273. static char *
  274. ngx_http_spdy_streams_index_mask(ngx_conf_t *cf, void *post, void *data)
  275. {
  276.     ngx_uint_t *np = data;

  277.     ngx_uint_t  mask;

  278.     mask = *np - 1;

  279.     if (*np == 0 || (*np & mask)) {
  280.         return "must be a power of two";
  281.     }

  282.     *np = mask;

  283.     return NGX_CONF_OK;
  284. }


  285. static char *
  286. ngx_http_spdy_chunk_size(ngx_conf_t *cf, void *post, void *data)
  287. {
  288.     size_t *sp = data;

  289.     if (*sp == 0) {
  290.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  291.                            "the spdy chunk size cannot be zero");
  292.         return NGX_CONF_ERROR;
  293.     }

  294.     if (*sp > NGX_SPDY_MAX_FRAME_SIZE) {
  295.         *sp = NGX_SPDY_MAX_FRAME_SIZE;
  296.     }

  297.     return NGX_CONF_OK;
  298. }