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

ngx_http_uploadprogress_module.c 55KB

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