選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ngx_http_uploadprogress_module.c 72KB

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