Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ngx_http_uploadprogress_module.c 55KB

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