選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ngx_http_uploadprogress_module.c 36KB

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