Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ngx_http_uploadprogress_module.c 29KB

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