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

ngx_http_uploadprogress_module.c 41KB

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