Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ngx_http_uploadprogress_module.c 47KB

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