File: ngx_http_mapcache_module.c

package info (click to toggle)
mapcache 1.14.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,844 kB
  • sloc: ansic: 33,609; xml: 889; sh: 183; makefile: 61; python: 48
file content (326 lines) | stat: -rw-r--r-- 10,946 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "../include/mapcache.h"
#include <apr_date.h>
#include <apr_strings.h>
#include <apr_pools.h>


apr_pool_t *process_pool = NULL;
static char *ngx_http_mapcache(ngx_conf_t *cf, ngx_command_t *cmd,
                               void *conf);

static ngx_command_t  ngx_http_mapcache_commands[] = {

  {
    ngx_string("mapcache"),
    NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
    ngx_http_mapcache,
    NGX_HTTP_LOC_CONF_OFFSET,
    0,
    NULL
  },

  ngx_null_command
};

typedef struct {
  mapcache_context ctx;
  ngx_http_request_t *r;
} mapcache_ngx_context;

static void ngx_mapcache_context_log(mapcache_context *c, mapcache_log_level level, char *message, ...)
{
  mapcache_ngx_context *ctx = (mapcache_ngx_context*)c;
  va_list args;
  if(!c->config || level >= c->config->loglevel) {
    va_start(args,message);
    ngx_log_error(NGX_LOG_ALERT, ctx->r->connection->log, 0,
                  apr_pvsprintf(c->pool,message,args));
    va_end(args);
  }
}

static mapcache_context* ngx_mapcache_context_clone(mapcache_context *ctx)
{
  mapcache_context *nctx = (mapcache_context*)apr_pcalloc(ctx->pool,
                           sizeof(mapcache_ngx_context));
  mapcache_context_copy(ctx,nctx);
  ((mapcache_ngx_context*)nctx)->r = ((mapcache_ngx_context*)ctx)->r;
  apr_pool_create(&nctx->pool,ctx->pool);
  return nctx;
}


static void *
ngx_http_mapcache_create_conf(ngx_conf_t *cf)
{
  apr_initialize();
  atexit(apr_terminate);
  apr_pool_initialize();
  apr_pool_create(&process_pool,NULL);
  mapcache_context *ctx = apr_pcalloc(process_pool, sizeof(mapcache_ngx_context));
  ctx->pool = process_pool;
  ctx->connection_pool = NULL;
  mapcache_context_init(ctx);
  ctx->log = ngx_mapcache_context_log;
  ctx->clone = ngx_mapcache_context_clone;
  ctx->config = NULL;


  return ctx;
}


static void ngx_http_mapcache_write_response(mapcache_context *ctx, ngx_http_request_t *r,
    mapcache_http_response *response)
{
  if(response->mtime) {
    time_t  if_modified_since;
    if(r->headers_in.if_modified_since) {
      if_modified_since = ngx_http_parse_time(r->headers_in.if_modified_since->value.data,
                                              r->headers_in.if_modified_since->value.len);
      if (if_modified_since != NGX_ERROR) {
        apr_time_t apr_if_m_s;
        apr_time_ansi_put ( &apr_if_m_s, if_modified_since);
        if(apr_if_m_s<response->mtime) {
          r->headers_out.status = NGX_HTTP_NOT_MODIFIED;
          ngx_http_send_header(r);
          return;
        }
      }
    }
    char *datestr;
    datestr = apr_palloc(ctx->pool, APR_RFC822_DATE_LEN);
    apr_rfc822_date(datestr, response->mtime);
    apr_table_setn(response->headers,"Last-Modified",datestr);
  }
  if(response->headers && !apr_is_empty_table(response->headers)) {
    const apr_array_header_t *elts = apr_table_elts(response->headers);
    int i;
    for(i=0; i<elts->nelts; i++) {
      apr_table_entry_t entry = APR_ARRAY_IDX(elts,i,apr_table_entry_t);
      if(!strcasecmp(entry.key,"Content-Type")) {
        r->headers_out.content_type.len = strlen(entry.val);
        r->headers_out.content_type.data = (u_char*)entry.val;
      } else {
        ngx_table_elt_t   *h;
        h = ngx_list_push(&r->headers_out.headers);
        if (h == NULL) {
          return;
        }
        h->key.len = strlen(entry.key) ;
        h->key.data = (u_char*)entry.key ;
        h->value.len = strlen(entry.val) ;
        h->value.data = (u_char*)entry.val ;
        h->hash = 1;
      }
    }
  }
  if(response->data) {
    r->headers_out.content_length_n = response->data->size;
  }
  int rc;
  r->headers_out.status = response->code;
  rc = ngx_http_send_header(r);
  if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
    return;
  }

  if(response->data) {
    ngx_buf_t    *b;
    ngx_chain_t   out;
    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
    if (b == NULL) {
      ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                    "Failed to allocate response buffer.");
      return;
    }

    b->pos = ngx_pcalloc(r->pool,response->data->size);
    memcpy(b->pos,response->data->buf,response->data->size);
    b->last = b->pos + response->data->size;
    b->memory = 1;
    b->last_buf = 1;
    b->flush = 1;
    out.buf = b;
    out.next = NULL;
    ngx_http_output_filter(r, &out);
  }

}


