Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ngx_http_uploadprogress_module.c 56KB

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