TCMS.pm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. if (!$active_user && !$has_query) {
  103. return _static("$path.z",$streaming) if -f "www/statics/$path.z" && $deflate;
  104. return _static($path,$streaming) if -f "www/statics/$path";
  105. }
  106. return _serve("www/$path", $start, $streaming, $last_fetch, $deflate) if -f "www/$path";
  107. #Handle regex/capture routes
  108. if (!exists $routes{$path}) {
  109. my @captures;
  110. foreach my $pattern (keys(%routes)) {
  111. @captures = $path =~ m/^$pattern$/;
  112. if (@captures) {
  113. $path = $pattern;
  114. foreach my $field (@{$routes{$path}{captures}}) {
  115. $routes{$path}{data} //= {};
  116. $routes{$path}{data}{$field} = shift @captures;
  117. }
  118. last;
  119. }
  120. }
  121. }
  122. $query->{deflate} = $deflate;
  123. $query->{user} = $active_user;
  124. return _notfound($query) unless exists $routes{$path};
  125. return _badrequest($query) unless grep { $env->{REQUEST_METHOD} eq $_ } ($routes{$path}{method} || '','HEAD');
  126. @{$query}{keys(%{$routes{$path}{'data'}})} = values(%{$routes{$path}{'data'}}) if ref $routes{$path}{'data'} eq 'HASH' && %{$routes{$path}{'data'}};
  127. #Actually parse the POSTDATA and dump it into the QUERY object if this is a POST
  128. if ($env->{REQUEST_METHOD} eq 'POST') {
  129. my $body = HTTP::Body->new( $env->{CONTENT_TYPE}, $env->{CONTENT_LENGTH} );
  130. while ( read($env->{'psgi.input'}, my $buf, $CHUNK_SIZE) ) {
  131. $body->add($buf);
  132. }
  133. @$query{keys(%{$body->param})} = values(%{$body->param});
  134. @$query{keys(%{$body->upload})} = values(%{$body->upload});
  135. }
  136. #Set various things we don't want overridden
  137. $query->{body} = '';
  138. $query->{user} = $active_user;
  139. $query->{domain} = $env->{HTTP_X_FORWARDED_HOST} || $env->{HTTP_HOST};
  140. $query->{route} = $path;
  141. $query->{scheme} = $env->{'psgi.url_scheme'} // 'http';
  142. $query->{social_meta} = 1;
  143. $query->{primary_post} = {};
  144. #XXX there is a trick to now use strict refs, but I don't remember it right at the moment
  145. {
  146. no strict 'refs';
  147. my $output = $routes{$path}{callback}->($query);
  148. # Append server-timing headers
  149. my $tot = tv_interval($start) * 1000;
  150. push(@{$output->[1]}, 'Server-Timing' => "app;dur=$tot");
  151. return $output;
  152. }
  153. };
  154. sub _generic($type, $query) {
  155. return _static("$type.z",$query->{streaming}) if -f "www/statics/$type.z";
  156. return _static($type, $query->{streaming}) if -f "www/statics/$type";
  157. my %lookup = (
  158. notfound => \&Trog::Routes::HTML::notfound,
  159. forbidden => \&Trog::Routes::HTML::forbidden,
  160. badrequest => \&Trog::Routes::HTML::badrequest,
  161. );
  162. return $lookup{$type}->($query);
  163. }
  164. sub _notfound ( $query ) {
  165. return _generic('notfound', $query);
  166. }
  167. sub _forbidden($query) {
  168. return _generic('forbidden', $query);
  169. }
  170. sub _badrequest($query) {
  171. return _generic('badrequest', $query);
  172. }
  173. sub _static($path,$streaming=0,$last_fetch=0) {
  174. # XXX because of psgi I can't just vomit the file directly
  175. if (open(my $fh, '<', "www/statics/$path")) {
  176. my $headers = '';
  177. # NOTE: this is relying on while advancing the file pointer
  178. while (<$fh>) {
  179. last if $_ eq "\n";
  180. $headers .= $_;
  181. }
  182. my $hdrs = HTTP::HeaderParser::XS->new(\$headers);
  183. my $headers_parsed = $hdrs->getHeaders();
  184. #XXX need to put this into the file itself
  185. my $mt = (stat($fh))[9];
  186. my @gm = gmtime($mt);
  187. my $now_string = strftime( "%a, %d %b %Y %H:%M:%S GMT", @gm );
  188. my $code = $mt > $last_fetch ? $hdrs->getStatusCode() : 304;
  189. $headers_parsed->{"Last-Modified"} = $now_string;
  190. return [$code, [%$headers_parsed], $fh];
  191. }
  192. return [ 403, ['Content-Type' => $Trog::Vars::content_types{plain}], ["STAY OUT YOU RED MENACE"]];
  193. }
  194. sub _serve ($path, $start, $streaming=0, $last_fetch=0, $deflate=0) {
  195. my $mf = Mojo::File->new($path);
  196. my $ext = '.'.$mf->extname();
  197. my $ft;
  198. if ($ext) {
  199. $ft = Plack::MIME->mime_type($ext) if $ext;
  200. $ft ||= $extra_types{$ext} if exists $extra_types{$ext};
  201. }
  202. $ft ||= $Trog::Vars::content_types{plain};
  203. my $ct = 'Content-type';
  204. my @headers = ($ct => $ft);
  205. #TODO use static Cache-Control for everything but JS/CSS?
  206. push(@headers,'Cache-control' => $Trog::Vars::cache_control{revalidate});
  207. my $mt = (stat($path))[9];
  208. my $sz = (stat(_))[7];
  209. my @gm = gmtime($mt);
  210. my $now_string = strftime( "%a, %d %b %Y %H:%M:%S GMT", @gm );
  211. my $code = $mt > $last_fetch ? 200 : 304;
  212. #XXX doing metadata=preload on videos doesn't work right?
  213. #push(@headers, "Content-Length: $sz");
  214. push(@headers, "Last-Modified" => $now_string);
  215. push(@headers, 'Vary' => 'Accept-Encoding');
  216. if (open(my $fh, '<', $path)) {
  217. return sub {
  218. my $responder = shift;
  219. my $writer = $responder->([ $code, \@headers]);
  220. while ( read($fh, my $buf, $CHUNK_SIZE) ) {
  221. $writer->write($buf);
  222. }
  223. close $fh;
  224. $writer->close;
  225. } if $streaming && $sz > $CHUNK_SIZE;
  226. #Return data in the event the caller does not support deflate
  227. if (!$deflate) {
  228. push( @headers, "Content-Length" => $sz );
  229. # Append server-timing headers
  230. my $tot = tv_interval($start) * 1000;
  231. push(@headers, 'Server-Timing' => "file;dur=$tot");
  232. return [ $code, \@headers, $fh];
  233. }
  234. #Compress everything less than 1MB
  235. push( @headers, "Content-Encoding" => "gzip" );
  236. my $dfh;
  237. IO::Compress::Gzip::gzip( $fh => \$dfh );
  238. print $IO::Compress::Gzip::GzipError if $IO::Compress::Gzip::GzipError;
  239. push( @headers, "Content-Length" => length($dfh) );
  240. # Append server-timing headers
  241. my $tot = tv_interval($start) * 1000;
  242. push(@headers, 'Server-Timing' => "file;dur=$tot");
  243. return [ $code, \@headers, [$dfh]];
  244. }
  245. return [ 403, [$ct => $Trog::Vars::content_types{plain}], ["STAY OUT YOU RED MENACE"]];
  246. }
  247. 1;