您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ngx_http_uploadprogress_module.c 57KB

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