src/http/modules/ngx_http_scgi_module.c - nginx-1.31.3 nginx/ @ 42f8df65b

Global variables defined

Data types defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. * Copyright (C) Manlio Perillo (manlio.perillo@gmail.com)
  5. */


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


  9. typedef struct {
  10.     ngx_array_t                caches;  /* ngx_http_file_cache_t * */
  11. } ngx_http_scgi_main_conf_t;


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


  19. typedef struct {
  20.     ngx_http_upstream_conf_t   upstream;

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

  26.     ngx_array_t               *scgi_lengths;
  27.     ngx_array_t               *scgi_values;

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


  32. static ngx_int_t ngx_http_scgi_eval(ngx_http_request_t *r,
  33.     ngx_http_scgi_loc_conf_t *scf);
  34. static ngx_int_t ngx_http_scgi_create_request(ngx_http_request_t *r);
  35. static ngx_int_t ngx_http_scgi_reinit_request(ngx_http_request_t *r);
  36. static ngx_int_t ngx_http_scgi_process_status_line(ngx_http_request_t *r);
  37. static ngx_int_t ngx_http_scgi_process_header(ngx_http_request_t *r);
  38. static ngx_int_t ngx_http_scgi_input_filter_init(void *data);
  39. static void ngx_http_scgi_abort_request(ngx_http_request_t *r);
  40. static void ngx_http_scgi_finalize_request(ngx_http_request_t *r, ngx_int_t rc);

  41. static void *ngx_http_scgi_create_main_conf(ngx_conf_t *cf);
  42. static void *ngx_http_scgi_create_loc_conf(ngx_conf_t *cf);
  43. static char *ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent,
  44.     void *child);
  45. static ngx_int_t ngx_http_scgi_init_params(ngx_conf_t *cf,
  46.     ngx_http_scgi_loc_conf_t *conf, ngx_http_scgi_params_t *params,
  47.     ngx_keyval_t *default_params);

  48. static char *ngx_http_scgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  49. static char *ngx_http_scgi_store(ngx_conf_t *cf, ngx_command_t *cmd,
  50.     void *conf);

  51. #if (NGX_HTTP_CACHE)
  52. static ngx_int_t ngx_http_scgi_create_key(ngx_http_request_t *r);
  53. static char *ngx_http_scgi_cache(ngx_conf_t *cf, ngx_command_t *cmd,
  54.     void *conf);
  55. static char *ngx_http_scgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd,
  56.     void *conf);
  57. #endif


  58. static ngx_conf_bitmask_t ngx_http_scgi_next_upstream_masks[] = {
  59.     { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
  60.     { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
  61.     { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
  62.     { ngx_string("non_idempotent"), NGX_HTTP_UPSTREAM_FT_NON_IDEMPOTENT },
  63.     { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
  64.     { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
  65.     { ngx_string("http_403"), NGX_HTTP_UPSTREAM_FT_HTTP_403 },
  66.     { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
  67.     { ngx_string("http_429"), NGX_HTTP_UPSTREAM_FT_HTTP_429 },
  68.     { ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING },
  69.     { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
  70.     { ngx_null_string, 0 }
  71. };


  72. ngx_module_t  ngx_http_scgi_module;


  73. static ngx_command_t ngx_http_scgi_commands[] = {

  74.     { ngx_string("scgi_pass"),
  75.       NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
  76.       ngx_http_scgi_pass,
  77.       NGX_HTTP_LOC_CONF_OFFSET,
  78.       0,
  79.       NULL },

  80.     { ngx_string("scgi_store"),
  81.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  82.       ngx_http_scgi_store,
  83.       NGX_HTTP_LOC_CONF_OFFSET,
  84.       0,
  85.       NULL },

  86.     { ngx_string("scgi_store_access"),
  87.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
  88.       ngx_conf_set_access_slot,
  89.       NGX_HTTP_LOC_CONF_OFFSET,
  90.       offsetof(ngx_http_scgi_loc_conf_t, upstream.store_access),
  91.       NULL },

  92.     { ngx_string("scgi_buffering"),
  93.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  94.       ngx_conf_set_flag_slot,
  95.       NGX_HTTP_LOC_CONF_OFFSET,
  96.       offsetof(ngx_http_scgi_loc_conf_t, upstream.buffering),
  97.       NULL },

  98.     { ngx_string("scgi_request_buffering"),
  99.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  100.       ngx_conf_set_flag_slot,
  101.       NGX_HTTP_LOC_CONF_OFFSET,
  102.       offsetof(ngx_http_scgi_loc_conf_t, upstream.request_buffering),
  103.       NULL },

  104.     { ngx_string("scgi_ignore_client_abort"),
  105.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  106.       ngx_conf_set_flag_slot,
  107.       NGX_HTTP_LOC_CONF_OFFSET,
  108.       offsetof(ngx_http_scgi_loc_conf_t, upstream.ignore_client_abort),
  109.       NULL },

  110.     { ngx_string("scgi_bind"),
  111.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
  112.       ngx_http_upstream_bind_set_slot,
  113.       NGX_HTTP_LOC_CONF_OFFSET,
  114.       offsetof(ngx_http_scgi_loc_conf_t, upstream.local),
  115.       NULL },

  116.     { ngx_string("scgi_socket_keepalive"),
  117.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  118.       ngx_conf_set_flag_slot,
  119.       NGX_HTTP_LOC_CONF_OFFSET,
  120.       offsetof(ngx_http_scgi_loc_conf_t, upstream.socket_keepalive),
  121.       NULL },

  122.     { ngx_string("scgi_connect_timeout"),
  123.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  124.       ngx_conf_set_msec_slot,
  125.       NGX_HTTP_LOC_CONF_OFFSET,
  126.       offsetof(ngx_http_scgi_loc_conf_t, upstream.connect_timeout),
  127.       NULL },

  128.     { ngx_string("scgi_send_timeout"),
  129.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  130.       ngx_conf_set_msec_slot,
  131.       NGX_HTTP_LOC_CONF_OFFSET,
  132.       offsetof(ngx_http_scgi_loc_conf_t, upstream.send_timeout),
  133.       NULL },

  134.     { ngx_string("scgi_buffer_size"),
  135.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  136.       ngx_conf_set_size_slot,
  137.       NGX_HTTP_LOC_CONF_OFFSET,
  138.       offsetof(ngx_http_scgi_loc_conf_t, upstream.buffer_size),
  139.       NULL },

  140.     { ngx_string("scgi_pass_request_headers"),
  141.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  142.       ngx_conf_set_flag_slot,
  143.       NGX_HTTP_LOC_CONF_OFFSET,
  144.       offsetof(ngx_http_scgi_loc_conf_t, upstream.pass_request_headers),
  145.       NULL },

  146.     { ngx_string("scgi_pass_request_body"),
  147.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  148.       ngx_conf_set_flag_slot,
  149.       NGX_HTTP_LOC_CONF_OFFSET,
  150.       offsetof(ngx_http_scgi_loc_conf_t, upstream.pass_request_body),
  151.       NULL },

  152.     { ngx_string("scgi_intercept_errors"),
  153.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  154.       ngx_conf_set_flag_slot,
  155.       NGX_HTTP_LOC_CONF_OFFSET,
  156.       offsetof(ngx_http_scgi_loc_conf_t, upstream.intercept_errors),
  157.       NULL },

  158.     { ngx_string("scgi_read_timeout"),
  159.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  160.       ngx_conf_set_msec_slot,
  161.       NGX_HTTP_LOC_CONF_OFFSET,
  162.       offsetof(ngx_http_scgi_loc_conf_t, upstream.read_timeout),
  163.       NULL },

  164.     { ngx_string("scgi_buffers"),
  165.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
  166.       ngx_conf_set_bufs_slot,
  167.       NGX_HTTP_LOC_CONF_OFFSET,
  168.       offsetof(ngx_http_scgi_loc_conf_t, upstream.bufs),
  169.       NULL },

  170.     { ngx_string("scgi_busy_buffers_size"),
  171.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  172.       ngx_conf_set_size_slot,
  173.       NGX_HTTP_LOC_CONF_OFFSET,
  174.       offsetof(ngx_http_scgi_loc_conf_t, upstream.busy_buffers_size_conf),
  175.       NULL },

  176.     { ngx_string("scgi_force_ranges"),
  177.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  178.       ngx_conf_set_flag_slot,
  179.       NGX_HTTP_LOC_CONF_OFFSET,
  180.       offsetof(ngx_http_scgi_loc_conf_t, upstream.force_ranges),
  181.       NULL },

  182.     { ngx_string("scgi_limit_rate"),
  183.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  184.       ngx_http_set_complex_value_size_slot,
  185.       NGX_HTTP_LOC_CONF_OFFSET,
  186.       offsetof(ngx_http_scgi_loc_conf_t, upstream.limit_rate),
  187.       NULL },

  188. #if (NGX_HTTP_CACHE)

  189.     { ngx_string("scgi_cache"),
  190.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  191.       ngx_http_scgi_cache,
  192.       NGX_HTTP_LOC_CONF_OFFSET,
  193.       0,
  194.       NULL },

  195.     { ngx_string("scgi_cache_key"),
  196.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  197.       ngx_http_scgi_cache_key,
  198.       NGX_HTTP_LOC_CONF_OFFSET,
  199.       0,
  200.       NULL },

  201.     { ngx_string("scgi_cache_path"),
  202.       NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
  203.       ngx_http_file_cache_set_slot,
  204.       NGX_HTTP_MAIN_CONF_OFFSET,
  205.       offsetof(ngx_http_scgi_main_conf_t, caches),
  206.       &ngx_http_scgi_module },

  207.     { ngx_string("scgi_cache_bypass"),
  208.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  209.       ngx_http_set_predicate_slot,
  210.       NGX_HTTP_LOC_CONF_OFFSET,
  211.       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_bypass),
  212.       NULL },

  213.     { ngx_string("scgi_no_cache"),
  214.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  215.       ngx_http_set_predicate_slot,
  216.       NGX_HTTP_LOC_CONF_OFFSET,
  217.       offsetof(ngx_http_scgi_loc_conf_t, upstream.no_cache),
  218.       NULL },

  219.     { ngx_string("scgi_cache_valid"),
  220.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  221.       ngx_http_file_cache_valid_set_slot,
  222.       NGX_HTTP_LOC_CONF_OFFSET,
  223.       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_valid),
  224.       NULL },

  225.     { ngx_string("scgi_cache_min_uses"),
  226.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  227.       ngx_conf_set_num_slot,
  228.       NGX_HTTP_LOC_CONF_OFFSET,
  229.       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_min_uses),
  230.       NULL },

  231.     { ngx_string("scgi_cache_max_range_offset"),
  232.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  233.       ngx_conf_set_off_slot,
  234.       NGX_HTTP_LOC_CONF_OFFSET,
  235.       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_max_range_offset),
  236.       NULL },

  237.     { ngx_string("scgi_cache_use_stale"),
  238.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  239.       ngx_conf_set_bitmask_slot,
  240.       NGX_HTTP_LOC_CONF_OFFSET,
  241.       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_use_stale),
  242.       &ngx_http_scgi_next_upstream_masks },

  243.     { ngx_string("scgi_cache_methods"),
  244.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  245.       ngx_conf_set_bitmask_slot,
  246.       NGX_HTTP_LOC_CONF_OFFSET,
  247.       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_methods),
  248.       &ngx_http_upstream_cache_method_mask },

  249.     { ngx_string("scgi_cache_lock"),
  250.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  251.       ngx_conf_set_flag_slot,
  252.       NGX_HTTP_LOC_CONF_OFFSET,
  253.       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_lock),
  254.       NULL },

  255.     { ngx_string("scgi_cache_lock_timeout"),
  256.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  257.       ngx_conf_set_msec_slot,
  258.       NGX_HTTP_LOC_CONF_OFFSET,
  259.       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_lock_timeout),
  260.       NULL },

  261.     { ngx_string("scgi_cache_lock_age"),
  262.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  263.       ngx_conf_set_msec_slot,
  264.       NGX_HTTP_LOC_CONF_OFFSET,
  265.       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_lock_age),
  266.       NULL },

  267.     { ngx_string("scgi_cache_revalidate"),
  268.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  269.       ngx_conf_set_flag_slot,
  270.       NGX_HTTP_LOC_CONF_OFFSET,
  271.       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_revalidate),
  272.       NULL },

  273.     { ngx_string("scgi_cache_background_update"),
  274.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  275.       ngx_conf_set_flag_slot,
  276.       NGX_HTTP_LOC_CONF_OFFSET,
  277.       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_background_update),
  278.       NULL },

  279. #endif

  280.     { ngx_string("scgi_temp_path"),
  281.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
  282.       ngx_conf_set_path_slot,
  283.       NGX_HTTP_LOC_CONF_OFFSET,
  284.       offsetof(ngx_http_scgi_loc_conf_t, upstream.temp_path),
  285.       NULL },

  286.     { ngx_string("scgi_max_temp_file_size"),
  287.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  288.       ngx_conf_set_size_slot,
  289.       NGX_HTTP_LOC_CONF_OFFSET,
  290.       offsetof(ngx_http_scgi_loc_conf_t, upstream.max_temp_file_size_conf),
  291.       NULL },

  292.     { ngx_string("scgi_temp_file_write_size"),
  293.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  294.       ngx_conf_set_size_slot,
  295.       NGX_HTTP_LOC_CONF_OFFSET,
  296.       offsetof(ngx_http_scgi_loc_conf_t, upstream.temp_file_write_size_conf),
  297.       NULL },

  298.     { ngx_string("scgi_next_upstream"),
  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_scgi_loc_conf_t, upstream.next_upstream),
  303.       &ngx_http_scgi_next_upstream_masks },

  304.     { ngx_string("scgi_next_upstream_tries"),
  305.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  306.       ngx_conf_set_num_slot,
  307.       NGX_HTTP_LOC_CONF_OFFSET,
  308.       offsetof(ngx_http_scgi_loc_conf_t, upstream.next_upstream_tries),
  309.       NULL },

  310.     { ngx_string("scgi_next_upstream_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_scgi_loc_conf_t, upstream.next_upstream_timeout),
  315.       NULL },

  316.     { ngx_string("scgi_param"),
  317.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE23,
  318.       ngx_http_upstream_param_set_slot,
  319.       NGX_HTTP_LOC_CONF_OFFSET,
  320.       offsetof(ngx_http_scgi_loc_conf_t, params_source),
  321.       NULL },

  322.     { ngx_string("scgi_pass_header"),
  323.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  324.       ngx_conf_set_str_array_slot,
  325.       NGX_HTTP_LOC_CONF_OFFSET,
  326.       offsetof(ngx_http_scgi_loc_conf_t, upstream.pass_headers),
  327.       NULL },

  328.     { ngx_string("scgi_hide_header"),
  329.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  330.       ngx_conf_set_str_array_slot,
  331.       NGX_HTTP_LOC_CONF_OFFSET,
  332.       offsetof(ngx_http_scgi_loc_conf_t, upstream.hide_headers),
  333.       NULL },

  334.     { ngx_string("scgi_ignore_headers"),
  335.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  336.       ngx_conf_set_bitmask_slot,
  337.       NGX_HTTP_LOC_CONF_OFFSET,
  338.       offsetof(ngx_http_scgi_loc_conf_t, upstream.ignore_headers),
  339.       &ngx_http_upstream_ignore_headers_masks },

  340.       ngx_null_command
  341. };


  342. static ngx_http_module_t ngx_http_scgi_module_ctx = {
  343.     NULL,                                  /* preconfiguration */
  344.     NULL,                                  /* postconfiguration */

  345.     ngx_http_scgi_create_main_conf,        /* create main configuration */
  346.     NULL,                                  /* init main configuration */

  347.     NULL,                                  /* create server configuration */
  348.     NULL,                                  /* merge server configuration */

  349.     ngx_http_scgi_create_loc_conf,         /* create location configuration */
  350.     ngx_http_scgi_merge_loc_conf           /* merge location configuration */
  351. };


  352. ngx_module_t ngx_http_scgi_module = {
  353.     NGX_MODULE_V1,
  354.     &ngx_http_scgi_module_ctx,             /* module context */
  355.     ngx_http_scgi_commands,                /* module directives */
  356.     NGX_HTTP_MODULE,                       /* module type */
  357.     NULL,                                  /* init master */
  358.     NULL,                                  /* init module */
  359.     NULL,                                  /* init process */
  360.     NULL,                                  /* init thread */
  361.     NULL,                                  /* exit thread */
  362.     NULL,                                  /* exit process */
  363.     NULL,                                  /* exit master */
  364.     NGX_MODULE_V1_PADDING
  365. };


  366. static ngx_str_t ngx_http_scgi_hide_headers[] = {
  367.     ngx_string("Status"),
  368.     ngx_string("X-Accel-Expires"),
  369.     ngx_string("X-Accel-Redirect"),
  370.     ngx_string("X-Accel-Limit-Rate"),
  371.     ngx_string("X-Accel-Buffering"),
  372.     ngx_string("X-Accel-Charset"),
  373.     ngx_null_string
  374. };


  375. static ngx_keyval_t  ngx_http_scgi_headers[] = {
  376.     { ngx_string("HTTP_HOST"),
  377.       ngx_string("$host$is_request_port$request_port") },
  378.     { ngx_null_string, ngx_null_string }
  379. };


  380. #if (NGX_HTTP_CACHE)

  381. static ngx_keyval_t  ngx_http_scgi_cache_headers[] = {
  382.     { ngx_string("HTTP_HOST"),
  383.       ngx_string("$host$is_request_port$request_port") },
  384.     { ngx_string("HTTP_IF_MODIFIED_SINCE"),
  385.       ngx_string("$upstream_cache_last_modified") },
  386.     { ngx_string("HTTP_IF_UNMODIFIED_SINCE"), ngx_string("") },
  387.     { ngx_string("HTTP_IF_NONE_MATCH"), ngx_string("$upstream_cache_etag") },
  388.     { ngx_string("HTTP_IF_MATCH"), ngx_string("") },
  389.     { ngx_string("HTTP_RANGE"), ngx_string("") },
  390.     { ngx_string("HTTP_IF_RANGE"), ngx_string("") },
  391.     { ngx_null_string, ngx_null_string }
  392. };

  393. #endif


  394. static ngx_path_init_t ngx_http_scgi_temp_path = {
  395.     ngx_string(NGX_HTTP_SCGI_TEMP_PATH), { 1, 2, 0 }
  396. };


  397. static ngx_int_t
  398. ngx_http_scgi_handler(ngx_http_request_t *r)
  399. {
  400.     ngx_int_t                   rc;
  401.     ngx_http_status_t          *status;
  402.     ngx_http_upstream_t        *u;
  403.     ngx_http_scgi_loc_conf_t   *scf;
  404. #if (NGX_HTTP_CACHE)
  405.     ngx_http_scgi_main_conf_t  *smcf;
  406. #endif

  407.     if (ngx_http_upstream_create(r) != NGX_OK) {
  408.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  409.     }

  410.     status = ngx_pcalloc(r->pool, sizeof(ngx_http_status_t));
  411.     if (status == NULL) {
  412.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  413.     }

  414.     ngx_http_set_ctx(r, status, ngx_http_scgi_module);

  415.     scf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module);

  416.     if (scf->scgi_lengths) {
  417.         if (ngx_http_scgi_eval(r, scf) != NGX_OK) {
  418.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  419.         }
  420.     }

  421.     u = r->upstream;

  422.     ngx_str_set(&u->schema, "scgi://");
  423.     u->output.tag = (ngx_buf_tag_t) &ngx_http_scgi_module;

  424.     u->conf = &scf->upstream;

  425. #if (NGX_HTTP_CACHE)
  426.     smcf = ngx_http_get_module_main_conf(r, ngx_http_scgi_module);

  427.     u->caches = &smcf->caches;
  428.     u->create_key = ngx_http_scgi_create_key;
  429. #endif

  430.     u->create_request = ngx_http_scgi_create_request;
  431.     u->reinit_request = ngx_http_scgi_reinit_request;
  432.     u->process_header = ngx_http_scgi_process_status_line;
  433.     u->abort_request = ngx_http_scgi_abort_request;
  434.     u->finalize_request = ngx_http_scgi_finalize_request;
  435.     r->state = 0;

  436.     u->buffering = scf->upstream.buffering;

  437.     u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
  438.     if (u->pipe == NULL) {
  439.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  440.     }

  441.     u->pipe->input_filter = ngx_event_pipe_copy_input_filter;
  442.     u->pipe->input_ctx = r;

  443.     u->input_filter_init = ngx_http_scgi_input_filter_init;
  444.     u->input_filter = ngx_http_upstream_non_buffered_filter;
  445.     u->input_filter_ctx = r;

  446.     if (!scf->upstream.request_buffering
  447.         && scf->upstream.pass_request_body
  448.         && !r->headers_in.chunked)
  449.     {
  450.         r->request_body_no_buffering = 1;
  451.     }

  452.     rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);

  453.     if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
  454.         return rc;
  455.     }

  456.     return NGX_DONE;
  457. }


  458. static ngx_int_t
  459. ngx_http_scgi_eval(ngx_http_request_t *r, ngx_http_scgi_loc_conf_t * scf)
  460. {
  461.     ngx_url_t             url;
  462.     ngx_http_upstream_t  *u;

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

  464.     if (ngx_http_script_run(r, &url.url, scf->scgi_lengths->elts, 0,
  465.                             scf->scgi_values->elts)
  466.         == NULL)
  467.     {
  468.         return NGX_ERROR;
  469.     }

  470.     url.no_resolve = 1;

  471.     if (ngx_parse_url(r->pool, &url) != NGX_OK) {
  472.         if (url.err) {
  473.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  474.                           "%s in upstream \"%V\"", url.err, &url.url);
  475.         }

  476.         return NGX_ERROR;
  477.     }

  478.     u = r->upstream;

  479.     u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
  480.     if (u->resolved == NULL) {
  481.         return NGX_ERROR;
  482.     }

  483.     if (url.addrs) {
  484.         u->resolved->sockaddr = url.addrs[0].sockaddr;
  485.         u->resolved->socklen = url.addrs[0].socklen;
  486.         u->resolved->name = url.addrs[0].name;
  487.         u->resolved->naddrs = 1;
  488.     }

  489.     u->resolved->host = url.host;
  490.     u->resolved->port = url.port;
  491.     u->resolved->no_port = url.no_port;

  492.     return NGX_OK;
  493. }


  494. #if (NGX_HTTP_CACHE)

  495. static ngx_int_t
  496. ngx_http_scgi_create_key(ngx_http_request_t *r)
  497. {
  498.     ngx_str_t                 *key;
  499.     ngx_http_scgi_loc_conf_t  *scf;

  500.     key = ngx_array_push(&r->cache->keys);
  501.     if (key == NULL) {
  502.         return NGX_ERROR;
  503.     }

  504.     scf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module);

  505.     if (ngx_http_complex_value(r, &scf->cache_key, key) != NGX_OK) {
  506.         return NGX_ERROR;
  507.     }

  508.     return NGX_OK;
  509. }

  510. #endif


  511. static ngx_int_t
  512. ngx_http_scgi_create_request(ngx_http_request_t *r)
  513. {
  514.     off_t                         content_length_n;
  515.     u_char                        ch, sep, *key, *val, *lowcase_key;
  516.     size_t                        len, key_len, val_len, allocated;
  517.     ngx_buf_t                    *b;
  518.     ngx_str_t                     content_length;
  519.     ngx_uint_t                    i, n, hash, skip_empty, header_params;
  520.     ngx_chain_t                  *cl, *body;
  521.     ngx_list_part_t              *part;
  522.     ngx_table_elt_t              *header, *hn, **ignored;
  523.     ngx_http_scgi_params_t       *params;
  524.     ngx_http_script_code_pt       code;
  525.     ngx_http_script_engine_t      e, le;
  526.     ngx_http_scgi_loc_conf_t     *scf;
  527.     ngx_http_script_len_code_pt   lcode;
  528.     u_char                        buffer[NGX_OFF_T_LEN];

  529.     content_length_n = 0;

  530.     if (r->headers_in.content_length_n > 0) {
  531.         content_length_n = r->headers_in.content_length_n;
  532.     }

  533.     content_length.data = buffer;
  534.     content_length.len = ngx_sprintf(buffer, "%O", content_length_n) - buffer;

  535.     len = sizeof("CONTENT_LENGTH") + content_length.len + 1;

  536.     header_params = 0;
  537.     ignored = NULL;

  538.     scf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module);

  539. #if (NGX_HTTP_CACHE)
  540.     params = r->upstream->cacheable ? &scf->params_cache : &scf->params;
  541. #else
  542.     params = &scf->params;
  543. #endif

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

  546.         ngx_http_script_flush_no_cacheable_variables(r, params->flushes);
  547.         le.flushed = 1;

  548.         le.ip = params->lengths->elts;
  549.         le.request = r;

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

  551.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  552.             key_len = lcode(&le);

  553.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  554.             skip_empty = lcode(&le);

  555.             for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
  556.                 lcode = *(ngx_http_script_len_code_pt *) le.ip;
  557.             }
  558.             le.ip += sizeof(uintptr_t);

  559.             if (skip_empty && val_len == 0) {
  560.                 continue;
  561.             }

  562.             len += key_len + val_len + 1;
  563.         }
  564.     }

  565.     if (scf->upstream.pass_request_headers) {

  566.         allocated = 0;
  567.         lowcase_key = NULL;

  568.         if (ngx_http_link_multi_headers(r) != NGX_OK) {
  569.             return NGX_ERROR;
  570.         }

  571.         if (params->number || r->headers_in.multi) {
  572.             n = 0;
  573.             part = &r->headers_in.headers.part;

  574.             while (part) {
  575.                 n += part->nelts;
  576.                 part = part->next;
  577.             }

  578.             ignored = ngx_palloc(r->pool, n * sizeof(void *));
  579.             if (ignored == NULL) {
  580.                 return NGX_ERROR;
  581.             }
  582.         }

  583.         part = &r->headers_in.headers.part;
  584.         header = part->elts;

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

  586.             if (i >= part->nelts) {
  587.                 if (part->next == NULL) {
  588.                     break;
  589.                 }

  590.                 part = part->next;
  591.                 header = part->elts;
  592.                 i = 0;
  593.             }

  594.             for (n = 0; n < header_params; n++) {
  595.                 if (&header[i] == ignored[n]) {
  596.                     goto next_length;
  597.                 }
  598.             }

  599.             if (params->number) {
  600.                 if (allocated < header[i].key.len) {
  601.                     allocated = header[i].key.len + 16;
  602.                     lowcase_key = ngx_pnalloc(r->pool, allocated);
  603.                     if (lowcase_key == NULL) {
  604.                         return NGX_ERROR;
  605.                     }
  606.                 }

  607.                 hash = 0;

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

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

  612.                     } else if (ch == '-') {
  613.                         ch = '_';
  614.                     }

  615.                     hash = ngx_hash(hash, ch);
  616.                     lowcase_key[n] = ch;
  617.                 }

  618.                 if (ngx_hash_find(&params->hash, hash, lowcase_key, n)) {
  619.                     ignored[header_params++] = &header[i];
  620.                     continue;
  621.                 }
  622.             }

  623.             len += sizeof("HTTP_") - 1 + header[i].key.len + 1
  624.                 + header[i].value.len + 1;

  625.             for (hn = header[i].next; hn; hn = hn->next) {
  626.                 len += hn->value.len + 2;
  627.                 ignored[header_params++] = hn;
  628.             }

  629.         next_length:

  630.             continue;
  631.         }
  632.     }

  633.     /* netstring: "length:" + packet + "," */

  634.     b = ngx_create_temp_buf(r->pool, NGX_SIZE_T_LEN + 1 + len + 1);
  635.     if (b == NULL) {
  636.         return NGX_ERROR;
  637.     }

  638.     cl = ngx_alloc_chain_link(r->pool);
  639.     if (cl == NULL) {
  640.         return NGX_ERROR;
  641.     }

  642.     cl->buf = b;

  643.     b->last = ngx_sprintf(b->last, "%ui:CONTENT_LENGTH%Z%V%Z",
  644.                           len, &content_length);

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

  647.         e.ip = params->values->elts;
  648.         e.pos = b->last;
  649.         e.request = r;
  650.         e.flushed = 1;

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

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

  653.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  654.             lcode(&le); /* key length */

  655.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  656.             skip_empty = lcode(&le);

  657.             for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
  658.                 lcode = *(ngx_http_script_len_code_pt *) le.ip;
  659.             }
  660.             le.ip += sizeof(uintptr_t);

  661.             if (skip_empty && val_len == 0) {
  662.                 e.skip = 1;

  663.                 while (*(uintptr_t *) e.ip) {
  664.                     code = *(ngx_http_script_code_pt *) e.ip;
  665.                     code((ngx_http_script_engine_t *) &e);
  666.                 }
  667.                 e.ip += sizeof(uintptr_t);

  668.                 e.skip = 0;

  669.                 continue;
  670.             }

  671. #if (NGX_DEBUG)
  672.             key = e.pos;
  673. #endif
  674.             code = *(ngx_http_script_code_pt *) e.ip;
  675.             code((ngx_http_script_engine_t *) &e);

  676. #if (NGX_DEBUG)
  677.             val = e.pos;
  678. #endif
  679.             while (*(uintptr_t *) e.ip) {
  680.                 code = *(ngx_http_script_code_pt *) e.ip;
  681.                 code((ngx_http_script_engine_t *) &e);
  682.             }
  683.             *e.pos++ = '\0';
  684.             e.ip += sizeof(uintptr_t);

  685.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  686.                            "scgi param: \"%s: %s\"", key, val);
  687.         }

  688.         b->last = e.pos;
  689.     }

  690.     if (scf->upstream.pass_request_headers) {

  691.         part = &r->headers_in.headers.part;
  692.         header = part->elts;

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

  694.             if (i >= part->nelts) {
  695.                 if (part->next == NULL) {
  696.                     break;
  697.                 }

  698.                 part = part->next;
  699.                 header = part->elts;
  700.                 i = 0;
  701.             }

  702.             for (n = 0; n < header_params; n++) {
  703.                 if (&header[i] == ignored[n]) {
  704.                     goto next_value;
  705.                 }
  706.             }

  707.             key = b->last;
  708.             b->last = ngx_cpymem(key, "HTTP_", sizeof("HTTP_") - 1);

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

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

  713.                 } else if (ch == '-') {
  714.                     ch = '_';
  715.                 }

  716.                 *b->last++ = ch;
  717.             }

  718.             *b->last++ = (u_char) 0;

  719.             val = b->last;
  720.             b->last = ngx_copy(val, header[i].value.data, header[i].value.len);

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

  722.                 if (header[i].key.len == sizeof("Cookie") - 1
  723.                     && ngx_strncasecmp(header[i].key.data, (u_char *) "Cookie",
  724.                                        sizeof("Cookie") - 1)
  725.                        == 0)
  726.                 {
  727.                     sep = ';';

  728.                 } else {
  729.                     sep = ',';
  730.                 }

  731.                 for (hn = header[i].next; hn; hn = hn->next) {
  732.                     *b->last++ = sep;
  733.                     *b->last++ = ' ';
  734.                     b->last = ngx_copy(b->last, hn->value.data, hn->value.len);
  735.                 }
  736.             }

  737.             *b->last++ = (u_char) 0;

  738.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  739.                            "scgi param: \"%s: %s\"", key, val);

  740.         next_value:

  741.             continue;
  742.         }
  743.     }

  744.     *b->last++ = (u_char) ',';

  745.     if (r->request_body_no_buffering) {
  746.         r->upstream->request_bufs = cl;

  747.     } else if (scf->upstream.pass_request_body) {
  748.         body = r->upstream->request_bufs;
  749.         r->upstream->request_bufs = cl;

  750.         while (body) {
  751.             b = ngx_alloc_buf(r->pool);
  752.             if (b == NULL) {
  753.                 return NGX_ERROR;
  754.             }

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

  756.             cl->next = ngx_alloc_chain_link(r->pool);
  757.             if (cl->next == NULL) {
  758.                 return NGX_ERROR;
  759.             }

  760.             cl = cl->next;
  761.             cl->buf = b;

  762.             body = body->next;
  763.         }

  764.     } else {
  765.         r->upstream->request_bufs = cl;
  766.     }

  767.     cl->next = NULL;

  768.     return NGX_OK;
  769. }


  770. static ngx_int_t
  771. ngx_http_scgi_reinit_request(ngx_http_request_t *r)
  772. {
  773.     ngx_http_status_t  *status;

  774.     status = ngx_http_get_module_ctx(r, ngx_http_scgi_module);

  775.     if (status == NULL) {
  776.         return NGX_OK;
  777.     }

  778.     status->code = 0;
  779.     status->count = 0;
  780.     status->start = NULL;
  781.     status->end = NULL;

  782.     r->upstream->process_header = ngx_http_scgi_process_status_line;
  783.     r->state = 0;

  784.     return NGX_OK;
  785. }


  786. static ngx_int_t
  787. ngx_http_scgi_process_status_line(ngx_http_request_t *r)
  788. {
  789.     size_t                len;
  790.     ngx_int_t             rc;
  791.     ngx_http_status_t    *status;
  792.     ngx_http_upstream_t  *u;

  793.     status = ngx_http_get_module_ctx(r, ngx_http_scgi_module);

  794.     if (status == NULL) {
  795.         return NGX_ERROR;
  796.     }

  797.     u = r->upstream;

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

  799.     if (rc == NGX_AGAIN) {
  800.         return rc;
  801.     }

  802.     if (rc == NGX_ERROR) {
  803.         u->process_header = ngx_http_scgi_process_header;
  804.         u->buffer.pos = status->line_start;
  805.         r->state = 0;
  806.         return ngx_http_scgi_process_header(r);
  807.     }

  808.     if (u->state && u->state->status == 0) {
  809.         u->state->status = status->code;
  810.     }

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

  812.     len = status->end - status->start;
  813.     u->headers_in.status_line.len = len;

  814.     u->headers_in.status_line.data = ngx_pnalloc(r->pool, len);
  815.     if (u->headers_in.status_line.data == NULL) {
  816.         return NGX_ERROR;
  817.     }

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

  819.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  820.                    "http scgi status %ui \"%V\"",
  821.                    u->headers_in.status_n, &u->headers_in.status_line);

  822.     u->process_header = ngx_http_scgi_process_header;

  823.     return ngx_http_scgi_process_header(r);
  824. }


  825. static ngx_int_t
  826. ngx_http_scgi_process_header(ngx_http_request_t *r)
  827. {
  828.     ngx_str_t                      *status_line;
  829.     ngx_int_t                       rc, status;
  830.     ngx_table_elt_t                *h;
  831.     ngx_http_upstream_t            *u;
  832.     ngx_http_upstream_header_t     *hh;
  833.     ngx_http_upstream_main_conf_t  *umcf;

  834.     umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);

  835.     for ( ;; ) {

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

  837.         if (rc == NGX_OK) {

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

  839.             h = ngx_list_push(&r->upstream->headers_in.headers);
  840.             if (h == NULL) {
  841.                 return NGX_ERROR;
  842.             }

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

  844.             h->key.len = r->header_name_end - r->header_name_start;
  845.             h->value.len = r->header_end - r->header_start;

  846.             h->key.data = ngx_pnalloc(r->pool,
  847.                                       h->key.len + 1 + h->value.len + 1
  848.                                       + h->key.len);
  849.             if (h->key.data == NULL) {
  850.                 h->hash = 0;
  851.                 return NGX_ERROR;
  852.             }

  853.             h->value.data = h->key.data + h->key.len + 1;
  854.             h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;

  855.             ngx_memcpy(h->key.data, r->header_name_start, h->key.len);
  856.             h->key.data[h->key.len] = '\0';
  857.             ngx_memcpy(h->value.data, r->header_start, h->value.len);
  858.             h->value.data[h->value.len] = '\0';

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

  861.             } else {
  862.                 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
  863.             }

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

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

  868.                 if (rc != NGX_OK) {
  869.                     return rc;
  870.                 }
  871.             }

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

  874.             continue;
  875.         }

  876.         if (rc == NGX_HTTP_PARSE_HEADER_DONE) {

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

  878.             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  879.                            "http scgi header done");

  880.             u = r->upstream;

  881.             if (u->headers_in.status_n) {
  882.                 goto done;
  883.             }

  884.             if (u->headers_in.status) {
  885.                 status_line = &u->headers_in.status->value;

  886.                 status = ngx_atoi(status_line->data, 3);
  887.                 if (status == NGX_ERROR) {
  888.                     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  889.                                   "upstream sent invalid status \"%V\"",
  890.                                   status_line);
  891.                     return NGX_HTTP_UPSTREAM_INVALID_HEADER;
  892.                 }

  893.                 u->headers_in.status_n = status;

  894.                 if (status_line->len > 3) {
  895.                     u->headers_in.status_line = *status_line;
  896.                 }

  897.             } else if (u->headers_in.location) {
  898.                 u->headers_in.status_n = 302;
  899.                 ngx_str_set(&u->headers_in.status_line,
  900.                             "302 Moved Temporarily");

  901.             } else {
  902.                 u->headers_in.status_n = 200;
  903.                 ngx_str_set(&u->headers_in.status_line, "200 OK");
  904.             }

  905.             if (u->state && u->state->status == 0) {
  906.                 u->state->status = u->headers_in.status_n;
  907.             }

  908.         done:

  909.             if (u->headers_in.status_n == NGX_HTTP_SWITCHING_PROTOCOLS
  910.                 && r->headers_in.upgrade)
  911.             {
  912.                 u->upgrade = 1;
  913.             }

  914.             return NGX_OK;
  915.         }

  916.         if (rc == NGX_AGAIN) {
  917.             return NGX_AGAIN;
  918.         }

  919.         /* rc == NGX_HTTP_PARSE_INVALID_HEADER */

  920.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  921.                       "upstream sent invalid header: \"%*s\\x%02xd...\"",
  922.                       r->header_end - r->header_name_start,
  923.                       r->header_name_start, *r->header_end);

  924.         return NGX_HTTP_UPSTREAM_INVALID_HEADER;
  925.     }
  926. }


  927. static ngx_int_t
  928. ngx_http_scgi_input_filter_init(void *data)
  929. {
  930.     ngx_http_request_t   *r = data;
  931.     ngx_http_upstream_t  *u;

  932.     u = r->upstream;

  933.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  934.                    "http scgi filter init s:%ui l:%O",
  935.                    u->headers_in.status_n, u->headers_in.content_length_n);

  936.     if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
  937.         || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED)
  938.     {
  939.         u->pipe->length = 0;
  940.         u->length = 0;

  941.     } else if (r->method == NGX_HTTP_HEAD) {
  942.         u->pipe->length = -1;
  943.         u->length = -1;

  944.     } else {
  945.         u->pipe->length = u->headers_in.content_length_n;
  946.         u->length = u->headers_in.content_length_n;
  947.     }

  948.     return NGX_OK;
  949. }


  950. static void
  951. ngx_http_scgi_abort_request(ngx_http_request_t *r)
  952. {
  953.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  954.                    "abort http scgi request");

  955.     return;
  956. }


  957. static void
  958. ngx_http_scgi_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
  959. {
  960.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  961.                    "finalize http scgi request");

  962.     return;
  963. }


  964. static void *
  965. ngx_http_scgi_create_main_conf(ngx_conf_t *cf)
  966. {
  967.     ngx_http_scgi_main_conf_t  *conf;

  968.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_scgi_main_conf_t));
  969.     if (conf == NULL) {
  970.         return NULL;
  971.     }

  972. #if (NGX_HTTP_CACHE)
  973.     if (ngx_array_init(&conf->caches, cf->pool, 4,
  974.                        sizeof(ngx_http_file_cache_t *))
  975.         != NGX_OK)
  976.     {
  977.         return NULL;
  978.     }
  979. #endif

  980.     return conf;
  981. }


  982. static void *
  983. ngx_http_scgi_create_loc_conf(ngx_conf_t *cf)
  984. {
  985.     ngx_http_scgi_loc_conf_t  *conf;

  986.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_scgi_loc_conf_t));
  987.     if (conf == NULL) {
  988.         return NULL;
  989.     }

  990.     /*
  991.      * set by ngx_pcalloc():
  992.      *
  993.      *     conf->upstream.bufs.num = 0;
  994.      *     conf->upstream.ignore_headers = 0;
  995.      *     conf->upstream.next_upstream = 0;
  996.      *     conf->upstream.cache_zone = NULL;
  997.      *     conf->upstream.cache_use_stale = 0;
  998.      *     conf->upstream.cache_methods = 0;
  999.      *     conf->upstream.temp_path = NULL;
  1000.      *     conf->upstream.hide_headers_hash = { NULL, 0 };
  1001.      *     conf->upstream.store_lengths = NULL;
  1002.      *     conf->upstream.store_values = NULL;
  1003.      */

  1004.     conf->upstream.store = NGX_CONF_UNSET;
  1005.     conf->upstream.store_access = NGX_CONF_UNSET_UINT;
  1006.     conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
  1007.     conf->upstream.buffering = NGX_CONF_UNSET;
  1008.     conf->upstream.request_buffering = NGX_CONF_UNSET;
  1009.     conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
  1010.     conf->upstream.force_ranges = NGX_CONF_UNSET;

  1011.     conf->upstream.local = NGX_CONF_UNSET_PTR;
  1012.     conf->upstream.socket_keepalive = NGX_CONF_UNSET;

  1013.     conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
  1014.     conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
  1015.     conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
  1016.     conf->upstream.next_upstream_timeout = NGX_CONF_UNSET_MSEC;

  1017.     conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
  1018.     conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
  1019.     conf->upstream.limit_rate = NGX_CONF_UNSET_PTR;

  1020.     conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
  1021.     conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
  1022.     conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;

  1023.     conf->upstream.pass_request_headers = NGX_CONF_UNSET;
  1024.     conf->upstream.pass_request_body = NGX_CONF_UNSET;

  1025. #if (NGX_HTTP_CACHE)
  1026.     conf->upstream.cache = NGX_CONF_UNSET;
  1027.     conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
  1028.     conf->upstream.cache_max_range_offset = NGX_CONF_UNSET;
  1029.     conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
  1030.     conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
  1031.     conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
  1032.     conf->upstream.cache_lock = NGX_CONF_UNSET;
  1033.     conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
  1034.     conf->upstream.cache_lock_age = NGX_CONF_UNSET_MSEC;
  1035.     conf->upstream.cache_revalidate = NGX_CONF_UNSET;
  1036.     conf->upstream.cache_background_update = NGX_CONF_UNSET;
  1037. #endif

  1038.     conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
  1039.     conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;

  1040.     conf->upstream.intercept_errors = NGX_CONF_UNSET;

  1041.     /* "scgi_cyclic_temp_file" is disabled */
  1042.     conf->upstream.cyclic_temp_file = 0;

  1043.     conf->upstream.change_buffering = 1;

  1044.     ngx_str_set(&conf->upstream.module, "scgi");

  1045.     return conf;
  1046. }


  1047. static char *
  1048. ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
  1049. {
  1050.     ngx_http_scgi_loc_conf_t *prev = parent;
  1051.     ngx_http_scgi_loc_conf_t *conf = child;

  1052.     size_t                        size;
  1053.     ngx_int_t                     rc;
  1054.     ngx_hash_init_t               hash;
  1055.     ngx_http_core_loc_conf_t     *clcf;

  1056. #if (NGX_HTTP_CACHE)

  1057.     if (conf->upstream.store > 0) {
  1058.         conf->upstream.cache = 0;
  1059.     }

  1060.     if (conf->upstream.cache > 0) {
  1061.         conf->upstream.store = 0;
  1062.     }

  1063. #endif

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

  1066.         conf->upstream.store_lengths = prev->upstream.store_lengths;
  1067.         conf->upstream.store_values = prev->upstream.store_values;
  1068.     }

  1069.     ngx_conf_merge_uint_value(conf->upstream.store_access,
  1070.                               prev->upstream.store_access, 0600);

  1071.     ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,
  1072.                               prev->upstream.next_upstream_tries, 0);

  1073.     ngx_conf_merge_value(conf->upstream.buffering,
  1074.                               prev->upstream.buffering, 1);

  1075.     ngx_conf_merge_value(conf->upstream.request_buffering,
  1076.                               prev->upstream.request_buffering, 1);

  1077.     ngx_conf_merge_value(conf->upstream.ignore_client_abort,
  1078.                               prev->upstream.ignore_client_abort, 0);

  1079.     ngx_conf_merge_value(conf->upstream.force_ranges,
  1080.                               prev->upstream.force_ranges, 0);

  1081.     ngx_conf_merge_ptr_value(conf->upstream.local,
  1082.                               prev->upstream.local, NULL);

  1083.     ngx_conf_merge_value(conf->upstream.socket_keepalive,
  1084.                               prev->upstream.socket_keepalive, 0);

  1085.     ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
  1086.                               prev->upstream.connect_timeout, 60000);

  1087.     ngx_conf_merge_msec_value(conf->upstream.send_timeout,
  1088.                               prev->upstream.send_timeout, 60000);

  1089.     ngx_conf_merge_msec_value(conf->upstream.read_timeout,
  1090.                               prev->upstream.read_timeout, 60000);

  1091.     ngx_conf_merge_msec_value(conf->upstream.next_upstream_timeout,
  1092.                               prev->upstream.next_upstream_timeout, 0);

  1093.     ngx_conf_merge_size_value(conf->upstream.send_lowat,
  1094.                               prev->upstream.send_lowat, 0);

  1095.     ngx_conf_merge_size_value(conf->upstream.buffer_size,
  1096.                               prev->upstream.buffer_size,
  1097.                               (size_t) ngx_pagesize);

  1098.     ngx_conf_merge_ptr_value(conf->upstream.limit_rate,
  1099.                               prev->upstream.limit_rate, NULL);


  1100.     ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
  1101.                               8, ngx_pagesize);

  1102.     if (conf->upstream.bufs.num < 2) {
  1103.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1104.                            "there must be at least 2 \"scgi_buffers\"");
  1105.         return NGX_CONF_ERROR;
  1106.     }


  1107.     size = conf->upstream.buffer_size;
  1108.     if (size < conf->upstream.bufs.size) {
  1109.         size = conf->upstream.bufs.size;
  1110.     }


  1111.     ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
  1112.                               prev->upstream.busy_buffers_size_conf,
  1113.                               NGX_CONF_UNSET_SIZE);

  1114.     if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
  1115.         conf->upstream.busy_buffers_size = 2 * size;
  1116.     } else {
  1117.         conf->upstream.busy_buffers_size =
  1118.             conf->upstream.busy_buffers_size_conf;
  1119.     }

  1120.     if (conf->upstream.busy_buffers_size < size) {
  1121.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1122.             "\"scgi_busy_buffers_size\" must be equal to or greater "
  1123.             "than the maximum of the value of \"scgi_buffer_size\" and "
  1124.             "one of the \"scgi_buffers\"");

  1125.         return NGX_CONF_ERROR;
  1126.     }

  1127.     if (conf->upstream.busy_buffers_size
  1128.         > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
  1129.     {
  1130.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1131.             "\"scgi_busy_buffers_size\" must be less than "
  1132.             "the size of all \"scgi_buffers\" minus one buffer");

  1133.         return NGX_CONF_ERROR;
  1134.     }


  1135.     ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
  1136.                               prev->upstream.temp_file_write_size_conf,
  1137.                               NGX_CONF_UNSET_SIZE);

  1138.     if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
  1139.         conf->upstream.temp_file_write_size = 2 * size;
  1140.     } else {
  1141.         conf->upstream.temp_file_write_size =
  1142.             conf->upstream.temp_file_write_size_conf;
  1143.     }

  1144.     if (conf->upstream.temp_file_write_size < size) {
  1145.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1146.             "\"scgi_temp_file_write_size\" must be equal to or greater than "
  1147.             "the maximum of the value of \"scgi_buffer_size\" and "
  1148.             "one of the \"scgi_buffers\"");

  1149.         return NGX_CONF_ERROR;
  1150.     }


  1151.     ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
  1152.                               prev->upstream.max_temp_file_size_conf,
  1153.                               NGX_CONF_UNSET_SIZE);

  1154.     if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
  1155.         conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
  1156.     } else {
  1157.         conf->upstream.max_temp_file_size =
  1158.             conf->upstream.max_temp_file_size_conf;
  1159.     }

  1160.     if (conf->upstream.max_temp_file_size != 0
  1161.         && conf->upstream.max_temp_file_size < size)
  1162.     {
  1163.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1164.             "\"scgi_max_temp_file_size\" must be equal to zero to disable "
  1165.             "temporary files usage or must be equal to or greater than "
  1166.             "the maximum of the value of \"scgi_buffer_size\" and "
  1167.             "one of the \"scgi_buffers\"");

  1168.         return NGX_CONF_ERROR;
  1169.     }


  1170.     ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
  1171.                                  prev->upstream.ignore_headers,
  1172.                                  NGX_CONF_BITMASK_SET);


  1173.     ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
  1174.                                  prev->upstream.next_upstream,
  1175.                                  (NGX_CONF_BITMASK_SET
  1176.                                   |NGX_HTTP_UPSTREAM_FT_ERROR
  1177.                                   |NGX_HTTP_UPSTREAM_FT_TIMEOUT));

  1178.     if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
  1179.         conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
  1180.                                        |NGX_HTTP_UPSTREAM_FT_OFF;
  1181.     }

  1182.     if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
  1183.                                   prev->upstream.temp_path,
  1184.                                   &ngx_http_scgi_temp_path)
  1185.         != NGX_CONF_OK)
  1186.     {
  1187.         return NGX_CONF_ERROR;
  1188.     }

  1189. #if (NGX_HTTP_CACHE)

  1190.     if (conf->upstream.cache == NGX_CONF_UNSET) {
  1191.         ngx_conf_merge_value(conf->upstream.cache,
  1192.                               prev->upstream.cache, 0);

  1193.         conf->upstream.cache_zone = prev->upstream.cache_zone;
  1194.         conf->upstream.cache_value = prev->upstream.cache_value;
  1195.     }

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

  1198.         shm_zone = conf->upstream.cache_zone;

  1199.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1200.                            "\"scgi_cache\" zone \"%V\" is unknown",
  1201.                            &shm_zone->shm.name);

  1202.         return NGX_CONF_ERROR;
  1203.     }

  1204.     ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
  1205.                               prev->upstream.cache_min_uses, 1);

  1206.     ngx_conf_merge_off_value(conf->upstream.cache_max_range_offset,
  1207.                               prev->upstream.cache_max_range_offset,
  1208.                               NGX_MAX_OFF_T_VALUE);

  1209.     ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
  1210.                               prev->upstream.cache_use_stale,
  1211.                               (NGX_CONF_BITMASK_SET
  1212.                                |NGX_HTTP_UPSTREAM_FT_OFF));

  1213.     if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
  1214.         conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
  1215.                                          |NGX_HTTP_UPSTREAM_FT_OFF;
  1216.     }

  1217.     if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
  1218.         conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
  1219.     }

  1220.     if (conf->upstream.cache_methods == 0) {
  1221.         conf->upstream.cache_methods = prev->upstream.cache_methods;
  1222.     }

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

  1224.     ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
  1225.                              prev->upstream.cache_bypass, NULL);

  1226.     ngx_conf_merge_ptr_value(conf->upstream.no_cache,
  1227.                              prev->upstream.no_cache, NULL);

  1228.     ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
  1229.                              prev->upstream.cache_valid, NULL);

  1230.     if (conf->cache_key.value.data == NULL) {
  1231.         conf->cache_key = prev->cache_key;
  1232.     }

  1233.     if (conf->upstream.cache && conf->cache_key.value.data == NULL) {
  1234.         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
  1235.                            "no \"scgi_cache_key\" for \"scgi_cache\"");
  1236.     }

  1237.     ngx_conf_merge_value(conf->upstream.cache_lock,
  1238.                               prev->upstream.cache_lock, 0);

  1239.     ngx_conf_merge_msec_value(conf->upstream.cache_lock_timeout,
  1240.                               prev->upstream.cache_lock_timeout, 5000);

  1241.     ngx_conf_merge_msec_value(conf->upstream.cache_lock_age,
  1242.                               prev->upstream.cache_lock_age, 5000);

  1243.     ngx_conf_merge_value(conf->upstream.cache_revalidate,
  1244.                               prev->upstream.cache_revalidate, 0);

  1245.     ngx_conf_merge_value(conf->upstream.cache_background_update,
  1246.                               prev->upstream.cache_background_update, 0);

  1247. #endif

  1248.     ngx_conf_merge_value(conf->upstream.pass_request_headers,
  1249.                          prev->upstream.pass_request_headers, 1);
  1250.     ngx_conf_merge_value(conf->upstream.pass_request_body,
  1251.                          prev->upstream.pass_request_body, 1);

  1252.     ngx_conf_merge_value(conf->upstream.intercept_errors,
  1253.                          prev->upstream.intercept_errors, 0);

  1254.     hash.max_size = 512;
  1255.     hash.bucket_size = ngx_align(64, ngx_cacheline_size);
  1256.     hash.name = "scgi_hide_headers_hash";

  1257.     if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
  1258.             &prev->upstream, ngx_http_scgi_hide_headers, &hash)
  1259.         != NGX_OK)
  1260.     {
  1261.         return NGX_CONF_ERROR;
  1262.     }

  1263.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

  1264.     if (clcf->noname
  1265.         && conf->upstream.upstream == NULL && conf->scgi_lengths == NULL)
  1266.     {
  1267.         conf->upstream.upstream = prev->upstream.upstream;
  1268.         conf->scgi_lengths = prev->scgi_lengths;
  1269.         conf->scgi_values = prev->scgi_values;
  1270.     }

  1271.     if (clcf->lmt_excpt && clcf->handler == NULL
  1272.         && (conf->upstream.upstream || conf->scgi_lengths))
  1273.     {
  1274.         clcf->handler = ngx_http_scgi_handler;
  1275.     }

  1276.     if (conf->params_source == NULL) {
  1277.         conf->params = prev->params;
  1278. #if (NGX_HTTP_CACHE)
  1279.         conf->params_cache = prev->params_cache;
  1280. #endif
  1281.         conf->params_source = prev->params_source;
  1282.     }

  1283.     rc = ngx_http_scgi_init_params(cf, conf, &conf->params,
  1284.                                    ngx_http_scgi_headers);
  1285.     if (rc != NGX_OK) {
  1286.         return NGX_CONF_ERROR;
  1287.     }

  1288. #if (NGX_HTTP_CACHE)

  1289.     if (conf->upstream.cache) {
  1290.         rc = ngx_http_scgi_init_params(cf, conf, &conf->params_cache,
  1291.                                        ngx_http_scgi_cache_headers);
  1292.         if (rc != NGX_OK) {
  1293.             return NGX_CONF_ERROR;
  1294.         }
  1295.     }

  1296. #endif

  1297.     /*
  1298.      * special handling to preserve conf->params in the "http" section
  1299.      * to inherit it to all servers
  1300.      */

  1301.     if (prev->params.hash.buckets == NULL
  1302.         && conf->params_source == prev->params_source)
  1303.     {
  1304.         prev->params = conf->params;
  1305. #if (NGX_HTTP_CACHE)
  1306.         prev->params_cache = conf->params_cache;
  1307. #endif
  1308.     }

  1309.     return NGX_CONF_OK;
  1310. }


  1311. static ngx_int_t
  1312. ngx_http_scgi_init_params(ngx_conf_t *cf, ngx_http_scgi_loc_conf_t *conf,
  1313.     ngx_http_scgi_params_t *params, ngx_keyval_t *default_params)
  1314. {
  1315.     u_char                       *p;
  1316.     size_t                        size;
  1317.     uintptr_t                    *code;
  1318.     ngx_uint_t                    i, nsrc;
  1319.     ngx_array_t                   headers_names, params_merged;
  1320.     ngx_keyval_t                 *h;
  1321.     ngx_hash_key_t               *hk;
  1322.     ngx_hash_init_t               hash;
  1323.     ngx_http_upstream_param_t    *src, *s;
  1324.     ngx_http_script_compile_t     sc;
  1325.     ngx_http_script_copy_code_t  *copy;

  1326.     if (params->hash.buckets) {
  1327.         return NGX_OK;
  1328.     }

  1329.     if (conf->params_source == NULL && default_params == NULL) {
  1330.         params->hash.buckets = (void *) 1;
  1331.         return NGX_OK;
  1332.     }

  1333.     params->lengths = ngx_array_create(cf->pool, 64, 1);
  1334.     if (params->lengths == NULL) {
  1335.         return NGX_ERROR;
  1336.     }

  1337.     params->values = ngx_array_create(cf->pool, 512, 1);
  1338.     if (params->values == NULL) {
  1339.         return NGX_ERROR;
  1340.     }

  1341.     if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
  1342.         != NGX_OK)
  1343.     {
  1344.         return NGX_ERROR;
  1345.     }

  1346.     if (conf->params_source) {
  1347.         src = conf->params_source->elts;
  1348.         nsrc = conf->params_source->nelts;

  1349.     } else {
  1350.         src = NULL;
  1351.         nsrc = 0;
  1352.     }

  1353.     if (default_params) {
  1354.         if (ngx_array_init(&params_merged, cf->temp_pool, 4,
  1355.                            sizeof(ngx_http_upstream_param_t))
  1356.             != NGX_OK)
  1357.         {
  1358.             return NGX_ERROR;
  1359.         }

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

  1361.             s = ngx_array_push(&params_merged);
  1362.             if (s == NULL) {
  1363.                 return NGX_ERROR;
  1364.             }

  1365.             *s = src[i];
  1366.         }

  1367.         h = default_params;

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

  1369.             src = params_merged.elts;
  1370.             nsrc = params_merged.nelts;

  1371.             for (i = 0; i < nsrc; i++) {
  1372.                 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
  1373.                     goto next;
  1374.                 }
  1375.             }

  1376.             s = ngx_array_push(&params_merged);
  1377.             if (s == NULL) {
  1378.                 return NGX_ERROR;
  1379.             }

  1380.             s->key = h->key;
  1381.             s->value = h->value;
  1382.             s->skip_empty = 1;

  1383.         next:

  1384.             h++;
  1385.         }

  1386.         src = params_merged.elts;
  1387.         nsrc = params_merged.nelts;
  1388.     }

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

  1390.         if (src[i].key.len > sizeof("HTTP_") - 1
  1391.             && ngx_strncmp(src[i].key.data, "HTTP_", sizeof("HTTP_") - 1) == 0)
  1392.         {
  1393.             hk = ngx_array_push(&headers_names);
  1394.             if (hk == NULL) {
  1395.                 return NGX_ERROR;
  1396.             }

  1397.             hk->key.len = src[i].key.len - 5;
  1398.             hk->key.data = src[i].key.data + 5;
  1399.             hk->key_hash = ngx_hash_key_lc(hk->key.data, hk->key.len);
  1400.             hk->value = (void *) 1;

  1401.             if (src[i].value.len == 0) {
  1402.                 continue;
  1403.             }
  1404.         }

  1405.         copy = ngx_array_push_n(params->lengths,
  1406.                                 sizeof(ngx_http_script_copy_code_t));
  1407.         if (copy == NULL) {
  1408.             return NGX_ERROR;
  1409.         }

  1410.         copy->code = (ngx_http_script_code_pt) (void *)
  1411.                                                  ngx_http_script_copy_len_code;
  1412.         copy->len = src[i].key.len + 1;

  1413.         copy = ngx_array_push_n(params->lengths,
  1414.                                 sizeof(ngx_http_script_copy_code_t));
  1415.         if (copy == NULL) {
  1416.             return NGX_ERROR;
  1417.         }

  1418.         copy->code = (ngx_http_script_code_pt) (void *)
  1419.                                                  ngx_http_script_copy_len_code;
  1420.         copy->len = src[i].skip_empty;


  1421.         size = (sizeof(ngx_http_script_copy_code_t)
  1422.                 + src[i].key.len + 1 + sizeof(uintptr_t) - 1)
  1423.                & ~(sizeof(uintptr_t) - 1);

  1424.         copy = ngx_array_push_n(params->values, size);
  1425.         if (copy == NULL) {
  1426.             return NGX_ERROR;
  1427.         }

  1428.         copy->code = ngx_http_script_copy_code;
  1429.         copy->len = src[i].key.len + 1;

  1430.         p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
  1431.         (void) ngx_cpystrn(p, src[i].key.data, src[i].key.len + 1);


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

  1433.         sc.cf = cf;
  1434.         sc.source = &src[i].value;
  1435.         sc.flushes = &params->flushes;
  1436.         sc.lengths = &params->lengths;
  1437.         sc.values = &params->values;

  1438.         if (ngx_http_script_compile(&sc) != NGX_OK) {
  1439.             return NGX_ERROR;
  1440.         }

  1441.         code = ngx_array_push_n(params->lengths, sizeof(uintptr_t));
  1442.         if (code == NULL) {
  1443.             return NGX_ERROR;
  1444.         }

  1445.         *code = (uintptr_t) NULL;


  1446.         code = ngx_array_push_n(params->values, sizeof(uintptr_t));
  1447.         if (code == NULL) {
  1448.             return NGX_ERROR;
  1449.         }

  1450.         *code = (uintptr_t) NULL;
  1451.     }

  1452.     code = ngx_array_push_n(params->lengths, sizeof(uintptr_t));
  1453.     if (code == NULL) {
  1454.         return NGX_ERROR;
  1455.     }

  1456.     *code = (uintptr_t) NULL;

  1457.     params->number = headers_names.nelts;

  1458.     hash.hash = &params->hash;
  1459.     hash.key = ngx_hash_key_lc;
  1460.     hash.max_size = 512;
  1461.     hash.bucket_size = 64;
  1462.     hash.name = "scgi_params_hash";
  1463.     hash.pool = cf->pool;
  1464.     hash.temp_pool = NULL;

  1465.     return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts);
  1466. }


  1467. static char *
  1468. ngx_http_scgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1469. {
  1470.     ngx_http_scgi_loc_conf_t *scf = conf;

  1471.     ngx_url_t                   u;
  1472.     ngx_str_t                  *value, *url;
  1473.     ngx_uint_t                  n;
  1474.     ngx_http_core_loc_conf_t   *clcf;
  1475.     ngx_http_script_compile_t   sc;

  1476.     if (scf->upstream.upstream || scf->scgi_lengths) {
  1477.         return "is duplicate";
  1478.     }

  1479.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  1480.     clcf->handler = ngx_http_scgi_handler;

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

  1482.     url = &value[1];

  1483.     n = ngx_http_script_variables_count(url);

  1484.     if (n) {

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

  1486.         sc.cf = cf;
  1487.         sc.source = url;
  1488.         sc.lengths = &scf->scgi_lengths;
  1489.         sc.values = &scf->scgi_values;
  1490.         sc.variables = n;
  1491.         sc.complete_lengths = 1;
  1492.         sc.complete_values = 1;

  1493.         if (ngx_http_script_compile(&sc) != NGX_OK) {
  1494.             return NGX_CONF_ERROR;
  1495.         }

  1496.         return NGX_CONF_OK;
  1497.     }

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

  1499.     u.url = value[1];
  1500.     u.no_resolve = 1;

  1501.     scf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
  1502.     if (scf->upstream.upstream == NULL) {
  1503.         return NGX_CONF_ERROR;
  1504.     }

  1505.     if (clcf->name.len && clcf->name.data[clcf->name.len - 1] == '/') {
  1506.         clcf->auto_redirect = 1;
  1507.     }

  1508.     return NGX_CONF_OK;
  1509. }


  1510. static char *
  1511. ngx_http_scgi_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1512. {
  1513.     ngx_http_scgi_loc_conf_t *scf = conf;

  1514.     ngx_str_t                  *value;
  1515.     ngx_http_script_compile_t   sc;

  1516.     if (scf->upstream.store != NGX_CONF_UNSET) {
  1517.         return "is duplicate";
  1518.     }

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

  1520.     if (ngx_strcmp(value[1].data, "off") == 0) {
  1521.         scf->upstream.store = 0;
  1522.         return NGX_CONF_OK;
  1523.     }

  1524.     if (value[1].len == 0) {
  1525.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "empty path");
  1526.         return NGX_CONF_ERROR;
  1527.     }

  1528. #if (NGX_HTTP_CACHE)
  1529.     if (scf->upstream.cache > 0) {
  1530.         return "is incompatible with \"scgi_cache\"";
  1531.     }
  1532. #endif

  1533.     scf->upstream.store = 1;

  1534.     if (ngx_strcmp(value[1].data, "on") == 0) {
  1535.         return NGX_CONF_OK;
  1536.     }

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

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

  1540.     sc.cf = cf;
  1541.     sc.source = &value[1];
  1542.     sc.lengths = &scf->upstream.store_lengths;
  1543.     sc.values = &scf->upstream.store_values;
  1544.     sc.variables = ngx_http_script_variables_count(&value[1]);
  1545.     sc.complete_lengths = 1;
  1546.     sc.complete_values = 1;

  1547.     if (ngx_http_script_compile(&sc) != NGX_OK) {
  1548.         return NGX_CONF_ERROR;
  1549.     }

  1550.     return NGX_CONF_OK;
  1551. }


  1552. #if (NGX_HTTP_CACHE)

  1553. static char *
  1554. ngx_http_scgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1555. {
  1556.     ngx_http_scgi_loc_conf_t *scf = conf;

  1557.     ngx_str_t                         *value;
  1558.     ngx_http_complex_value_t           cv;
  1559.     ngx_http_compile_complex_value_t   ccv;

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

  1561.     if (scf->upstream.cache != NGX_CONF_UNSET) {
  1562.         return "is duplicate";
  1563.     }

  1564.     if (ngx_strcmp(value[1].data, "off") == 0) {
  1565.         scf->upstream.cache = 0;
  1566.         return NGX_CONF_OK;
  1567.     }

  1568.     if (scf->upstream.store > 0) {
  1569.         return "is incompatible with \"scgi_store\"";
  1570.     }

  1571.     scf->upstream.cache = 1;

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

  1573.     ccv.cf = cf;
  1574.     ccv.value = &value[1];
  1575.     ccv.complex_value = &cv;

  1576.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  1577.         return NGX_CONF_ERROR;
  1578.     }

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

  1580.         scf->upstream.cache_value = ngx_palloc(cf->pool,
  1581.                                              sizeof(ngx_http_complex_value_t));
  1582.         if (scf->upstream.cache_value == NULL) {
  1583.             return NGX_CONF_ERROR;
  1584.         }

  1585.         *scf->upstream.cache_value = cv;

  1586.         return NGX_CONF_OK;
  1587.     }

  1588.     scf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0,
  1589.                                                      &ngx_http_scgi_module);
  1590.     if (scf->upstream.cache_zone == NULL) {
  1591.         return NGX_CONF_ERROR;
  1592.     }

  1593.     return NGX_CONF_OK;
  1594. }


  1595. static char *
  1596. ngx_http_scgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1597. {
  1598.     ngx_http_scgi_loc_conf_t *scf = conf;

  1599.     ngx_str_t                         *value;
  1600.     ngx_http_compile_complex_value_t   ccv;

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

  1602.     if (scf->cache_key.value.data) {
  1603.         return "is duplicate";
  1604.     }

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

  1606.     ccv.cf = cf;
  1607.     ccv.value = &value[1];
  1608.     ccv.complex_value = &scf->cache_key;

  1609.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  1610.         return NGX_CONF_ERROR;
  1611.     }

  1612.     return NGX_CONF_OK;
  1613. }

  1614. #endif