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

ngx_http_uploadprogress_module.c 39KB

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