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

ngx_http_uploadprogress_module.c 56KB

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