/*
 *  Phusion Passenger - https://www.phusionpassenger.com/
 *  Copyright (c) 2010-2015 Phusion
 *
 *  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */

/*
 * CacheLocationConfig.c is automatically generated from CacheLocationConfig.c.erb,
 * using definitions from lib/phusion_passenger/nginx/config_options.rb.
 * Edits to CacheLocationConfig.c will be lost.
 *
 * To update CacheLocationConfig.c:
 *   rake nginx
 *
 * To force regeneration of CacheLocationConfig.c:
 *   rm -f ext/nginx/CacheLocationConfig.c
 *   rake ext/nginx/CacheLocationConfig.c
 */

<%
require 'phusion_passenger/nginx/config_options'

def filter_eligible_options(options)
	return options.reject do |option|
		option[:alias_for] ||
			option.fetch(:field, true).nil? ||
			option[:field].to_s =~ /\./ ||
			!option.fetch(:header, true)
	end
end

def struct_field_for(option)
	if option.has_key?(:field)
		return option[:field]
	else
		return option[:name].sub(/^passenger_/, '')
	end
end

def header_name_for(option)
	return option[:header] || option[:name].upcase
end
%>

/* 0: NGX_ERROR, 1: OK */
int generated_cache_location_part(ngx_conf_t *cf, passenger_loc_conf_t *conf) {
	size_t len = 0;
	u_char int_buf[32], *end, *buf, *pos;

/* Calculate lengths */
<% for option in filter_eligible_options(LOCATION_CONFIGURATION_OPTIONS) %>
	<% if option[:type] == :string %>
		if (conf-><%= struct_field_for(option) %>.data != NULL) {
			len += sizeof("!~<%= header_name_for(option) %>: ") - 1;
			len += conf-><%= struct_field_for(option) %>.len;
			len += sizeof("\r\n") - 1;
		}
	<% elsif option[:type] == :integer %>
		if (conf-><%= struct_field_for(option) %> != NGX_CONF_UNSET) {
			end = ngx_snprintf(int_buf,
				sizeof(int_buf) - 1,
				"%d",
				conf-><%= struct_field_for(option) %>);
			len += sizeof("!~<%= header_name_for(option) %>: ") - 1;
			len += end - int_buf;
			len += sizeof("\r\n") - 1;
		}
	<% elsif option[:type] == :integer %>
		if (conf-><%= struct_field_for(option) %> != NGX_CONF_UNSET_UINT) {
			end = ngx_snprintf(int_buf,
				sizeof(int_buf) - 1,
				"%ui",
				conf-><%= struct_field_for(option) %>);
			len += sizeof("!~<%= header_name_for(option) %>: ") - 1;
			len += end - int_buf;
			len += sizeof("\r\n") - 1;
		}
	<% elsif option[:type] == :flag %>
		if (conf-><%= struct_field_for(option) %> != NGX_CONF_UNSET) {
			len += sizeof("!~<%= header_name_for(option) %>: ") - 1;
			len += conf-><%= struct_field_for(option) %>
				? sizeof("t\r\n") - 1
				: sizeof("f\r\n") - 1;
		}
	<% else %>
		<% raise "Unknown option type #{option[:type].inspect} for option #{option[:name]}" %>
	<% end %>
<% end %>

/* Create string */
buf = pos = ngx_pnalloc(cf->pool, len);
if (buf == NULL) {
	return 0;
}

<% for option in filter_eligible_options(LOCATION_CONFIGURATION_OPTIONS) %>
	<% if option[:type] == :string %>
		if (conf-><%= struct_field_for(option) %>.data != NULL) {
			pos = ngx_copy(pos,
				"!~<%= header_name_for(option) %>: ",
				sizeof("!~<%= header_name_for(option) %>: ") - 1);
			pos = ngx_copy(pos,
				conf-><%= struct_field_for(option) %>.data,
				conf-><%= struct_field_for(option) %>.len);
			pos = ngx_copy(pos, (const u_char *) "\r\n", sizeof("\r\n") - 1);
		}
	<% elsif option[:type] == :integer %>
		if (conf-><%= struct_field_for(option) %> != NGX_CONF_UNSET) {
			pos = ngx_copy(pos,
				"!~<%= header_name_for(option) %>: ",
				sizeof("!~<%= header_name_for(option) %>: ") - 1);
			end = ngx_snprintf(int_buf,
				sizeof(int_buf) - 1,
				"%d",
				conf-><%= struct_field_for(option) %>);
			pos = ngx_copy(pos, int_buf, end - int_buf);
			pos = ngx_copy(pos, (const u_char *) "\r\n", sizeof("\r\n") - 1);
		}
	<% elsif option[:type] == :flag %>
		if (conf-><%= struct_field_for(option) %> != NGX_CONF_UNSET) {
			pos = ngx_copy(pos,
				"!~<%= header_name_for(option) %>: ",
				sizeof("!~<%= header_name_for(option) %>: ") - 1);
			if (conf-><%= struct_field_for(option) %>) {
				pos = ngx_copy(pos, "t\r\n", sizeof("t\r\n") - 1);
			} else {
				pos = ngx_copy(pos, "f\r\n", sizeof("f\r\n") - 1);
			}
		}
	<% else %>
		<% raise "Unknown option type #{option[:type].inspect} for option #{option[:name]}" %>
	<% end %>
<% end %>

	conf->options_cache.data = buf;
	conf->options_cache.len = pos - buf;
	
	return 1;
}
