Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ngx_http_uploadprogress_module.c 55KB

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