Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ngx_http_uploadprogress_module.c 36KB

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