]>
Commit | Line | Data |
---|---|---|
1 | package Plack::App::Gruntmaster; | |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | our $VERSION = '5999.000_001'; | |
6 | ||
7 | use Encode qw/encode decode/; | |
8 | use File::Slurp qw/read_file/; | |
9 | use JSON::MaybeXS qw/encode_json/; | |
10 | use PerlX::Maybe; | |
11 | use Scope::Upper qw/unwind SUB UP/; | |
12 | use Web::Simple; | |
13 | ||
14 | use Gruntmaster::Data; | |
15 | use Plack::App::Gruntmaster::HTML; | |
16 | ||
17 | use warnings NONFATAL => 'all'; | |
18 | no warnings 'illegalproto'; | |
19 | ||
20 | ################################################## | |
21 | ||
22 | use constant USER_REGEX => qr/^\w{2,20}$/a; | |
23 | ||
24 | use constant CONTENT_TYPES => +{ | |
25 | c => 'text/x-csrc', | |
26 | cpp => 'text/x-c++src', | |
27 | cs => 'text/x-csharp', # Used by GNOME. Not in mime.types. | |
28 | go => 'text/plain', # ? | |
29 | gs => 'text/plain', | |
30 | hs => 'text/x-haskell', | |
31 | java => 'text/x-java', | |
32 | pas => 'text/x-pascal', | |
33 | pl => 'text/x-perl', | |
34 | py => 'text/x-python', | |
35 | rb => 'application/x-ruby', | |
36 | l => 'text/plain', | |
37 | }; | |
38 | ||
39 | use constant FORMAT_EXTENSION => { | |
40 | C => 'c', | |
41 | CPP => 'cpp', | |
42 | GCCGO => 'go', | |
43 | GOLANG => 'go', | |
44 | GOLFSCRIPT => 'gs', | |
45 | HASKELL => 'hs', | |
46 | MONO => 'cs', | |
47 | JAVA => 'java', | |
48 | PASCAL => 'pas', | |
49 | PERL => 'pl', | |
50 | PYTHON => 'py', | |
51 | RUBY => 'rb', | |
52 | SBCL => 'l', | |
53 | }; | |
54 | ||
55 | use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']]; | |
56 | ||
57 | my ($env, $privacy); | |
58 | ||
59 | sub db { $env->{'gruntmaster.dbic'} } | |
60 | ||
61 | sub remote_user { | |
62 | my $user = $env->{REMOTE_USER}; | |
63 | $user &&= db->user($user); | |
64 | $user | |
65 | } | |
66 | ||
67 | sub admin { remote_user && remote_user->admin } | |
68 | sub contest { db->contest ($_{contest}) } | |
69 | sub problem { db->problem ($_{problem}) } | |
70 | sub job { db->job ($_{job}) } | |
71 | sub user { db->user ($_{user}) } | |
72 | ||
73 | sub redirect { [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] } | |
74 | sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] } | |
75 | sub response { | |
76 | my ($template, $title, $params, $maxage) = @_; | |
77 | unless ($params) { | |
78 | $params = $title; | |
79 | $title = 'No title'; | |
80 | } | |
81 | $params->{time} = time; | |
82 | $params->{args} = {%_}; | |
83 | bless {template => $template, title => $title, params => $params, maxage => ($maxage // 3600)}, __PACKAGE__.'::Response' | |
84 | } | |
85 | ||
86 | sub forbid { | |
87 | my ($condition) = @_; | |
88 | $privacy = 'private' if $condition; | |
89 | return if !$condition || admin; | |
90 | unwind $env->{authcomplex}->unauthorized, SUB UP | |
91 | } | |
92 | ||
93 | sub dispatch_request{ | |
94 | $env = $_[PSGI_ENV]; | |
95 | $privacy = 'public'; | |
96 | ||
97 | sub (GET) { | |
98 | sub (/robots.txt) { NOT_FOUND }, | |
99 | ||
100 | sub (/src/:job) { | |
101 | return NOT_FOUND if !job; | |
102 | my $isowner = remote_user && remote_user->id eq job->rawowner; | |
103 | my $private = job->private || job->problem->private || job->contest && job->contest->is_running; | |
104 | forbid !$isowner && $private; | |
105 | my $privacy = $private ? 'private' : 'public'; | |
106 | my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => CONTENT_TYPES->{job->extension}); | |
107 | push @headers, (Vary => 'Authorization') if $private; | |
108 | [200, \@headers, [job->source]] | |
109 | }, | |
110 | ||
111 | sub (?:format~) { | |
112 | my $format = lc ($_{format} // ''); | |
113 | response_filter { | |
114 | my ($r) = @_; | |
115 | return $r if ref $r ne 'Plack::App::Gruntmaster::Response'; | |
116 | my $vary = 'X-Static'; | |
117 | $vary .= ', Authorization' if $privacy eq 'private'; | |
118 | my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}", Vary => $vary); | |
119 | return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json'; | |
120 | my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}}, maybe static => $env->{HTTP_X_STATIC}; | |
121 | [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]] | |
122 | }, | |
123 | }, | |
124 | ||
125 | sub (/st/:contest) { | |
126 | response st => 'Standings', { | |
127 | st => [ contest->standings ], | |
128 | problems => [ | |
129 | map { [$_->id, $_->name] } | |
130 | sort { $a->value <=> $b->value } | |
131 | map { $_->problem } contest->contest_problems], | |
132 | }, 10 | |
133 | }, | |
134 | ||
135 | sub (/ed/:contest) { | |
136 | forbid !contest->is_finished; | |
137 | my $pblist = db->problem_list(contest => $_{contest}, solution => 1); | |
138 | response ed => 'Editorial of ' . contest->name, {%$pblist, editorial => contest->editorial}, contest->is_finished(time - 86400) ? 60 : (); | |
139 | }, | |
140 | ||
141 | sub (/login) { | |
142 | forbid !remote_user; | |
143 | [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary => 'Authorization'], [$env->{REMOTE_USER}]] | |
144 | }, | |
145 | ||
146 | sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" }, | |
147 | ||
148 | sub (/us/) { response us => 'Users', {us => db->user_list} }, | |
149 | sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_), 300 }, | |
150 | sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) { | |
151 | forbid $_{private}; | |
152 | response log => 'Job list', db->job_list(%_), 5 | |
153 | }, | |
154 | sub (/pb/ + ?:owner~&:contest~&:private~) { | |
155 | forbid $_{private}; | |
156 | forbid contest && contest->is_pending; | |
157 | response pb => 'Problems', db->problem_list(%_) | |
158 | }, | |
159 | ||
160 | sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) }, | |
161 | sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}), 60 }, | |
162 | sub (/log/:job) { | |
163 | forbid job->private; | |
164 | response log_entry => "Job $_{job}", db->job_entry($_{job}), 10 | |
165 | }, | |
166 | sub (/pb/:problem + ?contest~) { | |
167 | my (undef, undef, $contest) = @_; | |
168 | $_{contest} = $contest; | |
169 | return NOT_FOUND if contest && !db->contest_problems->find($_{contest}, $_{problem}); | |
170 | forbid problem->private && !contest; | |
171 | if (contest) { | |
172 | return redirect "/pb/$_{problem}" unless contest->is_running; | |
173 | forbid !remote_user; | |
174 | $privacy = 'private'; | |
175 | } | |
176 | response pb_entry => problem->name, db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id), $_{contest} ? 10 : (); | |
177 | }, | |
178 | sub (/sol/:problem) { | |
179 | forbid problem->private; | |
180 | response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution}; | |
181 | }, | |
182 | ||
183 | sub (/) { redispatch_to '/index' }, | |
184 | sub (/favicon.ico) { redirect '/static/favicon.ico' }, | |
185 | sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=3600', 'X-Forever' => 1, Vary => 'X-Static'], [render_article $_{article}, 'en', maybe static => $env->{HTTP_X_STATIC}]] } | |
186 | }, | |
187 | ||
188 | sub (POST) { | |
189 | sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) { | |
190 | my (undef, undef, $prog) = @_; | |
191 | forbid !remote_user; | |
192 | my $private = (problem->private && !contest) ? 1 : 0; | |
193 | return reply 'This contest has finished' if contest && contest->is_finished; | |
194 | return reply 'This contest has not yet started' if contest && contest->is_pending; | |
195 | return reply 'This problem is private' if !admin && $private; | |
196 | return reply 'This problem does not belong to this contest' if contest && !db->contest_problems->find($_{contest}, $_{problem}); | |
197 | return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024; | |
198 | return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30; | |
199 | remote_user->update({lastjob => time}); | |
200 | ||
201 | my $source = $prog ? read_file $prog->path : $_{source_code}; | |
202 | unlink $prog->path if $prog; | |
203 | my $newjob = db->jobs->create({ | |
204 | maybe contest => $_{contest}, | |
205 | private => $private, | |
206 | date => time, | |
207 | extension => FORMAT_EXTENSION->{$_{prog_format}}, | |
208 | format => $_{prog_format}, | |
209 | problem => $_{problem}, | |
210 | source => $source, | |
211 | owner => remote_user->id, | |
212 | }); | |
213 | ||
214 | [303, [Location => '/log/' . $newjob->id], []] | |
215 | }, | |
216 | } | |
217 | } | |
218 | ||
219 | ||
220 | 1; | |
221 | __END__ |