Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ngx_http_uploadprogress_module.c 56KB

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