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

ngx_http_uploadprogress_module.c 44KB

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