Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ngx_http_uploadprogress_module.c 51KB

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