您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ngx_http_uploadprogress_module.c 46KB

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