src/http/modules/ngx_http_uwsgi_module.c - nginx

Global variables defined

Data types defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Unbit S.a.s. 2009-2010
  3. * Copyright (C) 2008 Manlio Perillo (manlio.perillo@gmail.com)
  4. * Copyright (C) Igor Sysoev
  5. * Copyright (C) Nginx, Inc.
  6. */


  7. #include <ngx_config.h>
  8. #include <ngx_core.h>
  9. #include <ngx_http.h>


  10. typedef struct {
  11.     ngx_array_t                caches;  /* ngx_http_file_cache_t * */
  12. } ngx_http_uwsgi_main_conf_t;


  13. typedef struct {
  14.     ngx_array_t               *flushes;
  15.     ngx_array_t               *lengths;
  16.     ngx_array_t               *values;
  17.     ngx_uint_t                 number;
  18.     ngx_hash_t                 hash;
  19. } ngx_http_uwsgi_params_t;


  20. typedef struct {
  21.     ngx_http_upstream_conf_t   upstream;

  22.     ngx_http_uwsgi_params_t    params;
  23. #if (NGX_HTTP_CACHE)
  24.     ngx_http_uwsgi_params_t    params_cache;
  25. #endif
  26.     ngx_array_t               *params_source;

  27.     ngx_array_t               *uwsgi_lengths;
  28.     ngx_array_t               *uwsgi_values;

  29. #if (NGX_HTTP_CACHE)
  30.     ngx_http_complex_value_t   cache_key;
  31. #endif

  32.     ngx_str_t                  uwsgi_string;

  33.     ngx_uint_t                 modifier1;
  34.     ngx_uint_t                 modifier2;

  35. #if (NGX_HTTP_SSL)
  36.     ngx_uint_t                 ssl;
  37.     ngx_uint_t                 ssl_protocols;
  38.     ngx_str_t                  ssl_ciphers;
  39.     ngx_uint_t                 ssl_verify_depth;
  40.     ngx_str_t                  ssl_trusted_certificate;
  41.     ngx_str_t                  ssl_crl;
  42.     ngx_array_t               *ssl_conf_commands;
  43. #endif
  44. } ngx_http_uwsgi_loc_conf_t;


  45. static ngx_int_t ngx_http_uwsgi_eval(ngx_http_request_t *r,
  46.     ngx_http_uwsgi_loc_conf_t *uwcf);
  47. static ngx_int_t ngx_http_uwsgi_create_request(ngx_http_request_t *r);
  48. static ngx_int_t ngx_http_uwsgi_reinit_request(ngx_http_request_t *r);
  49. static ngx_int_t ngx_http_uwsgi_process_status_line(ngx_http_request_t *r);
  50. static ngx_int_t ngx_http_uwsgi_process_header(ngx_http_request_t *r);
  51. static ngx_int_t ngx_http_uwsgi_input_filter_init(void *data);
  52. static void ngx_http_uwsgi_abort_request(ngx_http_request_t *r);
  53. static void ngx_http_uwsgi_finalize_request(ngx_http_request_t *r,
  54.     ngx_int_t rc);

  55. static void *ngx_http_uwsgi_create_main_conf(ngx_conf_t *cf);
  56. static void *ngx_http_uwsgi_create_loc_conf(ngx_conf_t *cf);
  57. static char *ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent,
  58.     void *child);
  59. static ngx_int_t ngx_http_uwsgi_init_params(ngx_conf_t *cf,
  60.     ngx_http_uwsgi_loc_conf_t *conf, ngx_http_uwsgi_params_t *params,
  61.     ngx_keyval_t *default_params);

  62. static char *ngx_http_uwsgi_pass(ngx_conf_t *cf, ngx_command_t *cmd,
  63.     void *conf);
  64. static char *ngx_http_uwsgi_store(ngx_conf_t *cf, ngx_command_t *cmd,
  65.     void *conf);

  66. #if (NGX_HTTP_CACHE)
  67. static ngx_int_t ngx_http_uwsgi_create_key(ngx_http_request_t *r);
  68. static char *ngx_http_uwsgi_cache(ngx_conf_t *cf, ngx_command_t *cmd,
  69.     void *conf);
  70. static char *ngx_http_uwsgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd,
  71.     void *conf);
  72. #endif

  73. #if (NGX_HTTP_SSL)
  74. static char *ngx_http_uwsgi_ssl_certificate_cache(ngx_conf_t *cf,
  75.     ngx_command_t *cmd, void *conf);
  76. static char *ngx_http_uwsgi_ssl_password_file(ngx_conf_t *cf,
  77.     ngx_command_t *cmd, void *conf);
  78. static char *ngx_http_uwsgi_ssl_conf_command_check(ngx_conf_t *cf, void *post,
  79.     void *data);
  80. static ngx_int_t ngx_http_uwsgi_merge_ssl(ngx_conf_t *cf,
  81.     ngx_http_uwsgi_loc_conf_t *conf, ngx_http_uwsgi_loc_conf_t *prev);
  82. static ngx_int_t ngx_http_uwsgi_set_ssl(ngx_conf_t *cf,
  83.     ngx_http_uwsgi_loc_conf_t *uwcf);
  84. #endif


  85. static ngx_conf_num_bounds_t  ngx_http_uwsgi_modifier_bounds = {
  86.     ngx_conf_check_num_bounds, 0, 255
  87. };


  88. static ngx_conf_bitmask_t ngx_http_uwsgi_next_upstream_masks[] = {
  89.     { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
  90.     { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
  91.     { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
  92.     { ngx_string("non_idempotent"), NGX_HTTP_UPSTREAM_FT_NON_IDEMPOTENT },
  93.     { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
  94.     { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
  95.     { ngx_string("http_403"), NGX_HTTP_UPSTREAM_FT_HTTP_403 },
  96.     { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
  97.     { ngx_string("http_429"), NGX_HTTP_UPSTREAM_FT_HTTP_429 },
  98.     { ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING },
  99.     { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
  100.     { ngx_null_string, 0 }
  101. };


  102. #if (NGX_HTTP_SSL)

  103. static ngx_conf_bitmask_t  ngx_http_uwsgi_ssl_protocols[] = {
  104.     { ngx_string("SSLv2"), NGX_SSL_SSLv2 },
  105.     { ngx_string("SSLv3"), NGX_SSL_SSLv3 },
  106.     { ngx_string("TLSv1"), NGX_SSL_TLSv1 },
  107.     { ngx_string("TLSv1.1"), NGX_SSL_TLSv1_1 },
  108.     { ngx_string("TLSv1.2"), NGX_SSL_TLSv1_2 },
  109.     { ngx_string("TLSv1.3"), NGX_SSL_TLSv1_3 },
  110.     { ngx_null_string, 0 }
  111. };

  112. static ngx_conf_post_t  ngx_http_uwsgi_ssl_conf_command_post =
  113.     { ngx_http_uwsgi_ssl_conf_command_check };

  114. #endif


  115. ngx_module_t  ngx_http_uwsgi_module;


  116. static ngx_command_t ngx_http_uwsgi_commands[] = {

  117.     { ngx_string("uwsgi_pass"),
  118.       NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
  119.       ngx_http_uwsgi_pass,
  120.       NGX_HTTP_LOC_CONF_OFFSET,
  121.       0,
  122.       NULL },

  123.     { ngx_string("uwsgi_modifier1"),
  124.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  125.       ngx_conf_set_num_slot,
  126.       NGX_HTTP_LOC_CONF_OFFSET,
  127.       offsetof(ngx_http_uwsgi_loc_conf_t, modifier1),
  128.       &ngx_http_uwsgi_modifier_bounds },

  129.     { ngx_string("uwsgi_modifier2"),
  130.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  131.       ngx_conf_set_num_slot,
  132.       NGX_HTTP_LOC_CONF_OFFSET,
  133.       offsetof(ngx_http_uwsgi_loc_conf_t, modifier2),
  134.       &ngx_http_uwsgi_modifier_bounds },

  135.     { ngx_string("uwsgi_store"),
  136.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  137.       ngx_http_uwsgi_store,
  138.       NGX_HTTP_LOC_CONF_OFFSET,
  139.       0,
  140.       NULL },

  141.     { ngx_string("uwsgi_store_access"),
  142.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
  143.       ngx_conf_set_access_slot,
  144.       NGX_HTTP_LOC_CONF_OFFSET,
  145.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.store_access),
  146.       NULL },

  147.     { ngx_string("uwsgi_buffering"),
  148.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  149.       ngx_conf_set_flag_slot,
  150.       NGX_HTTP_LOC_CONF_OFFSET,
  151.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.buffering),
  152.       NULL },

  153.     { ngx_string("uwsgi_request_buffering"),
  154.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  155.       ngx_conf_set_flag_slot,
  156.       NGX_HTTP_LOC_CONF_OFFSET,
  157.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.request_buffering),
  158.       NULL },

  159.     { ngx_string("uwsgi_ignore_client_abort"),
  160.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  161.       ngx_conf_set_flag_slot,
  162.       NGX_HTTP_LOC_CONF_OFFSET,
  163.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ignore_client_abort),
  164.       NULL },

  165.     { ngx_string("uwsgi_bind"),
  166.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
  167.       ngx_http_upstream_bind_set_slot,
  168.       NGX_HTTP_LOC_CONF_OFFSET,
  169.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.local),
  170.       NULL },

  171.     { ngx_string("uwsgi_socket_keepalive"),
  172.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  173.       ngx_conf_set_flag_slot,
  174.       NGX_HTTP_LOC_CONF_OFFSET,
  175.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.socket_keepalive),
  176.       NULL },

  177.     { ngx_string("uwsgi_connect_timeout"),
  178.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  179.       ngx_conf_set_msec_slot,
  180.       NGX_HTTP_LOC_CONF_OFFSET,
  181.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.connect_timeout),
  182.       NULL },

  183.     { ngx_string("uwsgi_send_timeout"),
  184.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  185.       ngx_conf_set_msec_slot,
  186.       NGX_HTTP_LOC_CONF_OFFSET,
  187.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.send_timeout),
  188.       NULL },

  189.     { ngx_string("uwsgi_buffer_size"),
  190.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  191.       ngx_conf_set_size_slot,
  192.       NGX_HTTP_LOC_CONF_OFFSET,
  193.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.buffer_size),
  194.       NULL },

  195.     { ngx_string("uwsgi_pass_request_headers"),
  196.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  197.       ngx_conf_set_flag_slot,
  198.       NGX_HTTP_LOC_CONF_OFFSET,
  199.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.pass_request_headers),
  200.       NULL },

  201.     { ngx_string("uwsgi_pass_request_body"),
  202.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  203.       ngx_conf_set_flag_slot,
  204.       NGX_HTTP_LOC_CONF_OFFSET,
  205.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.pass_request_body),
  206.       NULL },

  207.     { ngx_string("uwsgi_intercept_errors"),
  208.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  209.       ngx_conf_set_flag_slot,
  210.       NGX_HTTP_LOC_CONF_OFFSET,
  211.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.intercept_errors),
  212.       NULL },

  213.     { ngx_string("uwsgi_read_timeout"),
  214.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  215.       ngx_conf_set_msec_slot,
  216.       NGX_HTTP_LOC_CONF_OFFSET,
  217.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.read_timeout),
  218.       NULL },

  219.     { ngx_string("uwsgi_buffers"),
  220.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
  221.       ngx_conf_set_bufs_slot,
  222.       NGX_HTTP_LOC_CONF_OFFSET,
  223.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.bufs),
  224.       NULL },

  225.     { ngx_string("uwsgi_busy_buffers_size"),
  226.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  227.       ngx_conf_set_size_slot,
  228.       NGX_HTTP_LOC_CONF_OFFSET,
  229.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.busy_buffers_size_conf),
  230.       NULL },

  231.     { ngx_string("uwsgi_force_ranges"),
  232.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  233.       ngx_conf_set_flag_slot,
  234.       NGX_HTTP_LOC_CONF_OFFSET,
  235.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.force_ranges),
  236.       NULL },

  237.     { ngx_string("uwsgi_limit_rate"),
  238.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  239.       ngx_http_set_complex_value_size_slot,
  240.       NGX_HTTP_LOC_CONF_OFFSET,
  241.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.limit_rate),
  242.       NULL },

  243. #if (NGX_HTTP_CACHE)

  244.     { ngx_string("uwsgi_cache"),
  245.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  246.       ngx_http_uwsgi_cache,
  247.       NGX_HTTP_LOC_CONF_OFFSET,
  248.       0,
  249.       NULL },

  250.     { ngx_string("uwsgi_cache_key"),
  251.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  252.       ngx_http_uwsgi_cache_key,
  253.       NGX_HTTP_LOC_CONF_OFFSET,
  254.       0,
  255.       NULL },

  256.     { ngx_string("uwsgi_cache_path"),
  257.       NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
  258.       ngx_http_file_cache_set_slot,
  259.       NGX_HTTP_MAIN_CONF_OFFSET,
  260.       offsetof(ngx_http_uwsgi_main_conf_t, caches),
  261.       &ngx_http_uwsgi_module },

  262.     { ngx_string("uwsgi_cache_bypass"),
  263.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  264.       ngx_http_set_predicate_slot,
  265.       NGX_HTTP_LOC_CONF_OFFSET,
  266.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_bypass),
  267.       NULL },

  268.     { ngx_string("uwsgi_no_cache"),
  269.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  270.       ngx_http_set_predicate_slot,
  271.       NGX_HTTP_LOC_CONF_OFFSET,
  272.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.no_cache),
  273.       NULL },

  274.     { ngx_string("uwsgi_cache_valid"),
  275.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  276.       ngx_http_file_cache_valid_set_slot,
  277.       NGX_HTTP_LOC_CONF_OFFSET,
  278.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_valid),
  279.       NULL },

  280.     { ngx_string("uwsgi_cache_min_uses"),
  281.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  282.       ngx_conf_set_num_slot,
  283.       NGX_HTTP_LOC_CONF_OFFSET,
  284.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_min_uses),
  285.       NULL },

  286.     { ngx_string("uwsgi_cache_max_range_offset"),
  287.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  288.       ngx_conf_set_off_slot,
  289.       NGX_HTTP_LOC_CONF_OFFSET,
  290.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_max_range_offset),
  291.       NULL },

  292.     { ngx_string("uwsgi_cache_use_stale"),
  293.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  294.       ngx_conf_set_bitmask_slot,
  295.       NGX_HTTP_LOC_CONF_OFFSET,
  296.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_use_stale),
  297.       &ngx_http_uwsgi_next_upstream_masks },

  298.     { ngx_string("uwsgi_cache_methods"),
  299.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  300.       ngx_conf_set_bitmask_slot,
  301.       NGX_HTTP_LOC_CONF_OFFSET,
  302.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_methods),
  303.       &ngx_http_upstream_cache_method_mask },

  304.     { ngx_string("uwsgi_cache_lock"),
  305.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  306.       ngx_conf_set_flag_slot,
  307.       NGX_HTTP_LOC_CONF_OFFSET,
  308.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_lock),
  309.       NULL },

  310.     { ngx_string("uwsgi_cache_lock_timeout"),
  311.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  312.       ngx_conf_set_msec_slot,
  313.       NGX_HTTP_LOC_CONF_OFFSET,
  314.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_lock_timeout),
  315.       NULL },

  316.     { ngx_string("uwsgi_cache_lock_age"),
  317.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  318.       ngx_conf_set_msec_slot,
  319.       NGX_HTTP_LOC_CONF_OFFSET,
  320.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_lock_age),
  321.       NULL },

  322.     { ngx_string("uwsgi_cache_revalidate"),
  323.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  324.       ngx_conf_set_flag_slot,
  325.       NGX_HTTP_LOC_CONF_OFFSET,
  326.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_revalidate),
  327.       NULL },

  328.     { ngx_string("uwsgi_cache_background_update"),
  329.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  330.       ngx_conf_set_flag_slot,
  331.       NGX_HTTP_LOC_CONF_OFFSET,
  332.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_background_update),
  333.       NULL },

  334. #endif

  335.     { ngx_string("uwsgi_temp_path"),
  336.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
  337.       ngx_conf_set_path_slot,
  338.       NGX_HTTP_LOC_CONF_OFFSET,
  339.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.temp_path),
  340.       NULL },

  341.     { ngx_string("uwsgi_max_temp_file_size"),
  342.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  343.       ngx_conf_set_size_slot,
  344.       NGX_HTTP_LOC_CONF_OFFSET,
  345.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.max_temp_file_size_conf),
  346.       NULL },

  347.     { ngx_string("uwsgi_temp_file_write_size"),
  348.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  349.       ngx_conf_set_size_slot,
  350.       NGX_HTTP_LOC_CONF_OFFSET,
  351.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.temp_file_write_size_conf),
  352.       NULL },

  353.     { ngx_string("uwsgi_next_upstream"),
  354.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  355.       ngx_conf_set_bitmask_slot,
  356.       NGX_HTTP_LOC_CONF_OFFSET,
  357.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.next_upstream),
  358.       &ngx_http_uwsgi_next_upstream_masks },

  359.     { ngx_string("uwsgi_next_upstream_tries"),
  360.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  361.       ngx_conf_set_num_slot,
  362.       NGX_HTTP_LOC_CONF_OFFSET,
  363.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.next_upstream_tries),
  364.       NULL },

  365.     { ngx_string("uwsgi_next_upstream_timeout"),
  366.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  367.       ngx_conf_set_msec_slot,
  368.       NGX_HTTP_LOC_CONF_OFFSET,
  369.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.next_upstream_timeout),
  370.       NULL },

  371.     { ngx_string("uwsgi_param"),
  372.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE23,
  373.       ngx_http_upstream_param_set_slot,
  374.       NGX_HTTP_LOC_CONF_OFFSET,
  375.       offsetof(ngx_http_uwsgi_loc_conf_t, params_source),
  376.       NULL },

  377.     { ngx_string("uwsgi_string"),
  378.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  379.       ngx_conf_set_str_slot,
  380.       NGX_HTTP_LOC_CONF_OFFSET,
  381.       offsetof(ngx_http_uwsgi_loc_conf_t, uwsgi_string),
  382.       NULL },

  383.     { ngx_string("uwsgi_pass_header"),
  384.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  385.       ngx_conf_set_str_array_slot,
  386.       NGX_HTTP_LOC_CONF_OFFSET,
  387.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.pass_headers),
  388.       NULL },

  389.     { ngx_string("uwsgi_hide_header"),
  390.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  391.       ngx_conf_set_str_array_slot,
  392.       NGX_HTTP_LOC_CONF_OFFSET,
  393.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.hide_headers),
  394.       NULL },

  395.     { ngx_string("uwsgi_ignore_headers"),
  396.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  397.       ngx_conf_set_bitmask_slot,
  398.       NGX_HTTP_LOC_CONF_OFFSET,
  399.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ignore_headers),
  400.       &ngx_http_upstream_ignore_headers_masks },

  401. #if (NGX_HTTP_SSL)

  402.     { ngx_string("uwsgi_ssl_session_reuse"),
  403.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  404.       ngx_conf_set_flag_slot,
  405.       NGX_HTTP_LOC_CONF_OFFSET,
  406.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ssl_session_reuse),
  407.       NULL },

  408.     { ngx_string("uwsgi_ssl_protocols"),
  409.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  410.       ngx_conf_set_bitmask_slot,
  411.       NGX_HTTP_LOC_CONF_OFFSET,
  412.       offsetof(ngx_http_uwsgi_loc_conf_t, ssl_protocols),
  413.       &ngx_http_uwsgi_ssl_protocols },

  414.     { ngx_string("uwsgi_ssl_ciphers"),
  415.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  416.       ngx_conf_set_str_slot,
  417.       NGX_HTTP_LOC_CONF_OFFSET,
  418.       offsetof(ngx_http_uwsgi_loc_conf_t, ssl_ciphers),
  419.       NULL },

  420.     { ngx_string("uwsgi_ssl_name"),
  421.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  422.       ngx_http_set_complex_value_slot,
  423.       NGX_HTTP_LOC_CONF_OFFSET,
  424.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ssl_name),
  425.       NULL },

  426.     { ngx_string("uwsgi_ssl_server_name"),
  427.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  428.       ngx_conf_set_flag_slot,
  429.       NGX_HTTP_LOC_CONF_OFFSET,
  430.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ssl_server_name),
  431.       NULL },

  432.     { ngx_string("uwsgi_ssl_verify"),
  433.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  434.       ngx_conf_set_flag_slot,
  435.       NGX_HTTP_LOC_CONF_OFFSET,
  436.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ssl_verify),
  437.       NULL },

  438.     { ngx_string("uwsgi_ssl_verify_depth"),
  439.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  440.       ngx_conf_set_num_slot,
  441.       NGX_HTTP_LOC_CONF_OFFSET,
  442.       offsetof(ngx_http_uwsgi_loc_conf_t, ssl_verify_depth),
  443.       NULL },

  444.     { ngx_string("uwsgi_ssl_trusted_certificate"),
  445.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  446.       ngx_conf_set_str_slot,
  447.       NGX_HTTP_LOC_CONF_OFFSET,
  448.       offsetof(ngx_http_uwsgi_loc_conf_t, ssl_trusted_certificate),
  449.       NULL },

  450.     { ngx_string("uwsgi_ssl_crl"),
  451.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  452.       ngx_conf_set_str_slot,
  453.       NGX_HTTP_LOC_CONF_OFFSET,
  454.       offsetof(ngx_http_uwsgi_loc_conf_t, ssl_crl),
  455.       NULL },

  456.     { ngx_string("uwsgi_ssl_certificate"),
  457.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  458.       ngx_http_set_complex_value_zero_slot,
  459.       NGX_HTTP_LOC_CONF_OFFSET,
  460.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ssl_certificate),
  461.       NULL },

  462.     { ngx_string("uwsgi_ssl_certificate_key"),
  463.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  464.       ngx_http_set_complex_value_zero_slot,
  465.       NGX_HTTP_LOC_CONF_OFFSET,
  466.       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ssl_certificate_key),
  467.       NULL },

  468.     { ngx_string("uwsgi_ssl_certificate_cache"),
  469.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
  470.       ngx_http_uwsgi_ssl_certificate_cache,
  471.       NGX_HTTP_LOC_CONF_OFFSET,
  472.       0,
  473.       NULL },

  474.     { ngx_string("uwsgi_ssl_password_file"),
  475.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  476.       ngx_http_uwsgi_ssl_password_file,
  477.       NGX_HTTP_LOC_CONF_OFFSET,
  478.       0,
  479.       NULL },

  480.     { ngx_string("uwsgi_ssl_conf_command"),
  481.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
  482.       ngx_conf_set_keyval_slot,
  483.       NGX_HTTP_LOC_CONF_OFFSET,
  484.       offsetof(ngx_http_uwsgi_loc_conf_t, ssl_conf_commands),
  485.       &ngx_http_uwsgi_ssl_conf_command_post },

  486. #endif

  487.       ngx_null_command
  488. };


  489. static ngx_http_module_t ngx_http_uwsgi_module_ctx = {
  490.     NULL,                                  /* preconfiguration */
  491.     NULL,                                  /* postconfiguration */

  492.     ngx_http_uwsgi_create_main_conf,       /* create main configuration */
  493.     NULL,                                  /* init main configuration */

  494.     NULL,                                  /* create server configuration */
  495.     NULL,                                  /* merge server configuration */

  496.     ngx_http_uwsgi_create_loc_conf,        /* create location configuration */
  497.     ngx_http_uwsgi_merge_loc_conf          /* merge location configuration */
  498. };


  499. ngx_module_t ngx_http_uwsgi_module = {
  500.     NGX_MODULE_V1,
  501.     &ngx_http_uwsgi_module_ctx,            /* module context */
  502.     ngx_http_uwsgi_commands,               /* module directives */
  503.     NGX_HTTP_MODULE,                       /* module type */
  504.     NULL,                                  /* init master */
  505.     NULL,                                  /* init module */
  506.     NULL,                                  /* init process */
  507.     NULL,                                  /* init thread */
  508.     NULL,                                  /* exit thread */
  509.     NULL,                                  /* exit process */
  510.     NULL,                                  /* exit master */
  511.     NGX_MODULE_V1_PADDING
  512. };


  513. static ngx_str_t ngx_http_uwsgi_hide_headers[] = {
  514.     ngx_string("X-Accel-Expires"),
  515.     ngx_string("X-Accel-Redirect"),
  516.     ngx_string("X-Accel-Limit-Rate"),
  517.     ngx_string("X-Accel-Buffering"),
  518.     ngx_string("X-Accel-Charset"),
  519.     ngx_null_string
  520. };


  521. static ngx_keyval_t  ngx_http_uwsgi_headers[] = {
  522.     { ngx_string("HTTP_HOST"),
  523.       ngx_string("$host$is_request_port$request_port") },
  524.     { ngx_null_string, ngx_null_string }
  525. };


  526. #if (NGX_HTTP_CACHE)

  527. static ngx_keyval_t  ngx_http_uwsgi_cache_headers[] = {
  528.     { ngx_string("HTTP_HOST"),
  529.       ngx_string("$host$is_request_port$request_port") },
  530.     { ngx_string("HTTP_IF_MODIFIED_SINCE"),
  531.       ngx_string("$upstream_cache_last_modified") },
  532.     { ngx_string("HTTP_IF_UNMODIFIED_SINCE"), ngx_string("") },
  533.     { ngx_string("HTTP_IF_NONE_MATCH"), ngx_string("$upstream_cache_etag") },
  534.     { ngx_string("HTTP_IF_MATCH"), ngx_string("") },
  535.     { ngx_string("HTTP_RANGE"), ngx_string("") },
  536.     { ngx_string("HTTP_IF_RANGE"), ngx_string("") },
  537.     { ngx_null_string, ngx_null_string }
  538. };

  539. #endif


  540. static ngx_path_init_t ngx_http_uwsgi_temp_path = {
  541.     ngx_string(NGX_HTTP_UWSGI_TEMP_PATH), { 1, 2, 0 }
  542. };


  543. static ngx_int_t
  544. ngx_http_uwsgi_handler(ngx_http_request_t *r)
  545. {
  546.     ngx_int_t                    rc;
  547.     ngx_http_status_t           *status;
  548.     ngx_http_upstream_t         *u;
  549.     ngx_http_uwsgi_loc_conf_t   *uwcf;
  550. #if (NGX_HTTP_CACHE)
  551.     ngx_http_uwsgi_main_conf_t  *uwmcf;
  552. #endif

  553.     if (ngx_http_upstream_create(r) != NGX_OK) {
  554.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  555.     }

  556.     status = ngx_pcalloc(r->pool, sizeof(ngx_http_status_t));
  557.     if (status == NULL) {
  558.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  559.     }

  560.     ngx_http_set_ctx(r, status, ngx_http_uwsgi_module);

  561.     uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module);

  562.     u = r->upstream;

  563.     if (uwcf->uwsgi_lengths == NULL) {

  564. #if (NGX_HTTP_SSL)
  565.         u->ssl = uwcf->ssl;

  566.         if (u->ssl) {
  567.             ngx_str_set(&u->schema, "suwsgi://");

  568.         } else {
  569.             ngx_str_set(&u->schema, "uwsgi://");
  570.         }
  571. #else
  572.         ngx_str_set(&u->schema, "uwsgi://");
  573. #endif

  574.     } else {
  575.         if (ngx_http_uwsgi_eval(r, uwcf) != NGX_OK) {
  576.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  577.         }
  578.     }

  579.     u->output.tag = (ngx_buf_tag_t) &ngx_http_uwsgi_module;

  580.     u->conf = &uwcf->upstream;

  581. #if (NGX_HTTP_CACHE)
  582.     uwmcf = ngx_http_get_module_main_conf(r, ngx_http_uwsgi_module);

  583.     u->caches = &uwmcf->caches;
  584.     u->create_key = ngx_http_uwsgi_create_key;
  585. #endif

  586.     u->create_request = ngx_http_uwsgi_create_request;
  587.     u->reinit_request = ngx_http_uwsgi_reinit_request;
  588.     u->process_header = ngx_http_uwsgi_process_status_line;
  589.     u->abort_request = ngx_http_uwsgi_abort_request;
  590.     u->finalize_request = ngx_http_uwsgi_finalize_request;
  591.     r->state = 0;

  592.     u->buffering = uwcf->upstream.buffering;

  593.     u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
  594.     if (u->pipe == NULL) {
  595.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  596.     }

  597.     u->pipe->input_filter = ngx_event_pipe_copy_input_filter;
  598.     u->pipe->input_ctx = r;

  599.     u->input_filter_init = ngx_http_uwsgi_input_filter_init;
  600.     u->input_filter = ngx_http_upstream_non_buffered_filter;
  601.     u->input_filter_ctx = r;

  602.     if (!uwcf->upstream.request_buffering
  603.         && uwcf->upstream.pass_request_body
  604.         && !r->headers_in.chunked)
  605.     {
  606.         r->request_body_no_buffering = 1;
  607.     }

  608.     rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);

  609.     if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
  610.         return rc;
  611.     }

  612.     return NGX_DONE;
  613. }


  614. static ngx_int_t
  615. ngx_http_uwsgi_eval(ngx_http_request_t *r, ngx_http_uwsgi_loc_conf_t * uwcf)
  616. {
  617.     size_t                add;
  618.     ngx_url_t             url;
  619.     ngx_http_upstream_t  *u;

  620.     ngx_memzero(&url, sizeof(ngx_url_t));

  621.     if (ngx_http_script_run(r, &url.url, uwcf->uwsgi_lengths->elts, 0,
  622.                             uwcf->uwsgi_values->elts)
  623.         == NULL)
  624.     {
  625.         return NGX_ERROR;
  626.     }

  627.     if (url.url.len > 8
  628.         && ngx_strncasecmp(url.url.data, (u_char *) "uwsgi://", 8) == 0)
  629.     {
  630.         add = 8;

  631.     } else if (url.url.len > 9
  632.                && ngx_strncasecmp(url.url.data, (u_char *) "suwsgi://", 9) == 0)
  633.     {

  634. #if (NGX_HTTP_SSL)
  635.         add = 9;
  636.         r->upstream->ssl = 1;
  637. #else
  638.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  639.                       "suwsgi protocol requires SSL support");
  640.         return NGX_ERROR;
  641. #endif

  642.     } else {
  643.         add = 0;
  644.     }

  645.     u = r->upstream;

  646.     if (add) {
  647.         u->schema.len = add;
  648.         u->schema.data = url.url.data;

  649.         url.url.data += add;
  650.         url.url.len -= add;

  651.     } else {
  652.         ngx_str_set(&u->schema, "uwsgi://");
  653.     }

  654.     url.no_resolve = 1;

  655.     if (ngx_parse_url(r->pool, &url) != NGX_OK) {
  656.         if (url.err) {
  657.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  658.                           "%s in upstream \"%V\"", url.err, &url.url);
  659.         }

  660.         return NGX_ERROR;
  661.     }

  662.     u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
  663.     if (u->resolved == NULL) {
  664.         return NGX_ERROR;
  665.     }

  666.     if (url.addrs) {
  667.         u->resolved->sockaddr = url.addrs[0].sockaddr;
  668.         u->resolved->socklen = url.addrs[0].socklen;
  669.         u->resolved->name = url.addrs[0].name;
  670.         u->resolved->naddrs = 1;
  671.     }

  672.     u->resolved->host = url.host;
  673.     u->resolved->port = url.port;
  674.     u->resolved->no_port = url.no_port;

  675.     return NGX_OK;
  676. }


  677. #if (NGX_HTTP_CACHE)

  678. static ngx_int_t
  679. ngx_http_uwsgi_create_key(ngx_http_request_t *r)
  680. {
  681.     ngx_str_t                  *key;
  682.     ngx_http_uwsgi_loc_conf_t  *uwcf;

  683.     key = ngx_array_push(&r->cache->keys);
  684.     if (key == NULL) {
  685.         return NGX_ERROR;
  686.     }

  687.     uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module);

  688.     if (ngx_http_complex_value(r, &uwcf->cache_key, key) != NGX_OK) {
  689.         return NGX_ERROR;
  690.     }

  691.     return NGX_OK;
  692. }

  693. #endif


  694. static ngx_int_t
  695. ngx_http_uwsgi_create_request(ngx_http_request_t *r)
  696. {
  697.     u_char                        ch, sep, *lowcase_key;
  698.     size_t                        key_len, val_len, len, allocated;
  699.     ngx_uint_t                    i, n, hash, skip_empty, header_params;
  700.     ngx_buf_t                    *b;
  701.     ngx_chain_t                  *cl, *body;
  702.     ngx_list_part_t              *part;
  703.     ngx_table_elt_t              *header, *hn, **ignored;
  704.     ngx_http_uwsgi_params_t      *params;
  705.     ngx_http_script_code_pt       code;
  706.     ngx_http_script_engine_t      e, le;
  707.     ngx_http_uwsgi_loc_conf_t    *uwcf;
  708.     ngx_http_script_len_code_pt   lcode;

  709.     len = 0;
  710.     header_params = 0;
  711.     ignored = NULL;

  712.     uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module);

  713. #if (NGX_HTTP_CACHE)
  714.     params = r->upstream->cacheable ? &uwcf->params_cache : &uwcf->params;
  715. #else
  716.     params = &uwcf->params;
  717. #endif

  718.     if (params->lengths) {
  719.         ngx_memzero(&le, sizeof(ngx_http_script_engine_t));

  720.         ngx_http_script_flush_no_cacheable_variables(r, params->flushes);
  721.         le.flushed = 1;

  722.         le.ip = params->lengths->elts;
  723.         le.request = r;

  724.         while (*(uintptr_t *) le.ip) {

  725.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  726.             key_len = lcode(&le);

  727.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  728.             skip_empty = lcode(&le);

  729.             for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
  730.                 lcode = *(ngx_http_script_len_code_pt *) le.ip;
  731.             }
  732.             le.ip += sizeof(uintptr_t);

  733.             if (skip_empty && val_len == 0) {
  734.                 continue;
  735.             }

  736.             len += 2 + key_len + 2 + val_len;
  737.         }
  738.     }

  739.     if (uwcf->upstream.pass_request_headers) {

  740.         allocated = 0;
  741.         lowcase_key = NULL;

  742.         if (ngx_http_link_multi_headers(r) != NGX_OK) {
  743.             return NGX_ERROR;
  744.         }

  745.         if (params->number || r->headers_in.multi) {
  746.             n = 0;
  747.             part = &r->headers_in.headers.part;

  748.             while (part) {
  749.                 n += part->nelts;
  750.                 part = part->next;
  751.             }

  752.             ignored = ngx_palloc(r->pool, n * sizeof(void *));
  753.             if (ignored == NULL) {
  754.                 return NGX_ERROR;
  755.             }
  756.         }

  757.         part = &r->headers_in.headers.part;
  758.         header = part->elts;

  759.         for (i = 0; /* void */ ; i++) {

  760.             if (i >= part->nelts) {
  761.                 if (part->next == NULL) {
  762.                     break;
  763.                 }

  764.                 part = part->next;
  765.                 header = part->elts;
  766.                 i = 0;
  767.             }

  768.             for (n = 0; n < header_params; n++) {
  769.                 if (&header[i] == ignored[n]) {
  770.                     goto next_length;
  771.                 }
  772.             }

  773.             if (params->number) {
  774.                 if (allocated < header[i].key.len) {
  775.                     allocated = header[i].key.len + 16;
  776.                     lowcase_key = ngx_pnalloc(r->pool, allocated);
  777.                     if (lowcase_key == NULL) {
  778.                         return NGX_ERROR;
  779.                     }
  780.                 }

  781.                 hash = 0;

  782.                 for (n = 0; n < header[i].key.len; n++) {
  783.                     ch = header[i].key.data[n];

  784.                     if (ch >= 'A' && ch <= 'Z') {
  785.                         ch |= 0x20;

  786.                     } else if (ch == '-') {
  787.                         ch = '_';
  788.                     }

  789.                     hash = ngx_hash(hash, ch);
  790.                     lowcase_key[n] = ch;
  791.                 }

  792.                 if (ngx_hash_find(&params->hash, hash, lowcase_key, n)) {
  793.                     ignored[header_params++] = &header[i];
  794.                     continue;
  795.                 }
  796.             }

  797.             len += 2 + sizeof("HTTP_") - 1 + header[i].key.len
  798.                  + 2 + header[i].value.len;

  799.             for (hn = header[i].next; hn; hn = hn->next) {
  800.                 len += hn->value.len + 2;
  801.                 ignored[header_params++] = hn;
  802.             }

  803.         next_length:

  804.             continue;
  805.         }
  806.     }

  807.     len += uwcf->uwsgi_string.len;

  808. #if 0
  809.     /* allow custom uwsgi packet */
  810.     if (len > 0 && len < 2) {
  811.         ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
  812.                       "uwsgi request is too little: %uz", len);
  813.         return NGX_ERROR;
  814.     }
  815. #endif

  816.     if (len > 65535) {
  817.         ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
  818.                       "uwsgi request is too big: %uz", len);
  819.         return NGX_ERROR;
  820.     }

  821.     b = ngx_create_temp_buf(r->pool, len + 4);
  822.     if (b == NULL) {
  823.         return NGX_ERROR;
  824.     }

  825.     cl = ngx_alloc_chain_link(r->pool);
  826.     if (cl == NULL) {
  827.         return NGX_ERROR;
  828.     }

  829.     cl->buf = b;

  830.     *b->last++ = (u_char) uwcf->modifier1;
  831.     *b->last++ = (u_char) (len & 0xff);
  832.     *b->last++ = (u_char) ((len >> 8) & 0xff);
  833.     *b->last++ = (u_char) uwcf->modifier2;

  834.     if (params->lengths) {
  835.         ngx_memzero(&e, sizeof(ngx_http_script_engine_t));

  836.         e.ip = params->values->elts;
  837.         e.pos = b->last;
  838.         e.request = r;
  839.         e.flushed = 1;

  840.         le.ip = params->lengths->elts;

  841.         while (*(uintptr_t *) le.ip) {

  842.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  843.             key_len = (u_char) lcode(&le);

  844.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  845.             skip_empty = lcode(&le);

  846.             for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
  847.                 lcode = *(ngx_http_script_len_code_pt *) le.ip;
  848.             }
  849.             le.ip += sizeof(uintptr_t);

  850.             if (skip_empty && val_len == 0) {
  851.                 e.skip = 1;

  852.                 while (*(uintptr_t *) e.ip) {
  853.                     code = *(ngx_http_script_code_pt *) e.ip;
  854.                     code((ngx_http_script_engine_t *) &e);
  855.                 }
  856.                 e.ip += sizeof(uintptr_t);

  857.                 e.skip = 0;

  858.                 continue;
  859.             }

  860.             *e.pos++ = (u_char) (key_len & 0xff);
  861.             *e.pos++ = (u_char) ((key_len >> 8) & 0xff);

  862.             code = *(ngx_http_script_code_pt *) e.ip;
  863.             code((ngx_http_script_engine_t *) &e);

  864.             *e.pos++ = (u_char) (val_len & 0xff);
  865.             *e.pos++ = (u_char) ((val_len >> 8) & 0xff);

  866.             while (*(uintptr_t *) e.ip) {
  867.                 code = *(ngx_http_script_code_pt *) e.ip;
  868.                 code((ngx_http_script_engine_t *) &e);
  869.             }

  870.             e.ip += sizeof(uintptr_t);

  871.             ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  872.                            "uwsgi param: \"%*s: %*s\"",
  873.                            key_len, e.pos - (key_len + 2 + val_len),
  874.                            val_len, e.pos - val_len);
  875.         }

  876.         b->last = e.pos;
  877.     }

  878.     if (uwcf->upstream.pass_request_headers) {

  879.         part = &r->headers_in.headers.part;
  880.         header = part->elts;

  881.         for (i = 0; /* void */ ; i++) {

  882.             if (i >= part->nelts) {
  883.                 if (part->next == NULL) {
  884.                     break;
  885.                 }

  886.                 part = part->next;
  887.                 header = part->elts;
  888.                 i = 0;
  889.             }

  890.             for (n = 0; n < header_params; n++) {
  891.                 if (&header[i] == ignored[n]) {
  892.                     goto next_value;
  893.                 }
  894.             }

  895.             key_len = sizeof("HTTP_") - 1 + header[i].key.len;
  896.             *b->last++ = (u_char) (key_len & 0xff);
  897.             *b->last++ = (u_char) ((key_len >> 8) & 0xff);

  898.             b->last = ngx_cpymem(b->last, "HTTP_", sizeof("HTTP_") - 1);
  899.             for (n = 0; n < header[i].key.len; n++) {
  900.                 ch = header[i].key.data[n];

  901.                 if (ch >= 'a' && ch <= 'z') {
  902.                     ch &= ~0x20;

  903.                 } else if (ch == '-') {
  904.                     ch = '_';
  905.                 }

  906.                 *b->last++ = ch;
  907.             }

  908.             val_len = header[i].value.len;

  909.             for (hn = header[i].next; hn; hn = hn->next) {
  910.                 val_len += hn->value.len + 2;
  911.             }

  912.             *b->last++ = (u_char) (val_len & 0xff);
  913.             *b->last++ = (u_char) ((val_len >> 8) & 0xff);
  914.             b->last = ngx_copy(b->last, header[i].value.data,
  915.                                header[i].value.len);

  916.             if (header[i].next) {

  917.                 if (header[i].key.len == sizeof("Cookie") - 1
  918.                     && ngx_strncasecmp(header[i].key.data, (u_char *) "Cookie",
  919.                                        sizeof("Cookie") - 1)
  920.                        == 0)
  921.                 {
  922.                     sep = ';';

  923.                 } else {
  924.                     sep = ',';
  925.                 }

  926.                 for (hn = header[i].next; hn; hn = hn->next) {
  927.                     *b->last++ = sep;
  928.                     *b->last++ = ' ';
  929.                     b->last = ngx_copy(b->last, hn->value.data, hn->value.len);
  930.                 }
  931.             }

  932.             ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  933.                            "uwsgi param: \"%*s: %*s\"",
  934.                            key_len, b->last - (key_len + 2 + val_len),
  935.                            val_len, b->last - val_len);
  936.         next_value:

  937.             continue;
  938.         }
  939.     }

  940.     b->last = ngx_copy(b->last, uwcf->uwsgi_string.data,
  941.                        uwcf->uwsgi_string.len);

  942.     if (r->request_body_no_buffering) {
  943.         r->upstream->request_bufs = cl;

  944.     } else if (uwcf->upstream.pass_request_body) {
  945.         body = r->upstream->request_bufs;
  946.         r->upstream->request_bufs = cl;

  947.         while (body) {
  948.             b = ngx_alloc_buf(r->pool);
  949.             if (b == NULL) {
  950.                 return NGX_ERROR;
  951.             }

  952.             ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));

  953.             cl->next = ngx_alloc_chain_link(r->pool);
  954.             if (cl->next == NULL) {
  955.                 return NGX_ERROR;
  956.             }

  957.             cl = cl->next;
  958.             cl->buf = b;

  959.             body = body->next;
  960.         }

  961.     } else {
  962.         r->upstream->request_bufs = cl;
  963.     }

  964.     b->flush = 1;
  965.     cl->next = NULL;

  966.     return NGX_OK;
  967. }


  968. static ngx_int_t
  969. ngx_http_uwsgi_reinit_request(ngx_http_request_t *r)
  970. {
  971.     ngx_http_status_t  *status;

  972.     status = ngx_http_get_module_ctx(r, ngx_http_uwsgi_module);

  973.     if (status == NULL) {
  974.         return NGX_OK;
  975.     }

  976.     status->code = 0;
  977.     status->count = 0;
  978.     status->start = NULL;
  979.     status->end = NULL;

  980.     r->upstream->process_header = ngx_http_uwsgi_process_status_line;
  981.     r->state = 0;

  982.     return NGX_OK;
  983. }


  984. static ngx_int_t
  985. ngx_http_uwsgi_process_status_line(ngx_http_request_t *r)
  986. {
  987.     size_t                 len;
  988.     ngx_int_t              rc;
  989.     ngx_http_status_t     *status;
  990.     ngx_http_upstream_t   *u;

  991.     status = ngx_http_get_module_ctx(r, ngx_http_uwsgi_module);

  992.     if (status == NULL) {
  993.         return NGX_ERROR;
  994.     }

  995.     u = r->upstream;

  996.     rc = ngx_http_parse_status_line(r, &u->buffer, status);

  997.     if (rc == NGX_AGAIN) {
  998.         return rc;
  999.     }

  1000.     if (rc == NGX_ERROR) {
  1001.         u->process_header = ngx_http_uwsgi_process_header;
  1002.         u->buffer.pos = status->line_start;
  1003.         r->state = 0;
  1004.         return ngx_http_uwsgi_process_header(r);
  1005.     }

  1006.     if (u->state && u->state->status == 0) {
  1007.         u->state->status = status->code;
  1008.     }

  1009.     u->headers_in.status_n = status->code;

  1010.     len = status->end - status->start;
  1011.     u->headers_in.status_line.len = len;

  1012.     u->headers_in.status_line.data = ngx_pnalloc(r->pool, len);
  1013.     if (u->headers_in.status_line.data == NULL) {
  1014.         return NGX_ERROR;
  1015.     }

  1016.     ngx_memcpy(u->headers_in.status_line.data, status->start, len);

  1017.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1018.                    "http uwsgi status %ui \"%V\"",
  1019.                    u->headers_in.status_n, &u->headers_in.status_line);

  1020.     u->process_header = ngx_http_uwsgi_process_header;

  1021.     return ngx_http_uwsgi_process_header(r);
  1022. }


  1023. static ngx_int_t
  1024. ngx_http_uwsgi_process_header(ngx_http_request_t *r)
  1025. {
  1026.     ngx_str_t                      *status_line;
  1027.     ngx_int_t                       rc, status;
  1028.     ngx_table_elt_t                *h;
  1029.     ngx_http_upstream_t            *u;
  1030.     ngx_http_upstream_header_t     *hh;
  1031.     ngx_http_upstream_main_conf_t  *umcf;

  1032.     umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);

  1033.     for ( ;; ) {

  1034.         rc = ngx_http_parse_header_line(r, &r->upstream->buffer, 1);

  1035.         if (rc == NGX_OK) {

  1036.             /* a header line has been parsed successfully */

  1037.             h = ngx_list_push(&r->upstream->headers_in.headers);
  1038.             if (h == NULL) {
  1039.                 return NGX_ERROR;
  1040.             }

  1041.             h->hash = r->header_hash;

  1042.             h->key.len = r->header_name_end - r->header_name_start;
  1043.             h->value.len = r->header_end - r->header_start;

  1044.             h->key.data = ngx_pnalloc(r->pool,
  1045.                                       h->key.len + 1 + h->value.len + 1
  1046.                                       + h->key.len);
  1047.             if (h->key.data == NULL) {
  1048.                 h->hash = 0;
  1049.                 return NGX_ERROR;
  1050.             }

  1051.             h->value.data = h->key.data + h->key.len + 1;
  1052.             h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;

  1053.             ngx_memcpy(h->key.data, r->header_name_start, h->key.len);
  1054.             h->key.data[h->key.len] = '\0';
  1055.             ngx_memcpy(h->value.data, r->header_start, h->value.len);
  1056.             h->value.data[h->value.len] = '\0';

  1057.             if (h->key.len == r->lowcase_index) {
  1058.                 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);

  1059.             } else {
  1060.                 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
  1061.             }

  1062.             hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
  1063.                                h->lowcase_key, h->key.len);

  1064.             if (hh) {
  1065.                 rc = hh->handler(r, h, hh->offset);

  1066.                 if (rc != NGX_OK) {
  1067.                     return rc;
  1068.                 }
  1069.             }

  1070.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1071.                            "http uwsgi header: \"%V: %V\"", &h->key, &h->value);

  1072.             continue;
  1073.         }

  1074.         if (rc == NGX_HTTP_PARSE_HEADER_DONE) {

  1075.             /* a whole header has been parsed successfully */

  1076.             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1077.                            "http uwsgi header done");

  1078.             u = r->upstream;

  1079.             if (u->headers_in.status_n) {
  1080.                 goto done;
  1081.             }

  1082.             if (u->headers_in.status) {
  1083.                 status_line = &u->headers_in.status->value;

  1084.                 status = ngx_atoi(status_line->data, 3);
  1085.                 if (status == NGX_ERROR) {
  1086.                     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1087.                                   "upstream sent invalid status \"%V\"",
  1088.                                   status_line);
  1089.                     return NGX_HTTP_UPSTREAM_INVALID_HEADER;
  1090.                 }

  1091.                 u->headers_in.status_n = status;

  1092.                 if (status_line->len > 3) {
  1093.                     u->headers_in.status_line = *status_line;
  1094.                 }

  1095.             } else if (u->headers_in.location) {
  1096.                 u->headers_in.status_n = 302;
  1097.                 ngx_str_set(&u->headers_in.status_line,
  1098.                             "302 Moved Temporarily");

  1099.             } else {
  1100.                 u->headers_in.status_n = 200;
  1101.                 ngx_str_set(&u->headers_in.status_line, "200 OK");
  1102.             }

  1103.             if (u->state && u->state->status == 0) {
  1104.                 u->state->status = u->headers_in.status_n;
  1105.             }

  1106.         done:

  1107.             if (u->headers_in.status_n == NGX_HTTP_SWITCHING_PROTOCOLS
  1108.                 && r->headers_in.upgrade)
  1109.             {
  1110.                 u->upgrade = 1;
  1111.             }

  1112.             return NGX_OK;
  1113.         }

  1114.         if (rc == NGX_AGAIN) {
  1115.             return NGX_AGAIN;
  1116.         }

  1117.         /* rc == NGX_HTTP_PARSE_INVALID_HEADER */

  1118.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1119.                       "upstream sent invalid header: \"%*s\\x%02xd...\"",
  1120.                       r->header_end - r->header_name_start,
  1121.                       r->header_name_start, *r->header_end);

  1122.         return NGX_HTTP_UPSTREAM_INVALID_HEADER;
  1123.     }
  1124. }


  1125. static ngx_int_t
  1126. ngx_http_uwsgi_input_filter_init(void *data)
  1127. {
  1128.     ngx_http_request_t   *r = data;
  1129.     ngx_http_upstream_t  *u;

  1130.     u = r->upstream;

  1131.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1132.                    "http uwsgi filter init s:%ui l:%O",
  1133.                    u->headers_in.status_n, u->headers_in.content_length_n);

  1134.     if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
  1135.         || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED)
  1136.     {
  1137.         u->pipe->length = 0;
  1138.         u->length = 0;

  1139.     } else if (r->method == NGX_HTTP_HEAD) {
  1140.         u->pipe->length = -1;
  1141.         u->length = -1;

  1142.     } else {
  1143.         u->pipe->length = u->headers_in.content_length_n;
  1144.         u->length = u->headers_in.content_length_n;
  1145.     }

  1146.     return NGX_OK;
  1147. }


  1148. static void
  1149. ngx_http_uwsgi_abort_request(ngx_http_request_t *r)
  1150. {
  1151.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1152.                    "abort http uwsgi request");

  1153.     return;
  1154. }


  1155. static void
  1156. ngx_http_uwsgi_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
  1157. {
  1158.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1159.                    "finalize http uwsgi request");

  1160.     return;
  1161. }


  1162. static void *
  1163. ngx_http_uwsgi_create_main_conf(ngx_conf_t *cf)
  1164. {
  1165.     ngx_http_uwsgi_main_conf_t  *conf;

  1166.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_uwsgi_main_conf_t));
  1167.     if (conf == NULL) {
  1168.         return NULL;
  1169.     }

  1170. #if (NGX_HTTP_CACHE)
  1171.     if (ngx_array_init(&conf->caches, cf->pool, 4,
  1172.                        sizeof(ngx_http_file_cache_t *))
  1173.         != NGX_OK)
  1174.     {
  1175.         return NULL;
  1176.     }
  1177. #endif

  1178.     return conf;
  1179. }


  1180. static void *
  1181. ngx_http_uwsgi_create_loc_conf(ngx_conf_t *cf)
  1182. {
  1183.     ngx_http_uwsgi_loc_conf_t  *conf;

  1184.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_uwsgi_loc_conf_t));
  1185.     if (conf == NULL) {
  1186.         return NULL;
  1187.     }

  1188.     /*
  1189.      * set by ngx_pcalloc():
  1190.      *
  1191.      *     conf->upstream.bufs.num = 0;
  1192.      *     conf->upstream.ignore_headers = 0;
  1193.      *     conf->upstream.next_upstream = 0;
  1194.      *     conf->upstream.cache_zone = NULL;
  1195.      *     conf->upstream.cache_use_stale = 0;
  1196.      *     conf->upstream.cache_methods = 0;
  1197.      *     conf->upstream.temp_path = NULL;
  1198.      *     conf->upstream.hide_headers_hash = { NULL, 0 };
  1199.      *     conf->upstream.store_lengths = NULL;
  1200.      *     conf->upstream.store_values = NULL;
  1201.      *
  1202.      *     conf->uwsgi_string = { 0, NULL };
  1203.      *     conf->ssl = 0;
  1204.      *     conf->ssl_protocols = 0;
  1205.      *     conf->ssl_ciphers = { 0, NULL };
  1206.      *     conf->ssl_trusted_certificate = { 0, NULL };
  1207.      *     conf->ssl_crl = { 0, NULL };
  1208.      */

  1209.     conf->modifier1 = NGX_CONF_UNSET_UINT;
  1210.     conf->modifier2 = NGX_CONF_UNSET_UINT;

  1211.     conf->upstream.store = NGX_CONF_UNSET;
  1212.     conf->upstream.store_access = NGX_CONF_UNSET_UINT;
  1213.     conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
  1214.     conf->upstream.buffering = NGX_CONF_UNSET;
  1215.     conf->upstream.request_buffering = NGX_CONF_UNSET;
  1216.     conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
  1217.     conf->upstream.force_ranges = NGX_CONF_UNSET;

  1218.     conf->upstream.local = NGX_CONF_UNSET_PTR;
  1219.     conf->upstream.socket_keepalive = NGX_CONF_UNSET;

  1220.     conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
  1221.     conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
  1222.     conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
  1223.     conf->upstream.next_upstream_timeout = NGX_CONF_UNSET_MSEC;

  1224.     conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
  1225.     conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
  1226.     conf->upstream.limit_rate = NGX_CONF_UNSET_PTR;

  1227.     conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
  1228.     conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
  1229.     conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;

  1230.     conf->upstream.pass_request_headers = NGX_CONF_UNSET;
  1231.     conf->upstream.pass_request_body = NGX_CONF_UNSET;

  1232. #if (NGX_HTTP_CACHE)
  1233.     conf->upstream.cache = NGX_CONF_UNSET;
  1234.     conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
  1235.     conf->upstream.cache_max_range_offset = NGX_CONF_UNSET;
  1236.     conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
  1237.     conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
  1238.     conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
  1239.     conf->upstream.cache_lock = NGX_CONF_UNSET;
  1240.     conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
  1241.     conf->upstream.cache_lock_age = NGX_CONF_UNSET_MSEC;
  1242.     conf->upstream.cache_revalidate = NGX_CONF_UNSET;
  1243.     conf->upstream.cache_background_update = NGX_CONF_UNSET;
  1244. #endif

  1245.     conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
  1246.     conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;

  1247.     conf->upstream.intercept_errors = NGX_CONF_UNSET;

  1248. #if (NGX_HTTP_SSL)
  1249.     conf->upstream.ssl_session_reuse = NGX_CONF_UNSET;
  1250.     conf->upstream.ssl_name = NGX_CONF_UNSET_PTR;
  1251.     conf->upstream.ssl_server_name = NGX_CONF_UNSET;
  1252.     conf->upstream.ssl_verify = NGX_CONF_UNSET;
  1253.     conf->ssl_verify_depth = NGX_CONF_UNSET_UINT;
  1254.     conf->upstream.ssl_certificate = NGX_CONF_UNSET_PTR;
  1255.     conf->upstream.ssl_certificate_key = NGX_CONF_UNSET_PTR;
  1256.     conf->upstream.ssl_certificate_cache = NGX_CONF_UNSET_PTR;
  1257.     conf->upstream.ssl_passwords = NGX_CONF_UNSET_PTR;
  1258.     conf->ssl_conf_commands = NGX_CONF_UNSET_PTR;
  1259. #endif

  1260.     /* "uwsgi_cyclic_temp_file" is disabled */
  1261.     conf->upstream.cyclic_temp_file = 0;

  1262.     conf->upstream.change_buffering = 1;

  1263.     ngx_str_set(&conf->upstream.module, "uwsgi");

  1264.     return conf;
  1265. }


  1266. static char *
  1267. ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
  1268. {
  1269.     ngx_http_uwsgi_loc_conf_t *prev = parent;
  1270.     ngx_http_uwsgi_loc_conf_t *conf = child;

  1271.     size_t                        size;
  1272.     ngx_int_t                     rc;
  1273.     ngx_hash_init_t               hash;
  1274.     ngx_http_core_loc_conf_t     *clcf;

  1275. #if (NGX_HTTP_CACHE)

  1276.     if (conf->upstream.store > 0) {
  1277.         conf->upstream.cache = 0;
  1278.     }

  1279.     if (conf->upstream.cache > 0) {
  1280.         conf->upstream.store = 0;
  1281.     }

  1282. #endif

  1283.     if (conf->upstream.store == NGX_CONF_UNSET) {
  1284.         ngx_conf_merge_value(conf->upstream.store, prev->upstream.store, 0);

  1285.         conf->upstream.store_lengths = prev->upstream.store_lengths;
  1286.         conf->upstream.store_values = prev->upstream.store_values;
  1287.     }

  1288.     ngx_conf_merge_uint_value(conf->upstream.store_access,
  1289.                               prev->upstream.store_access, 0600);

  1290.     ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,
  1291.                               prev->upstream.next_upstream_tries, 0);

  1292.     ngx_conf_merge_value(conf->upstream.buffering,
  1293.                               prev->upstream.buffering, 1);

  1294.     ngx_conf_merge_value(conf->upstream.request_buffering,
  1295.                               prev->upstream.request_buffering, 1);

  1296.     ngx_conf_merge_value(conf->upstream.ignore_client_abort,
  1297.                               prev->upstream.ignore_client_abort, 0);

  1298.     ngx_conf_merge_value(conf->upstream.force_ranges,
  1299.                               prev->upstream.force_ranges, 0);

  1300.     ngx_conf_merge_ptr_value(conf->upstream.local,
  1301.                               prev->upstream.local, NULL);

  1302.     ngx_conf_merge_value(conf->upstream.socket_keepalive,
  1303.                               prev->upstream.socket_keepalive, 0);

  1304.     ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
  1305.                               prev->upstream.connect_timeout, 60000);

  1306.     ngx_conf_merge_msec_value(conf->upstream.send_timeout,
  1307.                               prev->upstream.send_timeout, 60000);

  1308.     ngx_conf_merge_msec_value(conf->upstream.read_timeout,
  1309.                               prev->upstream.read_timeout, 60000);

  1310.     ngx_conf_merge_msec_value(conf->upstream.next_upstream_timeout,
  1311.                               prev->upstream.next_upstream_timeout, 0);

  1312.     ngx_conf_merge_size_value(conf->upstream.send_lowat,
  1313.                               prev->upstream.send_lowat, 0);

  1314.     ngx_conf_merge_size_value(conf->upstream.buffer_size,
  1315.                               prev->upstream.buffer_size,
  1316.                               (size_t) ngx_pagesize);

  1317.     ngx_conf_merge_ptr_value(conf->upstream.limit_rate,
  1318.                               prev->upstream.limit_rate, NULL);


  1319.     ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
  1320.                               8, ngx_pagesize);

  1321.     if (conf->upstream.bufs.num < 2) {
  1322.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1323.                            "there must be at least 2 \"uwsgi_buffers\"");
  1324.         return NGX_CONF_ERROR;
  1325.     }


  1326.     size = conf->upstream.buffer_size;
  1327.     if (size < conf->upstream.bufs.size) {
  1328.         size = conf->upstream.bufs.size;
  1329.     }


  1330.     ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
  1331.                               prev->upstream.busy_buffers_size_conf,
  1332.                               NGX_CONF_UNSET_SIZE);

  1333.     if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
  1334.         conf->upstream.busy_buffers_size = 2 * size;
  1335.     } else {
  1336.         conf->upstream.busy_buffers_size =
  1337.             conf->upstream.busy_buffers_size_conf;
  1338.     }

  1339.     if (conf->upstream.busy_buffers_size < size) {
  1340.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1341.             "\"uwsgi_busy_buffers_size\" must be equal to or greater "
  1342.             "than the maximum of the value of \"uwsgi_buffer_size\" and "
  1343.             "one of the \"uwsgi_buffers\"");

  1344.         return NGX_CONF_ERROR;
  1345.     }

  1346.     if (conf->upstream.busy_buffers_size
  1347.         > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
  1348.     {
  1349.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1350.             "\"uwsgi_busy_buffers_size\" must be less than "
  1351.             "the size of all \"uwsgi_buffers\" minus one buffer");

  1352.         return NGX_CONF_ERROR;
  1353.     }


  1354.     ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
  1355.                               prev->upstream.temp_file_write_size_conf,
  1356.                               NGX_CONF_UNSET_SIZE);

  1357.     if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
  1358.         conf->upstream.temp_file_write_size = 2 * size;
  1359.     } else {
  1360.         conf->upstream.temp_file_write_size =
  1361.             conf->upstream.temp_file_write_size_conf;
  1362.     }

  1363.     if (conf->upstream.temp_file_write_size < size) {
  1364.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1365.             "\"uwsgi_temp_file_write_size\" must be equal to or greater than "
  1366.             "the maximum of the value of \"uwsgi_buffer_size\" and "
  1367.             "one of the \"uwsgi_buffers\"");

  1368.         return NGX_CONF_ERROR;
  1369.     }


  1370.     ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
  1371.                               prev->upstream.max_temp_file_size_conf,
  1372.                               NGX_CONF_UNSET_SIZE);

  1373.     if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
  1374.         conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
  1375.     } else {
  1376.         conf->upstream.max_temp_file_size =
  1377.             conf->upstream.max_temp_file_size_conf;
  1378.     }

  1379.     if (conf->upstream.max_temp_file_size != 0
  1380.         && conf->upstream.max_temp_file_size < size)
  1381.     {
  1382.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1383.             "\"uwsgi_max_temp_file_size\" must be equal to zero to disable "
  1384.             "temporary files usage or must be equal to or greater than "
  1385.             "the maximum of the value of \"uwsgi_buffer_size\" and "
  1386.             "one of the \"uwsgi_buffers\"");

  1387.         return NGX_CONF_ERROR;
  1388.     }


  1389.     ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
  1390.                                  prev->upstream.ignore_headers,
  1391.                                  NGX_CONF_BITMASK_SET);


  1392.     ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
  1393.                                  prev->upstream.next_upstream,
  1394.                                  (NGX_CONF_BITMASK_SET
  1395.                                   |NGX_HTTP_UPSTREAM_FT_ERROR
  1396.                                   |NGX_HTTP_UPSTREAM_FT_TIMEOUT));

  1397.     if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
  1398.         conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
  1399.                                        |NGX_HTTP_UPSTREAM_FT_OFF;
  1400.     }

  1401.     if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
  1402.                                   prev->upstream.temp_path,
  1403.                                   &ngx_http_uwsgi_temp_path)
  1404.         != NGX_CONF_OK)
  1405.     {
  1406.         return NGX_CONF_ERROR;
  1407.     }

  1408. #if (NGX_HTTP_CACHE)

  1409.     if (conf->upstream.cache == NGX_CONF_UNSET) {
  1410.         ngx_conf_merge_value(conf->upstream.cache,
  1411.                               prev->upstream.cache, 0);

  1412.         conf->upstream.cache_zone = prev->upstream.cache_zone;
  1413.         conf->upstream.cache_value = prev->upstream.cache_value;
  1414.     }

  1415.     if (conf->upstream.cache_zone && conf->upstream.cache_zone->data == NULL) {
  1416.         ngx_shm_zone_t  *shm_zone;

  1417.         shm_zone = conf->upstream.cache_zone;

  1418.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1419.                            "\"uwsgi_cache\" zone \"%V\" is unknown",
  1420.                            &shm_zone->shm.name);

  1421.         return NGX_CONF_ERROR;
  1422.     }

  1423.     ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
  1424.                               prev->upstream.cache_min_uses, 1);

  1425.     ngx_conf_merge_off_value(conf->upstream.cache_max_range_offset,
  1426.                               prev->upstream.cache_max_range_offset,
  1427.                               NGX_MAX_OFF_T_VALUE);

  1428.     ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
  1429.                               prev->upstream.cache_use_stale,
  1430.                               (NGX_CONF_BITMASK_SET
  1431.                                |NGX_HTTP_UPSTREAM_FT_OFF));

  1432.     if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
  1433.         conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
  1434.                                          |NGX_HTTP_UPSTREAM_FT_OFF;
  1435.     }

  1436.     if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
  1437.         conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
  1438.     }

  1439.     if (conf->upstream.cache_methods == 0) {
  1440.         conf->upstream.cache_methods = prev->upstream.cache_methods;
  1441.     }

  1442.     conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;

  1443.     ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
  1444.                              prev->upstream.cache_bypass, NULL);

  1445.     ngx_conf_merge_ptr_value(conf->upstream.no_cache,
  1446.                              prev->upstream.no_cache, NULL);

  1447.     ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
  1448.                              prev->upstream.cache_valid, NULL);

  1449.     if (conf->cache_key.value.data == NULL) {
  1450.         conf->cache_key = prev->cache_key;
  1451.     }

  1452.     if (conf->upstream.cache && conf->cache_key.value.data == NULL) {
  1453.         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
  1454.                            "no \"uwsgi_cache_key\" for \"uwsgi_cache\"");
  1455.     }

  1456.     ngx_conf_merge_value(conf->upstream.cache_lock,
  1457.                               prev->upstream.cache_lock, 0);

  1458.     ngx_conf_merge_msec_value(conf->upstream.cache_lock_timeout,
  1459.                               prev->upstream.cache_lock_timeout, 5000);

  1460.     ngx_conf_merge_msec_value(conf->upstream.cache_lock_age,
  1461.                               prev->upstream.cache_lock_age, 5000);

  1462.     ngx_conf_merge_value(conf->upstream.cache_revalidate,
  1463.                               prev->upstream.cache_revalidate, 0);

  1464.     ngx_conf_merge_value(conf->upstream.cache_background_update,
  1465.                               prev->upstream.cache_background_update, 0);

  1466. #endif

  1467.     ngx_conf_merge_value(conf->upstream.pass_request_headers,
  1468.                          prev->upstream.pass_request_headers, 1);
  1469.     ngx_conf_merge_value(conf->upstream.pass_request_body,
  1470.                          prev->upstream.pass_request_body, 1);

  1471.     ngx_conf_merge_value(conf->upstream.intercept_errors,
  1472.                          prev->upstream.intercept_errors, 0);

  1473. #if (NGX_HTTP_SSL)

  1474.     if (ngx_http_uwsgi_merge_ssl(cf, conf, prev) != NGX_OK) {
  1475.         return NGX_CONF_ERROR;
  1476.     }

  1477.     ngx_conf_merge_value(conf->upstream.ssl_session_reuse,
  1478.                               prev->upstream.ssl_session_reuse, 1);

  1479.     ngx_conf_merge_bitmask_value(conf->ssl_protocols, prev->ssl_protocols,
  1480.                               (NGX_CONF_BITMASK_SET|NGX_SSL_DEFAULT_PROTOCOLS));

  1481.     ngx_conf_merge_str_value(conf->ssl_ciphers, prev->ssl_ciphers,
  1482.                              "DEFAULT");

  1483.     ngx_conf_merge_ptr_value(conf->upstream.ssl_name,
  1484.                               prev->upstream.ssl_name, NULL);
  1485.     ngx_conf_merge_value(conf->upstream.ssl_server_name,
  1486.                               prev->upstream.ssl_server_name, 0);
  1487.     ngx_conf_merge_value(conf->upstream.ssl_verify,
  1488.                               prev->upstream.ssl_verify, 0);
  1489.     ngx_conf_merge_uint_value(conf->ssl_verify_depth,
  1490.                               prev->ssl_verify_depth, 1);
  1491.     ngx_conf_merge_str_value(conf->ssl_trusted_certificate,
  1492.                               prev->ssl_trusted_certificate, "");
  1493.     ngx_conf_merge_str_value(conf->ssl_crl, prev->ssl_crl, "");

  1494.     ngx_conf_merge_ptr_value(conf->upstream.ssl_certificate,
  1495.                               prev->upstream.ssl_certificate, NULL);
  1496.     ngx_conf_merge_ptr_value(conf->upstream.ssl_certificate_key,
  1497.                               prev->upstream.ssl_certificate_key, NULL);
  1498.     ngx_conf_merge_ptr_value(conf->upstream.ssl_certificate_cache,
  1499.                               prev->upstream.ssl_certificate_cache, NULL);

  1500.     if (ngx_http_upstream_merge_ssl_passwords(cf, &conf->upstream,
  1501.                                               &prev->upstream)
  1502.         != NGX_OK)
  1503.     {
  1504.         return NGX_CONF_ERROR;
  1505.     }

  1506.     ngx_conf_merge_ptr_value(conf->ssl_conf_commands,
  1507.                               prev->ssl_conf_commands, NULL);

  1508.     if (conf->ssl && ngx_http_uwsgi_set_ssl(cf, conf) != NGX_OK) {
  1509.         return NGX_CONF_ERROR;
  1510.     }

  1511. #endif

  1512.     ngx_conf_merge_str_value(conf->uwsgi_string, prev->uwsgi_string, "");

  1513.     hash.max_size = 512;
  1514.     hash.bucket_size = ngx_align(64, ngx_cacheline_size);
  1515.     hash.name = "uwsgi_hide_headers_hash";

  1516.     if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
  1517.             &prev->upstream, ngx_http_uwsgi_hide_headers, &hash)
  1518.         != NGX_OK)
  1519.     {
  1520.         return NGX_CONF_ERROR;
  1521.     }

  1522.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

  1523.     if (clcf->noname
  1524.         && conf->upstream.upstream == NULL && conf->uwsgi_lengths == NULL)
  1525.     {
  1526.         conf->upstream.upstream = prev->upstream.upstream;

  1527.         conf->uwsgi_lengths = prev->uwsgi_lengths;
  1528.         conf->uwsgi_values = prev->uwsgi_values;

  1529. #if (NGX_HTTP_SSL)
  1530.         conf->ssl = prev->ssl;
  1531. #endif
  1532.     }

  1533.     if (clcf->lmt_excpt && clcf->handler == NULL
  1534.         && (conf->upstream.upstream || conf->uwsgi_lengths))
  1535.     {
  1536.         clcf->handler = ngx_http_uwsgi_handler;
  1537.     }

  1538.     ngx_conf_merge_uint_value(conf->modifier1, prev->modifier1, 0);
  1539.     ngx_conf_merge_uint_value(conf->modifier2, prev->modifier2, 0);

  1540.     if (conf->params_source == NULL) {
  1541.         conf->params = prev->params;
  1542. #if (NGX_HTTP_CACHE)
  1543.         conf->params_cache = prev->params_cache;
  1544. #endif
  1545.         conf->params_source = prev->params_source;
  1546.     }

  1547.     rc = ngx_http_uwsgi_init_params(cf, conf, &conf->params,
  1548.                                     ngx_http_uwsgi_headers);
  1549.     if (rc != NGX_OK) {
  1550.         return NGX_CONF_ERROR;
  1551.     }

  1552. #if (NGX_HTTP_CACHE)

  1553.     if (conf->upstream.cache) {
  1554.         rc = ngx_http_uwsgi_init_params(cf, conf, &conf->params_cache,
  1555.                                         ngx_http_uwsgi_cache_headers);
  1556.         if (rc != NGX_OK) {
  1557.             return NGX_CONF_ERROR;
  1558.         }
  1559.     }

  1560. #endif

  1561.     /*
  1562.      * special handling to preserve conf->params in the "http" section
  1563.      * to inherit it to all servers
  1564.      */

  1565.     if (prev->params.hash.buckets == NULL
  1566.         && conf->params_source == prev->params_source)
  1567.     {
  1568.         prev->params = conf->params;
  1569. #if (NGX_HTTP_CACHE)
  1570.         prev->params_cache = conf->params_cache;
  1571. #endif
  1572.     }

  1573.     return NGX_CONF_OK;
  1574. }


  1575. static ngx_int_t
  1576. ngx_http_uwsgi_init_params(ngx_conf_t *cf, ngx_http_uwsgi_loc_conf_t *conf,
  1577.     ngx_http_uwsgi_params_t *params, ngx_keyval_t *default_params)
  1578. {
  1579.     u_char                       *p;
  1580.     size_t                        size;
  1581.     uintptr_t                    *code;
  1582.     ngx_uint_t                    i, nsrc;
  1583.     ngx_array_t                   headers_names, params_merged;
  1584.     ngx_keyval_t                 *h;
  1585.     ngx_hash_key_t               *hk;
  1586.     ngx_hash_init_t               hash;
  1587.     ngx_http_upstream_param_t    *src, *s;
  1588.     ngx_http_script_compile_t     sc;
  1589.     ngx_http_script_copy_code_t  *copy;

  1590.     if (params->hash.buckets) {
  1591.         return NGX_OK;
  1592.     }

  1593.     if (conf->params_source == NULL && default_params == NULL) {
  1594.         params->hash.buckets = (void *) 1;
  1595.         return NGX_OK;
  1596.     }

  1597.     params->lengths = ngx_array_create(cf->pool, 64, 1);
  1598.     if (params->lengths == NULL) {
  1599.         return NGX_ERROR;
  1600.     }

  1601.     params->values = ngx_array_create(cf->pool, 512, 1);
  1602.     if (params->values == NULL) {
  1603.         return NGX_ERROR;
  1604.     }

  1605.     if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
  1606.         != NGX_OK)
  1607.     {
  1608.         return NGX_ERROR;
  1609.     }

  1610.     if (conf->params_source) {
  1611.         src = conf->params_source->elts;
  1612.         nsrc = conf->params_source->nelts;

  1613.     } else {
  1614.         src = NULL;
  1615.         nsrc = 0;
  1616.     }

  1617.     if (default_params) {
  1618.         if (ngx_array_init(&params_merged, cf->temp_pool, 4,
  1619.                            sizeof(ngx_http_upstream_param_t))
  1620.             != NGX_OK)
  1621.         {
  1622.             return NGX_ERROR;
  1623.         }

  1624.         for (i = 0; i < nsrc; i++) {

  1625.             s = ngx_array_push(&params_merged);
  1626.             if (s == NULL) {
  1627.                 return NGX_ERROR;
  1628.             }

  1629.             *s = src[i];
  1630.         }

  1631.         h = default_params;

  1632.         while (h->key.len) {

  1633.             src = params_merged.elts;
  1634.             nsrc = params_merged.nelts;

  1635.             for (i = 0; i < nsrc; i++) {
  1636.                 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
  1637.                     goto next;
  1638.                 }
  1639.             }

  1640.             s = ngx_array_push(&params_merged);
  1641.             if (s == NULL) {
  1642.                 return NGX_ERROR;
  1643.             }

  1644.             s->key = h->key;
  1645.             s->value = h->value;
  1646.             s->skip_empty = 1;

  1647.         next:

  1648.             h++;
  1649.         }

  1650.         src = params_merged.elts;
  1651.         nsrc = params_merged.nelts;
  1652.     }

  1653.     for (i = 0; i < nsrc; i++) {

  1654.         if (src[i].key.len > sizeof("HTTP_") - 1
  1655.             && ngx_strncmp(src[i].key.data, "HTTP_", sizeof("HTTP_") - 1) == 0)
  1656.         {
  1657.             hk = ngx_array_push(&headers_names);
  1658.             if (hk == NULL) {
  1659.                 return NGX_ERROR;
  1660.             }

  1661.             hk->key.len = src[i].key.len - 5;
  1662.             hk->key.data = src[i].key.data + 5;
  1663.             hk->key_hash = ngx_hash_key_lc(hk->key.data, hk->key.len);
  1664.             hk->value = (void *) 1;

  1665.             if (src[i].value.len == 0) {
  1666.                 continue;
  1667.             }
  1668.         }

  1669.         copy = ngx_array_push_n(params->lengths,
  1670.                                 sizeof(ngx_http_script_copy_code_t));
  1671.         if (copy == NULL) {
  1672.             return NGX_ERROR;
  1673.         }

  1674.         copy->code = (ngx_http_script_code_pt) (void *)
  1675.                                                  ngx_http_script_copy_len_code;
  1676.         copy->len = src[i].key.len;

  1677.         copy = ngx_array_push_n(params->lengths,
  1678.                                 sizeof(ngx_http_script_copy_code_t));
  1679.         if (copy == NULL) {
  1680.             return NGX_ERROR;
  1681.         }

  1682.         copy->code = (ngx_http_script_code_pt) (void *)
  1683.                                                  ngx_http_script_copy_len_code;
  1684.         copy->len = src[i].skip_empty;


  1685.         size = (sizeof(ngx_http_script_copy_code_t)
  1686.                 + src[i].key.len + sizeof(uintptr_t) - 1)
  1687.                & ~(sizeof(uintptr_t) - 1);

  1688.         copy = ngx_array_push_n(params->values, size);
  1689.         if (copy == NULL) {
  1690.             return NGX_ERROR;
  1691.         }

  1692.         copy->code = ngx_http_script_copy_code;
  1693.         copy->len = src[i].key.len;

  1694.         p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
  1695.         ngx_memcpy(p, src[i].key.data, src[i].key.len);


  1696.         ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));

  1697.         sc.cf = cf;
  1698.         sc.source = &src[i].value;
  1699.         sc.flushes = &params->flushes;
  1700.         sc.lengths = &params->lengths;
  1701.         sc.values = &params->values;

  1702.         if (ngx_http_script_compile(&sc) != NGX_OK) {
  1703.             return NGX_ERROR;
  1704.         }

  1705.         code = ngx_array_push_n(params->lengths, sizeof(uintptr_t));
  1706.         if (code == NULL) {
  1707.             return NGX_ERROR;
  1708.         }

  1709.         *code = (uintptr_t) NULL;


  1710.         code = ngx_array_push_n(params->values, sizeof(uintptr_t));
  1711.         if (code == NULL) {
  1712.             return NGX_ERROR;
  1713.         }

  1714.         *code = (uintptr_t) NULL;
  1715.     }

  1716.     code = ngx_array_push_n(params->lengths, sizeof(uintptr_t));
  1717.     if (code == NULL) {
  1718.         return NGX_ERROR;
  1719.     }

  1720.     *code = (uintptr_t) NULL;

  1721.     params->number = headers_names.nelts;

  1722.     hash.hash = &params->hash;
  1723.     hash.key = ngx_hash_key_lc;
  1724.     hash.max_size = 512;
  1725.     hash.bucket_size = 64;
  1726.     hash.name = "uwsgi_params_hash";
  1727.     hash.pool = cf->pool;
  1728.     hash.temp_pool = NULL;

  1729.     return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts);
  1730. }


  1731. static char *
  1732. ngx_http_uwsgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1733. {
  1734.     ngx_http_uwsgi_loc_conf_t *uwcf = conf;

  1735.     size_t                      add;
  1736.     ngx_url_t                   u;
  1737.     ngx_str_t                  *value, *url;
  1738.     ngx_uint_t                  n;
  1739.     ngx_http_core_loc_conf_t   *clcf;
  1740.     ngx_http_script_compile_t   sc;

  1741.     if (uwcf->upstream.upstream || uwcf->uwsgi_lengths) {
  1742.         return "is duplicate";
  1743.     }

  1744.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  1745.     clcf->handler = ngx_http_uwsgi_handler;

  1746.     value = cf->args->elts;

  1747.     url = &value[1];

  1748.     n = ngx_http_script_variables_count(url);

  1749.     if (n) {

  1750.         ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));

  1751.         sc.cf = cf;
  1752.         sc.source = url;
  1753.         sc.lengths = &uwcf->uwsgi_lengths;
  1754.         sc.values = &uwcf->uwsgi_values;
  1755.         sc.variables = n;
  1756.         sc.complete_lengths = 1;
  1757.         sc.complete_values = 1;

  1758.         if (ngx_http_script_compile(&sc) != NGX_OK) {
  1759.             return NGX_CONF_ERROR;
  1760.         }

  1761. #if (NGX_HTTP_SSL)
  1762.         uwcf->ssl = 1;
  1763. #endif

  1764.         return NGX_CONF_OK;
  1765.     }

  1766.     if (ngx_strncasecmp(url->data, (u_char *) "uwsgi://", 8) == 0) {
  1767.         add = 8;

  1768.     } else if (ngx_strncasecmp(url->data, (u_char *) "suwsgi://", 9) == 0) {

  1769. #if (NGX_HTTP_SSL)
  1770.         add = 9;
  1771.         uwcf->ssl = 1;
  1772. #else
  1773.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1774.                            "suwsgi protocol requires SSL support");
  1775.         return NGX_CONF_ERROR;
  1776. #endif

  1777.     } else {
  1778.         add = 0;
  1779.     }

  1780.     ngx_memzero(&u, sizeof(ngx_url_t));

  1781.     u.url.len = url->len - add;
  1782.     u.url.data = url->data + add;
  1783.     u.no_resolve = 1;

  1784.     uwcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
  1785.     if (uwcf->upstream.upstream == NULL) {
  1786.         return NGX_CONF_ERROR;
  1787.     }

  1788.     if (clcf->name.len && clcf->name.data[clcf->name.len - 1] == '/') {
  1789.         clcf->auto_redirect = 1;
  1790.     }

  1791.     return NGX_CONF_OK;
  1792. }


  1793. static char *
  1794. ngx_http_uwsgi_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1795. {
  1796.     ngx_http_uwsgi_loc_conf_t *uwcf = conf;

  1797.     ngx_str_t                  *value;
  1798.     ngx_http_script_compile_t   sc;

  1799.     if (uwcf->upstream.store != NGX_CONF_UNSET) {
  1800.         return "is duplicate";
  1801.     }

  1802.     value = cf->args->elts;

  1803.     if (ngx_strcmp(value[1].data, "off") == 0) {
  1804.         uwcf->upstream.store = 0;
  1805.         return NGX_CONF_OK;
  1806.     }

  1807.     if (value[1].len == 0) {
  1808.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "empty path");
  1809.         return NGX_CONF_ERROR;
  1810.     }

  1811. #if (NGX_HTTP_CACHE)

  1812.     if (uwcf->upstream.cache > 0) {
  1813.         return "is incompatible with \"uwsgi_cache\"";
  1814.     }

  1815. #endif

  1816.     uwcf->upstream.store = 1;

  1817.     if (ngx_strcmp(value[1].data, "on") == 0) {
  1818.         return NGX_CONF_OK;
  1819.     }

  1820.     /* include the terminating '\0' into script */
  1821.     value[1].len++;

  1822.     ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));

  1823.     sc.cf = cf;
  1824.     sc.source = &value[1];
  1825.     sc.lengths = &uwcf->upstream.store_lengths;
  1826.     sc.values = &uwcf->upstream.store_values;
  1827.     sc.variables = ngx_http_script_variables_count(&value[1]);
  1828.     sc.complete_lengths = 1;
  1829.     sc.complete_values = 1;

  1830.     if (ngx_http_script_compile(&sc) != NGX_OK) {
  1831.         return NGX_CONF_ERROR;
  1832.     }

  1833.     return NGX_CONF_OK;
  1834. }


  1835. #if (NGX_HTTP_CACHE)

  1836. static char *
  1837. ngx_http_uwsgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1838. {
  1839.     ngx_http_uwsgi_loc_conf_t *uwcf = conf;

  1840.     ngx_str_t                         *value;
  1841.     ngx_http_complex_value_t           cv;
  1842.     ngx_http_compile_complex_value_t   ccv;

  1843.     value = cf->args->elts;

  1844.     if (uwcf->upstream.cache != NGX_CONF_UNSET) {
  1845.         return "is duplicate";
  1846.     }

  1847.     if (ngx_strcmp(value[1].data, "off") == 0) {
  1848.         uwcf->upstream.cache = 0;
  1849.         return NGX_CONF_OK;
  1850.     }

  1851.     if (uwcf->upstream.store > 0) {
  1852.         return "is incompatible with \"uwsgi_store\"";
  1853.     }

  1854.     uwcf->upstream.cache = 1;

  1855.     ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));

  1856.     ccv.cf = cf;
  1857.     ccv.value = &value[1];
  1858.     ccv.complex_value = &cv;

  1859.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  1860.         return NGX_CONF_ERROR;
  1861.     }

  1862.     if (cv.lengths != NULL) {

  1863.         uwcf->upstream.cache_value = ngx_palloc(cf->pool,
  1864.                                              sizeof(ngx_http_complex_value_t));
  1865.         if (uwcf->upstream.cache_value == NULL) {
  1866.             return NGX_CONF_ERROR;
  1867.         }

  1868.         *uwcf->upstream.cache_value = cv;

  1869.         return NGX_CONF_OK;
  1870.     }

  1871.     uwcf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0,
  1872.                                                       &ngx_http_uwsgi_module);
  1873.     if (uwcf->upstream.cache_zone == NULL) {
  1874.         return NGX_CONF_ERROR;
  1875.     }

  1876.     return NGX_CONF_OK;
  1877. }


  1878. static char *
  1879. ngx_http_uwsgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1880. {
  1881.     ngx_http_uwsgi_loc_conf_t *uwcf = conf;

  1882.     ngx_str_t                         *value;
  1883.     ngx_http_compile_complex_value_t   ccv;

  1884.     value = cf->args->elts;

  1885.     if (uwcf->cache_key.value.data) {
  1886.         return "is duplicate";
  1887.     }

  1888.     ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));

  1889.     ccv.cf = cf;
  1890.     ccv.value = &value[1];
  1891.     ccv.complex_value = &uwcf->cache_key;

  1892.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  1893.         return NGX_CONF_ERROR;
  1894.     }

  1895.     return NGX_CONF_OK;
  1896. }

  1897. #endif


  1898. #if (NGX_HTTP_SSL)

  1899. static char *
  1900. ngx_http_uwsgi_ssl_certificate_cache(ngx_conf_t *cf, ngx_command_t *cmd,
  1901.     void *conf)
  1902. {
  1903.     ngx_http_uwsgi_loc_conf_t *plcf = conf;

  1904.     time_t       inactive, valid;
  1905.     ngx_str_t   *value, s;
  1906.     ngx_int_t    max;
  1907.     ngx_uint_t   i;

  1908.     if (plcf->upstream.ssl_certificate_cache != NGX_CONF_UNSET_PTR) {
  1909.         return "is duplicate";
  1910.     }

  1911.     value = cf->args->elts;

  1912.     max = 0;
  1913.     inactive = 10;
  1914.     valid = 60;

  1915.     for (i = 1; i < cf->args->nelts; i++) {

  1916.         if (ngx_strncmp(value[i].data, "max=", 4) == 0) {

  1917.             max = ngx_atoi(value[i].data + 4, value[i].len - 4);
  1918.             if (max <= 0) {
  1919.                 goto failed;
  1920.             }

  1921.             continue;
  1922.         }

  1923.         if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {

  1924.             s.len = value[i].len - 9;
  1925.             s.data = value[i].data + 9;

  1926.             inactive = ngx_parse_time(&s, 1);
  1927.             if (inactive == (time_t) NGX_ERROR) {
  1928.                 goto failed;
  1929.             }

  1930.             continue;
  1931.         }

  1932.         if (ngx_strncmp(value[i].data, "valid=", 6) == 0) {

  1933.             s.len = value[i].len - 6;
  1934.             s.data = value[i].data + 6;

  1935.             valid = ngx_parse_time(&s, 1);
  1936.             if (valid == (time_t) NGX_ERROR) {
  1937.                 goto failed;
  1938.             }

  1939.             continue;
  1940.         }

  1941.         if (ngx_strcmp(value[i].data, "off") == 0) {

  1942.             plcf->upstream.ssl_certificate_cache = NULL;

  1943.             continue;
  1944.         }

  1945.     failed:

  1946.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1947.                            "invalid parameter \"%V\"", &value[i]);
  1948.         return NGX_CONF_ERROR;
  1949.     }

  1950.     if (plcf->upstream.ssl_certificate_cache == NULL) {
  1951.         return NGX_CONF_OK;
  1952.     }

  1953.     if (max == 0) {
  1954.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1955.                            "\"uwsgi_ssl_certificate_cache\" must have "
  1956.                            "the \"max\" parameter");
  1957.         return NGX_CONF_ERROR;
  1958.     }

  1959.     plcf->upstream.ssl_certificate_cache = ngx_ssl_cache_init(cf->pool, max,
  1960.                                                               valid, inactive);
  1961.     if (plcf->upstream.ssl_certificate_cache == NULL) {
  1962.         return NGX_CONF_ERROR;
  1963.     }

  1964.     return NGX_CONF_OK;
  1965. }


  1966. static char *
  1967. ngx_http_uwsgi_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1968. {
  1969.     ngx_http_uwsgi_loc_conf_t *uwcf = conf;

  1970.     ngx_str_t  *value;

  1971.     if (uwcf->upstream.ssl_passwords != NGX_CONF_UNSET_PTR) {
  1972.         return "is duplicate";
  1973.     }

  1974.     value = cf->args->elts;

  1975.     uwcf->upstream.ssl_passwords = ngx_ssl_read_password_file(cf, &value[1]);

  1976.     if (uwcf->upstream.ssl_passwords == NULL) {
  1977.         return NGX_CONF_ERROR;
  1978.     }

  1979.     return NGX_CONF_OK;
  1980. }


  1981. static char *
  1982. ngx_http_uwsgi_ssl_conf_command_check(ngx_conf_t *cf, void *post, void *data)
  1983. {
  1984. #ifndef SSL_CONF_FLAG_FILE
  1985.     return "is not supported on this platform";
  1986. #else
  1987.     return NGX_CONF_OK;
  1988. #endif
  1989. }


  1990. static ngx_int_t
  1991. ngx_http_uwsgi_merge_ssl(ngx_conf_t *cf, ngx_http_uwsgi_loc_conf_t *conf,
  1992.     ngx_http_uwsgi_loc_conf_t *prev)
  1993. {
  1994.     ngx_uint_t  preserve;

  1995.     if (conf->ssl_protocols == 0
  1996.         && conf->ssl_ciphers.data == NULL
  1997.         && conf->upstream.ssl_certificate == NGX_CONF_UNSET_PTR
  1998.         && conf->upstream.ssl_certificate_key == NGX_CONF_UNSET_PTR
  1999.         && conf->upstream.ssl_passwords == NGX_CONF_UNSET_PTR
  2000.         && conf->upstream.ssl_verify == NGX_CONF_UNSET
  2001.         && conf->ssl_verify_depth == NGX_CONF_UNSET_UINT
  2002.         && conf->ssl_trusted_certificate.data == NULL
  2003.         && conf->ssl_crl.data == NULL
  2004.         && conf->upstream.ssl_session_reuse == NGX_CONF_UNSET
  2005.         && conf->ssl_conf_commands == NGX_CONF_UNSET_PTR)
  2006.     {
  2007.         if (prev->upstream.ssl) {
  2008.             conf->upstream.ssl = prev->upstream.ssl;
  2009.             return NGX_OK;
  2010.         }

  2011.         preserve = 1;

  2012.     } else {
  2013.         preserve = 0;
  2014.     }

  2015.     conf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
  2016.     if (conf->upstream.ssl == NULL) {
  2017.         return NGX_ERROR;
  2018.     }

  2019.     conf->upstream.ssl->log = cf->log;

  2020.     /*
  2021.      * special handling to preserve conf->upstream.ssl
  2022.      * in the "http" section to inherit it to all servers
  2023.      */

  2024.     if (preserve) {
  2025.         prev->upstream.ssl = conf->upstream.ssl;
  2026.     }

  2027.     return NGX_OK;
  2028. }


  2029. static ngx_int_t
  2030. ngx_http_uwsgi_set_ssl(ngx_conf_t *cf, ngx_http_uwsgi_loc_conf_t *uwcf)
  2031. {
  2032.     ngx_pool_cleanup_t  *cln;

  2033.     if (uwcf->upstream.ssl->ctx) {
  2034.         return NGX_OK;
  2035.     }

  2036.     if (ngx_ssl_create(uwcf->upstream.ssl, uwcf->ssl_protocols, NULL)
  2037.         != NGX_OK)
  2038.     {
  2039.         return NGX_ERROR;
  2040.     }

  2041.     cln = ngx_pool_cleanup_add(cf->pool, 0);
  2042.     if (cln == NULL) {
  2043.         ngx_ssl_cleanup_ctx(uwcf->upstream.ssl);
  2044.         return NGX_ERROR;
  2045.     }

  2046.     cln->handler = ngx_ssl_cleanup_ctx;
  2047.     cln->data = uwcf->upstream.ssl;

  2048.     if (ngx_ssl_ciphers(cf, uwcf->upstream.ssl, &uwcf->ssl_ciphers, 0)
  2049.         != NGX_OK)
  2050.     {
  2051.         return NGX_ERROR;
  2052.     }

  2053.     if (uwcf->upstream.ssl_certificate
  2054.         && uwcf->upstream.ssl_certificate->value.len)
  2055.     {
  2056.         if (uwcf->upstream.ssl_certificate_key == NULL) {
  2057.             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  2058.                           "no \"uwsgi_ssl_certificate_key\" is defined "
  2059.                           "for certificate \"%V\"",
  2060.                           &uwcf->upstream.ssl_certificate->value);
  2061.             return NGX_ERROR;
  2062.         }

  2063.         if (uwcf->upstream.ssl_certificate->lengths == NULL
  2064.             && uwcf->upstream.ssl_certificate_key->lengths == NULL)
  2065.         {
  2066.             if (ngx_ssl_certificate(cf, uwcf->upstream.ssl,
  2067.                                     &uwcf->upstream.ssl_certificate->value,
  2068.                                     &uwcf->upstream.ssl_certificate_key->value,
  2069.                                     uwcf->upstream.ssl_passwords)
  2070.                 != NGX_OK)
  2071.             {
  2072.                 return NGX_ERROR;
  2073.             }
  2074.         }
  2075.     }

  2076.     if (uwcf->upstream.ssl_verify) {
  2077.         if (uwcf->ssl_trusted_certificate.len == 0) {
  2078.             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  2079.                       "no uwsgi_ssl_trusted_certificate for uwsgi_ssl_verify");
  2080.             return NGX_ERROR;
  2081.         }

  2082.         if (ngx_ssl_trusted_certificate(cf, uwcf->upstream.ssl,
  2083.                                         &uwcf->ssl_trusted_certificate,
  2084.                                         uwcf->ssl_verify_depth)
  2085.             != NGX_OK)
  2086.         {
  2087.             return NGX_ERROR;
  2088.         }

  2089.         if (ngx_ssl_crl(cf, uwcf->upstream.ssl, &uwcf->ssl_crl) != NGX_OK) {
  2090.             return NGX_ERROR;
  2091.         }
  2092.     }

  2093.     if (ngx_ssl_client_session_cache(cf, uwcf->upstream.ssl,
  2094.                                      uwcf->upstream.ssl_session_reuse)
  2095.         != NGX_OK)
  2096.     {
  2097.         return NGX_ERROR;
  2098.     }

  2099.     if (ngx_ssl_conf_commands(cf, uwcf->upstream.ssl, uwcf->ssl_conf_commands)
  2100.         != NGX_OK)
  2101.     {
  2102.         return NGX_ERROR;
  2103.     }

  2104.     return NGX_OK;
  2105. }

  2106. #endif