Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ngx_http_uploadprogress_module.c 51KB

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