Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ngx_http_uploadprogress_module.c 41KB

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