Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ngx_http_uploadprogress_module.c 34KB

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