src/http/modules/ngx_http_proxy_module.c - nginx

Global variables defined

Data types defined

Functions defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


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


  9. #define  NGX_HTTP_PROXY_COOKIE_SECURE           0x0001
  10. #define  NGX_HTTP_PROXY_COOKIE_SECURE_ON        0x0002
  11. #define  NGX_HTTP_PROXY_COOKIE_SECURE_OFF       0x0004
  12. #define  NGX_HTTP_PROXY_COOKIE_HTTPONLY         0x0008
  13. #define  NGX_HTTP_PROXY_COOKIE_HTTPONLY_ON      0x0010
  14. #define  NGX_HTTP_PROXY_COOKIE_HTTPONLY_OFF     0x0020
  15. #define  NGX_HTTP_PROXY_COOKIE_SAMESITE         0x0040
  16. #define  NGX_HTTP_PROXY_COOKIE_SAMESITE_STRICT  0x0080
  17. #define  NGX_HTTP_PROXY_COOKIE_SAMESITE_LAX     0x0100
  18. #define  NGX_HTTP_PROXY_COOKIE_SAMESITE_NONE    0x0200
  19. #define  NGX_HTTP_PROXY_COOKIE_SAMESITE_OFF     0x0400


  20. typedef struct ngx_http_proxy_rewrite_s  ngx_http_proxy_rewrite_t;

  21. typedef ngx_int_t (*ngx_http_proxy_rewrite_pt)(ngx_http_request_t *r,
  22.     ngx_str_t *value, size_t prefix, size_t len,
  23.     ngx_http_proxy_rewrite_t *pr);

  24. struct ngx_http_proxy_rewrite_s {
  25.     ngx_http_proxy_rewrite_pt      handler;

  26.     union {
  27.         ngx_http_complex_value_t   complex;
  28. #if (NGX_PCRE)
  29.         ngx_http_regex_t          *regex;
  30. #endif
  31.     } pattern;

  32.     ngx_http_complex_value_t       replacement;
  33. };


  34. typedef struct {
  35.     union {
  36.         ngx_http_complex_value_t   complex;
  37. #if (NGX_PCRE)
  38.         ngx_http_regex_t          *regex;
  39. #endif
  40.     } cookie;

  41.     ngx_array_t                    flags_values;
  42.     ngx_uint_t                     regex;
  43. } ngx_http_proxy_cookie_flags_t;


  44. static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r);
  45. static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r);
  46. static ngx_int_t ngx_http_proxy_body_output_filter(void *data, ngx_chain_t *in);
  47. static ngx_int_t ngx_http_proxy_process_status_line(ngx_http_request_t *r);
  48. static ngx_int_t ngx_http_proxy_process_header(ngx_http_request_t *r);
  49. static ngx_int_t ngx_http_proxy_input_filter_init(void *data);
  50. static ngx_int_t ngx_http_proxy_copy_filter(ngx_event_pipe_t *p,
  51.     ngx_buf_t *buf);
  52. static ngx_int_t ngx_http_proxy_chunked_filter(ngx_event_pipe_t *p,
  53.     ngx_buf_t *buf);
  54. static ngx_int_t ngx_http_proxy_non_buffered_copy_filter(void *data,
  55.     ssize_t bytes);
  56. static ngx_int_t ngx_http_proxy_non_buffered_chunked_filter(void *data,
  57.     ssize_t bytes);
  58. static ngx_int_t ngx_http_proxy_process_trailer(ngx_http_request_t *r,
  59.     ngx_buf_t *buf);
  60. static void ngx_http_proxy_abort_request(ngx_http_request_t *r);
  61. static void ngx_http_proxy_finalize_request(ngx_http_request_t *r,
  62.     ngx_int_t rc);

  63. static ngx_int_t ngx_http_proxy_host_variable(ngx_http_request_t *r,
  64.     ngx_http_variable_value_t *v, uintptr_t data);
  65. static ngx_int_t ngx_http_proxy_port_variable(ngx_http_request_t *r,
  66.     ngx_http_variable_value_t *v, uintptr_t data);
  67. static ngx_int_t
  68.     ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
  69.     ngx_http_variable_value_t *v, uintptr_t data);
  70. static ngx_int_t
  71.     ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
  72.     ngx_http_variable_value_t *v, uintptr_t data);
  73. static ngx_int_t ngx_http_proxy_internal_chunked_variable(ngx_http_request_t *r,
  74.     ngx_http_variable_value_t *v, uintptr_t data);
  75. static ngx_int_t ngx_http_proxy_parse_cookie(ngx_str_t *value,
  76.     ngx_array_t *attrs);
  77. static ngx_int_t ngx_http_proxy_rewrite_cookie_value(ngx_http_request_t *r,
  78.     ngx_str_t *value, ngx_array_t *rewrites);
  79. static ngx_int_t ngx_http_proxy_rewrite_cookie_flags(ngx_http_request_t *r,
  80.     ngx_array_t *attrs, ngx_array_t *flags);
  81. static ngx_int_t ngx_http_proxy_edit_cookie_flags(ngx_http_request_t *r,
  82.     ngx_array_t *attrs, ngx_uint_t flags);
  83. static ngx_int_t ngx_http_proxy_rewrite(ngx_http_request_t *r,
  84.     ngx_str_t *value, size_t prefix, size_t len, ngx_str_t *replacement);

  85. static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf);
  86. static void *ngx_http_proxy_create_main_conf(ngx_conf_t *cf);
  87. static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
  88. static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
  89.     void *parent, void *child);
  90. static ngx_int_t ngx_http_proxy_init_headers(ngx_conf_t *cf,
  91.     ngx_http_proxy_loc_conf_t *conf, ngx_http_proxy_headers_t *headers,
  92.     ngx_keyval_t *default_headers);

  93. static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd,
  94.     void *conf);
  95. static char *ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd,
  96.     void *conf);
  97. static char *ngx_http_proxy_cookie_domain(ngx_conf_t *cf, ngx_command_t *cmd,
  98.     void *conf);
  99. static char *ngx_http_proxy_cookie_path(ngx_conf_t *cf, ngx_command_t *cmd,
  100.     void *conf);
  101. static char *ngx_http_proxy_cookie_flags(ngx_conf_t *cf, ngx_command_t *cmd,
  102.     void *conf);
  103. static char *ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd,
  104.     void *conf);
  105. #if (NGX_HTTP_CACHE)
  106. static char *ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd,
  107.     void *conf);
  108. static char *ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd,
  109.     void *conf);
  110. #endif
  111. #if (NGX_HTTP_SSL)
  112. static char *ngx_http_proxy_ssl_certificate_cache(ngx_conf_t *cf,
  113.     ngx_command_t *cmd, void *conf);
  114. static char *ngx_http_proxy_ssl_password_file(ngx_conf_t *cf,
  115.     ngx_command_t *cmd, void *conf);
  116. #endif

  117. static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data);
  118. #if (NGX_HTTP_SSL)
  119. static char *ngx_http_proxy_ssl_conf_command_check(ngx_conf_t *cf, void *post,
  120.     void *data);
  121. #endif

  122. static ngx_int_t ngx_http_proxy_rewrite_regex(ngx_conf_t *cf,
  123.     ngx_http_proxy_rewrite_t *pr, ngx_str_t *regex, ngx_uint_t caseless);

  124. #if (NGX_HTTP_SSL)
  125. static ngx_int_t ngx_http_proxy_merge_ssl(ngx_conf_t *cf,
  126.     ngx_http_proxy_loc_conf_t *conf, ngx_http_proxy_loc_conf_t *prev);
  127. static ngx_int_t ngx_http_proxy_set_ssl(ngx_conf_t *cf,
  128.     ngx_http_proxy_loc_conf_t *plcf);
  129. #endif
  130. static void ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v);


  131. static ngx_conf_post_t  ngx_http_proxy_lowat_post =
  132.     { ngx_http_proxy_lowat_check };


  133. static ngx_conf_bitmask_t  ngx_http_proxy_next_upstream_masks[] = {
  134.     { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
  135.     { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
  136.     { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
  137.     { ngx_string("non_idempotent"), NGX_HTTP_UPSTREAM_FT_NON_IDEMPOTENT },
  138.     { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
  139.     { ngx_string("http_502"), NGX_HTTP_UPSTREAM_FT_HTTP_502 },
  140.     { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
  141.     { ngx_string("http_504"), NGX_HTTP_UPSTREAM_FT_HTTP_504 },
  142.     { ngx_string("http_403"), NGX_HTTP_UPSTREAM_FT_HTTP_403 },
  143.     { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
  144.     { ngx_string("http_429"), NGX_HTTP_UPSTREAM_FT_HTTP_429 },
  145.     { ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING },
  146.     { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
  147.     { ngx_null_string, 0 }
  148. };


  149. #if (NGX_HTTP_SSL)

  150. static ngx_conf_bitmask_t  ngx_http_proxy_ssl_protocols[] = {
  151.     { ngx_string("SSLv2"), NGX_SSL_SSLv2 },
  152.     { ngx_string("SSLv3"), NGX_SSL_SSLv3 },
  153.     { ngx_string("TLSv1"), NGX_SSL_TLSv1 },
  154.     { ngx_string("TLSv1.1"), NGX_SSL_TLSv1_1 },
  155.     { ngx_string("TLSv1.2"), NGX_SSL_TLSv1_2 },
  156.     { ngx_string("TLSv1.3"), NGX_SSL_TLSv1_3 },
  157.     { ngx_null_string, 0 }
  158. };

  159. static ngx_conf_post_t  ngx_http_proxy_ssl_conf_command_post =
  160.     { ngx_http_proxy_ssl_conf_command_check };

  161. #endif


  162. static ngx_conf_enum_t  ngx_http_proxy_http_version[] = {
  163.     { ngx_string("1.0"), NGX_HTTP_VERSION_10 },
  164.     { ngx_string("1.1"), NGX_HTTP_VERSION_11 },
  165. #if (NGX_HTTP_V2)
  166.     { ngx_string("2"), NGX_HTTP_VERSION_20 },
  167. #endif
  168.     { ngx_null_string, 0 }
  169. };


  170. ngx_module_t  ngx_http_proxy_module;


  171. static ngx_command_t  ngx_http_proxy_commands[] = {

  172.     { ngx_string("proxy_pass"),
  173.       NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1,
  174.       ngx_http_proxy_pass,
  175.       NGX_HTTP_LOC_CONF_OFFSET,
  176.       0,
  177.       NULL },

  178.     { ngx_string("proxy_redirect"),
  179.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
  180.       ngx_http_proxy_redirect,
  181.       NGX_HTTP_LOC_CONF_OFFSET,
  182.       0,
  183.       NULL },

  184.     { ngx_string("proxy_cookie_domain"),
  185.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
  186.       ngx_http_proxy_cookie_domain,
  187.       NGX_HTTP_LOC_CONF_OFFSET,
  188.       0,
  189.       NULL },

  190.     { ngx_string("proxy_cookie_path"),
  191.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
  192.       ngx_http_proxy_cookie_path,
  193.       NGX_HTTP_LOC_CONF_OFFSET,
  194.       0,
  195.       NULL },

  196.     { ngx_string("proxy_cookie_flags"),
  197.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
  198.       ngx_http_proxy_cookie_flags,
  199.       NGX_HTTP_LOC_CONF_OFFSET,
  200.       0,
  201.       NULL },

  202.     { ngx_string("proxy_store"),
  203.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  204.       ngx_http_proxy_store,
  205.       NGX_HTTP_LOC_CONF_OFFSET,
  206.       0,
  207.       NULL },

  208.     { ngx_string("proxy_store_access"),
  209.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
  210.       ngx_conf_set_access_slot,
  211.       NGX_HTTP_LOC_CONF_OFFSET,
  212.       offsetof(ngx_http_proxy_loc_conf_t, upstream.store_access),
  213.       NULL },

  214.     { ngx_string("proxy_buffering"),
  215.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  216.       ngx_conf_set_flag_slot,
  217.       NGX_HTTP_LOC_CONF_OFFSET,
  218.       offsetof(ngx_http_proxy_loc_conf_t, upstream.buffering),
  219.       NULL },

  220.     { ngx_string("proxy_request_buffering"),
  221.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  222.       ngx_conf_set_flag_slot,
  223.       NGX_HTTP_LOC_CONF_OFFSET,
  224.       offsetof(ngx_http_proxy_loc_conf_t, upstream.request_buffering),
  225.       NULL },

  226.     { ngx_string("proxy_ignore_client_abort"),
  227.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  228.       ngx_conf_set_flag_slot,
  229.       NGX_HTTP_LOC_CONF_OFFSET,
  230.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_client_abort),
  231.       NULL },

  232.     { ngx_string("proxy_bind"),
  233.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
  234.       ngx_http_upstream_bind_set_slot,
  235.       NGX_HTTP_LOC_CONF_OFFSET,
  236.       offsetof(ngx_http_proxy_loc_conf_t, upstream.local),
  237.       NULL },

  238.     { ngx_string("proxy_socket_keepalive"),
  239.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  240.       ngx_conf_set_flag_slot,
  241.       NGX_HTTP_LOC_CONF_OFFSET,
  242.       offsetof(ngx_http_proxy_loc_conf_t, upstream.socket_keepalive),
  243.       NULL },

  244.     { ngx_string("proxy_connect_timeout"),
  245.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  246.       ngx_conf_set_msec_slot,
  247.       NGX_HTTP_LOC_CONF_OFFSET,
  248.       offsetof(ngx_http_proxy_loc_conf_t, upstream.connect_timeout),
  249.       NULL },

  250.     { ngx_string("proxy_send_timeout"),
  251.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  252.       ngx_conf_set_msec_slot,
  253.       NGX_HTTP_LOC_CONF_OFFSET,
  254.       offsetof(ngx_http_proxy_loc_conf_t, upstream.send_timeout),
  255.       NULL },

  256.     { ngx_string("proxy_send_lowat"),
  257.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  258.       ngx_conf_set_size_slot,
  259.       NGX_HTTP_LOC_CONF_OFFSET,
  260.       offsetof(ngx_http_proxy_loc_conf_t, upstream.send_lowat),
  261.       &ngx_http_proxy_lowat_post },

  262.     { ngx_string("proxy_intercept_errors"),
  263.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  264.       ngx_conf_set_flag_slot,
  265.       NGX_HTTP_LOC_CONF_OFFSET,
  266.       offsetof(ngx_http_proxy_loc_conf_t, upstream.intercept_errors),
  267.       NULL },

  268.     { ngx_string("proxy_set_header"),
  269.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
  270.       ngx_conf_set_keyval_slot,
  271.       NGX_HTTP_LOC_CONF_OFFSET,
  272.       offsetof(ngx_http_proxy_loc_conf_t, headers_source),
  273.       NULL },

  274.     { ngx_string("proxy_headers_hash_max_size"),
  275.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  276.       ngx_conf_set_num_slot,
  277.       NGX_HTTP_LOC_CONF_OFFSET,
  278.       offsetof(ngx_http_proxy_loc_conf_t, headers_hash_max_size),
  279.       NULL },

  280.     { ngx_string("proxy_headers_hash_bucket_size"),
  281.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  282.       ngx_conf_set_num_slot,
  283.       NGX_HTTP_LOC_CONF_OFFSET,
  284.       offsetof(ngx_http_proxy_loc_conf_t, headers_hash_bucket_size),
  285.       NULL },

  286.     { ngx_string("proxy_set_body"),
  287.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  288.       ngx_conf_set_str_slot,
  289.       NGX_HTTP_LOC_CONF_OFFSET,
  290.       offsetof(ngx_http_proxy_loc_conf_t, body_source),
  291.       NULL },

  292.     { ngx_string("proxy_method"),
  293.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  294.       ngx_http_set_complex_value_slot,
  295.       NGX_HTTP_LOC_CONF_OFFSET,
  296.       offsetof(ngx_http_proxy_loc_conf_t, method),
  297.       NULL },

  298.     { ngx_string("proxy_pass_request_headers"),
  299.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  300.       ngx_conf_set_flag_slot,
  301.       NGX_HTTP_LOC_CONF_OFFSET,
  302.       offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_headers),
  303.       NULL },

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

  310.     { ngx_string("proxy_pass_trailers"),
  311.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  312.       ngx_conf_set_flag_slot,
  313.       NGX_HTTP_LOC_CONF_OFFSET,
  314.       offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_trailers),
  315.       NULL },

  316.     { ngx_string("proxy_buffer_size"),
  317.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  318.       ngx_conf_set_size_slot,
  319.       NGX_HTTP_LOC_CONF_OFFSET,
  320.       offsetof(ngx_http_proxy_loc_conf_t, upstream.buffer_size),
  321.       NULL },

  322.     { ngx_string("proxy_read_timeout"),
  323.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  324.       ngx_conf_set_msec_slot,
  325.       NGX_HTTP_LOC_CONF_OFFSET,
  326.       offsetof(ngx_http_proxy_loc_conf_t, upstream.read_timeout),
  327.       NULL },

  328.     { ngx_string("proxy_buffers"),
  329.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
  330.       ngx_conf_set_bufs_slot,
  331.       NGX_HTTP_LOC_CONF_OFFSET,
  332.       offsetof(ngx_http_proxy_loc_conf_t, upstream.bufs),
  333.       NULL },

  334.     { ngx_string("proxy_busy_buffers_size"),
  335.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  336.       ngx_conf_set_size_slot,
  337.       NGX_HTTP_LOC_CONF_OFFSET,
  338.       offsetof(ngx_http_proxy_loc_conf_t, upstream.busy_buffers_size_conf),
  339.       NULL },

  340.     { ngx_string("proxy_force_ranges"),
  341.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  342.       ngx_conf_set_flag_slot,
  343.       NGX_HTTP_LOC_CONF_OFFSET,
  344.       offsetof(ngx_http_proxy_loc_conf_t, upstream.force_ranges),
  345.       NULL },

  346.     { ngx_string("proxy_limit_rate"),
  347.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  348.       ngx_http_set_complex_value_size_slot,
  349.       NGX_HTTP_LOC_CONF_OFFSET,
  350.       offsetof(ngx_http_proxy_loc_conf_t, upstream.limit_rate),
  351.       NULL },

  352. #if (NGX_HTTP_CACHE)

  353.     { ngx_string("proxy_cache"),
  354.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  355.       ngx_http_proxy_cache,
  356.       NGX_HTTP_LOC_CONF_OFFSET,
  357.       0,
  358.       NULL },

  359.     { ngx_string("proxy_cache_key"),
  360.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  361.       ngx_http_proxy_cache_key,
  362.       NGX_HTTP_LOC_CONF_OFFSET,
  363.       0,
  364.       NULL },

  365.     { ngx_string("proxy_cache_path"),
  366.       NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
  367.       ngx_http_file_cache_set_slot,
  368.       NGX_HTTP_MAIN_CONF_OFFSET,
  369.       offsetof(ngx_http_proxy_main_conf_t, caches),
  370.       &ngx_http_proxy_module },

  371.     { ngx_string("proxy_cache_bypass"),
  372.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  373.       ngx_http_set_predicate_slot,
  374.       NGX_HTTP_LOC_CONF_OFFSET,
  375.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_bypass),
  376.       NULL },

  377.     { ngx_string("proxy_no_cache"),
  378.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  379.       ngx_http_set_predicate_slot,
  380.       NGX_HTTP_LOC_CONF_OFFSET,
  381.       offsetof(ngx_http_proxy_loc_conf_t, upstream.no_cache),
  382.       NULL },

  383.     { ngx_string("proxy_cache_valid"),
  384.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  385.       ngx_http_file_cache_valid_set_slot,
  386.       NGX_HTTP_LOC_CONF_OFFSET,
  387.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_valid),
  388.       NULL },

  389.     { ngx_string("proxy_cache_min_uses"),
  390.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  391.       ngx_conf_set_num_slot,
  392.       NGX_HTTP_LOC_CONF_OFFSET,
  393.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_min_uses),
  394.       NULL },

  395.     { ngx_string("proxy_cache_max_range_offset"),
  396.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  397.       ngx_conf_set_off_slot,
  398.       NGX_HTTP_LOC_CONF_OFFSET,
  399.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_max_range_offset),
  400.       NULL },

  401.     { ngx_string("proxy_cache_use_stale"),
  402.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  403.       ngx_conf_set_bitmask_slot,
  404.       NGX_HTTP_LOC_CONF_OFFSET,
  405.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_use_stale),
  406.       &ngx_http_proxy_next_upstream_masks },

  407.     { ngx_string("proxy_cache_methods"),
  408.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  409.       ngx_conf_set_bitmask_slot,
  410.       NGX_HTTP_LOC_CONF_OFFSET,
  411.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_methods),
  412.       &ngx_http_upstream_cache_method_mask },

  413.     { ngx_string("proxy_cache_lock"),
  414.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  415.       ngx_conf_set_flag_slot,
  416.       NGX_HTTP_LOC_CONF_OFFSET,
  417.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock),
  418.       NULL },

  419.     { ngx_string("proxy_cache_lock_timeout"),
  420.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  421.       ngx_conf_set_msec_slot,
  422.       NGX_HTTP_LOC_CONF_OFFSET,
  423.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock_timeout),
  424.       NULL },

  425.     { ngx_string("proxy_cache_lock_age"),
  426.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  427.       ngx_conf_set_msec_slot,
  428.       NGX_HTTP_LOC_CONF_OFFSET,
  429.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock_age),
  430.       NULL },

  431.     { ngx_string("proxy_cache_revalidate"),
  432.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  433.       ngx_conf_set_flag_slot,
  434.       NGX_HTTP_LOC_CONF_OFFSET,
  435.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_revalidate),
  436.       NULL },

  437.     { ngx_string("proxy_cache_convert_head"),
  438.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  439.       ngx_conf_set_flag_slot,
  440.       NGX_HTTP_LOC_CONF_OFFSET,
  441.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_convert_head),
  442.       NULL },

  443.     { ngx_string("proxy_cache_background_update"),
  444.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  445.       ngx_conf_set_flag_slot,
  446.       NGX_HTTP_LOC_CONF_OFFSET,
  447.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_background_update),
  448.       NULL },

  449. #endif

  450.     { ngx_string("proxy_temp_path"),
  451.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
  452.       ngx_conf_set_path_slot,
  453.       NGX_HTTP_LOC_CONF_OFFSET,
  454.       offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_path),
  455.       NULL },

  456.     { ngx_string("proxy_max_temp_file_size"),
  457.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  458.       ngx_conf_set_size_slot,
  459.       NGX_HTTP_LOC_CONF_OFFSET,
  460.       offsetof(ngx_http_proxy_loc_conf_t, upstream.max_temp_file_size_conf),
  461.       NULL },

  462.     { ngx_string("proxy_temp_file_write_size"),
  463.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  464.       ngx_conf_set_size_slot,
  465.       NGX_HTTP_LOC_CONF_OFFSET,
  466.       offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_file_write_size_conf),
  467.       NULL },

  468.     { ngx_string("proxy_next_upstream"),
  469.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  470.       ngx_conf_set_bitmask_slot,
  471.       NGX_HTTP_LOC_CONF_OFFSET,
  472.       offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream),
  473.       &ngx_http_proxy_next_upstream_masks },

  474.     { ngx_string("proxy_next_upstream_tries"),
  475.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  476.       ngx_conf_set_num_slot,
  477.       NGX_HTTP_LOC_CONF_OFFSET,
  478.       offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream_tries),
  479.       NULL },

  480.     { ngx_string("proxy_next_upstream_timeout"),
  481.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  482.       ngx_conf_set_msec_slot,
  483.       NGX_HTTP_LOC_CONF_OFFSET,
  484.       offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream_timeout),
  485.       NULL },

  486.     { ngx_string("proxy_pass_header"),
  487.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  488.       ngx_conf_set_str_array_slot,
  489.       NGX_HTTP_LOC_CONF_OFFSET,
  490.       offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_headers),
  491.       NULL },

  492.     { ngx_string("proxy_hide_header"),
  493.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  494.       ngx_conf_set_str_array_slot,
  495.       NGX_HTTP_LOC_CONF_OFFSET,
  496.       offsetof(ngx_http_proxy_loc_conf_t, upstream.hide_headers),
  497.       NULL },

  498.     { ngx_string("proxy_ignore_headers"),
  499.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  500.       ngx_conf_set_bitmask_slot,
  501.       NGX_HTTP_LOC_CONF_OFFSET,
  502.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_headers),
  503.       &ngx_http_upstream_ignore_headers_masks },

  504.     { ngx_string("proxy_http_version"),
  505.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  506.       ngx_conf_set_enum_slot,
  507.       NGX_HTTP_LOC_CONF_OFFSET,
  508.       offsetof(ngx_http_proxy_loc_conf_t, http_version),
  509.       &ngx_http_proxy_http_version },

  510. #if (NGX_HTTP_SSL)

  511.     { ngx_string("proxy_ssl_session_reuse"),
  512.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  513.       ngx_conf_set_flag_slot,
  514.       NGX_HTTP_LOC_CONF_OFFSET,
  515.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_session_reuse),
  516.       NULL },

  517.     { ngx_string("proxy_ssl_protocols"),
  518.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  519.       ngx_conf_set_bitmask_slot,
  520.       NGX_HTTP_LOC_CONF_OFFSET,
  521.       offsetof(ngx_http_proxy_loc_conf_t, ssl_protocols),
  522.       &ngx_http_proxy_ssl_protocols },

  523.     { ngx_string("proxy_ssl_ciphers"),
  524.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  525.       ngx_conf_set_str_slot,
  526.       NGX_HTTP_LOC_CONF_OFFSET,
  527.       offsetof(ngx_http_proxy_loc_conf_t, ssl_ciphers),
  528.       NULL },

  529.     { ngx_string("proxy_ssl_name"),
  530.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  531.       ngx_http_set_complex_value_slot,
  532.       NGX_HTTP_LOC_CONF_OFFSET,
  533.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_name),
  534.       NULL },

  535.     { ngx_string("proxy_ssl_server_name"),
  536.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  537.       ngx_conf_set_flag_slot,
  538.       NGX_HTTP_LOC_CONF_OFFSET,
  539.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_server_name),
  540.       NULL },

  541.     { ngx_string("proxy_ssl_verify"),
  542.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  543.       ngx_conf_set_flag_slot,
  544.       NGX_HTTP_LOC_CONF_OFFSET,
  545.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_verify),
  546.       NULL },

  547.     { ngx_string("proxy_ssl_verify_depth"),
  548.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  549.       ngx_conf_set_num_slot,
  550.       NGX_HTTP_LOC_CONF_OFFSET,
  551.       offsetof(ngx_http_proxy_loc_conf_t, ssl_verify_depth),
  552.       NULL },

  553.     { ngx_string("proxy_ssl_trusted_certificate"),
  554.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  555.       ngx_conf_set_str_slot,
  556.       NGX_HTTP_LOC_CONF_OFFSET,
  557.       offsetof(ngx_http_proxy_loc_conf_t, ssl_trusted_certificate),
  558.       NULL },

  559.     { ngx_string("proxy_ssl_crl"),
  560.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  561.       ngx_conf_set_str_slot,
  562.       NGX_HTTP_LOC_CONF_OFFSET,
  563.       offsetof(ngx_http_proxy_loc_conf_t, ssl_crl),
  564.       NULL },

  565.     { ngx_string("proxy_ssl_certificate"),
  566.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  567.       ngx_http_set_complex_value_zero_slot,
  568.       NGX_HTTP_LOC_CONF_OFFSET,
  569.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_certificate),
  570.       NULL },

  571.     { ngx_string("proxy_ssl_certificate_key"),
  572.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  573.       ngx_http_set_complex_value_zero_slot,
  574.       NGX_HTTP_LOC_CONF_OFFSET,
  575.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_certificate_key),
  576.       NULL },

  577.     { ngx_string("proxy_ssl_certificate_cache"),
  578.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
  579.       ngx_http_proxy_ssl_certificate_cache,
  580.       NGX_HTTP_LOC_CONF_OFFSET,
  581.       0,
  582.       NULL },

  583.     { ngx_string("proxy_ssl_password_file"),
  584.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  585.       ngx_http_proxy_ssl_password_file,
  586.       NGX_HTTP_LOC_CONF_OFFSET,
  587.       0,
  588.       NULL },

  589.     { ngx_string("proxy_ssl_conf_command"),
  590.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
  591.       ngx_conf_set_keyval_slot,
  592.       NGX_HTTP_LOC_CONF_OFFSET,
  593.       offsetof(ngx_http_proxy_loc_conf_t, ssl_conf_commands),
  594.       &ngx_http_proxy_ssl_conf_command_post },

  595. #endif

  596.       ngx_null_command
  597. };


  598. static ngx_http_module_t  ngx_http_proxy_module_ctx = {
  599.     ngx_http_proxy_add_variables,          /* preconfiguration */
  600.     NULL,                                  /* postconfiguration */

  601.     ngx_http_proxy_create_main_conf,       /* create main configuration */
  602.     NULL,                                  /* init main configuration */

  603.     NULL,                                  /* create server configuration */
  604.     NULL,                                  /* merge server configuration */

  605.     ngx_http_proxy_create_loc_conf,        /* create location configuration */
  606.     ngx_http_proxy_merge_loc_conf          /* merge location configuration */
  607. };


  608. ngx_module_t  ngx_http_proxy_module = {
  609.     NGX_MODULE_V1,
  610.     &ngx_http_proxy_module_ctx,            /* module context */
  611.     ngx_http_proxy_commands,               /* module directives */
  612.     NGX_HTTP_MODULE,                       /* module type */
  613.     NULL,                                  /* init master */
  614.     NULL,                                  /* init module */
  615.     NULL,                                  /* init process */
  616.     NULL,                                  /* init thread */
  617.     NULL,                                  /* exit thread */
  618.     NULL,                                  /* exit process */
  619.     NULL,                                  /* exit master */
  620.     NGX_MODULE_V1_PADDING
  621. };


  622. static char  ngx_http_proxy_version[] = " HTTP/1.0" CRLF;
  623. static char  ngx_http_proxy_version_11[] = " HTTP/1.1" CRLF;


  624. static ngx_keyval_t  ngx_http_proxy_headers[] = {
  625.     { ngx_string("Host"), ngx_string("$proxy_internal_host") },
  626.     { ngx_string("Connection"), ngx_string("") },
  627.     { ngx_string("Proxy-Connection"), ngx_string("") },
  628.     { ngx_string("Content-Length"), ngx_string("$proxy_internal_body_length") },
  629.     { ngx_string("Transfer-Encoding"), ngx_string("$proxy_internal_chunked") },
  630.     { ngx_string("TE"), ngx_string("") },
  631.     { ngx_string("Keep-Alive"), ngx_string("") },
  632.     { ngx_string("Expect"), ngx_string("") },
  633.     { ngx_string("Upgrade"), ngx_string("") },
  634.     { ngx_null_string, ngx_null_string }
  635. };


  636. static ngx_str_t  ngx_http_proxy_hide_headers[] = {
  637.     ngx_string("Date"),
  638.     ngx_string("Server"),
  639.     ngx_string("X-Pad"),
  640.     ngx_string("X-Accel-Expires"),
  641.     ngx_string("X-Accel-Redirect"),
  642.     ngx_string("X-Accel-Limit-Rate"),
  643.     ngx_string("X-Accel-Buffering"),
  644.     ngx_string("X-Accel-Charset"),
  645.     ngx_null_string
  646. };


  647. #if (NGX_HTTP_CACHE)

  648. static ngx_keyval_t  ngx_http_proxy_cache_headers[] = {
  649.     { ngx_string("Host"), ngx_string("$proxy_internal_host") },
  650.     { ngx_string("Connection"), ngx_string("") },
  651.     { ngx_string("Proxy-Connection"), ngx_string("") },
  652.     { ngx_string("Content-Length"), ngx_string("$proxy_internal_body_length") },
  653.     { ngx_string("Transfer-Encoding"), ngx_string("$proxy_internal_chunked") },
  654.     { ngx_string("TE"), ngx_string("") },
  655.     { ngx_string("Keep-Alive"), ngx_string("") },
  656.     { ngx_string("Expect"), ngx_string("") },
  657.     { ngx_string("Upgrade"), ngx_string("") },
  658.     { ngx_string("If-Modified-Since"),
  659.       ngx_string("$upstream_cache_last_modified") },
  660.     { ngx_string("If-Unmodified-Since"), ngx_string("") },
  661.     { ngx_string("If-None-Match"), ngx_string("$upstream_cache_etag") },
  662.     { ngx_string("If-Match"), ngx_string("") },
  663.     { ngx_string("Range"), ngx_string("") },
  664.     { ngx_string("If-Range"), ngx_string("") },
  665.     { ngx_null_string, ngx_null_string }
  666. };

  667. #endif


  668. static ngx_http_variable_t  ngx_http_proxy_vars[] = {

  669.     { ngx_string("proxy_host"), NULL, ngx_http_proxy_host_variable, 0,
  670.       NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },

  671.     { ngx_string("proxy_port"), NULL, ngx_http_proxy_port_variable, 0,
  672.       NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },

  673.     { ngx_string("proxy_add_x_forwarded_for"), NULL,
  674.       ngx_http_proxy_add_x_forwarded_for_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },

  675. #if 0
  676.     { ngx_string("proxy_add_via"), NULL, NULL, 0, NGX_HTTP_VAR_NOHASH, 0 },
  677. #endif

  678.     { ngx_string("proxy_internal_host"), NULL,
  679.       ngx_http_proxy_host_variable, 1,
  680.       NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },

  681.     { ngx_string("proxy_internal_body_length"), NULL,
  682.       ngx_http_proxy_internal_body_length_variable, 0,
  683.       NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },

  684.     { ngx_string("proxy_internal_chunked"), NULL,
  685.       ngx_http_proxy_internal_chunked_variable, 0,
  686.       NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },

  687.       ngx_http_null_variable
  688. };


  689. static ngx_path_init_t  ngx_http_proxy_temp_path = {
  690.     ngx_string(NGX_HTTP_PROXY_TEMP_PATH), { 1, 2, 0 }
  691. };


  692. static ngx_conf_bitmask_t  ngx_http_proxy_cookie_flags_masks[] = {

  693.     { ngx_string("secure"),
  694.       NGX_HTTP_PROXY_COOKIE_SECURE|NGX_HTTP_PROXY_COOKIE_SECURE_ON },

  695.     { ngx_string("nosecure"),
  696.       NGX_HTTP_PROXY_COOKIE_SECURE|NGX_HTTP_PROXY_COOKIE_SECURE_OFF },

  697.     { ngx_string("httponly"),
  698.       NGX_HTTP_PROXY_COOKIE_HTTPONLY|NGX_HTTP_PROXY_COOKIE_HTTPONLY_ON },

  699.     { ngx_string("nohttponly"),
  700.       NGX_HTTP_PROXY_COOKIE_HTTPONLY|NGX_HTTP_PROXY_COOKIE_HTTPONLY_OFF },

  701.     { ngx_string("samesite=strict"),
  702.       NGX_HTTP_PROXY_COOKIE_SAMESITE|NGX_HTTP_PROXY_COOKIE_SAMESITE_STRICT },

  703.     { ngx_string("samesite=lax"),
  704.       NGX_HTTP_PROXY_COOKIE_SAMESITE|NGX_HTTP_PROXY_COOKIE_SAMESITE_LAX },

  705.     { ngx_string("samesite=none"),
  706.       NGX_HTTP_PROXY_COOKIE_SAMESITE|NGX_HTTP_PROXY_COOKIE_SAMESITE_NONE },

  707.     { ngx_string("nosamesite"),
  708.       NGX_HTTP_PROXY_COOKIE_SAMESITE|NGX_HTTP_PROXY_COOKIE_SAMESITE_OFF },

  709.     { ngx_null_string, 0 }
  710. };


  711. static ngx_int_t
  712. ngx_http_proxy_handler(ngx_http_request_t *r)
  713. {
  714.     ngx_int_t                    rc;
  715.     ngx_http_upstream_t         *u;
  716.     ngx_http_proxy_ctx_t        *ctx;
  717.     ngx_http_proxy_loc_conf_t   *plcf;
  718. #if (NGX_HTTP_CACHE)
  719.     ngx_http_proxy_main_conf_t  *pmcf;
  720. #endif

  721.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  722. #if (NGX_HTTP_V2)
  723.     if (plcf->http_version == NGX_HTTP_VERSION_20) {
  724.         return ngx_http_proxy_v2_handler(r);
  725.     }
  726. #endif

  727.     if (ngx_http_upstream_create(r) != NGX_OK) {
  728.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  729.     }

  730.     ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t));
  731.     if (ctx == NULL) {
  732.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  733.     }

  734.     ctx->legacy = 1;

  735.     ngx_http_set_ctx(r, ctx, ngx_http_proxy_module);

  736.     u = r->upstream;

  737.     if (plcf->proxy_lengths == NULL) {
  738.         ctx->vars = plcf->vars;
  739.         u->schema = plcf->vars.schema;
  740. #if (NGX_HTTP_SSL)
  741.         u->ssl = plcf->ssl;
  742. #endif

  743.     } else {
  744.         if (ngx_http_proxy_eval(r, ctx, plcf) != NGX_OK) {
  745.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  746.         }
  747.     }

  748.     u->output.tag = (ngx_buf_tag_t) &ngx_http_proxy_module;

  749.     u->conf = &plcf->upstream;

  750. #if (NGX_HTTP_CACHE)
  751.     pmcf = ngx_http_get_module_main_conf(r, ngx_http_proxy_module);

  752.     u->caches = &pmcf->caches;
  753.     u->create_key = ngx_http_proxy_create_key;
  754. #endif

  755.     u->create_request = ngx_http_proxy_create_request;
  756.     u->reinit_request = ngx_http_proxy_reinit_request;
  757.     u->process_header = ngx_http_proxy_process_status_line;
  758.     u->abort_request = ngx_http_proxy_abort_request;
  759.     u->finalize_request = ngx_http_proxy_finalize_request;
  760.     r->state = 0;

  761.     if (plcf->redirects) {
  762.         u->rewrite_redirect = ngx_http_proxy_rewrite_redirect;
  763.     }

  764.     if (plcf->cookie_domains || plcf->cookie_paths || plcf->cookie_flags) {
  765.         u->rewrite_cookie = ngx_http_proxy_rewrite_cookie;
  766.     }

  767.     u->buffering = plcf->upstream.buffering;

  768.     u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
  769.     if (u->pipe == NULL) {
  770.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  771.     }

  772.     u->pipe->input_filter = ngx_http_proxy_copy_filter;
  773.     u->pipe->input_ctx = r;

  774.     u->input_filter_init = ngx_http_proxy_input_filter_init;
  775.     u->input_filter = ngx_http_proxy_non_buffered_copy_filter;
  776.     u->input_filter_ctx = r;

  777.     u->accel = 1;

  778.     if (!plcf->upstream.request_buffering
  779.         && plcf->body_values == NULL && plcf->upstream.pass_request_body
  780.         && (!r->headers_in.chunked
  781.             || plcf->http_version == NGX_HTTP_VERSION_11))
  782.     {
  783.         r->request_body_no_buffering = 1;
  784.     }

  785.     rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);

  786.     if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
  787.         return rc;
  788.     }

  789.     return NGX_DONE;
  790. }


  791. ngx_int_t
  792. ngx_http_proxy_eval(ngx_http_request_t *r, ngx_http_proxy_ctx_t *ctx,
  793.     ngx_http_proxy_loc_conf_t *plcf)
  794. {
  795.     u_char               *p;
  796.     size_t                add;
  797.     u_short               port;
  798.     ngx_str_t             proxy;
  799.     ngx_url_t             url;
  800.     ngx_http_upstream_t  *u;

  801.     if (ngx_http_script_run(r, &proxy, plcf->proxy_lengths->elts, 0,
  802.                             plcf->proxy_values->elts)
  803.         == NULL)
  804.     {
  805.         return NGX_ERROR;
  806.     }

  807.     if (proxy.len > 7
  808.         && ngx_strncasecmp(proxy.data, (u_char *) "http://", 7) == 0)
  809.     {
  810.         add = 7;
  811.         port = 80;

  812. #if (NGX_HTTP_SSL)

  813.     } else if (proxy.len > 8
  814.                && ngx_strncasecmp(proxy.data, (u_char *) "https://", 8) == 0)
  815.     {
  816.         add = 8;
  817.         port = 443;
  818.         r->upstream->ssl = 1;

  819. #endif

  820.     } else {
  821.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  822.                       "invalid URL prefix in \"%V\"", &proxy);
  823.         return NGX_ERROR;
  824.     }

  825.     u = r->upstream;

  826.     u->schema.len = add;
  827.     u->schema.data = proxy.data;

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

  829.     url.url.len = proxy.len - add;
  830.     url.url.data = proxy.data + add;
  831.     url.default_port = port;
  832.     url.uri_part = 1;
  833.     url.no_resolve = 1;

  834.     if (ngx_parse_url(r->pool, &url) != NGX_OK) {
  835.         if (url.err) {
  836.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  837.                           "%s in upstream \"%V\"", url.err, &url.url);
  838.         }

  839.         return NGX_ERROR;
  840.     }

  841.     if (url.uri.len) {
  842.         if (url.uri.data[0] == '?') {
  843.             p = ngx_pnalloc(r->pool, url.uri.len + 1);
  844.             if (p == NULL) {
  845.                 return NGX_ERROR;
  846.             }

  847.             *p++ = '/';
  848.             ngx_memcpy(p, url.uri.data, url.uri.len);

  849.             url.uri.len++;
  850.             url.uri.data = p - 1;
  851.         }
  852.     }

  853.     ctx->vars.key_start = u->schema;

  854.     ngx_http_proxy_set_vars(&url, &ctx->vars);

  855.     u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
  856.     if (u->resolved == NULL) {
  857.         return NGX_ERROR;
  858.     }

  859.     if (url.addrs) {
  860.         u->resolved->sockaddr = url.addrs[0].sockaddr;
  861.         u->resolved->socklen = url.addrs[0].socklen;
  862.         u->resolved->name = url.addrs[0].name;
  863.         u->resolved->naddrs = 1;
  864.     }

  865.     u->resolved->host = url.host;
  866.     u->resolved->port = (in_port_t) (url.no_port ? port : url.port);
  867.     u->resolved->no_port = url.no_port;

  868.     return NGX_OK;
  869. }


  870. #if (NGX_HTTP_CACHE)

  871. ngx_int_t
  872. ngx_http_proxy_create_key(ngx_http_request_t *r)
  873. {
  874.     size_t                      len, loc_len;
  875.     u_char                     *p;
  876.     uintptr_t                   escape;
  877.     ngx_str_t                  *key;
  878.     ngx_http_upstream_t        *u;
  879.     ngx_http_proxy_ctx_t       *ctx;
  880.     ngx_http_proxy_loc_conf_t  *plcf;

  881.     u = r->upstream;

  882.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  883.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  884.     key = ngx_array_push(&r->cache->keys);
  885.     if (key == NULL) {
  886.         return NGX_ERROR;
  887.     }

  888.     if (plcf->cache_key.value.data) {

  889.         if (ngx_http_complex_value(r, &plcf->cache_key, key) != NGX_OK) {
  890.             return NGX_ERROR;
  891.         }

  892.         return NGX_OK;
  893.     }

  894.     *key = ctx->vars.key_start;

  895.     key = ngx_array_push(&r->cache->keys);
  896.     if (key == NULL) {
  897.         return NGX_ERROR;
  898.     }

  899.     if (plcf->proxy_lengths && ctx->vars.uri.len) {

  900.         *key = ctx->vars.uri;
  901.         u->uri = ctx->vars.uri;

  902.         return NGX_OK;

  903.     } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri) {
  904.         *key = r->unparsed_uri;
  905.         u->uri = r->unparsed_uri;

  906.         return NGX_OK;
  907.     }

  908.     loc_len = (r->valid_location && ctx->vars.uri.len)
  909.               ? ngx_min(plcf->location.len, r->uri.len) : 0;

  910.     if (r->quoted_uri || r->internal) {
  911.         escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
  912.                                     r->uri.len - loc_len, NGX_ESCAPE_URI);
  913.     } else {
  914.         escape = 0;
  915.     }

  916.     len = ctx->vars.uri.len + r->uri.len - loc_len + escape
  917.           + sizeof("?") - 1 + r->args.len;

  918.     p = ngx_pnalloc(r->pool, len);
  919.     if (p == NULL) {
  920.         return NGX_ERROR;
  921.     }

  922.     key->data = p;

  923.     if (r->valid_location) {
  924.         p = ngx_copy(p, ctx->vars.uri.data, ctx->vars.uri.len);
  925.     }

  926.     if (escape) {
  927.         ngx_escape_uri(p, r->uri.data + loc_len,
  928.                        r->uri.len - loc_len, NGX_ESCAPE_URI);
  929.         p += r->uri.len - loc_len + escape;

  930.     } else {
  931.         p = ngx_copy(p, r->uri.data + loc_len, r->uri.len - loc_len);
  932.     }

  933.     if (r->args.len > 0) {
  934.         *p++ = '?';
  935.         p = ngx_copy(p, r->args.data, r->args.len);
  936.     }

  937.     key->len = p - key->data;
  938.     u->uri = *key;

  939.     return NGX_OK;
  940. }

  941. #endif


  942. static ngx_int_t
  943. ngx_http_proxy_create_request(ngx_http_request_t *r)
  944. {
  945.     size_t                        len, uri_len, loc_len, body_len,
  946.                                   key_len, val_len;
  947.     uintptr_t                     escape;
  948.     ngx_buf_t                    *b;
  949.     ngx_str_t                     method;
  950.     ngx_uint_t                    i, unparsed_uri;
  951.     ngx_chain_t                  *cl, *body;
  952.     ngx_list_part_t              *part;
  953.     ngx_table_elt_t              *header;
  954.     ngx_http_upstream_t          *u;
  955.     ngx_http_proxy_ctx_t         *ctx;
  956.     ngx_http_script_code_pt       code;
  957.     ngx_http_proxy_headers_t     *headers;
  958.     ngx_http_script_engine_t      e, le;
  959.     ngx_http_proxy_loc_conf_t    *plcf;
  960.     ngx_http_script_len_code_pt   lcode;

  961.     u = r->upstream;

  962.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  963. #if (NGX_HTTP_CACHE)
  964.     headers = u->cacheable ? &plcf->headers_cache : &plcf->headers;
  965. #else
  966.     headers = &plcf->headers;
  967. #endif

  968.     if (u->method.len) {
  969.         /* HEAD was changed to GET to cache response */
  970.         method = u->method;

  971.     } else if (plcf->method) {
  972.         if (ngx_http_complex_value(r, plcf->method, &method) != NGX_OK) {
  973.             return NGX_ERROR;
  974.         }

  975.     } else {
  976.         method = r->method_name;
  977.     }

  978.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  979.     if (method.len == 4
  980.         && ngx_strncasecmp(method.data, (u_char *) "HEAD", 4) == 0)
  981.     {
  982.         ctx->head = 1;
  983.     }

  984.     len = method.len + 1 + sizeof(ngx_http_proxy_version) - 1
  985.           + sizeof(CRLF) - 1;

  986.     escape = 0;
  987.     loc_len = 0;
  988.     unparsed_uri = 0;

  989.     if (plcf->proxy_lengths && ctx->vars.uri.len) {
  990.         uri_len = ctx->vars.uri.len;

  991.     } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri) {
  992.         unparsed_uri = 1;
  993.         uri_len = r->unparsed_uri.len;

  994.     } else {
  995.         loc_len = (r->valid_location && ctx->vars.uri.len)
  996.                   ? ngx_min(plcf->location.len, r->uri.len) : 0;

  997.         if (r->quoted_uri || r->internal) {
  998.             escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
  999.                                         r->uri.len - loc_len, NGX_ESCAPE_URI);
  1000.         }

  1001.         uri_len = ctx->vars.uri.len + r->uri.len - loc_len + escape
  1002.                   + sizeof("?") - 1 + r->args.len;
  1003.     }

  1004.     if (uri_len == 0) {
  1005.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1006.                       "zero length URI to proxy");
  1007.         return NGX_ERROR;
  1008.     }

  1009.     len += uri_len;

  1010.     ngx_memzero(&le, sizeof(ngx_http_script_engine_t));

  1011.     ngx_http_script_flush_no_cacheable_variables(r, plcf->body_flushes);
  1012.     ngx_http_script_flush_no_cacheable_variables(r, headers->flushes);

  1013.     if (plcf->body_lengths) {
  1014.         le.ip = plcf->body_lengths->elts;
  1015.         le.request = r;
  1016.         le.flushed = 1;
  1017.         body_len = 0;

  1018.         while (*(uintptr_t *) le.ip) {
  1019.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  1020.             body_len += lcode(&le);
  1021.         }

  1022.         ctx->internal_body_length = body_len;
  1023.         len += body_len;

  1024.     } else if (r->headers_in.chunked && r->reading_body) {
  1025.         ctx->internal_body_length = -1;
  1026.         ctx->internal_chunked = 1;

  1027.     } else {
  1028.         ctx->internal_body_length = r->headers_in.content_length_n;
  1029.     }

  1030.     le.ip = headers->lengths->elts;
  1031.     le.request = r;
  1032.     le.flushed = 1;

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

  1034.         lcode = *(ngx_http_script_len_code_pt *) le.ip;
  1035.         key_len = lcode(&le);

  1036.         for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
  1037.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  1038.         }
  1039.         le.ip += sizeof(uintptr_t);

  1040.         if (val_len == 0) {
  1041.             continue;
  1042.         }

  1043.         len += key_len + sizeof(": ") - 1 + val_len + sizeof(CRLF) - 1;
  1044.     }


  1045.     if (plcf->upstream.pass_request_headers) {
  1046.         part = &r->headers_in.headers.part;
  1047.         header = part->elts;

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

  1049.             if (i >= part->nelts) {
  1050.                 if (part->next == NULL) {
  1051.                     break;
  1052.                 }

  1053.                 part = part->next;
  1054.                 header = part->elts;
  1055.                 i = 0;
  1056.             }

  1057.             if (ngx_hash_find(&headers->hash, header[i].hash,
  1058.                               header[i].lowcase_key, header[i].key.len))
  1059.             {
  1060.                 continue;
  1061.             }

  1062.             len += header[i].key.len + sizeof(": ") - 1
  1063.                 + header[i].value.len + sizeof(CRLF) - 1;
  1064.         }
  1065.     }


  1066.     b = ngx_create_temp_buf(r->pool, len);
  1067.     if (b == NULL) {
  1068.         return NGX_ERROR;
  1069.     }

  1070.     cl = ngx_alloc_chain_link(r->pool);
  1071.     if (cl == NULL) {
  1072.         return NGX_ERROR;
  1073.     }

  1074.     cl->buf = b;


  1075.     /* the request line */

  1076.     b->last = ngx_copy(b->last, method.data, method.len);
  1077.     *b->last++ = ' ';

  1078.     u->uri.data = b->last;

  1079.     if (plcf->proxy_lengths && ctx->vars.uri.len) {
  1080.         b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);

  1081.     } else if (unparsed_uri) {
  1082.         b->last = ngx_copy(b->last, r->unparsed_uri.data, r->unparsed_uri.len);

  1083.     } else {
  1084.         if (r->valid_location) {
  1085.             b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);
  1086.         }

  1087.         if (escape) {
  1088.             ngx_escape_uri(b->last, r->uri.data + loc_len,
  1089.                            r->uri.len - loc_len, NGX_ESCAPE_URI);
  1090.             b->last += r->uri.len - loc_len + escape;

  1091.         } else {
  1092.             b->last = ngx_copy(b->last, r->uri.data + loc_len,
  1093.                                r->uri.len - loc_len);
  1094.         }

  1095.         if (r->args.len > 0) {
  1096.             *b->last++ = '?';
  1097.             b->last = ngx_copy(b->last, r->args.data, r->args.len);
  1098.         }
  1099.     }

  1100.     u->uri.len = b->last - u->uri.data;

  1101.     if (plcf->http_version == NGX_HTTP_VERSION_11) {
  1102.         b->last = ngx_cpymem(b->last, ngx_http_proxy_version_11,
  1103.                              sizeof(ngx_http_proxy_version_11) - 1);

  1104.     } else {
  1105.         b->last = ngx_cpymem(b->last, ngx_http_proxy_version,
  1106.                              sizeof(ngx_http_proxy_version) - 1);
  1107.     }

  1108.     ngx_memzero(&e, sizeof(ngx_http_script_engine_t));

  1109.     e.ip = headers->values->elts;
  1110.     e.pos = b->last;
  1111.     e.request = r;
  1112.     e.flushed = 1;

  1113.     le.ip = headers->lengths->elts;

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

  1115.         lcode = *(ngx_http_script_len_code_pt *) le.ip;
  1116.         (void) lcode(&le);

  1117.         for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
  1118.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  1119.         }
  1120.         le.ip += sizeof(uintptr_t);

  1121.         if (val_len == 0) {
  1122.             e.skip = 1;

  1123.             while (*(uintptr_t *) e.ip) {
  1124.                 code = *(ngx_http_script_code_pt *) e.ip;
  1125.                 code((ngx_http_script_engine_t *) &e);
  1126.             }
  1127.             e.ip += sizeof(uintptr_t);

  1128.             e.skip = 0;

  1129.             continue;
  1130.         }

  1131.         code = *(ngx_http_script_code_pt *) e.ip;
  1132.         code((ngx_http_script_engine_t *) &e);

  1133.         *e.pos++ = ':'; *e.pos++ = ' ';

  1134.         while (*(uintptr_t *) e.ip) {
  1135.             code = *(ngx_http_script_code_pt *) e.ip;
  1136.             code((ngx_http_script_engine_t *) &e);
  1137.         }
  1138.         e.ip += sizeof(uintptr_t);

  1139.         *e.pos++ = CR; *e.pos++ = LF;
  1140.     }

  1141.     b->last = e.pos;


  1142.     if (plcf->upstream.pass_request_headers) {
  1143.         part = &r->headers_in.headers.part;
  1144.         header = part->elts;

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

  1146.             if (i >= part->nelts) {
  1147.                 if (part->next == NULL) {
  1148.                     break;
  1149.                 }

  1150.                 part = part->next;
  1151.                 header = part->elts;
  1152.                 i = 0;
  1153.             }

  1154.             if (ngx_hash_find(&headers->hash, header[i].hash,
  1155.                               header[i].lowcase_key, header[i].key.len))
  1156.             {
  1157.                 continue;
  1158.             }

  1159.             b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);

  1160.             *b->last++ = ':'; *b->last++ = ' ';

  1161.             b->last = ngx_copy(b->last, header[i].value.data,
  1162.                                header[i].value.len);

  1163.             *b->last++ = CR; *b->last++ = LF;

  1164.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1165.                            "http proxy header: \"%V: %V\"",
  1166.                            &header[i].key, &header[i].value);
  1167.         }
  1168.     }


  1169.     /* add "\r\n" at the header end */
  1170.     *b->last++ = CR; *b->last++ = LF;

  1171.     if (plcf->body_values) {
  1172.         e.ip = plcf->body_values->elts;
  1173.         e.pos = b->last;
  1174.         e.skip = 0;

  1175.         while (*(uintptr_t *) e.ip) {
  1176.             code = *(ngx_http_script_code_pt *) e.ip;
  1177.             code((ngx_http_script_engine_t *) &e);
  1178.         }

  1179.         b->last = e.pos;
  1180.     }

  1181.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1182.                    "http proxy header:%N\"%*s\"",
  1183.                    (size_t) (b->last - b->pos), b->pos);

  1184.     if (r->request_body_no_buffering) {

  1185.         u->request_bufs = cl;

  1186.         if (ctx->internal_chunked) {
  1187.             u->output.output_filter = ngx_http_proxy_body_output_filter;
  1188.             u->output.filter_ctx = r;
  1189.         }

  1190.     } else if (plcf->body_values == NULL && plcf->upstream.pass_request_body) {

  1191.         body = u->request_bufs;
  1192.         u->request_bufs = cl;

  1193.         while (body) {
  1194.             b = ngx_alloc_buf(r->pool);
  1195.             if (b == NULL) {
  1196.                 return NGX_ERROR;
  1197.             }

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

  1199.             cl->next = ngx_alloc_chain_link(r->pool);
  1200.             if (cl->next == NULL) {
  1201.                 return NGX_ERROR;
  1202.             }

  1203.             cl = cl->next;
  1204.             cl->buf = b;

  1205.             body = body->next;
  1206.         }

  1207.     } else {
  1208.         u->request_bufs = cl;
  1209.     }

  1210.     b->flush = 1;
  1211.     cl->next = NULL;

  1212.     return NGX_OK;
  1213. }


  1214. static ngx_int_t
  1215. ngx_http_proxy_reinit_request(ngx_http_request_t *r)
  1216. {
  1217.     ngx_http_proxy_ctx_t  *ctx;

  1218.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1219.     if (ctx == NULL) {
  1220.         return NGX_OK;
  1221.     }

  1222.     ctx->status.code = 0;
  1223.     ctx->status.count = 0;
  1224.     ctx->status.start = NULL;
  1225.     ctx->status.end = NULL;
  1226.     ctx->chunked.state = 0;

  1227.     r->upstream->process_header = ngx_http_proxy_process_status_line;
  1228.     r->upstream->pipe->input_filter = ngx_http_proxy_copy_filter;
  1229.     r->upstream->input_filter = ngx_http_proxy_non_buffered_copy_filter;
  1230.     r->state = 0;

  1231.     return NGX_OK;
  1232. }


  1233. static ngx_int_t
  1234. ngx_http_proxy_body_output_filter(void *data, ngx_chain_t *in)
  1235. {
  1236.     ngx_http_request_t  *r = data;

  1237.     off_t                  size;
  1238.     u_char                *chunk;
  1239.     ngx_int_t              rc;
  1240.     ngx_buf_t             *b;
  1241.     ngx_chain_t           *out, *cl, *tl, **ll, **fl;
  1242.     ngx_http_proxy_ctx_t  *ctx;

  1243.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1244.                    "proxy output filter");

  1245.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1246.     if (in == NULL) {
  1247.         out = in;
  1248.         goto out;
  1249.     }

  1250.     out = NULL;
  1251.     ll = &out;

  1252.     if (!ctx->header_sent) {
  1253.         /* first buffer contains headers, pass it unmodified */

  1254.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1255.                        "proxy output header");

  1256.         ctx->header_sent = 1;

  1257.         tl = ngx_alloc_chain_link(r->pool);
  1258.         if (tl == NULL) {
  1259.             return NGX_ERROR;
  1260.         }

  1261.         tl->buf = in->buf;
  1262.         *ll = tl;
  1263.         ll = &tl->next;

  1264.         in = in->next;

  1265.         if (in == NULL) {
  1266.             tl->next = NULL;
  1267.             goto out;
  1268.         }
  1269.     }

  1270.     size = 0;
  1271.     cl = in;
  1272.     fl = ll;

  1273.     for ( ;; ) {
  1274.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1275.                        "proxy output chunk: %O", ngx_buf_size(cl->buf));

  1276.         size += ngx_buf_size(cl->buf);

  1277.         if (cl->buf->flush
  1278.             || cl->buf->sync
  1279.             || ngx_buf_in_memory(cl->buf)
  1280.             || cl->buf->in_file)
  1281.         {
  1282.             tl = ngx_alloc_chain_link(r->pool);
  1283.             if (tl == NULL) {
  1284.                 return NGX_ERROR;
  1285.             }

  1286.             tl->buf = cl->buf;
  1287.             *ll = tl;
  1288.             ll = &tl->next;
  1289.         }

  1290.         if (cl->next == NULL) {
  1291.             break;
  1292.         }

  1293.         cl = cl->next;
  1294.     }

  1295.     if (size) {
  1296.         tl = ngx_chain_get_free_buf(r->pool, &ctx->free);
  1297.         if (tl == NULL) {
  1298.             return NGX_ERROR;
  1299.         }

  1300.         b = tl->buf;
  1301.         chunk = b->start;

  1302.         if (chunk == NULL) {
  1303.             /* the "0000000000000000" is 64-bit hexadecimal string */

  1304.             chunk = ngx_palloc(r->pool, sizeof("0000000000000000" CRLF) - 1);
  1305.             if (chunk == NULL) {
  1306.                 return NGX_ERROR;
  1307.             }

  1308.             b->start = chunk;
  1309.             b->end = chunk + sizeof("0000000000000000" CRLF) - 1;
  1310.         }

  1311.         b->tag = (ngx_buf_tag_t) &ngx_http_proxy_body_output_filter;
  1312.         b->memory = 0;
  1313.         b->temporary = 1;
  1314.         b->pos = chunk;
  1315.         b->last = ngx_sprintf(chunk, "%xO" CRLF, size);

  1316.         tl->next = *fl;
  1317.         *fl = tl;
  1318.     }

  1319.     if (cl->buf->last_buf) {
  1320.         tl = ngx_chain_get_free_buf(r->pool, &ctx->free);
  1321.         if (tl == NULL) {
  1322.             return NGX_ERROR;
  1323.         }

  1324.         b = tl->buf;

  1325.         b->tag = (ngx_buf_tag_t) &ngx_http_proxy_body_output_filter;
  1326.         b->temporary = 0;
  1327.         b->memory = 1;
  1328.         b->last_buf = 1;
  1329.         b->pos = (u_char *) CRLF "0" CRLF CRLF;
  1330.         b->last = b->pos + 7;

  1331.         cl->buf->last_buf = 0;

  1332.         *ll = tl;

  1333.         if (size == 0) {
  1334.             b->pos += 2;
  1335.         }

  1336.     } else if (size > 0) {
  1337.         tl = ngx_chain_get_free_buf(r->pool, &ctx->free);
  1338.         if (tl == NULL) {
  1339.             return NGX_ERROR;
  1340.         }

  1341.         b = tl->buf;

  1342.         b->tag = (ngx_buf_tag_t) &ngx_http_proxy_body_output_filter;
  1343.         b->temporary = 0;
  1344.         b->memory = 1;
  1345.         b->pos = (u_char *) CRLF;
  1346.         b->last = b->pos + 2;

  1347.         *ll = tl;

  1348.     } else {
  1349.         *ll = NULL;
  1350.     }

  1351. out:

  1352.     rc = ngx_chain_writer(&r->upstream->writer, out);

  1353.     ngx_chain_update_chains(r->pool, &ctx->free, &ctx->busy, &out,
  1354.                             (ngx_buf_tag_t) &ngx_http_proxy_body_output_filter);

  1355.     return rc;
  1356. }


  1357. static ngx_int_t
  1358. ngx_http_proxy_process_status_line(ngx_http_request_t *r)
  1359. {
  1360.     size_t                 len;
  1361.     ngx_int_t              rc;
  1362.     ngx_http_upstream_t   *u;
  1363.     ngx_http_proxy_ctx_t  *ctx;

  1364.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1365.     if (ctx == NULL) {
  1366.         return NGX_ERROR;
  1367.     }

  1368.     u = r->upstream;

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

  1370.     if (rc == NGX_AGAIN) {
  1371.         return rc;
  1372.     }

  1373.     if (rc == NGX_ERROR) {
  1374.         u->buffer.pos = ctx->status.line_start;

  1375. #if (NGX_HTTP_CACHE)

  1376.         if (r->cache) {
  1377.             r->http_version = NGX_HTTP_VERSION_9;
  1378.             return NGX_OK;
  1379.         }

  1380. #endif

  1381.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1382.                       "upstream sent no valid HTTP/1.0 header");

  1383. #if 0
  1384.         if (u->accel) {
  1385.             return NGX_HTTP_UPSTREAM_INVALID_HEADER;
  1386.         }
  1387. #endif

  1388.         r->http_version = NGX_HTTP_VERSION_9;
  1389.         u->state->status = NGX_HTTP_OK;
  1390.         u->headers_in.connection_close = 1;

  1391.         return NGX_OK;
  1392.     }

  1393.     if (u->state && u->state->status == 0) {
  1394.         u->state->status = ctx->status.code;
  1395.     }

  1396.     u->headers_in.status_n = ctx->status.code;

  1397.     len = ctx->status.end - ctx->status.start;
  1398.     u->headers_in.status_line.len = len;

  1399.     u->headers_in.status_line.data = ngx_pnalloc(r->pool, len);
  1400.     if (u->headers_in.status_line.data == NULL) {
  1401.         return NGX_ERROR;
  1402.     }

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

  1404.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1405.                    "http proxy status %ui \"%V\"",
  1406.                    u->headers_in.status_n, &u->headers_in.status_line);

  1407.     if (ctx->status.http_version < NGX_HTTP_VERSION_11) {

  1408.         if (ctx->status.code == NGX_HTTP_EARLY_HINTS) {
  1409.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1410.                           "upstream sent HTTP/1.0 response with early hints");
  1411.             return NGX_HTTP_UPSTREAM_INVALID_HEADER;
  1412.         }

  1413.         u->headers_in.connection_close = 1;
  1414.     }

  1415.     u->process_header = ngx_http_proxy_process_header;

  1416.     return ngx_http_proxy_process_header(r);
  1417. }


  1418. static ngx_int_t
  1419. ngx_http_proxy_process_header(ngx_http_request_t *r)
  1420. {
  1421.     ngx_int_t                       rc;
  1422.     ngx_table_elt_t                *h;
  1423.     ngx_http_upstream_t            *u;
  1424.     ngx_http_proxy_ctx_t           *ctx;
  1425.     ngx_http_upstream_header_t     *hh;
  1426.     ngx_http_upstream_main_conf_t  *umcf;

  1427.     umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);

  1428.     for ( ;; ) {

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

  1430.         if (rc == NGX_OK) {

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

  1432.             h = ngx_list_push(&r->upstream->headers_in.headers);
  1433.             if (h == NULL) {
  1434.                 return NGX_ERROR;
  1435.             }

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

  1437.             h->key.len = r->header_name_end - r->header_name_start;
  1438.             h->value.len = r->header_end - r->header_start;

  1439.             h->key.data = ngx_pnalloc(r->pool,
  1440.                                h->key.len + 1 + h->value.len + 1 + h->key.len);
  1441.             if (h->key.data == NULL) {
  1442.                 h->hash = 0;
  1443.                 return NGX_ERROR;
  1444.             }

  1445.             h->value.data = h->key.data + h->key.len + 1;
  1446.             h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;

  1447.             ngx_memcpy(h->key.data, r->header_name_start, h->key.len);
  1448.             h->key.data[h->key.len] = '\0';
  1449.             ngx_memcpy(h->value.data, r->header_start, h->value.len);
  1450.             h->value.data[h->value.len] = '\0';

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

  1453.             } else {
  1454.                 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
  1455.             }

  1456.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1457.                            "http proxy header: \"%V: %V\"",
  1458.                            &h->key, &h->value);

  1459.             if (r->upstream->headers_in.status_n == NGX_HTTP_EARLY_HINTS) {
  1460.                 continue;
  1461.             }

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

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

  1466.                 if (rc != NGX_OK) {
  1467.                     return rc;
  1468.                 }
  1469.             }

  1470.             continue;
  1471.         }

  1472.         if (rc == NGX_HTTP_PARSE_HEADER_DONE) {

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

  1474.             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1475.                            "http proxy header done");

  1476.             ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1477.             if (r->upstream->headers_in.status_n == NGX_HTTP_EARLY_HINTS) {
  1478.                 ctx->status.code = 0;
  1479.                 ctx->status.count = 0;
  1480.                 ctx->status.start = NULL;
  1481.                 ctx->status.end = NULL;

  1482.                 r->upstream->process_header =
  1483.                                             ngx_http_proxy_process_status_line;
  1484.                 r->state = 0;
  1485.                 return NGX_HTTP_UPSTREAM_EARLY_HINTS;
  1486.             }

  1487.             /*
  1488.              * if no "Server" and "Date" in header line,
  1489.              * then add the special empty headers
  1490.              */

  1491.             if (r->upstream->headers_in.server == NULL) {
  1492.                 h = ngx_list_push(&r->upstream->headers_in.headers);
  1493.                 if (h == NULL) {
  1494.                     return NGX_ERROR;
  1495.                 }

  1496.                 h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(
  1497.                                     ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r');

  1498.                 ngx_str_set(&h->key, "Server");
  1499.                 ngx_str_null(&h->value);
  1500.                 h->lowcase_key = (u_char *) "server";
  1501.                 h->next = NULL;
  1502.             }

  1503.             if (r->upstream->headers_in.date == NULL) {
  1504.                 h = ngx_list_push(&r->upstream->headers_in.headers);
  1505.                 if (h == NULL) {
  1506.                     return NGX_ERROR;
  1507.                 }

  1508.                 h->hash = ngx_hash(ngx_hash(ngx_hash('d', 'a'), 't'), 'e');

  1509.                 ngx_str_set(&h->key, "Date");
  1510.                 ngx_str_null(&h->value);
  1511.                 h->lowcase_key = (u_char *) "date";
  1512.                 h->next = NULL;
  1513.             }

  1514.             /* clear content length if response is chunked */

  1515.             u = r->upstream;

  1516.             if (u->headers_in.chunked) {
  1517.                 u->headers_in.content_length_n = -1;
  1518.             }

  1519.             /*
  1520.              * set u->keepalive if response has no body; this allows to keep
  1521.              * connections alive in case of r->header_only or X-Accel-Redirect
  1522.              */

  1523.             if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
  1524.                 || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED
  1525.                 || ctx->head
  1526.                 || (!u->headers_in.chunked
  1527.                     && u->headers_in.content_length_n == 0))
  1528.             {
  1529.                 u->keepalive = !u->headers_in.connection_close;
  1530.             }

  1531.             if (u->headers_in.status_n == NGX_HTTP_SWITCHING_PROTOCOLS) {
  1532.                 u->keepalive = 0;

  1533.                 if (r->headers_in.upgrade) {
  1534.                     u->upgrade = 1;
  1535.                 }
  1536.             }

  1537.             return NGX_OK;
  1538.         }

  1539.         if (rc == NGX_AGAIN) {
  1540.             return NGX_AGAIN;
  1541.         }

  1542.         /* rc == NGX_HTTP_PARSE_INVALID_HEADER */

  1543.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1544.                       "upstream sent invalid header: \"%*s\\x%02xd...\"",
  1545.                       r->header_end - r->header_name_start,
  1546.                       r->header_name_start, *r->header_end);

  1547.         return NGX_HTTP_UPSTREAM_INVALID_HEADER;
  1548.     }
  1549. }


  1550. static ngx_int_t
  1551. ngx_http_proxy_input_filter_init(void *data)
  1552. {
  1553.     ngx_http_request_t    *r = data;
  1554.     ngx_http_upstream_t   *u;
  1555.     ngx_http_proxy_ctx_t  *ctx;

  1556.     u = r->upstream;
  1557.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1558.     if (ctx == NULL) {
  1559.         return NGX_ERROR;
  1560.     }

  1561.     ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1562.                    "http proxy filter init s:%ui h:%d c:%d l:%O",
  1563.                    u->headers_in.status_n, ctx->head, u->headers_in.chunked,
  1564.                    u->headers_in.content_length_n);

  1565.     /* as per RFC2616, 4.4 Message Length */

  1566.     if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
  1567.         || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED
  1568.         || ctx->head)
  1569.     {
  1570.         /* 1xx, 204, and 304 and replies to HEAD requests */
  1571.         /* no 1xx since we don't send Expect and Upgrade */

  1572.         u->pipe->length = 0;
  1573.         u->length = 0;
  1574.         u->keepalive = !u->headers_in.connection_close;

  1575.     } else if (u->headers_in.chunked) {
  1576.         /* chunked */

  1577.         u->pipe->input_filter = ngx_http_proxy_chunked_filter;
  1578.         u->pipe->length = 5; /* "0" CRLF CRLF */

  1579.         u->input_filter = ngx_http_proxy_non_buffered_chunked_filter;
  1580.         u->length = 1;

  1581.     } else if (u->headers_in.content_length_n == 0) {
  1582.         /* empty body: special case as filter won't be called */

  1583.         u->pipe->length = 0;
  1584.         u->length = 0;
  1585.         u->keepalive = !u->headers_in.connection_close;

  1586.     } else {
  1587.         /* content length or connection close */

  1588.         u->pipe->length = u->headers_in.content_length_n;
  1589.         u->length = u->headers_in.content_length_n;
  1590.     }

  1591.     return NGX_OK;
  1592. }


  1593. static ngx_int_t
  1594. ngx_http_proxy_copy_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
  1595. {
  1596.     ngx_buf_t           *b;
  1597.     ngx_chain_t         *cl;
  1598.     ngx_http_request_t  *r;

  1599.     if (buf->pos == buf->last) {
  1600.         return NGX_OK;
  1601.     }

  1602.     if (p->upstream_done) {
  1603.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, p->log, 0,
  1604.                        "http proxy data after close");
  1605.         return NGX_OK;
  1606.     }

  1607.     if (p->length == 0) {

  1608.         ngx_log_error(NGX_LOG_WARN, p->log, 0,
  1609.                       "upstream sent more data than specified in "
  1610.                       "\"Content-Length\" header");

  1611.         r = p->input_ctx;
  1612.         r->upstream->keepalive = 0;
  1613.         p->upstream_done = 1;

  1614.         return NGX_OK;
  1615.     }

  1616.     cl = ngx_chain_get_free_buf(p->pool, &p->free);
  1617.     if (cl == NULL) {
  1618.         return NGX_ERROR;
  1619.     }

  1620.     b = cl->buf;

  1621.     ngx_memcpy(b, buf, sizeof(ngx_buf_t));
  1622.     b->shadow = buf;
  1623.     b->tag = p->tag;
  1624.     b->last_shadow = 1;
  1625.     b->recycled = 1;
  1626.     buf->shadow = b;

  1627.     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "input buf #%d", b->num);

  1628.     if (p->in) {
  1629.         *p->last_in = cl;
  1630.     } else {
  1631.         p->in = cl;
  1632.     }
  1633.     p->last_in = &cl->next;

  1634.     if (p->length == -1) {
  1635.         return NGX_OK;
  1636.     }

  1637.     if (b->last - b->pos > p->length) {

  1638.         ngx_log_error(NGX_LOG_WARN, p->log, 0,
  1639.                       "upstream sent more data than specified in "
  1640.                       "\"Content-Length\" header");

  1641.         b->last = b->pos + p->length;
  1642.         p->upstream_done = 1;

  1643.         return NGX_OK;
  1644.     }

  1645.     p->length -= b->last - b->pos;

  1646.     if (p->length == 0) {
  1647.         r = p->input_ctx;
  1648.         r->upstream->keepalive = !r->upstream->headers_in.connection_close;
  1649.     }

  1650.     return NGX_OK;
  1651. }


  1652. static ngx_int_t
  1653. ngx_http_proxy_chunked_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
  1654. {
  1655.     ngx_int_t                   rc;
  1656.     ngx_buf_t                  *b, **prev;
  1657.     ngx_chain_t                *cl;
  1658.     ngx_http_request_t         *r;
  1659.     ngx_http_proxy_ctx_t       *ctx;
  1660.     ngx_http_proxy_loc_conf_t  *plcf;

  1661.     if (buf->pos == buf->last) {
  1662.         return NGX_OK;
  1663.     }

  1664.     r = p->input_ctx;
  1665.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1666.     if (ctx == NULL) {
  1667.         return NGX_ERROR;
  1668.     }

  1669.     if (p->upstream_done) {
  1670.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, p->log, 0,
  1671.                        "http proxy data after close");
  1672.         return NGX_OK;
  1673.     }

  1674.     if (p->length == 0) {

  1675.         ngx_log_error(NGX_LOG_WARN, p->log, 0,
  1676.                       "upstream sent data after final chunk");

  1677.         r->upstream->keepalive = 0;
  1678.         p->upstream_done = 1;

  1679.         return NGX_OK;
  1680.     }

  1681.     b = NULL;

  1682.     if (ctx->trailers) {
  1683.         rc = ngx_http_proxy_process_trailer(r, buf);

  1684.         if (rc == NGX_ERROR) {
  1685.             return NGX_ERROR;
  1686.         }

  1687.         if (rc == NGX_OK) {

  1688.             /* a whole response has been parsed successfully */

  1689.             p->length = 0;
  1690.             r->upstream->keepalive = !r->upstream->headers_in.connection_close;

  1691.             if (buf->pos != buf->last) {
  1692.                 ngx_log_error(NGX_LOG_WARN, p->log, 0,
  1693.                               "upstream sent data after trailers");
  1694.                 r->upstream->keepalive = 0;
  1695.             }
  1696.         }

  1697.         goto free_buf;
  1698.     }

  1699.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  1700.     prev = &buf->shadow;

  1701.     for ( ;; ) {

  1702.         rc = ngx_http_parse_chunked(r, buf, &ctx->chunked,
  1703.                                     plcf->upstream.pass_trailers);

  1704.         if (rc == NGX_OK) {

  1705.             /* a chunk has been parsed successfully */

  1706.             cl = ngx_chain_get_free_buf(p->pool, &p->free);
  1707.             if (cl == NULL) {
  1708.                 return NGX_ERROR;
  1709.             }

  1710.             b = cl->buf;

  1711.             ngx_memzero(b, sizeof(ngx_buf_t));

  1712.             b->pos = buf->pos;
  1713.             b->start = buf->start;
  1714.             b->end = buf->end;
  1715.             b->tag = p->tag;
  1716.             b->temporary = 1;
  1717.             b->recycled = 1;

  1718.             *prev = b;
  1719.             prev = &b->shadow;

  1720.             if (p->in) {
  1721.                 *p->last_in = cl;
  1722.             } else {
  1723.                 p->in = cl;
  1724.             }
  1725.             p->last_in = &cl->next;

  1726.             /* STUB */ b->num = buf->num;

  1727.             ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
  1728.                            "input buf #%d %p", b->num, b->pos);

  1729.             if (buf->last - buf->pos >= ctx->chunked.size) {

  1730.                 buf->pos += (size_t) ctx->chunked.size;
  1731.                 b->last = buf->pos;
  1732.                 ctx->chunked.size = 0;

  1733.                 continue;
  1734.             }

  1735.             ctx->chunked.size -= buf->last - buf->pos;
  1736.             buf->pos = buf->last;
  1737.             b->last = buf->last;

  1738.             continue;
  1739.         }

  1740.         if (rc == NGX_DONE) {

  1741.             if (plcf->upstream.pass_trailers) {
  1742.                 rc = ngx_http_proxy_process_trailer(r, buf);

  1743.                 if (rc == NGX_ERROR) {
  1744.                     return NGX_ERROR;
  1745.                 }

  1746.                 if (rc == NGX_AGAIN) {
  1747.                     p->length = 1;
  1748.                     break;
  1749.                 }
  1750.             }

  1751.             /* a whole response has been parsed successfully */

  1752.             p->length = 0;
  1753.             r->upstream->keepalive = !r->upstream->headers_in.connection_close;

  1754.             if (buf->pos != buf->last) {
  1755.                 ngx_log_error(NGX_LOG_WARN, p->log, 0,
  1756.                               "upstream sent data after final chunk");
  1757.                 r->upstream->keepalive = 0;
  1758.             }

  1759.             break;
  1760.         }

  1761.         if (rc == NGX_AGAIN) {

  1762.             /* set p->length, minimal amount of data we want to see */

  1763.             p->length = ctx->chunked.length;

  1764.             break;
  1765.         }

  1766.         /* invalid response */

  1767.         ngx_log_error(NGX_LOG_ERR, p->log, 0,
  1768.                       "upstream sent invalid chunked response");

  1769.         return NGX_ERROR;
  1770.     }

  1771. free_buf:

  1772.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, p->log, 0,
  1773.                    "http proxy chunked state %ui, length %O",
  1774.                    ctx->chunked.state, p->length);

  1775.     if (b) {
  1776.         b->shadow = buf;
  1777.         b->last_shadow = 1;

  1778.         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
  1779.                        "input buf %p %z", b->pos, b->last - b->pos);

  1780.         return NGX_OK;
  1781.     }

  1782.     /* there is no data record in the buf, add it to free chain */

  1783.     if (ngx_event_pipe_add_free_buf(p, buf) != NGX_OK) {
  1784.         return NGX_ERROR;
  1785.     }

  1786.     return NGX_OK;
  1787. }


  1788. static ngx_int_t
  1789. ngx_http_proxy_non_buffered_copy_filter(void *data, ssize_t bytes)
  1790. {
  1791.     ngx_http_request_t   *r = data;

  1792.     ngx_buf_t            *b;
  1793.     ngx_chain_t          *cl, **ll;
  1794.     ngx_http_upstream_t  *u;

  1795.     u = r->upstream;

  1796.     if (u->length == 0) {
  1797.         ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
  1798.                       "upstream sent more data than specified in "
  1799.                       "\"Content-Length\" header");
  1800.         u->keepalive = 0;
  1801.         return NGX_OK;
  1802.     }

  1803.     for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
  1804.         ll = &cl->next;
  1805.     }

  1806.     cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
  1807.     if (cl == NULL) {
  1808.         return NGX_ERROR;
  1809.     }

  1810.     *ll = cl;

  1811.     cl->buf->flush = 1;
  1812.     cl->buf->memory = 1;

  1813.     b = &u->buffer;

  1814.     cl->buf->pos = b->last;
  1815.     b->last += bytes;
  1816.     cl->buf->last = b->last;
  1817.     cl->buf->tag = u->output.tag;

  1818.     if (u->length == -1) {
  1819.         return NGX_OK;
  1820.     }

  1821.     if (bytes > u->length) {

  1822.         ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
  1823.                       "upstream sent more data than specified in "
  1824.                       "\"Content-Length\" header");

  1825.         cl->buf->last = cl->buf->pos + u->length;
  1826.         u->length = 0;

  1827.         return NGX_OK;
  1828.     }

  1829.     u->length -= bytes;

  1830.     if (u->length == 0) {
  1831.         u->keepalive = !u->headers_in.connection_close;
  1832.     }

  1833.     return NGX_OK;
  1834. }


  1835. static ngx_int_t
  1836. ngx_http_proxy_non_buffered_chunked_filter(void *data, ssize_t bytes)
  1837. {
  1838.     ngx_http_request_t   *r = data;

  1839.     ngx_int_t                   rc;
  1840.     ngx_buf_t                  *b, *buf;
  1841.     ngx_chain_t                *cl, **ll;
  1842.     ngx_http_upstream_t        *u;
  1843.     ngx_http_proxy_ctx_t       *ctx;
  1844.     ngx_http_proxy_loc_conf_t  *plcf;

  1845.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  1846.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1847.     if (ctx == NULL) {
  1848.         return NGX_ERROR;
  1849.     }

  1850.     u = r->upstream;
  1851.     buf = &u->buffer;

  1852.     buf->pos = buf->last;
  1853.     buf->last += bytes;

  1854.     if (ctx->trailers) {
  1855.         rc = ngx_http_proxy_process_trailer(r, buf);

  1856.         if (rc == NGX_ERROR) {
  1857.             return NGX_ERROR;
  1858.         }

  1859.         if (rc == NGX_OK) {

  1860.             /* a whole response has been parsed successfully */

  1861.             r->upstream->keepalive = !u->headers_in.connection_close;
  1862.             u->length = 0;

  1863.             if (buf->pos != buf->last) {
  1864.                 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
  1865.                               "upstream sent data after trailers");
  1866.                 u->keepalive = 0;
  1867.             }
  1868.         }

  1869.         return NGX_OK;
  1870.     }

  1871.     for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
  1872.         ll = &cl->next;
  1873.     }

  1874.     for ( ;; ) {

  1875.         rc = ngx_http_parse_chunked(r, buf, &ctx->chunked,
  1876.                                     plcf->upstream.pass_trailers);

  1877.         if (rc == NGX_OK) {

  1878.             /* a chunk has been parsed successfully */

  1879.             cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
  1880.             if (cl == NULL) {
  1881.                 return NGX_ERROR;
  1882.             }

  1883.             *ll = cl;
  1884.             ll = &cl->next;

  1885.             b = cl->buf;

  1886.             b->flush = 1;
  1887.             b->memory = 1;

  1888.             b->pos = buf->pos;
  1889.             b->tag = u->output.tag;

  1890.             if (buf->last - buf->pos >= ctx->chunked.size) {
  1891.                 buf->pos += (size_t) ctx->chunked.size;
  1892.                 b->last = buf->pos;
  1893.                 ctx->chunked.size = 0;

  1894.             } else {
  1895.                 ctx->chunked.size -= buf->last - buf->pos;
  1896.                 buf->pos = buf->last;
  1897.                 b->last = buf->last;
  1898.             }

  1899.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1900.                            "http proxy out buf %p %z",
  1901.                            b->pos, b->last - b->pos);

  1902.             continue;
  1903.         }

  1904.         if (rc == NGX_DONE) {

  1905.             if (plcf->upstream.pass_trailers) {
  1906.                 rc = ngx_http_proxy_process_trailer(r, buf);

  1907.                 if (rc == NGX_ERROR) {
  1908.                     return NGX_ERROR;
  1909.                 }

  1910.                 if (rc == NGX_AGAIN) {
  1911.                     u->length = 1;
  1912.                     break;
  1913.                 }
  1914.             }

  1915.             /* a whole response has been parsed successfully */

  1916.             u->keepalive = !u->headers_in.connection_close;
  1917.             u->length = 0;

  1918.             if (buf->pos != buf->last) {
  1919.                 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
  1920.                               "upstream sent data after final chunk");
  1921.                 u->keepalive = 0;
  1922.             }

  1923.             break;
  1924.         }

  1925.         if (rc == NGX_AGAIN) {
  1926.             break;
  1927.         }

  1928.         /* invalid response */

  1929.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1930.                       "upstream sent invalid chunked response");

  1931.         return NGX_ERROR;
  1932.     }

  1933.     return NGX_OK;
  1934. }


  1935. static ngx_int_t
  1936. ngx_http_proxy_process_trailer(ngx_http_request_t *r, ngx_buf_t *buf)
  1937. {
  1938.     size_t                      len;
  1939.     ngx_int_t                   rc;
  1940.     ngx_buf_t                  *b;
  1941.     ngx_table_elt_t            *h;
  1942.     ngx_http_proxy_ctx_t       *ctx;
  1943.     ngx_http_proxy_loc_conf_t  *plcf;

  1944.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  1945.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1946.     if (ctx->trailers == NULL) {
  1947.         ctx->trailers = ngx_create_temp_buf(r->pool,
  1948.                                             plcf->upstream.buffer_size);
  1949.         if (ctx->trailers == NULL) {
  1950.             return NGX_ERROR;
  1951.         }
  1952.     }

  1953.     b = ctx->trailers;
  1954.     len = ngx_min(buf->last - buf->pos, b->end - b->last);

  1955.     b->last = ngx_cpymem(b->last, buf->pos, len);

  1956.     for ( ;; ) {

  1957.         rc = ngx_http_parse_header_line(r, b, 1);

  1958.         if (rc == NGX_OK) {

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

  1960.             h = ngx_list_push(&r->upstream->headers_in.trailers);
  1961.             if (h == NULL) {
  1962.                 return NGX_ERROR;
  1963.             }

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

  1965.             h->key.len = r->header_name_end - r->header_name_start;
  1966.             h->value.len = r->header_end - r->header_start;

  1967.             h->key.data = ngx_pnalloc(r->pool,
  1968.                                h->key.len + 1 + h->value.len + 1 + h->key.len);
  1969.             if (h->key.data == NULL) {
  1970.                 h->hash = 0;
  1971.                 return NGX_ERROR;
  1972.             }

  1973.             h->value.data = h->key.data + h->key.len + 1;
  1974.             h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;

  1975.             ngx_memcpy(h->key.data, r->header_name_start, h->key.len);
  1976.             h->key.data[h->key.len] = '\0';
  1977.             ngx_memcpy(h->value.data, r->header_start, h->value.len);
  1978.             h->value.data[h->value.len] = '\0';

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

  1981.             } else {
  1982.                 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
  1983.             }

  1984.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1985.                            "http proxy trailer: \"%V: %V\"",
  1986.                            &h->key, &h->value);
  1987.             continue;
  1988.         }

  1989.         if (rc == NGX_HTTP_PARSE_HEADER_DONE) {

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

  1991.             buf->pos += len - (b->last - b->pos);

  1992.             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1993.                            "http proxy trailer done");

  1994.             return NGX_OK;
  1995.         }

  1996.         if (rc == NGX_AGAIN) {
  1997.             buf->pos += len;

  1998.             if (b->last == b->end) {
  1999.                 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  2000.                               "upstream sent too big trailers");
  2001.                 return NGX_ERROR;
  2002.             }

  2003.             return NGX_AGAIN;
  2004.         }

  2005.         /* rc == NGX_HTTP_PARSE_INVALID_HEADER */

  2006.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  2007.                       "upstream sent invalid trailer: \"%*s\\x%02xd...\"",
  2008.                       r->header_end - r->header_name_start,
  2009.                       r->header_name_start, *r->header_end);

  2010.         return NGX_ERROR;
  2011.     }
  2012. }


  2013. static void
  2014. ngx_http_proxy_abort_request(ngx_http_request_t *r)
  2015. {
  2016.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  2017.                    "abort http proxy request");

  2018.     return;
  2019. }


  2020. static void
  2021. ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
  2022. {
  2023.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  2024.                    "finalize http proxy request");

  2025.     return;
  2026. }


  2027. static ngx_int_t
  2028. ngx_http_proxy_host_variable(ngx_http_request_t *r,
  2029.     ngx_http_variable_value_t *v, uintptr_t data)
  2030. {
  2031.     ngx_http_proxy_ctx_t  *ctx;

  2032.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  2033.     if (ctx == NULL) {
  2034.         v->not_found = 1;
  2035.         return NGX_OK;
  2036.     }

  2037.     if (data == 1 && !ctx->legacy) {
  2038.         v->not_found = 1;
  2039.         return NGX_OK;
  2040.     }

  2041.     v->len = ctx->vars.host_header.len;
  2042.     v->valid = 1;
  2043.     v->no_cacheable = 0;
  2044.     v->not_found = 0;
  2045.     v->data = ctx->vars.host_header.data;

  2046.     return NGX_OK;
  2047. }


  2048. static ngx_int_t
  2049. ngx_http_proxy_port_variable(ngx_http_request_t *r,
  2050.     ngx_http_variable_value_t *v, uintptr_t data)
  2051. {
  2052.     ngx_http_proxy_ctx_t  *ctx;

  2053.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  2054.     if (ctx == NULL) {
  2055.         v->not_found = 1;
  2056.         return NGX_OK;
  2057.     }

  2058.     v->len = ctx->vars.port.len;
  2059.     v->valid = 1;
  2060.     v->no_cacheable = 0;
  2061.     v->not_found = 0;
  2062.     v->data = ctx->vars.port.data;

  2063.     return NGX_OK;
  2064. }


  2065. static ngx_int_t
  2066. ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
  2067.     ngx_http_variable_value_t *v, uintptr_t data)
  2068. {
  2069.     size_t            len;
  2070.     u_char           *p;
  2071.     ngx_table_elt_t  *h, *xfwd;

  2072.     v->valid = 1;
  2073.     v->no_cacheable = 0;
  2074.     v->not_found = 0;

  2075.     xfwd = r->headers_in.x_forwarded_for;

  2076.     len = 0;

  2077.     for (h = xfwd; h; h = h->next) {
  2078.         len += h->value.len + sizeof(", ") - 1;
  2079.     }

  2080.     if (len == 0) {
  2081.         v->len = r->connection->addr_text.len;
  2082.         v->data = r->connection->addr_text.data;
  2083.         return NGX_OK;
  2084.     }

  2085.     len += r->connection->addr_text.len;

  2086.     p = ngx_pnalloc(r->pool, len);
  2087.     if (p == NULL) {
  2088.         return NGX_ERROR;
  2089.     }

  2090.     v->len = len;
  2091.     v->data = p;

  2092.     for (h = xfwd; h; h = h->next) {
  2093.         p = ngx_copy(p, h->value.data, h->value.len);
  2094.         *p++ = ','; *p++ = ' ';
  2095.     }

  2096.     ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len);

  2097.     return NGX_OK;
  2098. }


  2099. static ngx_int_t
  2100. ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
  2101.     ngx_http_variable_value_t *v, uintptr_t data)
  2102. {
  2103.     ngx_http_proxy_ctx_t  *ctx;

  2104.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  2105.     if (ctx == NULL || ctx->internal_body_length < 0) {
  2106.         v->not_found = 1;
  2107.         return NGX_OK;
  2108.     }

  2109.     v->valid = 1;
  2110.     v->no_cacheable = 0;
  2111.     v->not_found = 0;

  2112.     v->data = ngx_pnalloc(r->pool, NGX_OFF_T_LEN);

  2113.     if (v->data == NULL) {
  2114.         return NGX_ERROR;
  2115.     }

  2116.     v->len = ngx_sprintf(v->data, "%O", ctx->internal_body_length) - v->data;

  2117.     return NGX_OK;
  2118. }


  2119. static ngx_int_t
  2120. ngx_http_proxy_internal_chunked_variable(ngx_http_request_t *r,
  2121.     ngx_http_variable_value_t *v, uintptr_t data)
  2122. {
  2123.     ngx_http_proxy_ctx_t  *ctx;

  2124.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  2125.     if (ctx == NULL || !ctx->internal_chunked) {
  2126.         v->not_found = 1;
  2127.         return NGX_OK;
  2128.     }

  2129.     v->valid = 1;
  2130.     v->no_cacheable = 0;
  2131.     v->not_found = 0;

  2132.     v->data = (u_char *) "chunked";
  2133.     v->len = sizeof("chunked") - 1;

  2134.     return NGX_OK;
  2135. }


  2136. ngx_int_t
  2137. ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,
  2138.     size_t prefix)
  2139. {
  2140.     size_t                      len;
  2141.     ngx_int_t                   rc;
  2142.     ngx_uint_t                  i;
  2143.     ngx_http_proxy_rewrite_t   *pr;
  2144.     ngx_http_proxy_loc_conf_t  *plcf;

  2145.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  2146.     pr = plcf->redirects->elts;

  2147.     if (pr == NULL) {
  2148.         return NGX_DECLINED;
  2149.     }

  2150.     len = h->value.len - prefix;

  2151.     for (i = 0; i < plcf->redirects->nelts; i++) {
  2152.         rc = pr[i].handler(r, &h->value, prefix, len, &pr[i]);

  2153.         if (rc != NGX_DECLINED) {
  2154.             return rc;
  2155.         }
  2156.     }

  2157.     return NGX_DECLINED;
  2158. }


  2159. ngx_int_t
  2160. ngx_http_proxy_rewrite_cookie(ngx_http_request_t *r, ngx_table_elt_t *h)
  2161. {
  2162.     u_char                     *p;
  2163.     size_t                      len;
  2164.     ngx_int_t                   rc, rv;
  2165.     ngx_str_t                  *key, *value;
  2166.     ngx_uint_t                  i;
  2167.     ngx_array_t                 attrs;
  2168.     ngx_keyval_t               *attr;
  2169.     ngx_http_proxy_loc_conf_t  *plcf;

  2170.     if (ngx_array_init(&attrs, r->pool, 2, sizeof(ngx_keyval_t)) != NGX_OK) {
  2171.         return NGX_ERROR;
  2172.     }

  2173.     if (ngx_http_proxy_parse_cookie(&h->value, &attrs) != NGX_OK) {
  2174.         return NGX_ERROR;
  2175.     }

  2176.     attr = attrs.elts;

  2177.     if (attr[0].value.data == NULL) {
  2178.         return NGX_DECLINED;
  2179.     }

  2180.     rv = NGX_DECLINED;

  2181.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  2182.     for (i = 1; i < attrs.nelts; i++) {

  2183.         key = &attr[i].key;
  2184.         value = &attr[i].value;

  2185.         if (plcf->cookie_domains && key->len == 6
  2186.             && ngx_strncasecmp(key->data, (u_char *) "domain", 6) == 0
  2187.             && value->data)
  2188.         {
  2189.             rc = ngx_http_proxy_rewrite_cookie_value(r, value,
  2190.                                                      plcf->cookie_domains);
  2191.             if (rc == NGX_ERROR) {
  2192.                 return NGX_ERROR;
  2193.             }

  2194.             if (rc != NGX_DECLINED) {
  2195.                 rv = rc;
  2196.             }
  2197.         }

  2198.         if (plcf->cookie_paths && key->len == 4
  2199.             && ngx_strncasecmp(key->data, (u_char *) "path", 4) == 0
  2200.             && value->data)
  2201.         {
  2202.             rc = ngx_http_proxy_rewrite_cookie_value(r, value,
  2203.                                                      plcf->cookie_paths);
  2204.             if (rc == NGX_ERROR) {
  2205.                 return NGX_ERROR;
  2206.             }

  2207.             if (rc != NGX_DECLINED) {
  2208.                 rv = rc;
  2209.             }
  2210.         }
  2211.     }

  2212.     if (plcf->cookie_flags) {
  2213.         rc = ngx_http_proxy_rewrite_cookie_flags(r, &attrs,
  2214.                                                  plcf->cookie_flags);
  2215.         if (rc == NGX_ERROR) {
  2216.             return NGX_ERROR;
  2217.         }

  2218.         if (rc != NGX_DECLINED) {
  2219.             rv = rc;
  2220.         }

  2221.         attr = attrs.elts;
  2222.     }

  2223.     if (rv != NGX_OK) {
  2224.         return rv;
  2225.     }

  2226.     len = 0;

  2227.     for (i = 0; i < attrs.nelts; i++) {

  2228.         if (attr[i].key.data == NULL) {
  2229.             continue;
  2230.         }

  2231.         if (i > 0) {
  2232.             len += 2;
  2233.         }

  2234.         len += attr[i].key.len;

  2235.         if (attr[i].value.data) {
  2236.             len += 1 + attr[i].value.len;
  2237.         }
  2238.     }

  2239.     p = ngx_pnalloc(r->pool, len + 1);
  2240.     if (p == NULL) {
  2241.         return NGX_ERROR;
  2242.     }

  2243.     h->value.data = p;
  2244.     h->value.len = len;

  2245.     for (i = 0; i < attrs.nelts; i++) {

  2246.         if (attr[i].key.data == NULL) {
  2247.             continue;
  2248.         }

  2249.         if (i > 0) {
  2250.             *p++ = ';';
  2251.             *p++ = ' ';
  2252.         }

  2253.         p = ngx_cpymem(p, attr[i].key.data, attr[i].key.len);

  2254.         if (attr[i].value.data) {
  2255.             *p++ = '=';
  2256.             p = ngx_cpymem(p, attr[i].value.data, attr[i].value.len);
  2257.         }
  2258.     }

  2259.     *p = '\0';

  2260.     return NGX_OK;
  2261. }


  2262. static ngx_int_t
  2263. ngx_http_proxy_parse_cookie(ngx_str_t *value, ngx_array_t *attrs)
  2264. {
  2265.     u_char        *start, *end, *p, *last;
  2266.     ngx_str_t      name, val;
  2267.     ngx_keyval_t  *attr;

  2268.     start = value->data;
  2269.     end = value->data + value->len;

  2270.     for ( ;; ) {

  2271.         last = (u_char *) ngx_strchr(start, ';');

  2272.         if (last == NULL) {
  2273.             last = end;
  2274.         }

  2275.         while (start < last && *start == ' ') { start++; }

  2276.         for (p = start; p < last && *p != '='; p++) { /* void */ }

  2277.         name.data = start;
  2278.         name.len = p - start;

  2279.         while (name.len && name.data[name.len - 1] == ' ') {
  2280.             name.len--;
  2281.         }

  2282.         if (p < last) {

  2283.             p++;

  2284.             while (p < last && *p == ' ') { p++; }

  2285.             val.data = p;
  2286.             val.len = last - val.data;

  2287.             while (val.len && val.data[val.len - 1] == ' ') {
  2288.                 val.len--;
  2289.             }

  2290.         } else {
  2291.             ngx_str_null(&val);
  2292.         }

  2293.         attr = ngx_array_push(attrs);
  2294.         if (attr == NULL) {
  2295.             return NGX_ERROR;
  2296.         }

  2297.         attr->key = name;
  2298.         attr->value = val;

  2299.         if (last == end) {
  2300.             break;
  2301.         }

  2302.         start = last + 1;
  2303.     }

  2304.     return NGX_OK;
  2305. }


  2306. static ngx_int_t
  2307. ngx_http_proxy_rewrite_cookie_value(ngx_http_request_t *r, ngx_str_t *value,
  2308.     ngx_array_t *rewrites)
  2309. {
  2310.     ngx_int_t                  rc;
  2311.     ngx_uint_t                 i;
  2312.     ngx_http_proxy_rewrite_t  *pr;

  2313.     pr = rewrites->elts;

  2314.     for (i = 0; i < rewrites->nelts; i++) {
  2315.         rc = pr[i].handler(r, value, 0, value->len, &pr[i]);

  2316.         if (rc != NGX_DECLINED) {
  2317.             return rc;
  2318.         }
  2319.     }

  2320.     return NGX_DECLINED;
  2321. }


  2322. static ngx_int_t
  2323. ngx_http_proxy_rewrite_cookie_flags(ngx_http_request_t *r, ngx_array_t *attrs,
  2324.     ngx_array_t *flags)
  2325. {
  2326.     ngx_str_t                       pattern, value;
  2327. #if (NGX_PCRE)
  2328.     ngx_int_t                       rc;
  2329. #endif
  2330.     ngx_uint_t                      i, m, f, nelts;
  2331.     ngx_keyval_t                   *attr;
  2332.     ngx_conf_bitmask_t             *mask;
  2333.     ngx_http_complex_value_t       *flags_values;
  2334.     ngx_http_proxy_cookie_flags_t  *pcf;

  2335.     attr = attrs->elts;
  2336.     pcf = flags->elts;

  2337.     for (i = 0; i < flags->nelts; i++) {

  2338. #if (NGX_PCRE)
  2339.         if (pcf[i].regex) {
  2340.             rc = ngx_http_regex_exec(r, pcf[i].cookie.regex, &attr[0].key);

  2341.             if (rc == NGX_ERROR) {
  2342.                 return NGX_ERROR;
  2343.             }

  2344.             if (rc == NGX_OK) {
  2345.                 break;
  2346.             }

  2347.             /* NGX_DECLINED */

  2348.             continue;
  2349.         }
  2350. #endif

  2351.         if (ngx_http_complex_value(r, &pcf[i].cookie.complex, &pattern)
  2352.             != NGX_OK)
  2353.         {
  2354.             return NGX_ERROR;
  2355.         }

  2356.         if (pattern.len == attr[0].key.len
  2357.             && ngx_strncasecmp(attr[0].key.data, pattern.data, pattern.len)
  2358.                == 0)
  2359.         {
  2360.             break;
  2361.         }
  2362.     }

  2363.     if (i == flags->nelts) {
  2364.         return NGX_DECLINED;
  2365.     }

  2366.     nelts = pcf[i].flags_values.nelts;
  2367.     flags_values = pcf[i].flags_values.elts;

  2368.     mask = ngx_http_proxy_cookie_flags_masks;
  2369.     f = 0;

  2370.     for (i = 0; i < nelts; i++) {

  2371.         if (ngx_http_complex_value(r, &flags_values[i], &value) != NGX_OK) {
  2372.             return NGX_ERROR;
  2373.         }

  2374.         if (value.len == 0) {
  2375.             continue;
  2376.         }

  2377.         for (m = 0; mask[m].name.len != 0; m++) {

  2378.             if (mask[m].name.len != value.len
  2379.                 || ngx_strncasecmp(mask[m].name.data, value.data, value.len)
  2380.                    != 0)
  2381.             {
  2382.                 continue;
  2383.             }

  2384.             f |= mask[m].mask;

  2385.             break;
  2386.         }

  2387.         if (mask[m].name.len == 0) {
  2388.             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  2389.                            "invalid proxy_cookie_flags flag \"%V\"", &value);
  2390.         }
  2391.     }

  2392.     if (f == 0) {
  2393.         return NGX_DECLINED;
  2394.     }

  2395.     return ngx_http_proxy_edit_cookie_flags(r, attrs, f);
  2396. }


  2397. static ngx_int_t
  2398. ngx_http_proxy_edit_cookie_flags(ngx_http_request_t *r, ngx_array_t *attrs,
  2399.     ngx_uint_t flags)
  2400. {
  2401.     ngx_str_t     *key, *value;
  2402.     ngx_uint_t     i;
  2403.     ngx_keyval_t  *attr;

  2404.     attr = attrs->elts;

  2405.     for (i = 1; i < attrs->nelts; i++) {
  2406.         key = &attr[i].key;

  2407.         if (key->len == 6
  2408.             && ngx_strncasecmp(key->data, (u_char *) "secure", 6) == 0)
  2409.         {
  2410.             if (flags & NGX_HTTP_PROXY_COOKIE_SECURE_ON) {
  2411.                 flags &= ~NGX_HTTP_PROXY_COOKIE_SECURE_ON;

  2412.             } else if (flags & NGX_HTTP_PROXY_COOKIE_SECURE_OFF) {
  2413.                 key->data = NULL;
  2414.             }

  2415.             continue;
  2416.         }

  2417.         if (key->len == 8
  2418.             && ngx_strncasecmp(key->data, (u_char *) "httponly", 8) == 0)
  2419.         {
  2420.             if (flags & NGX_HTTP_PROXY_COOKIE_HTTPONLY_ON) {
  2421.                 flags &= ~NGX_HTTP_PROXY_COOKIE_HTTPONLY_ON;

  2422.             } else if (flags & NGX_HTTP_PROXY_COOKIE_HTTPONLY_OFF) {
  2423.                 key->data = NULL;
  2424.             }

  2425.             continue;
  2426.         }

  2427.         if (key->len == 8
  2428.             && ngx_strncasecmp(key->data, (u_char *) "samesite", 8) == 0)
  2429.         {
  2430.             value = &attr[i].value;

  2431.             if (flags & NGX_HTTP_PROXY_COOKIE_SAMESITE_STRICT) {
  2432.                 flags &= ~NGX_HTTP_PROXY_COOKIE_SAMESITE_STRICT;

  2433.                 if (value->len != 6
  2434.                     || ngx_strncasecmp(value->data, (u_char *) "strict", 6)
  2435.                        != 0)
  2436.                 {
  2437.                     ngx_str_set(key, "SameSite");
  2438.                     ngx_str_set(value, "Strict");
  2439.                 }

  2440.             } else if (flags & NGX_HTTP_PROXY_COOKIE_SAMESITE_LAX) {
  2441.                 flags &= ~NGX_HTTP_PROXY_COOKIE_SAMESITE_LAX;

  2442.                 if (value->len != 3
  2443.                     || ngx_strncasecmp(value->data, (u_char *) "lax", 3) != 0)
  2444.                 {
  2445.                     ngx_str_set(key, "SameSite");
  2446.                     ngx_str_set(value, "Lax");
  2447.                 }

  2448.             } else if (flags & NGX_HTTP_PROXY_COOKIE_SAMESITE_NONE) {
  2449.                 flags &= ~NGX_HTTP_PROXY_COOKIE_SAMESITE_NONE;

  2450.                 if (value->len != 4
  2451.                     || ngx_strncasecmp(value->data, (u_char *) "none", 4) != 0)
  2452.                 {
  2453.                     ngx_str_set(key, "SameSite");
  2454.                     ngx_str_set(value, "None");
  2455.                 }

  2456.             } else if (flags & NGX_HTTP_PROXY_COOKIE_SAMESITE_OFF) {
  2457.                 key->data = NULL;
  2458.             }

  2459.             continue;
  2460.         }
  2461.     }

  2462.     if (flags & NGX_HTTP_PROXY_COOKIE_SECURE_ON) {
  2463.         attr = ngx_array_push(attrs);
  2464.         if (attr == NULL) {
  2465.             return NGX_ERROR;
  2466.         }

  2467.         ngx_str_set(&attr->key, "Secure");
  2468.         ngx_str_null(&attr->value);
  2469.     }

  2470.     if (flags & NGX_HTTP_PROXY_COOKIE_HTTPONLY_ON) {
  2471.         attr = ngx_array_push(attrs);
  2472.         if (attr == NULL) {
  2473.             return NGX_ERROR;
  2474.         }

  2475.         ngx_str_set(&attr->key, "HttpOnly");
  2476.         ngx_str_null(&attr->value);
  2477.     }

  2478.     if (flags & (NGX_HTTP_PROXY_COOKIE_SAMESITE_STRICT
  2479.                  |NGX_HTTP_PROXY_COOKIE_SAMESITE_LAX
  2480.                  |NGX_HTTP_PROXY_COOKIE_SAMESITE_NONE))
  2481.     {
  2482.         attr = ngx_array_push(attrs);
  2483.         if (attr == NULL) {
  2484.             return NGX_ERROR;
  2485.         }

  2486.         ngx_str_set(&attr->key, "SameSite");

  2487.         if (flags & NGX_HTTP_PROXY_COOKIE_SAMESITE_STRICT) {
  2488.             ngx_str_set(&attr->value, "Strict");

  2489.         } else if (flags & NGX_HTTP_PROXY_COOKIE_SAMESITE_LAX) {
  2490.             ngx_str_set(&attr->value, "Lax");

  2491.         } else {
  2492.             ngx_str_set(&attr->value, "None");
  2493.         }
  2494.     }

  2495.     return NGX_OK;
  2496. }


  2497. static ngx_int_t
  2498. ngx_http_proxy_rewrite_complex_handler(ngx_http_request_t *r, ngx_str_t *value,
  2499.     size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
  2500. {
  2501.     ngx_str_t  pattern, replacement;

  2502.     if (ngx_http_complex_value(r, &pr->pattern.complex, &pattern) != NGX_OK) {
  2503.         return NGX_ERROR;
  2504.     }

  2505.     if (pattern.len > len
  2506.         || ngx_rstrncmp(value->data + prefix, pattern.data, pattern.len) != 0)
  2507.     {
  2508.         return NGX_DECLINED;
  2509.     }

  2510.     if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
  2511.         return NGX_ERROR;
  2512.     }

  2513.     return ngx_http_proxy_rewrite(r, value, prefix, pattern.len, &replacement);
  2514. }


  2515. #if (NGX_PCRE)

  2516. static ngx_int_t
  2517. ngx_http_proxy_rewrite_regex_handler(ngx_http_request_t *r, ngx_str_t *value,
  2518.     size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
  2519. {
  2520.     ngx_str_t  pattern, replacement;

  2521.     pattern.len = len;
  2522.     pattern.data = value->data + prefix;

  2523.     if (ngx_http_regex_exec(r, pr->pattern.regex, &pattern) != NGX_OK) {
  2524.         return NGX_DECLINED;
  2525.     }

  2526.     if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
  2527.         return NGX_ERROR;
  2528.     }

  2529.     return ngx_http_proxy_rewrite(r, value, prefix, len, &replacement);
  2530. }

  2531. #endif


  2532. static ngx_int_t
  2533. ngx_http_proxy_rewrite_domain_handler(ngx_http_request_t *r, ngx_str_t *value,
  2534.     size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
  2535. {
  2536.     u_char     *p;
  2537.     ngx_str_t   pattern, replacement;

  2538.     if (ngx_http_complex_value(r, &pr->pattern.complex, &pattern) != NGX_OK) {
  2539.         return NGX_ERROR;
  2540.     }

  2541.     p = value->data + prefix;

  2542.     if (len && p[0] == '.') {
  2543.         p++;
  2544.         prefix++;
  2545.         len--;
  2546.     }

  2547.     if (pattern.len != len || ngx_rstrncasecmp(pattern.data, p, len) != 0) {
  2548.         return NGX_DECLINED;
  2549.     }

  2550.     if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
  2551.         return NGX_ERROR;
  2552.     }

  2553.     return ngx_http_proxy_rewrite(r, value, prefix, len, &replacement);
  2554. }


  2555. static ngx_int_t
  2556. ngx_http_proxy_rewrite(ngx_http_request_t *r, ngx_str_t *value, size_t prefix,
  2557.     size_t len, ngx_str_t *replacement)
  2558. {
  2559.     u_char  *p, *data;
  2560.     size_t   new_len;

  2561.     if (len == value->len) {
  2562.         *value = *replacement;
  2563.         return NGX_OK;
  2564.     }

  2565.     new_len = replacement->len + value->len - len;

  2566.     if (replacement->len > len) {

  2567.         data = ngx_pnalloc(r->pool, new_len + 1);
  2568.         if (data == NULL) {
  2569.             return NGX_ERROR;
  2570.         }

  2571.         p = ngx_copy(data, value->data, prefix);
  2572.         p = ngx_copy(p, replacement->data, replacement->len);

  2573.         ngx_memcpy(p, value->data + prefix + len,
  2574.                    value->len - len - prefix + 1);

  2575.         value->data = data;

  2576.     } else {
  2577.         p = ngx_copy(value->data + prefix, replacement->data, replacement->len);

  2578.         ngx_memmove(p, value->data + prefix + len,
  2579.                     value->len - len - prefix + 1);
  2580.     }

  2581.     value->len = new_len;

  2582.     return NGX_OK;
  2583. }


  2584. static ngx_int_t
  2585. ngx_http_proxy_add_variables(ngx_conf_t *cf)
  2586. {
  2587.     ngx_http_variable_t  *var, *v;

  2588.     for (v = ngx_http_proxy_vars; v->name.len; v++) {
  2589.         var = ngx_http_add_variable(cf, &v->name, v->flags);
  2590.         if (var == NULL) {
  2591.             return NGX_ERROR;
  2592.         }

  2593.         var->get_handler = v->get_handler;
  2594.         var->data = v->data;
  2595.     }

  2596.     return NGX_OK;
  2597. }


  2598. static void *
  2599. ngx_http_proxy_create_main_conf(ngx_conf_t *cf)
  2600. {
  2601.     ngx_http_proxy_main_conf_t  *conf;

  2602.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_main_conf_t));
  2603.     if (conf == NULL) {
  2604.         return NULL;
  2605.     }

  2606. #if (NGX_HTTP_CACHE)
  2607.     if (ngx_array_init(&conf->caches, cf->pool, 4,
  2608.                        sizeof(ngx_http_file_cache_t *))
  2609.         != NGX_OK)
  2610.     {
  2611.         return NULL;
  2612.     }
  2613. #endif

  2614.     return conf;
  2615. }


  2616. static void *
  2617. ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
  2618. {
  2619.     ngx_http_proxy_loc_conf_t  *conf;

  2620.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t));
  2621.     if (conf == NULL) {
  2622.         return NULL;
  2623.     }

  2624.     /*
  2625.      * set by ngx_pcalloc():
  2626.      *
  2627.      *     conf->upstream.bufs.num = 0;
  2628.      *     conf->upstream.ignore_headers = 0;
  2629.      *     conf->upstream.next_upstream = 0;
  2630.      *     conf->upstream.cache_zone = NULL;
  2631.      *     conf->upstream.cache_use_stale = 0;
  2632.      *     conf->upstream.cache_methods = 0;
  2633.      *     conf->upstream.temp_path = NULL;
  2634.      *     conf->upstream.hide_headers_hash = { NULL, 0 };
  2635.      *     conf->upstream.store_lengths = NULL;
  2636.      *     conf->upstream.store_values = NULL;
  2637.      *
  2638.      *     conf->location = NULL;
  2639.      *     conf->url = { 0, NULL };
  2640.      *     conf->headers.lengths = NULL;
  2641.      *     conf->headers.values = NULL;
  2642.      *     conf->headers.hash = { NULL, 0 };
  2643.      *     conf->headers_cache.lengths = NULL;
  2644.      *     conf->host_set = 0;
  2645.      *     conf->headers_cache.values = NULL;
  2646.      *     conf->headers_cache.hash = { NULL, 0 };
  2647.      *     conf->body_lengths = NULL;
  2648.      *     conf->body_values = NULL;
  2649.      *     conf->body_source = { 0, NULL };
  2650.      *     conf->redirects = NULL;
  2651.      *     conf->ssl = 0;
  2652.      *     conf->ssl_protocols = 0;
  2653.      *     conf->ssl_ciphers = { 0, NULL };
  2654.      *     conf->ssl_trusted_certificate = { 0, NULL };
  2655.      *     conf->ssl_crl = { 0, NULL };
  2656.      */

  2657.     conf->upstream.store = NGX_CONF_UNSET;
  2658.     conf->upstream.store_access = NGX_CONF_UNSET_UINT;
  2659.     conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
  2660.     conf->upstream.buffering = NGX_CONF_UNSET;
  2661.     conf->upstream.request_buffering = NGX_CONF_UNSET;
  2662.     conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
  2663.     conf->upstream.force_ranges = NGX_CONF_UNSET;

  2664.     conf->upstream.local = NGX_CONF_UNSET_PTR;
  2665.     conf->upstream.socket_keepalive = NGX_CONF_UNSET;

  2666.     conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
  2667.     conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
  2668.     conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
  2669.     conf->upstream.next_upstream_timeout = NGX_CONF_UNSET_MSEC;

  2670.     conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
  2671.     conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
  2672.     conf->upstream.limit_rate = NGX_CONF_UNSET_PTR;

  2673.     conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
  2674.     conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
  2675.     conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;

  2676.     conf->upstream.pass_request_headers = NGX_CONF_UNSET;
  2677.     conf->upstream.pass_request_body = NGX_CONF_UNSET;
  2678.     conf->upstream.pass_trailers = NGX_CONF_UNSET;

  2679. #if (NGX_HTTP_CACHE)
  2680.     conf->upstream.cache = NGX_CONF_UNSET;
  2681.     conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
  2682.     conf->upstream.cache_max_range_offset = NGX_CONF_UNSET;
  2683.     conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
  2684.     conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
  2685.     conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
  2686.     conf->upstream.cache_lock = NGX_CONF_UNSET;
  2687.     conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
  2688.     conf->upstream.cache_lock_age = NGX_CONF_UNSET_MSEC;
  2689.     conf->upstream.cache_revalidate = NGX_CONF_UNSET;
  2690.     conf->upstream.cache_convert_head = NGX_CONF_UNSET;
  2691.     conf->upstream.cache_background_update = NGX_CONF_UNSET;
  2692. #endif

  2693.     conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
  2694.     conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;

  2695.     conf->upstream.intercept_errors = NGX_CONF_UNSET;

  2696. #if (NGX_HTTP_SSL)
  2697.     conf->upstream.ssl_session_reuse = NGX_CONF_UNSET;
  2698.     conf->upstream.ssl_name = NGX_CONF_UNSET_PTR;
  2699.     conf->upstream.ssl_server_name = NGX_CONF_UNSET;
  2700.     conf->upstream.ssl_verify = NGX_CONF_UNSET;
  2701.     conf->upstream.ssl_certificate = NGX_CONF_UNSET_PTR;
  2702.     conf->upstream.ssl_certificate_key = NGX_CONF_UNSET_PTR;
  2703.     conf->upstream.ssl_certificate_cache = NGX_CONF_UNSET_PTR;
  2704.     conf->upstream.ssl_passwords = NGX_CONF_UNSET_PTR;
  2705.     conf->ssl_verify_depth = NGX_CONF_UNSET_UINT;
  2706.     conf->ssl_conf_commands = NGX_CONF_UNSET_PTR;
  2707. #endif

  2708.     /* the hardcoded values */
  2709.     conf->upstream.cyclic_temp_file = 0;
  2710.     conf->upstream.change_buffering = 1;
  2711.     conf->upstream.pass_early_hints = 1;

  2712.     conf->headers_source = NGX_CONF_UNSET_PTR;

  2713.     conf->method = NGX_CONF_UNSET_PTR;

  2714.     conf->redirect = NGX_CONF_UNSET;

  2715.     conf->cookie_domains = NGX_CONF_UNSET_PTR;
  2716.     conf->cookie_paths = NGX_CONF_UNSET_PTR;
  2717.     conf->cookie_flags = NGX_CONF_UNSET_PTR;

  2718.     conf->http_version = NGX_CONF_UNSET_UINT;

  2719.     conf->headers_hash_max_size = NGX_CONF_UNSET_UINT;
  2720.     conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT;

  2721.     ngx_str_set(&conf->upstream.module, "proxy");

  2722.     return conf;
  2723. }


  2724. static char *
  2725. ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
  2726. {
  2727.     ngx_http_proxy_loc_conf_t *prev = parent;
  2728.     ngx_http_proxy_loc_conf_t *conf = child;

  2729.     u_char                     *p;
  2730.     size_t                      size;
  2731.     ngx_int_t                   rc;
  2732.     ngx_hash_init_t             hash;
  2733.     ngx_http_core_loc_conf_t   *clcf;
  2734.     ngx_http_proxy_rewrite_t   *pr;
  2735.     ngx_http_script_compile_t   sc;

  2736. #if (NGX_HTTP_CACHE)

  2737.     if (conf->upstream.store > 0) {
  2738.         conf->upstream.cache = 0;
  2739.     }

  2740.     if (conf->upstream.cache > 0) {
  2741.         conf->upstream.store = 0;
  2742.     }

  2743. #endif

  2744.     if (conf->upstream.store == NGX_CONF_UNSET) {
  2745.         ngx_conf_merge_value(conf->upstream.store,
  2746.                               prev->upstream.store, 0);

  2747.         conf->upstream.store_lengths = prev->upstream.store_lengths;
  2748.         conf->upstream.store_values = prev->upstream.store_values;
  2749.     }

  2750.     ngx_conf_merge_uint_value(conf->upstream.store_access,
  2751.                               prev->upstream.store_access, 0600);

  2752.     ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,
  2753.                               prev->upstream.next_upstream_tries, 0);

  2754.     ngx_conf_merge_value(conf->upstream.buffering,
  2755.                               prev->upstream.buffering, 1);

  2756.     ngx_conf_merge_value(conf->upstream.request_buffering,
  2757.                               prev->upstream.request_buffering, 1);

  2758.     ngx_conf_merge_value(conf->upstream.ignore_client_abort,
  2759.                               prev->upstream.ignore_client_abort, 0);

  2760.     ngx_conf_merge_value(conf->upstream.force_ranges,
  2761.                               prev->upstream.force_ranges, 0);

  2762.     ngx_conf_merge_ptr_value(conf->upstream.local,
  2763.                               prev->upstream.local, NULL);

  2764.     ngx_conf_merge_value(conf->upstream.socket_keepalive,
  2765.                               prev->upstream.socket_keepalive, 0);

  2766.     ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
  2767.                               prev->upstream.connect_timeout, 60000);

  2768.     ngx_conf_merge_msec_value(conf->upstream.send_timeout,
  2769.                               prev->upstream.send_timeout, 60000);

  2770.     ngx_conf_merge_msec_value(conf->upstream.read_timeout,
  2771.                               prev->upstream.read_timeout, 60000);

  2772.     ngx_conf_merge_msec_value(conf->upstream.next_upstream_timeout,
  2773.                               prev->upstream.next_upstream_timeout, 0);

  2774.     ngx_conf_merge_size_value(conf->upstream.send_lowat,
  2775.                               prev->upstream.send_lowat, 0);

  2776.     ngx_conf_merge_size_value(conf->upstream.buffer_size,
  2777.                               prev->upstream.buffer_size,
  2778.                               (size_t) ngx_pagesize);

  2779.     ngx_conf_merge_ptr_value(conf->upstream.limit_rate,
  2780.                               prev->upstream.limit_rate, NULL);

  2781.     ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
  2782.                               8, ngx_pagesize);

  2783.     if (conf->upstream.bufs.num < 2) {
  2784.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  2785.                            "there must be at least 2 \"proxy_buffers\"");
  2786.         return NGX_CONF_ERROR;
  2787.     }


  2788.     size = conf->upstream.buffer_size;
  2789.     if (size < conf->upstream.bufs.size) {
  2790.         size = conf->upstream.bufs.size;
  2791.     }


  2792.     ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
  2793.                               prev->upstream.busy_buffers_size_conf,
  2794.                               NGX_CONF_UNSET_SIZE);

  2795.     if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
  2796.         conf->upstream.busy_buffers_size = 2 * size;
  2797.     } else {
  2798.         conf->upstream.busy_buffers_size =
  2799.                                          conf->upstream.busy_buffers_size_conf;
  2800.     }

  2801.     if (conf->upstream.busy_buffers_size < size) {
  2802.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  2803.              "\"proxy_busy_buffers_size\" must be equal to or greater than "
  2804.              "the maximum of the value of \"proxy_buffer_size\" and "
  2805.              "one of the \"proxy_buffers\"");

  2806.         return NGX_CONF_ERROR;
  2807.     }

  2808.     if (conf->upstream.busy_buffers_size
  2809.         > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
  2810.     {
  2811.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  2812.              "\"proxy_busy_buffers_size\" must be less than "
  2813.              "the size of all \"proxy_buffers\" minus one buffer");

  2814.         return NGX_CONF_ERROR;
  2815.     }


  2816.     ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
  2817.                               prev->upstream.temp_file_write_size_conf,
  2818.                               NGX_CONF_UNSET_SIZE);

  2819.     if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
  2820.         conf->upstream.temp_file_write_size = 2 * size;
  2821.     } else {
  2822.         conf->upstream.temp_file_write_size =
  2823.                                       conf->upstream.temp_file_write_size_conf;
  2824.     }

  2825.     if (conf->upstream.temp_file_write_size < size) {
  2826.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  2827.              "\"proxy_temp_file_write_size\" must be equal to or greater "
  2828.              "than the maximum of the value of \"proxy_buffer_size\" and "
  2829.              "one of the \"proxy_buffers\"");

  2830.         return NGX_CONF_ERROR;
  2831.     }

  2832.     ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
  2833.                               prev->upstream.max_temp_file_size_conf,
  2834.                               NGX_CONF_UNSET_SIZE);

  2835.     if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
  2836.         conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
  2837.     } else {
  2838.         conf->upstream.max_temp_file_size =
  2839.                                         conf->upstream.max_temp_file_size_conf;
  2840.     }

  2841.     if (conf->upstream.max_temp_file_size != 0
  2842.         && conf->upstream.max_temp_file_size < size)
  2843.     {
  2844.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  2845.              "\"proxy_max_temp_file_size\" must be equal to zero to disable "
  2846.              "temporary files usage or must be equal to or greater than "
  2847.              "the maximum of the value of \"proxy_buffer_size\" and "
  2848.              "one of the \"proxy_buffers\"");

  2849.         return NGX_CONF_ERROR;
  2850.     }


  2851.     ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
  2852.                               prev->upstream.ignore_headers,
  2853.                               NGX_CONF_BITMASK_SET);


  2854.     ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
  2855.                               prev->upstream.next_upstream,
  2856.                               (NGX_CONF_BITMASK_SET
  2857.                                |NGX_HTTP_UPSTREAM_FT_ERROR
  2858.                                |NGX_HTTP_UPSTREAM_FT_TIMEOUT));

  2859.     if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
  2860.         conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
  2861.                                        |NGX_HTTP_UPSTREAM_FT_OFF;
  2862.     }

  2863.     if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
  2864.                               prev->upstream.temp_path,
  2865.                               &ngx_http_proxy_temp_path)
  2866.         != NGX_CONF_OK)
  2867.     {
  2868.         return NGX_CONF_ERROR;
  2869.     }


  2870. #if (NGX_HTTP_CACHE)

  2871.     if (conf->upstream.cache == NGX_CONF_UNSET) {
  2872.         ngx_conf_merge_value(conf->upstream.cache,
  2873.                               prev->upstream.cache, 0);

  2874.         conf->upstream.cache_zone = prev->upstream.cache_zone;
  2875.         conf->upstream.cache_value = prev->upstream.cache_value;
  2876.     }

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

  2879.         shm_zone = conf->upstream.cache_zone;

  2880.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  2881.                            "\"proxy_cache\" zone \"%V\" is unknown",
  2882.                            &shm_zone->shm.name);

  2883.         return NGX_CONF_ERROR;
  2884.     }

  2885.     ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
  2886.                               prev->upstream.cache_min_uses, 1);

  2887.     ngx_conf_merge_off_value(conf->upstream.cache_max_range_offset,
  2888.                               prev->upstream.cache_max_range_offset,
  2889.                               NGX_MAX_OFF_T_VALUE);

  2890.     ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
  2891.                               prev->upstream.cache_use_stale,
  2892.                               (NGX_CONF_BITMASK_SET
  2893.                                |NGX_HTTP_UPSTREAM_FT_OFF));

  2894.     if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
  2895.         conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
  2896.                                          |NGX_HTTP_UPSTREAM_FT_OFF;
  2897.     }

  2898.     if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
  2899.         conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
  2900.     }

  2901.     if (conf->upstream.cache_methods == 0) {
  2902.         conf->upstream.cache_methods = prev->upstream.cache_methods;
  2903.     }

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

  2905.     ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
  2906.                              prev->upstream.cache_bypass, NULL);

  2907.     ngx_conf_merge_ptr_value(conf->upstream.no_cache,
  2908.                              prev->upstream.no_cache, NULL);

  2909.     ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
  2910.                              prev->upstream.cache_valid, NULL);

  2911.     if (conf->cache_key.value.data == NULL) {
  2912.         conf->cache_key = prev->cache_key;
  2913.     }

  2914.     ngx_conf_merge_value(conf->upstream.cache_lock,
  2915.                               prev->upstream.cache_lock, 0);

  2916.     ngx_conf_merge_msec_value(conf->upstream.cache_lock_timeout,
  2917.                               prev->upstream.cache_lock_timeout, 5000);

  2918.     ngx_conf_merge_msec_value(conf->upstream.cache_lock_age,
  2919.                               prev->upstream.cache_lock_age, 5000);

  2920.     ngx_conf_merge_value(conf->upstream.cache_revalidate,
  2921.                               prev->upstream.cache_revalidate, 0);

  2922.     ngx_conf_merge_value(conf->upstream.cache_convert_head,
  2923.                               prev->upstream.cache_convert_head, 1);

  2924.     ngx_conf_merge_value(conf->upstream.cache_background_update,
  2925.                               prev->upstream.cache_background_update, 0);

  2926. #endif

  2927.     ngx_conf_merge_value(conf->upstream.pass_request_headers,
  2928.                               prev->upstream.pass_request_headers, 1);
  2929.     ngx_conf_merge_value(conf->upstream.pass_request_body,
  2930.                               prev->upstream.pass_request_body, 1);

  2931.     ngx_conf_merge_value(conf->upstream.pass_trailers,
  2932.                               prev->upstream.pass_trailers, 0);

  2933.     ngx_conf_merge_value(conf->upstream.intercept_errors,
  2934.                               prev->upstream.intercept_errors, 0);

  2935. #if (NGX_HTTP_SSL)

  2936.     if (ngx_http_proxy_merge_ssl(cf, conf, prev) != NGX_OK) {
  2937.         return NGX_CONF_ERROR;
  2938.     }

  2939.     ngx_conf_merge_value(conf->upstream.ssl_session_reuse,
  2940.                               prev->upstream.ssl_session_reuse, 1);

  2941.     ngx_conf_merge_bitmask_value(conf->ssl_protocols, prev->ssl_protocols,
  2942.                               (NGX_CONF_BITMASK_SET|NGX_SSL_DEFAULT_PROTOCOLS));

  2943.     ngx_conf_merge_str_value(conf->ssl_ciphers, prev->ssl_ciphers,
  2944.                              "DEFAULT");

  2945.     ngx_conf_merge_ptr_value(conf->upstream.ssl_name,
  2946.                               prev->upstream.ssl_name, NULL);
  2947.     ngx_conf_merge_value(conf->upstream.ssl_server_name,
  2948.                               prev->upstream.ssl_server_name, 0);
  2949.     ngx_conf_merge_value(conf->upstream.ssl_verify,
  2950.                               prev->upstream.ssl_verify, 0);
  2951.     ngx_conf_merge_uint_value(conf->ssl_verify_depth,
  2952.                               prev->ssl_verify_depth, 1);
  2953.     ngx_conf_merge_str_value(conf->ssl_trusted_certificate,
  2954.                               prev->ssl_trusted_certificate, "");
  2955.     ngx_conf_merge_str_value(conf->ssl_crl, prev->ssl_crl, "");

  2956.     ngx_conf_merge_ptr_value(conf->upstream.ssl_certificate,
  2957.                               prev->upstream.ssl_certificate, NULL);
  2958.     ngx_conf_merge_ptr_value(conf->upstream.ssl_certificate_key,
  2959.                               prev->upstream.ssl_certificate_key, NULL);
  2960.     ngx_conf_merge_ptr_value(conf->upstream.ssl_certificate_cache,
  2961.                               prev->upstream.ssl_certificate_cache, NULL);

  2962.     if (ngx_http_upstream_merge_ssl_passwords(cf, &conf->upstream,
  2963.                                               &prev->upstream)
  2964.         != NGX_OK)
  2965.     {
  2966.         return NGX_CONF_ERROR;
  2967.     }

  2968.     ngx_conf_merge_ptr_value(conf->ssl_conf_commands,
  2969.                               prev->ssl_conf_commands, NULL);

  2970.     if (conf->ssl && ngx_http_proxy_set_ssl(cf, conf) != NGX_OK) {
  2971.         return NGX_CONF_ERROR;
  2972.     }

  2973. #endif

  2974.     ngx_conf_merge_ptr_value(conf->method, prev->method, NULL);

  2975.     ngx_conf_merge_value(conf->redirect, prev->redirect, 1);

  2976.     if (conf->redirect) {

  2977.         if (conf->redirects == NULL) {
  2978.             conf->redirects = prev->redirects;
  2979.         }

  2980.         if (conf->redirects == NULL && conf->url.data) {

  2981.             conf->redirects = ngx_array_create(cf->pool, 1,
  2982.                                              sizeof(ngx_http_proxy_rewrite_t));
  2983.             if (conf->redirects == NULL) {
  2984.                 return NGX_CONF_ERROR;
  2985.             }

  2986.             pr = ngx_array_push(conf->redirects);
  2987.             if (pr == NULL) {
  2988.                 return NGX_CONF_ERROR;
  2989.             }

  2990.             ngx_memzero(&pr->pattern.complex,
  2991.                         sizeof(ngx_http_complex_value_t));

  2992.             ngx_memzero(&pr->replacement, sizeof(ngx_http_complex_value_t));

  2993.             pr->handler = ngx_http_proxy_rewrite_complex_handler;

  2994.             if (conf->vars.uri.len) {
  2995.                 pr->pattern.complex.value = conf->url;
  2996.                 pr->replacement.value = conf->location;

  2997.             } else {
  2998.                 pr->pattern.complex.value.len = conf->url.len
  2999.                                                 + sizeof("/") - 1;

  3000.                 p = ngx_pnalloc(cf->pool, pr->pattern.complex.value.len);
  3001.                 if (p == NULL) {
  3002.                     return NGX_CONF_ERROR;
  3003.                 }

  3004.                 pr->pattern.complex.value.data = p;

  3005.                 p = ngx_cpymem(p, conf->url.data, conf->url.len);
  3006.                 *p = '/';

  3007.                 ngx_str_set(&pr->replacement.value, "/");
  3008.             }
  3009.         }
  3010.     }

  3011.     ngx_conf_merge_ptr_value(conf->cookie_domains, prev->cookie_domains, NULL);

  3012.     ngx_conf_merge_ptr_value(conf->cookie_paths, prev->cookie_paths, NULL);

  3013.     ngx_conf_merge_ptr_value(conf->cookie_flags, prev->cookie_flags, NULL);

  3014.     ngx_conf_merge_uint_value(conf->http_version, prev->http_version,
  3015.                               NGX_HTTP_VERSION_11);

  3016.     ngx_conf_merge_uint_value(conf->headers_hash_max_size,
  3017.                               prev->headers_hash_max_size, 512);

  3018.     ngx_conf_merge_uint_value(conf->headers_hash_bucket_size,
  3019.                               prev->headers_hash_bucket_size, 64);

  3020.     conf->headers_hash_bucket_size = ngx_align(conf->headers_hash_bucket_size,
  3021.                                                ngx_cacheline_size);

  3022.     hash.max_size = conf->headers_hash_max_size;
  3023.     hash.bucket_size = conf->headers_hash_bucket_size;
  3024.     hash.name = "proxy_headers_hash";

  3025.     if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
  3026.             &prev->upstream, ngx_http_proxy_hide_headers, &hash)
  3027.         != NGX_OK)
  3028.     {
  3029.         return NGX_CONF_ERROR;
  3030.     }

  3031.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

  3032.     if (clcf->noname
  3033.         && conf->upstream.upstream == NULL && conf->proxy_lengths == NULL)
  3034.     {
  3035.         conf->upstream.upstream = prev->upstream.upstream;
  3036.         conf->location = prev->location;
  3037.         conf->vars = prev->vars;

  3038.         conf->proxy_lengths = prev->proxy_lengths;
  3039.         conf->proxy_values = prev->proxy_values;

  3040. #if (NGX_HTTP_SSL)
  3041.         conf->ssl = prev->ssl;
  3042. #endif
  3043.     }

  3044.     if (clcf->lmt_excpt && clcf->handler == NULL
  3045.         && (conf->upstream.upstream || conf->proxy_lengths))
  3046.     {
  3047.         clcf->handler = ngx_http_proxy_handler;
  3048.     }

  3049.     if (conf->body_source.data == NULL) {
  3050.         conf->body_flushes = prev->body_flushes;
  3051.         conf->body_source = prev->body_source;
  3052.         conf->body_lengths = prev->body_lengths;
  3053.         conf->body_values = prev->body_values;
  3054.     }

  3055.     if (conf->body_source.data && conf->body_lengths == NULL) {

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

  3057.         sc.cf = cf;
  3058.         sc.source = &conf->body_source;
  3059.         sc.flushes = &conf->body_flushes;
  3060.         sc.lengths = &conf->body_lengths;
  3061.         sc.values = &conf->body_values;
  3062.         sc.complete_lengths = 1;
  3063.         sc.complete_values = 1;

  3064.         if (ngx_http_script_compile(&sc) != NGX_OK) {
  3065.             return NGX_CONF_ERROR;
  3066.         }
  3067.     }

  3068.     ngx_conf_merge_ptr_value(conf->headers_source, prev->headers_source, NULL);

  3069.     if (conf->headers_source == prev->headers_source) {
  3070.         conf->headers = prev->headers;
  3071. #if (NGX_HTTP_CACHE)
  3072.         conf->headers_cache = prev->headers_cache;
  3073. #endif
  3074.         conf->host_set = prev->host_set;
  3075.     }

  3076.     rc = ngx_http_proxy_init_headers(cf, conf, &conf->headers,
  3077.                                      ngx_http_proxy_headers);
  3078.     if (rc != NGX_OK) {
  3079.         return NGX_CONF_ERROR;
  3080.     }

  3081. #if (NGX_HTTP_CACHE)

  3082.     if (conf->upstream.cache) {
  3083.         rc = ngx_http_proxy_init_headers(cf, conf, &conf->headers_cache,
  3084.                                          ngx_http_proxy_cache_headers);
  3085.         if (rc != NGX_OK) {
  3086.             return NGX_CONF_ERROR;
  3087.         }
  3088.     }

  3089. #endif

  3090.     /*
  3091.      * special handling to preserve conf->headers in the "http" section
  3092.      * to inherit it to all servers
  3093.      */

  3094.     if (prev->headers.hash.buckets == NULL
  3095.         && conf->headers_source == prev->headers_source)
  3096.     {
  3097.         prev->headers = conf->headers;
  3098. #if (NGX_HTTP_CACHE)
  3099.         prev->headers_cache = conf->headers_cache;
  3100. #endif
  3101.         prev->host_set = conf->host_set;
  3102.     }

  3103.     return NGX_CONF_OK;
  3104. }


  3105. static ngx_int_t
  3106. ngx_http_proxy_init_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
  3107.     ngx_http_proxy_headers_t *headers, ngx_keyval_t *default_headers)
  3108. {
  3109.     u_char                       *p;
  3110.     size_t                        size;
  3111.     uintptr_t                    *code;
  3112.     ngx_uint_t                    i;
  3113.     ngx_array_t                   headers_names, headers_merged;
  3114.     ngx_keyval_t                 *src, *s, *h;
  3115.     ngx_hash_key_t               *hk;
  3116.     ngx_hash_init_t               hash;
  3117.     ngx_http_script_compile_t     sc;
  3118.     ngx_http_script_copy_code_t  *copy;

  3119.     if (headers->hash.buckets) {
  3120.         return NGX_OK;
  3121.     }

  3122.     if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
  3123.         != NGX_OK)
  3124.     {
  3125.         return NGX_ERROR;
  3126.     }

  3127.     if (ngx_array_init(&headers_merged, cf->temp_pool, 4, sizeof(ngx_keyval_t))
  3128.         != NGX_OK)
  3129.     {
  3130.         return NGX_ERROR;
  3131.     }

  3132.     headers->lengths = ngx_array_create(cf->pool, 64, 1);
  3133.     if (headers->lengths == NULL) {
  3134.         return NGX_ERROR;
  3135.     }

  3136.     headers->values = ngx_array_create(cf->pool, 512, 1);
  3137.     if (headers->values == NULL) {
  3138.         return NGX_ERROR;
  3139.     }

  3140.     if (conf->headers_source) {

  3141.         src = conf->headers_source->elts;
  3142.         for (i = 0; i < conf->headers_source->nelts; i++) {

  3143.             if (src[i].key.len == 4
  3144.                 && ngx_strncasecmp(src[i].key.data, (u_char *) "Host", 4) == 0)
  3145.             {
  3146.                 conf->host_set = 1;
  3147.             }

  3148.             s = ngx_array_push(&headers_merged);
  3149.             if (s == NULL) {
  3150.                 return NGX_ERROR;
  3151.             }

  3152.             *s = src[i];
  3153.         }
  3154.     }

  3155.     h = default_headers;

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

  3157.         src = headers_merged.elts;
  3158.         for (i = 0; i < headers_merged.nelts; i++) {
  3159.             if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
  3160.                 goto next;
  3161.             }
  3162.         }

  3163.         s = ngx_array_push(&headers_merged);
  3164.         if (s == NULL) {
  3165.             return NGX_ERROR;
  3166.         }

  3167.         *s = *h;

  3168.     next:

  3169.         h++;
  3170.     }


  3171.     src = headers_merged.elts;
  3172.     for (i = 0; i < headers_merged.nelts; i++) {

  3173.         hk = ngx_array_push(&headers_names);
  3174.         if (hk == NULL) {
  3175.             return NGX_ERROR;
  3176.         }

  3177.         hk->key = src[i].key;
  3178.         hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len);
  3179.         hk->value = (void *) 1;

  3180.         if (src[i].value.len == 0) {
  3181.             continue;
  3182.         }

  3183.         copy = ngx_array_push_n(headers->lengths,
  3184.                                 sizeof(ngx_http_script_copy_code_t));
  3185.         if (copy == NULL) {
  3186.             return NGX_ERROR;
  3187.         }

  3188.         copy->code = (ngx_http_script_code_pt) (void *)
  3189.                                                  ngx_http_script_copy_len_code;
  3190.         copy->len = src[i].key.len;

  3191.         size = (sizeof(ngx_http_script_copy_code_t)
  3192.                 + src[i].key.len + sizeof(uintptr_t) - 1)
  3193.                & ~(sizeof(uintptr_t) - 1);

  3194.         copy = ngx_array_push_n(headers->values, size);
  3195.         if (copy == NULL) {
  3196.             return NGX_ERROR;
  3197.         }

  3198.         copy->code = ngx_http_script_copy_code;
  3199.         copy->len = src[i].key.len;

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

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

  3203.         sc.cf = cf;
  3204.         sc.source = &src[i].value;
  3205.         sc.flushes = &headers->flushes;
  3206.         sc.lengths = &headers->lengths;
  3207.         sc.values = &headers->values;

  3208.         if (ngx_http_script_compile(&sc) != NGX_OK) {
  3209.             return NGX_ERROR;
  3210.         }

  3211.         code = ngx_array_push_n(headers->lengths, sizeof(uintptr_t));
  3212.         if (code == NULL) {
  3213.             return NGX_ERROR;
  3214.         }

  3215.         *code = (uintptr_t) NULL;

  3216.         code = ngx_array_push_n(headers->values, sizeof(uintptr_t));
  3217.         if (code == NULL) {
  3218.             return NGX_ERROR;
  3219.         }

  3220.         *code = (uintptr_t) NULL;
  3221.     }

  3222.     code = ngx_array_push_n(headers->lengths, sizeof(uintptr_t));
  3223.     if (code == NULL) {
  3224.         return NGX_ERROR;
  3225.     }

  3226.     *code = (uintptr_t) NULL;


  3227.     hash.hash = &headers->hash;
  3228.     hash.key = ngx_hash_key_lc;
  3229.     hash.max_size = conf->headers_hash_max_size;
  3230.     hash.bucket_size = conf->headers_hash_bucket_size;
  3231.     hash.name = "proxy_headers_hash";
  3232.     hash.pool = cf->pool;
  3233.     hash.temp_pool = NULL;

  3234.     return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts);
  3235. }


  3236. static char *
  3237. ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3238. {
  3239.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3240.     size_t                      add;
  3241.     u_short                     port;
  3242.     ngx_str_t                  *value, *url;
  3243.     ngx_url_t                   u;
  3244.     ngx_uint_t                  n;
  3245.     ngx_http_core_loc_conf_t   *clcf;
  3246.     ngx_http_script_compile_t   sc;

  3247.     if (plcf->upstream.upstream || plcf->proxy_lengths) {
  3248.         return "is duplicate";
  3249.     }

  3250.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

  3251.     clcf->handler = ngx_http_proxy_handler;

  3252.     if (clcf->name.len && clcf->name.data[clcf->name.len - 1] == '/') {
  3253.         clcf->auto_redirect = 1;
  3254.     }

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

  3256.     url = &value[1];

  3257.     n = ngx_http_script_variables_count(url);

  3258.     if (n) {

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

  3260.         sc.cf = cf;
  3261.         sc.source = url;
  3262.         sc.lengths = &plcf->proxy_lengths;
  3263.         sc.values = &plcf->proxy_values;
  3264.         sc.variables = n;
  3265.         sc.complete_lengths = 1;
  3266.         sc.complete_values = 1;

  3267.         if (ngx_http_script_compile(&sc) != NGX_OK) {
  3268.             return NGX_CONF_ERROR;
  3269.         }

  3270. #if (NGX_HTTP_SSL)
  3271.         plcf->ssl = 1;
  3272. #endif

  3273.         return NGX_CONF_OK;
  3274.     }

  3275.     if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {
  3276.         add = 7;
  3277.         port = 80;

  3278.     } else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) {

  3279. #if (NGX_HTTP_SSL)
  3280.         plcf->ssl = 1;

  3281.         add = 8;
  3282.         port = 443;
  3283. #else
  3284.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3285.                            "https protocol requires SSL support");
  3286.         return NGX_CONF_ERROR;
  3287. #endif

  3288.     } else {
  3289.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
  3290.         return NGX_CONF_ERROR;
  3291.     }

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

  3293.     u.url.len = url->len - add;
  3294.     u.url.data = url->data + add;
  3295.     u.default_port = port;
  3296.     u.uri_part = 1;
  3297.     u.no_resolve = 1;

  3298.     plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
  3299.     if (plcf->upstream.upstream == NULL) {
  3300.         return NGX_CONF_ERROR;
  3301.     }

  3302.     plcf->vars.schema.len = add;
  3303.     plcf->vars.schema.data = url->data;
  3304.     plcf->vars.key_start = plcf->vars.schema;

  3305.     ngx_http_proxy_set_vars(&u, &plcf->vars);

  3306.     plcf->location = clcf->name;

  3307.     if (clcf->named
  3308. #if (NGX_PCRE)
  3309.         || clcf->regex
  3310. #endif
  3311.         || clcf->noname)
  3312.     {
  3313.         if (plcf->vars.uri.len) {
  3314.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3315.                                "\"proxy_pass\" cannot have URI part in "
  3316.                                "location given by regular expression, "
  3317.                                "or inside named location, "
  3318.                                "or inside \"if\" statement, "
  3319.                                "or inside \"limit_except\" block");
  3320.             return NGX_CONF_ERROR;
  3321.         }

  3322.         plcf->location.len = 0;
  3323.     }

  3324.     plcf->url = *url;

  3325.     return NGX_CONF_OK;
  3326. }


  3327. static char *
  3328. ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3329. {
  3330.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3331.     u_char                            *p;
  3332.     ngx_str_t                         *value;
  3333.     ngx_http_proxy_rewrite_t          *pr;
  3334.     ngx_http_compile_complex_value_t   ccv;

  3335.     if (plcf->redirect == 0) {
  3336.         return "is duplicate";
  3337.     }

  3338.     plcf->redirect = 1;

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

  3340.     if (cf->args->nelts == 2) {
  3341.         if (ngx_strcmp(value[1].data, "off") == 0) {

  3342.             if (plcf->redirects) {
  3343.                 return "is duplicate";
  3344.             }

  3345.             plcf->redirect = 0;
  3346.             return NGX_CONF_OK;
  3347.         }

  3348.         if (ngx_strcmp(value[1].data, "default") != 0) {
  3349.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3350.                                "invalid parameter \"%V\"", &value[1]);
  3351.             return NGX_CONF_ERROR;
  3352.         }
  3353.     }

  3354.     if (plcf->redirects == NULL) {
  3355.         plcf->redirects = ngx_array_create(cf->pool, 1,
  3356.                                            sizeof(ngx_http_proxy_rewrite_t));
  3357.         if (plcf->redirects == NULL) {
  3358.             return NGX_CONF_ERROR;
  3359.         }
  3360.     }

  3361.     pr = ngx_array_push(plcf->redirects);
  3362.     if (pr == NULL) {
  3363.         return NGX_CONF_ERROR;
  3364.     }

  3365.     if (cf->args->nelts == 2
  3366.         && ngx_strcmp(value[1].data, "default") == 0)
  3367.     {
  3368.         if (plcf->proxy_lengths) {
  3369.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3370.                                "\"proxy_redirect default\" cannot be used "
  3371.                                "with \"proxy_pass\" directive with variables");
  3372.             return NGX_CONF_ERROR;
  3373.         }

  3374.         if (plcf->url.data == NULL) {
  3375.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3376.                                "\"proxy_redirect default\" should be placed "
  3377.                                "after the \"proxy_pass\" directive");
  3378.             return NGX_CONF_ERROR;
  3379.         }

  3380.         pr->handler = ngx_http_proxy_rewrite_complex_handler;

  3381.         ngx_memzero(&pr->pattern.complex, sizeof(ngx_http_complex_value_t));

  3382.         ngx_memzero(&pr->replacement, sizeof(ngx_http_complex_value_t));

  3383.         if (plcf->vars.uri.len) {
  3384.             pr->pattern.complex.value = plcf->url;
  3385.             pr->replacement.value = plcf->location;

  3386.         } else {
  3387.             pr->pattern.complex.value.len = plcf->url.len + sizeof("/") - 1;

  3388.             p = ngx_pnalloc(cf->pool, pr->pattern.complex.value.len);
  3389.             if (p == NULL) {
  3390.                 return NGX_CONF_ERROR;
  3391.             }

  3392.             pr->pattern.complex.value.data = p;

  3393.             p = ngx_cpymem(p, plcf->url.data, plcf->url.len);
  3394.             *p = '/';

  3395.             ngx_str_set(&pr->replacement.value, "/");
  3396.         }

  3397.         return NGX_CONF_OK;
  3398.     }


  3399.     if (value[1].data[0] == '~') {
  3400.         value[1].len--;
  3401.         value[1].data++;

  3402.         if (value[1].data[0] == '*') {
  3403.             value[1].len--;
  3404.             value[1].data++;

  3405.             if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
  3406.                 return NGX_CONF_ERROR;
  3407.             }

  3408.         } else {
  3409.             if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 0) != NGX_OK) {
  3410.                 return NGX_CONF_ERROR;
  3411.             }
  3412.         }

  3413.     } else {

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

  3415.         ccv.cf = cf;
  3416.         ccv.value = &value[1];
  3417.         ccv.complex_value = &pr->pattern.complex;

  3418.         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3419.             return NGX_CONF_ERROR;
  3420.         }

  3421.         pr->handler = ngx_http_proxy_rewrite_complex_handler;
  3422.     }


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

  3424.     ccv.cf = cf;
  3425.     ccv.value = &value[2];
  3426.     ccv.complex_value = &pr->replacement;

  3427.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3428.         return NGX_CONF_ERROR;
  3429.     }

  3430.     return NGX_CONF_OK;
  3431. }


  3432. static char *
  3433. ngx_http_proxy_cookie_domain(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3434. {
  3435.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3436.     ngx_str_t                         *value;
  3437.     ngx_http_proxy_rewrite_t          *pr;
  3438.     ngx_http_compile_complex_value_t   ccv;

  3439.     if (plcf->cookie_domains == NULL) {
  3440.         return "is duplicate";
  3441.     }

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

  3443.     if (cf->args->nelts == 2) {

  3444.         if (ngx_strcmp(value[1].data, "off") == 0) {

  3445.             if (plcf->cookie_domains != NGX_CONF_UNSET_PTR) {
  3446.                 return "is duplicate";
  3447.             }

  3448.             plcf->cookie_domains = NULL;
  3449.             return NGX_CONF_OK;
  3450.         }

  3451.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3452.                            "invalid parameter \"%V\"", &value[1]);
  3453.         return NGX_CONF_ERROR;
  3454.     }

  3455.     if (plcf->cookie_domains == NGX_CONF_UNSET_PTR) {
  3456.         plcf->cookie_domains = ngx_array_create(cf->pool, 1,
  3457.                                      sizeof(ngx_http_proxy_rewrite_t));
  3458.         if (plcf->cookie_domains == NULL) {
  3459.             return NGX_CONF_ERROR;
  3460.         }
  3461.     }

  3462.     pr = ngx_array_push(plcf->cookie_domains);
  3463.     if (pr == NULL) {
  3464.         return NGX_CONF_ERROR;
  3465.     }

  3466.     if (value[1].data[0] == '~') {
  3467.         value[1].len--;
  3468.         value[1].data++;

  3469.         if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
  3470.             return NGX_CONF_ERROR;
  3471.         }

  3472.     } else {

  3473.         if (value[1].data[0] == '.') {
  3474.             value[1].len--;
  3475.             value[1].data++;
  3476.         }

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

  3478.         ccv.cf = cf;
  3479.         ccv.value = &value[1];
  3480.         ccv.complex_value = &pr->pattern.complex;

  3481.         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3482.             return NGX_CONF_ERROR;
  3483.         }

  3484.         pr->handler = ngx_http_proxy_rewrite_domain_handler;

  3485.         if (value[2].data[0] == '.') {
  3486.             value[2].len--;
  3487.             value[2].data++;
  3488.         }
  3489.     }

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

  3491.     ccv.cf = cf;
  3492.     ccv.value = &value[2];
  3493.     ccv.complex_value = &pr->replacement;

  3494.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3495.         return NGX_CONF_ERROR;
  3496.     }

  3497.     return NGX_CONF_OK;
  3498. }


  3499. static char *
  3500. ngx_http_proxy_cookie_path(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3501. {
  3502.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3503.     ngx_str_t                         *value;
  3504.     ngx_http_proxy_rewrite_t          *pr;
  3505.     ngx_http_compile_complex_value_t   ccv;

  3506.     if (plcf->cookie_paths == NULL) {
  3507.         return "is duplicate";
  3508.     }

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

  3510.     if (cf->args->nelts == 2) {

  3511.         if (ngx_strcmp(value[1].data, "off") == 0) {

  3512.             if (plcf->cookie_paths != NGX_CONF_UNSET_PTR) {
  3513.                 return "is duplicate";
  3514.             }

  3515.             plcf->cookie_paths = NULL;
  3516.             return NGX_CONF_OK;
  3517.         }

  3518.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3519.                            "invalid parameter \"%V\"", &value[1]);
  3520.         return NGX_CONF_ERROR;
  3521.     }

  3522.     if (plcf->cookie_paths == NGX_CONF_UNSET_PTR) {
  3523.         plcf->cookie_paths = ngx_array_create(cf->pool, 1,
  3524.                                      sizeof(ngx_http_proxy_rewrite_t));
  3525.         if (plcf->cookie_paths == NULL) {
  3526.             return NGX_CONF_ERROR;
  3527.         }
  3528.     }

  3529.     pr = ngx_array_push(plcf->cookie_paths);
  3530.     if (pr == NULL) {
  3531.         return NGX_CONF_ERROR;
  3532.     }

  3533.     if (value[1].data[0] == '~') {
  3534.         value[1].len--;
  3535.         value[1].data++;

  3536.         if (value[1].data[0] == '*') {
  3537.             value[1].len--;
  3538.             value[1].data++;

  3539.             if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
  3540.                 return NGX_CONF_ERROR;
  3541.             }

  3542.         } else {
  3543.             if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 0) != NGX_OK) {
  3544.                 return NGX_CONF_ERROR;
  3545.             }
  3546.         }

  3547.     } else {

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

  3549.         ccv.cf = cf;
  3550.         ccv.value = &value[1];
  3551.         ccv.complex_value = &pr->pattern.complex;

  3552.         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3553.             return NGX_CONF_ERROR;
  3554.         }

  3555.         pr->handler = ngx_http_proxy_rewrite_complex_handler;
  3556.     }

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

  3558.     ccv.cf = cf;
  3559.     ccv.value = &value[2];
  3560.     ccv.complex_value = &pr->replacement;

  3561.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3562.         return NGX_CONF_ERROR;
  3563.     }

  3564.     return NGX_CONF_OK;
  3565. }


  3566. static char *
  3567. ngx_http_proxy_cookie_flags(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3568. {
  3569.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3570.     ngx_str_t                         *value;
  3571.     ngx_uint_t                         i;
  3572.     ngx_http_complex_value_t          *cv;
  3573.     ngx_http_proxy_cookie_flags_t     *pcf;
  3574.     ngx_http_compile_complex_value_t   ccv;
  3575. #if (NGX_PCRE)
  3576.     ngx_regex_compile_t                rc;
  3577.     u_char                             errstr[NGX_MAX_CONF_ERRSTR];
  3578. #endif

  3579.     if (plcf->cookie_flags == NULL) {
  3580.         return "is duplicate";
  3581.     }

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

  3583.     if (cf->args->nelts == 2) {

  3584.         if (ngx_strcmp(value[1].data, "off") == 0) {

  3585.             if (plcf->cookie_flags != NGX_CONF_UNSET_PTR) {
  3586.                 return "is duplicate";
  3587.             }

  3588.             plcf->cookie_flags = NULL;
  3589.             return NGX_CONF_OK;
  3590.         }

  3591.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3592.                            "invalid parameter \"%V\"", &value[1]);
  3593.         return NGX_CONF_ERROR;
  3594.     }

  3595.     if (plcf->cookie_flags == NGX_CONF_UNSET_PTR) {
  3596.         plcf->cookie_flags = ngx_array_create(cf->pool, 1,
  3597.                                         sizeof(ngx_http_proxy_cookie_flags_t));
  3598.         if (plcf->cookie_flags == NULL) {
  3599.             return NGX_CONF_ERROR;
  3600.         }
  3601.     }

  3602.     pcf = ngx_array_push(plcf->cookie_flags);
  3603.     if (pcf == NULL) {
  3604.         return NGX_CONF_ERROR;
  3605.     }

  3606.     pcf->regex = 0;

  3607.     if (value[1].data[0] == '~') {
  3608.         value[1].len--;
  3609.         value[1].data++;

  3610. #if (NGX_PCRE)
  3611.         ngx_memzero(&rc, sizeof(ngx_regex_compile_t));

  3612.         rc.pattern = value[1];
  3613.         rc.err.len = NGX_MAX_CONF_ERRSTR;
  3614.         rc.err.data = errstr;
  3615.         rc.options = NGX_REGEX_CASELESS;

  3616.         pcf->cookie.regex = ngx_http_regex_compile(cf, &rc);
  3617.         if (pcf->cookie.regex == NULL) {
  3618.             return NGX_CONF_ERROR;
  3619.         }

  3620.         pcf->regex = 1;
  3621. #else
  3622.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3623.                            "using regex \"%V\" requires PCRE library",
  3624.                            &value[1]);
  3625.         return NGX_CONF_ERROR;
  3626. #endif

  3627.     } else {

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

  3629.         ccv.cf = cf;
  3630.         ccv.value = &value[1];
  3631.         ccv.complex_value = &pcf->cookie.complex;

  3632.         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3633.             return NGX_CONF_ERROR;
  3634.         }
  3635.     }

  3636.     if (ngx_array_init(&pcf->flags_values, cf->pool, cf->args->nelts - 2,
  3637.                        sizeof(ngx_http_complex_value_t))
  3638.         != NGX_OK)
  3639.     {
  3640.         return NGX_CONF_ERROR;
  3641.     }

  3642.     for (i = 2; i < cf->args->nelts; i++) {

  3643.         cv = ngx_array_push(&pcf->flags_values);
  3644.         if (cv == NULL) {
  3645.             return NGX_CONF_ERROR;
  3646.         }

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

  3648.         ccv.cf = cf;
  3649.         ccv.value = &value[i];
  3650.         ccv.complex_value = cv;

  3651.         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3652.             return NGX_CONF_ERROR;
  3653.         }
  3654.     }

  3655.     return NGX_CONF_OK;
  3656. }


  3657. static ngx_int_t
  3658. ngx_http_proxy_rewrite_regex(ngx_conf_t *cf, ngx_http_proxy_rewrite_t *pr,
  3659.     ngx_str_t *regex, ngx_uint_t caseless)
  3660. {
  3661. #if (NGX_PCRE)
  3662.     u_char               errstr[NGX_MAX_CONF_ERRSTR];
  3663.     ngx_regex_compile_t  rc;

  3664.     ngx_memzero(&rc, sizeof(ngx_regex_compile_t));

  3665.     rc.pattern = *regex;
  3666.     rc.err.len = NGX_MAX_CONF_ERRSTR;
  3667.     rc.err.data = errstr;

  3668.     if (caseless) {
  3669.         rc.options = NGX_REGEX_CASELESS;
  3670.     }

  3671.     pr->pattern.regex = ngx_http_regex_compile(cf, &rc);
  3672.     if (pr->pattern.regex == NULL) {
  3673.         return NGX_ERROR;
  3674.     }

  3675.     pr->handler = ngx_http_proxy_rewrite_regex_handler;

  3676.     return NGX_OK;

  3677. #else

  3678.     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3679.                        "using regex \"%V\" requires PCRE library", regex);
  3680.     return NGX_ERROR;

  3681. #endif
  3682. }


  3683. static char *
  3684. ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3685. {
  3686.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3687.     ngx_str_t                  *value;
  3688.     ngx_http_script_compile_t   sc;

  3689.     if (plcf->upstream.store != NGX_CONF_UNSET) {
  3690.         return "is duplicate";
  3691.     }

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

  3693.     if (ngx_strcmp(value[1].data, "off") == 0) {
  3694.         plcf->upstream.store = 0;
  3695.         return NGX_CONF_OK;
  3696.     }

  3697.     if (value[1].len == 0) {
  3698.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "empty path");
  3699.         return NGX_CONF_ERROR;
  3700.     }

  3701. #if (NGX_HTTP_CACHE)
  3702.     if (plcf->upstream.cache > 0) {
  3703.         return "is incompatible with \"proxy_cache\"";
  3704.     }
  3705. #endif

  3706.     plcf->upstream.store = 1;

  3707.     if (ngx_strcmp(value[1].data, "on") == 0) {
  3708.         return NGX_CONF_OK;
  3709.     }

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

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

  3713.     sc.cf = cf;
  3714.     sc.source = &value[1];
  3715.     sc.lengths = &plcf->upstream.store_lengths;
  3716.     sc.values = &plcf->upstream.store_values;
  3717.     sc.variables = ngx_http_script_variables_count(&value[1]);
  3718.     sc.complete_lengths = 1;
  3719.     sc.complete_values = 1;

  3720.     if (ngx_http_script_compile(&sc) != NGX_OK) {
  3721.         return NGX_CONF_ERROR;
  3722.     }

  3723.     return NGX_CONF_OK;
  3724. }


  3725. #if (NGX_HTTP_CACHE)

  3726. static char *
  3727. ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3728. {
  3729.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3730.     ngx_str_t                         *value;
  3731.     ngx_http_complex_value_t           cv;
  3732.     ngx_http_compile_complex_value_t   ccv;

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

  3734.     if (plcf->upstream.cache != NGX_CONF_UNSET) {
  3735.         return "is duplicate";
  3736.     }

  3737.     if (ngx_strcmp(value[1].data, "off") == 0) {
  3738.         plcf->upstream.cache = 0;
  3739.         return NGX_CONF_OK;
  3740.     }

  3741.     if (plcf->upstream.store > 0) {
  3742.         return "is incompatible with \"proxy_store\"";
  3743.     }

  3744.     plcf->upstream.cache = 1;

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

  3746.     ccv.cf = cf;
  3747.     ccv.value = &value[1];
  3748.     ccv.complex_value = &cv;

  3749.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3750.         return NGX_CONF_ERROR;
  3751.     }

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

  3753.         plcf->upstream.cache_value = ngx_palloc(cf->pool,
  3754.                                              sizeof(ngx_http_complex_value_t));
  3755.         if (plcf->upstream.cache_value == NULL) {
  3756.             return NGX_CONF_ERROR;
  3757.         }

  3758.         *plcf->upstream.cache_value = cv;

  3759.         return NGX_CONF_OK;
  3760.     }

  3761.     plcf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0,
  3762.                                                       &ngx_http_proxy_module);
  3763.     if (plcf->upstream.cache_zone == NULL) {
  3764.         return NGX_CONF_ERROR;
  3765.     }

  3766.     return NGX_CONF_OK;
  3767. }


  3768. static char *
  3769. ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3770. {
  3771.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3772.     ngx_str_t                         *value;
  3773.     ngx_http_compile_complex_value_t   ccv;

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

  3775.     if (plcf->cache_key.value.data) {
  3776.         return "is duplicate";
  3777.     }

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

  3779.     ccv.cf = cf;
  3780.     ccv.value = &value[1];
  3781.     ccv.complex_value = &plcf->cache_key;

  3782.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3783.         return NGX_CONF_ERROR;
  3784.     }

  3785.     return NGX_CONF_OK;
  3786. }

  3787. #endif


  3788. #if (NGX_HTTP_SSL)

  3789. static char *
  3790. ngx_http_proxy_ssl_certificate_cache(ngx_conf_t *cf, ngx_command_t *cmd,
  3791.     void *conf)
  3792. {
  3793.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3794.     time_t       inactive, valid;
  3795.     ngx_str_t   *value, s;
  3796.     ngx_int_t    max;
  3797.     ngx_uint_t   i;

  3798.     if (plcf->upstream.ssl_certificate_cache != NGX_CONF_UNSET_PTR) {
  3799.         return "is duplicate";
  3800.     }

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

  3802.     max = 0;
  3803.     inactive = 10;
  3804.     valid = 60;

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

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

  3807.             max = ngx_atoi(value[i].data + 4, value[i].len - 4);
  3808.             if (max <= 0) {
  3809.                 goto failed;
  3810.             }

  3811.             continue;
  3812.         }

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

  3814.             s.len = value[i].len - 9;
  3815.             s.data = value[i].data + 9;

  3816.             inactive = ngx_parse_time(&s, 1);
  3817.             if (inactive == (time_t) NGX_ERROR) {
  3818.                 goto failed;
  3819.             }

  3820.             continue;
  3821.         }

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

  3823.             s.len = value[i].len - 6;
  3824.             s.data = value[i].data + 6;

  3825.             valid = ngx_parse_time(&s, 1);
  3826.             if (valid == (time_t) NGX_ERROR) {
  3827.                 goto failed;
  3828.             }

  3829.             continue;
  3830.         }

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

  3832.             plcf->upstream.ssl_certificate_cache = NULL;

  3833.             continue;
  3834.         }

  3835.     failed:

  3836.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3837.                            "invalid parameter \"%V\"", &value[i]);
  3838.         return NGX_CONF_ERROR;
  3839.     }

  3840.     if (plcf->upstream.ssl_certificate_cache == NULL) {
  3841.         return NGX_CONF_OK;
  3842.     }

  3843.     if (max == 0) {
  3844.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3845.                            "\"proxy_ssl_certificate_cache\" must have "
  3846.                            "the \"max\" parameter");
  3847.         return NGX_CONF_ERROR;
  3848.     }

  3849.     plcf->upstream.ssl_certificate_cache = ngx_ssl_cache_init(cf->pool, max,
  3850.                                                               valid, inactive);
  3851.     if (plcf->upstream.ssl_certificate_cache == NULL) {
  3852.         return NGX_CONF_ERROR;
  3853.     }

  3854.     return NGX_CONF_OK;
  3855. }


  3856. static char *
  3857. ngx_http_proxy_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3858. {
  3859.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3860.     ngx_str_t  *value;

  3861.     if (plcf->upstream.ssl_passwords != NGX_CONF_UNSET_PTR) {
  3862.         return "is duplicate";
  3863.     }

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

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

  3866.     if (plcf->upstream.ssl_passwords == NULL) {
  3867.         return NGX_CONF_ERROR;
  3868.     }

  3869.     return NGX_CONF_OK;
  3870. }

  3871. #endif


  3872. static char *
  3873. ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
  3874. {
  3875. #if (NGX_FREEBSD)
  3876.     ssize_t *np = data;

  3877.     if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
  3878.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3879.                            "\"proxy_send_lowat\" must be less than %d "
  3880.                            "(sysctl net.inet.tcp.sendspace)",
  3881.                            ngx_freebsd_net_inet_tcp_sendspace);

  3882.         return NGX_CONF_ERROR;
  3883.     }

  3884. #elif !(NGX_HAVE_SO_SNDLOWAT)
  3885.     ssize_t *np = data;

  3886.     ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
  3887.                        "\"proxy_send_lowat\" is not supported, ignored");

  3888.     *np = 0;

  3889. #endif

  3890.     return NGX_CONF_OK;
  3891. }


  3892. #if (NGX_HTTP_SSL)

  3893. static char *
  3894. ngx_http_proxy_ssl_conf_command_check(ngx_conf_t *cf, void *post, void *data)
  3895. {
  3896. #ifndef SSL_CONF_FLAG_FILE
  3897.     return "is not supported on this platform";
  3898. #else
  3899.     return NGX_CONF_OK;
  3900. #endif
  3901. }


  3902. static ngx_int_t
  3903. ngx_http_proxy_merge_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
  3904.     ngx_http_proxy_loc_conf_t *prev)
  3905. {
  3906.     ngx_uint_t  preserve;

  3907.     if (conf->ssl_protocols == 0
  3908.         && conf->ssl_ciphers.data == NULL
  3909.         && conf->upstream.ssl_certificate == NGX_CONF_UNSET_PTR
  3910.         && conf->upstream.ssl_certificate_key == NGX_CONF_UNSET_PTR
  3911.         && conf->upstream.ssl_passwords == NGX_CONF_UNSET_PTR
  3912.         && conf->upstream.ssl_verify == NGX_CONF_UNSET
  3913.         && conf->ssl_verify_depth == NGX_CONF_UNSET_UINT
  3914.         && conf->ssl_trusted_certificate.data == NULL
  3915.         && conf->ssl_crl.data == NULL
  3916.         && conf->upstream.ssl_session_reuse == NGX_CONF_UNSET
  3917.         && conf->ssl_conf_commands == NGX_CONF_UNSET_PTR)
  3918.     {
  3919.         if (prev->upstream.ssl) {
  3920.             conf->upstream.ssl = prev->upstream.ssl;
  3921.             return NGX_OK;
  3922.         }

  3923.         preserve = 1;

  3924.     } else {
  3925.         preserve = 0;
  3926.     }

  3927.     conf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
  3928.     if (conf->upstream.ssl == NULL) {
  3929.         return NGX_ERROR;
  3930.     }

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

  3932.     /*
  3933.      * special handling to preserve conf->upstream.ssl
  3934.      * in the "http" section to inherit it to all servers
  3935.      */

  3936.     if (preserve) {
  3937.         prev->upstream.ssl = conf->upstream.ssl;
  3938.     }

  3939.     return NGX_OK;
  3940. }


  3941. static ngx_int_t
  3942. ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf)
  3943. {
  3944.     ngx_pool_cleanup_t  *cln;

  3945.     if (plcf->upstream.ssl->ctx) {
  3946.         return NGX_OK;
  3947.     }

  3948.     if (ngx_ssl_create(plcf->upstream.ssl, plcf->ssl_protocols, NULL)
  3949.         != NGX_OK)
  3950.     {
  3951.         return NGX_ERROR;
  3952.     }

  3953.     cln = ngx_pool_cleanup_add(cf->pool, 0);
  3954.     if (cln == NULL) {
  3955.         ngx_ssl_cleanup_ctx(plcf->upstream.ssl);
  3956.         return NGX_ERROR;
  3957.     }

  3958.     cln->handler = ngx_ssl_cleanup_ctx;
  3959.     cln->data = plcf->upstream.ssl;

  3960.     if (ngx_ssl_ciphers(cf, plcf->upstream.ssl, &plcf->ssl_ciphers, 0)
  3961.         != NGX_OK)
  3962.     {
  3963.         return NGX_ERROR;
  3964.     }

  3965.     if (plcf->upstream.ssl_certificate
  3966.         && plcf->upstream.ssl_certificate->value.len)
  3967.     {
  3968.         if (plcf->upstream.ssl_certificate_key == NULL) {
  3969.             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  3970.                           "no \"proxy_ssl_certificate_key\" is defined "
  3971.                           "for certificate \"%V\"",
  3972.                           &plcf->upstream.ssl_certificate->value);
  3973.             return NGX_ERROR;
  3974.         }

  3975.         if (plcf->upstream.ssl_certificate->lengths == NULL
  3976.             && plcf->upstream.ssl_certificate_key->lengths == NULL)
  3977.         {
  3978.             if (ngx_ssl_certificate(cf, plcf->upstream.ssl,
  3979.                                     &plcf->upstream.ssl_certificate->value,
  3980.                                     &plcf->upstream.ssl_certificate_key->value,
  3981.                                     plcf->upstream.ssl_passwords)
  3982.                 != NGX_OK)
  3983.             {
  3984.                 return NGX_ERROR;
  3985.             }
  3986.         }
  3987.     }

  3988.     if (plcf->upstream.ssl_verify) {
  3989.         if (plcf->ssl_trusted_certificate.len == 0) {
  3990.             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  3991.                       "no proxy_ssl_trusted_certificate for proxy_ssl_verify");
  3992.             return NGX_ERROR;
  3993.         }

  3994.         if (ngx_ssl_trusted_certificate(cf, plcf->upstream.ssl,
  3995.                                         &plcf->ssl_trusted_certificate,
  3996.                                         plcf->ssl_verify_depth)
  3997.             != NGX_OK)
  3998.         {
  3999.             return NGX_ERROR;
  4000.         }

  4001.         if (ngx_ssl_crl(cf, plcf->upstream.ssl, &plcf->ssl_crl) != NGX_OK) {
  4002.             return NGX_ERROR;
  4003.         }
  4004.     }

  4005.     if (ngx_ssl_client_session_cache(cf, plcf->upstream.ssl,
  4006.                                      plcf->upstream.ssl_session_reuse)
  4007.         != NGX_OK)
  4008.     {
  4009.         return NGX_ERROR;
  4010.     }

  4011.     if (ngx_ssl_conf_commands(cf, plcf->upstream.ssl, plcf->ssl_conf_commands)
  4012.         != NGX_OK)
  4013.     {
  4014.         return NGX_ERROR;
  4015.     }

  4016.     return NGX_OK;
  4017. }

  4018. #endif


  4019. static void
  4020. ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v)
  4021. {
  4022.     if (u->family != AF_UNIX) {

  4023.         if (u->no_port || u->port == u->default_port) {

  4024.             v->host_header = u->host;

  4025.             if (u->default_port == 80) {
  4026.                 ngx_str_set(&v->port, "80");

  4027.             } else {
  4028.                 ngx_str_set(&v->port, "443");
  4029.             }

  4030.         } else {
  4031.             v->host_header.len = u->host.len + 1 + u->port_text.len;
  4032.             v->host_header.data = u->host.data;
  4033.             v->port = u->port_text;
  4034.         }

  4035.         v->key_start.len += v->host_header.len;

  4036.     } else {
  4037.         ngx_str_set(&v->host_header, "localhost");
  4038.         ngx_str_null(&v->port);
  4039.         v->key_start.len += sizeof("unix:") - 1 + u->host.len + 1;
  4040.     }

  4041.     v->uri = u->uri;
  4042. }