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

ngx_http_uploadprogress_module.c 70KB

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