You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ngx_http_uploadprogress_module.c 60KB

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