static ngx_http_module_t  ngx_http_mapcache_module_ctx = {
  NULL,                          /* preconfiguration */
  NULL,                          /* postconfiguration */

  NULL,                          /* create main configuration */
  NULL,                          /* init main configuration */

  NULL,                          /* create server configuration */
  NULL,                          /* merge server configuration */

  ngx_http_mapcache_create_conf, /* create location configuration */
  NULL                           /* merge location configuration */
};

static ngx_int_t ngx_mapcache_init_process(ngx_cycle_t *cycle)
{
  apr_initialize();
  atexit(apr_terminate);
  apr_pool_initialize();
  apr_pool_create(&process_pool,NULL);
  return NGX_OK;
}

static void ngx_mapcache_exit_process(ngx_cycle_t *cycle)
{
  apr_pool_destroy(process_pool);
}

ngx_module_t  ngx_http_mapcache_module = {
  NGX_MODULE_V1,
  &ngx_http_mapcache_module_ctx, /* module context */
  ngx_http_mapcache_commands,   /* module directives */
  NGX_HTTP_MODULE,               /* module type */
  NULL,                          /* init master */
  NULL,                          /* init module */
  ngx_mapcache_init_process,/* init process */
  NULL,                          /* init thread */
  NULL,                          /* exit thread */
  ngx_mapcache_exit_process,                          /* exit process */
  ngx_mapcache_exit_process,                          /* exit master */
  NGX_MODULE_V1_PADDING
};

static ngx_str_t  pathinfo_str = ngx_string("path_info");
static ngx_int_t pathinfo_index;
static ngx_str_t  urlprefix_str = ngx_string("url_prefix");
static ngx_int_t urlprefix_index;

static ngx_int_t
ngx_http_mapcache_handler(ngx_http_request_t *r)
{
  int ret = NGX_HTTP_OK;
  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_create(&(ctx->pool),process_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_GET_MAP) {
    mapcache_request_get_map *req_map = (mapcache_request_get_map*)request;
    http_response = mapcache_core_get_map(ctx,req_map);
#ifdef NGINX_RW
  } 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_FEATUREINFO) {
    mapcache_request_get_feature_info *req_fi = (mapcache_request_get_feature_info*)request;
    http_response = mapcache_core_get_featureinfo(ctx,req_fi);
#endif
#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:
  if(GC_HAS_ERROR(ctx))
    ret = ctx->_errcode?ctx->_errcode:500;
  ctx->clear_errors(ctx);
  apr_pool_destroy(ctx->pool);
  return ret;
}


static char *
ngx_http_mapcache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
  mapcache_context *ctx = conf;
  ngx_str_t *value;
  value = cf->args->elts;
  char *conffile = (char*)value[1].data;
  ctx->config = mapcache_configuration_create(ctx->pool);
  mapcache_configuration_parse(ctx,conffile,ctx->config,1);
  if(GC_HAS_ERROR(ctx)) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,ctx->get_error_message(ctx));
    return NGX_CONF_ERROR;
  }
  mapcache_configuration_post_config(ctx, ctx->config);
  if(GC_HAS_ERROR(ctx)) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,ctx->get_error_message(ctx));
    return NGX_CONF_ERROR;
  }
  if(mapcache_config_services_enabled(ctx, ctx->config) <= 0) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "no mapcache <service>s configured/enabled, no point in continuing.");
    return NGX_CONF_ERROR;
  }
  mapcache_cache_child_init(ctx,ctx->config,ctx->pool);
  if(GC_HAS_ERROR(ctx)) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,ctx->get_error_message(ctx));
    return NGX_CONF_ERROR;
  }
  mapcache_connection_pool_create(ctx->config, &ctx->connection_pool,ctx->pool);
  ctx->config->non_blocking = 1;

  ngx_http_core_loc_conf_t  *clcf;

  clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  clcf->handler = ngx_http_mapcache_handler;

  pathinfo_index = ngx_http_get_variable_index(cf, &pathinfo_str);
  if (pathinfo_index == NGX_ERROR) {
    return NGX_CONF_ERROR;
  }
  urlprefix_index = ngx_http_get_variable_index(cf, &urlprefix_str);
  if (urlprefix_index == NGX_ERROR) {
    return NGX_CONF_ERROR;
  }

  return NGX_CONF_OK;
}