Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ngx_http_uploadprogress_module.c 70KB

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