Skip to content

Commit

Permalink
refactor/extract HTTP functions, part 2: renaming
Browse files Browse the repository at this point in the history
Signed-off-by: Hans Zandbelt <hans.zandbelt@openidc.com>
  • Loading branch information
zandbelt committed Feb 25, 2024
1 parent 58385bd commit 35e7cbe
Show file tree
Hide file tree
Showing 14 changed files with 378 additions and 384 deletions.
2 changes: 1 addition & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
02/25/2024
- implement oidc_util_apr_hash_clear so clearing hashtables works with older versions of libapr
- refactor/extract HTTP functions, part 1
- refactor/extract HTTP functions

02/22/2024
- enable TCP keepalive on Redis connections by default
Expand Down
2 changes: 1 addition & 1 deletion src/cache/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int oidc_cache_file_post_config(server_rec *s) {
* return the cache file name for a specified key
*/
static const char *oidc_cache_file_name(request_rec *r, const char *section, const char *key) {
return apr_psprintf(r->pool, "%s%s-%s", OIDC_CACHE_FILE_PREFIX, section, oidc_util_escape_string(r, key));
return apr_psprintf(r->pool, "%s%s-%s", OIDC_CACHE_FILE_PREFIX, section, oidc_http_escape_string(r, key));
}

/*
Expand Down
14 changes: 7 additions & 7 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1282,13 +1282,13 @@ static void oidc_check_x_forwarded_hdr(request_rec *r, const apr_byte_t x_forwar

void oidc_config_check_x_forwarded(request_rec *r, const apr_byte_t x_forwarded_headers) {
oidc_check_x_forwarded_hdr(r, x_forwarded_headers, OIDC_HDR_X_FORWARDED_HOST, OIDC_HTTP_HDR_X_FORWARDED_HOST,
oidc_util_hdr_in_x_forwarded_host_get);
oidc_http_hdr_in_x_forwarded_host_get);
oidc_check_x_forwarded_hdr(r, x_forwarded_headers, OIDC_HDR_X_FORWARDED_PORT, OIDC_HTTP_HDR_X_FORWARDED_PORT,
oidc_util_hdr_in_x_forwarded_port_get);
oidc_http_hdr_in_x_forwarded_port_get);
oidc_check_x_forwarded_hdr(r, x_forwarded_headers, OIDC_HDR_X_FORWARDED_PROTO, OIDC_HTTP_HDR_X_FORWARDED_PROTO,
oidc_util_hdr_in_x_forwarded_proto_get);
oidc_http_hdr_in_x_forwarded_proto_get);
oidc_check_x_forwarded_hdr(r, x_forwarded_headers, OIDC_HDR_FORWARDED, OIDC_HTTP_HDR_FORWARDED,
oidc_util_hdr_in_forwarded_get);
oidc_http_hdr_in_forwarded_get);
}

static const char *oidc_set_redirect_urls_allowed(cmd_parms *cmd, void *m, const char *arg) {
Expand Down Expand Up @@ -2743,16 +2743,16 @@ static apr_status_t oidc_filter_in_filter(ap_filter_t *f, apr_bucket_brigade *br

if (userdata_post_params != NULL) {
buf = apr_psprintf(f->r->pool, "%s%s", ctx->nbytes > 0 ? "&" : "",
oidc_util_http_form_encoded_data(f->r, userdata_post_params));
oidc_http_form_encoded_data(f->r, userdata_post_params));
b_out =
apr_bucket_heap_create(buf, _oidc_strlen(buf), 0, f->r->connection->bucket_alloc);

APR_BRIGADE_INSERT_TAIL(brigade, b_out);

ctx->nbytes += _oidc_strlen(buf);

if (oidc_util_hdr_in_content_length_get(f->r) != NULL)
oidc_util_hdr_in_set(f->r, OIDC_HTTP_HDR_CONTENT_LENGTH,
if (oidc_http_hdr_in_content_length_get(f->r) != NULL)
oidc_http_hdr_in_set(f->r, OIDC_HTTP_HDR_CONTENT_LENGTH,
apr_psprintf(f->r->pool, "%ld", (long)ctx->nbytes));

apr_pool_userdata_set(NULL, OIDC_USERDATA_POST_PARAMS_KEY, NULL, f->r->pool);
Expand Down
147 changes: 74 additions & 73 deletions src/http.c

Large diffs are not rendered by default.

123 changes: 61 additions & 62 deletions src/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
// clang-format on
#include <jansson.h>

#define OIDC_CONTENT_TYPE_JSON "application/json"
#define OIDC_CONTENT_TYPE_JWT "application/jwt"
#define OIDC_CONTENT_TYPE_FORM_ENCODED "application/x-www-form-urlencoded"
#define OIDC_CONTENT_TYPE_IMAGE_PNG "image/png"
#define OIDC_CONTENT_TYPE_TEXT_HTML "text/html"
#define OIDC_CONTENT_TYPE_APP_XHTML_XML "application/xhtml+xml"
#define OIDC_CONTENT_TYPE_ANY "*/*"
#define OIDC_HTTP_CONTENT_TYPE_JSON "application/json"
#define OIDC_HTTP_CONTENT_TYPE_JWT "application/jwt"
#define OIDC_HTTP_CONTENT_TYPE_FORM_ENCODED "application/x-www-form-urlencoded"
#define OIDC_HTTP_CONTENT_TYPE_IMAGE_PNG "image/png"
#define OIDC_HTTP_CONTENT_TYPE_TEXT_HTML "text/html"
#define OIDC_HTTP_CONTENT_TYPE_APP_XHTML_XML "application/xhtml+xml"
#define OIDC_HTTP_CONTENT_TYPE_ANY "*/*"
/* HTTP header constants */
#define OIDC_HTTP_HDR_COOKIE "Cookie"
Expand Down Expand Up @@ -98,70 +98,69 @@ typedef struct oidc_http_timeout_t {
apr_time_t retry_interval;
} oidc_http_timeout_t;

typedef struct oidc_outgoing_proxy_t {
typedef struct oidc_http_outgoing_proxy_t {
const char *host_port;
const char *username_password;
unsigned long auth_type;
} oidc_outgoing_proxy_t;
} oidc_http_outgoing_proxy_t;

char *oidc_util_escape_string(const request_rec *r, const char *str);
char *oidc_util_unescape_string(const request_rec *r, const char *str);
char *oidc_http_escape_string(const request_rec *r, const char *str);
char *oidc_http_unescape_string(const request_rec *r, const char *str);

void oidc_util_hdr_err_out_add(const request_rec *r, const char *name, const char *value);
void oidc_util_hdr_in_set(const request_rec *r, const char *name, const char *value);
const char *oidc_util_hdr_in_cookie_get(const request_rec *r);
void oidc_util_hdr_in_cookie_set(const request_rec *r, const char *value);
const char *oidc_util_hdr_in_user_agent_get(const request_rec *r);
const char *oidc_util_hdr_in_x_forwarded_for_get(const request_rec *r);
const char *oidc_util_hdr_in_content_type_get(const request_rec *r);
const char *oidc_util_hdr_in_content_length_get(const request_rec *r);
const char *oidc_util_hdr_in_x_requested_with_get(const request_rec *r);
const char *oidc_util_hdr_in_sec_fetch_mode_get(const request_rec *r);
const char *oidc_util_hdr_in_sec_fetch_dest_get(const request_rec *r);
const char *oidc_util_hdr_in_accept_get(const request_rec *r);
apr_byte_t oidc_util_hdr_in_accept_contains(const request_rec *r, const char *needle);
const char *oidc_util_hdr_in_authorization_get(const request_rec *r);
const char *oidc_util_hdr_in_x_forwarded_proto_get(const request_rec *r);
const char *oidc_util_hdr_in_x_forwarded_port_get(const request_rec *r);
const char *oidc_util_hdr_in_x_forwarded_host_get(const request_rec *r);
const char *oidc_util_hdr_in_forwarded_get(const request_rec *r);
const char *oidc_util_hdr_in_host_get(const request_rec *r);
const char *oidc_util_hdr_in_traceparent_get(const request_rec *r);
void oidc_util_hdr_out_location_set(const request_rec *r, const char *value);
const char *oidc_util_hdr_out_location_get(const request_rec *r);
const char *oidc_util_hdr_forwarded_get(const request_rec *r, const char *elem);
void oidc_http_hdr_err_out_add(const request_rec *r, const char *name, const char *value);
void oidc_http_hdr_in_set(const request_rec *r, const char *name, const char *value);
const char *oidc_http_hdr_in_cookie_get(const request_rec *r);
void oidc_http_hdr_in_cookie_set(const request_rec *r, const char *value);
const char *oidc_http_hdr_in_user_agent_get(const request_rec *r);
const char *oidc_http_hdr_in_x_forwarded_for_get(const request_rec *r);
const char *oidc_http_hdr_in_content_type_get(const request_rec *r);
const char *oidc_http_hdr_in_content_length_get(const request_rec *r);
const char *oidc_http_hdr_in_x_requested_with_get(const request_rec *r);
const char *oidc_http_hdr_in_sec_fetch_mode_get(const request_rec *r);
const char *oidc_http_hdr_in_sec_fetch_dest_get(const request_rec *r);
const char *oidc_http_hdr_in_accept_get(const request_rec *r);
apr_byte_t oidc_http_hdr_in_accept_contains(const request_rec *r, const char *needle);
const char *oidc_http_hdr_in_authorization_get(const request_rec *r);
const char *oidc_http_hdr_in_x_forwarded_proto_get(const request_rec *r);
const char *oidc_http_hdr_in_x_forwarded_port_get(const request_rec *r);
const char *oidc_http_hdr_in_x_forwarded_host_get(const request_rec *r);
const char *oidc_http_hdr_in_forwarded_get(const request_rec *r);
const char *oidc_http_hdr_in_host_get(const request_rec *r);
const char *oidc_http_hdr_in_traceparent_get(const request_rec *r);
void oidc_http_hdr_out_location_set(const request_rec *r, const char *value);
const char *oidc_http_hdr_out_location_get(const request_rec *r);
const char *oidc_http_hdr_forwarded_get(const request_rec *r, const char *elem);

char *oidc_normalize_header_name(const request_rec *r, const char *str);
apr_byte_t oidc_util_http_get(request_rec *r, const char *url, const apr_table_t *params, const char *basic_auth,
const char *bearer_token, int ssl_validate_server, char **response,
oidc_http_timeout_t *http_timeout, const oidc_outgoing_proxy_t *outgoing_proxy,
apr_array_header_t *pass_cookies, const char *ssl_cert, const char *ssl_key,
const char *ssl_key_pwd);
apr_byte_t oidc_util_http_post_form(request_rec *r, const char *url, const apr_table_t *params, const char *basic_auth,
const char *bearer_token, int ssl_validate_server, char **response,
oidc_http_timeout_t *http_timeout, const oidc_outgoing_proxy_t *outgoing_proxy,
apr_array_header_t *pass_cookies, const char *ssl_cert, const char *ssl_key,
const char *ssl_key_pwd);
apr_byte_t oidc_util_http_post_json(request_rec *r, const char *url, json_t *data, const char *basic_auth,
const char *bearer_token, int ssl_validate_server, char **response,
oidc_http_timeout_t *http_timeout, const oidc_outgoing_proxy_t *outgoing_proxy,
apr_array_header_t *pass_cookies, const char *ssl_cert, const char *ssl_key,
const char *ssl_key_pwd);
apr_byte_t oidc_util_request_has_parameter(request_rec *r, const char *param);
apr_byte_t oidc_util_get_request_parameter(request_rec *r, char *name, char **value);
int oidc_util_http_send(request_rec *r, const char *data, size_t data_len, const char *content_type,
int success_rvalue);
apr_byte_t oidc_util_read_form_encoded_params(request_rec *r, apr_table_t *table, char *data);
apr_byte_t oidc_util_read_post_params(request_rec *r, apr_table_t *table, apr_byte_t propagate,
char *oidc_http_hdr_normalize_name(const request_rec *r, const char *str);
apr_byte_t oidc_http_get(request_rec *r, const char *url, const apr_table_t *params, const char *basic_auth,
const char *bearer_token, int ssl_validate_server, char **response,
oidc_http_timeout_t *http_timeout, const oidc_http_outgoing_proxy_t *outgoing_proxy,
apr_array_header_t *pass_cookies, const char *ssl_cert, const char *ssl_key,
const char *ssl_key_pwd);
apr_byte_t oidc_http_post_form(request_rec *r, const char *url, const apr_table_t *params, const char *basic_auth,
const char *bearer_token, int ssl_validate_server, char **response,
oidc_http_timeout_t *http_timeout, const oidc_http_outgoing_proxy_t *outgoing_proxy,
apr_array_header_t *pass_cookies, const char *ssl_cert, const char *ssl_key,
const char *ssl_key_pwd);
apr_byte_t oidc_http_post_json(request_rec *r, const char *url, json_t *data, const char *basic_auth,
const char *bearer_token, int ssl_validate_server, char **response,
oidc_http_timeout_t *http_timeout, const oidc_http_outgoing_proxy_t *outgoing_proxy,
apr_array_header_t *pass_cookies, const char *ssl_cert, const char *ssl_key,
const char *ssl_key_pwd);
apr_byte_t oidc_http_request_has_parameter(request_rec *r, const char *param);
apr_byte_t oidc_http_request_parameter_get(request_rec *r, char *name, char **value);
int oidc_http_send(request_rec *r, const char *data, size_t data_len, const char *content_type, int success_rvalue);
apr_byte_t oidc_http_read_form_encoded_params(request_rec *r, apr_table_t *table, char *data);
apr_byte_t oidc_http_read_post_params(request_rec *r, apr_table_t *table, apr_byte_t propagate,
const char *strip_param_name);
char *oidc_util_http_query_encoded_url(request_rec *r, const char *url, const apr_table_t *params);
char *oidc_util_http_form_encoded_data(request_rec *r, const apr_table_t *params);
char *oidc_http_query_encoded_url(request_rec *r, const char *url, const apr_table_t *params);
char *oidc_http_form_encoded_data(request_rec *r, const apr_table_t *params);

char *oidc_util_get_cookie(request_rec *r, const char *cookieName);
void oidc_util_set_cookie(request_rec *r, const char *cookieName, const char *cookieValue, apr_time_t expires,
char *oidc_http_get_cookie(request_rec *r, const char *cookieName);
void oidc_http_set_cookie(request_rec *r, const char *cookieName, const char *cookieValue, apr_time_t expires,
const char *ext);
char *oidc_util_get_chunked_cookie(request_rec *r, const char *cookieName, int chunkSize);
void oidc_util_set_chunked_cookie(request_rec *r, const char *cookieName, const char *cookieValue, apr_time_t expires,
char *oidc_http_get_chunked_cookie(request_rec *r, const char *cookieName, int chunkSize);
void oidc_http_set_chunked_cookie(request_rec *r, const char *cookieName, const char *cookieValue, apr_time_t expires,
int chunkSize, const char *ext);

#endif /* MOD_AUTH_OPENIDC_HTTP_H_ */
20 changes: 10 additions & 10 deletions src/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ static const char *oidc_metadata_issuer_to_filename(request_rec *r, const char *
if (p[n - 1] == OIDC_CHAR_FORWARD_SLASH)
p[n - 1] = '\0';

return oidc_util_escape_string(r, p);
return oidc_http_escape_string(r, p);
}

/*
Expand All @@ -152,7 +152,7 @@ static const char *oidc_metadata_filename_to_issuer(request_rec *r, const char *
char *result = apr_pstrdup(r->pool, filename);
char *p = strrchr(result, OIDC_CHAR_DOT);
*p = '\0';
p = oidc_util_unescape_string(r, result);
p = oidc_http_unescape_string(r, result);
return apr_psprintf(r->pool, "https://%s", p);
}

Expand Down Expand Up @@ -544,9 +544,9 @@ static apr_byte_t oidc_metadata_client_register(request_rec *r, oidc_cfg *cfg, o
}

/* dynamically register the client with the specified parameters */
if (oidc_util_http_post_json(r, provider->registration_endpoint_url, data, NULL, provider->registration_token,
provider->ssl_validate_server, response, &cfg->http_timeout_short,
&cfg->outgoing_proxy, oidc_dir_cfg_pass_cookies(r), NULL, NULL, NULL) == FALSE) {
if (oidc_http_post_json(r, provider->registration_endpoint_url, data, NULL, provider->registration_token,
provider->ssl_validate_server, response, &cfg->http_timeout_short, &cfg->outgoing_proxy,
oidc_dir_cfg_pass_cookies(r), NULL, NULL, NULL) == FALSE) {
json_decref(data);
return FALSE;
}
Expand All @@ -571,8 +571,8 @@ static apr_byte_t oidc_metadata_jwks_retrieve_and_cache(request_rec *r, oidc_cfg
const char *url = (jwks_uri->signed_uri != NULL) ? jwks_uri->signed_uri : jwks_uri->uri;

/* get the JWKs from the specified URL with the specified parameters */
if (oidc_util_http_get(r, url, NULL, NULL, NULL, ssl_validate_server, &response, &cfg->http_timeout_long,
&cfg->outgoing_proxy, oidc_dir_cfg_pass_cookies(r), NULL, NULL, NULL) == FALSE)
if (oidc_http_get(r, url, NULL, NULL, NULL, ssl_validate_server, &response, &cfg->http_timeout_long,
&cfg->outgoing_proxy, oidc_dir_cfg_pass_cookies(r), NULL, NULL, NULL) == FALSE)
return FALSE;

if ((jwks_uri->signed_uri != NULL) && (jwks_uri->jwk != NULL)) {
Expand Down Expand Up @@ -668,9 +668,9 @@ apr_byte_t oidc_metadata_provider_retrieve(request_rec *r, oidc_cfg *cfg, const
OIDC_METRICS_TIMING_START(r, cfg);

/* get provider metadata from the specified URL with the specified parameters */
if (oidc_util_http_get(r, url, NULL, NULL, NULL, cfg->provider.ssl_validate_server, response,
&cfg->http_timeout_short, &cfg->outgoing_proxy, oidc_dir_cfg_pass_cookies(r), NULL, NULL,
NULL) == FALSE) {
if (oidc_http_get(r, url, NULL, NULL, NULL, cfg->provider.ssl_validate_server, response,
&cfg->http_timeout_short, &cfg->outgoing_proxy, oidc_dir_cfg_pass_cookies(r), NULL, NULL,
NULL) == FALSE) {
OIDC_METRICS_COUNTER_INC(r, cfg, OM_PROVIDER_METADATA_ERROR);
return FALSE;
}
Expand Down
19 changes: 9 additions & 10 deletions src/metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ static int oidc_metrics_handle_json(request_rec *r, char *s_json) {
end:

/* return the data to the caller */
return oidc_util_http_send(r, s_json, _oidc_strlen(s_json), OIDC_CONTENT_TYPE_JSON, OK);
return oidc_http_send(r, s_json, _oidc_strlen(s_json), OIDC_HTTP_CONTENT_TYPE_JSON, OK);
}

/*
Expand All @@ -1057,7 +1057,7 @@ static int oidc_metrics_handle_json(request_rec *r, char *s_json) {
static int oidc_metrics_handle_internal(request_rec *r, char *s_json) {
if (s_json == NULL)
return HTTP_NOT_FOUND;
return oidc_util_http_send(r, s_json, _oidc_strlen(s_json), OIDC_CONTENT_TYPE_JSON, OK);
return oidc_http_send(r, s_json, _oidc_strlen(s_json), OIDC_HTTP_CONTENT_TYPE_JSON, OK);
}

#define OIDC_METRICS_SERVER_PARAM "server_name"
Expand All @@ -1075,9 +1075,9 @@ static int oidc_metrics_handle_status(request_rec *r, char *s_json) {
const char *s_key = NULL, *s_name = NULL;
void *iter = NULL;

oidc_util_get_request_parameter(r, OIDC_METRICS_SERVER_PARAM, &s_server);
oidc_util_get_request_parameter(r, OIDC_METRICS_COUNTER_PARAM, &metric);
oidc_util_get_request_parameter(r, OIDC_METRICS_SPEC_PARAM, &spec);
oidc_http_request_parameter_get(r, OIDC_METRICS_SERVER_PARAM, &s_server);
oidc_http_request_parameter_get(r, OIDC_METRICS_COUNTER_PARAM, &metric);
oidc_http_request_parameter_get(r, OIDC_METRICS_SPEC_PARAM, &spec);

if (s_server == NULL)
s_server = "localhost";
Expand Down Expand Up @@ -1115,7 +1115,7 @@ static int oidc_metrics_handle_status(request_rec *r, char *s_json) {
if (json)
json_decref(json);

return oidc_util_http_send(r, msg, _oidc_strlen(msg), "text/plain", OK);
return oidc_http_send(r, msg, _oidc_strlen(msg), "text/plain", OK);
}

/*
Expand Down Expand Up @@ -1282,8 +1282,7 @@ static int oidc_metrics_handle_prometheus(request_rec *r, char *s_json) {

json_decref(json);

return oidc_util_http_send(r, ctx.s_result, _oidc_strlen(ctx.s_result), OIDC_METRICS_PROMETHEUS_CONTENT_TYPE,
OK);
return oidc_http_send(r, ctx.s_result, _oidc_strlen(ctx.s_result), OIDC_METRICS_PROMETHEUS_CONTENT_TYPE, OK);
}

/*
Expand Down Expand Up @@ -1318,7 +1317,7 @@ static int oidc_metric_reset(request_rec *r, int dvalue) {
char svalue[16];
int value = 0;

oidc_util_get_request_parameter(r, OIDC_METRICS_RESET_PARAM, &s_reset);
oidc_http_request_parameter_get(r, OIDC_METRICS_RESET_PARAM, &s_reset);

if (s_reset == NULL)
return dvalue;
Expand All @@ -1343,7 +1342,7 @@ const oidc_metrics_content_handler_t *oidc_metrics_find_handler(request_rec *r)
int i = 0;

/* get the specified format */
oidc_util_get_request_parameter(r, OIDC_METRICS_FORMAT_PARAM, &s_format);
oidc_http_request_parameter_get(r, OIDC_METRICS_FORMAT_PARAM, &s_format);

if (s_format == NULL)
return &_oidc_metrics_handlers[0];
Expand Down
Loading

0 comments on commit 35e7cbe

Please sign in to comment.