您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ngx_http_uploadprogress_module.c 36KB

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