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

ngx_http_uploadprogress_module.c 29KB

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