Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ngx_http_uploadprogress_module.c 69KB

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