Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ngx_http_uploadprogress_module.c 34KB

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