You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ngx_http_uploadprogress_module.c 39KB

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