void oidc_util_set_cookie(request_rec *r, const char *cookieName,
		const char *cookieValue, apr_time_t expires, const char *ext) {

	oidc_cfg *c = ap_get_module_config(r->server->module_config,
			&auth_openidc_module);
	char *headerString, *expiresString = NULL;
	const char *appendString = NULL;

	/* see if we need to clear the cookie */
	if (apr_strnatcmp(cookieValue, "") == 0)
		expires = 0;

	/* construct the expire value */
	if (expires != -1) {
		expiresString = (char*) apr_pcalloc(r->pool, APR_RFC822_DATE_LEN);
		if (apr_rfc822_date(expiresString, expires) != APR_SUCCESS) {
			oidc_error(r, "could not set cookie expiry date");
		}
	}

	/* construct the cookie value */
	headerString = apr_psprintf(r->pool, "%s=%s", cookieName, cookieValue);

	headerString = apr_psprintf(r->pool, "%s; %s=%s", headerString,
			OIDC_COOKIE_FLAG_PATH, oidc_util_get_cookie_path(r));

	if (expiresString != NULL)
		headerString = apr_psprintf(r->pool, "%s; %s=%s", headerString,
				OIDC_COOKIE_FLAG_EXPIRES, expiresString);

	if (c->cookie_domain != NULL)
		headerString = apr_psprintf(r->pool, "%s; %s=%s", headerString,
				OIDC_COOKIE_FLAG_DOMAIN, c->cookie_domain);

	if (oidc_util_request_is_secure(r))
		headerString = apr_psprintf(r->pool, "%s; %s", headerString,
				OIDC_COOKIE_FLAG_SECURE);

	if (c->cookie_http_only != FALSE)
		headerString = apr_psprintf(r->pool, "%s; %s", headerString,
				OIDC_COOKIE_FLAG_HTTP_ONLY);

	appendString = oidc_util_set_cookie_append_value(r, c);
	if (appendString != NULL)
		headerString = apr_psprintf(r->pool, "%s; %s", headerString,
				appendString);
	else if (ext != NULL)
		headerString = apr_psprintf(r->pool, "%s; %s", headerString, ext);

	/* sanity check on overall cookie value size */
	if (strlen(headerString) > OIDC_COOKIE_MAX_SIZE) {
		oidc_warn(r,
				"the length of the cookie value (%d) is greater than %d(!) bytes, this may not work with all browsers/server combinations: consider switching to a server side caching!",
				(int )strlen(headerString), OIDC_COOKIE_MAX_SIZE);
	}

	/* use r->err_headers_out so we always print our headers (even on 302 redirect) - headers_out only prints on 2xx responses */
	oidc_util_hdr_err_out_add(r, OIDC_HTTP_HDR_SET_COOKIE, headerString);
}
void oidc_util_set_cookie(request_rec *r, const char *cookieName,
		const char *cookieValue, apr_time_t expires, const char *ext) {

	oidc_cfg *c = ap_get_module_config(r->server->module_config,
			&auth_openidc_module);
	char *headerString, *expiresString = NULL;
	const char *appendString = NULL;

	 
	if (apr_strnatcmp(cookieValue, "") == 0)
		expires = 0;

	 
	if (expires != -1) {
		expiresString = (char*) apr_pcalloc(r->pool, APR_RFC822_DATE_LEN);
		if (apr_rfc822_date(expiresString, expires) != APR_SUCCESS) {
			oidc_error(r, "could not set cookie expiry date");
		}
	}

	 
	headerString = apr_psprintf(r->pool, "%s=%s", cookieName, cookieValue);

	headerString = apr_psprintf(r->pool, "%s; %s=%s", headerString,
			OIDC_COOKIE_FLAG_PATH, oidc_util_get_cookie_path(r));

	if (expiresString != NULL)
		headerString = apr_psprintf(r->pool, "%s; %s=%s", headerString,
				OIDC_COOKIE_FLAG_EXPIRES, expiresString);

	if (c->cookie_domain != NULL)
		headerString = apr_psprintf(r->pool, "%s; %s=%s", headerString,
				OIDC_COOKIE_FLAG_DOMAIN, c->cookie_domain);

	if (oidc_util_request_is_secure(r))
		headerString = apr_psprintf(r->pool, "%s; %s", headerString,
				OIDC_COOKIE_FLAG_SECURE);

	if (c->cookie_http_only != FALSE)
		headerString = apr_psprintf(r->pool, "%s; %s", headerString,
				OIDC_COOKIE_FLAG_HTTP_ONLY);

	appendString = oidc_util_set_cookie_append_value(r, c);
	if (appendString != NULL)
		headerString = apr_psprintf(r->pool, "%s; %s", headerString,
				appendString);
	else if (ext != NULL)
		headerString = apr_psprintf(r->pool, "%s; %s", headerString, ext);

	 
	if (strlen(headerString) > OIDC_COOKIE_MAX_SIZE) {
		oidc_warn(r,
				"the length of the cookie value (%d) is greater than %d(!) bytes, this may not work with all browsers/server combinations: consider switching to a server side caching!",
				(int )strlen(headerString), OIDC_COOKIE_MAX_SIZE);
	}

	 
	oidc_util_hdr_err_out_add(r, OIDC_HTTP_HDR_SET_COOKIE, headerString);
}