選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ngx_http_uploadprogress_module.c 36KB

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