Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ngx_http_uploadprogress_module.c 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /*
  2. * Copyright (C) 2007 Brice Figureau
  3. */
  4. #include <ngx_config.h>
  5. #include <ngx_core.h>
  6. #include <ngx_http.h>
  7. typedef struct {
  8. u_char color;
  9. ngx_http_request_t *r;
  10. u_char len;
  11. u_char data[1];
  12. } ngx_http_uploadprogress_node_t;
  13. typedef struct {
  14. ngx_shm_zone_t *shm_zone;
  15. ngx_rbtree_node_t *node;
  16. } ngx_http_uploadprogress_cleanup_t;
  17. typedef struct {
  18. ngx_rbtree_t *rbtree;
  19. } ngx_http_uploadprogress_ctx_t;
  20. typedef struct {
  21. ngx_shm_zone_t *shm_zone;
  22. u_char track;
  23. } ngx_http_uploadprogress_conf_t;
  24. static ngx_int_t ngx_http_reportuploads_handler(ngx_http_request_t *r);
  25. static ngx_int_t ngx_http_reportuploads_handler(ngx_http_request_t *r);
  26. static void ngx_http_uploadprogress_cleanup(void *data);
  27. static char *ngx_http_report_uploads(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  28. static ngx_int_t ngx_http_uploadprogress_init_zone(ngx_shm_zone_t *shm_zone, void *data);
  29. static ngx_int_t ngx_http_uploadprogress_init(ngx_conf_t *cf);
  30. static void *ngx_http_uploadprogress_create_loc_conf(ngx_conf_t *cf);
  31. static char *ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
  32. static char *ngx_http_track_uploads(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  33. static char *ngx_http_report_uploads(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  34. static char *ngx_http_upload_progress(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  35. static ngx_command_t ngx_http_uploadprogress_commands[] = {
  36. { ngx_string("upload_progress"),
  37. NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2,
  38. ngx_http_upload_progress,
  39. 0,
  40. 0,
  41. NULL },
  42. { ngx_string("track_uploads"),
  43. NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  44. ngx_http_track_uploads,
  45. NGX_HTTP_LOC_CONF_OFFSET,
  46. 0,
  47. NULL },
  48. { ngx_string("report_uploads"),
  49. NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  50. ngx_http_report_uploads,
  51. NGX_HTTP_LOC_CONF_OFFSET,
  52. 0,
  53. NULL },
  54. ngx_null_command
  55. };
  56. static ngx_http_module_t ngx_http_uploadprogress_module_ctx = {
  57. NULL, /* preconfiguration */
  58. ngx_http_uploadprogress_init, /* postconfiguration */
  59. NULL, /* create main configuration */
  60. NULL, /* init main configuration */
  61. NULL, /* create server configuration */
  62. NULL, /* merge server configuration */
  63. ngx_http_uploadprogress_create_loc_conf, /* create location configuration */
  64. ngx_http_uploadprogress_merge_loc_conf /* merge location configuration */
  65. };
  66. ngx_module_t ngx_http_uploadprogress_module = {
  67. NGX_MODULE_V1,
  68. &ngx_http_uploadprogress_module_ctx, /* module context */
  69. ngx_http_uploadprogress_commands, /* module directives */
  70. NGX_HTTP_MODULE, /* module type */
  71. NULL, /* init master */
  72. NULL, /* init module */
  73. NULL, /* init process */
  74. NULL, /* init thread */
  75. NULL, /* exit thread */
  76. NULL, /* exit process */
  77. NULL, /* exit master */
  78. NGX_MODULE_V1_PADDING
  79. };
  80. static ngx_str_t x_progress_id = ngx_string("X-Progress-ID");
  81. static ngx_str_t*
  82. get_tracking_id(ngx_http_request_t *r)
  83. {
  84. u_char *p,*start_p;
  85. ngx_uint_t i;
  86. ngx_list_part_t *part;
  87. ngx_table_elt_t *header;
  88. ngx_str_t *ret;
  89. part = &r->headers_in.headers.part;
  90. header = part->elts;
  91. for (i = 0; /* void */; i++) {
  92. if (i >= part->nelts) {
  93. if (part->next == NULL) {
  94. break;
  95. }
  96. part = part->next;
  97. header = part->elts;
  98. i = 0;
  99. }
  100. if (header[i].key.len == x_progress_id.len && ngx_strncmp(header[i].key.data, x_progress_id.data,header[i].key.len) == 0)
  101. {
  102. ret = ngx_pcalloc(r->pool, sizeof(ngx_str_t));
  103. ret->data = header[i].value.data;
  104. ret->len = header[i].value.len;
  105. return ret;
  106. }
  107. }
  108. /* not found, check as a reaquest arg */
  109. if (r->args.len) {
  110. p = (u_char *) ngx_strstr(r->args.data, "X-Progress-ID=");
  111. if (p) {
  112. start_p = p += 14;
  113. while (p < r->args.data + r->args.len) {
  114. if (*p++ != '&') {
  115. continue;
  116. }
  117. }
  118. ret = ngx_pcalloc(r->pool, sizeof(ngx_str_t));
  119. ret->data = start_p;
  120. ret->len = p-start_p;
  121. return ret;
  122. }
  123. }
  124. return NULL;
  125. }
  126. /* This generates the response for the report */
  127. static ngx_int_t
  128. ngx_http_reportuploads_handler(ngx_http_request_t *r)
  129. {
  130. ngx_str_t *id;
  131. ngx_buf_t *b;
  132. ngx_chain_t out;
  133. ngx_http_request_t *orig;
  134. ngx_int_t rc, size;
  135. uint32_t hash;
  136. ngx_slab_pool_t *shpool;
  137. ngx_rbtree_node_t *node, *sentinel;
  138. ngx_http_uploadprogress_conf_t *lzcf;
  139. ngx_http_uploadprogress_ctx_t *ctx;
  140. ngx_http_uploadprogress_node_t *lz;
  141. ngx_table_elt_t *expires, *cc, **ccp;
  142. if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
  143. return NGX_HTTP_NOT_ALLOWED;
  144. }
  145. rc = ngx_http_discard_request_body(r);
  146. if (rc != NGX_OK) {
  147. return rc;
  148. }
  149. /* get the tracking id if any */
  150. id = get_tracking_id(r);
  151. if ( id == NULL )
  152. {
  153. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  154. "reportuploads handler cant find id");
  155. return NGX_DECLINED;
  156. }
  157. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  158. "reportuploads handler found id: %V", id);
  159. lzcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  160. if (lzcf->shm_zone == NULL) {
  161. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  162. "reportuploads no shm_zone for id: %V", id);
  163. return NGX_DECLINED;
  164. }
  165. orig = NULL;
  166. ctx = lzcf->shm_zone->data;
  167. hash = ngx_crc32_short(id->data, id->len);
  168. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  169. "reportuploads trying to find block with hash %08XD for id: %V", hash, id);
  170. /* get the original connection of the upload */
  171. shpool = (ngx_slab_pool_t *) lzcf->shm_zone->shm.addr;
  172. ngx_shmtx_lock(&shpool->mutex);
  173. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  174. "reportuploads in mutex lock for hash %08XD for id: %V", hash, id);
  175. node = ctx->rbtree->root;
  176. sentinel = ctx->rbtree->sentinel;
  177. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  178. "reportuploads root %p, sentinel %p for id: %V", node,sentinel, id);
  179. while (node != sentinel) {
  180. if (hash < node->key) {
  181. node = node->left;
  182. continue;
  183. }
  184. if (hash > node->key) {
  185. node = node->right;
  186. continue;
  187. }
  188. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  189. "reportuploads found matching hash %08XD, node %p for id: %V", hash, node, id);
  190. /* hash == node->key */
  191. do {
  192. lz = (ngx_http_uploadprogress_node_t *) &node->color;
  193. rc = ngx_memn2cmp(id->data, lz->data, id->len, (size_t) lz->len);
  194. if (rc == 0) {
  195. /* found the right one */
  196. /* lz contains the right node*/
  197. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  198. "reportuploads found request: %p", lz->r);
  199. orig = lz->r;
  200. goto found;
  201. }
  202. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  203. "reportuploads oops not the same : lz %V != id %V", lz, id);
  204. node = (rc < 0) ? node->left : node->right;
  205. } while (node != sentinel && hash == node->key);
  206. lz = NULL;
  207. /* couldn't find one */
  208. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  209. "reportuploads not matching request");
  210. }
  211. found:
  212. ngx_shmtx_unlock(&shpool->mutex);
  213. /* send the output */
  214. r->headers_out.content_type.len = sizeof("text/javascript") - 1;
  215. r->headers_out.content_type.data = (u_char *) "text/javascript";
  216. /* no-cache */
  217. expires = r->headers_out.expires;
  218. if (expires == NULL) {
  219. expires = ngx_list_push(&r->headers_out.headers);
  220. if (expires == NULL) {
  221. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  222. }
  223. r->headers_out.expires = expires;
  224. expires->hash = 1;
  225. expires->key.len = sizeof("Expires") - 1;
  226. expires->key.data = (u_char *) "Expires";
  227. }
  228. len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT");
  229. expires->value.len = len - 1;
  230. ccp = r->headers_out.cache_control.elts;
  231. if (ccp == NULL) {
  232. if (ngx_array_init(&r->headers_out.cache_control, r->pool,
  233. 1, sizeof(ngx_table_elt_t *))
  234. != NGX_OK)
  235. {
  236. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  237. }
  238. ccp = ngx_array_push(&r->headers_out.cache_control);
  239. if (ccp == NULL) {
  240. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  241. }
  242. cc = ngx_list_push(&r->headers_out.headers);
  243. if (cc == NULL) {
  244. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  245. }
  246. cc->hash = 1;
  247. cc->key.len = sizeof("Cache-Control") - 1;
  248. cc->key.data = (u_char *) "Cache-Control";
  249. *ccp = cc;
  250. } else {
  251. for (i = 1; i < r->headers_out.cache_control.nelts; i++) {
  252. ccp[i]->hash = 0;
  253. }
  254. cc = ccp[0];
  255. }
  256. expires->value.data = (u_char *) "Thu, 01 Jan 1970 00:00:01 GMT";
  257. cc->value.len = sizeof("no-cache") - 1;
  258. cc->value.data = (u_char *) "no-cache";
  259. if (r->method == NGX_HTTP_HEAD) {
  260. r->headers_out.status = NGX_HTTP_OK;
  261. rc = ngx_http_send_header(r);
  262. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  263. return rc;
  264. }
  265. }
  266. if ( orig == NULL )
  267. {
  268. if (lz != NULL )
  269. {
  270. size = sizeof("new Object({ 'state' : 'done' })\r\n");
  271. }
  272. else
  273. {
  274. size = sizeof("new Object({ 'state' : 'starting' })\r\n");
  275. }
  276. }
  277. else if ( orig->err_status == 413)
  278. {
  279. size = sizeof("new Object({ 'state' : 'error', 'status' : 413 })\r\n");
  280. }
  281. else
  282. {
  283. size = sizeof("new Object({ 'state' : 'uploading', 'received' : ") + NGX_INT_T_LEN + sizeof(" })\r\n");
  284. size += sizeof(", 'size' : ") + NGX_INT_T_LEN;
  285. }
  286. b = ngx_create_temp_buf(r->pool, size);
  287. if (b == NULL) {
  288. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  289. }
  290. out.buf = b;
  291. out.next = NULL;
  292. if (orig == NULL)
  293. {
  294. if (lz == NULL )
  295. {
  296. b->last = ngx_cpymem(b->last, "new Object({ 'state' : 'starting' })\r\n",
  297. sizeof("new Object({ 'state' : 'starting' })\r\n") - 1);
  298. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  299. "reportuploads returning starting");
  300. }
  301. else
  302. {
  303. b->last = ngx_cpymem(b->last, "new Object({ 'state' : 'done' })\r\n",
  304. sizeof("new Object({ 'state' : 'done' })\r\n") - 1);
  305. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  306. "reportuploads returning done");
  307. }
  308. }
  309. else if ( orig->err_status == 413)
  310. {
  311. b->last = ngx_cpymem(b->last, "new Object({ 'state' : 'error', 'status' : 413 })\r\n",
  312. sizeof("new Object({ 'state' : 'error', 'status' : 413 })\r\n") - 1);
  313. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  314. "reportuploads returning error 413");
  315. }
  316. else
  317. {
  318. b->last = ngx_cpymem(b->last,"new Object({ 'state' : 'uploading', 'received' : ",
  319. sizeof("new Object({ 'state' : 'uploading', 'received' : ")-1 );
  320. b->last = ngx_sprintf(b->last, "%uO, 'size' : %uO })\r\n", (orig->headers_in.content_length_n - orig->request_body->rest), orig->headers_in.content_length_n);
  321. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  322. "reportuploads returning %uO / %uO", (orig->headers_in.content_length_n - orig->request_body->rest), orig->headers_in.content_length_n);
  323. }
  324. // force no caching for proxy
  325. r->headers_out.status = NGX_HTTP_OK;
  326. r->headers_out.content_length_n = b->last - b->pos;
  327. b->last_buf = 1;
  328. rc = ngx_http_send_header(r);
  329. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  330. return rc;
  331. }
  332. return ngx_http_output_filter(r, &out);;
  333. }
  334. /*
  335. This is the post read phase. It registers the upload connection in the rb tree
  336. */
  337. static ngx_int_t
  338. ngx_http_uploadprogress_handler(ngx_http_request_t *r)
  339. {
  340. size_t n;
  341. ngx_str_t *id;
  342. ngx_int_t rc;
  343. uint32_t hash;
  344. ngx_slab_pool_t *shpool;
  345. ngx_rbtree_node_t *node, *sentinel;
  346. ngx_http_uploadprogress_conf_t *lzcf;
  347. ngx_http_uploadprogress_ctx_t *ctx;
  348. ngx_http_uploadprogress_node_t *lz;
  349. ngx_http_uploadprogress_cleanup_t *lzcln;
  350. ngx_pool_cleanup_t *cln;
  351. id = get_tracking_id(r);
  352. if ( id == NULL )
  353. {
  354. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  355. "trackuploads no id found in POST upload req");
  356. return NGX_DECLINED;
  357. }
  358. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  359. "trackuploads id found: %V", id);
  360. lzcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  361. if (!lzcf->track) {
  362. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  363. "trackuploads not tracking in this location for id: %V", id);
  364. return NGX_DECLINED;
  365. }
  366. if (lzcf->shm_zone == NULL) {
  367. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  368. "trackuploads no shm_zone for id: %V", id);
  369. return NGX_DECLINED;
  370. }
  371. ctx = lzcf->shm_zone->data;
  372. hash = ngx_crc32_short(id->data, id->len);
  373. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  374. "trackuploads hash %08XD for id: %V",hash, id);
  375. cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
  376. if (cln == NULL) {
  377. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  378. }
  379. shpool = (ngx_slab_pool_t *) lzcf->shm_zone->shm.addr;
  380. ngx_shmtx_lock(&shpool->mutex);
  381. node = ctx->rbtree->root;
  382. sentinel = ctx->rbtree->sentinel;
  383. while (node != sentinel) {
  384. if (hash < node->key) {
  385. node = node->left;
  386. continue;
  387. }
  388. if (hash > node->key) {
  389. node = node->right;
  390. continue;
  391. }
  392. /* hash == node->key */
  393. do {
  394. lz = (ngx_http_uploadprogress_node_t *) &node->color;
  395. rc = ngx_memn2cmp(id->data, lz->data, id->len, (size_t) lz->len);
  396. if (rc == 0) {
  397. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  398. "trackuploads already registered: %V",id);
  399. /* oops found already one */
  400. ngx_shmtx_unlock(&shpool->mutex);
  401. return NGX_HTTP_SERVICE_UNAVAILABLE;
  402. }
  403. node = (rc < 0) ? node->left : node->right;
  404. } while (node != sentinel && hash == node->key);
  405. break;
  406. }
  407. n = offsetof(ngx_rbtree_node_t, color)
  408. + offsetof(ngx_http_uploadprogress_node_t, data)
  409. + id->len;
  410. node = ngx_slab_alloc_locked(shpool, n);
  411. if (node == NULL) {
  412. ngx_shmtx_unlock(&shpool->mutex);
  413. return NGX_HTTP_SERVICE_UNAVAILABLE;
  414. }
  415. lz = (ngx_http_uploadprogress_node_t *) &node->color;
  416. node->key = hash;
  417. lz->len = (u_char) id->len;
  418. lz->r = r;
  419. ngx_memcpy(lz->data, id->data, id->len);
  420. ngx_rbtree_insert(ctx->rbtree, node);
  421. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  422. "trackuploads: %08XD", node->key);
  423. ngx_shmtx_unlock(&shpool->mutex);
  424. cln->handler = ngx_http_uploadprogress_cleanup;
  425. lzcln = cln->data;
  426. lzcln->shm_zone = lzcf->shm_zone;
  427. lzcln->node = node;
  428. return NGX_DECLINED;
  429. }
  430. static void
  431. ngx_http_uploadprogress_rbtree_insert_value(ngx_rbtree_node_t *temp,
  432. ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
  433. {
  434. ngx_http_uploadprogress_node_t *lzn, *lznt;
  435. for ( ;; ) {
  436. if (node->key < temp->key) {
  437. if (temp->left == sentinel) {
  438. temp->left = node;
  439. break;
  440. }
  441. temp = temp->left;
  442. } else if (node->key > temp->key) {
  443. if (temp->right == sentinel) {
  444. temp->right = node;
  445. break;
  446. }
  447. temp = temp->right;
  448. } else { /* node->key == temp->key */
  449. lzn = (ngx_http_uploadprogress_node_t *) &node->color;
  450. lznt = (ngx_http_uploadprogress_node_t *) &temp->color;
  451. if (ngx_memn2cmp(lzn->data, lznt->data, lzn->len, lznt->len) < 0) {
  452. if (temp->left == sentinel) {
  453. temp->left = node;
  454. break;
  455. }
  456. temp = temp->left;
  457. } else {
  458. if (temp->right == sentinel) {
  459. temp->right = node;
  460. break;
  461. }
  462. temp = temp->right;
  463. }
  464. }
  465. }
  466. node->parent = temp;
  467. node->left = sentinel;
  468. node->right = sentinel;
  469. ngx_rbt_red(node);
  470. }
  471. /*
  472. removes the expired node from the upload rbtree
  473. */
  474. static void
  475. ngx_http_uploadprogress_cleanup(void *data)
  476. {
  477. ngx_http_uploadprogress_cleanup_t *lzcln = data;
  478. ngx_slab_pool_t *shpool;
  479. ngx_rbtree_node_t *node;
  480. ngx_http_uploadprogress_ctx_t *ctx;
  481. ngx_http_uploadprogress_node_t *lz;
  482. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, lzcln->shm_zone->shm.log, 0,
  483. "uploadprogress cleanup called");
  484. ctx = lzcln->shm_zone->data;
  485. shpool = (ngx_slab_pool_t *) lzcln->shm_zone->shm.addr;
  486. node = lzcln->node;
  487. lz = (ngx_http_uploadprogress_node_t *) &node->color;
  488. ngx_shmtx_lock(&shpool->mutex);
  489. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, lzcln->shm_zone->shm.log, 0,
  490. "upload progress cleanup: %08XD", node->key);
  491. ngx_rbtree_delete(ctx->rbtree, node);
  492. ngx_slab_free_locked(shpool, node);
  493. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, lzcln->shm_zone->shm.log, 0,
  494. "upload progress cleanup node removed: %08XD", node->key);
  495. ngx_shmtx_unlock(&shpool->mutex);
  496. }
  497. static ngx_int_t
  498. ngx_http_uploadprogress_init_zone(ngx_shm_zone_t *shm_zone, void *data)
  499. {
  500. ngx_http_uploadprogress_ctx_t *octx = data;
  501. ngx_slab_pool_t *shpool;
  502. ngx_rbtree_node_t *sentinel;
  503. ngx_http_uploadprogress_ctx_t *ctx;
  504. ctx = shm_zone->data;
  505. if (octx) {
  506. ctx->rbtree = octx->rbtree;
  507. return NGX_OK;
  508. }
  509. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  510. ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t));
  511. if (ctx->rbtree == NULL) {
  512. return NGX_ERROR;
  513. }
  514. sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t));
  515. if (sentinel == NULL) {
  516. return NGX_ERROR;
  517. }
  518. ngx_rbtree_sentinel_init(sentinel);
  519. ctx->rbtree->root = sentinel;
  520. ctx->rbtree->sentinel = sentinel;
  521. ctx->rbtree->insert = ngx_http_uploadprogress_rbtree_insert_value;
  522. return NGX_OK;
  523. }
  524. static ngx_int_t
  525. ngx_http_uploadprogress_init(ngx_conf_t *cf)
  526. {
  527. ngx_http_handler_pt *h;
  528. ngx_http_core_main_conf_t *cmcf;
  529. cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
  530. h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
  531. if (h == NULL) {
  532. return NGX_ERROR;
  533. }
  534. *h = ngx_http_uploadprogress_handler;
  535. return NGX_OK;
  536. }
  537. static void *
  538. ngx_http_uploadprogress_create_loc_conf(ngx_conf_t *cf)
  539. {
  540. ngx_http_uploadprogress_conf_t *conf;
  541. conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_conf_t));
  542. if (conf == NULL) {
  543. return NGX_CONF_ERROR;
  544. }
  545. return conf;
  546. }
  547. static char *
  548. ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
  549. {
  550. ngx_http_uploadprogress_conf_t *prev = parent;
  551. ngx_http_uploadprogress_conf_t *conf = child;
  552. if (conf->shm_zone == NULL) {
  553. *conf = *prev;
  554. }
  555. return NGX_CONF_OK;
  556. }
  557. static char *
  558. ngx_http_upload_progress(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  559. {
  560. ssize_t n;
  561. ngx_str_t *value;
  562. ngx_shm_zone_t *shm_zone;
  563. ngx_http_uploadprogress_ctx_t *ctx;
  564. value = cf->args->elts;
  565. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  566. "ngx_upload_progress name: %V", &value[1]);
  567. ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_ctx_t));
  568. if (ctx == NULL) {
  569. return NGX_CONF_ERROR;
  570. }
  571. n = ngx_parse_size(&value[2]);
  572. if (n == NGX_ERROR) {
  573. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  574. "invalid size of track_uploads \"%V\"", &value[2]);
  575. return NGX_CONF_ERROR;
  576. }
  577. if (n < (ngx_int_t) (8 * ngx_pagesize)) {
  578. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  579. "track_uploads \"%V\" is too small", &value[1]);
  580. return NGX_CONF_ERROR;
  581. }
  582. shm_zone = ngx_shared_memory_add(cf, &value[1], n,
  583. &ngx_http_uploadprogress_module);
  584. if (shm_zone == NULL) {
  585. return NGX_CONF_ERROR;
  586. }
  587. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  588. "ngx_upload_progress name: %V, szhm_zone: %p", value[1], shm_zone);
  589. if (shm_zone->data) {
  590. ctx = shm_zone->data;
  591. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  592. "track_uploads \"%V\" is already created",
  593. &value[1]);
  594. return NGX_CONF_ERROR;
  595. }
  596. shm_zone->init = ngx_http_uploadprogress_init_zone;
  597. shm_zone->data = ctx;
  598. return NGX_CONF_OK;
  599. }
  600. static char *
  601. ngx_http_track_uploads(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  602. {
  603. ngx_http_uploadprogress_conf_t *lzcf = conf;
  604. ngx_str_t *value;
  605. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  606. "ngx_track_uploads in");
  607. value = cf->args->elts;
  608. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  609. "ngx_track_uploads name: %V", value[1]);
  610. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  611. &ngx_http_uploadprogress_module);
  612. if (lzcf->shm_zone == NULL) {
  613. return NGX_CONF_ERROR;
  614. }
  615. lzcf->track = (u_char)1;
  616. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  617. "ngx_track_uploads name: %V,szhm_zone: %p", value[1], lzcf->shm_zone);
  618. return NGX_CONF_OK;
  619. }
  620. static char *
  621. ngx_http_report_uploads(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  622. {
  623. ngx_http_uploadprogress_conf_t *lzcf = conf;
  624. ngx_http_core_loc_conf_t *clcf;
  625. ngx_str_t *value;
  626. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  627. "ngx_report_uploads in");
  628. value = cf->args->elts;
  629. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  630. "ngx_report_uploads name: %V", value[1]);
  631. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  632. &ngx_http_uploadprogress_module);
  633. if (lzcf->shm_zone == NULL) {
  634. return NGX_CONF_ERROR;
  635. }
  636. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  637. "ngx_report_uploads name: %V, szhm_zone: %p", value[1], lzcf->shm_zone);
  638. lzcf->track = (u_char)0;
  639. /* install our report handler */
  640. clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  641. clcf->handler = ngx_http_reportuploads_handler;
  642. return NGX_CONF_OK;
  643. }