TCMS.pm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package TCMS;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures state};
  6. use Date::Format qw{strftime};
  7. use HTTP::Body ();
  8. use URL::Encode ();
  9. use Text::Xslate ();
  10. use Plack::MIME ();
  11. use Mojo::File ();
  12. use DateTime::Format::HTTP();
  13. use CGI::Cookie ();
  14. use File::Basename();
  15. use IO::Compress::Gzip();
  16. use Time::HiRes qw{gettimeofday tv_interval};
  17. use HTTP::HeaderParser::XS;
  18. #Grab our custom routes
  19. use lib 'lib';
  20. use Trog::Routes::HTML;
  21. use Trog::Routes::JSON;
  22. use Trog::Auth;
  23. use Trog::Utils;
  24. use Trog::Config;
  25. use Trog::Data;
  26. use Trog::Vars;
  27. # Troglodyne philosophy - simple as possible
  28. # Import the routes
  29. my $conf = Trog::Config::get();
  30. my $data = Trog::Data->new($conf);
  31. my %roots = $data->routes();
  32. my %routes = %Trog::Routes::HTML::routes;
  33. @routes{keys(%Trog::Routes::JSON::routes)} = values(%Trog::Routes::JSON::routes);
  34. @routes{keys(%roots)} = values(%roots);
  35. my %aliases = $data->aliases();
  36. # XXX this is built progressively across the forks, leading to inconsistent behavior.
  37. # This should eventually be pre-filled from DB.
  38. my %etags;
  39. #1MB chunks
  40. my $CHUNK_SIZE = 1024000;
  41. #Stuff that isn't in upstream finders
  42. my %extra_types = (
  43. '.docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  44. );
  45. =head2 app()
  46. Dispatches requests based on %routes built above.
  47. The dispatcher here does *not* do anything with the authn/authz data. It sets those in the 'user' and 'acls' parameters of the query object passed to routes.
  48. If a path passed is not a defined route (or regex route), but exists as a file under www/, it will be served up immediately.
  49. =cut
  50. sub app {
  51. # Start the server timing clock
  52. my $start = [gettimeofday];
  53. my $env = shift;
  54. # Check eTags. If we don't know about it, just assume it's good and lazily fill the cache
  55. # XXX yes, this allows cache poisoning...but only for logged in users!
  56. if ($env->{HTTP_IF_NONE_MATCH}) {
  57. return [304, [], ['']] if $env->{HTTP_IF_NONE_MATCH} eq ($etags{$env->{REQUEST_URI}} || '');
  58. $etags{$env->{REQUEST_URI}} = $env->{HTTP_IF_NONE_MATCH} unless exists $etags{$env->{REQUEST_URI}};
  59. }
  60. my $last_fetch = 0;
  61. if ($env->{HTTP_IF_MODIFIED_SINCE}) {
  62. $last_fetch = DateTime::Format::HTTP->parse_datetime($env->{HTTP_IF_MODIFIED_SINCE})->epoch();
  63. }
  64. #XXX Don't use statics anything that has a search query
  65. # On one hand, I don't want to DOS the disk, but I'd also like some like ?rss...
  66. # Should probably turn those into aliases.
  67. my $has_query = !!$env->{QUERY_STRING};
  68. my $query = {};
  69. $query = URL::Encode::url_params_mixed($env->{QUERY_STRING}) if $env->{QUERY_STRING};
  70. my $path = $env->{PATH_INFO};
  71. $path = '/index' if $path eq '/';
  72. # Translate alias paths into their actual path
  73. $path = $aliases{$path} if exists $aliases{$path};
  74. # Figure out if we want compression or not
  75. my $alist = $env->{HTTP_ACCEPT_ENCODING} || '';
  76. $alist =~ s/\s//g;
  77. my @accept_encodings;
  78. @accept_encodings = split(/,/, $alist);
  79. my $deflate = grep { 'gzip' eq $_ } @accept_encodings;
  80. # Collapse multiple slashes in the path
  81. $path =~ s/[\/]+/\//g;
  82. # Let's open up our default route before we bother to see if users even exist
  83. return $routes{default}{callback}->($query) unless -f "config/setup";
  84. my $cookies = {};
  85. if ($env->{HTTP_COOKIE}) {
  86. $cookies = CGI::Cookie->parse($env->{HTTP_COOKIE});
  87. }
  88. my $active_user = '';
  89. if (exists $cookies->{tcmslogin}) {
  90. $active_user = Trog::Auth::session2user($cookies->{tcmslogin}->value);
  91. }
  92. $query->{acls} = [];
  93. $query->{acls} = Trog::Auth::acls4user($active_user) // [] if $active_user;
  94. #Disallow any paths that are naughty ( starman auto-removes .. up-traversal)
  95. if (index($path,'/templates') == 0 || index($path, '/statics') == 0 || $path =~ m/.*(\.psgi|\.pm)$/i ) {
  96. return _forbidden($query);
  97. }
  98. # If we have a static render, just use it instead (These will ALWAYS be correct, data saves invalidate this)
  99. # TODO: make this key on admin INSTEAD of active user when we add non-admin users.
  100. my $streaming = $env->{'psgi.streaming'};
  101. $query->{streaming} = $streaming;
  102. $query->{start} = $start;
  103. if (!$active_user && !$has_query) {
  104. return _static("$path.z",$start) if -f "www/statics/$path.z" && $deflate;
  105. return _static($path,$start) if -f "www/statics/$path";
  106. }
  107. return _serve("www/$path", $start, $streaming, $last_fetch, $deflate) if -f "www/$path";
  108. #Handle regex/capture routes
  109. if (!exists $routes{$path}) {
  110. my @captures;
  111. foreach my $pattern (keys(%routes)) {
  112. @captures = $path =~ m/^$pattern$/;
  113. if (@captures) {
  114. $path = $pattern;
  115. foreach my $field (@{$routes{$path}{captures}}) {
  116. $routes{$path}{data} //= {};
  117. $routes{$path}{data}{$field} = shift @captures;
  118. }
  119. last;
  120. }
  121. }
  122. }
  123. $query->{deflate} = $deflate;
  124. $query->{user} = $active_user;
  125. return _notfound($query) unless exists $routes{$path};
  126. return _badrequest($query) unless grep { $env->{REQUEST_METHOD} eq $_ } ($routes{$path}{method} || '','HEAD');
  127. @{$query}{keys(%{$routes{$path}{'data'}})} = values(%{$routes{$path}{'data'}}) if ref $routes{$path}{'data'} eq 'HASH' && %{$routes{$path}{'data'}};
  128. #Actually parse the POSTDATA and dump it into the QUERY object if this is a POST
  129. if ($env->{REQUEST_METHOD} eq 'POST') {
  130. my $body = HTTP::Body->new( $env->{CONTENT_TYPE}, $env->{CONTENT_LENGTH} );
  131. while ( read($env->{'psgi.input'}, my $buf, $CHUNK_SIZE) ) {
  132. $body->add($buf);
  133. }
  134. @$query{keys(%{$body->param})} = values(%{$body->param});
  135. @$query{keys(%{$body->upload})} = values(%{$body->upload});
  136. }
  137. #Set various things we don't want overridden
  138. $query->{body} = '';
  139. $query->{user} = $active_user;
  140. $query->{domain} = $env->{HTTP_X_FORWARDED_HOST} || $env->{HTTP_HOST};
  141. $query->{route} = $path;
  142. $query->{scheme} = $env->{'psgi.url_scheme'} // 'http';
  143. $query->{social_meta} = 1;
  144. $query->{primary_post} = {};
  145. #XXX there is a trick to now use strict refs, but I don't remember it right at the moment
  146. {
  147. no strict 'refs';
  148. my $output = $routes{$path}{callback}->($query);
  149. # Append server-timing headers
  150. my $tot = tv_interval($start) * 1000;
  151. push(@{$output->[1]}, 'Server-Timing' => "app;dur=$tot");
  152. return $output;
  153. }
  154. };
  155. sub _generic($type, $query) {
  156. return _static("$type.z",$query->{start}) if -f "www/statics/$type.z";
  157. return _static($type, $query->{start}) if -f "www/statics/$type";
  158. my %lookup = (
  159. notfound => \&Trog::Routes::HTML::notfound,
  160. forbidden => \&Trog::Routes::HTML::forbidden,
  161. badrequest => \&Trog::Routes::HTML::badrequest,
  162. );
  163. return $lookup{$type}->($query);
  164. }
  165. sub _notfound ($query) {
  166. return _generic('notfound', $query);
  167. }
  168. sub _forbidden($query) {
  169. return _generic('forbidden', $query);
  170. }
  171. sub _badrequest($query) {
  172. return _generic('badrequest', $query);
  173. }
  174. sub _static($path,$start,$last_fetch=0) {
  175. # XXX because of psgi I can't just vomit the file directly
  176. if (open(my $fh, '<', "www/statics/$path")) {
  177. my $headers = '';
  178. # NOTE: this is relying on while advancing the file pointer
  179. while (<$fh>) {
  180. last if $_ eq "\n";
  181. $headers .= $_;
  182. }
  183. my $hdrs = HTTP::HeaderParser::XS->new(\$headers);
  184. my $headers_parsed = $hdrs->getHeaders();
  185. #XXX need to put this into the file itself
  186. my $mt = (stat($fh))[9];
  187. my @gm = gmtime($mt);
  188. my $now_string = strftime( "%a, %d %b %Y %H:%M:%S GMT", @gm );
  189. my $code = $mt > $last_fetch ? $hdrs->getStatusCode() : 304;
  190. $headers_parsed->{"Last-Modified"} = $now_string;
  191. # Append server-timing headers
  192. my $tot = tv_interval($start) * 1000;
  193. $headers_parsed->{'Server-Timing'} = "static;dur=$tot";
  194. return [$code, [%$headers_parsed], $fh];
  195. }
  196. return [ 403, ['Content-Type' => $Trog::Vars::content_types{plain}], ["STAY OUT YOU RED MENACE"]];
  197. }
  198. sub _serve ($path, $start, $streaming=0, $last_fetch=0, $deflate=0) {
  199. my $mf = Mojo::File->new($path);
  200. my $ext = '.'.$mf->extname();
  201. my $ft;
  202. if ($ext) {
  203. $ft = Plack::MIME->mime_type($ext) if $ext;
  204. $ft ||= $extra_types{$ext} if exists $extra_types{$ext};
  205. }
  206. $ft ||= $Trog::Vars::content_types{plain};
  207. my $ct = 'Content-type';
  208. my @headers = ($ct => $ft);
  209. #TODO use static Cache-Control for everything but JS/CSS?
  210. push(@headers,'Cache-control' => $Trog::Vars::cache_control{revalidate});
  211. my $mt = (stat($path))[9];
  212. my $sz = (stat(_))[7];
  213. my @gm = gmtime($mt);
  214. my $now_string = strftime( "%a, %d %b %Y %H:%M:%S GMT", @gm );
  215. my $code = $mt > $last_fetch ? 200 : 304;
  216. #XXX doing metadata=preload on videos doesn't work right?
  217. #push(@headers, "Content-Length: $sz");
  218. push(@headers, "Last-Modified" => $now_string);
  219. push(@headers, 'Vary' => 'Accept-Encoding');
  220. if (open(my $fh, '<', $path)) {
  221. return sub {
  222. my $responder = shift;
  223. my $writer = $responder->([ $code, \@headers]);
  224. while ( read($fh, my $buf, $CHUNK_SIZE) ) {
  225. $writer->write($buf);
  226. }
  227. close $fh;
  228. $writer->close;
  229. } if $streaming && $sz > $CHUNK_SIZE;
  230. #Return data in the event the caller does not support deflate
  231. if (!$deflate) {
  232. push( @headers, "Content-Length" => $sz );
  233. # Append server-timing headers
  234. my $tot = tv_interval($start) * 1000;
  235. push(@headers, 'Server-Timing' => "file;dur=$tot");
  236. return [ $code, \@headers, $fh];
  237. }
  238. #Compress everything less than 1MB
  239. push( @headers, "Content-Encoding" => "gzip" );
  240. my $dfh;
  241. IO::Compress::Gzip::gzip( $fh => \$dfh );
  242. print $IO::Compress::Gzip::GzipError if $IO::Compress::Gzip::GzipError;
  243. push( @headers, "Content-Length" => length($dfh) );
  244. # Append server-timing headers
  245. my $tot = tv_interval($start) * 1000;
  246. push(@headers, 'Server-Timing' => "file;dur=$tot");
  247. return [ $code, \@headers, [$dfh]];
  248. }
  249. return [ 403, [$ct => $Trog::Vars::content_types{plain}], ["STAY OUT YOU RED MENACE"]];
  250. }
  251. 1;