選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ngx_http_uploadprogress_module.c 44KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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. if(up->length == 0)
  347. up->length = r->headers_in.content_length_n;
  348. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
  349. "upload-progress: read_event_handler storing rest %uO/%uO for %V", up->rest, up->length, id);
  350. } else {
  351. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
  352. "upload-progress: read_event_handler not found: %V", id);
  353. }
  354. ngx_shmtx_unlock(&shpool->mutex);
  355. ngx_http_uploadprogress_strdupfree(id);
  356. }
  357. /* This generates the response for the report */
  358. static ngx_int_t
  359. ngx_http_reportuploads_handler(ngx_http_request_t * r)
  360. {
  361. ngx_str_t *id, response;
  362. ngx_buf_t *b;
  363. ngx_chain_t out;
  364. ngx_int_t rc, found=0, done=0, err_status=0;
  365. off_t rest=0, length=0;
  366. ngx_uint_t len, i;
  367. ngx_slab_pool_t *shpool;
  368. ngx_http_uploadprogress_conf_t *upcf;
  369. ngx_http_uploadprogress_ctx_t *ctx;
  370. ngx_http_uploadprogress_node_t *up;
  371. ngx_table_elt_t *expires, *cc, **ccp;
  372. ngx_http_uploadprogress_state_t state;
  373. ngx_http_uploadprogress_template_t *t;
  374. if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
  375. return NGX_HTTP_NOT_ALLOWED;
  376. }
  377. rc = ngx_http_discard_request_body(r);
  378. if (rc != NGX_OK) {
  379. return rc;
  380. }
  381. /* get the tracking id if any */
  382. id = get_tracking_id(r);
  383. if (id == NULL) {
  384. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  385. "reportuploads handler cant find id");
  386. return NGX_DECLINED;
  387. }
  388. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  389. "reportuploads handler found id: %V", id);
  390. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  391. if (upcf->shm_zone == NULL) {
  392. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  393. "reportuploads no shm_zone for id: %V", id);
  394. ngx_free(id);
  395. return NGX_DECLINED;
  396. }
  397. ctx = upcf->shm_zone->data;
  398. /* get the original connection of the upload */
  399. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  400. ngx_shmtx_lock(&shpool->mutex);
  401. up = find_node(id, ctx, r->connection->log);
  402. if (up != NULL) {
  403. ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  404. "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", id, up->rest, up->length, up->done, up->err_status);
  405. rest = up->rest;
  406. length = up->length;
  407. done = up->done;
  408. err_status = up->err_status;
  409. found = 1;
  410. } else {
  411. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  412. "reportuploads not found: %V", id);
  413. }
  414. ngx_shmtx_unlock(&shpool->mutex);
  415. ngx_free(id);
  416. /* send the output */
  417. r->headers_out.content_type = upcf->content_type;
  418. /* force no-cache */
  419. expires = r->headers_out.expires;
  420. if (expires == NULL) {
  421. expires = ngx_list_push(&r->headers_out.headers);
  422. if (expires == NULL) {
  423. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  424. }
  425. r->headers_out.expires = expires;
  426. expires->hash = 1;
  427. expires->key.len = sizeof("Expires") - 1;
  428. expires->key.data = (u_char *) "Expires";
  429. }
  430. len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT");
  431. expires->value.len = len - 1;
  432. ccp = r->headers_out.cache_control.elts;
  433. if (ccp == NULL) {
  434. if (ngx_array_init(&r->headers_out.cache_control, r->pool,
  435. 1, sizeof(ngx_table_elt_t *))
  436. != NGX_OK) {
  437. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  438. }
  439. ccp = ngx_array_push(&r->headers_out.cache_control);
  440. if (ccp == NULL) {
  441. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  442. }
  443. cc = ngx_list_push(&r->headers_out.headers);
  444. if (cc == NULL) {
  445. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  446. }
  447. cc->hash = 1;
  448. cc->key.len = sizeof("Cache-Control") - 1;
  449. cc->key.data = (u_char *) "Cache-Control";
  450. *ccp = cc;
  451. } else {
  452. for (i = 1; i < r->headers_out.cache_control.nelts; i++) {
  453. ccp[i]->hash = 0;
  454. }
  455. cc = ccp[0];
  456. }
  457. expires->value.data = (u_char *) "Thu, 01 Jan 1970 00:00:01 GMT";
  458. cc->value.len = sizeof("no-cache") - 1;
  459. cc->value.data = (u_char *) "no-cache";
  460. if (r->method == NGX_HTTP_HEAD) {
  461. r->headers_out.status = NGX_HTTP_OK;
  462. rc = ngx_http_send_header(r);
  463. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  464. return rc;
  465. }
  466. }
  467. /*
  468. There are 4 possibilities
  469. * request not yet started: found = false
  470. * request in error: err_status >= NGX_HTTP_SPECIAL_RESPONSE
  471. * request finished: done = true
  472. * request not yet started but registered: length==0 && rest ==0
  473. * reauest in progress: rest > 0
  474. */
  475. if (!found) {
  476. state = uploadprogress_state_starting;
  477. } else if (err_status >= NGX_HTTP_SPECIAL_RESPONSE) {
  478. state = uploadprogress_state_error;
  479. } else if (done) {
  480. state = uploadprogress_state_done;
  481. } else if ( length == 0 && rest == 0 ) {
  482. state = uploadprogress_state_starting;
  483. } else {
  484. state = uploadprogress_state_uploading;
  485. }
  486. t = upcf->templates.elts;
  487. if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
  488. t[(ngx_uint_t)state].values->elts) == NULL)
  489. {
  490. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  491. }
  492. ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  493. "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
  494. state, err_status, (length - rest), length);
  495. b = ngx_calloc_buf(r->pool);
  496. if (b == NULL) {
  497. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  498. }
  499. b->pos = b->start = response.data;
  500. b->last = b->end = response.data + response.len;
  501. b->temporary = 1;
  502. b->memory = 1;
  503. out.buf = b;
  504. out.next = NULL;
  505. r->headers_out.status = NGX_HTTP_OK;
  506. r->headers_out.content_length_n = b->last - b->pos;
  507. b->last_buf = 1;
  508. rc = ngx_http_send_header(r);
  509. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  510. return rc;
  511. }
  512. return ngx_http_output_filter(r, &out);
  513. }
  514. /*
  515. Let's register the upload connection in our connections rb-tree
  516. */
  517. static ngx_int_t
  518. ngx_http_uploadprogress_handler(ngx_http_request_t * r)
  519. {
  520. size_t n;
  521. ngx_str_t *id;
  522. uint32_t hash;
  523. ngx_slab_pool_t *shpool;
  524. ngx_rbtree_node_t *node;
  525. ngx_http_uploadprogress_conf_t *upcf;
  526. ngx_http_uploadprogress_ctx_t *ctx;
  527. ngx_http_uploadprogress_node_t *up;
  528. ngx_http_uploadprogress_cleanup_t *upcln;
  529. ngx_pool_cleanup_t *cln;
  530. /* Is it a POST connection */
  531. if (r->method != NGX_HTTP_POST) {
  532. return NGX_DECLINED;
  533. }
  534. id = get_tracking_id(r);
  535. if (id == NULL) {
  536. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  537. "trackuploads no id found in POST upload req");
  538. return NGX_DECLINED;
  539. }
  540. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  541. "trackuploads id found: %V", id);
  542. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  543. if (!upcf->track) {
  544. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  545. "trackuploads not tracking in this location for id: %V", id);
  546. ngx_free(id);
  547. return NGX_DECLINED;
  548. }
  549. if (upcf->shm_zone == NULL) {
  550. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  551. "trackuploads no shm_zone for id: %V", id);
  552. ngx_free(id);
  553. return NGX_DECLINED;
  554. }
  555. ctx = upcf->shm_zone->data;
  556. hash = ngx_crc32_short(id->data, id->len);
  557. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  558. "trackuploads hash %08XD for id: %V", hash, id);
  559. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  560. ngx_shmtx_lock(&shpool->mutex);
  561. if (find_node(id, ctx, r->connection->log) != NULL) {
  562. ngx_shmtx_unlock(&shpool->mutex);
  563. /* already found a node with matching progress ID */
  564. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  565. "upload_progress: tracking already registered id: %V", id);
  566. ngx_free(id);
  567. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  568. }
  569. cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
  570. if (cln == NULL) {
  571. ngx_shmtx_unlock(&shpool->mutex);
  572. ngx_free(id);
  573. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  574. }
  575. n = sizeof(ngx_http_uploadprogress_node_t)
  576. + id->len;
  577. node = ngx_slab_alloc_locked(shpool, n);
  578. if (node == NULL) {
  579. ngx_shmtx_unlock(&shpool->mutex);
  580. ngx_free(id);
  581. return NGX_HTTP_SERVICE_UNAVAILABLE;
  582. }
  583. up = (ngx_http_uploadprogress_node_t *) node;
  584. node->key = hash;
  585. up->len = (u_char) id->len;
  586. up->err_status = r->err_status;
  587. up->done = 0;
  588. up->rest = 0;
  589. up->length = 0;
  590. up->timeout = 0;
  591. up->next = ctx->list_head.next;
  592. up->next->prev = up;
  593. up->prev = &ctx->list_head;
  594. ctx->list_head.next = up;
  595. ngx_memcpy(up->data, id->data, id->len);
  596. ngx_rbtree_insert(ctx->rbtree, node);
  597. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  598. "trackuploads: %08XD inserted in rbtree", node->key);
  599. if (!upcf->cleanup.timer_set) {
  600. upcf->cleanup.data = upcf->shm_zone;
  601. upcf->cleanup.handler = ngx_clean_old_connections;
  602. upcf->cleanup.log = upcf->shm_zone->shm.log;
  603. ngx_add_timer(&upcf->cleanup, TIMER_FREQUENCY);
  604. }
  605. ngx_shmtx_unlock(&shpool->mutex);
  606. cln->handler = ngx_http_uploadprogress_cleanup;
  607. upcln = cln->data;
  608. upcln->shm_zone = upcf->shm_zone;
  609. upcln->node = node;
  610. upcln->timeout = upcf->timeout;
  611. upcln->r = r;
  612. ngx_free(id);
  613. ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_uploadprogress_module_ctx_t));
  614. if (ctx == NULL) {
  615. return NGX_ERROR;
  616. }
  617. ngx_http_set_ctx(r, ctx, ngx_http_uploadprogress_module);
  618. /* finally says to the core we don't handle anything */
  619. return NGX_DECLINED;
  620. }
  621. static void
  622. ngx_http_uploadprogress_rbtree_insert_value(ngx_rbtree_node_t * temp,
  623. ngx_rbtree_node_t * node,
  624. ngx_rbtree_node_t * sentinel)
  625. {
  626. ngx_http_uploadprogress_node_t *upn, *upnt;
  627. for (;;) {
  628. if (node->key < temp->key) {
  629. if (temp->left == sentinel) {
  630. temp->left = node;
  631. break;
  632. }
  633. temp = temp->left;
  634. } else if (node->key > temp->key) {
  635. if (temp->right == sentinel) {
  636. temp->right = node;
  637. break;
  638. }
  639. temp = temp->right;
  640. } else { /* node->key == temp->key */
  641. upn = (ngx_http_uploadprogress_node_t *) node;
  642. upnt = (ngx_http_uploadprogress_node_t *) temp;
  643. if (ngx_memn2cmp(upn->data, upnt->data, upn->len, upnt->len) < 0) {
  644. if (temp->left == sentinel) {
  645. temp->left = node;
  646. break;
  647. }
  648. temp = temp->left;
  649. } else {
  650. if (temp->right == sentinel) {
  651. temp->right = node;
  652. break;
  653. }
  654. temp = temp->right;
  655. }
  656. }
  657. }
  658. node->parent = temp;
  659. node->left = sentinel;
  660. node->right = sentinel;
  661. ngx_rbt_red(node);
  662. }
  663. static void
  664. ngx_clean_old_connections(ngx_event_t * ev)
  665. {
  666. ngx_shm_zone_t *shm_zone;
  667. ngx_http_uploadprogress_ctx_t *ctx;
  668. ngx_slab_pool_t *shpool;
  669. ngx_rbtree_node_t *node;
  670. ngx_http_uploadprogress_node_t *up, *upprev;
  671. time_t now = ngx_time();
  672. int count = 0;
  673. /* scan the rbtree */
  674. shm_zone = ev->data;
  675. ctx = shm_zone->data;
  676. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  677. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  678. "uploadprogress clean old connections at %T", now);
  679. ngx_shmtx_lock(&shpool->mutex);
  680. node = (ngx_rbtree_node_t *) ctx->list_tail.prev;
  681. for (;;) {
  682. if (node == &ctx->list_head.node) {
  683. break;
  684. }
  685. up = (ngx_http_uploadprogress_node_t *) node;
  686. upprev = up->prev;
  687. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  688. "uploadprogress clean: scanning %08XD (req done %ui) timeout at %T",
  689. node->key, up->done, up->timeout);
  690. if ( (up->done && up->timeout < now) || (ngx_quit || ngx_terminate || ngx_exiting) ) {
  691. up->next->prev = up->prev;
  692. up->prev->next = up->next;
  693. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  694. "uploadprogress clean: removing %08XD (req %ui) ",
  695. node->key, up->done, up->timeout);
  696. ngx_rbtree_delete(ctx->rbtree, node);
  697. ngx_slab_free_locked(shpool, node);
  698. }
  699. else
  700. count++;
  701. node = (ngx_rbtree_node_t *) upprev;
  702. }
  703. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  704. "uploadprogress clean old connections: quit: %ui term: %ui count: %ui", ngx_quit, ngx_terminate, count);
  705. /* don't reschedule timer if ngx_quit or ngx_terminate && nodes emtpy */
  706. if ( count > 0 || !(ngx_quit || ngx_terminate || ngx_exiting)) {
  707. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  708. "uploadprogress clean old connections restarting timer");
  709. ngx_add_timer(ev, TIMER_FREQUENCY); /* trigger again in 60s */
  710. } else if (ngx_quit || ngx_terminate || ngx_exiting) {
  711. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  712. "uploadprogress clean old connections quitting , no more active connections: not restarting timer");
  713. }
  714. ngx_shmtx_unlock(&shpool->mutex);
  715. }
  716. /*
  717. removes the expired node from the upload rbtree
  718. */
  719. static void
  720. ngx_http_uploadprogress_cleanup(void *data)
  721. {
  722. ngx_http_uploadprogress_cleanup_t *upcln = data;
  723. ngx_slab_pool_t *shpool;
  724. ngx_rbtree_node_t *node;
  725. ngx_http_uploadprogress_ctx_t *ctx;
  726. ngx_http_uploadprogress_node_t *up;
  727. ngx_http_request_t *r;
  728. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, upcln->shm_zone->shm.log, 0,
  729. "uploadprogress cleanup called");
  730. ctx = upcln->shm_zone->data;
  731. shpool = (ngx_slab_pool_t *) upcln->shm_zone->shm.addr;
  732. node = upcln->node;
  733. r = upcln->r;
  734. up = (ngx_http_uploadprogress_node_t *) node;
  735. ngx_shmtx_lock(&shpool->mutex);
  736. up->done = 1; /* mark the original request as done */
  737. up->timeout = ngx_time() + upcln->timeout; /* keep tracking for 60s */
  738. if (r != NULL ) {
  739. ngx_uint_t rc = r->err_status ? r->err_status : r->headers_out.status;
  740. if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
  741. up->err_status = rc;
  742. }
  743. }
  744. ngx_shmtx_unlock(&shpool->mutex);
  745. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, upcln->shm_zone->shm.log, 0,
  746. "uploadprogress cleanup: connection %08XD to be deleted at %T",
  747. node->key, up->timeout);
  748. }
  749. static ngx_int_t
  750. ngx_http_uploadprogress_init_zone(ngx_shm_zone_t * shm_zone, void *data)
  751. {
  752. ngx_http_uploadprogress_ctx_t *octx = data;
  753. ngx_slab_pool_t *shpool;
  754. ngx_rbtree_node_t *sentinel;
  755. ngx_http_uploadprogress_ctx_t *ctx;
  756. ctx = shm_zone->data;
  757. if (octx) {
  758. ctx->rbtree = octx->rbtree;
  759. return NGX_OK;
  760. }
  761. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  762. ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t));
  763. if (ctx->rbtree == NULL) {
  764. return NGX_ERROR;
  765. }
  766. sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t));
  767. if (sentinel == NULL) {
  768. return NGX_ERROR;
  769. }
  770. ngx_rbtree_sentinel_init(sentinel);
  771. ctx->rbtree->root = sentinel;
  772. ctx->rbtree->sentinel = sentinel;
  773. ctx->rbtree->insert = ngx_http_uploadprogress_rbtree_insert_value;
  774. return NGX_OK;
  775. }
  776. static ngx_int_t
  777. ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
  778. {
  779. size_t n;
  780. ngx_str_t *id;
  781. ngx_slab_pool_t *shpool;
  782. ngx_rbtree_node_t *node;
  783. ngx_http_uploadprogress_ctx_t *ctx;
  784. ngx_http_uploadprogress_node_t *up;
  785. ngx_http_uploadprogress_conf_t *upcf;
  786. uint32_t hash;
  787. ngx_http_uploadprogress_cleanup_t *upcln;
  788. ngx_pool_cleanup_t *cln;
  789. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  790. "uploadprogress error-tracker error: %D", r->err_status);
  791. if (r->err_status >= NGX_HTTP_SPECIAL_RESPONSE) {
  792. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  793. if (!upcf->track) {
  794. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  795. "uploadprogress error-tracker not tracking in this location");
  796. goto finish;
  797. }
  798. id = get_tracking_id(r);
  799. if (id == NULL) {
  800. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  801. "trackuploads error-tracker no id found in POST upload req");
  802. goto finish;
  803. }
  804. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  805. "trackuploads error-tracker id found: %V", id);
  806. if (upcf->shm_zone == NULL) {
  807. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  808. "trackuploads no shm_zone for id: %V", id);
  809. ngx_free(id);
  810. goto finish;
  811. }
  812. ctx = upcf->shm_zone->data;
  813. hash = ngx_crc32_short(id->data, id->len);
  814. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  815. "trackuploads error-tracking hash %08XD for id: %V", hash,
  816. id);
  817. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  818. ngx_shmtx_lock(&shpool->mutex);
  819. if ((up = find_node(id, ctx, r->connection->log)) != NULL) {
  820. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  821. "trackuploads error-tracking found node for id: %V", id);
  822. up->err_status = r->err_status;
  823. ngx_shmtx_unlock(&shpool->mutex);
  824. ngx_free(id);
  825. goto finish;
  826. }
  827. /* no lz found for this tracking id */
  828. n = sizeof(ngx_http_uploadprogress_node_t) + id->len;
  829. cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
  830. if (cln == NULL) {
  831. ngx_shmtx_unlock(&shpool->mutex);
  832. ngx_free(id);
  833. goto finish;
  834. }
  835. node = ngx_slab_alloc_locked(shpool, n);
  836. if (node == NULL) {
  837. ngx_shmtx_unlock(&shpool->mutex);
  838. ngx_free(id);
  839. goto finish;
  840. }
  841. up = (ngx_http_uploadprogress_node_t *) node;
  842. node->key = hash;
  843. up->len = (u_char) id->len;
  844. up->err_status = r->err_status;
  845. ngx_memcpy(up->data, id->data, id->len);
  846. up->next = ctx->list_head.next;
  847. up->next->prev = up;
  848. up->prev = &ctx->list_head;
  849. ctx->list_head.next = up;
  850. ngx_rbtree_insert(ctx->rbtree, node);
  851. /* start the timer if needed */
  852. if (!upcf->cleanup.timer_set) {
  853. upcf->cleanup.data = upcf->shm_zone;
  854. upcf->cleanup.handler = ngx_clean_old_connections;
  855. upcf->cleanup.log = upcf->shm_zone->shm.log;
  856. ngx_add_timer(&upcf->cleanup, TIMER_FREQUENCY);
  857. }
  858. ngx_shmtx_unlock(&shpool->mutex);
  859. cln->handler = ngx_http_uploadprogress_cleanup;
  860. upcln = cln->data;
  861. upcln->shm_zone = upcf->shm_zone;
  862. upcln->node = node;
  863. upcln->timeout = upcf->timeout;
  864. upcln->r = r;
  865. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  866. "trackuploads error-tracking adding: %08XD", node->key);
  867. ngx_free(id);
  868. }
  869. finish:
  870. /* call the filter chain as usual */
  871. return ngx_http_next_header_filter(r);
  872. }
  873. static ngx_int_t
  874. ngx_http_uploadprogress_init(ngx_conf_t * cf)
  875. {
  876. ngx_http_handler_pt *h;
  877. ngx_http_core_main_conf_t *cmcf;
  878. ngx_http_uploadprogress_template_t *t;
  879. ngx_http_uploadprogress_state_map_t *m;
  880. ngx_http_script_compile_t sc;
  881. ssize_t n;
  882. cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
  883. /* install the tracking handler */
  884. h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
  885. if (h == NULL) {
  886. return NGX_ERROR;
  887. }
  888. *h = ngx_http_uploadprogress_handler;
  889. /*
  890. we also need to track HTTP errors
  891. unfortunately, the above handler is not called in case of
  892. errors.
  893. we have to register a header output filter that will be
  894. called in any case to track those errors
  895. */
  896. ngx_http_next_header_filter = ngx_http_top_header_filter;
  897. ngx_http_top_header_filter = ngx_http_uploadprogress_errortracker;
  898. /*
  899. * Compile global templates
  900. */
  901. if(ngx_array_init(&ngx_http_uploadprogress_global_templates, cf->pool, 4,
  902. sizeof(ngx_http_uploadprogress_template_t)) != NGX_OK) {
  903. return NGX_ERROR;
  904. }
  905. m = ngx_http_uploadprogress_state_map;
  906. t = ngx_http_uploadprogress_global_templates.elts;
  907. while(m->name.data != NULL) {
  908. n = ngx_http_script_variables_count(&m->def);
  909. ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
  910. sc.cf = cf;
  911. sc.source = &m->def;
  912. sc.lengths = &t->lengths;
  913. sc.values = &t->values;
  914. sc.variables = n;
  915. sc.complete_lengths = 1;
  916. sc.complete_values = 1;
  917. if (ngx_http_script_compile(&sc) != NGX_OK) {
  918. return NGX_ERROR;
  919. }
  920. m++;
  921. }
  922. return NGX_OK;
  923. }
  924. static void*
  925. ngx_http_uploadprogress_create_loc_conf(ngx_conf_t * cf)
  926. {
  927. ngx_http_uploadprogress_conf_t *conf;
  928. ngx_http_uploadprogress_template_t *t;
  929. ngx_uint_t i;
  930. conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_conf_t));
  931. if (conf == NULL) {
  932. return NGX_CONF_ERROR;
  933. }
  934. if(ngx_array_init(&conf->templates, cf->pool, 4, sizeof(ngx_http_uploadprogress_template_t)) != NGX_OK) {
  935. return NGX_CONF_ERROR;
  936. }
  937. t = conf->templates.elts;
  938. for(i = 0;i < conf->templates.nelts; i++) {
  939. t[i].values = NULL;
  940. t[i].lengths = NULL;
  941. }
  942. return conf;
  943. }
  944. static char*
  945. ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t * cf, void *parent, void *child)
  946. {
  947. ngx_http_uploadprogress_conf_t *prev = parent;
  948. ngx_http_uploadprogress_conf_t *conf = child;
  949. ngx_http_uploadprogress_template_t *t, *pt, *gt;
  950. ngx_uint_t i;
  951. if (conf->shm_zone == NULL) {
  952. *conf = *prev;
  953. }
  954. ngx_conf_merge_str_value(conf->content_type, prev->content_type, "text/javascript");
  955. t = conf->templates.elts;
  956. pt = prev->templates.elts;
  957. gt = ngx_http_uploadprogress_global_templates.elts;
  958. for(i = 0;i < conf->templates.nelts; i++) {
  959. if(t[i].values == NULL) {
  960. if(pt[i].values == NULL) {
  961. t[i].values = gt[i].values;
  962. t[i].lengths = gt[i].lengths;
  963. }
  964. else{
  965. t[i].values = pt[i].values;
  966. t[i].lengths = pt[i].lengths;
  967. }
  968. }
  969. }
  970. return NGX_CONF_OK;
  971. }
  972. static char*
  973. ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  974. {
  975. ssize_t n;
  976. ngx_str_t *value;
  977. ngx_shm_zone_t *shm_zone;
  978. ngx_http_uploadprogress_ctx_t *ctx;
  979. value = cf->args->elts;
  980. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  981. "ngx_upload_progress name: %V", &value[1]);
  982. ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_ctx_t));
  983. if (ctx == NULL) {
  984. return NGX_CONF_ERROR;
  985. }
  986. ctx->list_head.prev = NULL;
  987. ctx->list_head.next = &ctx->list_tail;
  988. ctx->list_tail.prev = &ctx->list_head;
  989. ctx->list_tail.next = NULL;
  990. n = ngx_parse_size(&value[2]);
  991. if (n == NGX_ERROR) {
  992. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  993. "invalid size of track_uploads \"%V\"", &value[2]);
  994. return NGX_CONF_ERROR;
  995. }
  996. if (n < (ngx_int_t) (8 * ngx_pagesize)) {
  997. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  998. "track_uploads \"%V\" is too small", &value[1]);
  999. return NGX_CONF_ERROR;
  1000. }
  1001. shm_zone = ngx_shared_memory_add(cf, &value[1], n,
  1002. &ngx_http_uploadprogress_module);
  1003. if (shm_zone == NULL) {
  1004. return NGX_CONF_ERROR;
  1005. }
  1006. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1007. "ngx_upload_progress name: %V, szhm_zone: %p", &value[1],
  1008. shm_zone);
  1009. if (shm_zone->data) {
  1010. ctx = shm_zone->data;
  1011. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1012. "track_uploads \"%V\" is already created", &value[1]);
  1013. return NGX_CONF_ERROR;
  1014. }
  1015. shm_zone->init = ngx_http_uploadprogress_init_zone;
  1016. shm_zone->data = ctx;
  1017. return NGX_CONF_OK;
  1018. }
  1019. static char*
  1020. ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1021. {
  1022. ngx_http_core_loc_conf_t *clcf;
  1023. ngx_http_uploadprogress_conf_t *lzcf = conf;
  1024. ngx_str_t *value;
  1025. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_track_uploads in");
  1026. value = cf->args->elts;
  1027. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1028. "ngx_track_uploads name: %V", &value[1]);
  1029. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  1030. &ngx_http_uploadprogress_module);
  1031. if (lzcf->shm_zone == NULL) {
  1032. return NGX_CONF_ERROR;
  1033. }
  1034. lzcf->track = (u_char) 1;
  1035. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1036. "ngx_track_uploads name: %V,szhm_zone: %p", &value[1],
  1037. lzcf->shm_zone);
  1038. lzcf->timeout = ngx_parse_time(&value[2], 1);
  1039. if (lzcf->timeout == NGX_ERROR) {
  1040. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1041. "track_uploads \"%V\" timeout value invalid", &value[1]);
  1042. return NGX_CONF_ERROR;
  1043. }
  1044. if (lzcf->timeout == NGX_PARSE_LARGE_TIME) {
  1045. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1046. "track_uploads \"%V\" timeout value must be less than 68 years", &value[1]);
  1047. return NGX_CONF_ERROR;
  1048. }
  1049. clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  1050. lzcf->handler = clcf->handler;
  1051. if ( lzcf->handler == NULL )
  1052. {
  1053. return "track_upload should be the last directive in the location, after either proxy_pass or fastcgi_pass";
  1054. }
  1055. clcf->handler = ngx_http_uploadprogress_content_handler;
  1056. return NGX_CONF_OK;
  1057. }
  1058. static char*
  1059. ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1060. {
  1061. ngx_http_uploadprogress_conf_t *lzcf = conf;
  1062. ngx_http_core_loc_conf_t *clcf;
  1063. ngx_str_t *value;
  1064. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_report_uploads in");
  1065. value = cf->args->elts;
  1066. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1067. "ngx_report_uploads name: %V", &value[1]);
  1068. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  1069. &ngx_http_uploadprogress_module);
  1070. if (lzcf->shm_zone == NULL) {
  1071. return NGX_CONF_ERROR;
  1072. }
  1073. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1074. "ngx_report_uploads name: %V, szhm_zone: %p", &value[1],
  1075. lzcf->shm_zone);
  1076. lzcf->track = (u_char) 0;
  1077. /* install our report handler */
  1078. clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  1079. clcf->handler = ngx_http_reportuploads_handler;
  1080. return NGX_CONF_OK;
  1081. }
  1082. static char*
  1083. ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1084. {
  1085. ngx_http_uploadprogress_conf_t *upcf = conf;
  1086. ssize_t n;
  1087. ngx_str_t *value;
  1088. ngx_http_uploadprogress_state_map_t *m;
  1089. ngx_http_uploadprogress_template_t *t;
  1090. ngx_http_script_compile_t sc;
  1091. value = cf->args->elts;
  1092. m = ngx_http_uploadprogress_state_map;
  1093. while(m->name.data != NULL) {
  1094. if((value[1].len == m->name.len && !ngx_strncmp(value[1].data, m->name.data, m->name.len))
  1095. || (value[1].len == 2 && !ngx_strncmp(value[1].data, m->name.data, 2))) {
  1096. break;
  1097. }
  1098. m++;
  1099. }
  1100. if (m->name.data == NULL) {
  1101. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1102. "unknown state \"%V\"", &value[1]);
  1103. return NGX_CONF_ERROR;
  1104. }
  1105. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts + (ngx_uint_t)m->idx;
  1106. n = ngx_http_script_variables_count(&value[2]);
  1107. ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
  1108. sc.cf = cf;
  1109. sc.source = &value[2];
  1110. sc.lengths = &t->lengths;
  1111. sc.values = &t->values;
  1112. sc.variables = n;
  1113. sc.complete_lengths = 1;
  1114. sc.complete_values = 1;
  1115. if (ngx_http_script_compile(&sc) != NGX_OK) {
  1116. return NGX_CONF_ERROR;
  1117. }
  1118. return NGX_CONF_OK;
  1119. }