| 1 |
|
| 2 |
/* |
| 3 |
* Copyright (C) Andreas Hauser |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <ngx_config.h> |
| 7 |
#include <ngx_core.h> |
| 8 |
#include <ngx_http.h> |
| 9 |
|
| 10 |
/* |
| 11 |
#define _GNU_SOURCE 1 |
| 12 |
#define _LARGEFILE64_SOURCE 1 |
| 13 |
#define _FILE_OFFSET_BITS 64 |
| 14 |
|
| 15 |
#include <limits.h> |
| 16 |
#include <stdlib.h> |
| 17 |
|
| 18 |
*/ |
| 19 |
|
| 20 |
#include <ffindex.h> |
| 21 |
#include <ffutil.h> |
| 22 |
|
| 23 |
typedef struct ffindex_config |
| 24 |
{ |
| 25 |
ngx_str_t ffdata_filename; |
| 26 |
ngx_str_t ffindex_filename; |
| 27 |
FILE * ffdata_file; |
| 28 |
FILE * ffindex_file; |
| 29 |
char * ffdata; |
| 30 |
size_t ffdata_size; |
| 31 |
ffindex_index_t * ffindex; |
| 32 |
} ffindex_config_t; |
| 33 |
|
| 34 |
|
| 35 |
//static char * ngx_http_ffindex_setup(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
| 36 |
static void * ngx_http_ffindex_create_loc_conf(ngx_conf_t *cf); |
| 37 |
static char * ngx_http_ffindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child); |
| 38 |
static ngx_int_t ngx_http_ffindex_handler(ngx_http_request_t *req); |
| 39 |
|
| 40 |
static ngx_command_t ngx_http_ffindex_commands[] = |
| 41 |
{ |
| 42 |
|
| 43 |
{ |
| 44 |
ngx_string("ffdata_filename"), |
| 45 |
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
| 46 |
ngx_conf_set_str_slot, |
| 47 |
NGX_HTTP_LOC_CONF_OFFSET, |
| 48 |
offsetof(ffindex_config_t, ffdata_filename), |
| 49 |
NULL |
| 50 |
}, |
| 51 |
|
| 52 |
{ |
| 53 |
ngx_string("ffindex_filename"), |
| 54 |
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
| 55 |
ngx_conf_set_str_slot, |
| 56 |
NGX_HTTP_LOC_CONF_OFFSET, |
| 57 |
offsetof(ffindex_config_t, ffindex_filename), |
| 58 |
NULL |
| 59 |
}, |
| 60 |
|
| 61 |
ngx_null_command |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static ngx_http_module_t ngx_http_ffindex_module_ctx = { |
| 67 |
NULL, /* preconfiguration */ |
| 68 |
NULL, /* postconfiguration */ |
| 69 |
|
| 70 |
NULL, /* create main configuration */ |
| 71 |
NULL, /* init main configuration */ |
| 72 |
|
| 73 |
NULL, /* create server configuration */ |
| 74 |
NULL, /* merge server configuration */ |
| 75 |
|
| 76 |
ngx_http_ffindex_create_loc_conf, /* create location configuration */ |
| 77 |
ngx_http_ffindex_merge_loc_conf /* merge location configuration */ |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
ngx_module_t ngx_http_ffindex_module = { |
| 82 |
NGX_MODULE_V1, |
| 83 |
&ngx_http_ffindex_module_ctx, /* module context */ |
| 84 |
ngx_http_ffindex_commands, /* module directives */ |
| 85 |
NGX_HTTP_MODULE, /* module type */ |
| 86 |
NULL, /* init master */ |
| 87 |
NULL, /* init module */ |
| 88 |
NULL, /* init process */ |
| 89 |
NULL, /* init thread */ |
| 90 |
NULL, /* exit thread */ |
| 91 |
NULL, /* exit process */ |
| 92 |
NULL, /* exit master */ |
| 93 |
NGX_MODULE_V1_PADDING |
| 94 |
}; |
| 95 |
|
| 96 |
static void * ngx_http_ffindex_create_loc_conf(ngx_conf_t *cf) |
| 97 |
{ |
| 98 |
ffindex_config_t *mcf; |
| 99 |
|
| 100 |
mcf = ngx_palloc(cf->pool, sizeof(ffindex_config_t)); |
| 101 |
if (mcf == NULL) { |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
|
| 105 |
mcf->ffdata_filename.len = 0; |
| 106 |
mcf->ffindex_filename.len = 0; |
| 107 |
|
| 108 |
return mcf; |
| 109 |
} |
| 110 |
|
| 111 |
static char * ngx_http_ffindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) |
| 112 |
{ |
| 113 |
ffindex_config_t * parent_config = parent; |
| 114 |
ffindex_config_t * child_config = child; |
| 115 |
if(child_config->ffdata_filename.len && child_config->ffindex_filename.len) |
| 116 |
{ |
| 117 |
size_t offset; |
| 118 |
fprintf(stderr, "plen: %ld, clen: %s %ld\n", parent_config->ffdata_filename.len, child_config->ffdata_filename.data, child_config->ffdata_filename.len); |
| 119 |
//ffindex_index_open(child_config->ffdata_filename.data, child_config->ffindex_filename.data, "r", &data_file, &index_file, &offset); |
| 120 |
|
| 121 |
child_config->ffdata_file = fopen(child_config->ffdata_filename.data, "r"); |
| 122 |
child_config->ffindex_file = fopen(child_config->ffindex_filename.data, "r"); |
| 123 |
|
| 124 |
if( child_config->ffdata_file == NULL) |
| 125 |
{ |
| 126 |
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "error ffdata file"); |
| 127 |
return NGX_CONF_ERROR; |
| 128 |
} |
| 129 |
if(child_config->ffindex_file == NULL) |
| 130 |
{ |
| 131 |
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "error ffindex file"); |
| 132 |
return NGX_CONF_ERROR; |
| 133 |
} |
| 134 |
|
| 135 |
size_t data_size; |
| 136 |
char *data = ffindex_mmap_data(child_config->ffdata_file, &data_size); |
| 137 |
child_config->ffdata_size = data_size; |
| 138 |
child_config->ffdata = data; |
| 139 |
|
| 140 |
ffindex_index_t* index = ffindex_index_parse(child_config->ffindex_file, 0); |
| 141 |
if(index == NULL) |
| 142 |
{ |
| 143 |
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "error parsing ffindex file"); |
| 144 |
return NGX_CONF_ERROR; |
| 145 |
} |
| 146 |
child_config->ffindex = index; |
| 147 |
|
| 148 |
ngx_http_core_loc_conf_t *clcf; |
| 149 |
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); |
| 150 |
clcf->handler = ngx_http_ffindex_handler; |
| 151 |
} |
| 152 |
|
| 153 |
return NGX_CONF_OK; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
static ngx_str_t ngx_http_ffindex_type = ngx_string("text/plain"); |
| 158 |
|
| 159 |
static ngx_int_t ngx_http_ffindex_handler(ngx_http_request_t *req) |
| 160 |
{ |
| 161 |
|
| 162 |
ffindex_config_t * ffindex_config = ngx_http_get_module_loc_conf(req, ngx_http_ffindex_module); |
| 163 |
|
| 164 |
if (!(req->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) { |
| 165 |
return NGX_HTTP_NOT_ALLOWED; |
| 166 |
} |
| 167 |
|
| 168 |
if(strncmp("get_data_by_index=", req->args.data, req->args.len)) |
| 169 |
{ |
| 170 |
size_t index = atol(req->args.data + sizeof("get_data_by_index=")) + 1; // index from 0 request from 1 |
| 171 |
ffindex_entry_t * entry = ffindex_get_entry_by_index(ffindex_config->ffindex, index); |
| 172 |
if(entry) |
| 173 |
{ |
| 174 |
char * data = ffindex_get_data_by_entry(ffindex_config->ffdata, entry); |
| 175 |
|
| 176 |
ngx_http_complex_value_t cv; |
| 177 |
ngx_memzero(&cv, sizeof(ngx_http_complex_value_t)); |
| 178 |
cv.value.data = data; |
| 179 |
cv.value.len = entry->length; |
| 180 |
|
| 181 |
return ngx_http_send_response(req, NGX_HTTP_OK, &ngx_http_ffindex_type, &cv); |
| 182 |
} |
| 183 |
else |
| 184 |
return NGX_HTTP_NOT_ALLOWED; |
| 185 |
} |
| 186 |
else if(strncmp("get_data_by_name=", req->args.data, req->args.len)) |
| 187 |
{ |
| 188 |
char * name = req->args.data + sizeof("get_data_by_name="); |
| 189 |
ffindex_entry_t * entry = ffindex_get_entry_by_name(ffindex_config->ffindex, name); |
| 190 |
if(entry) |
| 191 |
{ |
| 192 |
char * data = ffindex_get_data_by_entry(ffindex_config->ffdata, entry); |
| 193 |
|
| 194 |
ngx_http_complex_value_t cv; |
| 195 |
ngx_memzero(&cv, sizeof(ngx_http_complex_value_t)); |
| 196 |
cv.value.data = data; |
| 197 |
cv.value.len = entry->length; |
| 198 |
|
| 199 |
return ngx_http_send_response(req, NGX_HTTP_OK, &ngx_http_ffindex_type, &cv); |
| 200 |
} |
| 201 |
else |
| 202 |
return NGX_HTTP_NOT_ALLOWED; |
| 203 |
} |
| 204 |
else |
| 205 |
return NGX_HTTP_NOT_ALLOWED; |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
|