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

ngx_http_uploadprogress_module.c 34KB

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