選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ngx_http_uploadprogress_module.c 56KB

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