Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ngx_http_uploadprogress_module.c 55KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  1. /*
  2. * Copyright (C) 2007 Brice Figureau
  3. * shm_zone and rbtree code Copyright (c) 2002-2007 Igor Sysoev
  4. */
  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>
  8. #define TIMER_FREQUENCY 15 * 1000
  9. typedef enum {
  10. uploadprogress_state_starting = 0,
  11. uploadprogress_state_error = 1,
  12. uploadprogress_state_done = 2,
  13. uploadprogress_state_uploading = 3,
  14. uploadprogress_state_none
  15. } ngx_http_uploadprogress_state_t;
  16. typedef struct {
  17. ngx_str_t name;
  18. ngx_http_uploadprogress_state_t idx;
  19. } ngx_http_uploadprogress_state_map_t;
  20. typedef struct ngx_http_uploadprogress_node_s ngx_http_uploadprogress_node_t;
  21. struct ngx_http_uploadprogress_node_s {
  22. ngx_rbtree_node_t node;
  23. ngx_uint_t err_status;
  24. off_t rest;
  25. off_t length;
  26. ngx_uint_t done;
  27. time_t timeout;
  28. struct ngx_http_uploadprogress_node_s *prev;
  29. struct ngx_http_uploadprogress_node_s *next;
  30. u_char len;
  31. u_char data[1];
  32. };
  33. typedef struct {
  34. ngx_shm_zone_t *shm_zone;
  35. ngx_rbtree_node_t *node;
  36. ngx_http_request_t *r;
  37. time_t timeout;
  38. } ngx_http_uploadprogress_cleanup_t;
  39. typedef struct {
  40. ngx_rbtree_t *rbtree;
  41. ngx_http_uploadprogress_node_t list_head;
  42. ngx_http_uploadprogress_node_t list_tail;
  43. } ngx_http_uploadprogress_ctx_t;
  44. typedef struct {
  45. ngx_array_t *values;
  46. ngx_array_t *lengths;
  47. } ngx_http_uploadprogress_template_t;
  48. typedef struct {
  49. ngx_shm_zone_t *shm_zone;
  50. time_t timeout;
  51. ngx_event_t cleanup;
  52. ngx_http_handler_pt handler;
  53. u_char track;
  54. ngx_str_t content_type;
  55. ngx_array_t templates;
  56. ngx_str_t header;
  57. ngx_str_t jsonp_parameter;
  58. } ngx_http_uploadprogress_conf_t;
  59. typedef struct {
  60. ngx_http_event_handler_pt read_event_handler;
  61. } ngx_http_uploadprogress_module_ctx_t;
  62. static ngx_int_t ngx_http_reportuploads_handler(ngx_http_request_t *r);
  63. static void ngx_http_uploadprogress_cleanup(void *data);
  64. static char *ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  65. static ngx_int_t ngx_http_uploadprogress_init_zone(ngx_shm_zone_t * shm_zone, void *data);
  66. static ngx_int_t ngx_http_uploadprogress_init(ngx_conf_t * cf);
  67. static void *ngx_http_uploadprogress_create_loc_conf(ngx_conf_t *cf);
  68. static char *ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
  69. static ngx_int_t ngx_http_uploadprogress_init_variables_and_templates(ngx_conf_t *cf);
  70. static ngx_int_t ngx_http_uploadprogress_received_variable(ngx_http_request_t *r,
  71. ngx_http_variable_value_t *v, uintptr_t data);
  72. static ngx_int_t ngx_http_uploadprogress_offset_variable(ngx_http_request_t *r,
  73. ngx_http_variable_value_t *v, uintptr_t data);
  74. static ngx_int_t ngx_http_uploadprogress_status_variable(ngx_http_request_t *r,
  75. ngx_http_variable_value_t *v, uintptr_t data);
  76. static ngx_int_t ngx_http_uploadprogress_callback_variable(ngx_http_request_t *r,
  77. ngx_http_variable_value_t *v, uintptr_t data);
  78. static char* ngx_http_upload_progress_set_template(ngx_conf_t * cf, ngx_http_uploadprogress_template_t *t, ngx_str_t *source);
  79. static char *ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  80. static char *ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  81. static char *ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  82. static char* ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  83. static char* ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  84. static char* ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  85. static void ngx_clean_old_connections(ngx_event_t * ev);
  86. static ngx_int_t ngx_http_uploadprogress_content_handler(ngx_http_request_t *r);
  87. static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
  88. static ngx_command_t ngx_http_uploadprogress_commands[] = {
  89. {ngx_string("upload_progress"),
  90. NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE2,
  91. ngx_http_upload_progress,
  92. 0,
  93. 0,
  94. NULL},
  95. {ngx_string("track_uploads"),
  96. NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2,
  97. ngx_http_track_uploads,
  98. NGX_HTTP_LOC_CONF_OFFSET,
  99. 0,
  100. NULL},
  101. {ngx_string("report_uploads"),
  102. NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
  103. ngx_http_report_uploads,
  104. NGX_HTTP_LOC_CONF_OFFSET,
  105. 0,
  106. NULL},
  107. {ngx_string("upload_progress_content_type"),
  108. NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
  109. ngx_conf_set_str_slot,
  110. NGX_HTTP_LOC_CONF_OFFSET,
  111. offsetof(ngx_http_uploadprogress_conf_t, content_type),
  112. NULL},
  113. {ngx_string("upload_progress_template"),
  114. NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2,
  115. ngx_http_upload_progress_template,
  116. NGX_HTTP_LOC_CONF_OFFSET,
  117. offsetof(ngx_http_uploadprogress_conf_t, templates),
  118. NULL},
  119. {ngx_string("upload_progress_json_output"),
  120. NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
  121. ngx_http_upload_progress_json_output,
  122. NGX_HTTP_LOC_CONF_OFFSET,
  123. 0,
  124. NULL},
  125. {ngx_string("upload_progress_jsonp_output"),
  126. NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
  127. ngx_http_upload_progress_jsonp_output,
  128. NGX_HTTP_LOC_CONF_OFFSET,
  129. 0,
  130. NULL},
  131. {ngx_string("upload_progress_header"),
  132. NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
  133. ngx_conf_set_str_slot,
  134. NGX_HTTP_LOC_CONF_OFFSET,
  135. offsetof(ngx_http_uploadprogress_conf_t, header),
  136. NULL},
  137. {ngx_string("upload_progress_jsonp_parameter"),
  138. NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
  139. ngx_conf_set_str_slot,
  140. NGX_HTTP_LOC_CONF_OFFSET,
  141. offsetof(ngx_http_uploadprogress_conf_t, jsonp_parameter),
  142. NULL},
  143. ngx_null_command
  144. };
  145. static ngx_http_variable_t ngx_http_uploadprogress_variables[] = {
  146. { ngx_string("uploadprogress_received"), NULL, ngx_http_uploadprogress_received_variable,
  147. (uintptr_t) offsetof(ngx_http_uploadprogress_node_t, rest),
  148. NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
  149. { ngx_string("uploadprogress_remaining"), NULL, ngx_http_uploadprogress_offset_variable,
  150. (uintptr_t) offsetof(ngx_http_uploadprogress_node_t, rest),
  151. NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
  152. { ngx_string("uploadprogress_length"), NULL, ngx_http_uploadprogress_offset_variable,
  153. (uintptr_t) offsetof(ngx_http_uploadprogress_node_t, length),
  154. NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
  155. { ngx_string("uploadprogress_status"), NULL, ngx_http_uploadprogress_status_variable,
  156. (uintptr_t) offsetof(ngx_http_uploadprogress_node_t, err_status),
  157. NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
  158. { ngx_string("uploadprogress_callback"), NULL, ngx_http_uploadprogress_callback_variable,
  159. (uintptr_t) NULL,
  160. NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
  161. { ngx_null_string, NULL, NULL, 0, 0, 0 }
  162. };
  163. static ngx_http_module_t ngx_http_uploadprogress_module_ctx = {
  164. ngx_http_uploadprogress_init_variables_and_templates, /* preconfiguration */
  165. ngx_http_uploadprogress_init, /* postconfiguration */
  166. NULL, /* create main configuration */
  167. NULL, /* init main configuration */
  168. NULL, /* create server configuration */
  169. NULL, /* merge server configuration */
  170. ngx_http_uploadprogress_create_loc_conf, /* create location configuration */
  171. ngx_http_uploadprogress_merge_loc_conf /* merge location configuration */
  172. };
  173. ngx_module_t ngx_http_uploadprogress_module = {
  174. NGX_MODULE_V1,
  175. &ngx_http_uploadprogress_module_ctx, /* module context */
  176. ngx_http_uploadprogress_commands, /* module directives */
  177. NGX_HTTP_MODULE, /* module type */
  178. NULL, /* init master */
  179. NULL, /* init module */
  180. NULL, /* init process */
  181. NULL, /* init thread */
  182. NULL, /* exit thread */
  183. NULL, /* exit process */
  184. NULL, /* exit master */
  185. NGX_MODULE_V1_PADDING
  186. };
  187. static ngx_http_uploadprogress_state_map_t ngx_http_uploadprogress_state_map[] = {
  188. {ngx_string("starting"), uploadprogress_state_starting},
  189. {ngx_string("error"), uploadprogress_state_error},
  190. {ngx_string("done"), uploadprogress_state_done},
  191. {ngx_string("uploading"), uploadprogress_state_uploading},
  192. {ngx_null_string, uploadprogress_state_none},
  193. };
  194. static ngx_str_t ngx_http_uploadprogress_java_defaults[] = {
  195. ngx_string("new Object({ 'state' : 'starting' })\r\n"),
  196. ngx_string("new Object({ 'state' : 'error', 'status' : $uploadprogress_status })\r\n"),
  197. ngx_string("new Object({ 'state' : 'done' })\r\n"),
  198. ngx_string("new Object({ 'state' : 'uploading', 'received' : $uploadprogress_received, 'size' : $uploadprogress_length })\r\n")
  199. };
  200. static ngx_str_t ngx_http_uploadprogress_json_defaults[] = {
  201. ngx_string("{ \"state\" : \"starting\" }\r\n"),
  202. ngx_string("{ \"state\" : \"error\", \"status\" : $uploadprogress_status }\r\n"),
  203. ngx_string("{ \"state\" : \"done\" }\r\n"),
  204. ngx_string("{ \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length }\r\n")
  205. };
  206. static ngx_str_t ngx_http_uploadprogress_jsonp_defaults[] = {
  207. ngx_string("$uploadprogress_callback({ \"state\" : \"starting\" });\r\n"),
  208. ngx_string("$uploadprogress_callback({ \"state\" : \"error\", \"status\" : $uploadprogress_status });\r\n"),
  209. ngx_string("$uploadprogress_callback({ \"state\" : \"done\" });\r\n"),
  210. ngx_string("$uploadprogress_callback({ \"state\" : \"uploading\", \"received\" : $uploadprogress_received, \"size\" : $uploadprogress_length });\r\n")
  211. };
  212. static ngx_array_t ngx_http_uploadprogress_global_templates;
  213. static ngx_str_t*
  214. get_tracking_id(ngx_http_request_t * r)
  215. {
  216. u_char *p, *start_p;
  217. ngx_uint_t i;
  218. ngx_list_part_t *part;
  219. ngx_table_elt_t *header;
  220. ngx_str_t *ret, args;
  221. ngx_http_uploadprogress_conf_t *upcf;
  222. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  223. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: get_tracking_id");
  224. part = &r->headers_in.headers.part;
  225. header = part->elts;
  226. for (i = 0; /* void */ ; i++) {
  227. if (i >= part->nelts) {
  228. if (part->next == NULL) {
  229. break;
  230. }
  231. part = part->next;
  232. header = part->elts;
  233. i = 0;
  234. }
  235. if (header[i].key.len == upcf->header.len
  236. && ngx_strncasecmp(header[i].key.data, upcf->header.data,
  237. header[i].key.len) == 0) {
  238. ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log );
  239. ret->data = header[i].value.data;
  240. ret->len = header[i].value.len;
  241. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  242. "upload-progress: get_tracking_id found header: %V", ret);
  243. return ret;
  244. }
  245. }
  246. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  247. "upload-progress: get_tracking_id no header found");
  248. /* not found, check as a request arg */
  249. /* it is possible the request args have not been yet created (or already released) */
  250. /* so let's try harder first from the request line */
  251. args.len = r->args.len;
  252. args.data = r->args.data;
  253. if (args.len && args.data) {
  254. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  255. "upload-progress: get_tracking_id no header found, args found");
  256. i = 0;
  257. p = args.data;
  258. do {
  259. ngx_uint_t len = args.len - (p - args.data);
  260. if (len >= (upcf->header.len + 1) && ngx_strncasecmp(p, upcf->header.data, upcf->header.len) == 0
  261. && p[upcf->header.len] == '=') {
  262. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  263. "upload-progress: get_tracking_id found args: %s",p);
  264. i = 1;
  265. break;
  266. }
  267. if (len<=0)
  268. break;
  269. }
  270. while(p++);
  271. if (i) {
  272. start_p = p += upcf->header.len + 1;
  273. while (p < args.data + args.len) {
  274. if (*((p++) + 1) == '&') {
  275. break;
  276. }
  277. }
  278. ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log);
  279. ret->data = start_p;
  280. ret->len = p - start_p;
  281. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  282. "upload-progress: get_tracking_id found args: %V",ret);
  283. return ret;
  284. }
  285. }
  286. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  287. "upload-progress: get_tracking_id no id found");
  288. return NULL;
  289. }
  290. static ngx_http_uploadprogress_node_t *
  291. find_node(ngx_str_t * id, ngx_http_uploadprogress_ctx_t * ctx, ngx_log_t * log)
  292. {
  293. uint32_t hash;
  294. ngx_rbtree_node_t *node, *sentinel;
  295. ngx_int_t rc;
  296. ngx_http_uploadprogress_node_t *up;
  297. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "upload-progress: find_node %V", id);
  298. hash = ngx_crc32_short(id->data, id->len);
  299. node = ctx->rbtree->root;
  300. sentinel = ctx->rbtree->sentinel;
  301. while (node != sentinel) {
  302. if (hash < node->key) {
  303. node = node->left;
  304. continue;
  305. }
  306. if (hash > node->key) {
  307. node = node->right;
  308. continue;
  309. }
  310. /* hash == node->key */
  311. do {
  312. up = (ngx_http_uploadprogress_node_t *) node;
  313. rc = ngx_memn2cmp(id->data, up->data, id->len, (size_t) up->len);
  314. if (rc == 0) {
  315. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0,
  316. "upload-progress: found node");
  317. return up;
  318. }
  319. node = (rc < 0) ? node->left : node->right;
  320. } while (node != sentinel && hash == node->key);
  321. /* found a key with unmatching hash (and value), let's keep comparing hashes then */
  322. }
  323. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "upload-progress: can't find node");
  324. return NULL;
  325. }
  326. static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r);
  327. static ngx_int_t
  328. ngx_http_uploadprogress_content_handler(ngx_http_request_t *r)
  329. {
  330. ngx_int_t rc;
  331. ngx_http_uploadprogress_module_ctx_t *ctx;
  332. ngx_http_uploadprogress_conf_t *upcf;
  333. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: ngx_http_uploadprogress_content_handler");
  334. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  335. /* call the original request handler */
  336. rc = upcf->handler(r);
  337. /* bail out if error */
  338. if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
  339. return rc;
  340. }
  341. /* request is OK, hijack the read_event_handler if the request has to be tracked*/
  342. ctx = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
  343. if (ctx != NULL) {
  344. ctx->read_event_handler = r->read_event_handler;
  345. r->read_event_handler = ngx_http_uploadprogress_event_handler;
  346. }
  347. return rc;
  348. }
  349. static ngx_str_t* ngx_http_uploadprogress_strdup(ngx_str_t *src, ngx_log_t * log)
  350. {
  351. ngx_str_t *dst;
  352. dst = ngx_alloc(src->len + sizeof(ngx_str_t), log);
  353. if (dst == NULL) {
  354. return NULL;
  355. }
  356. dst->len = src->len;
  357. ngx_memcpy(((char*)dst + sizeof(ngx_str_t)) , src->data, src->len);
  358. dst->data = ((u_char*)dst + sizeof(ngx_str_t));
  359. return dst;
  360. }
  361. static void ngx_http_uploadprogress_strdupfree(ngx_str_t *str)
  362. {
  363. ngx_free(str);
  364. }
  365. static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r)
  366. {
  367. ngx_str_t *id, *oldid;
  368. ngx_slab_pool_t *shpool;
  369. ngx_connection_t *c;
  370. ngx_shm_zone_t *shm_zone;
  371. ngx_http_uploadprogress_ctx_t *ctx;
  372. ngx_http_uploadprogress_node_t *up;
  373. ngx_http_uploadprogress_conf_t *upcf;
  374. ngx_http_uploadprogress_module_ctx_t *module_ctx;
  375. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: ngx_http_uploadprogress_event_handler");
  376. c = r->connection;
  377. /* find node, update rest */
  378. oldid = id = get_tracking_id(r);
  379. if (id == NULL) {
  380. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
  381. "upload-progress: read_event_handler cant find id");
  382. return;
  383. }
  384. /* perform a deep copy of id */
  385. id = ngx_http_uploadprogress_strdup(id, r->connection->log);
  386. ngx_free(oldid);
  387. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  388. "upload-progress: read_event_handler found id: %V", id);
  389. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  390. shm_zone = upcf->shm_zone;
  391. /* call the original read event handler */
  392. module_ctx = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
  393. if (module_ctx != NULL ) {
  394. module_ctx->read_event_handler(r);
  395. }
  396. /* at this stage, r is not anymore safe to use */
  397. /* the request could have been closed/freed behind our back */
  398. /* and thats the same issue with any other material that was allocated in the request pool */
  399. /* that's why we duplicate id afterward */
  400. /* it's also possible that the id was null if we got a spurious (like abort) read */
  401. /* event. In this case we still have called the original read event handler */
  402. /* but we have to bail out, because we won't ever be able to find our upload node */
  403. if (shm_zone == NULL) {
  404. ngx_http_uploadprogress_strdupfree(id);
  405. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
  406. "upload-progress: read_event_handler no shm_zone for id: %V", id);
  407. return;
  408. }
  409. ctx = shm_zone->data;
  410. /* get the original connection of the upload */
  411. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  412. ngx_shmtx_lock(&shpool->mutex);
  413. up = find_node(id, ctx, ngx_cycle->log);
  414. if (up != NULL && !up->done) {
  415. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
  416. "upload-progress: read_event_handler found node: %V", id);
  417. up->rest = r->request_body->rest;
  418. if(up->length == 0)
  419. up->length = r->headers_in.content_length_n;
  420. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
  421. "upload-progress: read_event_handler storing rest %uO/%uO for %V", up->rest, up->length, id);
  422. } else {
  423. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
  424. "upload-progress: read_event_handler not found: %V", id);
  425. }
  426. ngx_shmtx_unlock(&shpool->mutex);
  427. ngx_http_uploadprogress_strdupfree(id);
  428. }
  429. /* This generates the response for the report */
  430. static ngx_int_t
  431. ngx_http_reportuploads_handler(ngx_http_request_t * r)
  432. {
  433. ngx_str_t *id, response;
  434. ngx_buf_t *b;
  435. ngx_chain_t out;
  436. ngx_int_t rc, found=0, done=0, err_status=0;
  437. off_t rest=0, length=0;
  438. ngx_uint_t len, i;
  439. ngx_slab_pool_t *shpool;
  440. ngx_http_uploadprogress_conf_t *upcf;
  441. ngx_http_uploadprogress_ctx_t *ctx;
  442. ngx_http_uploadprogress_node_t *up;
  443. ngx_table_elt_t *expires, *cc, **ccp;
  444. ngx_http_uploadprogress_state_t state;
  445. ngx_http_uploadprogress_template_t *t;
  446. if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
  447. return NGX_HTTP_NOT_ALLOWED;
  448. }
  449. rc = ngx_http_discard_request_body(r);
  450. if (rc != NGX_OK) {
  451. return rc;
  452. }
  453. /* get the tracking id if any */
  454. id = get_tracking_id(r);
  455. if (id == NULL) {
  456. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  457. "reportuploads handler cant find id");
  458. return NGX_DECLINED;
  459. }
  460. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  461. "reportuploads handler found id: %V", id);
  462. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  463. if (upcf->shm_zone == NULL) {
  464. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  465. "reportuploads no shm_zone for id: %V", id);
  466. ngx_free(id);
  467. return NGX_DECLINED;
  468. }
  469. ctx = upcf->shm_zone->data;
  470. /* get the original connection of the upload */
  471. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  472. ngx_shmtx_lock(&shpool->mutex);
  473. up = find_node(id, ctx, r->connection->log);
  474. if (up != NULL) {
  475. ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  476. "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", id, up->rest, up->length, up->done, up->err_status);
  477. rest = up->rest;
  478. length = up->length;
  479. done = up->done;
  480. err_status = up->err_status;
  481. found = 1;
  482. } else {
  483. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  484. "reportuploads not found: %V", id);
  485. }
  486. ngx_shmtx_unlock(&shpool->mutex);
  487. ngx_free(id);
  488. /* send the output */
  489. r->headers_out.content_type = upcf->content_type;
  490. /* force no-cache */
  491. expires = r->headers_out.expires;
  492. if (expires == NULL) {
  493. expires = ngx_list_push(&r->headers_out.headers);
  494. if (expires == NULL) {
  495. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  496. }
  497. r->headers_out.expires = expires;
  498. expires->hash = 1;
  499. expires->key.len = sizeof("Expires") - 1;
  500. expires->key.data = (u_char *) "Expires";
  501. }
  502. len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT");
  503. expires->value.len = len - 1;
  504. ccp = r->headers_out.cache_control.elts;
  505. if (ccp == NULL) {
  506. if (ngx_array_init(&r->headers_out.cache_control, r->pool,
  507. 1, sizeof(ngx_table_elt_t *))
  508. != NGX_OK) {
  509. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  510. }
  511. ccp = ngx_array_push(&r->headers_out.cache_control);
  512. if (ccp == NULL) {
  513. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  514. }
  515. cc = ngx_list_push(&r->headers_out.headers);
  516. if (cc == NULL) {
  517. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  518. }
  519. cc->hash = 1;
  520. cc->key.len = sizeof("Cache-Control") - 1;
  521. cc->key.data = (u_char *) "Cache-Control";
  522. *ccp = cc;
  523. } else {
  524. for (i = 1; i < r->headers_out.cache_control.nelts; i++) {
  525. ccp[i]->hash = 0;
  526. }
  527. cc = ccp[0];
  528. }
  529. expires->value.data = (u_char *) "Thu, 01 Jan 1970 00:00:01 GMT";
  530. cc->value.len = sizeof("no-cache") - 1;
  531. cc->value.data = (u_char *) "no-cache";
  532. if (r->method == NGX_HTTP_HEAD) {
  533. r->headers_out.status = NGX_HTTP_OK;
  534. rc = ngx_http_send_header(r);
  535. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  536. return rc;
  537. }
  538. }
  539. ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
  540. /*
  541. There are 4 possibilities
  542. * request not yet started: found = false
  543. * request in error: err_status >= NGX_HTTP_BAD_REQUEST
  544. * request finished: done = true
  545. * request not yet started but registered: length==0 && rest ==0
  546. * reauest in progress: rest > 0
  547. */
  548. if (!found) {
  549. state = uploadprogress_state_starting;
  550. } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
  551. state = uploadprogress_state_error;
  552. } else if (done) {
  553. state = uploadprogress_state_done;
  554. } else if ( length == 0 && rest == 0 ) {
  555. state = uploadprogress_state_starting;
  556. } else {
  557. state = uploadprogress_state_uploading;
  558. }
  559. t = upcf->templates.elts;
  560. if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
  561. t[(ngx_uint_t)state].values->elts) == NULL)
  562. {
  563. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  564. }
  565. ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  566. "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
  567. state, err_status, (length - rest), length);
  568. b = ngx_calloc_buf(r->pool);
  569. if (b == NULL) {
  570. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  571. }
  572. b->pos = b->start = response.data;
  573. b->last = b->end = response.data + response.len;
  574. b->temporary = 1;
  575. b->memory = 1;
  576. out.buf = b;
  577. out.next = NULL;
  578. r->headers_out.status = NGX_HTTP_OK;
  579. r->headers_out.content_length_n = b->last - b->pos;
  580. b->last_buf = 1;
  581. rc = ngx_http_send_header(r);
  582. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  583. return rc;
  584. }
  585. return ngx_http_output_filter(r, &out);
  586. }
  587. /*
  588. Let's register the upload connection in our connections rb-tree
  589. */
  590. static ngx_int_t
  591. ngx_http_uploadprogress_handler(ngx_http_request_t * r)
  592. {
  593. size_t n;
  594. ngx_str_t *id;
  595. uint32_t hash;
  596. ngx_slab_pool_t *shpool;
  597. ngx_rbtree_node_t *node;
  598. ngx_http_uploadprogress_conf_t *upcf;
  599. ngx_http_uploadprogress_ctx_t *ctx;
  600. ngx_http_uploadprogress_node_t *up;
  601. ngx_http_uploadprogress_cleanup_t *upcln;
  602. ngx_pool_cleanup_t *cln;
  603. /* Is it a POST connection */
  604. if (r->method != NGX_HTTP_POST) {
  605. return NGX_DECLINED;
  606. }
  607. id = get_tracking_id(r);
  608. if (id == NULL) {
  609. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  610. "trackuploads no id found in POST upload req");
  611. return NGX_DECLINED;
  612. }
  613. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  614. "trackuploads id found: %V", id);
  615. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  616. if (!upcf->track) {
  617. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  618. "trackuploads not tracking in this location for id: %V", id);
  619. ngx_free(id);
  620. return NGX_DECLINED;
  621. }
  622. if (upcf->shm_zone == NULL) {
  623. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  624. "trackuploads no shm_zone for id: %V", id);
  625. ngx_free(id);
  626. return NGX_DECLINED;
  627. }
  628. ctx = upcf->shm_zone->data;
  629. hash = ngx_crc32_short(id->data, id->len);
  630. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  631. "trackuploads hash %08XD for id: %V", hash, id);
  632. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  633. ngx_shmtx_lock(&shpool->mutex);
  634. if (find_node(id, ctx, r->connection->log) != NULL) {
  635. ngx_shmtx_unlock(&shpool->mutex);
  636. /* already found a node with matching progress ID */
  637. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  638. "upload_progress: tracking already registered id: %V", id);
  639. ngx_free(id);
  640. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  641. }
  642. cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
  643. if (cln == NULL) {
  644. ngx_shmtx_unlock(&shpool->mutex);
  645. ngx_free(id);
  646. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  647. }
  648. n = sizeof(ngx_http_uploadprogress_node_t)
  649. + id->len;
  650. node = ngx_slab_alloc_locked(shpool, n);
  651. if (node == NULL) {
  652. ngx_shmtx_unlock(&shpool->mutex);
  653. ngx_free(id);
  654. return NGX_HTTP_SERVICE_UNAVAILABLE;
  655. }
  656. up = (ngx_http_uploadprogress_node_t *) node;
  657. node->key = hash;
  658. up->len = (u_char) id->len;
  659. up->err_status = r->err_status;
  660. up->done = 0;
  661. up->rest = 0;
  662. up->length = 0;
  663. up->timeout = 0;
  664. up->next = ctx->list_head.next;
  665. up->next->prev = up;
  666. up->prev = &ctx->list_head;
  667. ctx->list_head.next = up;
  668. ngx_memcpy(up->data, id->data, id->len);
  669. ngx_rbtree_insert(ctx->rbtree, node);
  670. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  671. "trackuploads: %08XD inserted in rbtree", node->key);
  672. if (!upcf->cleanup.timer_set) {
  673. upcf->cleanup.data = upcf->shm_zone;
  674. upcf->cleanup.handler = ngx_clean_old_connections;
  675. upcf->cleanup.log = upcf->shm_zone->shm.log;
  676. ngx_add_timer(&upcf->cleanup, TIMER_FREQUENCY);
  677. }
  678. ngx_shmtx_unlock(&shpool->mutex);
  679. cln->handler = ngx_http_uploadprogress_cleanup;
  680. upcln = cln->data;
  681. upcln->shm_zone = upcf->shm_zone;
  682. upcln->node = node;
  683. upcln->timeout = upcf->timeout;
  684. upcln->r = r;
  685. ngx_free(id);
  686. ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_uploadprogress_module_ctx_t));
  687. if (ctx == NULL) {
  688. return NGX_ERROR;
  689. }
  690. ngx_http_set_ctx(r, ctx, ngx_http_uploadprogress_module);
  691. /* finally says to the core we don't handle anything */
  692. return NGX_DECLINED;
  693. }
  694. static void
  695. ngx_http_uploadprogress_rbtree_insert_value(ngx_rbtree_node_t * temp,
  696. ngx_rbtree_node_t * node,
  697. ngx_rbtree_node_t * sentinel)
  698. {
  699. ngx_http_uploadprogress_node_t *upn, *upnt;
  700. for (;;) {
  701. if (node->key < temp->key) {
  702. if (temp->left == sentinel) {
  703. temp->left = node;
  704. break;
  705. }
  706. temp = temp->left;
  707. } else if (node->key > temp->key) {
  708. if (temp->right == sentinel) {
  709. temp->right = node;
  710. break;
  711. }
  712. temp = temp->right;
  713. } else { /* node->key == temp->key */
  714. upn = (ngx_http_uploadprogress_node_t *) node;
  715. upnt = (ngx_http_uploadprogress_node_t *) temp;
  716. if (ngx_memn2cmp(upn->data, upnt->data, upn->len, upnt->len) < 0) {
  717. if (temp->left == sentinel) {
  718. temp->left = node;
  719. break;
  720. }
  721. temp = temp->left;
  722. } else {
  723. if (temp->right == sentinel) {
  724. temp->right = node;
  725. break;
  726. }
  727. temp = temp->right;
  728. }
  729. }
  730. }
  731. node->parent = temp;
  732. node->left = sentinel;
  733. node->right = sentinel;
  734. ngx_rbt_red(node);
  735. }
  736. static void
  737. ngx_clean_old_connections(ngx_event_t * ev)
  738. {
  739. ngx_shm_zone_t *shm_zone;
  740. ngx_http_uploadprogress_ctx_t *ctx;
  741. ngx_slab_pool_t *shpool;
  742. ngx_rbtree_node_t *node;
  743. ngx_http_uploadprogress_node_t *up, *upprev;
  744. time_t now = ngx_time();
  745. int count = 0;
  746. /* scan the rbtree */
  747. shm_zone = ev->data;
  748. ctx = shm_zone->data;
  749. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  750. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  751. "uploadprogress clean old connections at %T", now);
  752. ngx_shmtx_lock(&shpool->mutex);
  753. node = (ngx_rbtree_node_t *) ctx->list_tail.prev;
  754. for (;;) {
  755. if (node == &ctx->list_head.node) {
  756. break;
  757. }
  758. up = (ngx_http_uploadprogress_node_t *) node;
  759. upprev = up->prev;
  760. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  761. "uploadprogress clean: scanning %08XD (req done %ui) timeout at %T",
  762. node->key, up->done, up->timeout);
  763. if ( (up->done && up->timeout < now) || (ngx_quit || ngx_terminate || ngx_exiting) ) {
  764. up->next->prev = up->prev;
  765. up->prev->next = up->next;
  766. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  767. "uploadprogress clean: removing %08XD (req %ui) ",
  768. node->key, up->done, up->timeout);
  769. ngx_rbtree_delete(ctx->rbtree, node);
  770. ngx_slab_free_locked(shpool, node);
  771. }
  772. else
  773. count++;
  774. node = (ngx_rbtree_node_t *) upprev;
  775. }
  776. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  777. "uploadprogress clean old connections: quit: %ui term: %ui count: %ui", ngx_quit, ngx_terminate, count);
  778. /* don't reschedule timer if ngx_quit or ngx_terminate && nodes emtpy */
  779. if ( count > 0 || !(ngx_quit || ngx_terminate || ngx_exiting)) {
  780. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  781. "uploadprogress clean old connections restarting timer");
  782. ngx_add_timer(ev, TIMER_FREQUENCY); /* trigger again in 60s */
  783. } else if (ngx_quit || ngx_terminate || ngx_exiting) {
  784. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  785. "uploadprogress clean old connections quitting , no more active connections: not restarting timer");
  786. }
  787. ngx_shmtx_unlock(&shpool->mutex);
  788. }
  789. /*
  790. removes the expired node from the upload rbtree
  791. */
  792. static void
  793. ngx_http_uploadprogress_cleanup(void *data)
  794. {
  795. ngx_http_uploadprogress_cleanup_t *upcln = data;
  796. ngx_slab_pool_t *shpool;
  797. ngx_rbtree_node_t *node;
  798. ngx_http_uploadprogress_ctx_t *ctx;
  799. ngx_http_uploadprogress_node_t *up;
  800. ngx_http_request_t *r;
  801. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, upcln->shm_zone->shm.log, 0,
  802. "uploadprogress cleanup called");
  803. ctx = upcln->shm_zone->data;
  804. shpool = (ngx_slab_pool_t *) upcln->shm_zone->shm.addr;
  805. node = upcln->node;
  806. r = upcln->r;
  807. up = (ngx_http_uploadprogress_node_t *) node;
  808. ngx_shmtx_lock(&shpool->mutex);
  809. up->done = 1; /* mark the original request as done */
  810. up->timeout = ngx_time() + upcln->timeout; /* keep tracking for 60s */
  811. if (r != NULL ) {
  812. ngx_uint_t rc = r->err_status ? r->err_status : r->headers_out.status;
  813. if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
  814. up->err_status = rc;
  815. }
  816. }
  817. ngx_shmtx_unlock(&shpool->mutex);
  818. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, upcln->shm_zone->shm.log, 0,
  819. "uploadprogress cleanup: connection %08XD to be deleted at %T",
  820. node->key, up->timeout);
  821. }
  822. static ngx_int_t
  823. ngx_http_uploadprogress_init_zone(ngx_shm_zone_t * shm_zone, void *data)
  824. {
  825. ngx_http_uploadprogress_ctx_t *octx = data;
  826. ngx_slab_pool_t *shpool;
  827. ngx_rbtree_node_t *sentinel;
  828. ngx_http_uploadprogress_ctx_t *ctx;
  829. ctx = shm_zone->data;
  830. if (octx) {
  831. ctx->rbtree = octx->rbtree;
  832. return NGX_OK;
  833. }
  834. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  835. ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t));
  836. if (ctx->rbtree == NULL) {
  837. return NGX_ERROR;
  838. }
  839. sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t));
  840. if (sentinel == NULL) {
  841. return NGX_ERROR;
  842. }
  843. ngx_rbtree_sentinel_init(sentinel);
  844. ctx->rbtree->root = sentinel;
  845. ctx->rbtree->sentinel = sentinel;
  846. ctx->rbtree->insert = ngx_http_uploadprogress_rbtree_insert_value;
  847. return NGX_OK;
  848. }
  849. static ngx_int_t
  850. ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
  851. {
  852. size_t n;
  853. ngx_str_t *id;
  854. ngx_slab_pool_t *shpool;
  855. ngx_rbtree_node_t *node;
  856. ngx_http_uploadprogress_ctx_t *ctx;
  857. ngx_http_uploadprogress_node_t *up;
  858. ngx_http_uploadprogress_conf_t *upcf;
  859. uint32_t hash;
  860. ngx_http_uploadprogress_cleanup_t *upcln;
  861. ngx_pool_cleanup_t *cln;
  862. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  863. "uploadprogress error-tracker error: %D", r->err_status);
  864. if (r->err_status >= NGX_HTTP_SPECIAL_RESPONSE) {
  865. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  866. if (!upcf->track) {
  867. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  868. "uploadprogress error-tracker not tracking in this location");
  869. goto finish;
  870. }
  871. id = get_tracking_id(r);
  872. if (id == NULL) {
  873. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  874. "trackuploads error-tracker no id found in POST upload req");
  875. goto finish;
  876. }
  877. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  878. "trackuploads error-tracker id found: %V", id);
  879. if (upcf->shm_zone == NULL) {
  880. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  881. "trackuploads no shm_zone for id: %V", id);
  882. ngx_free(id);
  883. goto finish;
  884. }
  885. ctx = upcf->shm_zone->data;
  886. hash = ngx_crc32_short(id->data, id->len);
  887. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  888. "trackuploads error-tracking hash %08XD for id: %V", hash,
  889. id);
  890. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  891. ngx_shmtx_lock(&shpool->mutex);
  892. if ((up = find_node(id, ctx, r->connection->log)) != NULL) {
  893. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  894. "trackuploads error-tracking found node for id: %V", id);
  895. up->err_status = r->err_status;
  896. ngx_shmtx_unlock(&shpool->mutex);
  897. ngx_free(id);
  898. goto finish;
  899. }
  900. /* no lz found for this tracking id */
  901. n = sizeof(ngx_http_uploadprogress_node_t) + id->len;
  902. cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
  903. if (cln == NULL) {
  904. ngx_shmtx_unlock(&shpool->mutex);
  905. ngx_free(id);
  906. goto finish;
  907. }
  908. node = ngx_slab_alloc_locked(shpool, n);
  909. if (node == NULL) {
  910. ngx_shmtx_unlock(&shpool->mutex);
  911. ngx_free(id);
  912. goto finish;
  913. }
  914. up = (ngx_http_uploadprogress_node_t *) node;
  915. node->key = hash;
  916. up->len = (u_char) id->len;
  917. up->err_status = r->err_status;
  918. up->done = 0;
  919. up->rest = 0;
  920. up->length = 0;
  921. up->timeout = 0;
  922. ngx_memcpy(up->data, id->data, id->len);
  923. up->next = ctx->list_head.next;
  924. up->next->prev = up;
  925. up->prev = &ctx->list_head;
  926. ctx->list_head.next = up;
  927. ngx_rbtree_insert(ctx->rbtree, node);
  928. /* start the timer if needed */
  929. if (!upcf->cleanup.timer_set) {
  930. upcf->cleanup.data = upcf->shm_zone;
  931. upcf->cleanup.handler = ngx_clean_old_connections;
  932. upcf->cleanup.log = upcf->shm_zone->shm.log;
  933. ngx_add_timer(&upcf->cleanup, TIMER_FREQUENCY);
  934. }
  935. ngx_shmtx_unlock(&shpool->mutex);
  936. cln->handler = ngx_http_uploadprogress_cleanup;
  937. upcln = cln->data;
  938. upcln->shm_zone = upcf->shm_zone;
  939. upcln->node = node;
  940. upcln->timeout = upcf->timeout;
  941. upcln->r = r;
  942. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  943. "trackuploads error-tracking adding: %08XD", node->key);
  944. ngx_free(id);
  945. }
  946. finish:
  947. /* call the filter chain as usual */
  948. return ngx_http_next_header_filter(r);
  949. }
  950. static ngx_int_t
  951. ngx_http_uploadprogress_init(ngx_conf_t * cf)
  952. {
  953. ngx_http_handler_pt *h;
  954. ngx_http_core_main_conf_t *cmcf;
  955. cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
  956. /* install the tracking handler */
  957. h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
  958. if (h == NULL) {
  959. return NGX_ERROR;
  960. }
  961. *h = ngx_http_uploadprogress_handler;
  962. /*
  963. we also need to track HTTP errors
  964. unfortunately, the above handler is not called in case of
  965. errors.
  966. we have to register a header output filter that will be
  967. called in any case to track those errors
  968. */
  969. ngx_http_next_header_filter = ngx_http_top_header_filter;
  970. ngx_http_top_header_filter = ngx_http_uploadprogress_errortracker;
  971. return NGX_OK;
  972. }
  973. static void*
  974. ngx_http_uploadprogress_create_loc_conf(ngx_conf_t * cf)
  975. {
  976. ngx_http_uploadprogress_conf_t *conf;
  977. ngx_http_uploadprogress_template_t *t;
  978. ngx_uint_t i;
  979. conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_conf_t));
  980. if (conf == NULL) {
  981. return NGX_CONF_ERROR;
  982. }
  983. if(ngx_array_init(&conf->templates, cf->pool, 4, sizeof(ngx_http_uploadprogress_template_t)) != NGX_OK) {
  984. return NGX_CONF_ERROR;
  985. }
  986. t = conf->templates.elts;
  987. for(i = 0;i < conf->templates.nalloc; i++) {
  988. ngx_http_uploadprogress_template_t *elt = ngx_array_push(&conf->templates);
  989. if (elt == NULL) {
  990. return NGX_CONF_ERROR;
  991. }
  992. elt->values = NULL;
  993. elt->lengths = NULL;
  994. }
  995. return conf;
  996. }
  997. static char*
  998. ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t * cf, void *parent, void *child)
  999. {
  1000. ngx_http_uploadprogress_conf_t *prev = parent;
  1001. ngx_http_uploadprogress_conf_t *conf = child;
  1002. ngx_http_uploadprogress_template_t *t, *pt, *gt;
  1003. ngx_uint_t i;
  1004. if (conf->shm_zone == NULL) {
  1005. conf->shm_zone = prev->shm_zone;
  1006. conf->timeout = prev->timeout;
  1007. conf->cleanup = prev->cleanup;
  1008. conf->handler = prev->handler;
  1009. conf->track = prev->track;
  1010. }
  1011. ngx_conf_merge_str_value(conf->content_type, prev->content_type, "text/javascript");
  1012. t = conf->templates.elts;
  1013. pt = prev->templates.elts;
  1014. gt = ngx_http_uploadprogress_global_templates.elts;
  1015. for(i = 0;i < conf->templates.nelts; i++) {
  1016. if(t[i].values == NULL) {
  1017. if(pt[i].values == NULL && gt != NULL) {
  1018. t[i].values = gt[i].values;
  1019. t[i].lengths = gt[i].lengths;
  1020. }
  1021. else {
  1022. t[i].values = pt[i].values;
  1023. t[i].lengths = pt[i].lengths;
  1024. }
  1025. }
  1026. }
  1027. ngx_conf_merge_str_value(conf->header, prev->header, "X-Progress-ID");
  1028. ngx_conf_merge_str_value(conf->jsonp_parameter, prev->jsonp_parameter, "callback");
  1029. return NGX_CONF_OK;
  1030. }
  1031. static ngx_int_t
  1032. ngx_http_uploadprogress_init_variables_and_templates(ngx_conf_t *cf)
  1033. {
  1034. ngx_http_variable_t *var, *v;
  1035. ngx_http_uploadprogress_template_t *t;
  1036. ngx_http_uploadprogress_state_map_t *m;
  1037. ssize_t n;
  1038. ngx_uint_t i;
  1039. /* Add variables */
  1040. for (v = ngx_http_uploadprogress_variables; v->name.len; v++) {
  1041. var = ngx_http_add_variable(cf, &v->name, v->flags);
  1042. if (var == NULL) {
  1043. return NGX_ERROR;
  1044. }
  1045. var->get_handler = v->get_handler;
  1046. var->data = v->data;
  1047. }
  1048. /* Compile global templates (containing Javascript output) */
  1049. if(ngx_array_init(&ngx_http_uploadprogress_global_templates, cf->pool, 4,
  1050. sizeof(ngx_http_uploadprogress_template_t)) != NGX_OK) {
  1051. return NGX_ERROR;
  1052. }
  1053. m = ngx_http_uploadprogress_state_map;
  1054. t = ngx_http_uploadprogress_global_templates.elts;
  1055. i = 0;
  1056. while(m->name.data != NULL) {
  1057. ngx_http_uploadprogress_template_t *elt = ngx_array_push(&ngx_http_uploadprogress_global_templates);
  1058. n = ngx_http_script_variables_count(ngx_http_uploadprogress_java_defaults + i);
  1059. if (ngx_http_upload_progress_set_template(cf, elt, ngx_http_uploadprogress_java_defaults + i) != NGX_CONF_OK) {
  1060. return NGX_ERROR;
  1061. }
  1062. m++;
  1063. i++;
  1064. }
  1065. return NGX_OK;
  1066. }
  1067. static char*
  1068. ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1069. {
  1070. ssize_t n;
  1071. ngx_str_t *value;
  1072. ngx_shm_zone_t *shm_zone;
  1073. ngx_http_uploadprogress_ctx_t *ctx;
  1074. value = cf->args->elts;
  1075. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1076. "ngx_upload_progress name: %V", &value[1]);
  1077. ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_ctx_t));
  1078. if (ctx == NULL) {
  1079. return NGX_CONF_ERROR;
  1080. }
  1081. ctx->list_head.prev = NULL;
  1082. ctx->list_head.next = &ctx->list_tail;
  1083. ctx->list_tail.prev = &ctx->list_head;
  1084. ctx->list_tail.next = NULL;
  1085. n = ngx_parse_size(&value[2]);
  1086. if (n == NGX_ERROR) {
  1087. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1088. "invalid size of track_uploads \"%V\"", &value[2]);
  1089. return NGX_CONF_ERROR;
  1090. }
  1091. if (n < (ngx_int_t) (8 * ngx_pagesize)) {
  1092. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1093. "track_uploads \"%V\" is too small", &value[1]);
  1094. return NGX_CONF_ERROR;
  1095. }
  1096. shm_zone = ngx_shared_memory_add(cf, &value[1], n,
  1097. &ngx_http_uploadprogress_module);
  1098. if (shm_zone == NULL) {
  1099. return NGX_CONF_ERROR;
  1100. }
  1101. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1102. "ngx_upload_progress name: %V, szhm_zone: %p", &value[1],
  1103. shm_zone);
  1104. if (shm_zone->data) {
  1105. ctx = shm_zone->data;
  1106. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1107. "track_uploads \"%V\" is already created", &value[1]);
  1108. return NGX_CONF_ERROR;
  1109. }
  1110. shm_zone->init = ngx_http_uploadprogress_init_zone;
  1111. shm_zone->data = ctx;
  1112. return NGX_CONF_OK;
  1113. }
  1114. static char*
  1115. ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1116. {
  1117. ngx_http_core_loc_conf_t *clcf;
  1118. ngx_http_uploadprogress_conf_t *lzcf = conf;
  1119. ngx_str_t *value;
  1120. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_track_uploads in");
  1121. value = cf->args->elts;
  1122. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1123. "ngx_track_uploads name: %V", &value[1]);
  1124. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  1125. &ngx_http_uploadprogress_module);
  1126. if (lzcf->shm_zone == NULL) {
  1127. return NGX_CONF_ERROR;
  1128. }
  1129. lzcf->track = (u_char) 1;
  1130. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1131. "ngx_track_uploads name: %V,szhm_zone: %p", &value[1],
  1132. lzcf->shm_zone);
  1133. lzcf->timeout = ngx_parse_time(&value[2], 1);
  1134. if (lzcf->timeout == NGX_ERROR) {
  1135. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1136. "track_uploads \"%V\" timeout value invalid", &value[1]);
  1137. return NGX_CONF_ERROR;
  1138. }
  1139. if (lzcf->timeout == NGX_PARSE_LARGE_TIME) {
  1140. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1141. "track_uploads \"%V\" timeout value must be less than 68 years", &value[1]);
  1142. return NGX_CONF_ERROR;
  1143. }
  1144. clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  1145. lzcf->handler = clcf->handler;
  1146. if ( lzcf->handler == NULL )
  1147. {
  1148. return "track_upload should be the last directive in the location, after either proxy_pass or fastcgi_pass";
  1149. }
  1150. clcf->handler = ngx_http_uploadprogress_content_handler;
  1151. return NGX_CONF_OK;
  1152. }
  1153. static char*
  1154. ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1155. {
  1156. ngx_http_uploadprogress_conf_t *lzcf = conf;
  1157. ngx_http_core_loc_conf_t *clcf;
  1158. ngx_str_t *value;
  1159. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_report_uploads in");
  1160. value = cf->args->elts;
  1161. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1162. "ngx_report_uploads name: %V", &value[1]);
  1163. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  1164. &ngx_http_uploadprogress_module);
  1165. if (lzcf->shm_zone == NULL) {
  1166. return NGX_CONF_ERROR;
  1167. }
  1168. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1169. "ngx_report_uploads name: %V, szhm_zone: %p", &value[1],
  1170. lzcf->shm_zone);
  1171. lzcf->track = (u_char) 0;
  1172. /* install our report handler */
  1173. clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  1174. clcf->handler = ngx_http_reportuploads_handler;
  1175. return NGX_CONF_OK;
  1176. }
  1177. static char*
  1178. ngx_http_upload_progress_set_template(ngx_conf_t * cf, ngx_http_uploadprogress_template_t *t, ngx_str_t *source)
  1179. {
  1180. ssize_t n;
  1181. ngx_http_script_compile_t sc;
  1182. n = ngx_http_script_variables_count(source);
  1183. ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
  1184. t->lengths = NULL;
  1185. t->values = NULL;
  1186. sc.cf = cf;
  1187. sc.source = source;
  1188. sc.lengths = &t->lengths;
  1189. sc.values = &t->values;
  1190. sc.variables = n;
  1191. sc.complete_lengths = 1;
  1192. sc.complete_values = 1;
  1193. if (ngx_http_script_compile(&sc) != NGX_OK) {
  1194. return NGX_CONF_ERROR;
  1195. }
  1196. return NGX_CONF_OK;
  1197. }
  1198. static char*
  1199. ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1200. {
  1201. ngx_http_uploadprogress_conf_t *upcf = conf;
  1202. ngx_str_t *value;
  1203. ngx_http_uploadprogress_state_map_t *m = ngx_http_uploadprogress_state_map;
  1204. ngx_http_uploadprogress_template_t *t;
  1205. value = cf->args->elts;
  1206. while(m->name.data != NULL) {
  1207. if((value[1].len == m->name.len && !ngx_strncmp(value[1].data, m->name.data, m->name.len))
  1208. || (value[1].len == 2 && !ngx_strncmp(value[1].data, m->name.data, 2))) {
  1209. break;
  1210. }
  1211. m++;
  1212. }
  1213. if (m->name.data == NULL) {
  1214. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1215. "unknown state \"%V\"", &value[1]);
  1216. return NGX_CONF_ERROR;
  1217. }
  1218. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts + (ngx_uint_t)m->idx;
  1219. return ngx_http_upload_progress_set_template(cf, t, &value[2]);
  1220. }
  1221. static char*
  1222. ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1223. {
  1224. ngx_http_uploadprogress_conf_t *upcf = conf;
  1225. ngx_http_uploadprogress_template_t *t;
  1226. ngx_uint_t i;
  1227. char* rc;
  1228. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
  1229. for(i = 0;i < upcf->templates.nelts;i++) {
  1230. rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_json_defaults + i);
  1231. if(rc != NGX_CONF_OK) {
  1232. return rc;
  1233. }
  1234. }
  1235. upcf->content_type.data = (u_char*)"application/json";
  1236. upcf->content_type.len = sizeof("application/json") - 1;
  1237. return NGX_CONF_OK;
  1238. }
  1239. static char*
  1240. ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1241. {
  1242. ngx_http_uploadprogress_conf_t *upcf = conf;
  1243. ngx_http_uploadprogress_template_t *t;
  1244. ngx_uint_t i;
  1245. char* rc;
  1246. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
  1247. for(i = 0;i < upcf->templates.nelts;i++) {
  1248. rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_jsonp_defaults + i);
  1249. if(rc != NGX_CONF_OK) {
  1250. return rc;
  1251. }
  1252. }
  1253. upcf->content_type.data = (u_char*)"application/javascript";
  1254. upcf->content_type.len = sizeof("application/javascript") - 1;
  1255. return NGX_CONF_OK;
  1256. }
  1257. static ngx_int_t ngx_http_uploadprogress_received_variable(ngx_http_request_t *r,
  1258. ngx_http_variable_value_t *v, uintptr_t data)
  1259. {
  1260. ngx_http_uploadprogress_node_t *up;
  1261. u_char *p;
  1262. off_t *value;
  1263. up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
  1264. value = (off_t *) ((char *) up + data);
  1265. p = ngx_palloc(r->pool, NGX_OFF_T_LEN);
  1266. if (p == NULL) {
  1267. return NGX_ERROR;
  1268. }
  1269. v->len = ngx_sprintf(p, "%O", up->length - *value) - p;
  1270. v->valid = 1;
  1271. v->no_cacheable = 0;
  1272. v->not_found = 0;
  1273. v->data = p;
  1274. return NGX_OK;
  1275. }
  1276. static ngx_int_t ngx_http_uploadprogress_offset_variable(ngx_http_request_t *r,
  1277. ngx_http_variable_value_t *v, uintptr_t data)
  1278. {
  1279. ngx_http_uploadprogress_node_t *up;
  1280. u_char *p;
  1281. off_t *value;
  1282. up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
  1283. value = (off_t *) ((char *) up + data);
  1284. p = ngx_palloc(r->pool, NGX_OFF_T_LEN);
  1285. if (p == NULL) {
  1286. return NGX_ERROR;
  1287. }
  1288. v->len = ngx_sprintf(p, "%O", *value) - p;
  1289. v->valid = 1;
  1290. v->no_cacheable = 0;
  1291. v->not_found = 0;
  1292. v->data = p;
  1293. return NGX_OK;
  1294. }
  1295. static ngx_int_t
  1296. ngx_http_uploadprogress_status_variable(ngx_http_request_t *r,
  1297. ngx_http_variable_value_t *v, uintptr_t data)
  1298. {
  1299. ngx_http_uploadprogress_node_t *up;
  1300. u_char *p;
  1301. off_t *value;
  1302. up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
  1303. value = (off_t *) ((char *) up + data);
  1304. p = ngx_palloc(r->pool, NGX_OFF_T_LEN);
  1305. if (p == NULL) {
  1306. return NGX_ERROR;
  1307. }
  1308. v->len = ngx_sprintf(p, "%O", *value) - p;
  1309. v->valid = 1;
  1310. v->no_cacheable = 0;
  1311. v->not_found = 0;
  1312. v->data = p;
  1313. return NGX_OK;
  1314. }
  1315. static ngx_int_t
  1316. ngx_http_uploadprogress_callback_variable(ngx_http_request_t *r,
  1317. ngx_http_variable_value_t *v, uintptr_t data)
  1318. {
  1319. u_char *p, *start_p, *val, prefix[1024];
  1320. ngx_http_uploadprogress_conf_t *upcf;
  1321. u_int len;
  1322. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  1323. if (r->args.len) {
  1324. /* '=' has to be appended to avoid matching parameters that have the */
  1325. /* configured name as a prefix but are longer */
  1326. ngx_snprintf(prefix, sizeof(prefix) - 1, "%s=", upcf->jsonp_parameter.data);
  1327. len = upcf->jsonp_parameter.len + 1;
  1328. prefix[len] = '\0'; /* Force termination of string */
  1329. p = (u_char *) ngx_strstr(r->args.data, prefix);
  1330. if (p) {
  1331. p += len;
  1332. start_p = p;
  1333. while (p < r->args.data + r->args.len) {
  1334. if (*((p++) + 1) == '&') {
  1335. break;
  1336. }
  1337. }
  1338. v->len = p - start_p;
  1339. val = ngx_palloc(r->pool, v->len + 1);
  1340. if (val == NULL) {
  1341. return NGX_ERROR;
  1342. }
  1343. ngx_memcpy(val, start_p, v->len);
  1344. val[v->len] = '\0';
  1345. v->valid = 1;
  1346. v->no_cacheable = 0;
  1347. v->not_found = 0;
  1348. v->data = val;
  1349. } else {
  1350. return NGX_ERROR;
  1351. }
  1352. } else {
  1353. return NGX_ERROR;
  1354. }
  1355. return NGX_OK;
  1356. }