Skip to content

Commit 7ee7c4e

Browse files
committed
Add HTTP headers to ctx object
1 parent 529cb20 commit 7ee7c4e

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

cgi/mapcache.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
#include <fcgi_stdio.h>
4040
#endif
4141

42+
#ifndef WIN32
43+
extern char** environ;
44+
#endif
45+
4246
typedef struct mapcache_context_fcgi mapcache_context_fcgi;
4347
typedef struct mapcache_context_fcgi_request mapcache_context_fcgi_request;
4448

@@ -158,6 +162,16 @@ static void fcgi_write_response(mapcache_context_fcgi *ctx, mapcache_http_respon
158162
}
159163
}
160164

165+
// Replace all occurrences of substr in string
166+
char* str_replace_all(apr_pool_t* pool, const char* string,
167+
const char* substr, const char* replacement)
168+
{
169+
char* replaced = apr_pstrdup(pool, string);
170+
while (strstr(replaced, substr)) {
171+
replaced = mapcache_util_str_replace(pool, string, substr, replacement);
172+
}
173+
return replaced;
174+
}
161175

162176
apr_time_t mtime;
163177
char *conffile;
@@ -297,6 +311,41 @@ int main(int argc, const char **argv)
297311
goto cleanup;
298312
}
299313

314+
// add all environ settings including HTTP headers to
315+
// a ctx->headers_in apr_table_t
316+
317+
int num_env_var = 0;
318+
char** env = environ;
319+
320+
while (env[num_env_var] != NULL)
321+
num_env_var++;
322+
323+
apr_table_t* headers;
324+
headers = apr_table_make(ctx->pool, num_env_var);
325+
326+
char *key, *val, * kvp, *pair;
327+
int i;
328+
329+
for (i = 0; env[i] != NULL; i++) {
330+
kvp = env[i];
331+
332+
// convert HTTP header keys from the form HTTP_MY_HEADER to MY-HEADER
333+
key = apr_strtok(kvp, "=", &pair);
334+
key = mapcache_util_str_replace(ctx->pool, key, "HTTP_", "");
335+
key = mapcache_util_str_replace(ctx->pool, key, "_", "-");
336+
// key = str_replace_all(ctx->pool, key, "_", "-");
337+
val = apr_strtok(NULL, "=", &pair);
338+
339+
if (val != NULL) {
340+
apr_table_addn(headers, key, val);
341+
}
342+
else {
343+
apr_table_addn(headers, key, "");
344+
}
345+
}
346+
347+
ctx->headers_in = headers;
348+
300349
http_response = NULL;
301350
if(request->type == MAPCACHE_REQUEST_GET_CAPABILITIES) {
302351
mapcache_request_get_capabilities *req = (mapcache_request_get_capabilities*)request;

0 commit comments

Comments
 (0)