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 55KB

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