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

ngx_http_uploadprogress_module.c 50KB

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