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

ngx_http_uploadprogress_module.c 36KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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. ngx_uint_t rest;
  14. ngx_uint_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, rest=0, length=0, done=0, err_status=0;
  271. ngx_uint_t len, i;
  272. ngx_slab_pool_t *shpool;
  273. ngx_http_uploadprogress_conf_t *upcf;
  274. ngx_http_uploadprogress_ctx_t *ctx;
  275. ngx_http_uploadprogress_node_t *up;
  276. ngx_table_elt_t *expires, *cc, **ccp;
  277. if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
  278. return NGX_HTTP_NOT_ALLOWED;
  279. }
  280. rc = ngx_http_discard_request_body(r);
  281. if (rc != NGX_OK) {
  282. return rc;
  283. }
  284. /* get the tracking id if any */
  285. id = get_tracking_id(r);
  286. if (id == NULL) {
  287. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  288. "reportuploads handler cant find id");
  289. return NGX_DECLINED;
  290. }
  291. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  292. "reportuploads handler found id: %V", id);
  293. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  294. if (upcf->shm_zone == NULL) {
  295. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  296. "reportuploads no shm_zone for id: %V", id);
  297. return NGX_DECLINED;
  298. }
  299. ctx = upcf->shm_zone->data;
  300. /* get the original connection of the upload */
  301. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  302. ngx_shmtx_lock(&shpool->mutex);
  303. up = find_node(id, ctx, r->connection->log);
  304. if (up != NULL) {
  305. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  306. "reportuploads found node: %V", id);
  307. rest = up->rest;
  308. length = up->length;
  309. done = up->done;
  310. err_status = up->err_status;
  311. found = 1;
  312. } else {
  313. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  314. "reportuploads not found: %V", id);
  315. }
  316. ngx_shmtx_unlock(&shpool->mutex);
  317. /* send the output */
  318. r->headers_out.content_type.len = sizeof("text/javascript") - 1;
  319. r->headers_out.content_type.data = (u_char *) "text/javascript";
  320. /* force no-cache */
  321. expires = r->headers_out.expires;
  322. if (expires == NULL) {
  323. expires = ngx_list_push(&r->headers_out.headers);
  324. if (expires == NULL) {
  325. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  326. }
  327. r->headers_out.expires = expires;
  328. expires->hash = 1;
  329. expires->key.len = sizeof("Expires") - 1;
  330. expires->key.data = (u_char *) "Expires";
  331. }
  332. len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT");
  333. expires->value.len = len - 1;
  334. ccp = r->headers_out.cache_control.elts;
  335. if (ccp == NULL) {
  336. if (ngx_array_init(&r->headers_out.cache_control, r->pool,
  337. 1, sizeof(ngx_table_elt_t *))
  338. != NGX_OK) {
  339. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  340. }
  341. ccp = ngx_array_push(&r->headers_out.cache_control);
  342. if (ccp == NULL) {
  343. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  344. }
  345. cc = ngx_list_push(&r->headers_out.headers);
  346. if (cc == NULL) {
  347. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  348. }
  349. cc->hash = 1;
  350. cc->key.len = sizeof("Cache-Control") - 1;
  351. cc->key.data = (u_char *) "Cache-Control";
  352. *ccp = cc;
  353. } else {
  354. for (i = 1; i < r->headers_out.cache_control.nelts; i++) {
  355. ccp[i]->hash = 0;
  356. }
  357. cc = ccp[0];
  358. }
  359. expires->value.data = (u_char *) "Thu, 01 Jan 1970 00:00:01 GMT";
  360. cc->value.len = sizeof("no-cache") - 1;
  361. cc->value.data = (u_char *) "no-cache";
  362. if (r->method == NGX_HTTP_HEAD) {
  363. r->headers_out.status = NGX_HTTP_OK;
  364. rc = ngx_http_send_header(r);
  365. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  366. return rc;
  367. }
  368. }
  369. /*
  370. There are 4 possibilities
  371. * request not yet started: found = false
  372. * request in error: err_status >= NGX_HTTP_SPECIAL_RESPONSE
  373. * request finished: done = true
  374. * reauest in progress: rest > 0
  375. */
  376. if (!found) {
  377. size = sizeof("new Object({ 'state' : 'starting' })\r\n");
  378. } else if (err_status >= NGX_HTTP_SPECIAL_RESPONSE) {
  379. size = sizeof("new Object({ 'state' : 'error', 'status' : ") + NGX_INT_T_LEN + sizeof(" })\r\n");
  380. } else if (done) {
  381. size = sizeof("new Object({ 'state' : 'done' })\r\n");
  382. } else {
  383. size =
  384. sizeof("new Object({ 'state' : 'uploading', 'received' : ") +
  385. NGX_INT_T_LEN + sizeof(" })\r\n");
  386. size += sizeof(", 'size' : ") + NGX_INT_T_LEN;
  387. }
  388. b = ngx_create_temp_buf(r->pool, size);
  389. if (b == NULL) {
  390. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  391. }
  392. out.buf = b;
  393. out.next = NULL;
  394. if (!found) {
  395. b->last = ngx_cpymem(b->last, "new Object({ 'state' : 'starting' })\r\n",
  396. sizeof("new Object({ 'state' : 'starting' })\r\n") -
  397. 1);
  398. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  399. "reportuploads returning starting");
  400. } else if (err_status >= NGX_HTTP_SPECIAL_RESPONSE) {
  401. b->last = ngx_cpymem(b->last, "new Object({ 'state' : 'error', 'status' : ",
  402. sizeof("new Object({ 'state' : 'error', 'status' : ") - 1);
  403. b->last = ngx_sprintf(b->last, "%ui })\r\n", err_status );
  404. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  405. "reportuploads returning error condition: %ui", err_status);
  406. } else if (done) {
  407. b->last = ngx_cpymem(b->last, "new Object({ 'state' : 'done' })\r\n",
  408. sizeof("new Object({ 'state' : 'done' })\r\n") -
  409. 1);
  410. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  411. "reportuploads returning done");
  412. } else {
  413. b->last =
  414. ngx_cpymem(b->last, "new Object({ 'state' : 'uploading', 'received' : ",
  415. sizeof("new Object({ 'state' : 'uploading', 'received' : ") -
  416. 1);
  417. b->last = ngx_sprintf(b->last, "%uO, 'size' : %uO })\r\n", (length - rest), length);
  418. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  419. "reportuploads returning %uO / %uO",(length - rest), length );
  420. }
  421. r->headers_out.status = NGX_HTTP_OK;
  422. r->headers_out.content_length_n = b->last - b->pos;
  423. b->last_buf = 1;
  424. rc = ngx_http_send_header(r);
  425. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  426. return rc;
  427. }
  428. return ngx_http_output_filter(r, &out);
  429. }
  430. /*
  431. Let's register the upload connection in our connections rb-tree
  432. */
  433. static ngx_int_t
  434. ngx_http_uploadprogress_handler(ngx_http_request_t * r)
  435. {
  436. size_t n;
  437. ngx_str_t *id;
  438. uint32_t hash;
  439. ngx_slab_pool_t *shpool;
  440. ngx_rbtree_node_t *node;
  441. ngx_http_uploadprogress_conf_t *upcf;
  442. ngx_http_uploadprogress_ctx_t *ctx;
  443. ngx_http_uploadprogress_node_t *up;
  444. ngx_http_uploadprogress_cleanup_t *upcln;
  445. ngx_pool_cleanup_t *cln;
  446. /* Is it a POST connection */
  447. if (r->method != NGX_HTTP_POST) {
  448. return NGX_DECLINED;
  449. }
  450. id = get_tracking_id(r);
  451. if (id == NULL) {
  452. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  453. "trackuploads no id found in POST upload req");
  454. return NGX_DECLINED;
  455. }
  456. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  457. "trackuploads id found: %V", id);
  458. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  459. if (!upcf->track) {
  460. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  461. "trackuploads not tracking in this location for id: %V", id);
  462. return NGX_DECLINED;
  463. }
  464. if (upcf->shm_zone == NULL) {
  465. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  466. "trackuploads no shm_zone for id: %V", id);
  467. return NGX_DECLINED;
  468. }
  469. ctx = upcf->shm_zone->data;
  470. hash = ngx_crc32_short(id->data, id->len);
  471. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  472. "trackuploads hash %08XD for id: %V", hash, id);
  473. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  474. ngx_shmtx_lock(&shpool->mutex);
  475. if (find_node(id, ctx, r->connection->log) != NULL) {
  476. ngx_shmtx_unlock(&shpool->mutex);
  477. /* already found a node with matching progress ID */
  478. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  479. "upload_progress: tracking already registered id: %V", id);
  480. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  481. }
  482. cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
  483. if (cln == NULL) {
  484. ngx_shmtx_unlock(&shpool->mutex);
  485. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  486. }
  487. n = sizeof(ngx_http_uploadprogress_node_t)
  488. + id->len;
  489. node = ngx_slab_alloc_locked(shpool, n);
  490. if (node == NULL) {
  491. ngx_shmtx_unlock(&shpool->mutex);
  492. return NGX_HTTP_SERVICE_UNAVAILABLE;
  493. }
  494. up = (ngx_http_uploadprogress_node_t *) node;
  495. node->key = hash;
  496. up->len = (u_char) id->len;
  497. up->err_status = r->err_status;
  498. up->next = ctx->list_head.next;
  499. up->next->prev = up;
  500. up->prev = &ctx->list_head;
  501. ctx->list_head.next = up;
  502. ngx_memcpy(up->data, id->data, id->len);
  503. ngx_rbtree_insert(ctx->rbtree, node);
  504. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  505. "trackuploads: %08XD inserted in rbtree", node->key);
  506. if (!upcf->cleanup.timer_set) {
  507. upcf->cleanup.data = upcf->shm_zone;
  508. upcf->cleanup.handler = ngx_clean_old_connections;
  509. upcf->cleanup.log = upcf->shm_zone->shm.log;
  510. ngx_add_timer(&upcf->cleanup, TIMER_FREQUENCY);
  511. }
  512. ngx_shmtx_unlock(&shpool->mutex);
  513. cln->handler = ngx_http_uploadprogress_cleanup;
  514. upcln = cln->data;
  515. upcln->shm_zone = upcf->shm_zone;
  516. upcln->node = node;
  517. upcln->timeout = upcf->timeout;
  518. /* start the timer if needed */
  519. return NGX_DECLINED;
  520. }
  521. static void
  522. ngx_http_uploadprogress_rbtree_insert_value(ngx_rbtree_node_t * temp,
  523. ngx_rbtree_node_t * node,
  524. ngx_rbtree_node_t * sentinel)
  525. {
  526. ngx_http_uploadprogress_node_t *upn, *upnt;
  527. for (;;) {
  528. if (node->key < temp->key) {
  529. if (temp->left == sentinel) {
  530. temp->left = node;
  531. break;
  532. }
  533. temp = temp->left;
  534. } else if (node->key > temp->key) {
  535. if (temp->right == sentinel) {
  536. temp->right = node;
  537. break;
  538. }
  539. temp = temp->right;
  540. } else { /* node->key == temp->key */
  541. upn = (ngx_http_uploadprogress_node_t *) node;
  542. upnt = (ngx_http_uploadprogress_node_t *) temp;
  543. if (ngx_memn2cmp(upn->data, upnt->data, upn->len, upnt->len) < 0) {
  544. if (temp->left == sentinel) {
  545. temp->left = node;
  546. break;
  547. }
  548. temp = temp->left;
  549. } else {
  550. if (temp->right == sentinel) {
  551. temp->right = node;
  552. break;
  553. }
  554. temp = temp->right;
  555. }
  556. }
  557. }
  558. node->parent = temp;
  559. node->left = sentinel;
  560. node->right = sentinel;
  561. ngx_rbt_red(node);
  562. }
  563. static void
  564. ngx_clean_old_connections(ngx_event_t * ev)
  565. {
  566. ngx_shm_zone_t *shm_zone;
  567. ngx_http_uploadprogress_ctx_t *ctx;
  568. ngx_slab_pool_t *shpool;
  569. ngx_rbtree_node_t *node;
  570. ngx_http_uploadprogress_node_t *up;
  571. time_t now = ngx_time();
  572. /* scan the rbtree */
  573. shm_zone = ev->data;
  574. ctx = shm_zone->data;
  575. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  576. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  577. "uploadprogress clean old connections at %T", now);
  578. ngx_shmtx_lock(&shpool->mutex);
  579. node = (ngx_rbtree_node_t *) ctx->list_tail.prev;
  580. for (;;) {
  581. if (node == &ctx->list_head.node) {
  582. break;
  583. }
  584. up = (ngx_http_uploadprogress_node_t *) node;
  585. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  586. "uploadprogress clean: scanning %08XD (req done %ui) timeout at %T",
  587. node->key, up->done, up->timeout);
  588. if (up->done && up->timeout < now) {
  589. up->next->prev = up->prev;
  590. up->prev->next = up->next;
  591. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  592. "uploadprogress clean: removing %08XD (req %ui) ",
  593. node->key, up->done, up->timeout);
  594. ngx_rbtree_delete(ctx->rbtree, node);
  595. ngx_slab_free_locked(shpool, node);
  596. }
  597. node = (ngx_rbtree_node_t *) up->prev;
  598. }
  599. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  600. "uploadprogress clean old connections restarting timer");
  601. ngx_add_timer(ev, TIMER_FREQUENCY); /* trigger again in 60s */
  602. ngx_shmtx_unlock(&shpool->mutex);
  603. }
  604. /*
  605. removes the expired node from the upload rbtree
  606. */
  607. static void
  608. ngx_http_uploadprogress_cleanup(void *data)
  609. {
  610. ngx_http_uploadprogress_cleanup_t *upcln = data;
  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_log_debug0(NGX_LOG_DEBUG_HTTP, upcln->shm_zone->shm.log, 0,
  616. "uploadprogress cleanup called");
  617. ctx = upcln->shm_zone->data;
  618. shpool = (ngx_slab_pool_t *) upcln->shm_zone->shm.addr;
  619. node = upcln->node;
  620. up = (ngx_http_uploadprogress_node_t *) node;
  621. ngx_shmtx_lock(&shpool->mutex);
  622. up->done = 1; /* mark the original request as done */
  623. up->timeout = ngx_time() + upcln->timeout; /* keep tracking for 60s */
  624. ngx_shmtx_unlock(&shpool->mutex);
  625. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, upcln->shm_zone->shm.log, 0,
  626. "uploadprogress cleanup: connection %08XD to be deleted at %T",
  627. node->key, up->timeout);
  628. }
  629. static ngx_int_t
  630. ngx_http_uploadprogress_init_zone(ngx_shm_zone_t * shm_zone, void *data)
  631. {
  632. ngx_http_uploadprogress_ctx_t *octx = data;
  633. ngx_slab_pool_t *shpool;
  634. ngx_rbtree_node_t *sentinel;
  635. ngx_http_uploadprogress_ctx_t *ctx;
  636. ctx = shm_zone->data;
  637. if (octx) {
  638. ctx->rbtree = octx->rbtree;
  639. return NGX_OK;
  640. }
  641. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  642. ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t));
  643. if (ctx->rbtree == NULL) {
  644. return NGX_ERROR;
  645. }
  646. sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t));
  647. if (sentinel == NULL) {
  648. return NGX_ERROR;
  649. }
  650. ngx_rbtree_sentinel_init(sentinel);
  651. ctx->rbtree->root = sentinel;
  652. ctx->rbtree->sentinel = sentinel;
  653. ctx->rbtree->insert = ngx_http_uploadprogress_rbtree_insert_value;
  654. return NGX_OK;
  655. }
  656. static ngx_int_t
  657. ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
  658. {
  659. size_t n;
  660. ngx_str_t *id;
  661. ngx_slab_pool_t *shpool;
  662. ngx_rbtree_node_t *node;
  663. ngx_http_uploadprogress_ctx_t *ctx;
  664. ngx_http_uploadprogress_node_t *up;
  665. ngx_http_uploadprogress_conf_t *upcf;
  666. uint32_t hash;
  667. ngx_http_uploadprogress_cleanup_t *upcln;
  668. ngx_pool_cleanup_t *cln;
  669. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  670. "uploadprogress error-tracker error: %D", r->err_status);
  671. if (r->err_status >= NGX_HTTP_SPECIAL_RESPONSE) {
  672. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  673. if (!upcf->track) {
  674. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  675. "uploadprogress error-tracker not tracking in this location");
  676. goto finish;
  677. }
  678. id = get_tracking_id(r);
  679. if (id == NULL) {
  680. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  681. "trackuploads error-tracker no id found in POST upload req");
  682. goto finish;
  683. }
  684. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  685. "trackuploads error-tracker id found: %V", id);
  686. if (upcf->shm_zone == NULL) {
  687. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  688. "trackuploads no shm_zone for id: %V", id);
  689. goto finish;
  690. }
  691. ctx = upcf->shm_zone->data;
  692. hash = ngx_crc32_short(id->data, id->len);
  693. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  694. "trackuploads error-tracking hash %08XD for id: %V", hash,
  695. id);
  696. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  697. ngx_shmtx_lock(&shpool->mutex);
  698. if ((up = find_node(id, ctx, r->connection->log)) != NULL) {
  699. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  700. "trackuploads error-tracking found node for id: %V", id);
  701. up->err_status = r->err_status;
  702. ngx_shmtx_unlock(&shpool->mutex);
  703. goto finish;
  704. }
  705. /* no lz found for this tracking id */
  706. n = sizeof(ngx_http_uploadprogress_node_t) + id->len;
  707. cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
  708. if (cln == NULL) {
  709. ngx_shmtx_unlock(&shpool->mutex);
  710. goto finish;
  711. }
  712. node = ngx_slab_alloc_locked(shpool, n);
  713. if (node == NULL) {
  714. ngx_shmtx_unlock(&shpool->mutex);
  715. goto finish;
  716. }
  717. up = (ngx_http_uploadprogress_node_t *) node;
  718. node->key = hash;
  719. up->len = (u_char) id->len;
  720. up->err_status = r->err_status;
  721. ngx_memcpy(up->data, id->data, id->len);
  722. up->next = ctx->list_head.next;
  723. up->next->prev = up;
  724. up->prev = &ctx->list_head;
  725. ctx->list_head.next = up;
  726. ngx_rbtree_insert(ctx->rbtree, node);
  727. /* start the timer if needed */
  728. if (!upcf->cleanup.timer_set) {
  729. upcf->cleanup.data = upcf->shm_zone;
  730. upcf->cleanup.handler = ngx_clean_old_connections;
  731. upcf->cleanup.log = upcf->shm_zone->shm.log;
  732. ngx_add_timer(&upcf->cleanup, TIMER_FREQUENCY);
  733. }
  734. ngx_shmtx_unlock(&shpool->mutex);
  735. cln->handler = ngx_http_uploadprogress_cleanup;
  736. upcln = cln->data;
  737. upcln->shm_zone = upcf->shm_zone;
  738. upcln->node = node;
  739. upcln->timeout = upcf->timeout;
  740. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  741. "trackuploads error-tracking adding: %08XD", node->key);
  742. }
  743. finish:
  744. /* call the filter chain as usual */
  745. return ngx_http_next_header_filter(r);
  746. }
  747. static ngx_int_t
  748. ngx_http_uploadprogress_init(ngx_conf_t * cf)
  749. {
  750. ngx_http_handler_pt *h;
  751. ngx_http_core_main_conf_t *cmcf;
  752. cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
  753. /* install the tracking handler */
  754. h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
  755. if (h == NULL) {
  756. return NGX_ERROR;
  757. }
  758. *h = ngx_http_uploadprogress_handler;
  759. /*
  760. we also need to track HTTP errors
  761. unfortunately, the above handler is not called in case of
  762. errors.
  763. we have to register a header output filter that will be
  764. called in any case to track those errors
  765. */
  766. ngx_http_next_header_filter = ngx_http_top_header_filter;
  767. ngx_http_top_header_filter = ngx_http_uploadprogress_errortracker;
  768. return NGX_OK;
  769. }
  770. static void*
  771. ngx_http_uploadprogress_create_loc_conf(ngx_conf_t * cf)
  772. {
  773. ngx_http_uploadprogress_conf_t *conf;
  774. conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_conf_t));
  775. if (conf == NULL) {
  776. return NGX_CONF_ERROR;
  777. }
  778. return conf;
  779. }
  780. static char*
  781. ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t * cf, void *parent, void *child)
  782. {
  783. ngx_http_uploadprogress_conf_t *prev = parent;
  784. ngx_http_uploadprogress_conf_t *conf = child;
  785. if (conf->shm_zone == NULL) {
  786. *conf = *prev;
  787. }
  788. return NGX_CONF_OK;
  789. }
  790. static char*
  791. ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  792. {
  793. ssize_t n;
  794. ngx_str_t *value;
  795. ngx_shm_zone_t *shm_zone;
  796. ngx_http_uploadprogress_ctx_t *ctx;
  797. value = cf->args->elts;
  798. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  799. "ngx_upload_progress name: %V", &value[1]);
  800. ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_ctx_t));
  801. if (ctx == NULL) {
  802. return NGX_CONF_ERROR;
  803. }
  804. ctx->list_head.prev = NULL;
  805. ctx->list_head.next = &ctx->list_tail;
  806. ctx->list_tail.prev = &ctx->list_head;
  807. ctx->list_tail.next = NULL;
  808. n = ngx_parse_size(&value[2]);
  809. if (n == NGX_ERROR) {
  810. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  811. "invalid size of track_uploads \"%V\"", &value[2]);
  812. return NGX_CONF_ERROR;
  813. }
  814. if (n < (ngx_int_t) (8 * ngx_pagesize)) {
  815. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  816. "track_uploads \"%V\" is too small", &value[1]);
  817. return NGX_CONF_ERROR;
  818. }
  819. shm_zone = ngx_shared_memory_add(cf, &value[1], n,
  820. &ngx_http_uploadprogress_module);
  821. if (shm_zone == NULL) {
  822. return NGX_CONF_ERROR;
  823. }
  824. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  825. "ngx_upload_progress name: %V, szhm_zone: %p", &value[1],
  826. shm_zone);
  827. if (shm_zone->data) {
  828. ctx = shm_zone->data;
  829. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  830. "track_uploads \"%V\" is already created", &value[1]);
  831. return NGX_CONF_ERROR;
  832. }
  833. shm_zone->init = ngx_http_uploadprogress_init_zone;
  834. shm_zone->data = ctx;
  835. return NGX_CONF_OK;
  836. }
  837. static char*
  838. ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  839. {
  840. ngx_http_core_loc_conf_t *clcf;
  841. ngx_http_uploadprogress_conf_t *lzcf = conf;
  842. ngx_str_t *value;
  843. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_track_uploads in");
  844. value = cf->args->elts;
  845. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  846. "ngx_track_uploads name: %V", &value[1]);
  847. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  848. &ngx_http_uploadprogress_module);
  849. if (lzcf->shm_zone == NULL) {
  850. return NGX_CONF_ERROR;
  851. }
  852. lzcf->track = (u_char) 1;
  853. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  854. "ngx_track_uploads name: %V,szhm_zone: %p", &value[1],
  855. lzcf->shm_zone);
  856. lzcf->timeout = ngx_parse_time(&value[2], 1);
  857. if (lzcf->timeout == NGX_ERROR) {
  858. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  859. "track_uploads \"%V\" timeout value invalid", &value[1]);
  860. return NGX_CONF_ERROR;
  861. }
  862. if (lzcf->timeout == NGX_PARSE_LARGE_TIME) {
  863. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  864. "track_uploads \"%V\" timeout value must be less than 68 years", &value[1]);
  865. return NGX_CONF_ERROR;
  866. }
  867. clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  868. lzcf->handler = clcf->handler;
  869. if ( lzcf->handler == NULL )
  870. {
  871. return "track_upload should be the last directive in the location, after either proxy_pass or fastcgi_pass";
  872. }
  873. clcf->handler = ngx_http_uploadprogress_content_handler;
  874. return NGX_CONF_OK;
  875. }
  876. static char*
  877. ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  878. {
  879. ngx_http_uploadprogress_conf_t *lzcf = conf;
  880. ngx_http_core_loc_conf_t *clcf;
  881. ngx_str_t *value;
  882. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_report_uploads in");
  883. value = cf->args->elts;
  884. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  885. "ngx_report_uploads name: %V", &value[1]);
  886. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  887. &ngx_http_uploadprogress_module);
  888. if (lzcf->shm_zone == NULL) {
  889. return NGX_CONF_ERROR;
  890. }
  891. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  892. "ngx_report_uploads name: %V, szhm_zone: %p", &value[1],
  893. lzcf->shm_zone);
  894. lzcf->track = (u_char) 0;
  895. /* install our report handler */
  896. clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  897. clcf->handler = ngx_http_reportuploads_handler;
  898. return NGX_CONF_OK;
  899. }