You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ngx_http_uploadprogress_module.c 67KB

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