Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ngx_http_uploadprogress_module.c 56KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  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_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
  413. "upload-progress: read_event_handler no shm_zone for id: %V", id);
  414. ngx_http_uploadprogress_strdupfree(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;
  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;
  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->next = NULL;
  522. expires->hash = 1;
  523. expires->key.len = sizeof("Expires") - 1;
  524. expires->key.data = (u_char *) "Expires";
  525. }
  526. len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT");
  527. expires->value.len = len - 1;
  528. cc = r->headers_out.cache_control;
  529. if (cc == NULL) {
  530. cc = ngx_list_push(&r->headers_out.headers);
  531. if (cc == NULL) {
  532. expires->hash = 0;
  533. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  534. }
  535. r->headers_out.cache_control = cc;
  536. cc->next = NULL;
  537. cc->hash = 1;
  538. cc->key.len = sizeof("Cache-Control") - 1;
  539. cc->key.data = (u_char *) "Cache-Control";
  540. } else {
  541. for (cc = cc->next; cc; cc = cc->next) {
  542. cc->hash = 0;
  543. }
  544. cc = r->headers_out.cache_control;
  545. cc->next = NULL;
  546. }
  547. expires->value.data = (u_char *) "Thu, 01 Jan 1970 00:00:01 GMT";
  548. cc->value.len = sizeof("no-cache") - 1;
  549. cc->value.data = (u_char *) "no-cache";
  550. if (r->method == NGX_HTTP_HEAD) {
  551. r->headers_out.status = NGX_HTTP_OK;
  552. rc = ngx_http_send_header(r);
  553. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  554. return rc;
  555. }
  556. }
  557. ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
  558. /*
  559. There are 4 possibilities
  560. * request not yet started: found = false
  561. * request in error: err_status >= NGX_HTTP_BAD_REQUEST
  562. * request finished: done = true
  563. * request not yet started but registered: length==0 && rest ==0
  564. * reauest in progress: rest > 0
  565. */
  566. if (!found) {
  567. state = uploadprogress_state_starting;
  568. } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
  569. state = uploadprogress_state_error;
  570. } else if (done) {
  571. state = uploadprogress_state_done;
  572. } else if ( length == 0 && rest == 0 ) {
  573. state = uploadprogress_state_starting;
  574. } else {
  575. state = uploadprogress_state_uploading;
  576. }
  577. t = upcf->templates.elts;
  578. if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
  579. t[(ngx_uint_t)state].values->elts) == NULL)
  580. {
  581. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  582. }
  583. ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  584. "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
  585. state, err_status, (length - rest), length);
  586. b = ngx_calloc_buf(r->pool);
  587. if (b == NULL) {
  588. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  589. }
  590. b->pos = b->start = response.data;
  591. b->last = b->end = response.data + response.len;
  592. b->temporary = 1;
  593. b->memory = 1;
  594. out.buf = b;
  595. out.next = NULL;
  596. r->headers_out.status = NGX_HTTP_OK;
  597. r->headers_out.content_length_n = b->last - b->pos;
  598. b->last_buf = 1;
  599. rc = ngx_http_send_header(r);
  600. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  601. return rc;
  602. }
  603. return ngx_http_output_filter(r, &out);
  604. }
  605. /*
  606. Let's register the upload connection in our connections rb-tree
  607. */
  608. static ngx_int_t
  609. ngx_http_uploadprogress_handler(ngx_http_request_t * r)
  610. {
  611. size_t n;
  612. ngx_str_t *id;
  613. uint32_t hash;
  614. ngx_slab_pool_t *shpool;
  615. ngx_rbtree_node_t *node;
  616. ngx_http_uploadprogress_conf_t *upcf;
  617. ngx_http_uploadprogress_ctx_t *ctx;
  618. ngx_http_uploadprogress_node_t *up;
  619. ngx_http_uploadprogress_cleanup_t *upcln;
  620. ngx_pool_cleanup_t *cln;
  621. /* Is it a POST connection */
  622. if (r->method != NGX_HTTP_POST) {
  623. return NGX_DECLINED;
  624. }
  625. id = get_tracking_id(r);
  626. if (id == NULL) {
  627. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  628. "trackuploads no id found in POST upload req");
  629. return NGX_DECLINED;
  630. }
  631. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  632. "trackuploads id found: %V", id);
  633. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  634. if (!upcf->track) {
  635. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  636. "trackuploads not tracking in this location for id: %V", id);
  637. ngx_free(id);
  638. return NGX_DECLINED;
  639. }
  640. if (upcf->shm_zone == NULL) {
  641. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  642. "trackuploads no shm_zone for id: %V", id);
  643. ngx_free(id);
  644. return NGX_DECLINED;
  645. }
  646. ctx = upcf->shm_zone->data;
  647. hash = ngx_crc32_short(id->data, id->len);
  648. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  649. "trackuploads hash %08XD for id: %V", hash, id);
  650. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  651. ngx_shmtx_lock(&shpool->mutex);
  652. if (find_node(id, ctx, r->connection->log) != NULL) {
  653. ngx_shmtx_unlock(&shpool->mutex);
  654. /* already found a node with matching progress ID */
  655. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  656. "upload_progress: tracking already registered id: %V", id);
  657. ngx_free(id);
  658. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  659. }
  660. cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
  661. if (cln == NULL) {
  662. ngx_shmtx_unlock(&shpool->mutex);
  663. ngx_free(id);
  664. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  665. }
  666. n = sizeof(ngx_http_uploadprogress_node_t)
  667. + id->len;
  668. node = ngx_slab_alloc_locked(shpool, n);
  669. if (node == NULL) {
  670. ngx_shmtx_unlock(&shpool->mutex);
  671. ngx_free(id);
  672. return NGX_HTTP_SERVICE_UNAVAILABLE;
  673. }
  674. up = (ngx_http_uploadprogress_node_t *) node;
  675. node->key = hash;
  676. up->len = (u_char) id->len;
  677. up->err_status = r->err_status;
  678. up->done = 0;
  679. up->rest = 0;
  680. up->length = 0;
  681. up->timeout = 0;
  682. /* Properly handles small files where no read events happen after the */
  683. /* request is first handled (apparently this can happen on linux with epoll) */
  684. if (r->headers_in.content_length_n) {
  685. up->length = r->headers_in.content_length_n;
  686. if (r->request_body) {
  687. up->rest = r->request_body->rest;
  688. }
  689. }
  690. up->next = ctx->list_head.next;
  691. up->next->prev = up;
  692. up->prev = &ctx->list_head;
  693. ctx->list_head.next = up;
  694. ngx_memcpy(up->data, id->data, id->len);
  695. ngx_rbtree_insert(ctx->rbtree, node);
  696. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  697. "trackuploads: %08XD inserted in rbtree", node->key);
  698. if (!upcf->cleanup.timer_set) {
  699. upcf->cleanup.data = upcf->shm_zone;
  700. upcf->cleanup.handler = ngx_clean_old_connections;
  701. upcf->cleanup.log = upcf->shm_zone->shm.log;
  702. ngx_add_timer(&upcf->cleanup, TIMER_FREQUENCY);
  703. }
  704. ngx_shmtx_unlock(&shpool->mutex);
  705. cln->handler = ngx_http_uploadprogress_cleanup;
  706. upcln = cln->data;
  707. upcln->shm_zone = upcf->shm_zone;
  708. upcln->node = node;
  709. upcln->timeout = upcf->timeout;
  710. upcln->r = r;
  711. ngx_free(id);
  712. ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_uploadprogress_module_ctx_t));
  713. if (ctx == NULL) {
  714. return NGX_ERROR;
  715. }
  716. ngx_http_set_ctx(r, ctx, ngx_http_uploadprogress_module);
  717. /* finally says to the core we don't handle anything */
  718. return NGX_DECLINED;
  719. }
  720. static void
  721. ngx_http_uploadprogress_rbtree_insert_value(ngx_rbtree_node_t * temp,
  722. ngx_rbtree_node_t * node,
  723. ngx_rbtree_node_t * sentinel)
  724. {
  725. ngx_http_uploadprogress_node_t *upn, *upnt;
  726. for (;;) {
  727. if (node->key < temp->key) {
  728. if (temp->left == sentinel) {
  729. temp->left = node;
  730. break;
  731. }
  732. temp = temp->left;
  733. } else if (node->key > temp->key) {
  734. if (temp->right == sentinel) {
  735. temp->right = node;
  736. break;
  737. }
  738. temp = temp->right;
  739. } else { /* node->key == temp->key */
  740. upn = (ngx_http_uploadprogress_node_t *) node;
  741. upnt = (ngx_http_uploadprogress_node_t *) temp;
  742. if (ngx_memn2cmp(upn->data, upnt->data, upn->len, upnt->len) < 0) {
  743. if (temp->left == sentinel) {
  744. temp->left = node;
  745. break;
  746. }
  747. temp = temp->left;
  748. } else {
  749. if (temp->right == sentinel) {
  750. temp->right = node;
  751. break;
  752. }
  753. temp = temp->right;
  754. }
  755. }
  756. }
  757. node->parent = temp;
  758. node->left = sentinel;
  759. node->right = sentinel;
  760. ngx_rbt_red(node);
  761. }
  762. static void
  763. ngx_clean_old_connections(ngx_event_t * ev)
  764. {
  765. ngx_shm_zone_t *shm_zone;
  766. ngx_http_uploadprogress_ctx_t *ctx;
  767. ngx_slab_pool_t *shpool;
  768. ngx_rbtree_node_t *node;
  769. ngx_http_uploadprogress_node_t *up, *upprev;
  770. time_t now = ngx_time();
  771. int count = 0;
  772. /* scan the rbtree */
  773. shm_zone = ev->data;
  774. ctx = shm_zone->data;
  775. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  776. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  777. "uploadprogress clean old connections at %T", now);
  778. ngx_shmtx_lock(&shpool->mutex);
  779. node = (ngx_rbtree_node_t *) ctx->list_tail.prev;
  780. for (;;) {
  781. if (node == &ctx->list_head.node) {
  782. break;
  783. }
  784. up = (ngx_http_uploadprogress_node_t *) node;
  785. upprev = up->prev;
  786. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  787. "uploadprogress clean: scanning %08XD (req done %ui) timeout at %T",
  788. node->key, up->done, up->timeout);
  789. if ( (up->done && up->timeout < now) || (ngx_quit || ngx_terminate || ngx_exiting) ) {
  790. up->next->prev = up->prev;
  791. up->prev->next = up->next;
  792. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  793. "uploadprogress clean: removing %08XD (req %ui) ",
  794. node->key, up->done, up->timeout);
  795. ngx_rbtree_delete(ctx->rbtree, node);
  796. ngx_slab_free_locked(shpool, node);
  797. }
  798. else
  799. count++;
  800. node = (ngx_rbtree_node_t *) upprev;
  801. }
  802. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  803. "uploadprogress clean old connections: quit: %ui term: %ui count: %ui", ngx_quit, ngx_terminate, count);
  804. /* don't reschedule timer if ngx_quit or ngx_terminate && nodes emtpy */
  805. if ( count > 0 || !(ngx_quit || ngx_terminate || ngx_exiting)) {
  806. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  807. "uploadprogress clean old connections restarting timer");
  808. ngx_add_timer(ev, TIMER_FREQUENCY); /* trigger again in 60s */
  809. } else if (ngx_quit || ngx_terminate || ngx_exiting) {
  810. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  811. "uploadprogress clean old connections quitting , no more active connections: not restarting timer");
  812. }
  813. ngx_shmtx_unlock(&shpool->mutex);
  814. }
  815. /*
  816. removes the expired node from the upload rbtree
  817. */
  818. static void
  819. ngx_http_uploadprogress_cleanup(void *data)
  820. {
  821. ngx_http_uploadprogress_cleanup_t *upcln = data;
  822. ngx_slab_pool_t *shpool;
  823. ngx_rbtree_node_t *node;
  824. ngx_http_uploadprogress_node_t *up;
  825. ngx_http_request_t *r;
  826. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, upcln->shm_zone->shm.log, 0,
  827. "uploadprogress cleanup called");
  828. shpool = (ngx_slab_pool_t *) upcln->shm_zone->shm.addr;
  829. node = upcln->node;
  830. r = upcln->r;
  831. up = (ngx_http_uploadprogress_node_t *) node;
  832. ngx_shmtx_lock(&shpool->mutex);
  833. up->done = 1; /* mark the original request as done */
  834. up->timeout = ngx_time() + upcln->timeout; /* keep tracking for 60s */
  835. if (r != NULL ) {
  836. ngx_uint_t rc = r->err_status ? r->err_status : r->headers_out.status;
  837. if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
  838. up->err_status = rc;
  839. }
  840. }
  841. ngx_shmtx_unlock(&shpool->mutex);
  842. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, upcln->shm_zone->shm.log, 0,
  843. "uploadprogress cleanup: connection %08XD to be deleted at %T",
  844. node->key, up->timeout);
  845. }
  846. static ngx_int_t
  847. ngx_http_uploadprogress_init_zone(ngx_shm_zone_t * shm_zone, void *data)
  848. {
  849. ngx_http_uploadprogress_ctx_t *octx = data;
  850. ngx_slab_pool_t *shpool;
  851. ngx_rbtree_node_t *sentinel;
  852. ngx_http_uploadprogress_ctx_t *ctx;
  853. ctx = shm_zone->data;
  854. if (octx) {
  855. ctx->rbtree = octx->rbtree;
  856. return NGX_OK;
  857. }
  858. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  859. ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t));
  860. if (ctx->rbtree == NULL) {
  861. return NGX_ERROR;
  862. }
  863. sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t));
  864. if (sentinel == NULL) {
  865. return NGX_ERROR;
  866. }
  867. ngx_rbtree_init(ctx->rbtree, sentinel, ngx_http_uploadprogress_rbtree_insert_value);
  868. return NGX_OK;
  869. }
  870. static ngx_int_t
  871. ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
  872. {
  873. size_t n;
  874. ngx_str_t *id;
  875. ngx_slab_pool_t *shpool;
  876. ngx_rbtree_node_t *node;
  877. ngx_http_uploadprogress_ctx_t *ctx;
  878. ngx_http_uploadprogress_node_t *up;
  879. ngx_http_uploadprogress_conf_t *upcf;
  880. uint32_t hash;
  881. ngx_http_uploadprogress_cleanup_t *upcln;
  882. ngx_pool_cleanup_t *cln;
  883. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  884. "uploadprogress error-tracker error: %D", r->err_status);
  885. if (r->err_status >= NGX_HTTP_SPECIAL_RESPONSE) {
  886. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  887. if (!upcf->track) {
  888. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  889. "uploadprogress error-tracker not tracking in this location");
  890. goto finish;
  891. }
  892. id = get_tracking_id(r);
  893. if (id == NULL) {
  894. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  895. "trackuploads error-tracker no id found in POST upload req");
  896. goto finish;
  897. }
  898. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  899. "trackuploads error-tracker id found: %V", id);
  900. if (upcf->shm_zone == NULL) {
  901. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  902. "trackuploads no shm_zone for id: %V", id);
  903. ngx_free(id);
  904. goto finish;
  905. }
  906. ctx = upcf->shm_zone->data;
  907. hash = ngx_crc32_short(id->data, id->len);
  908. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  909. "trackuploads error-tracking hash %08XD for id: %V", hash,
  910. id);
  911. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  912. ngx_shmtx_lock(&shpool->mutex);
  913. if ((up = find_node(id, ctx, r->connection->log)) != NULL) {
  914. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  915. "trackuploads error-tracking found node for id: %V", id);
  916. up->err_status = r->err_status;
  917. ngx_shmtx_unlock(&shpool->mutex);
  918. ngx_free(id);
  919. goto finish;
  920. }
  921. /* no lz found for this tracking id */
  922. n = sizeof(ngx_http_uploadprogress_node_t) + id->len;
  923. cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
  924. if (cln == NULL) {
  925. ngx_shmtx_unlock(&shpool->mutex);
  926. ngx_free(id);
  927. goto finish;
  928. }
  929. node = ngx_slab_alloc_locked(shpool, n);
  930. if (node == NULL) {
  931. ngx_shmtx_unlock(&shpool->mutex);
  932. ngx_free(id);
  933. goto finish;
  934. }
  935. up = (ngx_http_uploadprogress_node_t *) node;
  936. node->key = hash;
  937. up->len = (u_char) id->len;
  938. up->err_status = r->err_status;
  939. up->done = 0;
  940. up->rest = 0;
  941. up->length = 0;
  942. up->timeout = 0;
  943. ngx_memcpy(up->data, id->data, id->len);
  944. up->next = ctx->list_head.next;
  945. up->next->prev = up;
  946. up->prev = &ctx->list_head;
  947. ctx->list_head.next = up;
  948. ngx_rbtree_insert(ctx->rbtree, node);
  949. /* start the timer if needed */
  950. if (!upcf->cleanup.timer_set) {
  951. upcf->cleanup.data = upcf->shm_zone;
  952. upcf->cleanup.handler = ngx_clean_old_connections;
  953. upcf->cleanup.log = upcf->shm_zone->shm.log;
  954. ngx_add_timer(&upcf->cleanup, TIMER_FREQUENCY);
  955. }
  956. ngx_shmtx_unlock(&shpool->mutex);
  957. cln->handler = ngx_http_uploadprogress_cleanup;
  958. upcln = cln->data;
  959. upcln->shm_zone = upcf->shm_zone;
  960. upcln->node = node;
  961. upcln->timeout = upcf->timeout;
  962. upcln->r = r;
  963. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  964. "trackuploads error-tracking adding: %08XD", node->key);
  965. ngx_free(id);
  966. }
  967. finish:
  968. /* call the filter chain as usual */
  969. return ngx_http_next_header_filter(r);
  970. }
  971. static ngx_int_t
  972. ngx_http_uploadprogress_init(ngx_conf_t * cf)
  973. {
  974. ngx_http_handler_pt *h;
  975. ngx_http_core_main_conf_t *cmcf;
  976. cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
  977. /* install the tracking handler */
  978. h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
  979. if (h == NULL) {
  980. return NGX_ERROR;
  981. }
  982. *h = ngx_http_uploadprogress_handler;
  983. /*
  984. we also need to track HTTP errors
  985. unfortunately, the above handler is not called in case of
  986. errors.
  987. we have to register a header output filter that will be
  988. called in any case to track those errors
  989. */
  990. ngx_http_next_header_filter = ngx_http_top_header_filter;
  991. ngx_http_top_header_filter = ngx_http_uploadprogress_errortracker;
  992. return NGX_OK;
  993. }
  994. static void*
  995. ngx_http_uploadprogress_create_loc_conf(ngx_conf_t * cf)
  996. {
  997. ngx_http_uploadprogress_conf_t *conf;
  998. ngx_uint_t i;
  999. conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_conf_t));
  1000. if (conf == NULL) {
  1001. return NGX_CONF_ERROR;
  1002. }
  1003. if(ngx_array_init(&conf->templates, cf->pool, 4, sizeof(ngx_http_uploadprogress_template_t)) != NGX_OK) {
  1004. return NGX_CONF_ERROR;
  1005. }
  1006. for(i = 0;i < conf->templates.nalloc; i++) {
  1007. ngx_http_uploadprogress_template_t *elt = ngx_array_push(&conf->templates);
  1008. if (elt == NULL) {
  1009. return NGX_CONF_ERROR;
  1010. }
  1011. elt->values = NULL;
  1012. elt->lengths = NULL;
  1013. }
  1014. return conf;
  1015. }
  1016. static char*
  1017. ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t * cf, void *parent, void *child)
  1018. {
  1019. ngx_http_uploadprogress_conf_t *prev = parent;
  1020. ngx_http_uploadprogress_conf_t *conf = child;
  1021. ngx_http_uploadprogress_template_t *t, *pt, *gt;
  1022. ngx_uint_t i;
  1023. if (conf->shm_zone == NULL) {
  1024. conf->shm_zone = prev->shm_zone;
  1025. conf->timeout = prev->timeout;
  1026. conf->cleanup = prev->cleanup;
  1027. conf->handler = prev->handler;
  1028. conf->track = prev->track;
  1029. }
  1030. ngx_conf_merge_str_value(conf->content_type, prev->content_type, "text/javascript");
  1031. t = conf->templates.elts;
  1032. pt = prev->templates.elts;
  1033. gt = ngx_http_uploadprogress_global_templates.elts;
  1034. for(i = 0;i < conf->templates.nelts; i++) {
  1035. if(t[i].values == NULL) {
  1036. if(pt[i].values == NULL && gt != NULL) {
  1037. t[i].values = gt[i].values;
  1038. t[i].lengths = gt[i].lengths;
  1039. }
  1040. else {
  1041. t[i].values = pt[i].values;
  1042. t[i].lengths = pt[i].lengths;
  1043. }
  1044. }
  1045. }
  1046. ngx_conf_merge_str_value(conf->header, prev->header, "X-Progress-ID");
  1047. ngx_conf_merge_str_value(conf->jsonp_parameter, prev->jsonp_parameter, "callback");
  1048. return NGX_CONF_OK;
  1049. }
  1050. static ngx_int_t
  1051. ngx_http_uploadprogress_init_variables_and_templates(ngx_conf_t *cf)
  1052. {
  1053. ngx_http_variable_t *var, *v;
  1054. ngx_http_uploadprogress_state_map_t *m;
  1055. ngx_uint_t i;
  1056. /* Add variables */
  1057. for (v = ngx_http_uploadprogress_variables; v->name.len; v++) {
  1058. var = ngx_http_add_variable(cf, &v->name, v->flags);
  1059. if (var == NULL) {
  1060. return NGX_ERROR;
  1061. }
  1062. var->get_handler = v->get_handler;
  1063. var->data = v->data;
  1064. }
  1065. /* Compile global templates (containing Javascript output) */
  1066. if(ngx_array_init(&ngx_http_uploadprogress_global_templates, cf->pool, 4,
  1067. sizeof(ngx_http_uploadprogress_template_t)) != NGX_OK) {
  1068. return NGX_ERROR;
  1069. }
  1070. m = ngx_http_uploadprogress_state_map;
  1071. i = 0;
  1072. while(m->name.data != NULL) {
  1073. ngx_http_uploadprogress_template_t *elt = ngx_array_push(&ngx_http_uploadprogress_global_templates);
  1074. ngx_http_script_variables_count(ngx_http_uploadprogress_jsonp_defaults + i);
  1075. if (ngx_http_upload_progress_set_template(cf, elt, ngx_http_uploadprogress_jsonp_defaults + i) != NGX_CONF_OK) {
  1076. return NGX_ERROR;
  1077. }
  1078. m++;
  1079. i++;
  1080. }
  1081. return NGX_OK;
  1082. }
  1083. static char*
  1084. ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1085. {
  1086. ssize_t n;
  1087. ngx_str_t *value;
  1088. ngx_shm_zone_t *shm_zone;
  1089. ngx_http_uploadprogress_ctx_t *ctx;
  1090. value = cf->args->elts;
  1091. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1092. "ngx_upload_progress name: %V", &value[1]);
  1093. ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_ctx_t));
  1094. if (ctx == NULL) {
  1095. return NGX_CONF_ERROR;
  1096. }
  1097. ctx->list_head.prev = NULL;
  1098. ctx->list_head.next = &ctx->list_tail;
  1099. ctx->list_tail.prev = &ctx->list_head;
  1100. ctx->list_tail.next = NULL;
  1101. n = ngx_parse_size(&value[2]);
  1102. if (n == NGX_ERROR) {
  1103. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1104. "invalid size of track_uploads \"%V\"", &value[2]);
  1105. return NGX_CONF_ERROR;
  1106. }
  1107. if (n < (ngx_int_t) (8 * ngx_pagesize)) {
  1108. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1109. "track_uploads \"%V\" is too small", &value[1]);
  1110. return NGX_CONF_ERROR;
  1111. }
  1112. shm_zone = ngx_shared_memory_add(cf, &value[1], n,
  1113. &ngx_http_uploadprogress_module);
  1114. if (shm_zone == NULL) {
  1115. return NGX_CONF_ERROR;
  1116. }
  1117. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1118. "ngx_upload_progress name: %V, szhm_zone: %p", &value[1],
  1119. shm_zone);
  1120. if (shm_zone->data) {
  1121. ctx = shm_zone->data;
  1122. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1123. "track_uploads \"%V\" is already created", &value[1]);
  1124. return NGX_CONF_ERROR;
  1125. }
  1126. shm_zone->init = ngx_http_uploadprogress_init_zone;
  1127. shm_zone->data = ctx;
  1128. return NGX_CONF_OK;
  1129. }
  1130. static char*
  1131. ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1132. {
  1133. ngx_http_core_loc_conf_t *clcf;
  1134. ngx_http_uploadprogress_conf_t *lzcf = conf;
  1135. ngx_str_t *value;
  1136. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_track_uploads in");
  1137. value = cf->args->elts;
  1138. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1139. "ngx_track_uploads name: %V", &value[1]);
  1140. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  1141. &ngx_http_uploadprogress_module);
  1142. if (lzcf->shm_zone == NULL) {
  1143. return NGX_CONF_ERROR;
  1144. }
  1145. lzcf->track = (u_char) 1;
  1146. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1147. "ngx_track_uploads name: %V,szhm_zone: %p", &value[1],
  1148. lzcf->shm_zone);
  1149. lzcf->timeout = ngx_parse_time(&value[2], 1);
  1150. if (lzcf->timeout == NGX_ERROR) {
  1151. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1152. "track_uploads \"%V\" timeout value invalid", &value[1]);
  1153. return NGX_CONF_ERROR;
  1154. }
  1155. clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  1156. lzcf->handler = clcf->handler;
  1157. if ( lzcf->handler == NULL )
  1158. {
  1159. return "track_upload should be the last directive in the location, after either proxy_pass or fastcgi_pass";
  1160. }
  1161. clcf->handler = ngx_http_uploadprogress_content_handler;
  1162. return NGX_CONF_OK;
  1163. }
  1164. static char*
  1165. ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1166. {
  1167. ngx_http_uploadprogress_conf_t *lzcf = conf;
  1168. ngx_http_core_loc_conf_t *clcf;
  1169. ngx_str_t *value;
  1170. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_report_uploads in");
  1171. value = cf->args->elts;
  1172. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1173. "ngx_report_uploads name: %V", &value[1]);
  1174. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  1175. &ngx_http_uploadprogress_module);
  1176. if (lzcf->shm_zone == NULL) {
  1177. return NGX_CONF_ERROR;
  1178. }
  1179. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1180. "ngx_report_uploads name: %V, szhm_zone: %p", &value[1],
  1181. lzcf->shm_zone);
  1182. lzcf->track = (u_char) 0;
  1183. /* install our report handler */
  1184. clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  1185. clcf->handler = ngx_http_reportuploads_handler;
  1186. return NGX_CONF_OK;
  1187. }
  1188. static char*
  1189. ngx_http_upload_progress_set_template(ngx_conf_t * cf, ngx_http_uploadprogress_template_t *t, ngx_str_t *source)
  1190. {
  1191. ssize_t n;
  1192. ngx_http_script_compile_t sc;
  1193. n = ngx_http_script_variables_count(source);
  1194. ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
  1195. t->lengths = NULL;
  1196. t->values = NULL;
  1197. sc.cf = cf;
  1198. sc.source = source;
  1199. sc.lengths = &t->lengths;
  1200. sc.values = &t->values;
  1201. sc.variables = n;
  1202. sc.complete_lengths = 1;
  1203. sc.complete_values = 1;
  1204. if (ngx_http_script_compile(&sc) != NGX_OK) {
  1205. return NGX_CONF_ERROR;
  1206. }
  1207. return NGX_CONF_OK;
  1208. }
  1209. static char*
  1210. ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1211. {
  1212. ngx_http_uploadprogress_conf_t *upcf = conf;
  1213. ngx_str_t *value;
  1214. ngx_http_uploadprogress_state_map_t *m = ngx_http_uploadprogress_state_map;
  1215. ngx_http_uploadprogress_template_t *t;
  1216. value = cf->args->elts;
  1217. while(m->name.data != NULL) {
  1218. if((value[1].len == m->name.len && !ngx_strncmp(value[1].data, m->name.data, m->name.len))
  1219. || (value[1].len == 2 && !ngx_strncmp(value[1].data, m->name.data, 2))) {
  1220. break;
  1221. }
  1222. m++;
  1223. }
  1224. if (m->name.data == NULL) {
  1225. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1226. "unknown state \"%V\"", &value[1]);
  1227. return NGX_CONF_ERROR;
  1228. }
  1229. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts + (ngx_uint_t)m->idx;
  1230. return ngx_http_upload_progress_set_template(cf, t, &value[2]);
  1231. }
  1232. static char*
  1233. ngx_http_upload_progress_java_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1234. {
  1235. ngx_http_uploadprogress_conf_t *upcf = conf;
  1236. ngx_http_uploadprogress_template_t *t;
  1237. ngx_uint_t i;
  1238. char* rc;
  1239. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
  1240. for(i = 0;i < upcf->templates.nelts;i++) {
  1241. rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_java_defaults + i);
  1242. if(rc != NGX_CONF_OK) {
  1243. return rc;
  1244. }
  1245. }
  1246. upcf->content_type.data = (u_char*)"text/javascript";
  1247. upcf->content_type.len = sizeof("text/javascript") - 1;
  1248. return NGX_CONF_OK;
  1249. }
  1250. static char*
  1251. ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1252. {
  1253. ngx_http_uploadprogress_conf_t *upcf = conf;
  1254. ngx_http_uploadprogress_template_t *t;
  1255. ngx_uint_t i;
  1256. char* rc;
  1257. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
  1258. for(i = 0;i < upcf->templates.nelts;i++) {
  1259. rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_json_defaults + i);
  1260. if(rc != NGX_CONF_OK) {
  1261. return rc;
  1262. }
  1263. }
  1264. upcf->content_type.data = (u_char*)"application/json";
  1265. upcf->content_type.len = sizeof("application/json") - 1;
  1266. return NGX_CONF_OK;
  1267. }
  1268. static char*
  1269. ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1270. {
  1271. ngx_http_uploadprogress_conf_t *upcf = conf;
  1272. ngx_http_uploadprogress_template_t *t;
  1273. ngx_uint_t i;
  1274. char* rc;
  1275. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
  1276. for(i = 0;i < upcf->templates.nelts;i++) {
  1277. rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_jsonp_defaults + i);
  1278. if(rc != NGX_CONF_OK) {
  1279. return rc;
  1280. }
  1281. }
  1282. upcf->content_type.data = (u_char*)"application/javascript";
  1283. upcf->content_type.len = sizeof("application/javascript") - 1;
  1284. return NGX_CONF_OK;
  1285. }
  1286. static ngx_int_t ngx_http_uploadprogress_received_variable(ngx_http_request_t *r,
  1287. ngx_http_variable_value_t *v, uintptr_t data)
  1288. {
  1289. ngx_http_uploadprogress_node_t *up;
  1290. u_char *p;
  1291. off_t *value;
  1292. up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
  1293. value = (off_t *) ((char *) up + data);
  1294. p = ngx_palloc(r->pool, NGX_OFF_T_LEN);
  1295. if (p == NULL) {
  1296. return NGX_ERROR;
  1297. }
  1298. v->len = ngx_sprintf(p, "%O", up->length - *value) - p;
  1299. v->valid = 1;
  1300. v->no_cacheable = 0;
  1301. v->not_found = 0;
  1302. v->data = p;
  1303. return NGX_OK;
  1304. }
  1305. static ngx_int_t ngx_http_uploadprogress_offset_variable(ngx_http_request_t *r,
  1306. ngx_http_variable_value_t *v, uintptr_t data)
  1307. {
  1308. ngx_http_uploadprogress_node_t *up;
  1309. u_char *p;
  1310. off_t *value;
  1311. up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
  1312. value = (off_t *) ((char *) up + data);
  1313. p = ngx_palloc(r->pool, NGX_OFF_T_LEN);
  1314. if (p == NULL) {
  1315. return NGX_ERROR;
  1316. }
  1317. v->len = ngx_sprintf(p, "%O", *value) - p;
  1318. v->valid = 1;
  1319. v->no_cacheable = 0;
  1320. v->not_found = 0;
  1321. v->data = p;
  1322. return NGX_OK;
  1323. }
  1324. static ngx_int_t
  1325. ngx_http_uploadprogress_status_variable(ngx_http_request_t *r,
  1326. ngx_http_variable_value_t *v, uintptr_t data)
  1327. {
  1328. ngx_http_uploadprogress_node_t *up;
  1329. u_char *p;
  1330. off_t *value;
  1331. up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
  1332. value = (off_t *) ((char *) up + data);
  1333. p = ngx_palloc(r->pool, NGX_OFF_T_LEN);
  1334. if (p == NULL) {
  1335. return NGX_ERROR;
  1336. }
  1337. v->len = ngx_sprintf(p, "%O", *value) - p;
  1338. v->valid = 1;
  1339. v->no_cacheable = 0;
  1340. v->not_found = 0;
  1341. v->data = p;
  1342. return NGX_OK;
  1343. }
  1344. static ngx_int_t
  1345. ngx_http_uploadprogress_callback_variable(ngx_http_request_t *r,
  1346. ngx_http_variable_value_t *v, uintptr_t data)
  1347. {
  1348. u_char *p, *start_p, *val, prefix[1024];
  1349. ngx_http_uploadprogress_conf_t *upcf;
  1350. u_int len;
  1351. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  1352. if (r->args.len) {
  1353. /* '=' has to be appended to avoid matching parameters that have the */
  1354. /* configured name as a prefix but are longer */
  1355. ngx_snprintf(prefix, sizeof(prefix) - 1, "%s=", upcf->jsonp_parameter.data);
  1356. len = upcf->jsonp_parameter.len + 1;
  1357. prefix[len] = '\0'; /* Force termination of string */
  1358. p = (u_char *) ngx_strstr(r->args.data, prefix);
  1359. if (p) {
  1360. p += len;
  1361. start_p = p;
  1362. while (p < r->args.data + r->args.len) {
  1363. if (*((p++) + 1) == '&') {
  1364. break;
  1365. }
  1366. }
  1367. v->len = p - start_p;
  1368. val = ngx_palloc(r->pool, v->len + 1);
  1369. if (val == NULL) {
  1370. return NGX_ERROR;
  1371. }
  1372. ngx_memcpy(val, start_p, v->len);
  1373. val[v->len] = '\0';
  1374. v->valid = 1;
  1375. v->no_cacheable = 0;
  1376. v->not_found = 0;
  1377. v->data = val;
  1378. } else {
  1379. return NGX_ERROR;
  1380. }
  1381. } else {
  1382. return NGX_ERROR;
  1383. }
  1384. return NGX_OK;
  1385. }