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

ngx_http_uploadprogress_module.c 39KB

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