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

ngx_http_uploadprogress_module.c 60KB

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