src/http/modules/ngx_http_scgi_module.c - nginx source code

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. #if (NGX_HTTP_CACHE)

  376. static ngx_keyval_t  ngx_http_scgi_cache_headers[] = {
  377.     { ngx_string("HTTP_IF_MODIFIED_SINCE"),
  378.       ngx_string("$upstream_cache_last_modified") },
  379.     { ngx_string("HTTP_IF_UNMODIFIED_SINCE"), ngx_string("") },
  380.     { ngx_string("HTTP_IF_NONE_MATCH"), ngx_string("$upstream_cache_etag") },
  381.     { ngx_string("HTTP_IF_MATCH"), ngx_string("") },
  382.     { ngx_string("HTTP_RANGE"), ngx_string("") },
  383.     { ngx_string("HTTP_IF_RANGE"), ngx_string("") },
  384.     { ngx_null_string, ngx_null_string }
  385. };

  386. #endif


  387. static ngx_path_init_t ngx_http_scgi_temp_path = {
  388.     ngx_string(NGX_HTTP_SCGI_TEMP_PATH), { 1, 2, 0 }
  389. };


  390. static ngx_int_t
  391. ngx_http_scgi_handler(ngx_http_request_t *r)
  392. {
  393.     ngx_int_t                   rc;
  394.     ngx_http_status_t          *status;
  395.     ngx_http_upstream_t        *u;
  396.     ngx_http_scgi_loc_conf_t   *scf;
  397. #if (NGX_HTTP_CACHE)
  398.     ngx_http_scgi_main_conf_t  *smcf;
  399. #endif

  400.     if (ngx_http_upstream_create(r) != NGX_OK) {
  401.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  402.     }

  403.     status = ngx_pcalloc(r->pool, sizeof(ngx_http_status_t));
  404.     if (status == NULL) {
  405.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  406.     }

  407.     ngx_http_set_ctx(r, status, ngx_http_scgi_module);

  408.     scf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module);

  409.     if (scf->scgi_lengths) {
  410.         if (ngx_http_scgi_eval(r, scf) != NGX_OK) {
  411.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  412.         }
  413.     }

  414.     u = r->upstream;

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

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

  418. #if (NGX_HTTP_CACHE)
  419.     smcf = ngx_http_get_module_main_conf(r, ngx_http_scgi_module);

  420.     u->caches = &smcf->caches;
  421.     u->create_key = ngx_http_scgi_create_key;
  422. #endif

  423.     u->create_request = ngx_http_scgi_create_request;
  424.     u->reinit_request = ngx_http_scgi_reinit_request;
  425.     u->process_header = ngx_http_scgi_process_status_line;
  426.     u->abort_request = ngx_http_scgi_abort_request;
  427.     u->finalize_request = ngx_http_scgi_finalize_request;
  428.     r->state = 0;

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

  430.     u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
  431.     if (u->pipe == NULL) {
  432.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  433.     }

  434.     u->pipe->input_filter = ngx_event_pipe_copy_input_filter;
  435.     u->pipe->input_ctx = r;

  436.     u->input_filter_init = ngx_http_scgi_input_filter_init;
  437.     u->input_filter = ngx_http_upstream_non_buffered_filter;
  438.     u->input_filter_ctx = r;

  439.     if (!scf->upstream.request_buffering
  440.         && scf->upstream.pass_request_body
  441.         && !r->headers_in.chunked)
  442.     {
  443.         r->request_body_no_buffering = 1;
  444.     }

  445.     rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);

  446.     if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
  447.         return rc;
  448.     }

  449.     return NGX_DONE;
  450. }


  451. static ngx_int_t
  452. ngx_http_scgi_eval(ngx_http_request_t *r, ngx_http_scgi_loc_conf_t * scf)
  453. {
  454.     ngx_url_t             url;
  455.     ngx_http_upstream_t  *u;

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

  457.     if (ngx_http_script_run(r, &url.url, scf->scgi_lengths->elts, 0,
  458.                             scf->scgi_values->elts)
  459.         == NULL)
  460.     {
  461.         return NGX_ERROR;
  462.     }

  463.     url.no_resolve = 1;

  464.     if (ngx_parse_url(r->pool, &url) != NGX_OK) {
  465.         if (url.err) {
  466.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  467.                           "%s in upstream \"%V\"", url.err, &url.url);
  468.         }

  469.         return NGX_ERROR;
  470.     }

  471.     u = r->upstream;

  472.     u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
  473.     if (u->resolved == NULL) {
  474.         return NGX_ERROR;
  475.     }

  476.     if (url.addrs) {
  477.         u->resolved->sockaddr = url.addrs[0].sockaddr;
  478.         u->resolved->socklen = url.addrs[0].socklen;
  479.         u->resolved->name = url.addrs[0].name;
  480.         u->resolved->naddrs = 1;
  481.     }

  482.     u->resolved->host = url.host;
  483.     u->resolved->port = url.port;
  484.     u->resolved->no_port = url.no_port;

  485.     return NGX_OK;
  486. }


  487. #if (NGX_HTTP_CACHE)

  488. static ngx_int_t
  489. ngx_http_scgi_create_key(ngx_http_request_t *r)
  490. {
  491.     ngx_str_t                 *key;
  492.     ngx_http_scgi_loc_conf_t  *scf;

  493.     key = ngx_array_push(&r->cache->keys);
  494.     if (key == NULL) {
  495.         return NGX_ERROR;
  496.     }

  497.     scf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module);

  498.     if (ngx_http_complex_value(r, &scf->cache_key, key) != NGX_OK) {
  499.         return NGX_ERROR;
  500.     }

  501.     return NGX_OK;
  502. }

  503. #endif


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

  522.     content_length_n = 0;
  523.     body = r->upstream->request_bufs;

  524.     while (body) {
  525.         content_length_n += ngx_buf_size(body->buf);
  526.         body = body->next;
  527.     }

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

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

  531.     header_params = 0;
  532.     ignored = NULL;

  533.     scf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module);

  534. #if (NGX_HTTP_CACHE)
  535.     params = r->upstream->cacheable ? &scf->params_cache : &scf->params;
  536. #else
  537.     params = &scf->params;
  538. #endif

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

  541.         ngx_http_script_flush_no_cacheable_variables(r, params->flushes);
  542.         le.flushed = 1;

  543.         le.ip = params->lengths->elts;
  544.         le.request = r;

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

  546.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  547.             key_len = lcode(&le);

  548.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  549.             skip_empty = lcode(&le);

  550.             for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
  551.                 lcode = *(ngx_http_script_len_code_pt *) le.ip;
  552.             }
  553.             le.ip += sizeof(uintptr_t);

  554.             if (skip_empty && val_len == 0) {
  555.                 continue;
  556.             }

  557.             len += key_len + val_len + 1;
  558.         }
  559.     }

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

  561.         allocated = 0;
  562.         lowcase_key = NULL;

  563.         if (ngx_http_link_multi_headers(r) != NGX_OK) {
  564.             return NGX_ERROR;
  565.         }

  566.         if (params->number || r->headers_in.multi) {
  567.             n = 0;
  568.             part = &r->headers_in.headers.part;

  569.             while (part) {
  570.                 n += part->nelts;
  571.                 part = part->next;
  572.             }

  573.             ignored = ngx_palloc(r->pool, n * sizeof(void *));
  574.             if (ignored == NULL) {
  575.                 return NGX_ERROR;
  576.             }
  577.         }

  578.         part = &r->headers_in.headers.part;
  579.         header = part->elts;

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

  581.             if (i >= part->nelts) {
  582.                 if (part->next == NULL) {
  583.                     break;
  584.                 }

  585.                 part = part->next;
  586.                 header = part->elts;
  587.                 i = 0;
  588.             }

  589.             for (n = 0; n < header_params; n++) {
  590.                 if (&header[i] == ignored[n]) {
  591.                     goto next_length;
  592.                 }
  593.             }

  594.             if (params->number) {
  595.                 if (allocated < header[i].key.len) {
  596.                     allocated = header[i].key.len + 16;
  597.                     lowcase_key = ngx_pnalloc(r->pool, allocated);
  598.                     if (lowcase_key == NULL) {
  599.                         return NGX_ERROR;
  600.                     }
  601.                 }

  602.                 hash = 0;

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

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

  607.                     } else if (ch == '-') {
  608.                         ch = '_';
  609.                     }

  610.                     hash = ngx_hash(hash, ch);
  611.                     lowcase_key[n] = ch;
  612.                 }

  613.                 if (ngx_hash_find(&params->hash, hash, lowcase_key, n)) {
  614.                     ignored[header_params++] = &header[i];
  615.                     continue;
  616.                 }
  617.             }

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

  620.             for (hn = header[i].next; hn; hn = hn->next) {
  621.                 len += hn->value.len + 2;
  622.                 ignored[header_params++] = hn;
  623.             }

  624.         next_length:

  625.             continue;
  626.         }
  627.     }

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

  629.     b = ngx_create_temp_buf(r->pool, NGX_SIZE_T_LEN + 1 + len + 1);
  630.     if (b == NULL) {
  631.         return NGX_ERROR;
  632.     }

  633.     cl = ngx_alloc_chain_link(r->pool);
  634.     if (cl == NULL) {
  635.         return NGX_ERROR;
  636.     }

  637.     cl->buf = b;

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

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

  642.         e.ip = params->values->elts;
  643.         e.pos = b->last;
  644.         e.request = r;
  645.         e.flushed = 1;

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

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

  648.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  649.             lcode(&le); /* key length */

  650.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  651.             skip_empty = lcode(&le);

  652.             for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
  653.                 lcode = *(ngx_http_script_len_code_pt *) le.ip;
  654.             }
  655.             le.ip += sizeof(uintptr_t);

  656.             if (skip_empty && val_len == 0) {
  657.                 e.skip = 1;

  658.                 while (*(uintptr_t *) e.ip) {
  659.                     code = *(ngx_http_script_code_pt *) e.ip;
  660.                     code((ngx_http_script_engine_t *) &e);
  661.                 }
  662.                 e.ip += sizeof(uintptr_t);

  663.                 e.skip = 0;

  664.                 continue;
  665.             }

  666. #if (NGX_DEBUG)
  667.             key = e.pos;
  668. #endif
  669.             code = *(ngx_http_script_code_pt *) e.ip;
  670.             code((ngx_http_script_engine_t *) &e);

  671. #if (NGX_DEBUG)
  672.             val = e.pos;
  673. #endif
  674.             while (*(uintptr_t *) e.ip) {
  675.                 code = *(ngx_http_script_code_pt *) e.ip;
  676.                 code((ngx_http_script_engine_t *) &e);
  677.             }
  678.             *e.pos++ = '\0';
  679.             e.ip += sizeof(uintptr_t);

  680.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  681.                            "scgi param: \"%s: %s\"", key, val);
  682.         }

  683.         b->last = e.pos;
  684.     }

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

  686.         part = &r->headers_in.headers.part;
  687.         header = part->elts;

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

  689.             if (i >= part->nelts) {
  690.                 if (part->next == NULL) {
  691.                     break;
  692.                 }

  693.                 part = part->next;
  694.                 header = part->elts;
  695.                 i = 0;
  696.             }

  697.             for (n = 0; n < header_params; n++) {
  698.                 if (&header[i] == ignored[n]) {
  699.                     goto next_value;
  700.                 }
  701.             }

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

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

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

  708.                 } else if (ch == '-') {
  709.                     ch = '_';
  710.                 }

  711.                 *b->last++ = ch;
  712.             }

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

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

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

  717.                 if (header[i].key.len == sizeof("Cookie") - 1
  718.                     && ngx_strncasecmp(header[i].key.data, (u_char *) "Cookie",
  719.                                        sizeof("Cookie") - 1)
  720.                        == 0)
  721.                 {
  722.                     sep = ';';

  723.                 } else {
  724.                     sep = ',';
  725.                 }

  726.                 for (hn = header[i].next; hn; hn = hn->next) {
  727.                     *b->last++ = sep;
  728.                     *b->last++ = ' ';
  729.                     b->last = ngx_copy(b->last, hn->value.data, hn->value.len);
  730.                 }
  731.             }

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

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

  735.         next_value:

  736.             continue;
  737.         }
  738.     }

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

  740.     if (r->request_body_no_buffering) {
  741.         r->upstream->request_bufs = cl;

  742.     } else if (scf->upstream.pass_request_body) {
  743.         body = r->upstream->request_bufs;
  744.         r->upstream->request_bufs = cl;

  745.         while (body) {
  746.             b = ngx_alloc_buf(r->pool);
  747.             if (b == NULL) {
  748.                 return NGX_ERROR;
  749.             }

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

  751.             cl->next = ngx_alloc_chain_link(r->pool);
  752.             if (cl->next == NULL) {
  753.                 return NGX_ERROR;
  754.             }

  755.             cl = cl->next;
  756.             cl->buf = b;

  757.             body = body->next;
  758.         }

  759.     } else {
  760.         r->upstream->request_bufs = cl;
  761.     }

  762.     cl->next = NULL;

  763.     return NGX_OK;
  764. }


  765. static ngx_int_t
  766. ngx_http_scgi_reinit_request(ngx_http_request_t *r)
  767. {
  768.     ngx_http_status_t  *status;

  769.     status = ngx_http_get_module_ctx(r, ngx_http_scgi_module);

  770.     if (status == NULL) {
  771.         return NGX_OK;
  772.     }

  773.     status->code = 0;
  774.     status->count = 0;
  775.     status->start = NULL;
  776.     status->end = NULL;

  777.     r->upstream->process_header = ngx_http_scgi_process_status_line;
  778.     r->state = 0;

  779.     return NGX_OK;
  780. }


  781. static ngx_int_t
  782. ngx_http_scgi_process_status_line(ngx_http_request_t *r)
  783. {
  784.     size_t                len;
  785.     ngx_int_t             rc;
  786.     ngx_http_status_t    *status;
  787.     ngx_http_upstream_t  *u;

  788.     status = ngx_http_get_module_ctx(r, ngx_http_scgi_module);

  789.     if (status == NULL) {
  790.         return NGX_ERROR;
  791.     }

  792.     u = r->upstream;

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

  794.     if (rc == NGX_AGAIN) {
  795.         return rc;
  796.     }

  797.     if (rc == NGX_ERROR) {
  798.         u->process_header = ngx_http_scgi_process_header;
  799.         return ngx_http_scgi_process_header(r);
  800.     }

  801.     if (u->state && u->state->status == 0) {
  802.         u->state->status = status->code;
  803.     }

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

  805.     len = status->end - status->start;
  806.     u->headers_in.status_line.len = len;

  807.     u->headers_in.status_line.data = ngx_pnalloc(r->pool, len);
  808.     if (u->headers_in.status_line.data == NULL) {
  809.         return NGX_ERROR;
  810.     }

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

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

  815.     u->process_header = ngx_http_scgi_process_header;

  816.     return ngx_http_scgi_process_header(r);
  817. }


  818. static ngx_int_t
  819. ngx_http_scgi_process_header(ngx_http_request_t *r)
  820. {
  821.     ngx_str_t                      *status_line;
  822.     ngx_int_t                       rc, status;
  823.     ngx_table_elt_t                *h;
  824.     ngx_http_upstream_t            *u;
  825.     ngx_http_upstream_header_t     *hh;
  826.     ngx_http_upstream_main_conf_t  *umcf;

  827.     umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);

  828.     for ( ;; ) {

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

  830.         if (rc == NGX_OK) {

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

  832.             h = ngx_list_push(&r->upstream->headers_in.headers);
  833.             if (h == NULL) {
  834.                 return NGX_ERROR;
  835.             }

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

  837.             h->key.len = r->header_name_end - r->header_name_start;
  838.             h->value.len = r->header_end - r->header_start;

  839.             h->key.data = ngx_pnalloc(r->pool,
  840.                                       h->key.len + 1 + h->value.len + 1
  841.                                       + h->key.len);
  842.             if (h->key.data == NULL) {
  843.                 h->hash = 0;
  844.                 return NGX_ERROR;
  845.             }

  846.             h->value.data = h->key.data + h->key.len + 1;
  847.             h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;

  848.             ngx_memcpy(h->key.data, r->header_name_start, h->key.len);
  849.             h->key.data[h->key.len] = '\0';
  850.             ngx_memcpy(h->value.data, r->header_start, h->value.len);
  851.             h->value.data[h->value.len] = '\0';

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

  854.             } else {
  855.                 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
  856.             }

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

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

  861.                 if (rc != NGX_OK) {
  862.                     return rc;
  863.                 }
  864.             }

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

  867.             continue;
  868.         }

  869.         if (rc == NGX_HTTP_PARSE_HEADER_DONE) {

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

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

  873.             u = r->upstream;

  874.             if (u->headers_in.status_n) {
  875.                 goto done;
  876.             }

  877.             if (u->headers_in.status) {
  878.                 status_line = &u->headers_in.status->value;

  879.                 status = ngx_atoi(status_line->data, 3);
  880.                 if (status == NGX_ERROR) {
  881.                     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  882.                                   "upstream sent invalid status \"%V\"",
  883.                                   status_line);
  884.                     return NGX_HTTP_UPSTREAM_INVALID_HEADER;
  885.                 }

  886.                 u->headers_in.status_n = status;

  887.                 if (status_line->len > 3) {
  888.                     u->headers_in.status_line = *status_line;
  889.                 }

  890.             } else if (u->headers_in.location) {
  891.                 u->headers_in.status_n = 302;
  892.                 ngx_str_set(&u->headers_in.status_line,
  893.                             "302 Moved Temporarily");

  894.             } else {
  895.                 u->headers_in.status_n = 200;
  896.                 ngx_str_set(&u->headers_in.status_line, "200 OK");
  897.             }

  898.             if (u->state && u->state->status == 0) {
  899.                 u->state->status = u->headers_in.status_n;
  900.             }

  901.         done:

  902.             if (u->headers_in.status_n == NGX_HTTP_SWITCHING_PROTOCOLS
  903.                 && r->headers_in.upgrade)
  904.             {
  905.                 u->upgrade = 1;
  906.             }

  907.             return NGX_OK;
  908.         }

  909.         if (rc == NGX_AGAIN) {
  910.             return NGX_AGAIN;
  911.         }

  912.         /* rc == NGX_HTTP_PARSE_INVALID_HEADER */

  913.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  914.                       "upstream sent invalid header: \"%*s\\x%02xd...\"",
  915.                       r->header_end - r->header_name_start,
  916.                       r->header_name_start, *r->header_end);

  917.         return NGX_HTTP_UPSTREAM_INVALID_HEADER;
  918.     }
  919. }


  920. static ngx_int_t
  921. ngx_http_scgi_input_filter_init(void *data)
  922. {
  923.     ngx_http_request_t   *r = data;
  924.     ngx_http_upstream_t  *u;

  925.     u = r->upstream;

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

  929.     if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
  930.         || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED)
  931.     {
  932.         u->pipe->length = 0;
  933.         u->length = 0;

  934.     } else if (r->method == NGX_HTTP_HEAD) {
  935.         u->pipe->length = -1;
  936.         u->length = -1;

  937.     } else {
  938.         u->pipe->length = u->headers_in.content_length_n;
  939.         u->length = u->headers_in.content_length_n;
  940.     }

  941.     return NGX_OK;
  942. }


  943. static void
  944. ngx_http_scgi_abort_request(ngx_http_request_t *r)
  945. {
  946.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  947.                    "abort http scgi request");

  948.     return;
  949. }


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

  955.     return;
  956. }


  957. static void *
  958. ngx_http_scgi_create_main_conf(ngx_conf_t *cf)
  959. {
  960.     ngx_http_scgi_main_conf_t  *conf;

  961.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_scgi_main_conf_t));
  962.     if (conf == NULL) {
  963.         return NULL;
  964.     }

  965. #if (NGX_HTTP_CACHE)
  966.     if (ngx_array_init(&conf->caches, cf->pool, 4,
  967.                        sizeof(ngx_http_file_cache_t *))
  968.         != NGX_OK)
  969.     {
  970.         return NULL;
  971.     }
  972. #endif

  973.     return conf;
  974. }


  975. static void *
  976. ngx_http_scgi_create_loc_conf(ngx_conf_t *cf)
  977. {
  978.     ngx_http_scgi_loc_conf_t  *conf;

  979.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_scgi_loc_conf_t));
  980.     if (conf == NULL) {
  981.         return NULL;
  982.     }

  983.     /*
  984.      * set by ngx_pcalloc():
  985.      *
  986.      *     conf->upstream.bufs.num = 0;
  987.      *     conf->upstream.ignore_headers = 0;
  988.      *     conf->upstream.next_upstream = 0;
  989.      *     conf->upstream.cache_zone = NULL;
  990.      *     conf->upstream.cache_use_stale = 0;
  991.      *     conf->upstream.cache_methods = 0;
  992.      *     conf->upstream.temp_path = NULL;
  993.      *     conf->upstream.hide_headers_hash = { NULL, 0 };
  994.      *     conf->upstream.store_lengths = NULL;
  995.      *     conf->upstream.store_values = NULL;
  996.      */

  997.     conf->upstream.store = NGX_CONF_UNSET;
  998.     conf->upstream.store_access = NGX_CONF_UNSET_UINT;
  999.     conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
  1000.     conf->upstream.buffering = NGX_CONF_UNSET;
  1001.     conf->upstream.request_buffering = NGX_CONF_UNSET;
  1002.     conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
  1003.     conf->upstream.force_ranges = NGX_CONF_UNSET;

  1004.     conf->upstream.local = NGX_CONF_UNSET_PTR;
  1005.     conf->upstream.socket_keepalive = NGX_CONF_UNSET;

  1006.     conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
  1007.     conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
  1008.     conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
  1009.     conf->upstream.next_upstream_timeout = NGX_CONF_UNSET_MSEC;

  1010.     conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
  1011.     conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
  1012.     conf->upstream.limit_rate = NGX_CONF_UNSET_PTR;

  1013.     conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
  1014.     conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
  1015.     conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;

  1016.     conf->upstream.pass_request_headers = NGX_CONF_UNSET;
  1017.     conf->upstream.pass_request_body = NGX_CONF_UNSET;

  1018. #if (NGX_HTTP_CACHE)
  1019.     conf->upstream.cache = NGX_CONF_UNSET;
  1020.     conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
  1021.     conf->upstream.cache_max_range_offset = NGX_CONF_UNSET;
  1022.     conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
  1023.     conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
  1024.     conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
  1025.     conf->upstream.cache_lock = NGX_CONF_UNSET;
  1026.     conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
  1027.     conf->upstream.cache_lock_age = NGX_CONF_UNSET_MSEC;
  1028.     conf->upstream.cache_revalidate = NGX_CONF_UNSET;
  1029.     conf->upstream.cache_background_update = NGX_CONF_UNSET;
  1030. #endif

  1031.     conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
  1032.     conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;

  1033.     conf->upstream.intercept_errors = NGX_CONF_UNSET;

  1034.     /* "scgi_cyclic_temp_file" is disabled */
  1035.     conf->upstream.cyclic_temp_file = 0;

  1036.     conf->upstream.change_buffering = 1;

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

  1038.     return conf;
  1039. }


  1040. static char *
  1041. ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
  1042. {
  1043.     ngx_http_scgi_loc_conf_t *prev = parent;
  1044.     ngx_http_scgi_loc_conf_t *conf = child;

  1045.     size_t                        size;
  1046.     ngx_int_t                     rc;
  1047.     ngx_hash_init_t               hash;
  1048.     ngx_http_core_loc_conf_t     *clcf;

  1049. #if (NGX_HTTP_CACHE)

  1050.     if (conf->upstream.store > 0) {
  1051.         conf->upstream.cache = 0;
  1052.     }

  1053.     if (conf->upstream.cache > 0) {
  1054.         conf->upstream.store = 0;
  1055.     }

  1056. #endif

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

  1059.         conf->upstream.store_lengths = prev->upstream.store_lengths;
  1060.         conf->upstream.store_values = prev->upstream.store_values;
  1061.     }

  1062.     ngx_conf_merge_uint_value(conf->upstream.store_access,
  1063.                               prev->upstream.store_access, 0600);

  1064.     ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,
  1065.                               prev->upstream.next_upstream_tries, 0);

  1066.     ngx_conf_merge_value(conf->upstream.buffering,
  1067.                               prev->upstream.buffering, 1);

  1068.     ngx_conf_merge_value(conf->upstream.request_buffering,
  1069.                               prev->upstream.request_buffering, 1);

  1070.     ngx_conf_merge_value(conf->upstream.ignore_client_abort,
  1071.                               prev->upstream.ignore_client_abort, 0);

  1072.     ngx_conf_merge_value(conf->upstream.force_ranges,
  1073.                               prev->upstream.force_ranges, 0);

  1074.     ngx_conf_merge_ptr_value(conf->upstream.local,
  1075.                               prev->upstream.local, NULL);

  1076.     ngx_conf_merge_value(conf->upstream.socket_keepalive,
  1077.                               prev->upstream.socket_keepalive, 0);

  1078.     ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
  1079.                               prev->upstream.connect_timeout, 60000);

  1080.     ngx_conf_merge_msec_value(conf->upstream.send_timeout,
  1081.                               prev->upstream.send_timeout, 60000);

  1082.     ngx_conf_merge_msec_value(conf->upstream.read_timeout,
  1083.                               prev->upstream.read_timeout, 60000);

  1084.     ngx_conf_merge_msec_value(conf->upstream.next_upstream_timeout,
  1085.                               prev->upstream.next_upstream_timeout, 0);

  1086.     ngx_conf_merge_size_value(conf->upstream.send_lowat,
  1087.                               prev->upstream.send_lowat, 0);

  1088.     ngx_conf_merge_size_value(conf->upstream.buffer_size,
  1089.                               prev->upstream.buffer_size,
  1090.                               (size_t) ngx_pagesize);

  1091.     ngx_conf_merge_ptr_value(conf->upstream.limit_rate,
  1092.                               prev->upstream.limit_rate, NULL);


  1093.     ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
  1094.                               8, ngx_pagesize);

  1095.     if (conf->upstream.bufs.num < 2) {
  1096.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1097.                            "there must be at least 2 \"scgi_buffers\"");
  1098.         return NGX_CONF_ERROR;
  1099.     }


  1100.     size = conf->upstream.buffer_size;
  1101.     if (size < conf->upstream.bufs.size) {
  1102.         size = conf->upstream.bufs.size;
  1103.     }


  1104.     ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
  1105.                               prev->upstream.busy_buffers_size_conf,
  1106.                               NGX_CONF_UNSET_SIZE);

  1107.     if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
  1108.         conf->upstream.busy_buffers_size = 2 * size;
  1109.     } else {
  1110.         conf->upstream.busy_buffers_size =
  1111.             conf->upstream.busy_buffers_size_conf;
  1112.     }

  1113.     if (conf->upstream.busy_buffers_size < size) {
  1114.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1115.             "\"scgi_busy_buffers_size\" must be equal to or greater "
  1116.             "than the maximum of the value of \"scgi_buffer_size\" and "
  1117.             "one of the \"scgi_buffers\"");

  1118.         return NGX_CONF_ERROR;
  1119.     }

  1120.     if (conf->upstream.busy_buffers_size
  1121.         > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
  1122.     {
  1123.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1124.             "\"scgi_busy_buffers_size\" must be less than "
  1125.             "the size of all \"scgi_buffers\" minus one buffer");

  1126.         return NGX_CONF_ERROR;
  1127.     }


  1128.     ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
  1129.                               prev->upstream.temp_file_write_size_conf,
  1130.                               NGX_CONF_UNSET_SIZE);

  1131.     if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
  1132.         conf->upstream.temp_file_write_size = 2 * size;
  1133.     } else {
  1134.         conf->upstream.temp_file_write_size =
  1135.             conf->upstream.temp_file_write_size_conf;
  1136.     }

  1137.     if (conf->upstream.temp_file_write_size < size) {
  1138.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1139.             "\"scgi_temp_file_write_size\" must be equal to or greater than "
  1140.             "the maximum of the value of \"scgi_buffer_size\" and "
  1141.             "one of the \"scgi_buffers\"");

  1142.         return NGX_CONF_ERROR;
  1143.     }


  1144.     ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
  1145.                               prev->upstream.max_temp_file_size_conf,
  1146.                               NGX_CONF_UNSET_SIZE);

  1147.     if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
  1148.         conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
  1149.     } else {
  1150.         conf->upstream.max_temp_file_size =
  1151.             conf->upstream.max_temp_file_size_conf;
  1152.     }

  1153.     if (conf->upstream.max_temp_file_size != 0
  1154.         && conf->upstream.max_temp_file_size < size)
  1155.     {
  1156.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1157.             "\"scgi_max_temp_file_size\" must be equal to zero to disable "
  1158.             "temporary files usage or must be equal to or greater than "
  1159.             "the maximum of the value of \"scgi_buffer_size\" and "
  1160.             "one of the \"scgi_buffers\"");

  1161.         return NGX_CONF_ERROR;
  1162.     }


  1163.     ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
  1164.                                  prev->upstream.ignore_headers,
  1165.                                  NGX_CONF_BITMASK_SET);


  1166.     ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
  1167.                                  prev->upstream.next_upstream,
  1168.                                  (NGX_CONF_BITMASK_SET
  1169.                                   |NGX_HTTP_UPSTREAM_FT_ERROR
  1170.                                   |NGX_HTTP_UPSTREAM_FT_TIMEOUT));

  1171.     if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
  1172.         conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
  1173.                                        |NGX_HTTP_UPSTREAM_FT_OFF;
  1174.     }

  1175.     if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
  1176.                                   prev->upstream.temp_path,
  1177.                                   &ngx_http_scgi_temp_path)
  1178.         != NGX_OK)
  1179.     {
  1180.         return NGX_CONF_ERROR;
  1181.     }

  1182. #if (NGX_HTTP_CACHE)

  1183.     if (conf->upstream.cache == NGX_CONF_UNSET) {
  1184.         ngx_conf_merge_value(conf->upstream.cache,
  1185.                               prev->upstream.cache, 0);

  1186.         conf->upstream.cache_zone = prev->upstream.cache_zone;
  1187.         conf->upstream.cache_value = prev->upstream.cache_value;
  1188.     }

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

  1191.         shm_zone = conf->upstream.cache_zone;

  1192.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1193.                            "\"scgi_cache\" zone \"%V\" is unknown",
  1194.                            &shm_zone->shm.name);

  1195.         return NGX_CONF_ERROR;
  1196.     }

  1197.     ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
  1198.                               prev->upstream.cache_min_uses, 1);

  1199.     ngx_conf_merge_off_value(conf->upstream.cache_max_range_offset,
  1200.                               prev->upstream.cache_max_range_offset,
  1201.                               NGX_MAX_OFF_T_VALUE);

  1202.     ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
  1203.                               prev->upstream.cache_use_stale,
  1204.                               (NGX_CONF_BITMASK_SET
  1205.                                |NGX_HTTP_UPSTREAM_FT_OFF));

  1206.     if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
  1207.         conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
  1208.                                          |NGX_HTTP_UPSTREAM_FT_OFF;
  1209.     }

  1210.     if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
  1211.         conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
  1212.     }

  1213.     if (conf->upstream.cache_methods == 0) {
  1214.         conf->upstream.cache_methods = prev->upstream.cache_methods;
  1215.     }

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

  1217.     ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
  1218.                              prev->upstream.cache_bypass, NULL);

  1219.     ngx_conf_merge_ptr_value(conf->upstream.no_cache,
  1220.                              prev->upstream.no_cache, NULL);

  1221.     ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
  1222.                              prev->upstream.cache_valid, NULL);

  1223.     if (conf->cache_key.value.data == NULL) {
  1224.         conf->cache_key = prev->cache_key;
  1225.     }

  1226.     if (conf->upstream.cache && conf->cache_key.value.data == NULL) {
  1227.         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
  1228.                            "no \"scgi_cache_key\" for \"scgi_cache\"");
  1229.     }

  1230.     ngx_conf_merge_value(conf->upstream.cache_lock,
  1231.                               prev->upstream.cache_lock, 0);

  1232.     ngx_conf_merge_msec_value(conf->upstream.cache_lock_timeout,
  1233.                               prev->upstream.cache_lock_timeout, 5000);

  1234.     ngx_conf_merge_msec_value(conf->upstream.cache_lock_age,
  1235.                               prev->upstream.cache_lock_age, 5000);

  1236.     ngx_conf_merge_value(conf->upstream.cache_revalidate,
  1237.                               prev->upstream.cache_revalidate, 0);

  1238.     ngx_conf_merge_value(conf->upstream.cache_background_update,
  1239.                               prev->upstream.cache_background_update, 0);

  1240. #endif

  1241.     ngx_conf_merge_value(conf->upstream.pass_request_headers,
  1242.                          prev->upstream.pass_request_headers, 1);
  1243.     ngx_conf_merge_value(conf->upstream.pass_request_body,
  1244.                          prev->upstream.pass_request_body, 1);

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

  1247.     hash.max_size = 512;
  1248.     hash.bucket_size = ngx_align(64, ngx_cacheline_size);
  1249.     hash.name = "scgi_hide_headers_hash";

  1250.     if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
  1251.             &prev->upstream, ngx_http_scgi_hide_headers, &hash)
  1252.         != NGX_OK)
  1253.     {
  1254.         return NGX_CONF_ERROR;
  1255.     }

  1256.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

  1257.     if (clcf->noname
  1258.         && conf->upstream.upstream == NULL && conf->scgi_lengths == NULL)
  1259.     {
  1260.         conf->upstream.upstream = prev->upstream.upstream;
  1261.         conf->scgi_lengths = prev->scgi_lengths;
  1262.         conf->scgi_values = prev->scgi_values;
  1263.     }

  1264.     if (clcf->lmt_excpt && clcf->handler == NULL
  1265.         && (conf->upstream.upstream || conf->scgi_lengths))
  1266.     {
  1267.         clcf->handler = ngx_http_scgi_handler;
  1268.     }

  1269.     if (conf->params_source == NULL) {
  1270.         conf->params = prev->params;
  1271. #if (NGX_HTTP_CACHE)
  1272.         conf->params_cache = prev->params_cache;
  1273. #endif
  1274.         conf->params_source = prev->params_source;
  1275.     }

  1276.     rc = ngx_http_scgi_init_params(cf, conf, &conf->params, NULL);
  1277.     if (rc != NGX_OK) {
  1278.         return NGX_CONF_ERROR;
  1279.     }

  1280. #if (NGX_HTTP_CACHE)

  1281.     if (conf->upstream.cache) {
  1282.         rc = ngx_http_scgi_init_params(cf, conf, &conf->params_cache,
  1283.                                        ngx_http_scgi_cache_headers);
  1284.         if (rc != NGX_OK) {
  1285.             return NGX_CONF_ERROR;
  1286.         }
  1287.     }

  1288. #endif

  1289.     /*
  1290.      * special handling to preserve conf->params in the "http" section
  1291.      * to inherit it to all servers
  1292.      */

  1293.     if (prev->params.hash.buckets == NULL
  1294.         && conf->params_source == prev->params_source)
  1295.     {
  1296.         prev->params = conf->params;
  1297. #if (NGX_HTTP_CACHE)
  1298.         prev->params_cache = conf->params_cache;
  1299. #endif
  1300.     }

  1301.     return NGX_CONF_OK;
  1302. }


  1303. static ngx_int_t
  1304. ngx_http_scgi_init_params(ngx_conf_t *cf, ngx_http_scgi_loc_conf_t *conf,
  1305.     ngx_http_scgi_params_t *params, ngx_keyval_t *default_params)
  1306. {
  1307.     u_char                       *p;
  1308.     size_t                        size;
  1309.     uintptr_t                    *code;
  1310.     ngx_uint_t                    i, nsrc;
  1311.     ngx_array_t                   headers_names, params_merged;
  1312.     ngx_keyval_t                 *h;
  1313.     ngx_hash_key_t               *hk;
  1314.     ngx_hash_init_t               hash;
  1315.     ngx_http_upstream_param_t    *src, *s;
  1316.     ngx_http_script_compile_t     sc;
  1317.     ngx_http_script_copy_code_t  *copy;

  1318.     if (params->hash.buckets) {
  1319.         return NGX_OK;
  1320.     }

  1321.     if (conf->params_source == NULL && default_params == NULL) {
  1322.         params->hash.buckets = (void *) 1;
  1323.         return NGX_OK;
  1324.     }

  1325.     params->lengths = ngx_array_create(cf->pool, 64, 1);
  1326.     if (params->lengths == NULL) {
  1327.         return NGX_ERROR;
  1328.     }

  1329.     params->values = ngx_array_create(cf->pool, 512, 1);
  1330.     if (params->values == NULL) {
  1331.         return NGX_ERROR;
  1332.     }

  1333.     if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
  1334.         != NGX_OK)
  1335.     {
  1336.         return NGX_ERROR;
  1337.     }

  1338.     if (conf->params_source) {
  1339.         src = conf->params_source->elts;
  1340.         nsrc = conf->params_source->nelts;

  1341.     } else {
  1342.         src = NULL;
  1343.         nsrc = 0;
  1344.     }

  1345.     if (default_params) {
  1346.         if (ngx_array_init(&params_merged, cf->temp_pool, 4,
  1347.                            sizeof(ngx_http_upstream_param_t))
  1348.             != NGX_OK)
  1349.         {
  1350.             return NGX_ERROR;
  1351.         }

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

  1353.             s = ngx_array_push(&params_merged);
  1354.             if (s == NULL) {
  1355.                 return NGX_ERROR;
  1356.             }

  1357.             *s = src[i];
  1358.         }

  1359.         h = default_params;

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

  1361.             src = params_merged.elts;
  1362.             nsrc = params_merged.nelts;

  1363.             for (i = 0; i < nsrc; i++) {
  1364.                 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
  1365.                     goto next;
  1366.                 }
  1367.             }

  1368.             s = ngx_array_push(&params_merged);
  1369.             if (s == NULL) {
  1370.                 return NGX_ERROR;
  1371.             }

  1372.             s->key = h->key;
  1373.             s->value = h->value;
  1374.             s->skip_empty = 1;

  1375.         next:

  1376.             h++;
  1377.         }

  1378.         src = params_merged.elts;
  1379.         nsrc = params_merged.nelts;
  1380.     }

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

  1382.         if (src[i].key.len > sizeof("HTTP_") - 1
  1383.             && ngx_strncmp(src[i].key.data, "HTTP_", sizeof("HTTP_") - 1) == 0)
  1384.         {
  1385.             hk = ngx_array_push(&headers_names);
  1386.             if (hk == NULL) {
  1387.                 return NGX_ERROR;
  1388.             }

  1389.             hk->key.len = src[i].key.len - 5;
  1390.             hk->key.data = src[i].key.data + 5;
  1391.             hk->key_hash = ngx_hash_key_lc(hk->key.data, hk->key.len);
  1392.             hk->value = (void *) 1;

  1393.             if (src[i].value.len == 0) {
  1394.                 continue;
  1395.             }
  1396.         }

  1397.         copy = ngx_array_push_n(params->lengths,
  1398.                                 sizeof(ngx_http_script_copy_code_t));
  1399.         if (copy == NULL) {
  1400.             return NGX_ERROR;
  1401.         }

  1402.         copy->code = (ngx_http_script_code_pt) (void *)
  1403.                                                  ngx_http_script_copy_len_code;
  1404.         copy->len = src[i].key.len + 1;

  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].skip_empty;


  1413.         size = (sizeof(ngx_http_script_copy_code_t)
  1414.                 + src[i].key.len + 1 + sizeof(uintptr_t) - 1)
  1415.                & ~(sizeof(uintptr_t) - 1);

  1416.         copy = ngx_array_push_n(params->values, size);
  1417.         if (copy == NULL) {
  1418.             return NGX_ERROR;
  1419.         }

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

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


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

  1425.         sc.cf = cf;
  1426.         sc.source = &src[i].value;
  1427.         sc.flushes = &params->flushes;
  1428.         sc.lengths = &params->lengths;
  1429.         sc.values = &params->values;

  1430.         if (ngx_http_script_compile(&sc) != NGX_OK) {
  1431.             return NGX_ERROR;
  1432.         }

  1433.         code = ngx_array_push_n(params->lengths, sizeof(uintptr_t));
  1434.         if (code == NULL) {
  1435.             return NGX_ERROR;
  1436.         }

  1437.         *code = (uintptr_t) NULL;


  1438.         code = ngx_array_push_n(params->values, sizeof(uintptr_t));
  1439.         if (code == NULL) {
  1440.             return NGX_ERROR;
  1441.         }

  1442.         *code = (uintptr_t) NULL;
  1443.     }

  1444.     code = ngx_array_push_n(params->lengths, sizeof(uintptr_t));
  1445.     if (code == NULL) {
  1446.         return NGX_ERROR;
  1447.     }

  1448.     *code = (uintptr_t) NULL;

  1449.     params->number = headers_names.nelts;

  1450.     hash.hash = &params->hash;
  1451.     hash.key = ngx_hash_key_lc;
  1452.     hash.max_size = 512;
  1453.     hash.bucket_size = 64;
  1454.     hash.name = "scgi_params_hash";
  1455.     hash.pool = cf->pool;
  1456.     hash.temp_pool = NULL;

  1457.     return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts);
  1458. }


  1459. static char *
  1460. ngx_http_scgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1461. {
  1462.     ngx_http_scgi_loc_conf_t *scf = conf;

  1463.     ngx_url_t                   u;
  1464.     ngx_str_t                  *value, *url;
  1465.     ngx_uint_t                  n;
  1466.     ngx_http_core_loc_conf_t   *clcf;
  1467.     ngx_http_script_compile_t   sc;

  1468.     if (scf->upstream.upstream || scf->scgi_lengths) {
  1469.         return "is duplicate";
  1470.     }

  1471.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  1472.     clcf->handler = ngx_http_scgi_handler;

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

  1474.     url = &value[1];

  1475.     n = ngx_http_script_variables_count(url);

  1476.     if (n) {

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

  1478.         sc.cf = cf;
  1479.         sc.source = url;
  1480.         sc.lengths = &scf->scgi_lengths;
  1481.         sc.values = &scf->scgi_values;
  1482.         sc.variables = n;
  1483.         sc.complete_lengths = 1;
  1484.         sc.complete_values = 1;

  1485.         if (ngx_http_script_compile(&sc) != NGX_OK) {
  1486.             return NGX_CONF_ERROR;
  1487.         }

  1488.         return NGX_CONF_OK;
  1489.     }

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

  1491.     u.url = value[1];
  1492.     u.no_resolve = 1;

  1493.     scf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
  1494.     if (scf->upstream.upstream == NULL) {
  1495.         return NGX_CONF_ERROR;
  1496.     }

  1497.     if (clcf->name.len && clcf->name.data[clcf->name.len - 1] == '/') {
  1498.         clcf->auto_redirect = 1;
  1499.     }

  1500.     return NGX_CONF_OK;
  1501. }


  1502. static char *
  1503. ngx_http_scgi_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1504. {
  1505.     ngx_http_scgi_loc_conf_t *scf = conf;

  1506.     ngx_str_t                  *value;
  1507.     ngx_http_script_compile_t   sc;

  1508.     if (scf->upstream.store != NGX_CONF_UNSET) {
  1509.         return "is duplicate";
  1510.     }

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

  1512.     if (ngx_strcmp(value[1].data, "off") == 0) {
  1513.         scf->upstream.store = 0;
  1514.         return NGX_CONF_OK;
  1515.     }

  1516.     if (value[1].len == 0) {
  1517.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "empty path");
  1518.         return NGX_CONF_ERROR;
  1519.     }

  1520. #if (NGX_HTTP_CACHE)
  1521.     if (scf->upstream.cache > 0) {
  1522.         return "is incompatible with \"scgi_cache\"";
  1523.     }
  1524. #endif

  1525.     scf->upstream.store = 1;

  1526.     if (ngx_strcmp(value[1].data, "on") == 0) {
  1527.         return NGX_CONF_OK;
  1528.     }

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

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

  1532.     sc.cf = cf;
  1533.     sc.source = &value[1];
  1534.     sc.lengths = &scf->upstream.store_lengths;
  1535.     sc.values = &scf->upstream.store_values;
  1536.     sc.variables = ngx_http_script_variables_count(&value[1]);
  1537.     sc.complete_lengths = 1;
  1538.     sc.complete_values = 1;

  1539.     if (ngx_http_script_compile(&sc) != NGX_OK) {
  1540.         return NGX_CONF_ERROR;
  1541.     }

  1542.     return NGX_CONF_OK;
  1543. }


  1544. #if (NGX_HTTP_CACHE)

  1545. static char *
  1546. ngx_http_scgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1547. {
  1548.     ngx_http_scgi_loc_conf_t *scf = conf;

  1549.     ngx_str_t                         *value;
  1550.     ngx_http_complex_value_t           cv;
  1551.     ngx_http_compile_complex_value_t   ccv;

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

  1553.     if (scf->upstream.cache != NGX_CONF_UNSET) {
  1554.         return "is duplicate";
  1555.     }

  1556.     if (ngx_strcmp(value[1].data, "off") == 0) {
  1557.         scf->upstream.cache = 0;
  1558.         return NGX_CONF_OK;
  1559.     }

  1560.     if (scf->upstream.store > 0) {
  1561.         return "is incompatible with \"scgi_store\"";
  1562.     }

  1563.     scf->upstream.cache = 1;

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

  1565.     ccv.cf = cf;
  1566.     ccv.value = &value[1];
  1567.     ccv.complex_value = &cv;

  1568.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  1569.         return NGX_CONF_ERROR;
  1570.     }

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

  1572.         scf->upstream.cache_value = ngx_palloc(cf->pool,
  1573.                                              sizeof(ngx_http_complex_value_t));
  1574.         if (scf->upstream.cache_value == NULL) {
  1575.             return NGX_CONF_ERROR;
  1576.         }

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

  1578.         return NGX_CONF_OK;
  1579.     }

  1580.     scf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0,
  1581.                                                      &ngx_http_scgi_module);
  1582.     if (scf->upstream.cache_zone == NULL) {
  1583.         return NGX_CONF_ERROR;
  1584.     }

  1585.     return NGX_CONF_OK;
  1586. }


  1587. static char *
  1588. ngx_http_scgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  1589. {
  1590.     ngx_http_scgi_loc_conf_t *scf = conf;

  1591.     ngx_str_t                         *value;
  1592.     ngx_http_compile_complex_value_t   ccv;

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

  1594.     if (scf->cache_key.value.data) {
  1595.         return "is duplicate";
  1596.     }

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

  1598.     ccv.cf = cf;
  1599.     ccv.value = &value[1];
  1600.     ccv.complex_value = &scf->cache_key;

  1601.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  1602.         return NGX_CONF_ERROR;
  1603.     }

  1604.     return NGX_CONF_OK;
  1605. }

  1606. #endif