You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ngx_http_uploadprogress_module.c 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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 struct ngx_http_uploadprogress_node_s ngx_http_uploadprogress_node_t;
  10. struct ngx_http_uploadprogress_node_s {
  11. ngx_rbtree_node_t node;
  12. ngx_uint_t err_status;
  13. off_t rest;
  14. off_t length;
  15. ngx_uint_t done;
  16. time_t timeout;
  17. struct ngx_http_uploadprogress_node_s *prev;
  18. struct ngx_http_uploadprogress_node_s *next;
  19. u_char len;
  20. u_char data[1];
  21. };
  22. typedef struct {
  23. ngx_shm_zone_t *shm_zone;
  24. ngx_rbtree_node_t *node;
  25. time_t timeout;
  26. } ngx_http_uploadprogress_cleanup_t;
  27. typedef struct {
  28. ngx_rbtree_t *rbtree;
  29. ngx_http_uploadprogress_node_t list_head;
  30. ngx_http_uploadprogress_node_t list_tail;
  31. } ngx_http_uploadprogress_ctx_t;
  32. typedef struct {
  33. ngx_shm_zone_t *shm_zone;
  34. time_t timeout;
  35. ngx_event_t cleanup;
  36. ngx_http_handler_pt handler;
  37. u_char track;
  38. } ngx_http_uploadprogress_conf_t;
  39. typedef struct {
  40. ngx_http_event_handler_pt read_event_handler;
  41. } ngx_http_uploadprogress_module_ctx_t;
  42. static ngx_int_t ngx_http_reportuploads_handler(ngx_http_request_t *r);
  43. static void ngx_http_uploadprogress_cleanup(void *data);
  44. static char *ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  45. static ngx_int_t ngx_http_uploadprogress_init_zone(ngx_shm_zone_t * shm_zone, void *data);
  46. static ngx_int_t ngx_http_uploadprogress_init(ngx_conf_t * cf);
  47. static void *ngx_http_uploadprogress_create_loc_conf(ngx_conf_t *cf);
  48. static char *ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
  49. static char *ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  50. static char *ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  51. static char *ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  52. static void ngx_clean_old_connections(ngx_event_t * ev);
  53. static ngx_int_t ngx_http_uploadprogress_content_handler(ngx_http_request_t *r);
  54. static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
  55. static ngx_command_t ngx_http_uploadprogress_commands[] = {
  56. {ngx_string("upload_progress"),
  57. NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE2,
  58. ngx_http_upload_progress,
  59. 0,
  60. 0,
  61. NULL},
  62. {ngx_string("track_uploads"),
  63. NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE2,
  64. ngx_http_track_uploads,
  65. NGX_HTTP_LOC_CONF_OFFSET,
  66. 0,
  67. NULL},
  68. {ngx_string("report_uploads"),
  69. NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
  70. ngx_http_report_uploads,
  71. NGX_HTTP_LOC_CONF_OFFSET,
  72. 0,
  73. NULL},
  74. ngx_null_command
  75. };
  76. static ngx_http_module_t ngx_http_uploadprogress_module_ctx = {
  77. NULL, /* preconfiguration */
  78. ngx_http_uploadprogress_init, /* postconfiguration */
  79. NULL, /* create main configuration */
  80. NULL, /* init main configuration */
  81. NULL, /* create server configuration */
  82. NULL, /* merge server configuration */
  83. ngx_http_uploadprogress_create_loc_conf, /* create location configuration */
  84. ngx_http_uploadprogress_merge_loc_conf /* merge location configuration */
  85. };
  86. ngx_module_t ngx_http_uploadprogress_module = {
  87. NGX_MODULE_V1,
  88. &ngx_http_uploadprogress_module_ctx, /* module context */
  89. ngx_http_uploadprogress_commands, /* module directives */
  90. NGX_HTTP_MODULE, /* module type */
  91. NULL, /* init master */
  92. NULL, /* init module */
  93. NULL, /* init process */
  94. NULL, /* init thread */
  95. NULL, /* exit thread */
  96. NULL, /* exit process */
  97. NULL, /* exit master */
  98. NGX_MODULE_V1_PADDING
  99. };
  100. static ngx_str_t x_progress_id = ngx_string("X-Progress-ID");
  101. static ngx_str_t*
  102. get_tracking_id(ngx_http_request_t * r)
  103. {
  104. u_char *p, *start_p;
  105. ngx_uint_t i;
  106. ngx_list_part_t *part;
  107. ngx_table_elt_t *header;
  108. ngx_str_t *ret;
  109. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: get_tracking_id");
  110. part = &r->headers_in.headers.part;
  111. header = part->elts;
  112. for (i = 0; /* void */ ; i++) {
  113. if (i >= part->nelts) {
  114. if (part->next == NULL) {
  115. break;
  116. }
  117. part = part->next;
  118. header = part->elts;
  119. i = 0;
  120. }
  121. if (header[i].key.len == x_progress_id.len
  122. && ngx_strncasecmp(header[i].key.data, x_progress_id.data,
  123. header[i].key.len) == 0) {
  124. ret = ngx_pcalloc(r->pool, sizeof(ngx_str_t));
  125. ret->data = header[i].value.data;
  126. ret->len = header[i].value.len;
  127. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  128. "upload-progress: get_tracking_id found header: %V", ret);
  129. return ret;
  130. }
  131. }
  132. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  133. "upload-progress: get_tracking_id no header found");
  134. /* not found, check as a reaquest arg */
  135. if (r->args.len) {
  136. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  137. "upload-progress: get_tracking_id no header found but args present");
  138. i = 0;
  139. p = r->args.data;
  140. do {
  141. ngx_uint_t len = r->args.len - (p - r->args.data);
  142. if (len >= 14 && ngx_strncasecmp(p, (u_char*)"X-Progress-ID=", 14) == 0) {
  143. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  144. "upload-progress: get_tracking_id found args: %s",p);
  145. i = 1;
  146. break;
  147. }
  148. if (len<=0)
  149. break;
  150. }
  151. while(p++);
  152. if (i) {
  153. start_p = p += 14;
  154. while (p < r->args.data + r->args.len) {
  155. if (*p++ != '&') {
  156. continue;
  157. }
  158. }
  159. ret = ngx_pcalloc(r->pool, sizeof(ngx_str_t));
  160. ret->data = start_p;
  161. ret->len = p - start_p;
  162. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  163. "upload-progress: get_tracking_id found args: %V",ret);
  164. return ret;
  165. }
  166. }
  167. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  168. "upload-progress: get_tracking_id no id found");
  169. return NULL;
  170. }
  171. static ngx_http_uploadprogress_node_t *
  172. find_node(ngx_str_t * id, ngx_http_uploadprogress_ctx_t * ctx, ngx_log_t * log)
  173. {
  174. uint32_t hash;
  175. ngx_rbtree_node_t *node, *sentinel;
  176. ngx_int_t rc;
  177. ngx_http_uploadprogress_node_t *up;
  178. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "upload-progress: find_node %V", id);
  179. hash = ngx_crc32_short(id->data, id->len);
  180. node = ctx->rbtree->root;
  181. sentinel = ctx->rbtree->sentinel;
  182. while (node != sentinel) {
  183. if (hash < node->key) {
  184. node = node->left;
  185. continue;
  186. }
  187. if (hash > node->key) {
  188. node = node->right;
  189. continue;
  190. }
  191. /* hash == node->key */
  192. do {
  193. up = (ngx_http_uploadprogress_node_t *) node;
  194. rc = ngx_memn2cmp(id->data, up->data, id->len, (size_t) up->len);
  195. if (rc == 0) {
  196. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0,
  197. "upload-progress: found node");
  198. return up;
  199. }
  200. node = (rc < 0) ? node->left : node->right;
  201. } while (node != sentinel && hash == node->key);
  202. break;
  203. }
  204. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "upload-progress: can't find node");
  205. return NULL;
  206. }
  207. static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r);
  208. static ngx_int_t
  209. ngx_http_uploadprogress_content_handler(ngx_http_request_t *r)
  210. {
  211. ngx_int_t rc;
  212. ngx_http_uploadprogress_module_ctx_t *ctx;
  213. ngx_http_uploadprogress_conf_t *upcf;
  214. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: ngx_http_uploadprogress_content_handler");
  215. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  216. /* call the original request handler */
  217. rc = upcf->handler(r);
  218. /* bail out if error */
  219. if (rc >= NGX_HTTP_SPECIAL_RESPONSE)
  220. return rc;
  221. /* request is OK, hijack the read_event_handler */
  222. ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_uploadprogress_module_ctx_t));
  223. if (ctx == NULL) {
  224. return NGX_ERROR;
  225. }
  226. ngx_http_set_ctx(r, ctx, ngx_http_uploadprogress_module);
  227. r->read_event_handler = ngx_http_uploadprogress_event_handler;
  228. return rc;
  229. }
  230. static void ngx_http_uploadprogress_event_handler(ngx_http_request_t *r)
  231. {
  232. ngx_str_t *id;
  233. ngx_slab_pool_t *shpool;
  234. ngx_connection_t *c;
  235. ngx_http_uploadprogress_ctx_t *ctx;
  236. ngx_http_uploadprogress_node_t *up;
  237. ngx_http_uploadprogress_conf_t *upcf;
  238. ngx_http_uploadprogress_module_ctx_t *module_ctx;
  239. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: ngx_http_uploadprogress_event_handler");
  240. c = r->connection;
  241. /* call the original read event handler */
  242. module_ctx = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
  243. module_ctx->read_event_handler(r);
  244. /* check that the request/connection is still OK */
  245. if (r->headers_out.status >= NGX_HTTP_SPECIAL_RESPONSE) {
  246. return;
  247. }
  248. /* find node, update rest */
  249. id = get_tracking_id(r);
  250. if (id == NULL) {
  251. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
  252. "upload-progress: read_event_handler cant find id");
  253. return;
  254. }
  255. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
  256. "upload-progress: read_event_handler found id: %V", id);
  257. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  258. if (upcf->shm_zone == NULL) {
  259. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
  260. "upload-progress: read_event_handler no shm_zone for id: %V", id);
  261. return;
  262. }
  263. ctx = upcf->shm_zone->data;
  264. /* get the original connection of the upload */
  265. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  266. ngx_shmtx_lock(&shpool->mutex);
  267. up = find_node(id, ctx, r->connection->log);
  268. if (up != NULL && !up->done) {
  269. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  270. "upload-progress: read_event_handler found node: %V", id);
  271. up->rest = r->request_body->rest;
  272. up->length = r->headers_in.content_length_n;
  273. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  274. "upload-progress: read_event_handler storing rest %uO/%uO for %V", up->rest, up->length, id);
  275. } else {
  276. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  277. "upload-progress: read_event_handler not found: %V", id);
  278. }
  279. ngx_shmtx_unlock(&shpool->mutex);
  280. }
  281. /* This generates the response for the report */
  282. static ngx_int_t
  283. ngx_http_reportuploads_handler(ngx_http_request_t * r)
  284. {
  285. ngx_str_t *id;
  286. ngx_buf_t *b;
  287. ngx_chain_t out;
  288. ngx_int_t rc, size, found=0, done=0, err_status=0;
  289. off_t rest=0, length=0;
  290. ngx_uint_t len, i;
  291. ngx_slab_pool_t *shpool;
  292. ngx_http_uploadprogress_conf_t *upcf;
  293. ngx_http_uploadprogress_ctx_t *ctx;
  294. ngx_http_uploadprogress_node_t *up;
  295. ngx_table_elt_t *expires, *cc, **ccp;
  296. if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
  297. return NGX_HTTP_NOT_ALLOWED;
  298. }
  299. rc = ngx_http_discard_request_body(r);
  300. if (rc != NGX_OK) {
  301. return rc;
  302. }
  303. /* get the tracking id if any */
  304. id = get_tracking_id(r);
  305. if (id == NULL) {
  306. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  307. "reportuploads handler cant find id");
  308. return NGX_DECLINED;
  309. }
  310. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  311. "reportuploads handler found id: %V", id);
  312. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  313. if (upcf->shm_zone == NULL) {
  314. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  315. "reportuploads no shm_zone for id: %V", id);
  316. return NGX_DECLINED;
  317. }
  318. ctx = upcf->shm_zone->data;
  319. /* get the original connection of the upload */
  320. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  321. ngx_shmtx_lock(&shpool->mutex);
  322. up = find_node(id, ctx, r->connection->log);
  323. if (up != NULL) {
  324. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  325. "reportuploads found node: %V", id);
  326. rest = up->rest;
  327. length = up->length;
  328. done = up->done;
  329. err_status = up->err_status;
  330. found = 1;
  331. } else {
  332. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  333. "reportuploads not found: %V", id);
  334. }
  335. ngx_shmtx_unlock(&shpool->mutex);
  336. /* send the output */
  337. r->headers_out.content_type.len = sizeof("text/javascript") - 1;
  338. r->headers_out.content_type.data = (u_char *) "text/javascript";
  339. /* force no-cache */
  340. expires = r->headers_out.expires;
  341. if (expires == NULL) {
  342. expires = ngx_list_push(&r->headers_out.headers);
  343. if (expires == NULL) {
  344. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  345. }
  346. r->headers_out.expires = expires;
  347. expires->hash = 1;
  348. expires->key.len = sizeof("Expires") - 1;
  349. expires->key.data = (u_char *) "Expires";
  350. }
  351. len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT");
  352. expires->value.len = len - 1;
  353. ccp = r->headers_out.cache_control.elts;
  354. if (ccp == NULL) {
  355. if (ngx_array_init(&r->headers_out.cache_control, r->pool,
  356. 1, sizeof(ngx_table_elt_t *))
  357. != NGX_OK) {
  358. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  359. }
  360. ccp = ngx_array_push(&r->headers_out.cache_control);
  361. if (ccp == NULL) {
  362. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  363. }
  364. cc = ngx_list_push(&r->headers_out.headers);
  365. if (cc == NULL) {
  366. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  367. }
  368. cc->hash = 1;
  369. cc->key.len = sizeof("Cache-Control") - 1;
  370. cc->key.data = (u_char *) "Cache-Control";
  371. *ccp = cc;
  372. } else {
  373. for (i = 1; i < r->headers_out.cache_control.nelts; i++) {
  374. ccp[i]->hash = 0;
  375. }
  376. cc = ccp[0];
  377. }
  378. expires->value.data = (u_char *) "Thu, 01 Jan 1970 00:00:01 GMT";
  379. cc->value.len = sizeof("no-cache") - 1;
  380. cc->value.data = (u_char *) "no-cache";
  381. if (r->method == NGX_HTTP_HEAD) {
  382. r->headers_out.status = NGX_HTTP_OK;
  383. rc = ngx_http_send_header(r);
  384. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  385. return rc;
  386. }
  387. }
  388. /*
  389. There are 4 possibilities
  390. * request not yet started: found = false
  391. * request in error: err_status >= NGX_HTTP_SPECIAL_RESPONSE
  392. * request finished: done = true
  393. * reauest in progress: rest > 0
  394. */
  395. if (!found) {
  396. size = sizeof("new Object({ 'state' : 'starting' })\r\n");
  397. } else if (err_status >= NGX_HTTP_SPECIAL_RESPONSE) {
  398. size = sizeof("new Object({ 'state' : 'error', 'status' : ") + NGX_INT_T_LEN + sizeof(" })\r\n");
  399. } else if (done) {
  400. size = sizeof("new Object({ 'state' : 'done' })\r\n");
  401. } else {
  402. size = sizeof("new Object({ 'state' : 'uploading', 'received' : ") + NGX_INT_T_LEN + sizeof(" })\r\n");
  403. size += sizeof(", 'size' : ") + NGX_INT_T_LEN;
  404. }
  405. b = ngx_create_temp_buf(r->pool, size);
  406. if (b == NULL) {
  407. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  408. }
  409. out.buf = b;
  410. out.next = NULL;
  411. if (!found) {
  412. b->last = ngx_cpymem(b->last, "new Object({ 'state' : 'starting' })\r\n",
  413. sizeof("new Object({ 'state' : 'starting' })\r\n") - 1);
  414. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "reportuploads returning starting");
  415. } else if (err_status >= NGX_HTTP_SPECIAL_RESPONSE) {
  416. b->last = ngx_cpymem(b->last, "new Object({ 'state' : 'error', 'status' : ",
  417. sizeof("new Object({ 'state' : 'error', 'status' : ") - 1);
  418. b->last = ngx_sprintf(b->last, "%ui })\r\n", err_status );
  419. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  420. "reportuploads returning error condition: %ui", err_status);
  421. } else if (done) {
  422. b->last = ngx_cpymem(b->last, "new Object({ 'state' : 'done' })\r\n",
  423. sizeof("new Object({ 'state' : 'done' })\r\n") - 1);
  424. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  425. "reportuploads returning done");
  426. } else {
  427. b->last = ngx_cpymem(b->last, "new Object({ 'state' : 'uploading', 'received' : ",
  428. sizeof("new Object({ 'state' : 'uploading', 'received' : ") - 1);
  429. b->last = ngx_sprintf(b->last, "%uO, 'size' : %uO })\r\n", (length - rest), length);
  430. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  431. "reportuploads returning %uO / %uO",(length - rest), length );
  432. }
  433. r->headers_out.status = NGX_HTTP_OK;
  434. r->headers_out.content_length_n = b->last - b->pos;
  435. b->last_buf = 1;
  436. rc = ngx_http_send_header(r);
  437. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  438. return rc;
  439. }
  440. return ngx_http_output_filter(r, &out);
  441. }
  442. /*
  443. Let's register the upload connection in our connections rb-tree
  444. */
  445. static ngx_int_t
  446. ngx_http_uploadprogress_handler(ngx_http_request_t * r)
  447. {
  448. size_t n;
  449. ngx_str_t *id;
  450. uint32_t hash;
  451. ngx_slab_pool_t *shpool;
  452. ngx_rbtree_node_t *node;
  453. ngx_http_uploadprogress_conf_t *upcf;
  454. ngx_http_uploadprogress_ctx_t *ctx;
  455. ngx_http_uploadprogress_node_t *up;
  456. ngx_http_uploadprogress_cleanup_t *upcln;
  457. ngx_pool_cleanup_t *cln;
  458. /* Is it a POST connection */
  459. if (r->method != NGX_HTTP_POST) {
  460. return NGX_DECLINED;
  461. }
  462. id = get_tracking_id(r);
  463. if (id == NULL) {
  464. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  465. "trackuploads no id found in POST upload req");
  466. return NGX_DECLINED;
  467. }
  468. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  469. "trackuploads id found: %V", id);
  470. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  471. if (!upcf->track) {
  472. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  473. "trackuploads not tracking in this location for id: %V", id);
  474. return NGX_DECLINED;
  475. }
  476. if (upcf->shm_zone == NULL) {
  477. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  478. "trackuploads no shm_zone for id: %V", id);
  479. return NGX_DECLINED;
  480. }
  481. ctx = upcf->shm_zone->data;
  482. hash = ngx_crc32_short(id->data, id->len);
  483. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  484. "trackuploads hash %08XD for id: %V", hash, id);
  485. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  486. ngx_shmtx_lock(&shpool->mutex);
  487. if (find_node(id, ctx, r->connection->log) != NULL) {
  488. ngx_shmtx_unlock(&shpool->mutex);
  489. /* already found a node with matching progress ID */
  490. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  491. "upload_progress: tracking already registered id: %V", id);
  492. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  493. }
  494. cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
  495. if (cln == NULL) {
  496. ngx_shmtx_unlock(&shpool->mutex);
  497. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  498. }
  499. n = sizeof(ngx_http_uploadprogress_node_t)
  500. + id->len;
  501. node = ngx_slab_alloc_locked(shpool, n);
  502. if (node == NULL) {
  503. ngx_shmtx_unlock(&shpool->mutex);
  504. return NGX_HTTP_SERVICE_UNAVAILABLE;
  505. }
  506. up = (ngx_http_uploadprogress_node_t *) node;
  507. node->key = hash;
  508. up->len = (u_char) id->len;
  509. up->err_status = r->err_status;
  510. up->next = ctx->list_head.next;
  511. up->next->prev = up;
  512. up->prev = &ctx->list_head;
  513. ctx->list_head.next = up;
  514. ngx_memcpy(up->data, id->data, id->len);
  515. ngx_rbtree_insert(ctx->rbtree, node);
  516. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  517. "trackuploads: %08XD inserted in rbtree", node->key);
  518. if (!upcf->cleanup.timer_set) {
  519. upcf->cleanup.data = upcf->shm_zone;
  520. upcf->cleanup.handler = ngx_clean_old_connections;
  521. upcf->cleanup.log = upcf->shm_zone->shm.log;
  522. ngx_add_timer(&upcf->cleanup, TIMER_FREQUENCY);
  523. }
  524. ngx_shmtx_unlock(&shpool->mutex);
  525. cln->handler = ngx_http_uploadprogress_cleanup;
  526. upcln = cln->data;
  527. upcln->shm_zone = upcf->shm_zone;
  528. upcln->node = node;
  529. upcln->timeout = upcf->timeout;
  530. /* start the timer if needed */
  531. return NGX_DECLINED;
  532. }
  533. static void
  534. ngx_http_uploadprogress_rbtree_insert_value(ngx_rbtree_node_t * temp,
  535. ngx_rbtree_node_t * node,
  536. ngx_rbtree_node_t * sentinel)
  537. {
  538. ngx_http_uploadprogress_node_t *upn, *upnt;
  539. for (;;) {
  540. if (node->key < temp->key) {
  541. if (temp->left == sentinel) {
  542. temp->left = node;
  543. break;
  544. }
  545. temp = temp->left;
  546. } else if (node->key > temp->key) {
  547. if (temp->right == sentinel) {
  548. temp->right = node;
  549. break;
  550. }
  551. temp = temp->right;
  552. } else { /* node->key == temp->key */
  553. upn = (ngx_http_uploadprogress_node_t *) node;
  554. upnt = (ngx_http_uploadprogress_node_t *) temp;
  555. if (ngx_memn2cmp(upn->data, upnt->data, upn->len, upnt->len) < 0) {
  556. if (temp->left == sentinel) {
  557. temp->left = node;
  558. break;
  559. }
  560. temp = temp->left;
  561. } else {
  562. if (temp->right == sentinel) {
  563. temp->right = node;
  564. break;
  565. }
  566. temp = temp->right;
  567. }
  568. }
  569. }
  570. node->parent = temp;
  571. node->left = sentinel;
  572. node->right = sentinel;
  573. ngx_rbt_red(node);
  574. }
  575. static void
  576. ngx_clean_old_connections(ngx_event_t * ev)
  577. {
  578. ngx_shm_zone_t *shm_zone;
  579. ngx_http_uploadprogress_ctx_t *ctx;
  580. ngx_slab_pool_t *shpool;
  581. ngx_rbtree_node_t *node;
  582. ngx_http_uploadprogress_node_t *up;
  583. time_t now = ngx_time();
  584. /* scan the rbtree */
  585. shm_zone = ev->data;
  586. ctx = shm_zone->data;
  587. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  588. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  589. "uploadprogress clean old connections at %T", now);
  590. ngx_shmtx_lock(&shpool->mutex);
  591. node = (ngx_rbtree_node_t *) ctx->list_tail.prev;
  592. for (;;) {
  593. if (node == &ctx->list_head.node) {
  594. break;
  595. }
  596. up = (ngx_http_uploadprogress_node_t *) node;
  597. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  598. "uploadprogress clean: scanning %08XD (req done %ui) timeout at %T",
  599. node->key, up->done, up->timeout);
  600. if (up->done && up->timeout < now) {
  601. up->next->prev = up->prev;
  602. up->prev->next = up->next;
  603. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  604. "uploadprogress clean: removing %08XD (req %ui) ",
  605. node->key, up->done, up->timeout);
  606. ngx_rbtree_delete(ctx->rbtree, node);
  607. ngx_slab_free_locked(shpool, node);
  608. }
  609. node = (ngx_rbtree_node_t *) up->prev;
  610. }
  611. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  612. "uploadprogress clean old connections restarting timer");
  613. ngx_add_timer(ev, TIMER_FREQUENCY); /* trigger again in 60s */
  614. ngx_shmtx_unlock(&shpool->mutex);
  615. }
  616. /*
  617. removes the expired node from the upload rbtree
  618. */
  619. static void
  620. ngx_http_uploadprogress_cleanup(void *data)
  621. {
  622. ngx_http_uploadprogress_cleanup_t *upcln = data;
  623. ngx_slab_pool_t *shpool;
  624. ngx_rbtree_node_t *node;
  625. ngx_http_uploadprogress_ctx_t *ctx;
  626. ngx_http_uploadprogress_node_t *up;
  627. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, upcln->shm_zone->shm.log, 0,
  628. "uploadprogress cleanup called");
  629. ctx = upcln->shm_zone->data;
  630. shpool = (ngx_slab_pool_t *) upcln->shm_zone->shm.addr;
  631. node = upcln->node;
  632. up = (ngx_http_uploadprogress_node_t *) node;
  633. ngx_shmtx_lock(&shpool->mutex);
  634. up->done = 1; /* mark the original request as done */
  635. up->timeout = ngx_time() + upcln->timeout; /* keep tracking for 60s */
  636. ngx_shmtx_unlock(&shpool->mutex);
  637. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, upcln->shm_zone->shm.log, 0,
  638. "uploadprogress cleanup: connection %08XD to be deleted at %T",
  639. node->key, up->timeout);
  640. }
  641. static ngx_int_t
  642. ngx_http_uploadprogress_init_zone(ngx_shm_zone_t * shm_zone, void *data)
  643. {
  644. ngx_http_uploadprogress_ctx_t *octx = data;
  645. ngx_slab_pool_t *shpool;
  646. ngx_rbtree_node_t *sentinel;
  647. ngx_http_uploadprogress_ctx_t *ctx;
  648. ctx = shm_zone->data;
  649. if (octx) {
  650. ctx->rbtree = octx->rbtree;
  651. return NGX_OK;
  652. }
  653. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  654. ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t));
  655. if (ctx->rbtree == NULL) {
  656. return NGX_ERROR;
  657. }
  658. sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t));
  659. if (sentinel == NULL) {
  660. return NGX_ERROR;
  661. }
  662. ngx_rbtree_sentinel_init(sentinel);
  663. ctx->rbtree->root = sentinel;
  664. ctx->rbtree->sentinel = sentinel;
  665. ctx->rbtree->insert = ngx_http_uploadprogress_rbtree_insert_value;
  666. return NGX_OK;
  667. }
  668. static ngx_int_t
  669. ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
  670. {
  671. size_t n;
  672. ngx_str_t *id;
  673. ngx_slab_pool_t *shpool;
  674. ngx_rbtree_node_t *node;
  675. ngx_http_uploadprogress_ctx_t *ctx;
  676. ngx_http_uploadprogress_node_t *up;
  677. ngx_http_uploadprogress_conf_t *upcf;
  678. uint32_t hash;
  679. ngx_http_uploadprogress_cleanup_t *upcln;
  680. ngx_pool_cleanup_t *cln;
  681. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  682. "uploadprogress error-tracker error: %D", r->err_status);
  683. if (r->err_status >= NGX_HTTP_SPECIAL_RESPONSE) {
  684. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  685. if (!upcf->track) {
  686. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  687. "uploadprogress error-tracker not tracking in this location");
  688. goto finish;
  689. }
  690. id = get_tracking_id(r);
  691. if (id == NULL) {
  692. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  693. "trackuploads error-tracker no id found in POST upload req");
  694. goto finish;
  695. }
  696. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  697. "trackuploads error-tracker id found: %V", id);
  698. if (upcf->shm_zone == NULL) {
  699. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  700. "trackuploads no shm_zone for id: %V", id);
  701. goto finish;
  702. }
  703. ctx = upcf->shm_zone->data;
  704. hash = ngx_crc32_short(id->data, id->len);
  705. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  706. "trackuploads error-tracking hash %08XD for id: %V", hash,
  707. id);
  708. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  709. ngx_shmtx_lock(&shpool->mutex);
  710. if ((up = find_node(id, ctx, r->connection->log)) != NULL) {
  711. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  712. "trackuploads error-tracking found node for id: %V", id);
  713. up->err_status = r->err_status;
  714. ngx_shmtx_unlock(&shpool->mutex);
  715. goto finish;
  716. }
  717. /* no lz found for this tracking id */
  718. n = sizeof(ngx_http_uploadprogress_node_t) + id->len;
  719. cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
  720. if (cln == NULL) {
  721. ngx_shmtx_unlock(&shpool->mutex);
  722. goto finish;
  723. }
  724. node = ngx_slab_alloc_locked(shpool, n);
  725. if (node == NULL) {
  726. ngx_shmtx_unlock(&shpool->mutex);
  727. goto finish;
  728. }
  729. up = (ngx_http_uploadprogress_node_t *) node;
  730. node->key = hash;
  731. up->len = (u_char) id->len;
  732. up->err_status = r->err_status;
  733. ngx_memcpy(up->data, id->data, id->len);
  734. up->next = ctx->list_head.next;
  735. up->next->prev = up;
  736. up->prev = &ctx->list_head;
  737. ctx->list_head.next = up;
  738. ngx_rbtree_insert(ctx->rbtree, node);
  739. /* start the timer if needed */
  740. if (!upcf->cleanup.timer_set) {
  741. upcf->cleanup.data = upcf->shm_zone;
  742. upcf->cleanup.handler = ngx_clean_old_connections;
  743. upcf->cleanup.log = upcf->shm_zone->shm.log;
  744. ngx_add_timer(&upcf->cleanup, TIMER_FREQUENCY);
  745. }
  746. ngx_shmtx_unlock(&shpool->mutex);
  747. cln->handler = ngx_http_uploadprogress_cleanup;
  748. upcln = cln->data;
  749. upcln->shm_zone = upcf->shm_zone;
  750. upcln->node = node;
  751. upcln->timeout = upcf->timeout;
  752. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  753. "trackuploads error-tracking adding: %08XD", node->key);
  754. }
  755. finish:
  756. /* call the filter chain as usual */
  757. return ngx_http_next_header_filter(r);
  758. }
  759. static ngx_int_t
  760. ngx_http_uploadprogress_init(ngx_conf_t * cf)
  761. {
  762. ngx_http_handler_pt *h;
  763. ngx_http_core_main_conf_t *cmcf;
  764. cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
  765. /* install the tracking handler */
  766. h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
  767. if (h == NULL) {
  768. return NGX_ERROR;
  769. }
  770. *h = ngx_http_uploadprogress_handler;
  771. /*
  772. we also need to track HTTP errors
  773. unfortunately, the above handler is not called in case of
  774. errors.
  775. we have to register a header output filter that will be
  776. called in any case to track those errors
  777. */
  778. ngx_http_next_header_filter = ngx_http_top_header_filter;
  779. ngx_http_top_header_filter = ngx_http_uploadprogress_errortracker;
  780. return NGX_OK;
  781. }
  782. static void*
  783. ngx_http_uploadprogress_create_loc_conf(ngx_conf_t * cf)
  784. {
  785. ngx_http_uploadprogress_conf_t *conf;
  786. conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_conf_t));
  787. if (conf == NULL) {
  788. return NGX_CONF_ERROR;
  789. }
  790. return conf;
  791. }
  792. static char*
  793. ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t * cf, void *parent, void *child)
  794. {
  795. ngx_http_uploadprogress_conf_t *prev = parent;
  796. ngx_http_uploadprogress_conf_t *conf = child;
  797. if (conf->shm_zone == NULL) {
  798. *conf = *prev;
  799. }
  800. return NGX_CONF_OK;
  801. }
  802. static char*
  803. ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  804. {
  805. ssize_t n;
  806. ngx_str_t *value;
  807. ngx_shm_zone_t *shm_zone;
  808. ngx_http_uploadprogress_ctx_t *ctx;
  809. value = cf->args->elts;
  810. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  811. "ngx_upload_progress name: %V", &value[1]);
  812. ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_ctx_t));
  813. if (ctx == NULL) {
  814. return NGX_CONF_ERROR;
  815. }
  816. ctx->list_head.prev = NULL;
  817. ctx->list_head.next = &ctx->list_tail;
  818. ctx->list_tail.prev = &ctx->list_head;
  819. ctx->list_tail.next = NULL;
  820. n = ngx_parse_size(&value[2]);
  821. if (n == NGX_ERROR) {
  822. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  823. "invalid size of track_uploads \"%V\"", &value[2]);
  824. return NGX_CONF_ERROR;
  825. }
  826. if (n < (ngx_int_t) (8 * ngx_pagesize)) {
  827. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  828. "track_uploads \"%V\" is too small", &value[1]);
  829. return NGX_CONF_ERROR;
  830. }
  831. shm_zone = ngx_shared_memory_add(cf, &value[1], n,
  832. &ngx_http_uploadprogress_module);
  833. if (shm_zone == NULL) {
  834. return NGX_CONF_ERROR;
  835. }
  836. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  837. "ngx_upload_progress name: %V, szhm_zone: %p", &value[1],
  838. shm_zone);
  839. if (shm_zone->data) {
  840. ctx = shm_zone->data;
  841. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  842. "track_uploads \"%V\" is already created", &value[1]);
  843. return NGX_CONF_ERROR;
  844. }
  845. shm_zone->init = ngx_http_uploadprogress_init_zone;
  846. shm_zone->data = ctx;
  847. return NGX_CONF_OK;
  848. }
  849. static char*
  850. ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  851. {
  852. ngx_http_core_loc_conf_t *clcf;
  853. ngx_http_uploadprogress_conf_t *lzcf = conf;
  854. ngx_str_t *value;
  855. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_track_uploads in");
  856. value = cf->args->elts;
  857. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  858. "ngx_track_uploads name: %V", &value[1]);
  859. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  860. &ngx_http_uploadprogress_module);
  861. if (lzcf->shm_zone == NULL) {
  862. return NGX_CONF_ERROR;
  863. }
  864. lzcf->track = (u_char) 1;
  865. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  866. "ngx_track_uploads name: %V,szhm_zone: %p", &value[1],
  867. lzcf->shm_zone);
  868. lzcf->timeout = ngx_parse_time(&value[2], 1);
  869. if (lzcf->timeout == NGX_ERROR) {
  870. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  871. "track_uploads \"%V\" timeout value invalid", &value[1]);
  872. return NGX_CONF_ERROR;
  873. }
  874. if (lzcf->timeout == NGX_PARSE_LARGE_TIME) {
  875. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  876. "track_uploads \"%V\" timeout value must be less than 68 years", &value[1]);
  877. return NGX_CONF_ERROR;
  878. }
  879. clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  880. lzcf->handler = clcf->handler;
  881. if ( lzcf->handler == NULL )
  882. {
  883. return "track_upload should be the last directive in the location, after either proxy_pass or fastcgi_pass";
  884. }
  885. clcf->handler = ngx_http_uploadprogress_content_handler;
  886. return NGX_CONF_OK;
  887. }
  888. static char*
  889. ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  890. {
  891. ngx_http_uploadprogress_conf_t *lzcf = conf;
  892. ngx_http_core_loc_conf_t *clcf;
  893. ngx_str_t *value;
  894. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_report_uploads in");
  895. value = cf->args->elts;
  896. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  897. "ngx_report_uploads name: %V", &value[1]);
  898. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  899. &ngx_http_uploadprogress_module);
  900. if (lzcf->shm_zone == NULL) {
  901. return NGX_CONF_ERROR;
  902. }
  903. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  904. "ngx_report_uploads name: %V, szhm_zone: %p", &value[1],
  905. lzcf->shm_zone);
  906. lzcf->track = (u_char) 0;
  907. /* install our report handler */
  908. clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  909. clcf->handler = ngx_http_reportuploads_handler;
  910. return NGX_CONF_OK;
  911. }