Skip to content

Commit 47d2757

Browse files
authored
Merge pull request #258 from geographika/fastcgi-headers
Add HTTP headers to ctx object in FastCGI executable
2 parents 529cb20 + f2de657 commit 47d2757

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

cgi/mapcache.c

Lines changed: 43 additions & 1 deletion
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,7 +162,6 @@ static void fcgi_write_response(mapcache_context_fcgi *ctx, mapcache_http_respon
158162
}
159163
}
160164

161-
162165
apr_time_t mtime;
163166
char *conffile;
164167

@@ -228,6 +231,43 @@ static void load_config(mapcache_context *ctx, char *filename)
228231

229232
}
230233

234+
static void set_headers(mapcache_context* ctx, char** env)
235+
{
236+
// add all environ settings including HTTP headers to
237+
// a ctx->headers_in apr_table_t
238+
239+
char * key, * val, * kvp, * pair;
240+
int i;
241+
int num_env_var;
242+
apr_table_t* headers;
243+
244+
num_env_var = 0;
245+
while (env[num_env_var] != NULL)
246+
num_env_var++;
247+
248+
headers = apr_table_make(ctx->pool, num_env_var);
249+
250+
for (i = 0; env[i] != NULL; i++) {
251+
kvp = apr_pstrdup(ctx->pool, env[i]);
252+
253+
// convert HTTP header keys from the form HTTP_MY_HEADER to MY-HEADER
254+
key = apr_strtok(kvp, "=", &pair);
255+
key = mapcache_util_str_replace(ctx->pool, key, "HTTP_", "");
256+
key = mapcache_util_str_replace_all(ctx->pool, key, "_", "-");
257+
258+
val = apr_strtok(NULL, "=", &pair);
259+
260+
if (val != NULL) {
261+
apr_table_addn(headers, key, val);
262+
}
263+
else {
264+
apr_table_addn(headers, key, "");
265+
}
266+
}
267+
268+
ctx->headers_in = headers;
269+
}
270+
231271
int main(int argc, const char **argv)
232272
{
233273
mapcache_context_fcgi* globalctx;
@@ -297,6 +337,8 @@ int main(int argc, const char **argv)
297337
goto cleanup;
298338
}
299339

340+
set_headers(ctx, environ);
341+
300342
http_response = NULL;
301343
if(request->type == MAPCACHE_REQUEST_GET_CAPABILITIES) {
302344
mapcache_request_get_capabilities *req = (mapcache_request_get_capabilities*)request;

include/mapcache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,7 @@ MS_DLL_EXPORT char *mapcache_util_str_replace(apr_pool_t *pool, const char *stri
14421442
const char *replacement );
14431443
char *mapcache_util_dbl_replace(apr_pool_t *pool, const char *string, const char *substr,
14441444
double replacement );
1445-
char *mapcache_util_str_replace_all(apr_pool_t *pool, const char *string, const char *substr,
1445+
MS_DLL_EXPORT char *mapcache_util_str_replace_all(apr_pool_t *pool, const char *string, const char *substr,
14461446
const char *replacement );
14471447
char *mapcache_util_dbl_replace_all(apr_pool_t *pool, const char *string, const char *substr,
14481448
double replacement );

0 commit comments

Comments
 (0)