Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ngx_http_uploadprogress_module.c 55KB

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