comparison src/http/modules/ngx_http_xslt_filter_module.c @ 2156:0c6d0bd60579

reuse compiled DTD hash in different locations, add DTD cleanup
author Igor Sysoev <igor@sysoev.ru>
date Tue, 05 Aug 2008 19:05:15 +0000
parents 4f48a2765da0
children 18e85d78fb0c
comparison
equal deleted inserted replaced
2155:febb71974a35 2156:0c6d0bd60579
21 #endif 21 #endif
22 22
23 23
24 typedef struct { 24 typedef struct {
25 u_char *name; 25 u_char *name;
26 xsltStylesheetPtr stylesheet; 26 void *data;
27 } ngx_http_xslt_sheet_file_t; 27 } ngx_http_xslt_file_t;
28 28
29 29
30 typedef struct { 30 typedef struct {
31 ngx_array_t sheet_files; /* ngx_http_xslt_sheet_file_t */ 31 ngx_array_t dtd_files; /* ngx_http_xslt_file_t */
32 ngx_array_t sheet_files; /* ngx_http_xslt_file_t */
32 } ngx_http_xslt_filter_main_conf_t; 33 } ngx_http_xslt_filter_main_conf_t;
33 34
34 35
35 typedef struct { 36 typedef struct {
36 ngx_array_t *lengths; 37 ngx_array_t *lengths;
126 127
127 static char *ngx_http_xslt_entities(ngx_conf_t *cf, ngx_command_t *cmd, 128 static char *ngx_http_xslt_entities(ngx_conf_t *cf, ngx_command_t *cmd,
128 void *conf); 129 void *conf);
129 static char *ngx_http_xslt_stylesheet(ngx_conf_t *cf, ngx_command_t *cmd, 130 static char *ngx_http_xslt_stylesheet(ngx_conf_t *cf, ngx_command_t *cmd,
130 void *conf); 131 void *conf);
132 static void ngx_http_xslt_cleanup_dtd(void *data);
131 static void ngx_http_xslt_cleanup_stylesheet(void *data); 133 static void ngx_http_xslt_cleanup_stylesheet(void *data);
132 static void *ngx_http_xslt_filter_create_main_conf(ngx_conf_t *cf); 134 static void *ngx_http_xslt_filter_create_main_conf(ngx_conf_t *cf);
133 static void *ngx_http_xslt_filter_create_conf(ngx_conf_t *cf); 135 static void *ngx_http_xslt_filter_create_conf(ngx_conf_t *cf);
134 static char *ngx_http_xslt_filter_merge_conf(ngx_conf_t *cf, void *parent, 136 static char *ngx_http_xslt_filter_merge_conf(ngx_conf_t *cf, void *parent,
135 void *child); 137 void *child);
992 static char * 994 static char *
993 ngx_http_xslt_entities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 995 ngx_http_xslt_entities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
994 { 996 {
995 ngx_http_xslt_filter_loc_conf_t *xlcf = conf; 997 ngx_http_xslt_filter_loc_conf_t *xlcf = conf;
996 998
997 ngx_str_t *value; 999 ngx_str_t *value;
1000 ngx_uint_t i;
1001 ngx_pool_cleanup_t *cln;
1002 ngx_http_xslt_file_t *file;
1003 ngx_http_xslt_filter_main_conf_t *xmcf;
998 1004
999 if (xlcf->dtd) { 1005 if (xlcf->dtd) {
1000 return "is duplicate"; 1006 return "is duplicate";
1001 } 1007 }
1002 1008
1003 value = cf->args->elts; 1009 value = cf->args->elts;
1010
1011 xmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_xslt_filter_module);
1012
1013 file = xmcf->dtd_files.elts;
1014 for (i = 0; i < xmcf->dtd_files.nelts; i++) {
1015 if (ngx_strcmp(file[i].name, &value[1].data) == 0) {
1016 xlcf->dtd = file[i].data;
1017 return NGX_CONF_OK;
1018 }
1019 }
1020
1021 cln = ngx_pool_cleanup_add(cf->pool, 0);
1022 if (cln == NULL) {
1023 return NGX_CONF_ERROR;
1024 }
1004 1025
1005 xlcf->dtd = xmlParseDTD(NULL, (xmlChar *) value[1].data); 1026 xlcf->dtd = xmlParseDTD(NULL, (xmlChar *) value[1].data);
1006 1027
1007 if (xlcf->dtd == NULL) { 1028 if (xlcf->dtd == NULL) {
1008 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "xmlParseDTD() failed"); 1029 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "xmlParseDTD() failed");
1009 return NGX_CONF_ERROR; 1030 return NGX_CONF_ERROR;
1010 } 1031 }
1011 1032
1033 cln->handler = ngx_http_xslt_cleanup_dtd;
1034 cln->data = xlcf->dtd;
1035
1036 file = ngx_array_push(&xmcf->dtd_files);
1037 if (file == NULL) {
1038 return NGX_CONF_ERROR;
1039 }
1040
1041 file->name = value[1].data;
1042 file->data = xlcf->dtd;
1043
1012 return NGX_CONF_OK; 1044 return NGX_CONF_OK;
1013 } 1045 }
1014 1046
1015 1047
1016 1048
1020 ngx_http_xslt_filter_loc_conf_t *xlcf = conf; 1052 ngx_http_xslt_filter_loc_conf_t *xlcf = conf;
1021 1053
1022 ngx_str_t *value; 1054 ngx_str_t *value;
1023 ngx_uint_t i, n; 1055 ngx_uint_t i, n;
1024 ngx_pool_cleanup_t *cln; 1056 ngx_pool_cleanup_t *cln;
1057 ngx_http_xslt_file_t *file;
1025 ngx_http_xslt_sheet_t *sheet; 1058 ngx_http_xslt_sheet_t *sheet;
1026 ngx_http_xslt_param_t *param; 1059 ngx_http_xslt_param_t *param;
1027 ngx_http_script_compile_t sc; 1060 ngx_http_script_compile_t sc;
1028 ngx_http_xslt_sheet_file_t *file;
1029 ngx_http_xslt_filter_main_conf_t *xmcf; 1061 ngx_http_xslt_filter_main_conf_t *xmcf;
1030 1062
1031 value = cf->args->elts; 1063 value = cf->args->elts;
1032 1064
1033 if (xlcf->sheets.elts == NULL) { 1065 if (xlcf->sheets.elts == NULL) {
1053 xmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_xslt_filter_module); 1085 xmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_xslt_filter_module);
1054 1086
1055 file = xmcf->sheet_files.elts; 1087 file = xmcf->sheet_files.elts;
1056 for (i = 0; i < xmcf->sheet_files.nelts; i++) { 1088 for (i = 0; i < xmcf->sheet_files.nelts; i++) {
1057 if (ngx_strcmp(file[i].name, &value[1].data) == 0) { 1089 if (ngx_strcmp(file[i].name, &value[1].data) == 0) {
1058 sheet->stylesheet = file[i].stylesheet; 1090 sheet->stylesheet = file[i].data;
1059 goto found; 1091 goto found;
1060 } 1092 }
1061 } 1093 }
1062 1094
1063 cln = ngx_pool_cleanup_add(cf->pool, 0); 1095 cln = ngx_pool_cleanup_add(cf->pool, 0);
1080 if (file == NULL) { 1112 if (file == NULL) {
1081 return NGX_CONF_ERROR; 1113 return NGX_CONF_ERROR;
1082 } 1114 }
1083 1115
1084 file->name = value[1].data; 1116 file->name = value[1].data;
1085 file->stylesheet = sheet->stylesheet; 1117 file->data = sheet->stylesheet;
1086 1118
1087 found: 1119 found:
1088 1120
1089 n = cf->args->nelts; 1121 n = cf->args->nelts;
1090 1122
1127 return NGX_CONF_OK; 1159 return NGX_CONF_OK;
1128 } 1160 }
1129 1161
1130 1162
1131 static void 1163 static void
1164 ngx_http_xslt_cleanup_dtd(void *data)
1165 {
1166 xmlFreeDtd(data);
1167 }
1168
1169
1170 static void
1132 ngx_http_xslt_cleanup_stylesheet(void *data) 1171 ngx_http_xslt_cleanup_stylesheet(void *data)
1133 { 1172 {
1134 xsltStylesheetPtr stylesheet = data; 1173 xsltFreeStylesheet(data);
1135
1136 xsltFreeStylesheet(stylesheet);
1137 } 1174 }
1138 1175
1139 1176
1140 static void * 1177 static void *
1141 ngx_http_xslt_filter_create_main_conf(ngx_conf_t *cf) 1178 ngx_http_xslt_filter_create_main_conf(ngx_conf_t *cf)
1145 conf = ngx_palloc(cf->pool, sizeof(ngx_http_xslt_filter_main_conf_t)); 1182 conf = ngx_palloc(cf->pool, sizeof(ngx_http_xslt_filter_main_conf_t));
1146 if (conf == NULL) { 1183 if (conf == NULL) {
1147 return NGX_CONF_ERROR; 1184 return NGX_CONF_ERROR;
1148 } 1185 }
1149 1186
1187 if (ngx_array_init(&conf->dtd_files, cf->pool, 1,
1188 sizeof(ngx_http_xslt_file_t))
1189 != NGX_OK)
1190 {
1191 return NULL;
1192 }
1193
1150 if (ngx_array_init(&conf->sheet_files, cf->pool, 1, 1194 if (ngx_array_init(&conf->sheet_files, cf->pool, 1,
1151 sizeof(ngx_http_xslt_sheet_file_t)) 1195 sizeof(ngx_http_xslt_file_t))
1152 != NGX_OK) 1196 != NGX_OK)
1153 { 1197 {
1154 return NULL; 1198 return NULL;
1155 } 1199 }
1156 1200