TCMS.pm 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package TCMS;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures};
  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 Encode qw{encode_utf8};
  14. use CGI::Cookie ();
  15. use File::Basename();
  16. use IO::Compress::Deflate();
  17. #Grab our custom routes
  18. use lib 'lib';
  19. use Trog::Routes::HTML;
  20. use Trog::Routes::JSON;
  21. use Trog::Auth;
  22. use Trog::Utils;
  23. use Trog::Config;
  24. use Trog::Data;
  25. # Troglodyne philosophy - simple as possible
  26. # Import the routes
  27. my $conf = Trog::Config::get();
  28. my $data = Trog::Data->new($conf);
  29. my %roots = $data->routes();
  30. my %routes = %Trog::Routes::HTML::routes;
  31. @routes{keys(%Trog::Routes::JSON::routes)} = values(%Trog::Routes::JSON::routes);
  32. @routes{keys(%roots)} = values(%roots);
  33. my %aliases = $data->aliases();
  34. #1MB chunks
  35. my $CHUNK_SIZE = 1024000;
  36. # Things we will actually produce from routes rather than just serving up files
  37. my $ct = 'Content-type';
  38. my %content_types = (
  39. plain => "text/plain;",
  40. html => "text/html; charset=UTF-8",
  41. json => "application/json;",
  42. blob => "application/octet-stream;",
  43. );
  44. my $cc = 'Cache-control';
  45. my %cache_control = (
  46. revalidate => "no-cache, max-age=0",
  47. nocache => "no-store",
  48. static => "public, max-age=604800, immutable",
  49. );
  50. #Stuff that isn't in upstream finders
  51. my %extra_types = (
  52. '.docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  53. );
  54. =head2 app()
  55. Dispatches requests based on %routes built above.
  56. 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.
  57. 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.
  58. =cut
  59. sub app {
  60. my $env = shift;
  61. my $last_fetch = 0;
  62. if ($env->{HTTP_IF_MODIFIED_SINCE}) {
  63. $last_fetch = DateTime::Format::HTTP->parse_datetime($env->{HTTP_IF_MODIFIED_SINCE})->epoch();
  64. }
  65. my $query = {};
  66. $query = URL::Encode::url_params_mixed($env->{QUERY_STRING}) if $env->{QUERY_STRING};
  67. my $path = $env->{PATH_INFO};
  68. # Translate alias paths into their actual path
  69. $path = $aliases{$path} if exists $aliases{$path};
  70. # Collapse multiple slashes in the path
  71. $path =~ s/[\/]+/\//g;
  72. # Let's open up our default route before we bother to see if users even exist
  73. return $routes{default}{callback}->($query,\&_render) unless -f "config/setup";
  74. my $cookies = {};
  75. if ($env->{HTTP_COOKIE}) {
  76. $cookies = CGI::Cookie->parse($env->{HTTP_COOKIE});
  77. }
  78. my $active_user = '';
  79. if (exists $cookies->{tcmslogin}) {
  80. $active_user = Trog::Auth::session2user($cookies->{tcmslogin}->value);
  81. }
  82. #Disallow any paths that are naughty ( starman auto-removes .. up-traversal)
  83. if (index($path,'/templates') == 0 || $path =~ m/.*\.psgi$/i ) {
  84. return Trog::Routes::HTML::forbidden($query, \&_render);
  85. }
  86. # If it's just a file, serve it up
  87. my $alist = $env->{HTTP_ACCEPT_ENCODING} || '';
  88. $alist =~ s/\s//g;
  89. my @accept_encodings;
  90. @accept_encodings = split(/,/, $alist);
  91. my $deflate = grep { 'deflate' eq $_ } @accept_encodings;
  92. return _serve("www/$path", $env->{'psgi.streaming'}, $last_fetch, $deflate) if -f "www/$path";
  93. #Handle regex/capture routes
  94. if (!exists $routes{$path}) {
  95. my @captures;
  96. foreach my $pattern (keys(%routes)) {
  97. @captures = $path =~ m/^$pattern$/;
  98. if (@captures) {
  99. $path = $pattern;
  100. foreach my $field (@{$routes{$path}{captures}}) {
  101. $routes{$path}{data} //= {};
  102. $routes{$path}{data}{$field} = shift @captures;
  103. }
  104. last;
  105. }
  106. }
  107. }
  108. $query->{deflate} = $deflate;
  109. $query->{user} = $active_user;
  110. return Trog::Routes::HTML::notfound($query, \&_render) unless exists $routes{$path};
  111. return Trog::Routes::HTML::badrequest($query, \&_render) unless grep { $env->{REQUEST_METHOD} eq $_ } ($routes{$path}{method},'HEAD');
  112. @{$query}{keys(%{$routes{$path}{'data'}})} = values(%{$routes{$path}{'data'}}) if ref $routes{$path}{'data'} eq 'HASH' && %{$routes{$path}{'data'}};
  113. #Actually parse the POSTDATA and dump it into the QUERY object if this is a POST
  114. if ($env->{REQUEST_METHOD} eq 'POST') {
  115. my $body = HTTP::Body->new( $env->{CONTENT_TYPE}, $env->{CONTENT_LENGTH} );
  116. while ( read($env->{'psgi.input'}, my $buf, $CHUNK_SIZE) ) {
  117. $body->add($buf);
  118. }
  119. @$query{keys(%{$body->param})} = values(%{$body->param});
  120. @$query{keys(%{$body->upload})} = values(%{$body->upload});
  121. }
  122. #Set various things we don't want overridden
  123. $query->{acls} = Trog::Auth::acls4user($active_user) // [] if $active_user;
  124. $query->{user} = $active_user;
  125. $query->{domain} = $env->{HTTP_X_FORWARDED_HOST} || $env->{HTTP_HOST};
  126. $query->{route} = $path;
  127. #$query->{route} = $env->{REQUEST_URI};
  128. #$query->{route} =~ s/\?\Q$env->{QUERY_STRING}\E//;
  129. $query->{scheme} = $env->{'psgi.url_scheme'} // 'http';
  130. $query->{social_meta} = 1;
  131. $query->{primary_post} = {};
  132. #XXX there is a trick to now use strict refs, but I don't remember it right at the moment
  133. {
  134. no strict 'refs';
  135. my $output = $routes{$path}{callback}->($query, \&_render);
  136. return $output;
  137. }
  138. };
  139. sub _serve ($path, $streaming=0, $last_fetch=0, $deflate=0) {
  140. my $mf = Mojo::File->new($path);
  141. my $ext = '.'.$mf->extname();
  142. my $ft;
  143. if ($ext) {
  144. $ft = Plack::MIME->mime_type($ext) if $ext;
  145. $ft ||= $extra_types{$ext} if exists $extra_types{$ext};
  146. }
  147. $ft ||= $content_types{plain};
  148. my @headers = ($ct => $ft);
  149. #TODO use static Cache-Control for everything but JS/CSS?
  150. push(@headers,$cc => $cache_control{revalidate});
  151. #TODO Return 304 unchanged for files that haven't changed since the requestor reports they last fetched
  152. my $mt = (stat($path))[9];
  153. my $sz = (stat(_))[7];
  154. my @gm = gmtime($mt);
  155. my $now_string = strftime( "%a, %d %b %Y %H:%M:%S GMT", @gm );
  156. my $code = $mt > $last_fetch ? 200 : 304;
  157. #XXX something broken about the above logic
  158. $code=200;
  159. #XXX doing metadata=preload on videos doesn't work right?
  160. #push(@headers, "Content-Length: $sz");
  161. push(@headers, "Last-Modified" => $now_string);
  162. if (open(my $fh, '<', $path)) {
  163. return sub {
  164. my $responder = shift;
  165. my $writer = $responder->([ $code, \@headers]);
  166. while ( read($fh, my $buf, $CHUNK_SIZE) ) {
  167. $writer->write($buf);
  168. }
  169. close $fh;
  170. $writer->close;
  171. } if $streaming && $sz > $CHUNK_SIZE;
  172. #Return data in the event the caller does not support deflate
  173. if (!$deflate) {
  174. push( @headers, "Content-Length" => $sz );
  175. return [ $code, \@headers, $fh];
  176. }
  177. #Compress everything less than 1MB
  178. push( @headers, "Content-Encoding" => "deflate" );
  179. my $dfh;
  180. IO::Compress::Deflate::deflate( $fh => \$dfh );
  181. print $IO::Compress::Deflate::DeflateError if $IO::Compress::Deflate::DeflateError;
  182. push( @headers, "Content-Length" => length($dfh) );
  183. return [ $code, \@headers, [$dfh]];
  184. }
  185. return [ 403, [$ct => $content_types{plain}], ["STAY OUT YOU RED MENACE"]];
  186. }
  187. sub _render ($template, $vars, @headers) {
  188. my $processor = Text::Xslate->new(
  189. path => 'www/templates',
  190. header => ['header.tx'],
  191. footer => ['footer.tx'],
  192. function => {
  193. iso8601 => sub {
  194. my $t = shift;
  195. my $dt = DateTime->from_epoch( epoch => $t );
  196. return $dt->iso8601;
  197. },
  198. strip_and_trunc => \&Trog::Utils::strip_and_trunc,
  199. },
  200. );
  201. #XXX default vars that need to be pulled from config
  202. $vars->{dir} //= 'ltr';
  203. $vars->{lang} //= 'en-US';
  204. $vars->{title} //= 'tCMS';
  205. #XXX Need to have minification detection and so forth, use LESS
  206. $vars->{stylesheets} //= [];
  207. #XXX Need to have minification detection, use Typescript
  208. $vars->{scripts} //= [];
  209. # Absolute-ize the paths for scripts & stylesheets
  210. @{$vars->{stylesheets}} = map { index($_, '/') == 0 ? $_ : "/$_" } @{$vars->{stylesheets}};
  211. @{$vars->{scripts}} = map { index($_, '/') == 0 ? $_ : "/$_" } @{$vars->{scripts}};
  212. $vars->{contenttype} //= $content_types{html};
  213. $vars->{cachecontrol} //= $cache_control{revalidate};
  214. $vars->{code} ||= 200;
  215. push(@headers, $ct => $vars->{contenttype});
  216. push(@headers, $cc => $vars->{cachecontrol}) if $vars->{cachecontrol};
  217. my $body = $processor->render($template,$vars);
  218. $body = encode_utf8($body);
  219. #Return data in the event the caller does not support deflate
  220. if (!$vars->{deflate}) {
  221. push( @headers, "Content-Length" => length($body) );
  222. return [ $vars->{code}, \@headers, [$body]];
  223. }
  224. #Compress
  225. push( @headers, "Content-Encoding" => "deflate" );
  226. #Disallow framing UNLESS we are in embed mode
  227. push( @headers, "Content-Security-Policy" => qq{frame-ancestors 'none'} ) unless $vars->{embed};
  228. my $dfh;
  229. IO::Compress::Deflate::deflate( \$body => \$dfh );
  230. print $IO::Compress::Deflate::DeflateError if $IO::Compress::Deflate::DeflateError;
  231. push( @headers, "Content-Length" => length($dfh) );
  232. return [$vars->{code}, \@headers, [$dfh]];
  233. }
  234. 1;