Skip to content

Commit

Permalink
Merge pull request #48 from beetlebugorg/feature/useragent
Browse files Browse the repository at this point in the history
User-Agent
  • Loading branch information
JC authored May 23, 2023
2 parents e96a524 + ae8b158 commit 5a46277
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AC_PREREQ(2.60)
AC_INIT(mod_dims, 3.3.28, [[email protected]])
AC_INIT(mod_dims, 3.3.29, [[email protected]])
AM_INIT_AUTOMAKE([no-define])
AC_CONFIG_SRCDIR([src/mod_dims.c])

Expand Down
1 change: 1 addition & 0 deletions docker/dims.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ DimsAddClient ${DIMS_CLIENT} ${DIMS_NO_IMAGE_URL} ${DIMS_CACHE_CONTROL_MAX_AGE}
DimsDefaultImageURL ${DIMS_DEFAULT_IMAGE_URL}
DimsCacheExpire ${DIMS_CACHE_EXPIRE}
DimsNoImageCacheExpire ${DIMS_NO_IMAGE_CACHE_EXPIRE}
DimsUserAgentEnabled true

## Handler definitions. ##

Expand Down
62 changes: 61 additions & 1 deletion src/mod_dims.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/

#define MODULE_RELEASE "$Revision: $"
#define MODULE_VERSION "3.3.28"
#define MODULE_VERSION "3.3.29"

#include "mod_dims.h"
#include "util_md5.h"
Expand Down Expand Up @@ -265,6 +265,30 @@ dims_config_set_default_output_format(cmd_parms *cmd, void *dummy, const char *a
return NULL;
}

static const char *
dims_config_set_user_agent_override(cmd_parms *cmd, void *dummy, const char *arg)
{
dims_config_rec *config = (dims_config_rec *) ap_get_module_config(
cmd->server->module_config, &dims_module);
char *user_agent = (char *) arg;
config->user_agent_override = user_agent;
return NULL;
}

static const char *
dims_config_set_user_agent_enabled(cmd_parms *cmd, void *dummy, const char *arg)
{
dims_config_rec *config = (dims_config_rec *) ap_get_module_config(
cmd->server->module_config, &dims_module);
if(strcmp(arg, "true") == 0) {
config->user_agent_enabled = 1;
}
else {
config->user_agent_enabled = 0;
}
return NULL;
}

static const char *
dims_config_set_client(cmd_parms *cmd, void *d, int argc, char *const argv[])
{
Expand Down Expand Up @@ -550,6 +574,23 @@ char *url_encode(char *str) {
return buf;
}

static int
dims_curl_debug_cb(CURL *handle,
curl_infotype type,
char *data,
size_t size,
void *clientp)
{
dims_request_rec *d = (dims_request_rec *) clientp;
switch(type) {
case CURLINFO_HEADER_OUT:
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, d->r, "Curl request header data: %s ", data);
break;
default:
break;
}
}

CURLcode
dims_get_image_data(dims_request_rec *d, char *fetch_url, dims_image_data_t *data)
{
Expand Down Expand Up @@ -587,6 +628,17 @@ dims_get_image_data(dims_request_rec *d, char *fetch_url, dims_image_data_t *dat
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT_MS, d->config->download_timeout + extra_time);
curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl_handle, CURLOPT_DEBUGFUNCTION, dims_curl_debug_cb);
curl_easy_setopt(curl_handle, CURLOPT_DEBUGDATA, d);

/* Set the user agent to dims/<version> */
if (d->config->user_agent_override != NULL && d->config->user_agent_enabled == 1) {
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, d->config->user_agent_override);
} else if (d->config->user_agent_enabled == 1) {
char *dims_useragent = apr_psprintf(d->r->pool, "mod_dims/%s", MODULE_VERSION);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, dims_useragent);
}

/* The curl shared handle allows this process to share DNS cache
* and prevents the DNS cache from going away after every request.
Expand Down Expand Up @@ -2118,6 +2170,14 @@ static const command_rec dims_commands[] =
AP_INIT_TAKE1("DimsDefaultOutputFormat",
dims_config_set_default_output_format, NULL, RSRC_CONF,
"Default output format if 'format' command is not present in the request."),
AP_INIT_TAKE1("DimsUserAgentEnabled",
dims_config_set_user_agent_enabled, NULL, RSRC_CONF,
"Enable DIMS User-Agent header ('dims/<version>'), true OR false."
"The default is false."),
AP_INIT_TAKE1("DimsUserAgentOverride",
dims_config_set_user_agent_override, NULL, RSRC_CONF,
"Override DIMS User-Agent header"
"The default is 'dims/<version>."),
{NULL}
};

Expand Down
3 changes: 3 additions & 0 deletions src/mod_dims.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ struct dims_config_rec {
long max_expiry_period;
char *cache_dir;
char *default_image_prefix;

char *user_agent_override;
int user_agent_enabled;
};

struct dims_client_config_rec {
Expand Down

0 comments on commit 5a46277

Please sign in to comment.