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

ngx_http_uploadprogress_module.c 73KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  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. ngx_uint_t sequence;
  28. ngx_uint_t sent_portion;
  29. time_t timeout;
  30. struct ngx_http_uploadprogress_node_s *prev;
  31. struct ngx_http_uploadprogress_node_s *next;
  32. u_char len;
  33. u_char data[1];
  34. };
  35. typedef struct {
  36. ngx_shm_zone_t *shm_zone;
  37. ngx_rbtree_node_t *node;
  38. ngx_http_request_t *r;
  39. time_t timeout;
  40. } ngx_http_uploadprogress_cleanup_t;
  41. typedef struct {
  42. ngx_rbtree_t *rbtree;
  43. ngx_http_uploadprogress_node_t list_head;
  44. ngx_http_uploadprogress_node_t list_tail;
  45. } ngx_http_uploadprogress_ctx_t;
  46. typedef struct {
  47. ngx_array_t *values;
  48. ngx_array_t *lengths;
  49. } ngx_http_uploadprogress_template_t;
  50. typedef struct {
  51. ngx_shm_zone_t *shm_zone;
  52. time_t timeout;
  53. ngx_event_t cleanup;
  54. ngx_http_handler_pt handler;
  55. u_char track;
  56. ngx_str_t content_type;
  57. ngx_array_t templates;
  58. ngx_str_t header;
  59. ngx_str_t header_mul;
  60. ngx_addr_t progress_server;
  61. int udp_socket;
  62. ngx_str_t jsonp_parameter;
  63. ngx_int_t json_multiple:1;
  64. } ngx_http_uploadprogress_conf_t;
  65. typedef struct {
  66. ngx_http_event_handler_pt read_event_handler;
  67. } ngx_http_uploadprogress_module_ctx_t;
  68. static ngx_int_t ngx_http_reportuploads_handler(ngx_http_request_t *r);
  69. static void ngx_http_uploadprogress_cleanup(void *data);
  70. static char *ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  71. static ngx_int_t ngx_http_uploadprogress_init_zone(ngx_shm_zone_t * shm_zone, void *data);
  72. static ngx_int_t ngx_http_uploadprogress_init(ngx_conf_t * cf);
  73. static void *ngx_http_uploadprogress_create_loc_conf(ngx_conf_t *cf);
  74. static char *ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
  75. static ngx_int_t ngx_http_uploadprogress_init_variables_and_templates(ngx_conf_t *cf);
  76. static ngx_int_t ngx_http_uploadprogress_received_variable(ngx_http_request_t *r,
  77. ngx_http_variable_value_t *v, uintptr_t data);
  78. static ngx_int_t ngx_http_uploadprogress_offset_variable(ngx_http_request_t *r,
  79. ngx_http_variable_value_t *v, uintptr_t data);
  80. static ngx_int_t ngx_http_uploadprogress_status_variable(ngx_http_request_t *r,
  81. ngx_http_variable_value_t *v, uintptr_t data);
  82. static ngx_int_t ngx_http_uploadprogress_id_variable(ngx_http_request_t *r,
  83. ngx_http_variable_value_t *v, uintptr_t data);
  84. static ngx_int_t ngx_http_uploadprogress_callback_variable(ngx_http_request_t *r,
  85. ngx_http_variable_value_t *v, uintptr_t data);
  86. static char* ngx_http_upload_progress_set_template(ngx_conf_t * cf, ngx_http_uploadprogress_template_t *t, ngx_str_t *source);
  87. static char *ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  88. static char *ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  89. static char *ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  90. static char* ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
  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 | NGX_CONF_TAKE3,
  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(ngx_http_request_t * r)
  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. ngx_http_uploadprogress_conf_t *upcf;
  272. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  273. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: get_tracking_id");
  274. part = &r->headers_in.headers.part;
  275. header = part->elts;
  276. for (i = 0; /* void */ ; i++) {
  277. if (i >= part->nelts) {
  278. if (part->next == NULL) {
  279. break;
  280. }
  281. part = part->next;
  282. header = part->elts;
  283. i = 0;
  284. }
  285. if (header[i].key.len == upcf->header.len
  286. && ngx_strncasecmp(header[i].key.data, upcf->header.data,
  287. header[i].key.len) == 0) {
  288. ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log );
  289. ret->data = header[i].value.data;
  290. ret->len = header[i].value.len;
  291. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  292. "upload-progress: get_tracking_id found header: %V", ret);
  293. return ret;
  294. }
  295. }
  296. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  297. "upload-progress: get_tracking_id no header found");
  298. /* not found, check as a request arg */
  299. /* it is possible the request args have not been yet created (or already released) */
  300. /* so let's try harder first from the request line */
  301. args.len = r->args.len;
  302. args.data = r->args.data;
  303. if (args.len && args.data) {
  304. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  305. "upload-progress: get_tracking_id no header found, args found");
  306. i = 0;
  307. p = args.data;
  308. do {
  309. ngx_uint_t len = args.len - (p - args.data);
  310. if (len >= (upcf->header.len + 1) && ngx_strncasecmp(p, upcf->header.data, upcf->header.len) == 0
  311. && p[upcf->header.len] == '=') {
  312. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  313. "upload-progress: get_tracking_id found args: %s",p);
  314. i = 1;
  315. break;
  316. }
  317. else if (!len)
  318. break;
  319. }
  320. while(p++);
  321. if (i) {
  322. start_p = p += upcf->header.len + 1;
  323. while (p < args.data + args.len) {
  324. if (*((p++) + 1) == '&') {
  325. break;
  326. }
  327. }
  328. ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log);
  329. ret->data = start_p;
  330. ret->len = p - start_p;
  331. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  332. "upload-progress: get_tracking_id found args: %V",ret);
  333. return ret;
  334. }
  335. }
  336. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  337. "upload-progress: get_tracking_id no id found");
  338. return NULL;
  339. }
  340. static ngx_str_t*
  341. get_tracking_ids_mul(ngx_http_request_t * r)
  342. {
  343. u_char *p, *start_p;
  344. ngx_uint_t i;
  345. ngx_list_part_t *part;
  346. ngx_table_elt_t *header;
  347. ngx_str_t *ret, args;
  348. ngx_http_uploadprogress_conf_t *upcf;
  349. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  350. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: get_tracking_ids");
  351. part = &r->headers_in.headers.part;
  352. header = part->elts;
  353. for (i = 0; /* void */ ; i++) {
  354. if (i >= part->nelts) {
  355. if (part->next == NULL) {
  356. break;
  357. }
  358. part = part->next;
  359. header = part->elts;
  360. i = 0;
  361. }
  362. if (header[i].key.len == upcf->header_mul.len
  363. && ngx_strncasecmp(header[i].key.data, upcf->header_mul.data,
  364. header[i].key.len) == 0) {
  365. ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log );
  366. ret->data = header[i].value.data;
  367. ret->len = header[i].value.len;
  368. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  369. "upload-progress: get_tracking_ids found header: %V", ret);
  370. return ret;
  371. }
  372. }
  373. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  374. "upload-progress: get_tracking_ids no header found");
  375. /* not found, check as a request arg */
  376. /* it is possible the request args have not been yet created (or already released) */
  377. /* so let's try harder first from the request line */
  378. args.len = r->args.len;
  379. args.data = r->args.data;
  380. if (args.len && args.data) {
  381. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  382. "upload-progress: get_tracking_id no header found, args found");
  383. i = 0;
  384. p = args.data;
  385. do {
  386. ngx_uint_t len = args.len - (p - args.data);
  387. if (len >= (upcf->header_mul.len + 1) && ngx_strncasecmp(p, upcf->header_mul.data, upcf->header_mul.len) == 0
  388. && p[upcf->header_mul.len] == '=') {
  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++) + 1) == '&') {
  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. ngx_memcpy(((char*)dst + sizeof(ngx_str_t)) , src->data, src->len);
  484. dst->data = ((u_char*)dst + sizeof(ngx_str_t));
  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. if(upcf->udp_socket != -1 && upcf->progress_server.socklen != 0)
  564. {
  565. u_char datagram_buf[1024];
  566. u_char * end;
  567. off_t uploaded;
  568. ngx_uint_t portion;
  569. uploaded = up->length - up->rest;
  570. if(up->length)
  571. portion = 100 * uploaded / up->length;
  572. else
  573. portion = 100;
  574. if(portion > up->sent_portion)
  575. {
  576. end = ngx_snprintf(datagram_buf, sizeof(datagram_buf), "{\"id\" : \"%V\", \"sequence\" : %d, \"size\" : %uO, \"uploaded\" : %uO }",
  577. id, up->sequence, up->length, uploaded);
  578. sendto(upcf->udp_socket, datagram_buf, end - datagram_buf, 0, (struct sockaddr*)upcf->progress_server.sockaddr, upcf->progress_server.socklen);
  579. up->sent_portion = portion;
  580. ++up->sequence;
  581. }
  582. }
  583. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
  584. "upload-progress: read_event_handler storing rest %uO/%uO for %V", up->rest, up->length, id);
  585. } else {
  586. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
  587. "upload-progress: read_event_handler not found: %V", id);
  588. }
  589. ngx_shmtx_unlock(&shpool->mutex);
  590. ngx_http_uploadprogress_strdupfree(id);
  591. }
  592. /* This generates the response for the report */
  593. static ngx_int_t
  594. ngx_http_reportuploads_handler(ngx_http_request_t * r)
  595. {
  596. ngx_str_t *id, response;
  597. ngx_buf_t *b;
  598. ngx_chain_t out;
  599. ngx_int_t rc, found=0, done=0, err_status=0;
  600. off_t rest=0, length=0;
  601. ngx_uint_t len, i;
  602. ngx_slab_pool_t *shpool;
  603. ngx_http_uploadprogress_conf_t *upcf;
  604. ngx_http_uploadprogress_ctx_t *ctx;
  605. ngx_http_uploadprogress_node_t *up;
  606. ngx_table_elt_t *expires, *cc, **ccp;
  607. ngx_http_uploadprogress_state_t state;
  608. ngx_http_uploadprogress_template_t *t;
  609. if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
  610. return NGX_HTTP_NOT_ALLOWED;
  611. }
  612. rc = ngx_http_discard_request_body(r);
  613. if (rc != NGX_OK) {
  614. return rc;
  615. }
  616. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  617. /* get the tracking id if any */
  618. if(upcf->json_multiple)
  619. id = get_tracking_ids_mul(r);
  620. else
  621. id = get_tracking_id(r);
  622. if (id == NULL) {
  623. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  624. "reportuploads handler cant find id");
  625. return NGX_DECLINED;
  626. }
  627. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  628. "reportuploads handler found id: %V", id);
  629. if (upcf->shm_zone == NULL) {
  630. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  631. "reportuploads no shm_zone for id: %V", id);
  632. ngx_free(id);
  633. return NGX_DECLINED;
  634. }
  635. /* force no-cache */
  636. expires = r->headers_out.expires;
  637. if (expires == NULL) {
  638. expires = ngx_list_push(&r->headers_out.headers);
  639. if (expires == NULL) {
  640. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  641. }
  642. r->headers_out.expires = expires;
  643. expires->hash = 1;
  644. expires->key.len = sizeof("Expires") - 1;
  645. expires->key.data = (u_char *) "Expires";
  646. }
  647. len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT");
  648. expires->value.len = len - 1;
  649. ccp = r->headers_out.cache_control.elts;
  650. if (ccp == NULL) {
  651. if (ngx_array_init(&r->headers_out.cache_control, r->pool,
  652. 1, sizeof(ngx_table_elt_t *))
  653. != NGX_OK) {
  654. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  655. }
  656. ccp = ngx_array_push(&r->headers_out.cache_control);
  657. if (ccp == NULL) {
  658. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  659. }
  660. cc = ngx_list_push(&r->headers_out.headers);
  661. if (cc == NULL) {
  662. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  663. }
  664. cc->hash = 1;
  665. cc->key.len = sizeof("Cache-Control") - 1;
  666. cc->key.data = (u_char *) "Cache-Control";
  667. *ccp = cc;
  668. } else {
  669. for (i = 1; i < r->headers_out.cache_control.nelts; i++) {
  670. ccp[i]->hash = 0;
  671. }
  672. cc = ccp[0];
  673. }
  674. expires->value.data = (u_char *) "Thu, 01 Jan 1970 00:00:01 GMT";
  675. cc->value.len = sizeof("no-cache") - 1;
  676. cc->value.data = (u_char *) "no-cache";
  677. if (r->method == NGX_HTTP_HEAD) {
  678. r->headers_out.status = NGX_HTTP_OK;
  679. rc = ngx_http_send_header(r);
  680. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  681. return rc;
  682. }
  683. }
  684. ctx = upcf->shm_zone->data;
  685. /* get the original connection of the upload */
  686. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  687. if(upcf->json_multiple)
  688. {
  689. ngx_chain_t * p_chain_end = 0;
  690. ngx_chain_t * p_chain_start = 0;
  691. size_t offs = 0;
  692. u_char * p1 = id->data, * p2;
  693. r->headers_out.content_length_n = 0;
  694. while(offs < id->len)
  695. {
  696. p2 = memchr((char *)id->data + offs, ';', id->len - offs);
  697. if(!p2) p2 = id->data + id->len;
  698. size_t len = p2 - p1;
  699. if(len)
  700. {
  701. ngx_str_t sub_id;
  702. sub_id.data = p1;
  703. sub_id.len = len;
  704. // ---->
  705. ngx_shmtx_lock(&shpool->mutex);
  706. up = find_node(&sub_id, ctx, r->connection->log);
  707. if (up != NULL) {
  708. ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  709. "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", &sub_id, up->rest, up->length, up->done, up->err_status);
  710. rest = up->rest;
  711. length = up->length;
  712. done = up->done;
  713. err_status = up->err_status;
  714. found = 1;
  715. } else {
  716. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  717. "reportuploads not found: %V", &sub_id);
  718. }
  719. ngx_shmtx_unlock(&shpool->mutex);
  720. /* send the output */
  721. r->headers_out.content_type = upcf->content_type;
  722. if(up == NULL)
  723. {
  724. // For current id
  725. ngx_http_uploadprogress_node_t * tmp_node = ngx_pcalloc(r->pool, sizeof(ngx_http_uploadprogress_node_t) + sub_id.len);
  726. tmp_node->len = sub_id.len;
  727. ngx_memcpy(tmp_node->data, sub_id.data, sub_id.len);
  728. ngx_http_set_ctx(r, tmp_node, ngx_http_uploadprogress_module);
  729. }
  730. else
  731. ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
  732. if (!found) {
  733. state = uploadprogress_state_starting;
  734. } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
  735. state = uploadprogress_state_error;
  736. } else if (done) {
  737. state = uploadprogress_state_done;
  738. } else if ( length == 0 && rest == 0 ) {
  739. state = uploadprogress_state_starting;
  740. } else {
  741. state = uploadprogress_state_uploading;
  742. }
  743. t = upcf->templates.elts;
  744. if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
  745. t[(ngx_uint_t)state].values->elts) == NULL)
  746. {
  747. ngx_free(id);
  748. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  749. }
  750. ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  751. "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
  752. state, err_status, (length - rest), length);
  753. if(p_chain_end)
  754. {
  755. p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
  756. if (p_chain_end->next == NULL) {
  757. ngx_free(id);
  758. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  759. }
  760. p_chain_end = p_chain_end->next;
  761. // Insert comma
  762. b = ngx_calloc_buf(r->pool);
  763. if (b == NULL) {
  764. ngx_free(id);
  765. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  766. }
  767. b->pos = b->start = ngx_palloc(r->pool, 2);
  768. if (b->pos == NULL) {
  769. ngx_free(id);
  770. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  771. }
  772. b->last = b->end = b->pos + 2;
  773. ngx_memcpy(b->pos, ", ", 2);
  774. b->temporary = 1;
  775. b->memory = 1;
  776. p_chain_end->buf = b;
  777. p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
  778. if (p_chain_end->next == NULL) {
  779. ngx_free(id);
  780. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  781. }
  782. p_chain_end = p_chain_end->next;
  783. }
  784. else
  785. {
  786. p_chain_start = p_chain_end = ngx_palloc(r->pool, sizeof(ngx_chain_t));
  787. }
  788. b = ngx_calloc_buf(r->pool);
  789. if (b == NULL) {
  790. ngx_free(id);
  791. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  792. }
  793. b->pos = b->start = response.data;
  794. b->last = b->end = response.data + response.len;
  795. b->temporary = 1;
  796. b->memory = 1;
  797. p_chain_end->buf = b;
  798. p_chain_end->next = NULL;
  799. // ---->
  800. r->headers_out.content_length_n += b->last - b->pos;
  801. p1 = p2 + 1;
  802. }
  803. offs += len + 1;
  804. }
  805. ngx_free(id);
  806. if(!p_chain_end) // Malformed id
  807. {
  808. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  809. "reportuploads malformed multiple id");
  810. return NGX_DECLINED;
  811. }
  812. // Prepend brace
  813. b = ngx_calloc_buf(r->pool);
  814. if (b == NULL) {
  815. ngx_free(id);
  816. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  817. }
  818. b->pos = b->start = ngx_palloc(r->pool, 2);
  819. if (b->pos == NULL) {
  820. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  821. }
  822. b->last = b->end = b->pos + 2;
  823. ngx_memcpy(b->pos, "[ ", 2);
  824. b->temporary = 1;
  825. b->memory = 1;
  826. r->headers_out.content_length_n += 2;
  827. out.buf = b;
  828. out.next = p_chain_start;
  829. // Append brace
  830. p_chain_end->next = ngx_palloc(r->pool, sizeof(ngx_chain_t));
  831. if (p_chain_end->next == NULL) {
  832. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  833. }
  834. p_chain_end = p_chain_end->next;
  835. b = ngx_calloc_buf(r->pool);
  836. if (b == NULL) {
  837. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  838. }
  839. b->pos = b->start = ngx_palloc(r->pool, 2);
  840. if (b->pos == NULL) {
  841. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  842. }
  843. b->last = b->end = b->pos + 4;
  844. ngx_memcpy(b->pos, " ]\r\n", 4);
  845. b->temporary = 1;
  846. b->memory = 1;
  847. r->headers_out.content_length_n += 4;
  848. p_chain_end->buf = b;
  849. p_chain_end->next = NULL;
  850. r->headers_out.status = NGX_HTTP_OK;
  851. p_chain_end->buf->last_buf = 1;
  852. }
  853. else
  854. {
  855. ngx_shmtx_lock(&shpool->mutex);
  856. up = find_node(id, ctx, r->connection->log);
  857. if (up != NULL) {
  858. ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  859. "reportuploads found node: %V (rest: %uO, length: %uO, done: %ui, err_status: %ui)", id, up->rest, up->length, up->done, up->err_status);
  860. rest = up->rest;
  861. length = up->length;
  862. done = up->done;
  863. err_status = up->err_status;
  864. found = 1;
  865. } else {
  866. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  867. "reportuploads not found: %V", id);
  868. }
  869. ngx_shmtx_unlock(&shpool->mutex);
  870. ngx_free(id);
  871. /* send the output */
  872. r->headers_out.content_type = upcf->content_type;
  873. ngx_http_set_ctx(r, up, ngx_http_uploadprogress_module);
  874. /*
  875. There are 4 possibilities
  876. * request not yet started: found = false
  877. * request in error: err_status >= NGX_HTTP_BAD_REQUEST
  878. * request finished: done = true
  879. * request not yet started but registered: length==0 && rest ==0
  880. * reauest in progress: rest > 0
  881. */
  882. if (!found) {
  883. state = uploadprogress_state_starting;
  884. } else if (err_status >= NGX_HTTP_BAD_REQUEST) {
  885. state = uploadprogress_state_error;
  886. } else if (done) {
  887. state = uploadprogress_state_done;
  888. } else if ( length == 0 && rest == 0 ) {
  889. state = uploadprogress_state_starting;
  890. } else {
  891. state = uploadprogress_state_uploading;
  892. }
  893. t = upcf->templates.elts;
  894. if (ngx_http_script_run(r, &response, t[(ngx_uint_t)state].lengths->elts, 0,
  895. t[(ngx_uint_t)state].values->elts) == NULL)
  896. {
  897. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  898. }
  899. ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  900. "upload progress: state=%d, err_status=%ui, remaining=%uO, length=%uO",
  901. state, err_status, (length - rest), length);
  902. b = ngx_calloc_buf(r->pool);
  903. if (b == NULL) {
  904. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  905. }
  906. b->pos = b->start = response.data;
  907. b->last = b->end = response.data + response.len;
  908. b->temporary = 1;
  909. b->memory = 1;
  910. out.buf = b;
  911. out.next = NULL;
  912. r->headers_out.status = NGX_HTTP_OK;
  913. r->headers_out.content_length_n = b->last - b->pos;
  914. b->last_buf = 1;
  915. }
  916. rc = ngx_http_send_header(r);
  917. if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  918. return rc;
  919. }
  920. return ngx_http_output_filter(r, &out);
  921. }
  922. /*
  923. Let's register the upload connection in our connections rb-tree
  924. */
  925. static ngx_int_t
  926. ngx_http_uploadprogress_handler(ngx_http_request_t * r)
  927. {
  928. size_t n;
  929. ngx_str_t *id;
  930. uint32_t hash;
  931. ngx_slab_pool_t *shpool;
  932. ngx_rbtree_node_t *node;
  933. ngx_http_uploadprogress_conf_t *upcf;
  934. ngx_http_uploadprogress_ctx_t *ctx;
  935. ngx_http_uploadprogress_node_t *up;
  936. ngx_http_uploadprogress_cleanup_t *upcln;
  937. ngx_pool_cleanup_t *cln;
  938. /* Is it a POST connection */
  939. if (r->method != NGX_HTTP_POST) {
  940. return NGX_DECLINED;
  941. }
  942. id = get_tracking_id(r);
  943. if (id == NULL) {
  944. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  945. "trackuploads no id found in POST upload req");
  946. return NGX_DECLINED;
  947. }
  948. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  949. "trackuploads id found: %V", id);
  950. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  951. if (!upcf->track) {
  952. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  953. "trackuploads not tracking in this location for id: %V", id);
  954. ngx_free(id);
  955. return NGX_DECLINED;
  956. }
  957. if (upcf->shm_zone == NULL) {
  958. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  959. "trackuploads no shm_zone for id: %V", id);
  960. ngx_free(id);
  961. return NGX_DECLINED;
  962. }
  963. ctx = upcf->shm_zone->data;
  964. hash = ngx_crc32_short(id->data, id->len);
  965. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  966. "trackuploads hash %08XD for id: %V", hash, id);
  967. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  968. ngx_shmtx_lock(&shpool->mutex);
  969. if (find_node(id, ctx, r->connection->log) != NULL) {
  970. ngx_shmtx_unlock(&shpool->mutex);
  971. /* already found a node with matching progress ID */
  972. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  973. "upload_progress: tracking already registered id: %V", id);
  974. ngx_free(id);
  975. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  976. }
  977. cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
  978. if (cln == NULL) {
  979. ngx_shmtx_unlock(&shpool->mutex);
  980. ngx_free(id);
  981. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  982. }
  983. n = sizeof(ngx_http_uploadprogress_node_t)
  984. + id->len;
  985. node = ngx_slab_alloc_locked(shpool, n);
  986. if (node == NULL) {
  987. ngx_shmtx_unlock(&shpool->mutex);
  988. ngx_free(id);
  989. return NGX_HTTP_SERVICE_UNAVAILABLE;
  990. }
  991. up = (ngx_http_uploadprogress_node_t *) node;
  992. node->key = hash;
  993. up->len = (u_char) id->len;
  994. up->err_status = r->err_status;
  995. up->done = 0;
  996. up->rest = 0;
  997. up->length = 0;
  998. up->timeout = 0;
  999. up->sequence = 0;
  1000. up->sent_portion = 0;
  1001. /* Properly handles small files where no read events happen after the */
  1002. /* request is first handled (apparently this can happen on linux with epoll) */
  1003. if (r->headers_in.content_length_n) {
  1004. up->length = r->headers_in.content_length_n;
  1005. if (r->request_body) {
  1006. up->rest = r->request_body->rest;
  1007. }
  1008. }
  1009. up->next = ctx->list_head.next;
  1010. up->next->prev = up;
  1011. up->prev = &ctx->list_head;
  1012. ctx->list_head.next = up;
  1013. ngx_memcpy(up->data, id->data, id->len);
  1014. ngx_rbtree_insert(ctx->rbtree, node);
  1015. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1016. "trackuploads: %08XD inserted in rbtree", node->key);
  1017. if (!upcf->cleanup.timer_set) {
  1018. upcf->cleanup.data = upcf->shm_zone;
  1019. upcf->cleanup.handler = ngx_clean_old_connections;
  1020. upcf->cleanup.log = upcf->shm_zone->shm.log;
  1021. ngx_add_timer(&upcf->cleanup, TIMER_FREQUENCY);
  1022. }
  1023. ngx_shmtx_unlock(&shpool->mutex);
  1024. cln->handler = ngx_http_uploadprogress_cleanup;
  1025. upcln = cln->data;
  1026. upcln->shm_zone = upcf->shm_zone;
  1027. upcln->node = node;
  1028. upcln->timeout = upcf->timeout;
  1029. upcln->r = r;
  1030. ngx_free(id);
  1031. ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_uploadprogress_module_ctx_t));
  1032. if (ctx == NULL) {
  1033. return NGX_ERROR;
  1034. }
  1035. ngx_http_set_ctx(r, ctx, ngx_http_uploadprogress_module);
  1036. /* finally says to the core we don't handle anything */
  1037. return NGX_DECLINED;
  1038. }
  1039. static void
  1040. ngx_http_uploadprogress_rbtree_insert_value(ngx_rbtree_node_t * temp,
  1041. ngx_rbtree_node_t * node,
  1042. ngx_rbtree_node_t * sentinel)
  1043. {
  1044. ngx_http_uploadprogress_node_t *upn, *upnt;
  1045. for (;;) {
  1046. if (node->key < temp->key) {
  1047. if (temp->left == sentinel) {
  1048. temp->left = node;
  1049. break;
  1050. }
  1051. temp = temp->left;
  1052. } else if (node->key > temp->key) {
  1053. if (temp->right == sentinel) {
  1054. temp->right = node;
  1055. break;
  1056. }
  1057. temp = temp->right;
  1058. } else { /* node->key == temp->key */
  1059. upn = (ngx_http_uploadprogress_node_t *) node;
  1060. upnt = (ngx_http_uploadprogress_node_t *) temp;
  1061. if (ngx_memn2cmp(upn->data, upnt->data, upn->len, upnt->len) < 0) {
  1062. if (temp->left == sentinel) {
  1063. temp->left = node;
  1064. break;
  1065. }
  1066. temp = temp->left;
  1067. } else {
  1068. if (temp->right == sentinel) {
  1069. temp->right = node;
  1070. break;
  1071. }
  1072. temp = temp->right;
  1073. }
  1074. }
  1075. }
  1076. node->parent = temp;
  1077. node->left = sentinel;
  1078. node->right = sentinel;
  1079. ngx_rbt_red(node);
  1080. }
  1081. static void
  1082. ngx_clean_old_connections(ngx_event_t * ev)
  1083. {
  1084. ngx_shm_zone_t *shm_zone;
  1085. ngx_http_uploadprogress_ctx_t *ctx;
  1086. ngx_slab_pool_t *shpool;
  1087. ngx_rbtree_node_t *node;
  1088. ngx_http_uploadprogress_node_t *up, *upprev;
  1089. time_t now = ngx_time();
  1090. int count = 0;
  1091. /* scan the rbtree */
  1092. shm_zone = ev->data;
  1093. ctx = shm_zone->data;
  1094. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  1095. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  1096. "uploadprogress clean old connections at %T", now);
  1097. ngx_shmtx_lock(&shpool->mutex);
  1098. node = (ngx_rbtree_node_t *) ctx->list_tail.prev;
  1099. for (;;) {
  1100. if (node == &ctx->list_head.node) {
  1101. break;
  1102. }
  1103. up = (ngx_http_uploadprogress_node_t *) node;
  1104. upprev = up->prev;
  1105. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  1106. "uploadprogress clean: scanning %08XD (req done %ui) timeout at %T",
  1107. node->key, up->done, up->timeout);
  1108. if ( (up->done && up->timeout < now) || (ngx_quit || ngx_terminate || ngx_exiting) ) {
  1109. up->next->prev = up->prev;
  1110. up->prev->next = up->next;
  1111. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  1112. "uploadprogress clean: removing %08XD (req %ui) ",
  1113. node->key, up->done, up->timeout);
  1114. ngx_rbtree_delete(ctx->rbtree, node);
  1115. ngx_slab_free_locked(shpool, node);
  1116. }
  1117. else
  1118. count++;
  1119. node = (ngx_rbtree_node_t *) upprev;
  1120. }
  1121. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  1122. "uploadprogress clean old connections: quit: %ui term: %ui count: %ui", ngx_quit, ngx_terminate, count);
  1123. /* don't reschedule timer if ngx_quit or ngx_terminate && nodes emtpy */
  1124. if ( count > 0 || !(ngx_quit || ngx_terminate || ngx_exiting)) {
  1125. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  1126. "uploadprogress clean old connections restarting timer");
  1127. ngx_add_timer(ev, TIMER_FREQUENCY); /* trigger again in 60s */
  1128. } else if (ngx_quit || ngx_terminate || ngx_exiting) {
  1129. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, shm_zone->shm.log, 0,
  1130. "uploadprogress clean old connections quitting , no more active connections: not restarting timer");
  1131. }
  1132. ngx_shmtx_unlock(&shpool->mutex);
  1133. }
  1134. /*
  1135. removes the expired node from the upload rbtree
  1136. */
  1137. static void
  1138. ngx_http_uploadprogress_cleanup(void *data)
  1139. {
  1140. ngx_http_uploadprogress_cleanup_t *upcln = data;
  1141. ngx_slab_pool_t *shpool;
  1142. ngx_rbtree_node_t *node;
  1143. ngx_http_uploadprogress_node_t *up;
  1144. ngx_http_request_t *r;
  1145. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, upcln->shm_zone->shm.log, 0,
  1146. "uploadprogress cleanup called");
  1147. shpool = (ngx_slab_pool_t *) upcln->shm_zone->shm.addr;
  1148. node = upcln->node;
  1149. r = upcln->r;
  1150. up = (ngx_http_uploadprogress_node_t *) node;
  1151. ngx_shmtx_lock(&shpool->mutex);
  1152. up->done = 1; /* mark the original request as done */
  1153. up->timeout = ngx_time() + upcln->timeout; /* keep tracking for 60s */
  1154. if (r != NULL ) {
  1155. ngx_uint_t rc = r->err_status ? r->err_status : r->headers_out.status;
  1156. if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
  1157. up->err_status = rc;
  1158. }
  1159. }
  1160. ngx_shmtx_unlock(&shpool->mutex);
  1161. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, upcln->shm_zone->shm.log, 0,
  1162. "uploadprogress cleanup: connection %08XD to be deleted at %T",
  1163. node->key, up->timeout);
  1164. }
  1165. static ngx_int_t
  1166. ngx_http_uploadprogress_init_zone(ngx_shm_zone_t * shm_zone, void *data)
  1167. {
  1168. ngx_http_uploadprogress_ctx_t *octx = data;
  1169. ngx_slab_pool_t *shpool;
  1170. ngx_rbtree_node_t *sentinel;
  1171. ngx_http_uploadprogress_ctx_t *ctx;
  1172. ctx = shm_zone->data;
  1173. if (octx) {
  1174. ctx->rbtree = octx->rbtree;
  1175. return NGX_OK;
  1176. }
  1177. shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
  1178. ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t));
  1179. if (ctx->rbtree == NULL) {
  1180. return NGX_ERROR;
  1181. }
  1182. sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t));
  1183. if (sentinel == NULL) {
  1184. return NGX_ERROR;
  1185. }
  1186. ngx_rbtree_init(ctx->rbtree, sentinel, ngx_http_uploadprogress_rbtree_insert_value);
  1187. return NGX_OK;
  1188. }
  1189. static ngx_int_t
  1190. ngx_http_uploadprogress_errortracker(ngx_http_request_t * r)
  1191. {
  1192. size_t n;
  1193. ngx_str_t *id;
  1194. ngx_slab_pool_t *shpool;
  1195. ngx_rbtree_node_t *node;
  1196. ngx_http_uploadprogress_ctx_t *ctx;
  1197. ngx_http_uploadprogress_node_t *up;
  1198. ngx_http_uploadprogress_conf_t *upcf;
  1199. uint32_t hash;
  1200. ngx_http_uploadprogress_cleanup_t *upcln;
  1201. ngx_pool_cleanup_t *cln;
  1202. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1203. "uploadprogress error-tracker error: %D", r->err_status);
  1204. if (r->err_status >= NGX_HTTP_SPECIAL_RESPONSE) {
  1205. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  1206. if (!upcf->track) {
  1207. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1208. "uploadprogress error-tracker not tracking in this location");
  1209. goto finish;
  1210. }
  1211. id = get_tracking_id(r);
  1212. if (id == NULL) {
  1213. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1214. "trackuploads error-tracker no id found in POST upload req");
  1215. goto finish;
  1216. }
  1217. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1218. "trackuploads error-tracker id found: %V", id);
  1219. if (upcf->shm_zone == NULL) {
  1220. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1221. "trackuploads no shm_zone for id: %V", id);
  1222. ngx_free(id);
  1223. goto finish;
  1224. }
  1225. ctx = upcf->shm_zone->data;
  1226. hash = ngx_crc32_short(id->data, id->len);
  1227. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1228. "trackuploads error-tracking hash %08XD for id: %V", hash,
  1229. id);
  1230. shpool = (ngx_slab_pool_t *) upcf->shm_zone->shm.addr;
  1231. ngx_shmtx_lock(&shpool->mutex);
  1232. if ((up = find_node(id, ctx, r->connection->log)) != NULL) {
  1233. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1234. "trackuploads error-tracking found node for id: %V", id);
  1235. up->err_status = r->err_status;
  1236. ngx_shmtx_unlock(&shpool->mutex);
  1237. ngx_free(id);
  1238. goto finish;
  1239. }
  1240. /* no lz found for this tracking id */
  1241. n = sizeof(ngx_http_uploadprogress_node_t) + id->len;
  1242. cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_uploadprogress_cleanup_t));
  1243. if (cln == NULL) {
  1244. ngx_shmtx_unlock(&shpool->mutex);
  1245. ngx_free(id);
  1246. goto finish;
  1247. }
  1248. node = ngx_slab_alloc_locked(shpool, n);
  1249. if (node == NULL) {
  1250. ngx_shmtx_unlock(&shpool->mutex);
  1251. ngx_free(id);
  1252. goto finish;
  1253. }
  1254. up = (ngx_http_uploadprogress_node_t *) node;
  1255. node->key = hash;
  1256. up->len = (u_char) id->len;
  1257. up->err_status = r->err_status;
  1258. up->done = 0;
  1259. up->rest = 0;
  1260. up->length = 0;
  1261. up->timeout = 0;
  1262. up->sequence = 0;
  1263. up->sent_portion = 0;
  1264. ngx_memcpy(up->data, id->data, id->len);
  1265. up->next = ctx->list_head.next;
  1266. up->next->prev = up;
  1267. up->prev = &ctx->list_head;
  1268. ctx->list_head.next = up;
  1269. ngx_rbtree_insert(ctx->rbtree, node);
  1270. /* start the timer if needed */
  1271. if (!upcf->cleanup.timer_set) {
  1272. upcf->cleanup.data = upcf->shm_zone;
  1273. upcf->cleanup.handler = ngx_clean_old_connections;
  1274. upcf->cleanup.log = upcf->shm_zone->shm.log;
  1275. ngx_add_timer(&upcf->cleanup, TIMER_FREQUENCY);
  1276. }
  1277. ngx_shmtx_unlock(&shpool->mutex);
  1278. cln->handler = ngx_http_uploadprogress_cleanup;
  1279. upcln = cln->data;
  1280. upcln->shm_zone = upcf->shm_zone;
  1281. upcln->node = node;
  1282. upcln->timeout = upcf->timeout;
  1283. upcln->r = r;
  1284. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1285. "trackuploads error-tracking adding: %08XD", node->key);
  1286. ngx_free(id);
  1287. }
  1288. finish:
  1289. /* call the filter chain as usual */
  1290. return ngx_http_next_header_filter(r);
  1291. }
  1292. static ngx_int_t
  1293. ngx_http_uploadprogress_init(ngx_conf_t * cf)
  1294. {
  1295. ngx_http_handler_pt *h;
  1296. ngx_http_core_main_conf_t *cmcf;
  1297. cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
  1298. /* install the tracking handler */
  1299. h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
  1300. if (h == NULL) {
  1301. return NGX_ERROR;
  1302. }
  1303. *h = ngx_http_uploadprogress_handler;
  1304. /*
  1305. we also need to track HTTP errors
  1306. unfortunately, the above handler is not called in case of
  1307. errors.
  1308. we have to register a header output filter that will be
  1309. called in any case to track those errors
  1310. */
  1311. ngx_http_next_header_filter = ngx_http_top_header_filter;
  1312. ngx_http_top_header_filter = ngx_http_uploadprogress_errortracker;
  1313. return NGX_OK;
  1314. }
  1315. static void*
  1316. ngx_http_uploadprogress_create_loc_conf(ngx_conf_t * cf)
  1317. {
  1318. ngx_http_uploadprogress_conf_t *conf;
  1319. ngx_uint_t i;
  1320. conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_conf_t));
  1321. if (conf == NULL) {
  1322. return NGX_CONF_ERROR;
  1323. }
  1324. if(ngx_array_init(&conf->templates, cf->pool, 4, sizeof(ngx_http_uploadprogress_template_t)) != NGX_OK) {
  1325. return NGX_CONF_ERROR;
  1326. }
  1327. for(i = 0;i < conf->templates.nalloc; i++) {
  1328. ngx_http_uploadprogress_template_t *elt = ngx_array_push(&conf->templates);
  1329. if (elt == NULL) {
  1330. return NGX_CONF_ERROR;
  1331. }
  1332. elt->values = NULL;
  1333. elt->lengths = NULL;
  1334. }
  1335. conf->udp_socket = -1;
  1336. return conf;
  1337. }
  1338. static char*
  1339. ngx_http_uploadprogress_merge_loc_conf(ngx_conf_t * cf, void *parent, void *child)
  1340. {
  1341. ngx_http_uploadprogress_conf_t *prev = parent;
  1342. ngx_http_uploadprogress_conf_t *conf = child;
  1343. ngx_http_uploadprogress_template_t *t, *pt, *gt;
  1344. ngx_uint_t i;
  1345. if (conf->shm_zone == NULL) {
  1346. conf->shm_zone = prev->shm_zone;
  1347. conf->timeout = prev->timeout;
  1348. conf->cleanup = prev->cleanup;
  1349. conf->handler = prev->handler;
  1350. conf->track = prev->track;
  1351. }
  1352. ngx_conf_merge_str_value(conf->content_type, prev->content_type, "text/javascript");
  1353. t = conf->templates.elts;
  1354. pt = prev->templates.elts;
  1355. gt = ngx_http_uploadprogress_global_templates.elts;
  1356. for(i = 0;i < conf->templates.nelts; i++) {
  1357. if(t[i].values == NULL) {
  1358. if(pt[i].values == NULL && gt != NULL) {
  1359. t[i].values = gt[i].values;
  1360. t[i].lengths = gt[i].lengths;
  1361. }
  1362. else {
  1363. t[i].values = pt[i].values;
  1364. t[i].lengths = pt[i].lengths;
  1365. }
  1366. }
  1367. }
  1368. ngx_conf_merge_str_value(conf->header, prev->header, "X-Progress-ID");
  1369. ngx_conf_merge_str_value(conf->header_mul, prev->header_mul, "X-ProgressMultiple-ID");
  1370. ngx_conf_merge_str_value(conf->jsonp_parameter, prev->jsonp_parameter, "callback");
  1371. return NGX_CONF_OK;
  1372. }
  1373. static ngx_int_t
  1374. ngx_http_uploadprogress_init_variables_and_templates(ngx_conf_t *cf)
  1375. {
  1376. ngx_http_variable_t *var, *v;
  1377. ngx_http_uploadprogress_state_map_t *m;
  1378. ngx_uint_t i;
  1379. /* Add variables */
  1380. for (v = ngx_http_uploadprogress_variables; v->name.len; v++) {
  1381. var = ngx_http_add_variable(cf, &v->name, v->flags);
  1382. if (var == NULL) {
  1383. return NGX_ERROR;
  1384. }
  1385. var->get_handler = v->get_handler;
  1386. var->data = v->data;
  1387. }
  1388. /* Compile global templates (containing Javascript output) */
  1389. if(ngx_array_init(&ngx_http_uploadprogress_global_templates, cf->pool, 4,
  1390. sizeof(ngx_http_uploadprogress_template_t)) != NGX_OK) {
  1391. return NGX_ERROR;
  1392. }
  1393. m = ngx_http_uploadprogress_state_map;
  1394. i = 0;
  1395. while(m->name.data != NULL) {
  1396. ngx_http_uploadprogress_template_t *elt = ngx_array_push(&ngx_http_uploadprogress_global_templates);
  1397. ngx_http_script_variables_count(ngx_http_uploadprogress_jsonp_defaults + i);
  1398. if (ngx_http_upload_progress_set_template(cf, elt, ngx_http_uploadprogress_jsonp_defaults + i) != NGX_CONF_OK) {
  1399. return NGX_ERROR;
  1400. }
  1401. m++;
  1402. i++;
  1403. }
  1404. return NGX_OK;
  1405. }
  1406. static char*
  1407. ngx_http_upload_progress(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1408. {
  1409. ssize_t n;
  1410. ngx_str_t *value;
  1411. ngx_shm_zone_t *shm_zone;
  1412. ngx_http_uploadprogress_ctx_t *ctx;
  1413. value = cf->args->elts;
  1414. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1415. "ngx_upload_progress name: %V", &value[1]);
  1416. ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_uploadprogress_ctx_t));
  1417. if (ctx == NULL) {
  1418. return NGX_CONF_ERROR;
  1419. }
  1420. ctx->list_head.prev = NULL;
  1421. ctx->list_head.next = &ctx->list_tail;
  1422. ctx->list_tail.prev = &ctx->list_head;
  1423. ctx->list_tail.next = NULL;
  1424. n = ngx_parse_size(&value[2]);
  1425. if (n == NGX_ERROR) {
  1426. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1427. "invalid size of track_uploads \"%V\"", &value[2]);
  1428. return NGX_CONF_ERROR;
  1429. }
  1430. if (n < (ngx_int_t) (8 * ngx_pagesize)) {
  1431. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1432. "track_uploads \"%V\" is too small", &value[1]);
  1433. return NGX_CONF_ERROR;
  1434. }
  1435. shm_zone = ngx_shared_memory_add(cf, &value[1], n,
  1436. &ngx_http_uploadprogress_module);
  1437. if (shm_zone == NULL) {
  1438. return NGX_CONF_ERROR;
  1439. }
  1440. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1441. "ngx_upload_progress name: %V, szhm_zone: %p", &value[1],
  1442. shm_zone);
  1443. if (shm_zone->data) {
  1444. ctx = shm_zone->data;
  1445. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1446. "track_uploads \"%V\" is already created", &value[1]);
  1447. return NGX_CONF_ERROR;
  1448. }
  1449. shm_zone->init = ngx_http_uploadprogress_init_zone;
  1450. shm_zone->data = ctx;
  1451. return NGX_CONF_OK;
  1452. }
  1453. static char*
  1454. ngx_http_track_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1455. {
  1456. ngx_http_core_loc_conf_t *clcf;
  1457. ngx_http_uploadprogress_conf_t *lzcf = conf;
  1458. ngx_str_t *value;
  1459. ngx_url_t url;
  1460. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_track_uploads in");
  1461. value = cf->args->elts;
  1462. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1463. "ngx_track_uploads name: %V", &value[1]);
  1464. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  1465. &ngx_http_uploadprogress_module);
  1466. if (lzcf->shm_zone == NULL) {
  1467. return NGX_CONF_ERROR;
  1468. }
  1469. lzcf->track = (u_char) 1;
  1470. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1471. "ngx_track_uploads name: %V,szhm_zone: %p", &value[1],
  1472. lzcf->shm_zone);
  1473. lzcf->timeout = ngx_parse_time(&value[2], 1);
  1474. if (lzcf->timeout == NGX_ERROR) {
  1475. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1476. "track_uploads \"%V\" timeout value invalid", &value[2]);
  1477. return NGX_CONF_ERROR;
  1478. }
  1479. if(cf->args->nelts > 3)
  1480. {
  1481. ngx_memzero(&url, sizeof(ngx_url_t));
  1482. url.url = value[3];
  1483. url.default_port = 80;
  1484. url.no_resolve = 0;
  1485. if(ngx_parse_url(cf->pool, &url) != NGX_OK)
  1486. {
  1487. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "Invalid graphite server %V: %s", &url.host, url.err);
  1488. return NGX_CONF_ERROR;
  1489. }
  1490. lzcf->progress_server = url.addrs[0];
  1491. if(lzcf->udp_socket == -1)
  1492. lzcf->udp_socket = ngx_socket(PF_INET, SOCK_DGRAM, 0);
  1493. }
  1494. clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  1495. lzcf->handler = clcf->handler;
  1496. if ( lzcf->handler == NULL )
  1497. {
  1498. return "track_upload should be the last directive in the location, after either proxy_pass or fastcgi_pass";
  1499. }
  1500. clcf->handler = ngx_http_uploadprogress_content_handler;
  1501. return NGX_CONF_OK;
  1502. }
  1503. static char*
  1504. ngx_http_report_uploads(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1505. {
  1506. ngx_http_uploadprogress_conf_t *lzcf = conf;
  1507. ngx_http_core_loc_conf_t *clcf;
  1508. ngx_str_t *value;
  1509. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_report_uploads in");
  1510. value = cf->args->elts;
  1511. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1512. "ngx_report_uploads name: %V", &value[1]);
  1513. lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
  1514. &ngx_http_uploadprogress_module);
  1515. if (lzcf->shm_zone == NULL) {
  1516. return NGX_CONF_ERROR;
  1517. }
  1518. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
  1519. "ngx_report_uploads name: %V, szhm_zone: %p", &value[1],
  1520. lzcf->shm_zone);
  1521. lzcf->track = (u_char) 0;
  1522. /* install our report handler */
  1523. clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  1524. clcf->handler = ngx_http_reportuploads_handler;
  1525. return NGX_CONF_OK;
  1526. }
  1527. static char*
  1528. ngx_http_upload_progress_set_template(ngx_conf_t * cf, ngx_http_uploadprogress_template_t *t, ngx_str_t *source)
  1529. {
  1530. ssize_t n;
  1531. ngx_http_script_compile_t sc;
  1532. n = ngx_http_script_variables_count(source);
  1533. ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
  1534. t->lengths = NULL;
  1535. t->values = NULL;
  1536. sc.cf = cf;
  1537. sc.source = source;
  1538. sc.lengths = &t->lengths;
  1539. sc.values = &t->values;
  1540. sc.variables = n;
  1541. sc.complete_lengths = 1;
  1542. sc.complete_values = 1;
  1543. if (ngx_http_script_compile(&sc) != NGX_OK) {
  1544. return NGX_CONF_ERROR;
  1545. }
  1546. return NGX_CONF_OK;
  1547. }
  1548. static char*
  1549. ngx_http_upload_progress_template(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1550. {
  1551. ngx_http_uploadprogress_conf_t *upcf = conf;
  1552. ngx_str_t *value;
  1553. ngx_http_uploadprogress_state_map_t *m = ngx_http_uploadprogress_state_map;
  1554. ngx_http_uploadprogress_template_t *t;
  1555. upcf->json_multiple = 0;
  1556. value = cf->args->elts;
  1557. while(m->name.data != NULL) {
  1558. if((value[1].len == m->name.len && !ngx_strncmp(value[1].data, m->name.data, m->name.len))
  1559. || (value[1].len == 2 && !ngx_strncmp(value[1].data, m->name.data, 2))) {
  1560. break;
  1561. }
  1562. m++;
  1563. }
  1564. if (m->name.data == NULL) {
  1565. ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  1566. "unknown state \"%V\"", &value[1]);
  1567. return NGX_CONF_ERROR;
  1568. }
  1569. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts + (ngx_uint_t)m->idx;
  1570. return ngx_http_upload_progress_set_template(cf, t, &value[2]);
  1571. }
  1572. static char*
  1573. ngx_http_upload_progress_java_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1574. {
  1575. ngx_http_uploadprogress_conf_t *upcf = conf;
  1576. ngx_http_uploadprogress_template_t *t;
  1577. ngx_uint_t i;
  1578. char* rc;
  1579. upcf->json_multiple = 0;
  1580. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
  1581. for(i = 0;i < upcf->templates.nelts;i++) {
  1582. rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_java_defaults + i);
  1583. if(rc != NGX_CONF_OK) {
  1584. return rc;
  1585. }
  1586. }
  1587. upcf->content_type.data = (u_char*)"text/javascript";
  1588. upcf->content_type.len = sizeof("text/javascript") - 1;
  1589. return NGX_CONF_OK;
  1590. }
  1591. static char*
  1592. ngx_http_upload_progress_json_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1593. {
  1594. ngx_http_uploadprogress_conf_t *upcf = conf;
  1595. ngx_http_uploadprogress_template_t *t;
  1596. ngx_uint_t i;
  1597. char* rc;
  1598. upcf->json_multiple = 0;
  1599. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
  1600. for(i = 0;i < upcf->templates.nelts;i++) {
  1601. rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_json_defaults + i);
  1602. if(rc != NGX_CONF_OK) {
  1603. return rc;
  1604. }
  1605. }
  1606. upcf->content_type.data = (u_char*)"application/json";
  1607. upcf->content_type.len = sizeof("application/json") - 1;
  1608. return NGX_CONF_OK;
  1609. }
  1610. static char*
  1611. ngx_http_upload_progress_jsonp_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1612. {
  1613. ngx_http_uploadprogress_conf_t *upcf = conf;
  1614. ngx_http_uploadprogress_template_t *t;
  1615. ngx_uint_t i;
  1616. char* rc;
  1617. upcf->json_multiple = 0;
  1618. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
  1619. for(i = 0;i < upcf->templates.nelts;i++) {
  1620. rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_jsonp_defaults + i);
  1621. if(rc != NGX_CONF_OK) {
  1622. return rc;
  1623. }
  1624. }
  1625. upcf->content_type.data = (u_char*)"application/javascript";
  1626. upcf->content_type.len = sizeof("application/javascript") - 1;
  1627. return NGX_CONF_OK;
  1628. }
  1629. static char*
  1630. ngx_http_upload_progress_json_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1631. {
  1632. ngx_http_uploadprogress_conf_t *upcf = conf;
  1633. ngx_http_uploadprogress_template_t *t;
  1634. ngx_uint_t i;
  1635. char* rc;
  1636. upcf->json_multiple = 1;
  1637. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
  1638. for(i = 0;i < upcf->templates.nelts;i++) {
  1639. rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_json_multiple_defaults + i);
  1640. if(rc != NGX_CONF_OK) {
  1641. return rc;
  1642. }
  1643. }
  1644. upcf->content_type.data = (u_char*)"application/json";
  1645. upcf->content_type.len = sizeof("application/json") - 1;
  1646. return NGX_CONF_OK;
  1647. }
  1648. static char*
  1649. ngx_http_upload_progress_jsonp_multiple_output(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
  1650. {
  1651. ngx_http_uploadprogress_conf_t *upcf = conf;
  1652. ngx_http_uploadprogress_template_t *t;
  1653. ngx_uint_t i;
  1654. char* rc;
  1655. upcf->json_multiple = 1;
  1656. t = (ngx_http_uploadprogress_template_t*)upcf->templates.elts;
  1657. for(i = 0;i < upcf->templates.nelts;i++) {
  1658. rc = ngx_http_upload_progress_set_template(cf, t + i, ngx_http_uploadprogress_jsonp_multiple_defaults + i);
  1659. if(rc != NGX_CONF_OK) {
  1660. return rc;
  1661. }
  1662. }
  1663. upcf->content_type.data = (u_char*)"application/json";
  1664. upcf->content_type.len = sizeof("application/json") - 1;
  1665. return NGX_CONF_OK;
  1666. }
  1667. static ngx_int_t ngx_http_uploadprogress_received_variable(ngx_http_request_t *r,
  1668. ngx_http_variable_value_t *v, uintptr_t data)
  1669. {
  1670. ngx_http_uploadprogress_node_t *up;
  1671. u_char *p;
  1672. off_t *value;
  1673. up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
  1674. value = (off_t *) ((char *) up + data);
  1675. p = ngx_palloc(r->pool, NGX_OFF_T_LEN);
  1676. if (p == NULL) {
  1677. return NGX_ERROR;
  1678. }
  1679. v->len = ngx_sprintf(p, "%O", up->length - *value) - p;
  1680. v->valid = 1;
  1681. v->no_cacheable = 0;
  1682. v->not_found = 0;
  1683. v->data = p;
  1684. return NGX_OK;
  1685. }
  1686. static ngx_int_t ngx_http_uploadprogress_offset_variable(ngx_http_request_t *r,
  1687. ngx_http_variable_value_t *v, uintptr_t data)
  1688. {
  1689. ngx_http_uploadprogress_node_t *up;
  1690. u_char *p;
  1691. off_t *value;
  1692. up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
  1693. value = (off_t *) ((char *) up + data);
  1694. p = ngx_palloc(r->pool, NGX_OFF_T_LEN);
  1695. if (p == NULL) {
  1696. return NGX_ERROR;
  1697. }
  1698. v->len = ngx_sprintf(p, "%O", *value) - p;
  1699. v->valid = 1;
  1700. v->no_cacheable = 0;
  1701. v->not_found = 0;
  1702. v->data = p;
  1703. return NGX_OK;
  1704. }
  1705. static ngx_int_t
  1706. ngx_http_uploadprogress_status_variable(ngx_http_request_t *r,
  1707. ngx_http_variable_value_t *v, uintptr_t data)
  1708. {
  1709. ngx_http_uploadprogress_node_t *up;
  1710. u_char *p;
  1711. off_t *value;
  1712. up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
  1713. value = (off_t *) ((char *) up + data);
  1714. p = ngx_palloc(r->pool, NGX_OFF_T_LEN);
  1715. if (p == NULL) {
  1716. return NGX_ERROR;
  1717. }
  1718. v->len = ngx_sprintf(p, "%O", *value) - p;
  1719. v->valid = 1;
  1720. v->no_cacheable = 0;
  1721. v->not_found = 0;
  1722. v->data = p;
  1723. return NGX_OK;
  1724. }
  1725. static ngx_int_t
  1726. ngx_http_uploadprogress_id_variable(ngx_http_request_t *r,
  1727. ngx_http_variable_value_t *v, uintptr_t data)
  1728. {
  1729. ngx_http_uploadprogress_node_t *up;
  1730. u_char *p;
  1731. up = ngx_http_get_module_ctx(r, ngx_http_uploadprogress_module);
  1732. p = ngx_palloc(r->pool, up->len);
  1733. if (p == NULL) {
  1734. return NGX_ERROR;
  1735. }
  1736. v->len = up->len;
  1737. v->data = p;
  1738. ngx_memcpy(v->data, up->data, up->len);
  1739. v->valid = 1;
  1740. v->no_cacheable = 0;
  1741. v->not_found = 0;
  1742. return NGX_OK;
  1743. }
  1744. static ngx_int_t
  1745. ngx_http_uploadprogress_callback_variable(ngx_http_request_t *r,
  1746. ngx_http_variable_value_t *v, uintptr_t data)
  1747. {
  1748. u_char *p, *start_p, *val, prefix[1024];
  1749. ngx_http_uploadprogress_conf_t *upcf;
  1750. u_int len;
  1751. upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
  1752. if (r->args.len) {
  1753. /* '=' has to be appended to avoid matching parameters that have the */
  1754. /* configured name as a prefix but are longer */
  1755. ngx_snprintf(prefix, sizeof(prefix) - 1, "%s=", upcf->jsonp_parameter.data);
  1756. len = upcf->jsonp_parameter.len + 1;
  1757. prefix[len] = '\0'; /* Force termination of string */
  1758. p = (u_char *) ngx_strstr(r->args.data, prefix);
  1759. if (p) {
  1760. p += len;
  1761. start_p = p;
  1762. while (p < r->args.data + r->args.len) {
  1763. if (*((p++) + 1) == '&') {
  1764. break;
  1765. }
  1766. }
  1767. v->len = p - start_p;
  1768. val = ngx_palloc(r->pool, v->len + 1);
  1769. if (val == NULL) {
  1770. return NGX_ERROR;
  1771. }
  1772. ngx_memcpy(val, start_p, v->len);
  1773. val[v->len] = '\0';
  1774. v->valid = 1;
  1775. v->no_cacheable = 0;
  1776. v->not_found = 0;
  1777. v->data = val;
  1778. } else {
  1779. return NGX_ERROR;
  1780. }
  1781. } else {
  1782. return NGX_ERROR;
  1783. }
  1784. return NGX_OK;
  1785. }