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

ngx_http_uploadprogress_module.c 52KB

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