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

ngx_http_uploadprogress_module.c 66KB

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