src/http/modules/ngx_http_proxy_module.c - nginx-1.31.4 nginx/ @ 8d9666701

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_socket_rcvbuf"),
  245.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  246.       ngx_conf_set_size_slot,
  247.       NGX_HTTP_LOC_CONF_OFFSET,
  248.       offsetof(ngx_http_proxy_loc_conf_t, upstream.socket_rcvbuf),
  249.       NULL },

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

  256.     { ngx_string("proxy_connect_timeout"),
  257.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  258.       ngx_conf_set_msec_slot,
  259.       NGX_HTTP_LOC_CONF_OFFSET,
  260.       offsetof(ngx_http_proxy_loc_conf_t, upstream.connect_timeout),
  261.       NULL },

  262.     { ngx_string("proxy_send_timeout"),
  263.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  264.       ngx_conf_set_msec_slot,
  265.       NGX_HTTP_LOC_CONF_OFFSET,
  266.       offsetof(ngx_http_proxy_loc_conf_t, upstream.send_timeout),
  267.       NULL },

  268.     { ngx_string("proxy_send_lowat"),
  269.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  270.       ngx_conf_set_size_slot,
  271.       NGX_HTTP_LOC_CONF_OFFSET,
  272.       offsetof(ngx_http_proxy_loc_conf_t, upstream.send_lowat),
  273.       &ngx_http_proxy_lowat_post },

  274.     { ngx_string("proxy_intercept_errors"),
  275.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  276.       ngx_conf_set_flag_slot,
  277.       NGX_HTTP_LOC_CONF_OFFSET,
  278.       offsetof(ngx_http_proxy_loc_conf_t, upstream.intercept_errors),
  279.       NULL },

  280.     { ngx_string("proxy_set_header"),
  281.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
  282.       ngx_conf_set_keyval_slot,
  283.       NGX_HTTP_LOC_CONF_OFFSET,
  284.       offsetof(ngx_http_proxy_loc_conf_t, headers_source),
  285.       NULL },

  286.     { ngx_string("proxy_headers_hash_max_size"),
  287.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  288.       ngx_conf_set_num_slot,
  289.       NGX_HTTP_LOC_CONF_OFFSET,
  290.       offsetof(ngx_http_proxy_loc_conf_t, headers_hash_max_size),
  291.       NULL },

  292.     { ngx_string("proxy_headers_hash_bucket_size"),
  293.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  294.       ngx_conf_set_num_slot,
  295.       NGX_HTTP_LOC_CONF_OFFSET,
  296.       offsetof(ngx_http_proxy_loc_conf_t, headers_hash_bucket_size),
  297.       NULL },

  298.     { ngx_string("proxy_set_body"),
  299.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  300.       ngx_conf_set_str_slot,
  301.       NGX_HTTP_LOC_CONF_OFFSET,
  302.       offsetof(ngx_http_proxy_loc_conf_t, body_source),
  303.       NULL },

  304.     { ngx_string("proxy_method"),
  305.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  306.       ngx_http_set_complex_value_slot,
  307.       NGX_HTTP_LOC_CONF_OFFSET,
  308.       offsetof(ngx_http_proxy_loc_conf_t, method),
  309.       NULL },

  310.     { ngx_string("proxy_pass_request_headers"),
  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_request_headers),
  315.       NULL },

  316.     { ngx_string("proxy_pass_request_body"),
  317.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  318.       ngx_conf_set_flag_slot,
  319.       NGX_HTTP_LOC_CONF_OFFSET,
  320.       offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_body),
  321.       NULL },

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

  328.     { ngx_string("proxy_buffer_size"),
  329.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  330.       ngx_conf_set_size_slot,
  331.       NGX_HTTP_LOC_CONF_OFFSET,
  332.       offsetof(ngx_http_proxy_loc_conf_t, upstream.buffer_size),
  333.       NULL },

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

  340.     { ngx_string("proxy_buffers"),
  341.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
  342.       ngx_conf_set_bufs_slot,
  343.       NGX_HTTP_LOC_CONF_OFFSET,
  344.       offsetof(ngx_http_proxy_loc_conf_t, upstream.bufs),
  345.       NULL },

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

  352.     { ngx_string("proxy_force_ranges"),
  353.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  354.       ngx_conf_set_flag_slot,
  355.       NGX_HTTP_LOC_CONF_OFFSET,
  356.       offsetof(ngx_http_proxy_loc_conf_t, upstream.force_ranges),
  357.       NULL },

  358.     { ngx_string("proxy_limit_rate"),
  359.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  360.       ngx_http_set_complex_value_size_slot,
  361.       NGX_HTTP_LOC_CONF_OFFSET,
  362.       offsetof(ngx_http_proxy_loc_conf_t, upstream.limit_rate),
  363.       NULL },

  364. #if (NGX_HTTP_CACHE)

  365.     { ngx_string("proxy_cache"),
  366.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  367.       ngx_http_proxy_cache,
  368.       NGX_HTTP_LOC_CONF_OFFSET,
  369.       0,
  370.       NULL },

  371.     { ngx_string("proxy_cache_key"),
  372.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  373.       ngx_http_proxy_cache_key,
  374.       NGX_HTTP_LOC_CONF_OFFSET,
  375.       0,
  376.       NULL },

  377.     { ngx_string("proxy_cache_path"),
  378.       NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
  379.       ngx_http_file_cache_set_slot,
  380.       NGX_HTTP_MAIN_CONF_OFFSET,
  381.       offsetof(ngx_http_proxy_main_conf_t, caches),
  382.       &ngx_http_proxy_module },

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

  389.     { ngx_string("proxy_no_cache"),
  390.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  391.       ngx_http_set_predicate_slot,
  392.       NGX_HTTP_LOC_CONF_OFFSET,
  393.       offsetof(ngx_http_proxy_loc_conf_t, upstream.no_cache),
  394.       NULL },

  395.     { ngx_string("proxy_cache_valid"),
  396.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  397.       ngx_http_file_cache_valid_set_slot,
  398.       NGX_HTTP_LOC_CONF_OFFSET,
  399.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_valid),
  400.       NULL },

  401.     { ngx_string("proxy_cache_min_uses"),
  402.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  403.       ngx_conf_set_num_slot,
  404.       NGX_HTTP_LOC_CONF_OFFSET,
  405.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_min_uses),
  406.       NULL },

  407.     { ngx_string("proxy_cache_max_range_offset"),
  408.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  409.       ngx_conf_set_off_slot,
  410.       NGX_HTTP_LOC_CONF_OFFSET,
  411.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_max_range_offset),
  412.       NULL },

  413.     { ngx_string("proxy_cache_use_stale"),
  414.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  415.       ngx_conf_set_bitmask_slot,
  416.       NGX_HTTP_LOC_CONF_OFFSET,
  417.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_use_stale),
  418.       &ngx_http_proxy_next_upstream_masks },

  419.     { ngx_string("proxy_cache_methods"),
  420.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  421.       ngx_conf_set_bitmask_slot,
  422.       NGX_HTTP_LOC_CONF_OFFSET,
  423.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_methods),
  424.       &ngx_http_upstream_cache_method_mask },

  425.     { ngx_string("proxy_cache_lock"),
  426.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  427.       ngx_conf_set_flag_slot,
  428.       NGX_HTTP_LOC_CONF_OFFSET,
  429.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock),
  430.       NULL },

  431.     { ngx_string("proxy_cache_lock_timeout"),
  432.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  433.       ngx_conf_set_msec_slot,
  434.       NGX_HTTP_LOC_CONF_OFFSET,
  435.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock_timeout),
  436.       NULL },

  437.     { ngx_string("proxy_cache_lock_age"),
  438.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  439.       ngx_conf_set_msec_slot,
  440.       NGX_HTTP_LOC_CONF_OFFSET,
  441.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock_age),
  442.       NULL },

  443.     { ngx_string("proxy_cache_revalidate"),
  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_revalidate),
  448.       NULL },

  449.     { ngx_string("proxy_cache_convert_head"),
  450.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  451.       ngx_conf_set_flag_slot,
  452.       NGX_HTTP_LOC_CONF_OFFSET,
  453.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_convert_head),
  454.       NULL },

  455.     { ngx_string("proxy_cache_background_update"),
  456.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  457.       ngx_conf_set_flag_slot,
  458.       NGX_HTTP_LOC_CONF_OFFSET,
  459.       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_background_update),
  460.       NULL },

  461. #endif

  462.     { ngx_string("proxy_temp_path"),
  463.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
  464.       ngx_conf_set_path_slot,
  465.       NGX_HTTP_LOC_CONF_OFFSET,
  466.       offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_path),
  467.       NULL },

  468.     { ngx_string("proxy_max_temp_file_size"),
  469.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  470.       ngx_conf_set_size_slot,
  471.       NGX_HTTP_LOC_CONF_OFFSET,
  472.       offsetof(ngx_http_proxy_loc_conf_t, upstream.max_temp_file_size_conf),
  473.       NULL },

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

  480.     { ngx_string("proxy_next_upstream"),
  481.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  482.       ngx_conf_set_bitmask_slot,
  483.       NGX_HTTP_LOC_CONF_OFFSET,
  484.       offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream),
  485.       &ngx_http_proxy_next_upstream_masks },

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

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

  498.     { ngx_string("proxy_pass_header"),
  499.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  500.       ngx_conf_set_str_array_slot,
  501.       NGX_HTTP_LOC_CONF_OFFSET,
  502.       offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_headers),
  503.       NULL },

  504.     { ngx_string("proxy_hide_header"),
  505.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  506.       ngx_conf_set_str_array_slot,
  507.       NGX_HTTP_LOC_CONF_OFFSET,
  508.       offsetof(ngx_http_proxy_loc_conf_t, upstream.hide_headers),
  509.       NULL },

  510.     { ngx_string("proxy_ignore_headers"),
  511.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  512.       ngx_conf_set_bitmask_slot,
  513.       NGX_HTTP_LOC_CONF_OFFSET,
  514.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_headers),
  515.       &ngx_http_upstream_ignore_headers_masks },

  516.     { ngx_string("proxy_http_version"),
  517.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  518.       ngx_conf_set_enum_slot,
  519.       NGX_HTTP_LOC_CONF_OFFSET,
  520.       offsetof(ngx_http_proxy_loc_conf_t, http_version),
  521.       &ngx_http_proxy_http_version },

  522. #if (NGX_HTTP_SSL)

  523.     { ngx_string("proxy_ssl_session_reuse"),
  524.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  525.       ngx_conf_set_flag_slot,
  526.       NGX_HTTP_LOC_CONF_OFFSET,
  527.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_session_reuse),
  528.       NULL },

  529.     { ngx_string("proxy_ssl_protocols"),
  530.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
  531.       ngx_conf_set_bitmask_slot,
  532.       NGX_HTTP_LOC_CONF_OFFSET,
  533.       offsetof(ngx_http_proxy_loc_conf_t, ssl_protocols),
  534.       &ngx_http_proxy_ssl_protocols },

  535.     { ngx_string("proxy_ssl_ciphers"),
  536.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  537.       ngx_conf_set_str_slot,
  538.       NGX_HTTP_LOC_CONF_OFFSET,
  539.       offsetof(ngx_http_proxy_loc_conf_t, ssl_ciphers),
  540.       NULL },

  541.     { ngx_string("proxy_ssl_name"),
  542.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  543.       ngx_http_set_complex_value_slot,
  544.       NGX_HTTP_LOC_CONF_OFFSET,
  545.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_name),
  546.       NULL },

  547.     { ngx_string("proxy_ssl_server_name"),
  548.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  549.       ngx_conf_set_flag_slot,
  550.       NGX_HTTP_LOC_CONF_OFFSET,
  551.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_server_name),
  552.       NULL },

  553.     { ngx_string("proxy_ssl_verify"),
  554.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  555.       ngx_conf_set_flag_slot,
  556.       NGX_HTTP_LOC_CONF_OFFSET,
  557.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_verify),
  558.       NULL },

  559.     { ngx_string("proxy_ssl_verify_depth"),
  560.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  561.       ngx_conf_set_num_slot,
  562.       NGX_HTTP_LOC_CONF_OFFSET,
  563.       offsetof(ngx_http_proxy_loc_conf_t, ssl_verify_depth),
  564.       NULL },

  565.     { ngx_string("proxy_ssl_trusted_certificate"),
  566.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  567.       ngx_conf_set_str_slot,
  568.       NGX_HTTP_LOC_CONF_OFFSET,
  569.       offsetof(ngx_http_proxy_loc_conf_t, ssl_trusted_certificate),
  570.       NULL },

  571.     { ngx_string("proxy_ssl_crl"),
  572.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  573.       ngx_conf_set_str_slot,
  574.       NGX_HTTP_LOC_CONF_OFFSET,
  575.       offsetof(ngx_http_proxy_loc_conf_t, ssl_crl),
  576.       NULL },

  577.     { ngx_string("proxy_ssl_certificate"),
  578.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  579.       ngx_http_set_complex_value_zero_slot,
  580.       NGX_HTTP_LOC_CONF_OFFSET,
  581.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_certificate),
  582.       NULL },

  583.     { ngx_string("proxy_ssl_certificate_key"),
  584.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  585.       ngx_http_set_complex_value_zero_slot,
  586.       NGX_HTTP_LOC_CONF_OFFSET,
  587.       offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_certificate_key),
  588.       NULL },

  589.     { ngx_string("proxy_ssl_certificate_cache"),
  590.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
  591.       ngx_http_proxy_ssl_certificate_cache,
  592.       NGX_HTTP_LOC_CONF_OFFSET,
  593.       0,
  594.       NULL },

  595.     { ngx_string("proxy_ssl_password_file"),
  596.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  597.       ngx_http_proxy_ssl_password_file,
  598.       NGX_HTTP_LOC_CONF_OFFSET,
  599.       0,
  600.       NULL },

  601.     { ngx_string("proxy_ssl_conf_command"),
  602.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
  603.       ngx_conf_set_keyval_slot,
  604.       NGX_HTTP_LOC_CONF_OFFSET,
  605.       offsetof(ngx_http_proxy_loc_conf_t, ssl_conf_commands),
  606.       &ngx_http_proxy_ssl_conf_command_post },

  607. #endif

  608.       ngx_null_command
  609. };


  610. static ngx_http_module_t  ngx_http_proxy_module_ctx = {
  611.     ngx_http_proxy_add_variables,          /* preconfiguration */
  612.     NULL,                                  /* postconfiguration */

  613.     ngx_http_proxy_create_main_conf,       /* create main configuration */
  614.     NULL,                                  /* init main configuration */

  615.     NULL,                                  /* create server configuration */
  616.     NULL,                                  /* merge server configuration */

  617.     ngx_http_proxy_create_loc_conf,        /* create location configuration */
  618.     ngx_http_proxy_merge_loc_conf          /* merge location configuration */
  619. };


  620. ngx_module_t  ngx_http_proxy_module = {
  621.     NGX_MODULE_V1,
  622.     &ngx_http_proxy_module_ctx,            /* module context */
  623.     ngx_http_proxy_commands,               /* module directives */
  624.     NGX_HTTP_MODULE,                       /* module type */
  625.     NULL,                                  /* init master */
  626.     NULL,                                  /* init module */
  627.     NULL,                                  /* init process */
  628.     NULL,                                  /* init thread */
  629.     NULL,                                  /* exit thread */
  630.     NULL,                                  /* exit process */
  631.     NULL,                                  /* exit master */
  632.     NGX_MODULE_V1_PADDING
  633. };


  634. static char  ngx_http_proxy_version[] = " HTTP/1.0" CRLF;
  635. static char  ngx_http_proxy_version_11[] = " HTTP/1.1" CRLF;


  636. static ngx_keyval_t  ngx_http_proxy_headers[] = {
  637.     { ngx_string("Host"), ngx_string("") },
  638.     { ngx_string("Connection"), ngx_string("") },
  639.     { ngx_string("Proxy-Connection"), ngx_string("") },
  640.     { ngx_string("Content-Length"), ngx_string("$proxy_internal_body_length") },
  641.     { ngx_string("Transfer-Encoding"), ngx_string("$proxy_internal_chunked") },
  642.     { ngx_string("TE"), ngx_string("") },
  643.     { ngx_string("Keep-Alive"), ngx_string("") },
  644.     { ngx_string("Expect"), ngx_string("") },
  645.     { ngx_string("Upgrade"), ngx_string("") },
  646.     { ngx_null_string, ngx_null_string }
  647. };


  648. static ngx_str_t  ngx_http_proxy_hide_headers[] = {
  649.     ngx_string("Date"),
  650.     ngx_string("Server"),
  651.     ngx_string("X-Pad"),
  652.     ngx_string("X-Accel-Expires"),
  653.     ngx_string("X-Accel-Redirect"),
  654.     ngx_string("X-Accel-Limit-Rate"),
  655.     ngx_string("X-Accel-Buffering"),
  656.     ngx_string("X-Accel-Charset"),
  657.     ngx_null_string
  658. };


  659. #if (NGX_HTTP_CACHE)

  660. static ngx_keyval_t  ngx_http_proxy_cache_headers[] = {
  661.     { ngx_string("Host"), ngx_string("") },
  662.     { ngx_string("Connection"), ngx_string("") },
  663.     { ngx_string("Proxy-Connection"), ngx_string("") },
  664.     { ngx_string("Content-Length"), ngx_string("$proxy_internal_body_length") },
  665.     { ngx_string("Transfer-Encoding"), ngx_string("$proxy_internal_chunked") },
  666.     { ngx_string("TE"), ngx_string("") },
  667.     { ngx_string("Keep-Alive"), ngx_string("") },
  668.     { ngx_string("Expect"), ngx_string("") },
  669.     { ngx_string("Upgrade"), ngx_string("") },
  670.     { ngx_string("If-Modified-Since"),
  671.       ngx_string("$upstream_cache_last_modified") },
  672.     { ngx_string("If-Unmodified-Since"), ngx_string("") },
  673.     { ngx_string("If-None-Match"), ngx_string("$upstream_cache_etag") },
  674.     { ngx_string("If-Match"), ngx_string("") },
  675.     { ngx_string("Range"), ngx_string("") },
  676.     { ngx_string("If-Range"), ngx_string("") },
  677.     { ngx_null_string, ngx_null_string }
  678. };

  679. #endif


  680. static ngx_http_variable_t  ngx_http_proxy_vars[] = {

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

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

  685.     { ngx_string("proxy_add_x_forwarded_for"), NULL,
  686.       ngx_http_proxy_add_x_forwarded_for_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },

  687. #if 0
  688.     { ngx_string("proxy_add_via"), NULL, NULL, 0, NGX_HTTP_VAR_NOHASH, 0 },
  689. #endif

  690.     { ngx_string("proxy_internal_body_length"), NULL,
  691.       ngx_http_proxy_internal_body_length_variable, 0,
  692.       NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },

  693.     { ngx_string("proxy_internal_chunked"), NULL,
  694.       ngx_http_proxy_internal_chunked_variable, 0,
  695.       NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },

  696.       ngx_http_null_variable
  697. };


  698. static ngx_path_init_t  ngx_http_proxy_temp_path = {
  699.     ngx_string(NGX_HTTP_PROXY_TEMP_PATH), { 1, 2, 0 }
  700. };


  701. static ngx_conf_bitmask_t  ngx_http_proxy_cookie_flags_masks[] = {

  702.     { ngx_string("secure"),
  703.       NGX_HTTP_PROXY_COOKIE_SECURE|NGX_HTTP_PROXY_COOKIE_SECURE_ON },

  704.     { ngx_string("nosecure"),
  705.       NGX_HTTP_PROXY_COOKIE_SECURE|NGX_HTTP_PROXY_COOKIE_SECURE_OFF },

  706.     { ngx_string("httponly"),
  707.       NGX_HTTP_PROXY_COOKIE_HTTPONLY|NGX_HTTP_PROXY_COOKIE_HTTPONLY_ON },

  708.     { ngx_string("nohttponly"),
  709.       NGX_HTTP_PROXY_COOKIE_HTTPONLY|NGX_HTTP_PROXY_COOKIE_HTTPONLY_OFF },

  710.     { ngx_string("samesite=strict"),
  711.       NGX_HTTP_PROXY_COOKIE_SAMESITE|NGX_HTTP_PROXY_COOKIE_SAMESITE_STRICT },

  712.     { ngx_string("samesite=lax"),
  713.       NGX_HTTP_PROXY_COOKIE_SAMESITE|NGX_HTTP_PROXY_COOKIE_SAMESITE_LAX },

  714.     { ngx_string("samesite=none"),
  715.       NGX_HTTP_PROXY_COOKIE_SAMESITE|NGX_HTTP_PROXY_COOKIE_SAMESITE_NONE },

  716.     { ngx_string("nosamesite"),
  717.       NGX_HTTP_PROXY_COOKIE_SAMESITE|NGX_HTTP_PROXY_COOKIE_SAMESITE_OFF },

  718.     { ngx_null_string, 0 }
  719. };


  720. static ngx_int_t
  721. ngx_http_proxy_handler(ngx_http_request_t *r)
  722. {
  723.     ngx_int_t                    rc;
  724.     ngx_http_upstream_t         *u;
  725.     ngx_http_proxy_ctx_t        *ctx;
  726.     ngx_http_proxy_loc_conf_t   *plcf;
  727. #if (NGX_HTTP_CACHE)
  728.     ngx_http_proxy_main_conf_t  *pmcf;
  729. #endif

  730.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  731. #if (NGX_HTTP_V2)
  732.     if (plcf->http_version == NGX_HTTP_VERSION_20) {
  733.         return ngx_http_proxy_v2_handler(r);
  734.     }
  735. #endif

  736.     if (ngx_http_upstream_create(r) != NGX_OK) {
  737.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  738.     }

  739.     ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t));
  740.     if (ctx == NULL) {
  741.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  742.     }

  743.     ngx_http_set_ctx(r, ctx, ngx_http_proxy_module);

  744.     u = r->upstream;

  745.     if (plcf->proxy_lengths == NULL) {
  746.         ctx->vars = plcf->vars;
  747.         u->schema = plcf->vars.schema;
  748. #if (NGX_HTTP_SSL)
  749.         u->ssl = plcf->ssl;
  750. #endif

  751.     } else {
  752.         if (ngx_http_proxy_eval(r, ctx, plcf) != NGX_OK) {
  753.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  754.         }
  755.     }

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

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

  758. #if (NGX_HTTP_CACHE)
  759.     pmcf = ngx_http_get_module_main_conf(r, ngx_http_proxy_module);

  760.     u->caches = &pmcf->caches;
  761.     u->create_key = ngx_http_proxy_create_key;
  762. #endif

  763.     u->create_request = ngx_http_proxy_create_request;
  764.     u->reinit_request = ngx_http_proxy_reinit_request;
  765.     u->process_header = ngx_http_proxy_process_status_line;
  766.     u->abort_request = ngx_http_proxy_abort_request;
  767.     u->finalize_request = ngx_http_proxy_finalize_request;
  768.     r->state = 0;

  769.     if (plcf->redirects) {
  770.         u->rewrite_redirect = ngx_http_proxy_rewrite_redirect;
  771.     }

  772.     if (plcf->cookie_domains || plcf->cookie_paths || plcf->cookie_flags) {
  773.         u->rewrite_cookie = ngx_http_proxy_rewrite_cookie;
  774.     }

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

  776.     u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
  777.     if (u->pipe == NULL) {
  778.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  779.     }

  780.     u->pipe->input_filter = ngx_http_proxy_copy_filter;
  781.     u->pipe->input_ctx = r;

  782.     u->input_filter_init = ngx_http_proxy_input_filter_init;
  783.     u->input_filter = ngx_http_proxy_non_buffered_copy_filter;
  784.     u->input_filter_ctx = r;

  785.     u->accel = 1;

  786.     if (!plcf->upstream.request_buffering
  787.         && plcf->body_values == NULL && plcf->upstream.pass_request_body
  788.         && (!r->headers_in.chunked
  789.             || plcf->http_version == NGX_HTTP_VERSION_11))
  790.     {
  791.         r->request_body_no_buffering = 1;
  792.     }

  793.     rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);

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

  797.     return NGX_DONE;
  798. }


  799. ngx_int_t
  800. ngx_http_proxy_eval(ngx_http_request_t *r, ngx_http_proxy_ctx_t *ctx,
  801.     ngx_http_proxy_loc_conf_t *plcf)
  802. {
  803.     u_char               *p;
  804.     size_t                add;
  805.     u_short               port;
  806.     ngx_str_t             proxy;
  807.     ngx_url_t             url;
  808.     ngx_http_upstream_t  *u;

  809.     if (ngx_http_script_run(r, &proxy, plcf->proxy_lengths->elts, 0,
  810.                             plcf->proxy_values->elts)
  811.         == NULL)
  812.     {
  813.         return NGX_ERROR;
  814.     }

  815.     if (proxy.len > 7
  816.         && ngx_strncasecmp(proxy.data, (u_char *) "http://", 7) == 0)
  817.     {
  818.         add = 7;
  819.         port = 80;

  820. #if (NGX_HTTP_SSL)

  821.     } else if (proxy.len > 8
  822.                && ngx_strncasecmp(proxy.data, (u_char *) "https://", 8) == 0)
  823.     {
  824.         add = 8;
  825.         port = 443;
  826.         r->upstream->ssl = 1;

  827. #endif

  828.     } else {
  829.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  830.                       "invalid URL prefix in \"%V\"", &proxy);
  831.         return NGX_ERROR;
  832.     }

  833.     u = r->upstream;

  834.     u->schema.len = add;
  835.     u->schema.data = proxy.data;

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

  837.     url.url.len = proxy.len - add;
  838.     url.url.data = proxy.data + add;
  839.     url.default_port = port;
  840.     url.uri_part = 1;
  841.     url.no_resolve = 1;

  842.     if (ngx_parse_url(r->pool, &url) != NGX_OK) {
  843.         if (url.err) {
  844.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  845.                           "%s in upstream \"%V\"", url.err, &url.url);
  846.         }

  847.         return NGX_ERROR;
  848.     }

  849.     if (url.uri.len) {
  850.         if (url.uri.data[0] == '?') {
  851.             p = ngx_pnalloc(r->pool, url.uri.len + 1);
  852.             if (p == NULL) {
  853.                 return NGX_ERROR;
  854.             }

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

  857.             url.uri.len++;
  858.             url.uri.data = p - 1;
  859.         }
  860.     }

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

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

  863.     u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
  864.     if (u->resolved == NULL) {
  865.         return NGX_ERROR;
  866.     }

  867.     if (url.addrs) {
  868.         u->resolved->sockaddr = url.addrs[0].sockaddr;
  869.         u->resolved->socklen = url.addrs[0].socklen;
  870.         u->resolved->name = url.addrs[0].name;
  871.         u->resolved->naddrs = 1;
  872.     }

  873.     u->resolved->host = url.host;
  874.     u->resolved->port = (in_port_t) (url.no_port ? port : url.port);
  875.     u->resolved->no_port = url.no_port;

  876.     return NGX_OK;
  877. }


  878. #if (NGX_HTTP_CACHE)

  879. ngx_int_t
  880. ngx_http_proxy_create_key(ngx_http_request_t *r)
  881. {
  882.     size_t                      len, loc_len;
  883.     u_char                     *p;
  884.     uintptr_t                   escape;
  885.     ngx_str_t                  *key;
  886.     ngx_http_upstream_t        *u;
  887.     ngx_http_proxy_ctx_t       *ctx;
  888.     ngx_http_proxy_loc_conf_t  *plcf;

  889.     u = r->upstream;

  890.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  891.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  892.     key = ngx_array_push(&r->cache->keys);
  893.     if (key == NULL) {
  894.         return NGX_ERROR;
  895.     }

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

  897.         if (ngx_http_complex_value(r, &plcf->cache_key, key) != NGX_OK) {
  898.             return NGX_ERROR;
  899.         }

  900.         return NGX_OK;
  901.     }

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

  903.     key = ngx_array_push(&r->cache->keys);
  904.     if (key == NULL) {
  905.         return NGX_ERROR;
  906.     }

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

  908.         *key = ctx->vars.uri;
  909.         u->uri = ctx->vars.uri;

  910.         return NGX_OK;

  911.     } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri) {
  912.         *key = r->unparsed_uri;
  913.         u->uri = r->unparsed_uri;

  914.         return NGX_OK;
  915.     }

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

  918.     if (r->quoted_uri || r->internal) {
  919.         escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
  920.                                     r->uri.len - loc_len, NGX_ESCAPE_URI);
  921.     } else {
  922.         escape = 0;
  923.     }

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

  926.     p = ngx_pnalloc(r->pool, len);
  927.     if (p == NULL) {
  928.         return NGX_ERROR;
  929.     }

  930.     key->data = p;

  931.     if (r->valid_location) {
  932.         p = ngx_copy(p, ctx->vars.uri.data, ctx->vars.uri.len);
  933.     }

  934.     if (escape) {
  935.         ngx_escape_uri(p, r->uri.data + loc_len,
  936.                        r->uri.len - loc_len, NGX_ESCAPE_URI);
  937.         p += r->uri.len - loc_len + escape;

  938.     } else {
  939.         p = ngx_copy(p, r->uri.data + loc_len, r->uri.len - loc_len);
  940.     }

  941.     if (r->args.len > 0) {
  942.         *p++ = '?';
  943.         p = ngx_copy(p, r->args.data, r->args.len);
  944.     }

  945.     key->len = p - key->data;
  946.     u->uri = *key;

  947.     return NGX_OK;
  948. }

  949. #endif


  950. static ngx_int_t
  951. ngx_http_proxy_create_request(ngx_http_request_t *r)
  952. {
  953.     size_t                        len, uri_len, loc_len, body_len, headers_len,
  954.                                   key_len, val_len;
  955.     uintptr_t                     escape;
  956.     ngx_buf_t                    *b;
  957.     ngx_str_t                     method, host;
  958.     ngx_uint_t                    i, unparsed_uri;
  959.     ngx_chain_t                  *cl, *body;
  960.     ngx_list_part_t              *part;
  961.     ngx_table_elt_t              *header;
  962.     ngx_http_upstream_t          *u;
  963.     ngx_http_proxy_ctx_t         *ctx;
  964.     ngx_http_script_code_pt       code;
  965.     ngx_http_proxy_headers_t     *headers;
  966.     ngx_http_script_engine_t      e, le;
  967.     ngx_http_proxy_loc_conf_t    *plcf;
  968.     ngx_http_script_len_code_pt   lcode;

  969.     u = r->upstream;

  970.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  971. #if (NGX_HTTP_CACHE)
  972.     headers = u->cacheable ? &plcf->headers_cache : &plcf->headers;
  973. #else
  974.     headers = &plcf->headers;
  975. #endif

  976.     if (u->method.len) {
  977.         /* HEAD was changed to GET to cache response */
  978.         method = u->method;

  979.     } else if (plcf->method) {
  980.         if (ngx_http_complex_value(r, plcf->method, &method) != NGX_OK) {
  981.             return NGX_ERROR;
  982.         }

  983.     } else {
  984.         method = r->method_name;
  985.     }

  986.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  987.     host.len = 0;
  988. #if (NGX_SUPPRESS_WARN)
  989.     host.data = NULL;
  990. #endif

  991.     if (plcf->host_value
  992.         && ngx_http_complex_value(r, plcf->host_value, &host) != NGX_OK)
  993.     {
  994.         return NGX_ERROR;
  995.     }

  996.     if (plcf->host_value == NULL
  997.         || (host.len == 0 && plcf->http_version == NGX_HTTP_VERSION_11))
  998.     {
  999.         host = ctx->vars.host_header;
  1000.     }

  1001.     if (method.len == 4
  1002.         && ngx_strncasecmp(method.data, (u_char *) "HEAD", 4) == 0)
  1003.     {
  1004.         ctx->head = 1;
  1005.     }

  1006.     len = method.len + 1 + sizeof(ngx_http_proxy_version) - 1
  1007.           + sizeof(CRLF) - 1;

  1008.     escape = 0;
  1009.     loc_len = 0;
  1010.     unparsed_uri = 0;
  1011.     body_len = 0;
  1012.     headers_len = 0;

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

  1015.     } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri) {
  1016.         unparsed_uri = 1;
  1017.         uri_len = r->unparsed_uri.len;

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

  1021.         if (r->quoted_uri || r->internal) {
  1022.             escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
  1023.                                         r->uri.len - loc_len, NGX_ESCAPE_URI);
  1024.         }

  1025.         uri_len = ctx->vars.uri.len + r->uri.len - loc_len + escape
  1026.                   + sizeof("?") - 1 + r->args.len;
  1027.     }

  1028.     if (uri_len == 0) {
  1029.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1030.                       "zero length URI to proxy");
  1031.         return NGX_ERROR;
  1032.     }

  1033.     len += uri_len;

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

  1035.     ngx_http_script_flush_no_cacheable_variables(r, plcf->body_flushes);
  1036.     ngx_http_script_flush_no_cacheable_variables(r, headers->flushes);

  1037.     if (plcf->body_lengths) {
  1038.         le.ip = plcf->body_lengths->elts;
  1039.         le.request = r;
  1040.         le.flushed = 1;

  1041.         while (*(uintptr_t *) le.ip) {
  1042.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  1043.             body_len += lcode(&le);
  1044.         }

  1045.         ctx->internal_body_length = body_len;
  1046.         len += body_len;

  1047.     } else if (r->headers_in.chunked && r->reading_body) {
  1048.         ctx->internal_body_length = -1;
  1049.         ctx->internal_chunked = 1;

  1050.     } else {
  1051.         ctx->internal_body_length = r->headers_in.content_length_n;
  1052.     }

  1053.     if (host.len) {
  1054.         len += sizeof("Host: ") - 1 + host.len + sizeof(CRLF) - 1;
  1055.     }

  1056.     le.ip = headers->lengths->elts;
  1057.     le.request = r;
  1058.     le.flushed = 1;

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

  1060.         lcode = *(ngx_http_script_len_code_pt *) le.ip;
  1061.         key_len = lcode(&le);

  1062.         for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
  1063.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  1064.         }
  1065.         le.ip += sizeof(uintptr_t);

  1066.         if (val_len == 0) {
  1067.             continue;
  1068.         }

  1069.         headers_len += key_len + sizeof(": ") - 1 + val_len + sizeof(CRLF) - 1;
  1070.     }

  1071.     len += headers_len;


  1072.     if (plcf->upstream.pass_request_headers) {
  1073.         part = &r->headers_in.headers.part;
  1074.         header = part->elts;

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

  1076.             if (i >= part->nelts) {
  1077.                 if (part->next == NULL) {
  1078.                     break;
  1079.                 }

  1080.                 part = part->next;
  1081.                 header = part->elts;
  1082.                 i = 0;
  1083.             }

  1084.             if (ngx_hash_find(&headers->hash, header[i].hash,
  1085.                               header[i].lowcase_key, header[i].key.len))
  1086.             {
  1087.                 continue;
  1088.             }

  1089.             len += header[i].key.len + sizeof(": ") - 1
  1090.                 + header[i].value.len + sizeof(CRLF) - 1;
  1091.         }
  1092.     }


  1093.     b = ngx_create_temp_buf(r->pool, len);
  1094.     if (b == NULL) {
  1095.         return NGX_ERROR;
  1096.     }

  1097.     cl = ngx_alloc_chain_link(r->pool);
  1098.     if (cl == NULL) {
  1099.         return NGX_ERROR;
  1100.     }

  1101.     cl->buf = b;


  1102.     /* the request line */

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

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

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

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

  1110.     } else {
  1111.         if (r->valid_location) {
  1112.             b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);
  1113.         }

  1114.         if (escape) {
  1115.             ngx_escape_uri(b->last, r->uri.data + loc_len,
  1116.                            r->uri.len - loc_len, NGX_ESCAPE_URI);
  1117.             b->last += r->uri.len - loc_len + escape;

  1118.         } else {
  1119.             b->last = ngx_copy(b->last, r->uri.data + loc_len,
  1120.                                r->uri.len - loc_len);
  1121.         }

  1122.         if (r->args.len > 0) {
  1123.             *b->last++ = '?';
  1124.             b->last = ngx_copy(b->last, r->args.data, r->args.len);
  1125.         }
  1126.     }

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

  1128.     if (plcf->http_version == NGX_HTTP_VERSION_11) {
  1129.         b->last = ngx_cpymem(b->last, ngx_http_proxy_version_11,
  1130.                              sizeof(ngx_http_proxy_version_11) - 1);

  1131.     } else {
  1132.         b->last = ngx_cpymem(b->last, ngx_http_proxy_version,
  1133.                              sizeof(ngx_http_proxy_version) - 1);
  1134.     }

  1135.     if (host.len) {
  1136.         b->last = ngx_cpymem(b->last, "Host: ", sizeof("Host: ") - 1);
  1137.         b->last = ngx_cpymem(b->last, host.data, host.len);
  1138.         *b->last++ = CR; *b->last++ = LF;
  1139.     }

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

  1141.     e.ip = headers->values->elts;
  1142.     e.pos = b->last;
  1143.     e.end = b->last + headers_len;
  1144.     e.request = r;
  1145.     e.flushed = 1;

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

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

  1148.         lcode = *(ngx_http_script_len_code_pt *) le.ip;
  1149.         (void) lcode(&le);

  1150.         for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
  1151.             lcode = *(ngx_http_script_len_code_pt *) le.ip;
  1152.         }
  1153.         le.ip += sizeof(uintptr_t);

  1154.         if (val_len == 0) {
  1155.             e.skip = 1;

  1156.             while (*(uintptr_t *) e.ip) {
  1157.                 code = *(ngx_http_script_code_pt *) e.ip;
  1158.                 code((ngx_http_script_engine_t *) &e);
  1159.             }
  1160.             e.ip += sizeof(uintptr_t);

  1161.             e.skip = 0;

  1162.             continue;
  1163.         }

  1164.         code = *(ngx_http_script_code_pt *) e.ip;
  1165.         code((ngx_http_script_engine_t *) &e);

  1166.         if (e.status) {
  1167.             return NGX_ERROR;
  1168.         }

  1169.         if (ngx_http_script_check_length(&e, 2) != NGX_OK) {
  1170.             return NGX_ERROR;
  1171.         }

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

  1173.         while (*(uintptr_t *) e.ip) {
  1174.             code = *(ngx_http_script_code_pt *) e.ip;
  1175.             code((ngx_http_script_engine_t *) &e);
  1176.         }
  1177.         e.ip += sizeof(uintptr_t);

  1178.         if (e.status) {
  1179.             return NGX_ERROR;
  1180.         }

  1181.         if (ngx_http_script_check_length(&e, 2) != NGX_OK) {
  1182.             return NGX_ERROR;
  1183.         }

  1184.         *e.pos++ = CR; *e.pos++ = LF;
  1185.     }

  1186.     b->last = e.pos;


  1187.     if (plcf->upstream.pass_request_headers) {
  1188.         part = &r->headers_in.headers.part;
  1189.         header = part->elts;

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

  1191.             if (i >= part->nelts) {
  1192.                 if (part->next == NULL) {
  1193.                     break;
  1194.                 }

  1195.                 part = part->next;
  1196.                 header = part->elts;
  1197.                 i = 0;
  1198.             }

  1199.             if (ngx_hash_find(&headers->hash, header[i].hash,
  1200.                               header[i].lowcase_key, header[i].key.len))
  1201.             {
  1202.                 continue;
  1203.             }

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

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

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

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

  1209.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1210.                            "http proxy header: \"%V: %V\"",
  1211.                            &header[i].key, &header[i].value);
  1212.         }
  1213.     }


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

  1216.     if (plcf->body_values) {
  1217.         e.ip = plcf->body_values->elts;
  1218.         e.pos = b->last;
  1219.         e.end = b->last + body_len;
  1220.         e.skip = 0;

  1221.         while (*(uintptr_t *) e.ip) {
  1222.             code = *(ngx_http_script_code_pt *) e.ip;
  1223.             code((ngx_http_script_engine_t *) &e);
  1224.         }

  1225.         if (e.status) {
  1226.             return NGX_ERROR;
  1227.         }

  1228.         b->last = e.pos;
  1229.     }

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

  1233.     if (r->request_body_no_buffering) {

  1234.         u->request_bufs = cl;

  1235.         if (ctx->internal_chunked) {
  1236.             u->output.output_filter = ngx_http_proxy_body_output_filter;
  1237.             u->output.filter_ctx = r;
  1238.         }

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

  1240.         body = u->request_bufs;
  1241.         u->request_bufs = cl;

  1242.         while (body) {
  1243.             b = ngx_alloc_buf(r->pool);
  1244.             if (b == NULL) {
  1245.                 return NGX_ERROR;
  1246.             }

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

  1248.             cl->next = ngx_alloc_chain_link(r->pool);
  1249.             if (cl->next == NULL) {
  1250.                 return NGX_ERROR;
  1251.             }

  1252.             cl = cl->next;
  1253.             cl->buf = b;

  1254.             body = body->next;
  1255.         }

  1256.     } else {
  1257.         u->request_bufs = cl;
  1258.     }

  1259.     b->flush = 1;
  1260.     cl->next = NULL;

  1261.     return NGX_OK;
  1262. }


  1263. static ngx_int_t
  1264. ngx_http_proxy_reinit_request(ngx_http_request_t *r)
  1265. {
  1266.     ngx_http_proxy_ctx_t  *ctx;

  1267.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1268.     if (ctx == NULL) {
  1269.         return NGX_OK;
  1270.     }

  1271.     ctx->status.code = 0;
  1272.     ctx->status.count = 0;
  1273.     ctx->status.start = NULL;
  1274.     ctx->status.end = NULL;
  1275.     ctx->chunked.state = 0;

  1276.     r->upstream->process_header = ngx_http_proxy_process_status_line;
  1277.     r->upstream->pipe->input_filter = ngx_http_proxy_copy_filter;
  1278.     r->upstream->input_filter = ngx_http_proxy_non_buffered_copy_filter;
  1279.     r->state = 0;

  1280.     return NGX_OK;
  1281. }


  1282. static ngx_int_t
  1283. ngx_http_proxy_body_output_filter(void *data, ngx_chain_t *in)
  1284. {
  1285.     ngx_http_request_t  *r = data;

  1286.     off_t                  size;
  1287.     u_char                *chunk;
  1288.     ngx_int_t              rc;
  1289.     ngx_buf_t             *b;
  1290.     ngx_chain_t           *out, *cl, *tl, **ll, **fl;
  1291.     ngx_http_proxy_ctx_t  *ctx;

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

  1294.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1295.     if (in == NULL) {
  1296.         out = in;
  1297.         goto out;
  1298.     }

  1299.     out = NULL;
  1300.     ll = &out;

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

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

  1305.         ctx->header_sent = 1;

  1306.         tl = ngx_alloc_chain_link(r->pool);
  1307.         if (tl == NULL) {
  1308.             return NGX_ERROR;
  1309.         }

  1310.         tl->buf = in->buf;
  1311.         *ll = tl;
  1312.         ll = &tl->next;

  1313.         in = in->next;

  1314.         if (in == NULL) {
  1315.             tl->next = NULL;
  1316.             goto out;
  1317.         }
  1318.     }

  1319.     size = 0;
  1320.     cl = in;
  1321.     fl = ll;

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

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

  1326.         if (cl->buf->flush
  1327.             || cl->buf->sync
  1328.             || ngx_buf_in_memory(cl->buf)
  1329.             || cl->buf->in_file)
  1330.         {
  1331.             tl = ngx_alloc_chain_link(r->pool);
  1332.             if (tl == NULL) {
  1333.                 return NGX_ERROR;
  1334.             }

  1335.             tl->buf = cl->buf;
  1336.             *ll = tl;
  1337.             ll = &tl->next;
  1338.         }

  1339.         if (cl->next == NULL) {
  1340.             break;
  1341.         }

  1342.         cl = cl->next;
  1343.     }

  1344.     if (size) {
  1345.         tl = ngx_chain_get_free_buf(r->pool, &ctx->free);
  1346.         if (tl == NULL) {
  1347.             return NGX_ERROR;
  1348.         }

  1349.         b = tl->buf;
  1350.         chunk = b->start;

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

  1353.             chunk = ngx_palloc(r->pool, sizeof("0000000000000000" CRLF) - 1);
  1354.             if (chunk == NULL) {
  1355.                 return NGX_ERROR;
  1356.             }

  1357.             b->start = chunk;
  1358.             b->end = chunk + sizeof("0000000000000000" CRLF) - 1;
  1359.         }

  1360.         b->tag = (ngx_buf_tag_t) &ngx_http_proxy_body_output_filter;
  1361.         b->memory = 0;
  1362.         b->temporary = 1;
  1363.         b->pos = chunk;
  1364.         b->last = ngx_sprintf(chunk, "%xO" CRLF, size);

  1365.         tl->next = *fl;
  1366.         *fl = tl;
  1367.     }

  1368.     if (cl->buf->last_buf) {
  1369.         tl = ngx_chain_get_free_buf(r->pool, &ctx->free);
  1370.         if (tl == NULL) {
  1371.             return NGX_ERROR;
  1372.         }

  1373.         b = tl->buf;

  1374.         b->tag = (ngx_buf_tag_t) &ngx_http_proxy_body_output_filter;
  1375.         b->temporary = 0;
  1376.         b->memory = 1;
  1377.         b->last_buf = 1;
  1378.         b->pos = (u_char *) CRLF "0" CRLF CRLF;
  1379.         b->last = b->pos + 7;

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

  1381.         *ll = tl;

  1382.         if (size == 0) {
  1383.             b->pos += 2;
  1384.         }

  1385.     } else if (size > 0) {
  1386.         tl = ngx_chain_get_free_buf(r->pool, &ctx->free);
  1387.         if (tl == NULL) {
  1388.             return NGX_ERROR;
  1389.         }

  1390.         b = tl->buf;

  1391.         b->tag = (ngx_buf_tag_t) &ngx_http_proxy_body_output_filter;
  1392.         b->temporary = 0;
  1393.         b->memory = 1;
  1394.         b->pos = (u_char *) CRLF;
  1395.         b->last = b->pos + 2;

  1396.         *ll = tl;

  1397.     } else {
  1398.         *ll = NULL;
  1399.     }

  1400. out:

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

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

  1404.     return rc;
  1405. }


  1406. static ngx_int_t
  1407. ngx_http_proxy_process_status_line(ngx_http_request_t *r)
  1408. {
  1409.     size_t                 len;
  1410.     ngx_int_t              rc;
  1411.     ngx_http_upstream_t   *u;
  1412.     ngx_http_proxy_ctx_t  *ctx;

  1413.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1414.     if (ctx == NULL) {
  1415.         return NGX_ERROR;
  1416.     }

  1417.     u = r->upstream;

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

  1419.     if (rc == NGX_AGAIN) {
  1420.         return rc;
  1421.     }

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

  1424. #if (NGX_HTTP_CACHE)

  1425.         if (r->cache) {
  1426.             r->http_version = NGX_HTTP_VERSION_9;
  1427.             return NGX_OK;
  1428.         }

  1429. #endif

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

  1432. #if 0
  1433.         if (u->accel) {
  1434.             return NGX_HTTP_UPSTREAM_INVALID_HEADER;
  1435.         }
  1436. #endif

  1437.         r->http_version = NGX_HTTP_VERSION_9;
  1438.         u->state->status = NGX_HTTP_OK;
  1439.         u->headers_in.connection_close = 1;

  1440.         return NGX_OK;
  1441.     }

  1442.     if (u->state && u->state->status == 0) {
  1443.         u->state->status = ctx->status.code;
  1444.     }

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

  1446.     len = ctx->status.end - ctx->status.start;
  1447.     u->headers_in.status_line.len = len;

  1448.     u->headers_in.status_line.data = ngx_pnalloc(r->pool, len);
  1449.     if (u->headers_in.status_line.data == NULL) {
  1450.         return NGX_ERROR;
  1451.     }

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

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

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

  1457.         if (ctx->status.code == NGX_HTTP_EARLY_HINTS) {
  1458.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1459.                           "upstream sent HTTP/1.0 response with early hints");
  1460.             return NGX_HTTP_UPSTREAM_INVALID_HEADER;
  1461.         }

  1462.         u->headers_in.connection_close = 1;
  1463.     }

  1464.     u->process_header = ngx_http_proxy_process_header;

  1465.     return ngx_http_proxy_process_header(r);
  1466. }


  1467. static ngx_int_t
  1468. ngx_http_proxy_process_header(ngx_http_request_t *r)
  1469. {
  1470.     ngx_int_t                       rc;
  1471.     ngx_table_elt_t                *h;
  1472.     ngx_http_upstream_t            *u;
  1473.     ngx_http_proxy_ctx_t           *ctx;
  1474.     ngx_http_upstream_header_t     *hh;
  1475.     ngx_http_upstream_main_conf_t  *umcf;

  1476.     umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);

  1477.     for ( ;; ) {

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

  1479.         if (rc == NGX_OK) {

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

  1481.             h = ngx_list_push(&r->upstream->headers_in.headers);
  1482.             if (h == NULL) {
  1483.                 return NGX_ERROR;
  1484.             }

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

  1486.             h->key.len = r->header_name_end - r->header_name_start;
  1487.             h->value.len = r->header_end - r->header_start;

  1488.             h->key.data = ngx_pnalloc(r->pool,
  1489.                                h->key.len + 1 + h->value.len + 1 + h->key.len);
  1490.             if (h->key.data == NULL) {
  1491.                 h->hash = 0;
  1492.                 return NGX_ERROR;
  1493.             }

  1494.             h->value.data = h->key.data + h->key.len + 1;
  1495.             h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;

  1496.             ngx_memcpy(h->key.data, r->header_name_start, h->key.len);
  1497.             h->key.data[h->key.len] = '\0';
  1498.             ngx_memcpy(h->value.data, r->header_start, h->value.len);
  1499.             h->value.data[h->value.len] = '\0';

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

  1502.             } else {
  1503.                 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
  1504.             }

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

  1508.             if (r->upstream->headers_in.status_n == NGX_HTTP_EARLY_HINTS) {
  1509.                 continue;
  1510.             }

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

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

  1515.                 if (rc != NGX_OK) {
  1516.                     return rc;
  1517.                 }
  1518.             }

  1519.             continue;
  1520.         }

  1521.         if (rc == NGX_HTTP_PARSE_HEADER_DONE) {

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

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

  1525.             ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1526.             if (r->upstream->headers_in.status_n == NGX_HTTP_EARLY_HINTS) {
  1527.                 ctx->status.code = 0;
  1528.                 ctx->status.count = 0;
  1529.                 ctx->status.start = NULL;
  1530.                 ctx->status.end = NULL;

  1531.                 r->upstream->process_header =
  1532.                                             ngx_http_proxy_process_status_line;
  1533.                 r->state = 0;
  1534.                 return NGX_HTTP_UPSTREAM_EARLY_HINTS;
  1535.             }

  1536.             /*
  1537.              * if no "Server" and "Date" in header line,
  1538.              * then add the special empty headers
  1539.              */

  1540.             if (r->upstream->headers_in.server == NULL) {
  1541.                 h = ngx_list_push(&r->upstream->headers_in.headers);
  1542.                 if (h == NULL) {
  1543.                     return NGX_ERROR;
  1544.                 }

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

  1547.                 ngx_str_set(&h->key, "Server");
  1548.                 ngx_str_null(&h->value);
  1549.                 h->lowcase_key = (u_char *) "server";
  1550.                 h->next = NULL;
  1551.             }

  1552.             if (r->upstream->headers_in.date == NULL) {
  1553.                 h = ngx_list_push(&r->upstream->headers_in.headers);
  1554.                 if (h == NULL) {
  1555.                     return NGX_ERROR;
  1556.                 }

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

  1558.                 ngx_str_set(&h->key, "Date");
  1559.                 ngx_str_null(&h->value);
  1560.                 h->lowcase_key = (u_char *) "date";
  1561.                 h->next = NULL;
  1562.             }

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

  1564.             u = r->upstream;

  1565.             if (u->headers_in.chunked) {
  1566.                 u->headers_in.content_length_n = -1;
  1567.             }

  1568.             /*
  1569.              * set u->keepalive if response has no body; this allows to keep
  1570.              * connections alive in case of r->header_only or X-Accel-Redirect
  1571.              */

  1572.             if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
  1573.                 || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED
  1574.                 || ctx->head
  1575.                 || (!u->headers_in.chunked
  1576.                     && u->headers_in.content_length_n == 0))
  1577.             {
  1578.                 u->keepalive = !u->headers_in.connection_close;
  1579.             }

  1580.             if (u->headers_in.status_n == NGX_HTTP_SWITCHING_PROTOCOLS) {
  1581.                 u->keepalive = 0;

  1582.                 if (r->headers_in.upgrade) {
  1583.                     u->upgrade = 1;
  1584.                 }
  1585.             }

  1586.             return NGX_OK;
  1587.         }

  1588.         if (rc == NGX_AGAIN) {
  1589.             return NGX_AGAIN;
  1590.         }

  1591.         /* rc == NGX_HTTP_PARSE_INVALID_HEADER */

  1592.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1593.                       "upstream sent invalid header: \"%*s\\x%02xd...\"",
  1594.                       r->header_end - r->header_name_start,
  1595.                       r->header_name_start, *r->header_end);

  1596.         return NGX_HTTP_UPSTREAM_INVALID_HEADER;
  1597.     }
  1598. }


  1599. static ngx_int_t
  1600. ngx_http_proxy_input_filter_init(void *data)
  1601. {
  1602.     ngx_http_request_t    *r = data;
  1603.     ngx_http_upstream_t   *u;
  1604.     ngx_http_proxy_ctx_t  *ctx;

  1605.     u = r->upstream;
  1606.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1607.     if (ctx == NULL) {
  1608.         return NGX_ERROR;
  1609.     }

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

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

  1615.     if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
  1616.         || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED
  1617.         || ctx->head)
  1618.     {
  1619.         /* 1xx, 204, and 304 and replies to HEAD requests */
  1620.         /* no 1xx since we don't send Expect and Upgrade */

  1621.         u->pipe->length = 0;
  1622.         u->length = 0;
  1623.         u->keepalive = !u->headers_in.connection_close;

  1624.     } else if (u->headers_in.chunked) {
  1625.         /* chunked */

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

  1628.         u->input_filter = ngx_http_proxy_non_buffered_chunked_filter;
  1629.         u->length = 1;

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

  1632.         u->pipe->length = 0;
  1633.         u->length = 0;
  1634.         u->keepalive = !u->headers_in.connection_close;

  1635.     } else {
  1636.         /* content length or connection close */

  1637.         u->pipe->length = u->headers_in.content_length_n;
  1638.         u->length = u->headers_in.content_length_n;
  1639.     }

  1640.     return NGX_OK;
  1641. }


  1642. static ngx_int_t
  1643. ngx_http_proxy_copy_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
  1644. {
  1645.     ngx_buf_t           *b;
  1646.     ngx_chain_t         *cl;
  1647.     ngx_http_request_t  *r;

  1648.     if (buf->pos == buf->last) {
  1649.         return NGX_OK;
  1650.     }

  1651.     if (p->upstream_done) {
  1652.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, p->log, 0,
  1653.                        "http proxy data after close");
  1654.         return NGX_OK;
  1655.     }

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

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

  1660.         r = p->input_ctx;
  1661.         r->upstream->keepalive = 0;
  1662.         p->upstream_done = 1;

  1663.         return NGX_OK;
  1664.     }

  1665.     cl = ngx_chain_get_free_buf(p->pool, &p->free);
  1666.     if (cl == NULL) {
  1667.         return NGX_ERROR;
  1668.     }

  1669.     b = cl->buf;

  1670.     ngx_memcpy(b, buf, sizeof(ngx_buf_t));
  1671.     b->shadow = buf;
  1672.     b->tag = p->tag;
  1673.     b->last_shadow = 1;
  1674.     b->recycled = 1;
  1675.     buf->shadow = b;

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

  1677.     if (p->in) {
  1678.         *p->last_in = cl;
  1679.     } else {
  1680.         p->in = cl;
  1681.     }
  1682.     p->last_in = &cl->next;

  1683.     if (p->length == -1) {
  1684.         return NGX_OK;
  1685.     }

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

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

  1690.         b->last = b->pos + p->length;
  1691.         p->upstream_done = 1;

  1692.         return NGX_OK;
  1693.     }

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

  1695.     if (p->length == 0) {
  1696.         r = p->input_ctx;
  1697.         r->upstream->keepalive = !r->upstream->headers_in.connection_close;
  1698.     }

  1699.     return NGX_OK;
  1700. }


  1701. static ngx_int_t
  1702. ngx_http_proxy_chunked_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
  1703. {
  1704.     ngx_int_t                   rc;
  1705.     ngx_buf_t                  *b, **prev;
  1706.     ngx_chain_t                *cl;
  1707.     ngx_http_request_t         *r;
  1708.     ngx_http_proxy_ctx_t       *ctx;
  1709.     ngx_http_proxy_loc_conf_t  *plcf;

  1710.     if (buf->pos == buf->last) {
  1711.         return NGX_OK;
  1712.     }

  1713.     r = p->input_ctx;
  1714.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1715.     if (ctx == NULL) {
  1716.         return NGX_ERROR;
  1717.     }

  1718.     if (p->upstream_done) {
  1719.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, p->log, 0,
  1720.                        "http proxy data after close");
  1721.         return NGX_OK;
  1722.     }

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

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

  1726.         r->upstream->keepalive = 0;
  1727.         p->upstream_done = 1;

  1728.         return NGX_OK;
  1729.     }

  1730.     b = NULL;

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

  1733.         if (rc == NGX_ERROR) {
  1734.             return NGX_ERROR;
  1735.         }

  1736.         if (rc == NGX_OK) {

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

  1738.             p->length = 0;
  1739.             r->upstream->keepalive = !r->upstream->headers_in.connection_close;

  1740.             if (buf->pos != buf->last) {
  1741.                 ngx_log_error(NGX_LOG_WARN, p->log, 0,
  1742.                               "upstream sent data after trailers");
  1743.                 r->upstream->keepalive = 0;
  1744.             }
  1745.         }

  1746.         goto free_buf;
  1747.     }

  1748.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  1749.     prev = &buf->shadow;

  1750.     for ( ;; ) {

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

  1753.         if (rc == NGX_OK) {

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

  1755.             cl = ngx_chain_get_free_buf(p->pool, &p->free);
  1756.             if (cl == NULL) {
  1757.                 return NGX_ERROR;
  1758.             }

  1759.             b = cl->buf;

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

  1761.             b->pos = buf->pos;
  1762.             b->start = buf->start;
  1763.             b->end = buf->end;
  1764.             b->tag = p->tag;
  1765.             b->temporary = 1;
  1766.             b->recycled = 1;

  1767.             *prev = b;
  1768.             prev = &b->shadow;

  1769.             if (p->in) {
  1770.                 *p->last_in = cl;
  1771.             } else {
  1772.                 p->in = cl;
  1773.             }
  1774.             p->last_in = &cl->next;

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

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

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

  1779.                 buf->pos += (size_t) ctx->chunked.size;
  1780.                 b->last = buf->pos;
  1781.                 ctx->chunked.size = 0;

  1782.                 continue;
  1783.             }

  1784.             ctx->chunked.size -= buf->last - buf->pos;
  1785.             buf->pos = buf->last;
  1786.             b->last = buf->last;

  1787.             continue;
  1788.         }

  1789.         if (rc == NGX_DONE) {

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

  1792.                 if (rc == NGX_ERROR) {
  1793.                     return NGX_ERROR;
  1794.                 }

  1795.                 if (rc == NGX_AGAIN) {
  1796.                     p->length = 1;
  1797.                     break;
  1798.                 }
  1799.             }

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

  1801.             p->length = 0;
  1802.             r->upstream->keepalive = !r->upstream->headers_in.connection_close;

  1803.             if (buf->pos != buf->last) {
  1804.                 ngx_log_error(NGX_LOG_WARN, p->log, 0,
  1805.                               "upstream sent data after final chunk");
  1806.                 r->upstream->keepalive = 0;
  1807.             }

  1808.             break;
  1809.         }

  1810.         if (rc == NGX_AGAIN) {

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

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

  1813.             break;
  1814.         }

  1815.         /* invalid response */

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

  1818.         return NGX_ERROR;
  1819.     }

  1820. free_buf:

  1821.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, p->log, 0,
  1822.                    "http proxy chunked state %ui, length %O",
  1823.                    ctx->chunked.state, p->length);

  1824.     if (b) {
  1825.         b->shadow = buf;
  1826.         b->last_shadow = 1;

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

  1829.         return NGX_OK;
  1830.     }

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

  1832.     if (ngx_event_pipe_add_free_buf(p, buf) != NGX_OK) {
  1833.         return NGX_ERROR;
  1834.     }

  1835.     return NGX_OK;
  1836. }


  1837. static ngx_int_t
  1838. ngx_http_proxy_non_buffered_copy_filter(void *data, ssize_t bytes)
  1839. {
  1840.     ngx_http_request_t   *r = data;

  1841.     ngx_buf_t            *b;
  1842.     ngx_chain_t          *cl, **ll;
  1843.     ngx_http_upstream_t  *u;

  1844.     u = r->upstream;

  1845.     if (u->length == 0) {
  1846.         ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
  1847.                       "upstream sent more data than specified in "
  1848.                       "\"Content-Length\" header");
  1849.         u->keepalive = 0;
  1850.         return NGX_OK;
  1851.     }

  1852.     for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
  1853.         ll = &cl->next;
  1854.     }

  1855.     cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
  1856.     if (cl == NULL) {
  1857.         return NGX_ERROR;
  1858.     }

  1859.     *ll = cl;

  1860.     cl->buf->flush = 1;
  1861.     cl->buf->memory = 1;

  1862.     b = &u->buffer;

  1863.     cl->buf->pos = b->last;
  1864.     b->last += bytes;
  1865.     cl->buf->last = b->last;
  1866.     cl->buf->tag = u->output.tag;

  1867.     if (u->length == -1) {
  1868.         return NGX_OK;
  1869.     }

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

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

  1874.         cl->buf->last = cl->buf->pos + u->length;
  1875.         u->length = 0;

  1876.         return NGX_OK;
  1877.     }

  1878.     u->length -= bytes;

  1879.     if (u->length == 0) {
  1880.         u->keepalive = !u->headers_in.connection_close;
  1881.     }

  1882.     return NGX_OK;
  1883. }


  1884. static ngx_int_t
  1885. ngx_http_proxy_non_buffered_chunked_filter(void *data, ssize_t bytes)
  1886. {
  1887.     ngx_http_request_t   *r = data;

  1888.     ngx_int_t                   rc;
  1889.     ngx_buf_t                  *b, *buf;
  1890.     ngx_chain_t                *cl, **ll;
  1891.     ngx_http_upstream_t        *u;
  1892.     ngx_http_proxy_ctx_t       *ctx;
  1893.     ngx_http_proxy_loc_conf_t  *plcf;

  1894.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  1895.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1896.     if (ctx == NULL) {
  1897.         return NGX_ERROR;
  1898.     }

  1899.     u = r->upstream;
  1900.     buf = &u->buffer;

  1901.     buf->pos = buf->last;
  1902.     buf->last += bytes;

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

  1905.         if (rc == NGX_ERROR) {
  1906.             return NGX_ERROR;
  1907.         }

  1908.         if (rc == NGX_OK) {

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

  1910.             r->upstream->keepalive = !u->headers_in.connection_close;
  1911.             u->length = 0;

  1912.             if (buf->pos != buf->last) {
  1913.                 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
  1914.                               "upstream sent data after trailers");
  1915.                 u->keepalive = 0;
  1916.             }
  1917.         }

  1918.         return NGX_OK;
  1919.     }

  1920.     for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
  1921.         ll = &cl->next;
  1922.     }

  1923.     for ( ;; ) {

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

  1926.         if (rc == NGX_OK) {

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

  1928.             cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
  1929.             if (cl == NULL) {
  1930.                 return NGX_ERROR;
  1931.             }

  1932.             *ll = cl;
  1933.             ll = &cl->next;

  1934.             b = cl->buf;

  1935.             b->flush = 1;
  1936.             b->memory = 1;

  1937.             b->pos = buf->pos;
  1938.             b->tag = u->output.tag;

  1939.             if (buf->last - buf->pos >= ctx->chunked.size) {
  1940.                 buf->pos += (size_t) ctx->chunked.size;
  1941.                 b->last = buf->pos;
  1942.                 ctx->chunked.size = 0;

  1943.             } else {
  1944.                 ctx->chunked.size -= buf->last - buf->pos;
  1945.                 buf->pos = buf->last;
  1946.                 b->last = buf->last;
  1947.             }

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

  1951.             continue;
  1952.         }

  1953.         if (rc == NGX_DONE) {

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

  1956.                 if (rc == NGX_ERROR) {
  1957.                     return NGX_ERROR;
  1958.                 }

  1959.                 if (rc == NGX_AGAIN) {
  1960.                     u->length = 1;
  1961.                     break;
  1962.                 }
  1963.             }

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

  1965.             u->keepalive = !u->headers_in.connection_close;
  1966.             u->length = 0;

  1967.             if (buf->pos != buf->last) {
  1968.                 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
  1969.                               "upstream sent data after final chunk");
  1970.                 u->keepalive = 0;
  1971.             }

  1972.             break;
  1973.         }

  1974.         if (rc == NGX_AGAIN) {
  1975.             break;
  1976.         }

  1977.         /* invalid response */

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

  1980.         return NGX_ERROR;
  1981.     }

  1982.     return NGX_OK;
  1983. }


  1984. static ngx_int_t
  1985. ngx_http_proxy_process_trailer(ngx_http_request_t *r, ngx_buf_t *buf)
  1986. {
  1987.     size_t                      len;
  1988.     ngx_int_t                   rc;
  1989.     ngx_buf_t                  *b;
  1990.     ngx_table_elt_t            *h;
  1991.     ngx_http_proxy_ctx_t       *ctx;
  1992.     ngx_http_proxy_loc_conf_t  *plcf;

  1993.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

  1994.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  1995.     if (ctx->trailers == NULL) {
  1996.         ctx->trailers = ngx_create_temp_buf(r->pool,
  1997.                                             plcf->upstream.buffer_size);
  1998.         if (ctx->trailers == NULL) {
  1999.             return NGX_ERROR;
  2000.         }
  2001.     }

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

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

  2005.     for ( ;; ) {

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

  2007.         if (rc == NGX_OK) {

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

  2009.             h = ngx_list_push(&r->upstream->headers_in.trailers);
  2010.             if (h == NULL) {
  2011.                 return NGX_ERROR;
  2012.             }

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

  2014.             h->key.len = r->header_name_end - r->header_name_start;
  2015.             h->value.len = r->header_end - r->header_start;

  2016.             h->key.data = ngx_pnalloc(r->pool,
  2017.                                h->key.len + 1 + h->value.len + 1 + h->key.len);
  2018.             if (h->key.data == NULL) {
  2019.                 h->hash = 0;
  2020.                 return NGX_ERROR;
  2021.             }

  2022.             h->value.data = h->key.data + h->key.len + 1;
  2023.             h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;

  2024.             ngx_memcpy(h->key.data, r->header_name_start, h->key.len);
  2025.             h->key.data[h->key.len] = '\0';
  2026.             ngx_memcpy(h->value.data, r->header_start, h->value.len);
  2027.             h->value.data[h->value.len] = '\0';

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

  2030.             } else {
  2031.                 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
  2032.             }

  2033.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  2034.                            "http proxy trailer: \"%V: %V\"",
  2035.                            &h->key, &h->value);
  2036.             continue;
  2037.         }

  2038.         if (rc == NGX_HTTP_PARSE_HEADER_DONE) {

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

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

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

  2043.             return NGX_OK;
  2044.         }

  2045.         if (rc == NGX_AGAIN) {
  2046.             buf->pos += len;

  2047.             if (b->last == b->end) {
  2048.                 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  2049.                               "upstream sent too big trailers");
  2050.                 return NGX_ERROR;
  2051.             }

  2052.             return NGX_AGAIN;
  2053.         }

  2054.         /* rc == NGX_HTTP_PARSE_INVALID_HEADER */

  2055.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  2056.                       "upstream sent invalid trailer: \"%*s\\x%02xd...\"",
  2057.                       r->header_end - r->header_name_start,
  2058.                       r->header_name_start, *r->header_end);

  2059.         return NGX_ERROR;
  2060.     }
  2061. }


  2062. static void
  2063. ngx_http_proxy_abort_request(ngx_http_request_t *r)
  2064. {
  2065.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  2066.                    "abort http proxy request");

  2067.     return;
  2068. }


  2069. static void
  2070. ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
  2071. {
  2072.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  2073.                    "finalize http proxy request");

  2074.     return;
  2075. }


  2076. static ngx_int_t
  2077. ngx_http_proxy_host_variable(ngx_http_request_t *r,
  2078.     ngx_http_variable_value_t *v, uintptr_t data)
  2079. {
  2080.     ngx_http_proxy_ctx_t  *ctx;

  2081.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  2082.     if (ctx == NULL) {
  2083.         v->not_found = 1;
  2084.         return NGX_OK;
  2085.     }

  2086.     v->len = ctx->vars.host_header.len;
  2087.     v->valid = 1;
  2088.     v->no_cacheable = 0;
  2089.     v->not_found = 0;
  2090.     v->data = ctx->vars.host_header.data;

  2091.     return NGX_OK;
  2092. }


  2093. static ngx_int_t
  2094. ngx_http_proxy_port_variable(ngx_http_request_t *r,
  2095.     ngx_http_variable_value_t *v, uintptr_t data)
  2096. {
  2097.     ngx_http_proxy_ctx_t  *ctx;

  2098.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  2099.     if (ctx == NULL) {
  2100.         v->not_found = 1;
  2101.         return NGX_OK;
  2102.     }

  2103.     v->len = ctx->vars.port.len;
  2104.     v->valid = 1;
  2105.     v->no_cacheable = 0;
  2106.     v->not_found = 0;
  2107.     v->data = ctx->vars.port.data;

  2108.     return NGX_OK;
  2109. }


  2110. static ngx_int_t
  2111. ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
  2112.     ngx_http_variable_value_t *v, uintptr_t data)
  2113. {
  2114.     size_t            len;
  2115.     u_char           *p;
  2116.     ngx_table_elt_t  *h, *xfwd;

  2117.     v->valid = 1;
  2118.     v->no_cacheable = 0;
  2119.     v->not_found = 0;

  2120.     xfwd = r->headers_in.x_forwarded_for;

  2121.     len = 0;

  2122.     for (h = xfwd; h; h = h->next) {
  2123.         len += h->value.len + sizeof(", ") - 1;
  2124.     }

  2125.     if (len == 0) {
  2126.         v->len = r->connection->addr_text.len;
  2127.         v->data = r->connection->addr_text.data;
  2128.         return NGX_OK;
  2129.     }

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

  2131.     p = ngx_pnalloc(r->pool, len);
  2132.     if (p == NULL) {
  2133.         return NGX_ERROR;
  2134.     }

  2135.     v->len = len;
  2136.     v->data = p;

  2137.     for (h = xfwd; h; h = h->next) {
  2138.         p = ngx_copy(p, h->value.data, h->value.len);
  2139.         *p++ = ','; *p++ = ' ';
  2140.     }

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

  2142.     return NGX_OK;
  2143. }


  2144. static ngx_int_t
  2145. ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
  2146.     ngx_http_variable_value_t *v, uintptr_t data)
  2147. {
  2148.     ngx_http_proxy_ctx_t  *ctx;

  2149.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  2150.     if (ctx == NULL || ctx->internal_body_length < 0) {
  2151.         v->not_found = 1;
  2152.         return NGX_OK;
  2153.     }

  2154.     v->valid = 1;
  2155.     v->no_cacheable = 0;
  2156.     v->not_found = 0;

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

  2158.     if (v->data == NULL) {
  2159.         return NGX_ERROR;
  2160.     }

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

  2162.     return NGX_OK;
  2163. }


  2164. static ngx_int_t
  2165. ngx_http_proxy_internal_chunked_variable(ngx_http_request_t *r,
  2166.     ngx_http_variable_value_t *v, uintptr_t data)
  2167. {
  2168.     ngx_http_proxy_ctx_t  *ctx;

  2169.     ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

  2170.     if (ctx == NULL || !ctx->internal_chunked) {
  2171.         v->not_found = 1;
  2172.         return NGX_OK;
  2173.     }

  2174.     v->valid = 1;
  2175.     v->no_cacheable = 0;
  2176.     v->not_found = 0;

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

  2179.     return NGX_OK;
  2180. }


  2181. ngx_int_t
  2182. ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,
  2183.     size_t prefix)
  2184. {
  2185.     size_t                      len;
  2186.     ngx_int_t                   rc;
  2187.     ngx_uint_t                  i;
  2188.     ngx_http_proxy_rewrite_t   *pr;
  2189.     ngx_http_proxy_loc_conf_t  *plcf;

  2190.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

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

  2192.     if (pr == NULL) {
  2193.         return NGX_DECLINED;
  2194.     }

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

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

  2198.         if (rc != NGX_DECLINED) {
  2199.             return rc;
  2200.         }
  2201.     }

  2202.     return NGX_DECLINED;
  2203. }


  2204. ngx_int_t
  2205. ngx_http_proxy_rewrite_cookie(ngx_http_request_t *r, ngx_table_elt_t *h)
  2206. {
  2207.     u_char                     *p;
  2208.     size_t                      len;
  2209.     ngx_int_t                   rc, rv;
  2210.     ngx_str_t                  *key, *value;
  2211.     ngx_uint_t                  i;
  2212.     ngx_array_t                 attrs;
  2213.     ngx_keyval_t               *attr;
  2214.     ngx_http_proxy_loc_conf_t  *plcf;

  2215.     if (ngx_array_init(&attrs, r->pool, 2, sizeof(ngx_keyval_t)) != NGX_OK) {
  2216.         return NGX_ERROR;
  2217.     }

  2218.     if (ngx_http_proxy_parse_cookie(&h->value, &attrs) != NGX_OK) {
  2219.         return NGX_ERROR;
  2220.     }

  2221.     attr = attrs.elts;

  2222.     if (attr[0].value.data == NULL) {
  2223.         return NGX_DECLINED;
  2224.     }

  2225.     rv = NGX_DECLINED;

  2226.     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

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

  2228.         key = &attr[i].key;
  2229.         value = &attr[i].value;

  2230.         if (plcf->cookie_domains && key->len == 6
  2231.             && ngx_strncasecmp(key->data, (u_char *) "domain", 6) == 0
  2232.             && value->data)
  2233.         {
  2234.             rc = ngx_http_proxy_rewrite_cookie_value(r, value,
  2235.                                                      plcf->cookie_domains);
  2236.             if (rc == NGX_ERROR) {
  2237.                 return NGX_ERROR;
  2238.             }

  2239.             if (rc != NGX_DECLINED) {
  2240.                 rv = rc;
  2241.             }
  2242.         }

  2243.         if (plcf->cookie_paths && key->len == 4
  2244.             && ngx_strncasecmp(key->data, (u_char *) "path", 4) == 0
  2245.             && value->data)
  2246.         {
  2247.             rc = ngx_http_proxy_rewrite_cookie_value(r, value,
  2248.                                                      plcf->cookie_paths);
  2249.             if (rc == NGX_ERROR) {
  2250.                 return NGX_ERROR;
  2251.             }

  2252.             if (rc != NGX_DECLINED) {
  2253.                 rv = rc;
  2254.             }
  2255.         }
  2256.     }

  2257.     if (plcf->cookie_flags) {
  2258.         rc = ngx_http_proxy_rewrite_cookie_flags(r, &attrs,
  2259.                                                  plcf->cookie_flags);
  2260.         if (rc == NGX_ERROR) {
  2261.             return NGX_ERROR;
  2262.         }

  2263.         if (rc != NGX_DECLINED) {
  2264.             rv = rc;
  2265.         }

  2266.         attr = attrs.elts;
  2267.     }

  2268.     if (rv != NGX_OK) {
  2269.         return rv;
  2270.     }

  2271.     len = 0;

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

  2273.         if (attr[i].key.data == NULL) {
  2274.             continue;
  2275.         }

  2276.         if (i > 0) {
  2277.             len += 2;
  2278.         }

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

  2280.         if (attr[i].value.data) {
  2281.             len += 1 + attr[i].value.len;
  2282.         }
  2283.     }

  2284.     p = ngx_pnalloc(r->pool, len + 1);
  2285.     if (p == NULL) {
  2286.         return NGX_ERROR;
  2287.     }

  2288.     h->value.data = p;
  2289.     h->value.len = len;

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

  2291.         if (attr[i].key.data == NULL) {
  2292.             continue;
  2293.         }

  2294.         if (i > 0) {
  2295.             *p++ = ';';
  2296.             *p++ = ' ';
  2297.         }

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

  2299.         if (attr[i].value.data) {
  2300.             *p++ = '=';
  2301.             p = ngx_cpymem(p, attr[i].value.data, attr[i].value.len);
  2302.         }
  2303.     }

  2304.     *p = '\0';

  2305.     return NGX_OK;
  2306. }


  2307. static ngx_int_t
  2308. ngx_http_proxy_parse_cookie(ngx_str_t *value, ngx_array_t *attrs)
  2309. {
  2310.     u_char        *start, *end, *p, *last;
  2311.     ngx_str_t      name, val;
  2312.     ngx_keyval_t  *attr;

  2313.     start = value->data;
  2314.     end = value->data + value->len;

  2315.     for ( ;; ) {

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

  2317.         if (last == NULL) {
  2318.             last = end;
  2319.         }

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

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

  2322.         name.data = start;
  2323.         name.len = p - start;

  2324.         while (name.len && name.data[name.len - 1] == ' ') {
  2325.             name.len--;
  2326.         }

  2327.         if (p < last) {

  2328.             p++;

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

  2330.             val.data = p;
  2331.             val.len = last - val.data;

  2332.             while (val.len && val.data[val.len - 1] == ' ') {
  2333.                 val.len--;
  2334.             }

  2335.         } else {
  2336.             ngx_str_null(&val);
  2337.         }

  2338.         attr = ngx_array_push(attrs);
  2339.         if (attr == NULL) {
  2340.             return NGX_ERROR;
  2341.         }

  2342.         attr->key = name;
  2343.         attr->value = val;

  2344.         if (last == end) {
  2345.             break;
  2346.         }

  2347.         start = last + 1;
  2348.     }

  2349.     return NGX_OK;
  2350. }


  2351. static ngx_int_t
  2352. ngx_http_proxy_rewrite_cookie_value(ngx_http_request_t *r, ngx_str_t *value,
  2353.     ngx_array_t *rewrites)
  2354. {
  2355.     ngx_int_t                  rc;
  2356.     ngx_uint_t                 i;
  2357.     ngx_http_proxy_rewrite_t  *pr;

  2358.     pr = rewrites->elts;

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

  2361.         if (rc != NGX_DECLINED) {
  2362.             return rc;
  2363.         }
  2364.     }

  2365.     return NGX_DECLINED;
  2366. }


  2367. static ngx_int_t
  2368. ngx_http_proxy_rewrite_cookie_flags(ngx_http_request_t *r, ngx_array_t *attrs,
  2369.     ngx_array_t *flags)
  2370. {
  2371.     ngx_str_t                       pattern, value;
  2372. #if (NGX_PCRE)
  2373.     ngx_int_t                       rc;
  2374. #endif
  2375.     ngx_uint_t                      i, m, f, nelts;
  2376.     ngx_keyval_t                   *attr;
  2377.     ngx_conf_bitmask_t             *mask;
  2378.     ngx_http_complex_value_t       *flags_values;
  2379.     ngx_http_proxy_cookie_flags_t  *pcf;

  2380.     attr = attrs->elts;
  2381.     pcf = flags->elts;

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

  2383. #if (NGX_PCRE)
  2384.         if (pcf[i].regex) {
  2385.             rc = ngx_http_regex_exec(r, pcf[i].cookie.regex, &attr[0].key);

  2386.             if (rc == NGX_ERROR) {
  2387.                 return NGX_ERROR;
  2388.             }

  2389.             if (rc == NGX_OK) {
  2390.                 break;
  2391.             }

  2392.             /* NGX_DECLINED */

  2393.             continue;
  2394.         }
  2395. #endif

  2396.         if (ngx_http_complex_value(r, &pcf[i].cookie.complex, &pattern)
  2397.             != NGX_OK)
  2398.         {
  2399.             return NGX_ERROR;
  2400.         }

  2401.         if (pattern.len == attr[0].key.len
  2402.             && ngx_strncasecmp(attr[0].key.data, pattern.data, pattern.len)
  2403.                == 0)
  2404.         {
  2405.             break;
  2406.         }
  2407.     }

  2408.     if (i == flags->nelts) {
  2409.         return NGX_DECLINED;
  2410.     }

  2411.     nelts = pcf[i].flags_values.nelts;
  2412.     flags_values = pcf[i].flags_values.elts;

  2413.     mask = ngx_http_proxy_cookie_flags_masks;
  2414.     f = 0;

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

  2416.         if (ngx_http_complex_value(r, &flags_values[i], &value) != NGX_OK) {
  2417.             return NGX_ERROR;
  2418.         }

  2419.         if (value.len == 0) {
  2420.             continue;
  2421.         }

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

  2423.             if (mask[m].name.len != value.len
  2424.                 || ngx_strncasecmp(mask[m].name.data, value.data, value.len)
  2425.                    != 0)
  2426.             {
  2427.                 continue;
  2428.             }

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

  2430.             break;
  2431.         }

  2432.         if (mask[m].name.len == 0) {
  2433.             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  2434.                            "invalid proxy_cookie_flags flag \"%V\"", &value);
  2435.         }
  2436.     }

  2437.     if (f == 0) {
  2438.         return NGX_DECLINED;
  2439.     }

  2440.     return ngx_http_proxy_edit_cookie_flags(r, attrs, f);
  2441. }


  2442. static ngx_int_t
  2443. ngx_http_proxy_edit_cookie_flags(ngx_http_request_t *r, ngx_array_t *attrs,
  2444.     ngx_uint_t flags)
  2445. {
  2446.     ngx_str_t     *key, *value;
  2447.     ngx_uint_t     i;
  2448.     ngx_keyval_t  *attr;

  2449.     attr = attrs->elts;

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

  2452.         if (key->len == 6
  2453.             && ngx_strncasecmp(key->data, (u_char *) "secure", 6) == 0)
  2454.         {
  2455.             if (flags & NGX_HTTP_PROXY_COOKIE_SECURE_ON) {
  2456.                 flags &= ~NGX_HTTP_PROXY_COOKIE_SECURE_ON;

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

  2460.             continue;
  2461.         }

  2462.         if (key->len == 8
  2463.             && ngx_strncasecmp(key->data, (u_char *) "httponly", 8) == 0)
  2464.         {
  2465.             if (flags & NGX_HTTP_PROXY_COOKIE_HTTPONLY_ON) {
  2466.                 flags &= ~NGX_HTTP_PROXY_COOKIE_HTTPONLY_ON;

  2467.             } else if (flags & NGX_HTTP_PROXY_COOKIE_HTTPONLY_OFF) {
  2468.                 key->data = NULL;
  2469.             }

  2470.             continue;
  2471.         }

  2472.         if (key->len == 8
  2473.             && ngx_strncasecmp(key->data, (u_char *) "samesite", 8) == 0)
  2474.         {
  2475.             value = &attr[i].value;

  2476.             if (flags & NGX_HTTP_PROXY_COOKIE_SAMESITE_STRICT) {
  2477.                 flags &= ~NGX_HTTP_PROXY_COOKIE_SAMESITE_STRICT;

  2478.                 if (value->len != 6
  2479.                     || ngx_strncasecmp(value->data, (u_char *) "strict", 6)
  2480.                        != 0)
  2481.                 {
  2482.                     ngx_str_set(key, "SameSite");
  2483.                     ngx_str_set(value, "Strict");
  2484.                 }

  2485.             } else if (flags & NGX_HTTP_PROXY_COOKIE_SAMESITE_LAX) {
  2486.                 flags &= ~NGX_HTTP_PROXY_COOKIE_SAMESITE_LAX;

  2487.                 if (value->len != 3
  2488.                     || ngx_strncasecmp(value->data, (u_char *) "lax", 3) != 0)
  2489.                 {
  2490.                     ngx_str_set(key, "SameSite");
  2491.                     ngx_str_set(value, "Lax");
  2492.                 }

  2493.             } else if (flags & NGX_HTTP_PROXY_COOKIE_SAMESITE_NONE) {
  2494.                 flags &= ~NGX_HTTP_PROXY_COOKIE_SAMESITE_NONE;

  2495.                 if (value->len != 4
  2496.                     || ngx_strncasecmp(value->data, (u_char *) "none", 4) != 0)
  2497.                 {
  2498.                     ngx_str_set(key, "SameSite");
  2499.                     ngx_str_set(value, "None");
  2500.                 }

  2501.             } else if (flags & NGX_HTTP_PROXY_COOKIE_SAMESITE_OFF) {
  2502.                 key->data = NULL;
  2503.             }

  2504.             continue;
  2505.         }
  2506.     }

  2507.     if (flags & NGX_HTTP_PROXY_COOKIE_SECURE_ON) {
  2508.         attr = ngx_array_push(attrs);
  2509.         if (attr == NULL) {
  2510.             return NGX_ERROR;
  2511.         }

  2512.         ngx_str_set(&attr->key, "Secure");
  2513.         ngx_str_null(&attr->value);
  2514.     }

  2515.     if (flags & NGX_HTTP_PROXY_COOKIE_HTTPONLY_ON) {
  2516.         attr = ngx_array_push(attrs);
  2517.         if (attr == NULL) {
  2518.             return NGX_ERROR;
  2519.         }

  2520.         ngx_str_set(&attr->key, "HttpOnly");
  2521.         ngx_str_null(&attr->value);
  2522.     }

  2523.     if (flags & (NGX_HTTP_PROXY_COOKIE_SAMESITE_STRICT
  2524.                  |NGX_HTTP_PROXY_COOKIE_SAMESITE_LAX
  2525.                  |NGX_HTTP_PROXY_COOKIE_SAMESITE_NONE))
  2526.     {
  2527.         attr = ngx_array_push(attrs);
  2528.         if (attr == NULL) {
  2529.             return NGX_ERROR;
  2530.         }

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

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

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

  2536.         } else {
  2537.             ngx_str_set(&attr->value, "None");
  2538.         }
  2539.     }

  2540.     return NGX_OK;
  2541. }


  2542. static ngx_int_t
  2543. ngx_http_proxy_rewrite_complex_handler(ngx_http_request_t *r, ngx_str_t *value,
  2544.     size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
  2545. {
  2546.     ngx_str_t  pattern, replacement;

  2547.     if (ngx_http_complex_value(r, &pr->pattern.complex, &pattern) != NGX_OK) {
  2548.         return NGX_ERROR;
  2549.     }

  2550.     if (pattern.len > len
  2551.         || ngx_rstrncmp(value->data + prefix, pattern.data, pattern.len) != 0)
  2552.     {
  2553.         return NGX_DECLINED;
  2554.     }

  2555.     if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
  2556.         return NGX_ERROR;
  2557.     }

  2558.     return ngx_http_proxy_rewrite(r, value, prefix, pattern.len, &replacement);
  2559. }


  2560. #if (NGX_PCRE)

  2561. static ngx_int_t
  2562. ngx_http_proxy_rewrite_regex_handler(ngx_http_request_t *r, ngx_str_t *value,
  2563.     size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
  2564. {
  2565.     ngx_str_t  pattern, replacement;

  2566.     pattern.len = len;
  2567.     pattern.data = value->data + prefix;

  2568.     if (ngx_http_regex_exec(r, pr->pattern.regex, &pattern) != NGX_OK) {
  2569.         return NGX_DECLINED;
  2570.     }

  2571.     if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
  2572.         return NGX_ERROR;
  2573.     }

  2574.     return ngx_http_proxy_rewrite(r, value, prefix, len, &replacement);
  2575. }

  2576. #endif


  2577. static ngx_int_t
  2578. ngx_http_proxy_rewrite_domain_handler(ngx_http_request_t *r, ngx_str_t *value,
  2579.     size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
  2580. {
  2581.     u_char     *p;
  2582.     ngx_str_t   pattern, replacement;

  2583.     if (ngx_http_complex_value(r, &pr->pattern.complex, &pattern) != NGX_OK) {
  2584.         return NGX_ERROR;
  2585.     }

  2586.     p = value->data + prefix;

  2587.     if (len && p[0] == '.') {
  2588.         p++;
  2589.         prefix++;
  2590.         len--;
  2591.     }

  2592.     if (pattern.len != len || ngx_rstrncasecmp(pattern.data, p, len) != 0) {
  2593.         return NGX_DECLINED;
  2594.     }

  2595.     if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
  2596.         return NGX_ERROR;
  2597.     }

  2598.     return ngx_http_proxy_rewrite(r, value, prefix, len, &replacement);
  2599. }


  2600. static ngx_int_t
  2601. ngx_http_proxy_rewrite(ngx_http_request_t *r, ngx_str_t *value, size_t prefix,
  2602.     size_t len, ngx_str_t *replacement)
  2603. {
  2604.     u_char  *p, *data;
  2605.     size_t   new_len;

  2606.     if (len == value->len) {
  2607.         *value = *replacement;
  2608.         return NGX_OK;
  2609.     }

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

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

  2612.         data = ngx_pnalloc(r->pool, new_len + 1);
  2613.         if (data == NULL) {
  2614.             return NGX_ERROR;
  2615.         }

  2616.         p = ngx_copy(data, value->data, prefix);
  2617.         p = ngx_copy(p, replacement->data, replacement->len);

  2618.         ngx_memcpy(p, value->data + prefix + len,
  2619.                    value->len - len - prefix + 1);

  2620.         value->data = data;

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

  2623.         ngx_memmove(p, value->data + prefix + len,
  2624.                     value->len - len - prefix + 1);
  2625.     }

  2626.     value->len = new_len;

  2627.     return NGX_OK;
  2628. }


  2629. static ngx_int_t
  2630. ngx_http_proxy_add_variables(ngx_conf_t *cf)
  2631. {
  2632.     ngx_http_variable_t  *var, *v;

  2633.     for (v = ngx_http_proxy_vars; v->name.len; v++) {
  2634.         var = ngx_http_add_variable(cf, &v->name, v->flags);
  2635.         if (var == NULL) {
  2636.             return NGX_ERROR;
  2637.         }

  2638.         var->get_handler = v->get_handler;
  2639.         var->data = v->data;
  2640.     }

  2641.     return NGX_OK;
  2642. }


  2643. static void *
  2644. ngx_http_proxy_create_main_conf(ngx_conf_t *cf)
  2645. {
  2646.     ngx_http_proxy_main_conf_t  *conf;

  2647.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_main_conf_t));
  2648.     if (conf == NULL) {
  2649.         return NULL;
  2650.     }

  2651. #if (NGX_HTTP_CACHE)
  2652.     if (ngx_array_init(&conf->caches, cf->pool, 4,
  2653.                        sizeof(ngx_http_file_cache_t *))
  2654.         != NGX_OK)
  2655.     {
  2656.         return NULL;
  2657.     }
  2658. #endif

  2659.     return conf;
  2660. }


  2661. static void *
  2662. ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
  2663. {
  2664.     ngx_http_proxy_loc_conf_t  *conf;

  2665.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t));
  2666.     if (conf == NULL) {
  2667.         return NULL;
  2668.     }

  2669.     /*
  2670.      * set by ngx_pcalloc():
  2671.      *
  2672.      *     conf->upstream.bufs.num = 0;
  2673.      *     conf->upstream.ignore_headers = 0;
  2674.      *     conf->upstream.next_upstream = 0;
  2675.      *     conf->upstream.cache_zone = NULL;
  2676.      *     conf->upstream.cache_use_stale = 0;
  2677.      *     conf->upstream.cache_methods = 0;
  2678.      *     conf->upstream.temp_path = NULL;
  2679.      *     conf->upstream.hide_headers_hash = { NULL, 0 };
  2680.      *     conf->upstream.store_lengths = NULL;
  2681.      *     conf->upstream.store_values = NULL;
  2682.      *
  2683.      *     conf->location = NULL;
  2684.      *     conf->url = { 0, NULL };
  2685.      *     conf->headers.lengths = NULL;
  2686.      *     conf->headers.values = NULL;
  2687.      *     conf->headers.hash = { NULL, 0 };
  2688.      *     conf->headers_cache.lengths = NULL;
  2689.      *     conf->host_value = NULL;
  2690.      *     conf->headers_cache.values = NULL;
  2691.      *     conf->headers_cache.hash = { NULL, 0 };
  2692.      *     conf->body_lengths = NULL;
  2693.      *     conf->body_values = NULL;
  2694.      *     conf->body_source = { 0, NULL };
  2695.      *     conf->redirects = NULL;
  2696.      *     conf->ssl = 0;
  2697.      *     conf->ssl_protocols = 0;
  2698.      *     conf->ssl_ciphers = { 0, NULL };
  2699.      *     conf->ssl_trusted_certificate = { 0, NULL };
  2700.      *     conf->ssl_crl = { 0, NULL };
  2701.      */

  2702.     conf->upstream.store = NGX_CONF_UNSET;
  2703.     conf->upstream.store_access = NGX_CONF_UNSET_UINT;
  2704.     conf->upstream.next_upstream_tries = NGX_CONF_UNSET_UINT;
  2705.     conf->upstream.buffering = NGX_CONF_UNSET;
  2706.     conf->upstream.request_buffering = NGX_CONF_UNSET;
  2707.     conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
  2708.     conf->upstream.force_ranges = NGX_CONF_UNSET;

  2709.     conf->upstream.local = NGX_CONF_UNSET_PTR;
  2710.     conf->upstream.socket_keepalive = NGX_CONF_UNSET;
  2711.     conf->upstream.socket_rcvbuf = NGX_CONF_UNSET_SIZE;
  2712.     conf->upstream.socket_sndbuf = NGX_CONF_UNSET_SIZE;

  2713.     conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
  2714.     conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
  2715.     conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
  2716.     conf->upstream.next_upstream_timeout = NGX_CONF_UNSET_MSEC;

  2717.     conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
  2718.     conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
  2719.     conf->upstream.limit_rate = NGX_CONF_UNSET_PTR;

  2720.     conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
  2721.     conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
  2722.     conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;

  2723.     conf->upstream.pass_request_headers = NGX_CONF_UNSET;
  2724.     conf->upstream.pass_request_body = NGX_CONF_UNSET;
  2725.     conf->upstream.pass_trailers = NGX_CONF_UNSET;

  2726. #if (NGX_HTTP_CACHE)
  2727.     conf->upstream.cache = NGX_CONF_UNSET;
  2728.     conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
  2729.     conf->upstream.cache_max_range_offset = NGX_CONF_UNSET;
  2730.     conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
  2731.     conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
  2732.     conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
  2733.     conf->upstream.cache_lock = NGX_CONF_UNSET;
  2734.     conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
  2735.     conf->upstream.cache_lock_age = NGX_CONF_UNSET_MSEC;
  2736.     conf->upstream.cache_revalidate = NGX_CONF_UNSET;
  2737.     conf->upstream.cache_convert_head = NGX_CONF_UNSET;
  2738.     conf->upstream.cache_background_update = NGX_CONF_UNSET;
  2739. #endif

  2740.     conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
  2741.     conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;

  2742.     conf->upstream.intercept_errors = NGX_CONF_UNSET;

  2743. #if (NGX_HTTP_SSL)
  2744.     conf->upstream.ssl_session_reuse = NGX_CONF_UNSET;
  2745.     conf->upstream.ssl_name = NGX_CONF_UNSET_PTR;
  2746.     conf->upstream.ssl_server_name = NGX_CONF_UNSET;
  2747.     conf->upstream.ssl_verify = NGX_CONF_UNSET;
  2748.     conf->upstream.ssl_certificate = NGX_CONF_UNSET_PTR;
  2749.     conf->upstream.ssl_certificate_key = NGX_CONF_UNSET_PTR;
  2750.     conf->upstream.ssl_certificate_cache = NGX_CONF_UNSET_PTR;
  2751.     conf->upstream.ssl_passwords = NGX_CONF_UNSET_PTR;
  2752.     conf->ssl_verify_depth = NGX_CONF_UNSET_UINT;
  2753.     conf->ssl_conf_commands = NGX_CONF_UNSET_PTR;
  2754. #endif

  2755.     /* the hardcoded values */
  2756.     conf->upstream.cyclic_temp_file = 0;
  2757.     conf->upstream.change_buffering = 1;
  2758.     conf->upstream.pass_early_hints = 1;

  2759.     conf->headers_source = NGX_CONF_UNSET_PTR;

  2760.     conf->method = NGX_CONF_UNSET_PTR;

  2761.     conf->redirect = NGX_CONF_UNSET;

  2762.     conf->cookie_domains = NGX_CONF_UNSET_PTR;
  2763.     conf->cookie_paths = NGX_CONF_UNSET_PTR;
  2764.     conf->cookie_flags = NGX_CONF_UNSET_PTR;

  2765.     conf->http_version = NGX_CONF_UNSET_UINT;

  2766.     conf->headers_hash_max_size = NGX_CONF_UNSET_UINT;
  2767.     conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT;

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

  2769.     return conf;
  2770. }


  2771. static char *
  2772. ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
  2773. {
  2774.     ngx_http_proxy_loc_conf_t *prev = parent;
  2775.     ngx_http_proxy_loc_conf_t *conf = child;

  2776.     u_char                     *p;
  2777.     size_t                      size;
  2778.     ngx_int_t                   rc;
  2779.     ngx_hash_init_t             hash;
  2780.     ngx_http_core_loc_conf_t   *clcf;
  2781.     ngx_http_proxy_rewrite_t   *pr;
  2782.     ngx_http_script_compile_t   sc;

  2783. #if (NGX_HTTP_CACHE)

  2784.     if (conf->upstream.store > 0) {
  2785.         conf->upstream.cache = 0;
  2786.     }

  2787.     if (conf->upstream.cache > 0) {
  2788.         conf->upstream.store = 0;
  2789.     }

  2790. #endif

  2791.     if (conf->upstream.store == NGX_CONF_UNSET) {
  2792.         ngx_conf_merge_value(conf->upstream.store,
  2793.                               prev->upstream.store, 0);

  2794.         conf->upstream.store_lengths = prev->upstream.store_lengths;
  2795.         conf->upstream.store_values = prev->upstream.store_values;
  2796.     }

  2797.     ngx_conf_merge_uint_value(conf->upstream.store_access,
  2798.                               prev->upstream.store_access, 0600);

  2799.     ngx_conf_merge_uint_value(conf->upstream.next_upstream_tries,
  2800.                               prev->upstream.next_upstream_tries, 0);

  2801.     ngx_conf_merge_value(conf->upstream.buffering,
  2802.                               prev->upstream.buffering, 1);

  2803.     ngx_conf_merge_value(conf->upstream.request_buffering,
  2804.                               prev->upstream.request_buffering, 1);

  2805.     ngx_conf_merge_value(conf->upstream.ignore_client_abort,
  2806.                               prev->upstream.ignore_client_abort, 0);

  2807.     ngx_conf_merge_value(conf->upstream.force_ranges,
  2808.                               prev->upstream.force_ranges, 0);

  2809.     ngx_conf_merge_ptr_value(conf->upstream.local,
  2810.                               prev->upstream.local, NULL);

  2811.     ngx_conf_merge_value(conf->upstream.socket_keepalive,
  2812.                               prev->upstream.socket_keepalive, 0);

  2813.     ngx_conf_merge_size_value(conf->upstream.socket_rcvbuf,
  2814.                               prev->upstream.socket_rcvbuf, 0);

  2815.     ngx_conf_merge_size_value(conf->upstream.socket_sndbuf,
  2816.                               prev->upstream.socket_sndbuf, 0);

  2817.     ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
  2818.                               prev->upstream.connect_timeout, 60000);

  2819.     ngx_conf_merge_msec_value(conf->upstream.send_timeout,
  2820.                               prev->upstream.send_timeout, 60000);

  2821.     ngx_conf_merge_msec_value(conf->upstream.read_timeout,
  2822.                               prev->upstream.read_timeout, 60000);

  2823.     ngx_conf_merge_msec_value(conf->upstream.next_upstream_timeout,
  2824.                               prev->upstream.next_upstream_timeout, 0);

  2825.     ngx_conf_merge_size_value(conf->upstream.send_lowat,
  2826.                               prev->upstream.send_lowat, 0);

  2827.     ngx_conf_merge_size_value(conf->upstream.buffer_size,
  2828.                               prev->upstream.buffer_size,
  2829.                               (size_t) ngx_pagesize);

  2830.     ngx_conf_merge_ptr_value(conf->upstream.limit_rate,
  2831.                               prev->upstream.limit_rate, NULL);

  2832.     ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
  2833.                               8, ngx_pagesize);

  2834.     if (conf->upstream.bufs.num < 2) {
  2835.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  2836.                            "there must be at least 2 \"proxy_buffers\"");
  2837.         return NGX_CONF_ERROR;
  2838.     }


  2839.     size = conf->upstream.buffer_size;
  2840.     if (size < conf->upstream.bufs.size) {
  2841.         size = conf->upstream.bufs.size;
  2842.     }


  2843.     ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
  2844.                               prev->upstream.busy_buffers_size_conf,
  2845.                               NGX_CONF_UNSET_SIZE);

  2846.     if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
  2847.         conf->upstream.busy_buffers_size = 2 * size;
  2848.     } else {
  2849.         conf->upstream.busy_buffers_size =
  2850.                                          conf->upstream.busy_buffers_size_conf;
  2851.     }

  2852.     if (conf->upstream.busy_buffers_size < size) {
  2853.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  2854.              "\"proxy_busy_buffers_size\" must be equal to or greater than "
  2855.              "the maximum of the value of \"proxy_buffer_size\" and "
  2856.              "one of the \"proxy_buffers\"");

  2857.         return NGX_CONF_ERROR;
  2858.     }

  2859.     if (conf->upstream.busy_buffers_size
  2860.         > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
  2861.     {
  2862.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  2863.              "\"proxy_busy_buffers_size\" must be less than "
  2864.              "the size of all \"proxy_buffers\" minus one buffer");

  2865.         return NGX_CONF_ERROR;
  2866.     }


  2867.     ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
  2868.                               prev->upstream.temp_file_write_size_conf,
  2869.                               NGX_CONF_UNSET_SIZE);

  2870.     if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
  2871.         conf->upstream.temp_file_write_size = 2 * size;
  2872.     } else {
  2873.         conf->upstream.temp_file_write_size =
  2874.                                       conf->upstream.temp_file_write_size_conf;
  2875.     }

  2876.     if (conf->upstream.temp_file_write_size < size) {
  2877.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  2878.              "\"proxy_temp_file_write_size\" must be equal to or greater "
  2879.              "than the maximum of the value of \"proxy_buffer_size\" and "
  2880.              "one of the \"proxy_buffers\"");

  2881.         return NGX_CONF_ERROR;
  2882.     }

  2883.     ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
  2884.                               prev->upstream.max_temp_file_size_conf,
  2885.                               NGX_CONF_UNSET_SIZE);

  2886.     if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
  2887.         conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
  2888.     } else {
  2889.         conf->upstream.max_temp_file_size =
  2890.                                         conf->upstream.max_temp_file_size_conf;
  2891.     }

  2892.     if (conf->upstream.max_temp_file_size != 0
  2893.         && conf->upstream.max_temp_file_size < size)
  2894.     {
  2895.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  2896.              "\"proxy_max_temp_file_size\" must be equal to zero to disable "
  2897.              "temporary files usage or must be equal to or greater than "
  2898.              "the maximum of the value of \"proxy_buffer_size\" and "
  2899.              "one of the \"proxy_buffers\"");

  2900.         return NGX_CONF_ERROR;
  2901.     }


  2902.     ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
  2903.                               prev->upstream.ignore_headers,
  2904.                               NGX_CONF_BITMASK_SET);


  2905.     ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
  2906.                               prev->upstream.next_upstream,
  2907.                               (NGX_CONF_BITMASK_SET
  2908.                                |NGX_HTTP_UPSTREAM_FT_ERROR
  2909.                                |NGX_HTTP_UPSTREAM_FT_TIMEOUT));

  2910.     if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
  2911.         conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
  2912.                                        |NGX_HTTP_UPSTREAM_FT_OFF;
  2913.     }

  2914.     if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
  2915.                               prev->upstream.temp_path,
  2916.                               &ngx_http_proxy_temp_path)
  2917.         != NGX_CONF_OK)
  2918.     {
  2919.         return NGX_CONF_ERROR;
  2920.     }


  2921. #if (NGX_HTTP_CACHE)

  2922.     if (conf->upstream.cache == NGX_CONF_UNSET) {
  2923.         ngx_conf_merge_value(conf->upstream.cache,
  2924.                               prev->upstream.cache, 0);

  2925.         conf->upstream.cache_zone = prev->upstream.cache_zone;
  2926.         conf->upstream.cache_value = prev->upstream.cache_value;
  2927.     }

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

  2930.         shm_zone = conf->upstream.cache_zone;

  2931.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  2932.                            "\"proxy_cache\" zone \"%V\" is unknown",
  2933.                            &shm_zone->shm.name);

  2934.         return NGX_CONF_ERROR;
  2935.     }

  2936.     ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
  2937.                               prev->upstream.cache_min_uses, 1);

  2938.     ngx_conf_merge_off_value(conf->upstream.cache_max_range_offset,
  2939.                               prev->upstream.cache_max_range_offset,
  2940.                               NGX_MAX_OFF_T_VALUE);

  2941.     ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
  2942.                               prev->upstream.cache_use_stale,
  2943.                               (NGX_CONF_BITMASK_SET
  2944.                                |NGX_HTTP_UPSTREAM_FT_OFF));

  2945.     if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
  2946.         conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
  2947.                                          |NGX_HTTP_UPSTREAM_FT_OFF;
  2948.     }

  2949.     if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
  2950.         conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
  2951.     }

  2952.     if (conf->upstream.cache_methods == 0) {
  2953.         conf->upstream.cache_methods = prev->upstream.cache_methods;
  2954.     }

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

  2956.     ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
  2957.                              prev->upstream.cache_bypass, NULL);

  2958.     ngx_conf_merge_ptr_value(conf->upstream.no_cache,
  2959.                              prev->upstream.no_cache, NULL);

  2960.     ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
  2961.                              prev->upstream.cache_valid, NULL);

  2962.     if (conf->cache_key.value.data == NULL) {
  2963.         conf->cache_key = prev->cache_key;
  2964.     }

  2965.     ngx_conf_merge_value(conf->upstream.cache_lock,
  2966.                               prev->upstream.cache_lock, 0);

  2967.     ngx_conf_merge_msec_value(conf->upstream.cache_lock_timeout,
  2968.                               prev->upstream.cache_lock_timeout, 5000);

  2969.     ngx_conf_merge_msec_value(conf->upstream.cache_lock_age,
  2970.                               prev->upstream.cache_lock_age, 5000);

  2971.     ngx_conf_merge_value(conf->upstream.cache_revalidate,
  2972.                               prev->upstream.cache_revalidate, 0);

  2973.     ngx_conf_merge_value(conf->upstream.cache_convert_head,
  2974.                               prev->upstream.cache_convert_head, 1);

  2975.     ngx_conf_merge_value(conf->upstream.cache_background_update,
  2976.                               prev->upstream.cache_background_update, 0);

  2977. #endif

  2978.     ngx_conf_merge_value(conf->upstream.pass_request_headers,
  2979.                               prev->upstream.pass_request_headers, 1);
  2980.     ngx_conf_merge_value(conf->upstream.pass_request_body,
  2981.                               prev->upstream.pass_request_body, 1);

  2982.     ngx_conf_merge_value(conf->upstream.pass_trailers,
  2983.                               prev->upstream.pass_trailers, 0);

  2984.     ngx_conf_merge_value(conf->upstream.intercept_errors,
  2985.                               prev->upstream.intercept_errors, 0);

  2986. #if (NGX_HTTP_SSL)

  2987.     if (ngx_http_proxy_merge_ssl(cf, conf, prev) != NGX_OK) {
  2988.         return NGX_CONF_ERROR;
  2989.     }

  2990.     ngx_conf_merge_value(conf->upstream.ssl_session_reuse,
  2991.                               prev->upstream.ssl_session_reuse, 1);

  2992.     ngx_conf_merge_bitmask_value(conf->ssl_protocols, prev->ssl_protocols,
  2993.                               (NGX_CONF_BITMASK_SET|NGX_SSL_DEFAULT_PROTOCOLS));

  2994.     ngx_conf_merge_str_value(conf->ssl_ciphers, prev->ssl_ciphers,
  2995.                              "DEFAULT");

  2996.     ngx_conf_merge_ptr_value(conf->upstream.ssl_name,
  2997.                               prev->upstream.ssl_name, NULL);
  2998.     ngx_conf_merge_value(conf->upstream.ssl_server_name,
  2999.                               prev->upstream.ssl_server_name, 0);
  3000.     ngx_conf_merge_value(conf->upstream.ssl_verify,
  3001.                               prev->upstream.ssl_verify, 0);
  3002.     ngx_conf_merge_uint_value(conf->ssl_verify_depth,
  3003.                               prev->ssl_verify_depth, 1);
  3004.     ngx_conf_merge_str_value(conf->ssl_trusted_certificate,
  3005.                               prev->ssl_trusted_certificate, "");
  3006.     ngx_conf_merge_str_value(conf->ssl_crl, prev->ssl_crl, "");

  3007.     ngx_conf_merge_ptr_value(conf->upstream.ssl_certificate,
  3008.                               prev->upstream.ssl_certificate, NULL);
  3009.     ngx_conf_merge_ptr_value(conf->upstream.ssl_certificate_key,
  3010.                               prev->upstream.ssl_certificate_key, NULL);
  3011.     ngx_conf_merge_ptr_value(conf->upstream.ssl_certificate_cache,
  3012.                               prev->upstream.ssl_certificate_cache, NULL);

  3013.     if (ngx_http_upstream_merge_ssl_passwords(cf, &conf->upstream,
  3014.                                               &prev->upstream)
  3015.         != NGX_OK)
  3016.     {
  3017.         return NGX_CONF_ERROR;
  3018.     }

  3019.     ngx_conf_merge_ptr_value(conf->ssl_conf_commands,
  3020.                               prev->ssl_conf_commands, NULL);

  3021.     if (conf->ssl && ngx_http_proxy_set_ssl(cf, conf) != NGX_OK) {
  3022.         return NGX_CONF_ERROR;
  3023.     }

  3024. #endif

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

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

  3027.     if (conf->redirect) {

  3028.         if (conf->redirects == NULL) {
  3029.             conf->redirects = prev->redirects;
  3030.         }

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

  3032.             conf->redirects = ngx_array_create(cf->pool, 1,
  3033.                                              sizeof(ngx_http_proxy_rewrite_t));
  3034.             if (conf->redirects == NULL) {
  3035.                 return NGX_CONF_ERROR;
  3036.             }

  3037.             pr = ngx_array_push(conf->redirects);
  3038.             if (pr == NULL) {
  3039.                 return NGX_CONF_ERROR;
  3040.             }

  3041.             ngx_memzero(&pr->pattern.complex,
  3042.                         sizeof(ngx_http_complex_value_t));

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

  3044.             pr->handler = ngx_http_proxy_rewrite_complex_handler;

  3045.             if (conf->vars.uri.len) {
  3046.                 pr->pattern.complex.value = conf->url;
  3047.                 pr->replacement.value = conf->location;

  3048.             } else {
  3049.                 pr->pattern.complex.value.len = conf->url.len
  3050.                                                 + sizeof("/") - 1;

  3051.                 p = ngx_pnalloc(cf->pool, pr->pattern.complex.value.len);
  3052.                 if (p == NULL) {
  3053.                     return NGX_CONF_ERROR;
  3054.                 }

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

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

  3058.                 ngx_str_set(&pr->replacement.value, "/");
  3059.             }
  3060.         }
  3061.     }

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

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

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

  3065.     ngx_conf_merge_uint_value(conf->http_version, prev->http_version,
  3066.                               NGX_HTTP_VERSION_11);

  3067.     ngx_conf_merge_uint_value(conf->headers_hash_max_size,
  3068.                               prev->headers_hash_max_size, 512);

  3069.     ngx_conf_merge_uint_value(conf->headers_hash_bucket_size,
  3070.                               prev->headers_hash_bucket_size, 64);

  3071.     conf->headers_hash_bucket_size = ngx_align(conf->headers_hash_bucket_size,
  3072.                                                ngx_cacheline_size);

  3073.     hash.max_size = conf->headers_hash_max_size;
  3074.     hash.bucket_size = conf->headers_hash_bucket_size;
  3075.     hash.name = "proxy_headers_hash";

  3076.     if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
  3077.             &prev->upstream, ngx_http_proxy_hide_headers, &hash)
  3078.         != NGX_OK)
  3079.     {
  3080.         return NGX_CONF_ERROR;
  3081.     }

  3082.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

  3083.     if (clcf->noname
  3084.         && conf->upstream.upstream == NULL && conf->proxy_lengths == NULL)
  3085.     {
  3086.         conf->upstream.upstream = prev->upstream.upstream;
  3087.         conf->location = prev->location;
  3088.         conf->vars = prev->vars;

  3089.         conf->proxy_lengths = prev->proxy_lengths;
  3090.         conf->proxy_values = prev->proxy_values;

  3091. #if (NGX_HTTP_SSL)
  3092.         conf->ssl = prev->ssl;
  3093. #endif
  3094.     }

  3095.     if (clcf->lmt_excpt && clcf->handler == NULL
  3096.         && (conf->upstream.upstream || conf->proxy_lengths))
  3097.     {
  3098.         clcf->handler = ngx_http_proxy_handler;
  3099.     }

  3100.     if (conf->body_source.data == NULL) {
  3101.         conf->body_flushes = prev->body_flushes;
  3102.         conf->body_source = prev->body_source;
  3103.         conf->body_lengths = prev->body_lengths;
  3104.         conf->body_values = prev->body_values;
  3105.     }

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

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

  3108.         sc.cf = cf;
  3109.         sc.source = &conf->body_source;
  3110.         sc.flushes = &conf->body_flushes;
  3111.         sc.lengths = &conf->body_lengths;
  3112.         sc.values = &conf->body_values;
  3113.         sc.complete_lengths = 1;
  3114.         sc.complete_values = 1;

  3115.         if (ngx_http_script_compile(&sc) != NGX_OK) {
  3116.             return NGX_CONF_ERROR;
  3117.         }
  3118.     }

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

  3120.     if (conf->headers_source == prev->headers_source) {
  3121.         conf->headers = prev->headers;
  3122. #if (NGX_HTTP_CACHE)
  3123.         conf->headers_cache = prev->headers_cache;
  3124. #endif
  3125.         conf->host_value = prev->host_value;
  3126.     }

  3127.     rc = ngx_http_proxy_init_headers(cf, conf, &conf->headers,
  3128.                                      ngx_http_proxy_headers);
  3129.     if (rc != NGX_OK) {
  3130.         return NGX_CONF_ERROR;
  3131.     }

  3132. #if (NGX_HTTP_CACHE)

  3133.     if (conf->upstream.cache) {
  3134.         rc = ngx_http_proxy_init_headers(cf, conf, &conf->headers_cache,
  3135.                                          ngx_http_proxy_cache_headers);
  3136.         if (rc != NGX_OK) {
  3137.             return NGX_CONF_ERROR;
  3138.         }
  3139.     }

  3140. #endif

  3141.     /*
  3142.      * special handling to preserve conf->headers in the "http" section
  3143.      * to inherit it to all servers
  3144.      */

  3145.     if (prev->headers.hash.buckets == NULL
  3146.         && conf->headers_source == prev->headers_source)
  3147.     {
  3148.         prev->headers = conf->headers;
  3149. #if (NGX_HTTP_CACHE)
  3150.         prev->headers_cache = conf->headers_cache;
  3151. #endif
  3152.         prev->host_value = conf->host_value;
  3153.     }

  3154.     return NGX_CONF_OK;
  3155. }


  3156. static ngx_int_t
  3157. ngx_http_proxy_init_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
  3158.     ngx_http_proxy_headers_t *headers, ngx_keyval_t *default_headers)
  3159. {
  3160.     u_char                            *p;
  3161.     size_t                             size;
  3162.     uintptr_t                         *code;
  3163.     ngx_uint_t                         i;
  3164.     ngx_array_t                        headers_names, headers_merged;
  3165.     ngx_keyval_t                      *src, *s, *h;
  3166.     ngx_hash_key_t                    *hk;
  3167.     ngx_hash_init_t                    hash;
  3168.     ngx_http_script_compile_t          sc;
  3169.     ngx_http_script_copy_code_t       *copy;
  3170.     ngx_http_compile_complex_value_t   ccv;

  3171.     if (headers->hash.buckets) {
  3172.         return NGX_OK;
  3173.     }

  3174.     if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
  3175.         != NGX_OK)
  3176.     {
  3177.         return NGX_ERROR;
  3178.     }

  3179.     if (ngx_array_init(&headers_merged, cf->temp_pool, 4, sizeof(ngx_keyval_t))
  3180.         != NGX_OK)
  3181.     {
  3182.         return NGX_ERROR;
  3183.     }

  3184.     headers->lengths = ngx_array_create(cf->pool, 64, 1);
  3185.     if (headers->lengths == NULL) {
  3186.         return NGX_ERROR;
  3187.     }

  3188.     headers->values = ngx_array_create(cf->pool, 512, 1);
  3189.     if (headers->values == NULL) {
  3190.         return NGX_ERROR;
  3191.     }

  3192.     if (conf->headers_source) {

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

  3195.             if (src[i].key.len == 4
  3196.                 && ngx_strncasecmp(src[i].key.data, (u_char *) "Host", 4) == 0)
  3197.             {
  3198.                 conf->host_value = ngx_pcalloc(cf->pool,
  3199.                                              sizeof(ngx_http_complex_value_t));
  3200.                 if (conf->host_value == NULL) {
  3201.                     return NGX_ERROR;
  3202.                 }

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

  3204.                 ccv.cf = cf;
  3205.                 ccv.value = &src[i].value;
  3206.                 ccv.complex_value = conf->host_value;

  3207.                 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3208.                     return NGX_ERROR;
  3209.                 }

  3210.                 continue;
  3211.             }

  3212.             s = ngx_array_push(&headers_merged);
  3213.             if (s == NULL) {
  3214.                 return NGX_ERROR;
  3215.             }

  3216.             *s = src[i];
  3217.         }
  3218.     }

  3219.     h = default_headers;

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

  3221.         src = headers_merged.elts;
  3222.         for (i = 0; i < headers_merged.nelts; i++) {
  3223.             if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
  3224.                 goto next;
  3225.             }
  3226.         }

  3227.         s = ngx_array_push(&headers_merged);
  3228.         if (s == NULL) {
  3229.             return NGX_ERROR;
  3230.         }

  3231.         *s = *h;

  3232.     next:

  3233.         h++;
  3234.     }


  3235.     src = headers_merged.elts;
  3236.     for (i = 0; i < headers_merged.nelts; i++) {

  3237.         hk = ngx_array_push(&headers_names);
  3238.         if (hk == NULL) {
  3239.             return NGX_ERROR;
  3240.         }

  3241.         hk->key = src[i].key;
  3242.         hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len);
  3243.         hk->value = (void *) 1;

  3244.         if (src[i].value.len == 0) {
  3245.             continue;
  3246.         }

  3247.         copy = ngx_array_push_n(headers->lengths,
  3248.                                 sizeof(ngx_http_script_copy_code_t));
  3249.         if (copy == NULL) {
  3250.             return NGX_ERROR;
  3251.         }

  3252.         copy->code = (ngx_http_script_code_pt) (void *)
  3253.                                                  ngx_http_script_copy_len_code;
  3254.         copy->len = src[i].key.len;

  3255.         size = (sizeof(ngx_http_script_copy_code_t)
  3256.                 + src[i].key.len + sizeof(uintptr_t) - 1)
  3257.                & ~(sizeof(uintptr_t) - 1);

  3258.         copy = ngx_array_push_n(headers->values, size);
  3259.         if (copy == NULL) {
  3260.             return NGX_ERROR;
  3261.         }

  3262.         copy->code = ngx_http_script_copy_code;
  3263.         copy->len = src[i].key.len;

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

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

  3267.         sc.cf = cf;
  3268.         sc.source = &src[i].value;
  3269.         sc.flushes = &headers->flushes;
  3270.         sc.lengths = &headers->lengths;
  3271.         sc.values = &headers->values;

  3272.         if (ngx_http_script_compile(&sc) != NGX_OK) {
  3273.             return NGX_ERROR;
  3274.         }

  3275.         code = ngx_array_push_n(headers->lengths, sizeof(uintptr_t));
  3276.         if (code == NULL) {
  3277.             return NGX_ERROR;
  3278.         }

  3279.         *code = (uintptr_t) NULL;

  3280.         code = ngx_array_push_n(headers->values, sizeof(uintptr_t));
  3281.         if (code == NULL) {
  3282.             return NGX_ERROR;
  3283.         }

  3284.         *code = (uintptr_t) NULL;
  3285.     }

  3286.     code = ngx_array_push_n(headers->lengths, sizeof(uintptr_t));
  3287.     if (code == NULL) {
  3288.         return NGX_ERROR;
  3289.     }

  3290.     *code = (uintptr_t) NULL;


  3291.     hash.hash = &headers->hash;
  3292.     hash.key = ngx_hash_key_lc;
  3293.     hash.max_size = conf->headers_hash_max_size;
  3294.     hash.bucket_size = conf->headers_hash_bucket_size;
  3295.     hash.name = "proxy_headers_hash";
  3296.     hash.pool = cf->pool;
  3297.     hash.temp_pool = NULL;

  3298.     return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts);
  3299. }


  3300. static char *
  3301. ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3302. {
  3303.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3304.     size_t                      add;
  3305.     u_short                     port;
  3306.     ngx_str_t                  *value, *url;
  3307.     ngx_url_t                   u;
  3308.     ngx_uint_t                  n;
  3309.     ngx_http_core_loc_conf_t   *clcf;
  3310.     ngx_http_script_compile_t   sc;

  3311.     if (plcf->upstream.upstream || plcf->proxy_lengths) {
  3312.         return "is duplicate";
  3313.     }

  3314.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

  3315.     clcf->handler = ngx_http_proxy_handler;

  3316.     if (clcf->name.len && clcf->name.data[clcf->name.len - 1] == '/') {
  3317.         clcf->auto_redirect = 1;
  3318.     }

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

  3320.     url = &value[1];

  3321.     n = ngx_http_script_variables_count(url);

  3322.     if (n) {

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

  3324.         sc.cf = cf;
  3325.         sc.source = url;
  3326.         sc.lengths = &plcf->proxy_lengths;
  3327.         sc.values = &plcf->proxy_values;
  3328.         sc.variables = n;
  3329.         sc.complete_lengths = 1;
  3330.         sc.complete_values = 1;

  3331.         if (ngx_http_script_compile(&sc) != NGX_OK) {
  3332.             return NGX_CONF_ERROR;
  3333.         }

  3334. #if (NGX_HTTP_SSL)
  3335.         plcf->ssl = 1;
  3336. #endif

  3337.         return NGX_CONF_OK;
  3338.     }

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

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

  3343. #if (NGX_HTTP_SSL)
  3344.         plcf->ssl = 1;

  3345.         add = 8;
  3346.         port = 443;
  3347. #else
  3348.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3349.                            "https protocol requires SSL support");
  3350.         return NGX_CONF_ERROR;
  3351. #endif

  3352.     } else {
  3353.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
  3354.         return NGX_CONF_ERROR;
  3355.     }

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

  3357.     u.url.len = url->len - add;
  3358.     u.url.data = url->data + add;
  3359.     u.default_port = port;
  3360.     u.uri_part = 1;
  3361.     u.no_resolve = 1;

  3362.     plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
  3363.     if (plcf->upstream.upstream == NULL) {
  3364.         return NGX_CONF_ERROR;
  3365.     }

  3366.     plcf->vars.schema.len = add;
  3367.     plcf->vars.schema.data = url->data;
  3368.     plcf->vars.key_start = plcf->vars.schema;

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

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

  3371.     if (clcf->named
  3372. #if (NGX_PCRE)
  3373.         || clcf->regex
  3374. #endif
  3375.         || clcf->noname)
  3376.     {
  3377.         if (plcf->vars.uri.len) {
  3378.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3379.                                "\"proxy_pass\" cannot have URI part in "
  3380.                                "location given by regular expression, "
  3381.                                "or inside named location, "
  3382.                                "or inside \"if\" statement, "
  3383.                                "or inside \"limit_except\" block");
  3384.             return NGX_CONF_ERROR;
  3385.         }

  3386.         plcf->location.len = 0;
  3387.     }

  3388.     plcf->url = *url;

  3389.     return NGX_CONF_OK;
  3390. }


  3391. static char *
  3392. ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3393. {
  3394.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3395.     u_char                            *p;
  3396.     ngx_str_t                         *value;
  3397.     ngx_http_proxy_rewrite_t          *pr;
  3398.     ngx_http_compile_complex_value_t   ccv;

  3399.     if (plcf->redirect == 0) {
  3400.         return "is duplicate";
  3401.     }

  3402.     plcf->redirect = 1;

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

  3404.     if (cf->args->nelts == 2) {
  3405.         if (ngx_strcmp(value[1].data, "off") == 0) {

  3406.             if (plcf->redirects) {
  3407.                 return "is duplicate";
  3408.             }

  3409.             plcf->redirect = 0;
  3410.             return NGX_CONF_OK;
  3411.         }

  3412.         if (ngx_strcmp(value[1].data, "default") != 0) {
  3413.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3414.                                "invalid parameter \"%V\"", &value[1]);
  3415.             return NGX_CONF_ERROR;
  3416.         }
  3417.     }

  3418.     if (plcf->redirects == NULL) {
  3419.         plcf->redirects = ngx_array_create(cf->pool, 1,
  3420.                                            sizeof(ngx_http_proxy_rewrite_t));
  3421.         if (plcf->redirects == NULL) {
  3422.             return NGX_CONF_ERROR;
  3423.         }
  3424.     }

  3425.     pr = ngx_array_push(plcf->redirects);
  3426.     if (pr == NULL) {
  3427.         return NGX_CONF_ERROR;
  3428.     }

  3429.     if (cf->args->nelts == 2
  3430.         && ngx_strcmp(value[1].data, "default") == 0)
  3431.     {
  3432.         if (plcf->proxy_lengths) {
  3433.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3434.                                "\"proxy_redirect default\" cannot be used "
  3435.                                "with \"proxy_pass\" directive with variables");
  3436.             return NGX_CONF_ERROR;
  3437.         }

  3438.         if (plcf->url.data == NULL) {
  3439.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3440.                                "\"proxy_redirect default\" should be placed "
  3441.                                "after the \"proxy_pass\" directive");
  3442.             return NGX_CONF_ERROR;
  3443.         }

  3444.         pr->handler = ngx_http_proxy_rewrite_complex_handler;

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

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

  3447.         if (plcf->vars.uri.len) {
  3448.             pr->pattern.complex.value = plcf->url;
  3449.             pr->replacement.value = plcf->location;

  3450.         } else {
  3451.             pr->pattern.complex.value.len = plcf->url.len + sizeof("/") - 1;

  3452.             p = ngx_pnalloc(cf->pool, pr->pattern.complex.value.len);
  3453.             if (p == NULL) {
  3454.                 return NGX_CONF_ERROR;
  3455.             }

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

  3457.             p = ngx_cpymem(p, plcf->url.data, plcf->url.len);
  3458.             *p = '/';

  3459.             ngx_str_set(&pr->replacement.value, "/");
  3460.         }

  3461.         return NGX_CONF_OK;
  3462.     }


  3463.     if (value[1].data[0] == '~') {
  3464.         value[1].len--;
  3465.         value[1].data++;

  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 (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 0) != NGX_OK) {
  3474.                 return NGX_CONF_ERROR;
  3475.             }
  3476.         }

  3477.     } else {

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

  3479.         ccv.cf = cf;
  3480.         ccv.value = &value[1];
  3481.         ccv.complex_value = &pr->pattern.complex;

  3482.         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3483.             return NGX_CONF_ERROR;
  3484.         }

  3485.         pr->handler = ngx_http_proxy_rewrite_complex_handler;
  3486.     }


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

  3488.     ccv.cf = cf;
  3489.     ccv.value = &value[2];
  3490.     ccv.complex_value = &pr->replacement;

  3491.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3492.         return NGX_CONF_ERROR;
  3493.     }

  3494.     return NGX_CONF_OK;
  3495. }


  3496. static char *
  3497. ngx_http_proxy_cookie_domain(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3498. {
  3499.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3500.     ngx_str_t                         *value;
  3501.     ngx_http_proxy_rewrite_t          *pr;
  3502.     ngx_http_compile_complex_value_t   ccv;

  3503.     if (plcf->cookie_domains == NULL) {
  3504.         return "is duplicate";
  3505.     }

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

  3507.     if (cf->args->nelts == 2) {

  3508.         if (ngx_strcmp(value[1].data, "off") == 0) {

  3509.             if (plcf->cookie_domains != NGX_CONF_UNSET_PTR) {
  3510.                 return "is duplicate";
  3511.             }

  3512.             plcf->cookie_domains = NULL;
  3513.             return NGX_CONF_OK;
  3514.         }

  3515.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3516.                            "invalid parameter \"%V\"", &value[1]);
  3517.         return NGX_CONF_ERROR;
  3518.     }

  3519.     if (plcf->cookie_domains == NGX_CONF_UNSET_PTR) {
  3520.         plcf->cookie_domains = ngx_array_create(cf->pool, 1,
  3521.                                      sizeof(ngx_http_proxy_rewrite_t));
  3522.         if (plcf->cookie_domains == NULL) {
  3523.             return NGX_CONF_ERROR;
  3524.         }
  3525.     }

  3526.     pr = ngx_array_push(plcf->cookie_domains);
  3527.     if (pr == NULL) {
  3528.         return NGX_CONF_ERROR;
  3529.     }

  3530.     if (value[1].data[0] == '~') {
  3531.         value[1].len--;
  3532.         value[1].data++;

  3533.         if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
  3534.             return NGX_CONF_ERROR;
  3535.         }

  3536.     } else {

  3537.         if (value[1].data[0] == '.') {
  3538.             value[1].len--;
  3539.             value[1].data++;
  3540.         }

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

  3542.         ccv.cf = cf;
  3543.         ccv.value = &value[1];
  3544.         ccv.complex_value = &pr->pattern.complex;

  3545.         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3546.             return NGX_CONF_ERROR;
  3547.         }

  3548.         pr->handler = ngx_http_proxy_rewrite_domain_handler;

  3549.         if (value[2].data[0] == '.') {
  3550.             value[2].len--;
  3551.             value[2].data++;
  3552.         }
  3553.     }

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

  3555.     ccv.cf = cf;
  3556.     ccv.value = &value[2];
  3557.     ccv.complex_value = &pr->replacement;

  3558.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3559.         return NGX_CONF_ERROR;
  3560.     }

  3561.     return NGX_CONF_OK;
  3562. }


  3563. static char *
  3564. ngx_http_proxy_cookie_path(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3565. {
  3566.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3567.     ngx_str_t                         *value;
  3568.     ngx_http_proxy_rewrite_t          *pr;
  3569.     ngx_http_compile_complex_value_t   ccv;

  3570.     if (plcf->cookie_paths == NULL) {
  3571.         return "is duplicate";
  3572.     }

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

  3574.     if (cf->args->nelts == 2) {

  3575.         if (ngx_strcmp(value[1].data, "off") == 0) {

  3576.             if (plcf->cookie_paths != NGX_CONF_UNSET_PTR) {
  3577.                 return "is duplicate";
  3578.             }

  3579.             plcf->cookie_paths = NULL;
  3580.             return NGX_CONF_OK;
  3581.         }

  3582.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3583.                            "invalid parameter \"%V\"", &value[1]);
  3584.         return NGX_CONF_ERROR;
  3585.     }

  3586.     if (plcf->cookie_paths == NGX_CONF_UNSET_PTR) {
  3587.         plcf->cookie_paths = ngx_array_create(cf->pool, 1,
  3588.                                      sizeof(ngx_http_proxy_rewrite_t));
  3589.         if (plcf->cookie_paths == NULL) {
  3590.             return NGX_CONF_ERROR;
  3591.         }
  3592.     }

  3593.     pr = ngx_array_push(plcf->cookie_paths);
  3594.     if (pr == NULL) {
  3595.         return NGX_CONF_ERROR;
  3596.     }

  3597.     if (value[1].data[0] == '~') {
  3598.         value[1].len--;
  3599.         value[1].data++;

  3600.         if (value[1].data[0] == '*') {
  3601.             value[1].len--;
  3602.             value[1].data++;

  3603.             if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
  3604.                 return NGX_CONF_ERROR;
  3605.             }

  3606.         } else {
  3607.             if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 0) != NGX_OK) {
  3608.                 return NGX_CONF_ERROR;
  3609.             }
  3610.         }

  3611.     } else {

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

  3613.         ccv.cf = cf;
  3614.         ccv.value = &value[1];
  3615.         ccv.complex_value = &pr->pattern.complex;

  3616.         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3617.             return NGX_CONF_ERROR;
  3618.         }

  3619.         pr->handler = ngx_http_proxy_rewrite_complex_handler;
  3620.     }

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

  3622.     ccv.cf = cf;
  3623.     ccv.value = &value[2];
  3624.     ccv.complex_value = &pr->replacement;

  3625.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3626.         return NGX_CONF_ERROR;
  3627.     }

  3628.     return NGX_CONF_OK;
  3629. }


  3630. static char *
  3631. ngx_http_proxy_cookie_flags(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3632. {
  3633.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3634.     ngx_str_t                         *value;
  3635.     ngx_uint_t                         i;
  3636.     ngx_http_complex_value_t          *cv;
  3637.     ngx_http_proxy_cookie_flags_t     *pcf;
  3638.     ngx_http_compile_complex_value_t   ccv;
  3639. #if (NGX_PCRE)
  3640.     ngx_regex_compile_t                rc;
  3641.     u_char                             errstr[NGX_MAX_CONF_ERRSTR];
  3642. #endif

  3643.     if (plcf->cookie_flags == NULL) {
  3644.         return "is duplicate";
  3645.     }

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

  3647.     if (cf->args->nelts == 2) {

  3648.         if (ngx_strcmp(value[1].data, "off") == 0) {

  3649.             if (plcf->cookie_flags != NGX_CONF_UNSET_PTR) {
  3650.                 return "is duplicate";
  3651.             }

  3652.             plcf->cookie_flags = NULL;
  3653.             return NGX_CONF_OK;
  3654.         }

  3655.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3656.                            "invalid parameter \"%V\"", &value[1]);
  3657.         return NGX_CONF_ERROR;
  3658.     }

  3659.     if (plcf->cookie_flags == NGX_CONF_UNSET_PTR) {
  3660.         plcf->cookie_flags = ngx_array_create(cf->pool, 1,
  3661.                                         sizeof(ngx_http_proxy_cookie_flags_t));
  3662.         if (plcf->cookie_flags == NULL) {
  3663.             return NGX_CONF_ERROR;
  3664.         }
  3665.     }

  3666.     pcf = ngx_array_push(plcf->cookie_flags);
  3667.     if (pcf == NULL) {
  3668.         return NGX_CONF_ERROR;
  3669.     }

  3670.     pcf->regex = 0;

  3671.     if (value[1].data[0] == '~') {
  3672.         value[1].len--;
  3673.         value[1].data++;

  3674. #if (NGX_PCRE)
  3675.         ngx_memzero(&rc, sizeof(ngx_regex_compile_t));

  3676.         rc.pattern = value[1];
  3677.         rc.err.len = NGX_MAX_CONF_ERRSTR;
  3678.         rc.err.data = errstr;
  3679.         rc.options = NGX_REGEX_CASELESS;

  3680.         pcf->cookie.regex = ngx_http_regex_compile(cf, &rc);
  3681.         if (pcf->cookie.regex == NULL) {
  3682.             return NGX_CONF_ERROR;
  3683.         }

  3684.         pcf->regex = 1;
  3685. #else
  3686.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3687.                            "using regex \"%V\" requires PCRE library",
  3688.                            &value[1]);
  3689.         return NGX_CONF_ERROR;
  3690. #endif

  3691.     } else {

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

  3693.         ccv.cf = cf;
  3694.         ccv.value = &value[1];
  3695.         ccv.complex_value = &pcf->cookie.complex;

  3696.         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3697.             return NGX_CONF_ERROR;
  3698.         }
  3699.     }

  3700.     if (ngx_array_init(&pcf->flags_values, cf->pool, cf->args->nelts - 2,
  3701.                        sizeof(ngx_http_complex_value_t))
  3702.         != NGX_OK)
  3703.     {
  3704.         return NGX_CONF_ERROR;
  3705.     }

  3706.     for (i = 2; i < cf->args->nelts; i++) {

  3707.         cv = ngx_array_push(&pcf->flags_values);
  3708.         if (cv == NULL) {
  3709.             return NGX_CONF_ERROR;
  3710.         }

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

  3712.         ccv.cf = cf;
  3713.         ccv.value = &value[i];
  3714.         ccv.complex_value = cv;

  3715.         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3716.             return NGX_CONF_ERROR;
  3717.         }
  3718.     }

  3719.     return NGX_CONF_OK;
  3720. }


  3721. static ngx_int_t
  3722. ngx_http_proxy_rewrite_regex(ngx_conf_t *cf, ngx_http_proxy_rewrite_t *pr,
  3723.     ngx_str_t *regex, ngx_uint_t caseless)
  3724. {
  3725. #if (NGX_PCRE)
  3726.     u_char               errstr[NGX_MAX_CONF_ERRSTR];
  3727.     ngx_regex_compile_t  rc;

  3728.     ngx_memzero(&rc, sizeof(ngx_regex_compile_t));

  3729.     rc.pattern = *regex;
  3730.     rc.err.len = NGX_MAX_CONF_ERRSTR;
  3731.     rc.err.data = errstr;

  3732.     if (caseless) {
  3733.         rc.options = NGX_REGEX_CASELESS;
  3734.     }

  3735.     pr->pattern.regex = ngx_http_regex_compile(cf, &rc);
  3736.     if (pr->pattern.regex == NULL) {
  3737.         return NGX_ERROR;
  3738.     }

  3739.     pr->handler = ngx_http_proxy_rewrite_regex_handler;

  3740.     return NGX_OK;

  3741. #else

  3742.     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3743.                        "using regex \"%V\" requires PCRE library", regex);
  3744.     return NGX_ERROR;

  3745. #endif
  3746. }


  3747. static char *
  3748. ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3749. {
  3750.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3751.     ngx_str_t                  *value;
  3752.     ngx_http_script_compile_t   sc;

  3753.     if (plcf->upstream.store != NGX_CONF_UNSET) {
  3754.         return "is duplicate";
  3755.     }

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

  3757.     if (ngx_strcmp(value[1].data, "off") == 0) {
  3758.         plcf->upstream.store = 0;
  3759.         return NGX_CONF_OK;
  3760.     }

  3761.     if (value[1].len == 0) {
  3762.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "empty path");
  3763.         return NGX_CONF_ERROR;
  3764.     }

  3765. #if (NGX_HTTP_CACHE)
  3766.     if (plcf->upstream.cache > 0) {
  3767.         return "is incompatible with \"proxy_cache\"";
  3768.     }
  3769. #endif

  3770.     plcf->upstream.store = 1;

  3771.     if (ngx_strcmp(value[1].data, "on") == 0) {
  3772.         return NGX_CONF_OK;
  3773.     }

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

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

  3777.     sc.cf = cf;
  3778.     sc.source = &value[1];
  3779.     sc.lengths = &plcf->upstream.store_lengths;
  3780.     sc.values = &plcf->upstream.store_values;
  3781.     sc.variables = ngx_http_script_variables_count(&value[1]);
  3782.     sc.complete_lengths = 1;
  3783.     sc.complete_values = 1;

  3784.     if (ngx_http_script_compile(&sc) != NGX_OK) {
  3785.         return NGX_CONF_ERROR;
  3786.     }

  3787.     return NGX_CONF_OK;
  3788. }


  3789. #if (NGX_HTTP_CACHE)

  3790. static char *
  3791. ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3792. {
  3793.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3794.     ngx_str_t                         *value;
  3795.     ngx_http_complex_value_t           cv;
  3796.     ngx_http_compile_complex_value_t   ccv;

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

  3798.     if (plcf->upstream.cache != NGX_CONF_UNSET) {
  3799.         return "is duplicate";
  3800.     }

  3801.     if (ngx_strcmp(value[1].data, "off") == 0) {
  3802.         plcf->upstream.cache = 0;
  3803.         return NGX_CONF_OK;
  3804.     }

  3805.     if (plcf->upstream.store > 0) {
  3806.         return "is incompatible with \"proxy_store\"";
  3807.     }

  3808.     plcf->upstream.cache = 1;

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

  3810.     ccv.cf = cf;
  3811.     ccv.value = &value[1];
  3812.     ccv.complex_value = &cv;

  3813.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3814.         return NGX_CONF_ERROR;
  3815.     }

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

  3817.         plcf->upstream.cache_value = ngx_palloc(cf->pool,
  3818.                                              sizeof(ngx_http_complex_value_t));
  3819.         if (plcf->upstream.cache_value == NULL) {
  3820.             return NGX_CONF_ERROR;
  3821.         }

  3822.         *plcf->upstream.cache_value = cv;

  3823.         return NGX_CONF_OK;
  3824.     }

  3825.     plcf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0,
  3826.                                                       &ngx_http_proxy_module);
  3827.     if (plcf->upstream.cache_zone == NULL) {
  3828.         return NGX_CONF_ERROR;
  3829.     }

  3830.     return NGX_CONF_OK;
  3831. }


  3832. static char *
  3833. ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3834. {
  3835.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3836.     ngx_str_t                         *value;
  3837.     ngx_http_compile_complex_value_t   ccv;

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

  3839.     if (plcf->cache_key.value.data) {
  3840.         return "is duplicate";
  3841.     }

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

  3843.     ccv.cf = cf;
  3844.     ccv.value = &value[1];
  3845.     ccv.complex_value = &plcf->cache_key;

  3846.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  3847.         return NGX_CONF_ERROR;
  3848.     }

  3849.     return NGX_CONF_OK;
  3850. }

  3851. #endif


  3852. #if (NGX_HTTP_SSL)

  3853. static char *
  3854. ngx_http_proxy_ssl_certificate_cache(ngx_conf_t *cf, ngx_command_t *cmd,
  3855.     void *conf)
  3856. {
  3857.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3858.     time_t       inactive, valid;
  3859.     ngx_str_t   *value, s;
  3860.     ngx_int_t    max;
  3861.     ngx_uint_t   i;

  3862.     if (plcf->upstream.ssl_certificate_cache != NGX_CONF_UNSET_PTR) {
  3863.         return "is duplicate";
  3864.     }

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

  3866.     max = 0;
  3867.     inactive = 10;
  3868.     valid = 60;

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

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

  3871.             max = ngx_atoi(value[i].data + 4, value[i].len - 4);
  3872.             if (max <= 0) {
  3873.                 goto failed;
  3874.             }

  3875.             continue;
  3876.         }

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

  3878.             s.len = value[i].len - 9;
  3879.             s.data = value[i].data + 9;

  3880.             inactive = ngx_parse_time(&s, 1);
  3881.             if (inactive == (time_t) NGX_ERROR) {
  3882.                 goto failed;
  3883.             }

  3884.             continue;
  3885.         }

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

  3887.             s.len = value[i].len - 6;
  3888.             s.data = value[i].data + 6;

  3889.             valid = ngx_parse_time(&s, 1);
  3890.             if (valid == (time_t) NGX_ERROR) {
  3891.                 goto failed;
  3892.             }

  3893.             continue;
  3894.         }

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

  3896.             plcf->upstream.ssl_certificate_cache = NULL;

  3897.             continue;
  3898.         }

  3899.     failed:

  3900.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3901.                            "invalid parameter \"%V\"", &value[i]);
  3902.         return NGX_CONF_ERROR;
  3903.     }

  3904.     if (plcf->upstream.ssl_certificate_cache == NULL) {
  3905.         return NGX_CONF_OK;
  3906.     }

  3907.     if (max == 0) {
  3908.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3909.                            "\"proxy_ssl_certificate_cache\" must have "
  3910.                            "the \"max\" parameter");
  3911.         return NGX_CONF_ERROR;
  3912.     }

  3913.     plcf->upstream.ssl_certificate_cache = ngx_ssl_cache_init(cf->pool, max,
  3914.                                                               valid, inactive);
  3915.     if (plcf->upstream.ssl_certificate_cache == NULL) {
  3916.         return NGX_CONF_ERROR;
  3917.     }

  3918.     return NGX_CONF_OK;
  3919. }


  3920. static char *
  3921. ngx_http_proxy_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3922. {
  3923.     ngx_http_proxy_loc_conf_t *plcf = conf;

  3924.     ngx_str_t  *value;

  3925.     if (plcf->upstream.ssl_passwords != NGX_CONF_UNSET_PTR) {
  3926.         return "is duplicate";
  3927.     }

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

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

  3930.     if (plcf->upstream.ssl_passwords == NULL) {
  3931.         return NGX_CONF_ERROR;
  3932.     }

  3933.     return NGX_CONF_OK;
  3934. }

  3935. #endif


  3936. static char *
  3937. ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
  3938. {
  3939. #if (NGX_FREEBSD)
  3940.     ssize_t *np = data;

  3941.     if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
  3942.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  3943.                            "\"proxy_send_lowat\" must be less than %d "
  3944.                            "(sysctl net.inet.tcp.sendspace)",
  3945.                            ngx_freebsd_net_inet_tcp_sendspace);

  3946.         return NGX_CONF_ERROR;
  3947.     }

  3948. #elif !(NGX_HAVE_SO_SNDLOWAT)
  3949.     ssize_t *np = data;

  3950.     ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
  3951.                        "\"proxy_send_lowat\" is not supported, ignored");

  3952.     *np = 0;

  3953. #endif

  3954.     return NGX_CONF_OK;
  3955. }


  3956. #if (NGX_HTTP_SSL)

  3957. static char *
  3958. ngx_http_proxy_ssl_conf_command_check(ngx_conf_t *cf, void *post, void *data)
  3959. {
  3960. #ifndef SSL_CONF_FLAG_FILE
  3961.     return "is not supported on this platform";
  3962. #else
  3963.     return NGX_CONF_OK;
  3964. #endif
  3965. }


  3966. static ngx_int_t
  3967. ngx_http_proxy_merge_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
  3968.     ngx_http_proxy_loc_conf_t *prev)
  3969. {
  3970.     ngx_uint_t  preserve;

  3971.     if (conf->ssl_protocols == 0
  3972.         && conf->ssl_ciphers.data == NULL
  3973.         && conf->upstream.ssl_certificate == NGX_CONF_UNSET_PTR
  3974.         && conf->upstream.ssl_certificate_key == NGX_CONF_UNSET_PTR
  3975.         && conf->upstream.ssl_passwords == NGX_CONF_UNSET_PTR
  3976.         && conf->upstream.ssl_verify == NGX_CONF_UNSET
  3977.         && conf->ssl_verify_depth == NGX_CONF_UNSET_UINT
  3978.         && conf->ssl_trusted_certificate.data == NULL
  3979.         && conf->ssl_crl.data == NULL
  3980.         && conf->upstream.ssl_session_reuse == NGX_CONF_UNSET
  3981.         && conf->ssl_conf_commands == NGX_CONF_UNSET_PTR)
  3982.     {
  3983.         if (prev->upstream.ssl) {
  3984.             conf->upstream.ssl = prev->upstream.ssl;
  3985.             return NGX_OK;
  3986.         }

  3987.         preserve = 1;

  3988.     } else {
  3989.         preserve = 0;
  3990.     }

  3991.     conf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
  3992.     if (conf->upstream.ssl == NULL) {
  3993.         return NGX_ERROR;
  3994.     }

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

  3996.     /*
  3997.      * special handling to preserve conf->upstream.ssl
  3998.      * in the "http" section to inherit it to all servers
  3999.      */

  4000.     if (preserve) {
  4001.         prev->upstream.ssl = conf->upstream.ssl;
  4002.     }

  4003.     return NGX_OK;
  4004. }


  4005. static ngx_int_t
  4006. ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf)
  4007. {
  4008.     ngx_pool_cleanup_t  *cln;

  4009.     if (plcf->upstream.ssl->ctx) {
  4010.         return NGX_OK;
  4011.     }

  4012.     if (ngx_ssl_create(plcf->upstream.ssl, plcf->ssl_protocols, NULL)
  4013.         != NGX_OK)
  4014.     {
  4015.         return NGX_ERROR;
  4016.     }

  4017.     cln = ngx_pool_cleanup_add(cf->pool, 0);
  4018.     if (cln == NULL) {
  4019.         ngx_ssl_cleanup_ctx(plcf->upstream.ssl);
  4020.         return NGX_ERROR;
  4021.     }

  4022.     cln->handler = ngx_ssl_cleanup_ctx;
  4023.     cln->data = plcf->upstream.ssl;

  4024.     if (ngx_ssl_ciphers(cf, plcf->upstream.ssl, &plcf->ssl_ciphers, 0)
  4025.         != NGX_OK)
  4026.     {
  4027.         return NGX_ERROR;
  4028.     }

  4029.     if (plcf->upstream.ssl_certificate
  4030.         && plcf->upstream.ssl_certificate->value.len)
  4031.     {
  4032.         if (plcf->upstream.ssl_certificate_key == NULL) {
  4033.             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  4034.                           "no \"proxy_ssl_certificate_key\" is defined "
  4035.                           "for certificate \"%V\"",
  4036.                           &plcf->upstream.ssl_certificate->value);
  4037.             return NGX_ERROR;
  4038.         }

  4039.         if (plcf->upstream.ssl_certificate->lengths == NULL
  4040.             && plcf->upstream.ssl_certificate_key->lengths == NULL)
  4041.         {
  4042.             if (ngx_ssl_certificate(cf, plcf->upstream.ssl,
  4043.                                     &plcf->upstream.ssl_certificate->value,
  4044.                                     &plcf->upstream.ssl_certificate_key->value,
  4045.                                     plcf->upstream.ssl_passwords)
  4046.                 != NGX_OK)
  4047.             {
  4048.                 return NGX_ERROR;
  4049.             }
  4050.         }
  4051.     }

  4052.     if (plcf->upstream.ssl_verify) {
  4053.         if (plcf->ssl_trusted_certificate.len == 0) {
  4054.             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
  4055.                       "no proxy_ssl_trusted_certificate for proxy_ssl_verify");
  4056.             return NGX_ERROR;
  4057.         }

  4058.         if (ngx_ssl_trusted_certificate(cf, plcf->upstream.ssl,
  4059.                                         &plcf->ssl_trusted_certificate,
  4060.                                         plcf->ssl_verify_depth)
  4061.             != NGX_OK)
  4062.         {
  4063.             return NGX_ERROR;
  4064.         }

  4065.         if (ngx_ssl_crl(cf, plcf->upstream.ssl, &plcf->ssl_crl) != NGX_OK) {
  4066.             return NGX_ERROR;
  4067.         }
  4068.     }

  4069.     if (ngx_ssl_client_session_cache(cf, plcf->upstream.ssl,
  4070.                                      plcf->upstream.ssl_session_reuse)
  4071.         != NGX_OK)
  4072.     {
  4073.         return NGX_ERROR;
  4074.     }

  4075.     if (ngx_ssl_conf_commands(cf, plcf->upstream.ssl, plcf->ssl_conf_commands)
  4076.         != NGX_OK)
  4077.     {
  4078.         return NGX_ERROR;
  4079.     }

  4080.     return NGX_OK;
  4081. }

  4082. #endif


  4083. static void
  4084. ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v)
  4085. {
  4086.     if (u->family != AF_UNIX) {

  4087.         if (u->no_port || u->port == u->default_port) {

  4088.             v->host_header = u->host;

  4089.             if (u->default_port == 80) {
  4090.                 ngx_str_set(&v->port, "80");

  4091.             } else {
  4092.                 ngx_str_set(&v->port, "443");
  4093.             }

  4094.         } else {
  4095.             v->host_header.len = u->host.len + 1 + u->port_text.len;
  4096.             v->host_header.data = u->host.data;
  4097.             v->port = u->port_text;
  4098.         }

  4099.         v->key_start.len += v->host_header.len;

  4100.     } else {
  4101.         ngx_str_set(&v->host_header, "localhost");
  4102.         ngx_str_null(&v->port);
  4103.         v->key_start.len += sizeof("unix:") - 1 + u->host.len + 1;
  4104.     }

  4105.     v->uri = u->uri;
  4106. }