Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ngx_http_uploadprogress_module.c 69KB

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