Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ngx_http_uploadprogress_module.c 56KB

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