-
-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathRenderViaRPC.pm
More file actions
91 lines (71 loc) · 2.88 KB
/
Copy pathRenderViaRPC.pm
File metadata and controls
91 lines (71 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package WeBWorK::ContentGenerator::RenderViaRPC;
use Mojo::Base 'WeBWorK::ContentGenerator', -signatures, -async_await;
=head1 NAME
WeBWorK::ContentGenerator::RenderViaRPC - This is a content generator that
processes requests for problem rendering via remote procedure calls to the
webwork webservice.
=head1 Description
Receives WeBWorK requests presented as HTML forms, containing the requisite
information for rendering a problem. This package checks that authentication
succeeded, calls WebworkWebservice::RenderProblem::renderProblem, and then
passes its return value to FormatRenderedProblem::formatRenderedProblem. The
result is returned in the JSON or HTML format as determined by the request type.
=cut
use WebworkWebservice;
sub initializeRoute ($c, $routeCaptures) {
$c->{rpc} = 1;
my $allow_unsecured_rpc = $c->config('allow_unsecured_rpc');
my $disable_cookies = 0;
if ($allow_unsecured_rpc) {
if (ref($allow_unsecured_rpc) eq 'HASH') {
my $courseID = $c->param('courseID');
if ($courseID && $allow_unsecured_rpc->{$courseID}) {
if (ref($allow_unsecured_rpc->{$courseID}) eq 'HASH') {
my $userID = $c->param('user');
if ($userID && $allow_unsecured_rpc->{$courseID}{$userID}) {
$disable_cookies = 1;
}
} else {
$disable_cookies = 1;
}
}
} else {
$disable_cookies = 1;
}
}
$c->stash(disable_cookies => 1)
if $c->current_route eq 'render_rpc' && $c->param('disableCookies') && $disable_cookies;
# Get the courseID from the parameters.
$routeCaptures->{courseID} = $c->stash->{courseID} = $c->param('courseID') if $c->param('courseID');
return;
}
async sub pre_header_initialize ($c) {
$c->{wantsjson} = ($c->param('outputformat') // '') eq 'json' || ($c->param('send_pg_flags') // 0);
unless ($c->authen->was_verified) {
$c->{output} =
$c->{wantsjson}
? { error => $c->maketext('Authentication failed. Log in again to continue.') }
: $c->maketext('Authentication failed. Log in again to continue.');
return;
}
$c->param('displayMode', 'tex')
if $c->param('outputformat') && ($c->param('outputformat') eq 'pdf' || $c->param('outputformat') eq 'tex');
# Call the WebworkWebservice to render the problem and store the result in $c->return_object.
my $rpc_service = WebworkWebservice->new($c);
await $rpc_service->rpc_execute('renderProblem');
if ($rpc_service->error_string) {
$c->{output} = $c->{wantsjson} ? { error => $rpc_service->error_string } : $rpc_service->error_string;
return;
}
# Format the return in the requested format. A response is rendered unless there is an error.
$c->{output} = $rpc_service->formatRenderedProblem;
return;
}
sub content ($c) {
# If there were no errors a response will have been rendered. Return in that case.
return if $c->res->code;
# Handle rendering of errors.
return $c->render(json => $c->{output}) if $c->{wantsjson};
return $c->render(text => $c->{output});
}
1;