您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ngx_http_uploadprogress_module.c 72KB

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