static int mod_mapcache_request_handler(request_rec *r) { apr_table_t *params; mapcache_request *request = NULL; mapcache_context_apache_request *apache_ctx = NULL; mapcache_http_response *http_response = NULL; mapcache_context *global_ctx = NULL; if (!r->handler || strcmp(r->handler, "mapcache")) { return DECLINED; } if (r->method_number != M_GET) { return HTTP_METHOD_NOT_ALLOWED; } apache_ctx = apache_request_context_create(r); global_ctx = (mapcache_context*)apache_ctx; params = mapcache_http_parse_param_string(global_ctx, r->args); mapcache_service_dispatch_request(global_ctx,&request,r->path_info,params,global_ctx->config); if(GC_HAS_ERROR(global_ctx) || !request) { return write_http_response(apache_ctx, mapcache_core_respond_to_error(global_ctx)); } if(request->type == MAPCACHE_REQUEST_GET_CAPABILITIES) { mapcache_request_get_capabilities *req_caps = (mapcache_request_get_capabilities*)request; request_rec *original; char *url; if(r->main) original = r->main; else original = r; url = ap_construct_url(r->pool,original->uri,original); /* * remove the path_info from the end of the url (we want the url of the base of the service) * TODO: is there an apache api to access this ? */ if(*(original->path_info) && strcmp(original->path_info,"/")) { char *end = strstr(url,original->path_info); if(end) { /* make sure our url ends with a single '/' */ if(*end == '/') { char *slash = end; while((*(--slash))=='/') end--; end++; } *end = '\0'; } } http_response = mapcache_core_get_capabilities(global_ctx,request->service,req_caps, url,original->path_info,global_ctx->config); } else if( request->type == MAPCACHE_REQUEST_GET_TILE) { mapcache_request_get_tile *req_tile = (mapcache_request_get_tile*)request; http_response = mapcache_core_get_tile(global_ctx,req_tile); } else if( request->type == MAPCACHE_REQUEST_PROXY ) { mapcache_request_proxy *req_proxy = (mapcache_request_proxy*)request; http_response = mapcache_core_proxy_request(global_ctx, req_proxy); } else if( request->type == MAPCACHE_REQUEST_GET_MAP) { mapcache_request_get_map *req_map = (mapcache_request_get_map*)request; http_response = mapcache_core_get_map(global_ctx,req_map); } else if( request->type == MAPCACHE_REQUEST_GET_FEATUREINFO) { mapcache_request_get_feature_info *req_fi = (mapcache_request_get_feature_info*)request; http_response = mapcache_core_get_featureinfo(global_ctx,req_fi); } else { global_ctx->set_error(global_ctx,500,"###BUG### unknown request type"); } if(GC_HAS_ERROR(global_ctx)) { return write_http_response(apache_ctx, mapcache_core_respond_to_error(global_ctx)); } return write_http_response(apache_ctx,http_response); }
int main(int argc, const char **argv) { mapcache_context_fcgi* globalctx; mapcache_context* ctx; apr_table_t *params; mapcache_request *request = NULL; char *pathInfo; mapcache_http_response *http_response; (void) signal(SIGTERM,handle_signal); #ifndef _WIN32 (void) signal(SIGUSR1,handle_signal); #endif apr_initialize(); atexit(apr_terminate); if(apr_pool_create(&global_pool,NULL) != APR_SUCCESS) { return 1; } config_pool = NULL; globalctx = fcgi_context_create(); ctx = (mapcache_context*)globalctx; conffile = getenv("MAPCACHE_CONFIG_FILE"); #ifdef DEBUG if(!conffile) { int i; for(i=1; i<argc; i++) { if( strncmp(argv[i], "-c", 2) == 0 ) { conffile = strdup(argv[i+1]); putenv( "REQUEST_METHOD=GET" ); } else if( strncmp(argv[i], "QUERY_STRING=", 13) == 0 ) { putenv( strdup(argv[i]) ); } else if( strncmp(argv[i], "PATH_INFO=", 10) == 0 ) { putenv( strdup(argv[i]) ); } } } #endif if(!conffile) { ctx->log(ctx,MAPCACHE_ERROR,"no config file found in MAPCACHE_CONFIG_FILE environment"); return 1; } ctx->log(ctx,MAPCACHE_INFO,"mapcache fcgi conf file: %s",conffile); #ifdef USE_FASTCGI while (FCGI_Accept() >= 0) { #endif ctx->pool = config_pool; if(!ctx->config || ctx->config->autoreload) { load_config(ctx,conffile); if(GC_HAS_ERROR(ctx)) { fcgi_write_response(globalctx, mapcache_core_respond_to_error(ctx)); goto cleanup; } } apr_pool_create(&(ctx->pool),config_pool); request = NULL; pathInfo = getenv("PATH_INFO"); params = mapcache_http_parse_param_string(ctx, getenv("QUERY_STRING")); mapcache_service_dispatch_request(ctx,&request,pathInfo,params,ctx->config); if(GC_HAS_ERROR(ctx) || !request) { fcgi_write_response(globalctx, mapcache_core_respond_to_error(ctx)); goto cleanup; } http_response = NULL; if(request->type == MAPCACHE_REQUEST_GET_CAPABILITIES) { mapcache_request_get_capabilities *req = (mapcache_request_get_capabilities*)request; char *host = getenv("SERVER_NAME"); char *port = getenv("SERVER_PORT"); char *fullhost; char *url; if(getenv("HTTPS")) { if(!port || !strcmp(port,"443")) { fullhost = apr_psprintf(ctx->pool,"https://%s",host); } else { fullhost = apr_psprintf(ctx->pool,"https://%s:%s",host,port); } } else { if(!port || !strcmp(port,"80")) { fullhost = apr_psprintf(ctx->pool,"http://%s",host); } else { fullhost = apr_psprintf(ctx->pool,"http://%s:%s",host,port); } } url = apr_psprintf(ctx->pool,"%s%s/", fullhost, getenv("SCRIPT_NAME") ); http_response = mapcache_core_get_capabilities(ctx,request->service,req,url,pathInfo,ctx->config); } else if( request->type == MAPCACHE_REQUEST_GET_TILE) { mapcache_request_get_tile *req_tile = (mapcache_request_get_tile*)request; http_response = mapcache_core_get_tile(ctx,req_tile); } else if( request->type == MAPCACHE_REQUEST_PROXY ) { mapcache_request_proxy *req_proxy = (mapcache_request_proxy*)request; http_response = mapcache_core_proxy_request(ctx, req_proxy); } else if( request->type == MAPCACHE_REQUEST_GET_MAP) { mapcache_request_get_map *req_map = (mapcache_request_get_map*)request; http_response = mapcache_core_get_map(ctx,req_map); } else if( request->type == MAPCACHE_REQUEST_GET_FEATUREINFO) { mapcache_request_get_feature_info *req_fi = (mapcache_request_get_feature_info*)request; http_response = mapcache_core_get_featureinfo(ctx,req_fi); #ifdef DEBUG } else { ctx->set_error(ctx,500,"###BUG### unknown request type"); #endif } if(GC_HAS_ERROR(ctx)) { fcgi_write_response(globalctx, mapcache_core_respond_to_error(ctx)); goto cleanup; } #ifdef DEBUG if(!http_response) { ctx->set_error(ctx,500,"###BUG### NULL response"); fcgi_write_response(globalctx, mapcache_core_respond_to_error(ctx)); goto cleanup; } #endif fcgi_write_response(globalctx,http_response); cleanup: #ifdef USE_FASTCGI apr_pool_destroy(ctx->pool); ctx->clear_errors(ctx); } #endif apr_pool_destroy(global_pool); apr_terminate(); return 0; }
static ngx_int_t ngx_http_mapcache_handler(ngx_http_request_t *r) { if (!(r->method & (NGX_HTTP_GET))) { return NGX_HTTP_NOT_ALLOWED; } mapcache_ngx_context *ngctx = ngx_http_get_module_loc_conf(r, ngx_http_mapcache_module); mapcache_context *ctx = (mapcache_context*)ngctx; apr_pool_t *main_pool = ctx->pool; apr_pool_create(&(ctx->pool),main_pool); ngctx->r = r; mapcache_request *request = NULL; mapcache_http_response *http_response; ngx_http_variable_value_t *pathinfovv = ngx_http_get_indexed_variable(r, pathinfo_index); char* pathInfo = apr_pstrndup(ctx->pool, (char*)pathinfovv->data, pathinfovv->len); char *sparams = apr_pstrndup(ctx->pool, (char*)r->args.data, r->args.len); apr_table_t *params = mapcache_http_parse_param_string(ctx, sparams); mapcache_service_dispatch_request(ctx,&request,pathInfo,params,ctx->config); if(GC_HAS_ERROR(ctx) || !request) { ngx_http_mapcache_write_response(ctx,r, mapcache_core_respond_to_error(ctx)); goto cleanup; } http_response = NULL; if(request->type == MAPCACHE_REQUEST_GET_CAPABILITIES) { mapcache_request_get_capabilities *req = (mapcache_request_get_capabilities*)request; ngx_http_variable_value_t *urlprefixvv = ngx_http_get_indexed_variable(r, urlprefix_index); char *url = apr_pstrcat(ctx->pool, "http://", apr_pstrndup(ctx->pool, (char*)r->headers_in.host->value.data, r->headers_in.host->value.len), apr_pstrndup(ctx->pool, (char*)urlprefixvv->data, urlprefixvv->len), "/", NULL ); http_response = mapcache_core_get_capabilities(ctx,request->service,req,url,pathInfo,ctx->config); } else if( request->type == MAPCACHE_REQUEST_GET_TILE) { mapcache_request_get_tile *req_tile = (mapcache_request_get_tile*)request; http_response = mapcache_core_get_tile(ctx,req_tile); } else if( request->type == MAPCACHE_REQUEST_PROXY ) { mapcache_request_proxy *req_proxy = (mapcache_request_proxy*)request; http_response = mapcache_core_proxy_request(ctx, req_proxy); } else if( request->type == MAPCACHE_REQUEST_GET_MAP) { mapcache_request_get_map *req_map = (mapcache_request_get_map*)request; http_response = mapcache_core_get_map(ctx,req_map); } else if( request->type == MAPCACHE_REQUEST_GET_FEATUREINFO) { mapcache_request_get_feature_info *req_fi = (mapcache_request_get_feature_info*)request; http_response = mapcache_core_get_featureinfo(ctx,req_fi); #ifdef DEBUG } else { ctx->set_error(ctx,500,"###BUG### unknown request type"); #endif } if(GC_HAS_ERROR(ctx)) { ngx_http_mapcache_write_response(ctx,r, mapcache_core_respond_to_error(ctx)); goto cleanup; } #ifdef DEBUG if(!http_response) { ctx->set_error(ctx,500,"###BUG### NULL response"); ngx_http_mapcache_write_response(ctx,r, mapcache_core_respond_to_error(ctx)); goto cleanup; } #endif ngx_http_mapcache_write_response(ctx,r,http_response); cleanup: ctx->clear_errors(ctx); apr_pool_destroy(ctx->pool); ctx->pool = main_pool; return NGX_HTTP_OK; }
static int mod_mapcache_request_handler(request_rec *r) { apr_table_t *params; mapcache_request *request = NULL; mapcache_context_apache_request *apache_ctx = NULL; mapcache_http_response *http_response = NULL; mapcache_context *global_ctx = NULL; if (!r->handler || strcmp(r->handler, "mapcache")) { return DECLINED; } if (r->method_number != M_GET && r->method_number != M_POST) { return HTTP_METHOD_NOT_ALLOWED; } apache_ctx = apache_request_context_create(r); global_ctx = (mapcache_context*)apache_ctx; global_ctx->supports_redirects = 1; global_ctx->headers_in = r->headers_in; params = mapcache_http_parse_param_string(global_ctx, r->args); mapcache_service_dispatch_request(global_ctx,&request,r->path_info,params,global_ctx->config); if(GC_HAS_ERROR(global_ctx) || !request) { return write_http_response(apache_ctx, mapcache_core_respond_to_error(global_ctx)); } if(request->type == MAPCACHE_REQUEST_GET_CAPABILITIES) { mapcache_request_get_capabilities *req_caps = (mapcache_request_get_capabilities*)request; request_rec *original; char *url; if(r->main) original = r->main; else original = r; url = ap_construct_url(r->pool,original->uri,original); /* * remove the path_info from the end of the url (we want the url of the base of the service) * TODO: is there an apache api to access this ? */ if(*(original->path_info) && strcmp(original->path_info,"/")) { char *end = strstr(url,original->path_info); if(end) { /* make sure our url ends with a single '/' */ if(*end == '/') { char *slash = end; while((*(--slash))=='/') end--; end++; } *end = '\0'; } } http_response = mapcache_core_get_capabilities(global_ctx,request->service,req_caps, url,original->path_info,global_ctx->config); } else if( request->type == MAPCACHE_REQUEST_GET_TILE) { mapcache_request_get_tile *req_tile = (mapcache_request_get_tile*)request; http_response = mapcache_core_get_tile(global_ctx,req_tile); } else if( request->type == MAPCACHE_REQUEST_PROXY ) { const char *buf; mapcache_request_proxy *req_proxy = (mapcache_request_proxy*)request; if(r->method_number == M_POST) { read_post_body(apache_ctx, req_proxy); if(GC_HAS_ERROR(global_ctx)) { return write_http_response(apache_ctx, mapcache_core_respond_to_error(global_ctx)); } if(!req_proxy->headers) { req_proxy->headers = apr_table_make(global_ctx->pool, 2); } apr_table_set(req_proxy->headers, "Content-Type", r->content_type); if((buf = apr_table_get(r->headers_in,"X-Forwarded-For"))) { #if (AP_SERVER_MAJORVERSION_NUMBER == 2) && (AP_SERVER_MINORVERSION_NUMBER < 4) apr_table_set(req_proxy->headers, "X-Forwarded-For", apr_psprintf(global_ctx->pool,"%s, %s", buf, r->connection->remote_ip)); #else apr_table_set(req_proxy->headers, "X-Forwarded-For", apr_psprintf(global_ctx->pool,"%s, %s", buf, r->connection->client_ip)); #endif } else { #if (AP_SERVER_MAJORVERSION_NUMBER == 2) && (AP_SERVER_MINORVERSION_NUMBER < 4) apr_table_set(req_proxy->headers, "X-Forwarded-For", r->connection->remote_ip); #else apr_table_set(req_proxy->headers, "X-Forwarded-For", r->connection->client_ip); #endif } if ((buf = apr_table_get(r->headers_in, "Host"))) { const char *buf2; if((buf2 = apr_table_get(r->headers_in,"X-Forwarded-Host"))) { apr_table_set(req_proxy->headers, "X-Forwarded-Host", apr_psprintf(global_ctx->pool,"%s, %s",buf2,buf)); } else { apr_table_set(req_proxy->headers, "X-Forwarded-Host", buf); } } if ((buf = apr_table_get(r->headers_in, "X-Forwarded-Server"))) { apr_table_set(req_proxy->headers, "X-Forwarded-Server", apr_psprintf(global_ctx->pool, "%s, %s", buf, r->server->server_hostname)); } else { apr_table_set(req_proxy->headers, "X-Forwarded-Server", r->server->server_hostname); } } http_response = mapcache_core_proxy_request(global_ctx, req_proxy); } else if( request->type == MAPCACHE_REQUEST_GET_MAP) { mapcache_request_get_map *req_map = (mapcache_request_get_map*)request; http_response = mapcache_core_get_map(global_ctx,req_map); } else if( request->type == MAPCACHE_REQUEST_GET_FEATUREINFO) { mapcache_request_get_feature_info *req_fi = (mapcache_request_get_feature_info*)request; http_response = mapcache_core_get_featureinfo(global_ctx,req_fi); } else { global_ctx->set_error(global_ctx,500,"###BUG### unknown request type"); } if(GC_HAS_ERROR(global_ctx)) { return write_http_response(apache_ctx, mapcache_core_respond_to_error(global_ctx)); } return write_http_response(apache_ctx,http_response); }