blib/lib/PAGI/Context/HTTP.pm

filter: Statementstmt 100% 34 / 34 Branchbran 83.3% 5 / 6 Conditioncond 86.6% 13 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 13 / 13 Podpod 100% 10 / 10 totaltotal 91.9% 80 / 87 CC 9 SCAR 11.6
CC
9
Cov
95%
respond
7.0
request
2.0
response
2.0
CRAP 3.2
« lib/PAGI/App/Cascade.pm ↑ Summary blib/lib/PAGI/Test/Response.pm »
1 package PAGI::Context::HTTP;
2 28 $PAGI::Context::HTTP::VERSION = '0.002002';
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 28 our @ISA = ('PAGI::Context');
8  
9 =encoding UTF-8
10  
11 =head1 NAME
12  
13 PAGI::Context::HTTP - HTTP-specific context subclass
14  
15 =head1 DESCRIPTION
16  
17 Returned by C<< PAGI::Context->new(...) >> when C<< $scope->{type} >> is
18 C<'http'>. Adds lazy accessors for L<PAGI::Request> and L<PAGI::Response>,
19 plus an HTTP C<method> accessor.
20  
21 Inherits all shared methods from L<PAGI::Context>.
22  
23 =head1 METHODS
24  
25 =head2 request
26  
27 my $req = $ctx->request;
28  
29 Returns a L<PAGI::Request> instance. Lazy-constructed and cached.
30  
31 =head2 response
32  
33 my $res = $ctx->response;
34  
35 Returns a detached L<PAGI::Response> accumulator. Lazy-constructed and cached
36 for the lifetime of the context. The response holds no connection — it is a
37 pure value object you mutate via the chainer methods (C<status>, C<header>,
38 C<json>, etc.) and then pass to L</respond> when ready to send.
39  
40 =head2 respond
41  
42 $ctx->respond($res);
43  
44 Guarded send. Sends the L<PAGI::Response> value C<$res> over this request's
45 connection, marks the request as done, and returns a L<Future> that resolves
46 when all protocol events have been emitted.
47  
48 The sent state is read from C<< $scope->{'pagi.connection'}->response_started >>,
49 the server-seeded per-request object — a shared reference, so the fact propagates
50 across the whole middleware stack even though middleware shallow-clone the scope.
51 A second C<respond> on the same context is rejected synchronously via a per-context
52 flag; the server enforces single-response at the protocol level as the backstop.
53  
54 Delegates to the unguarded primitive C<< $res->respond($send) >>.
55  
56 =head2 method
57  
58 my $method = $ctx->method; # 'GET', 'POST', etc.
59  
60 Returns the HTTP method from the scope.
61  
62 =head2 req
63  
64 my $req = $ctx->req;
65  
66 Alias for C<request>.
67  
68 =head2 resp
69  
70 my $res = $ctx->resp;
71  
72 Alias for C<response>.
73  
74 =head2 text, html, json, redirect
75  
76 return $ctx->text('Hello');
77 return $ctx->html('<h1>Hi</h1>');
78 return $ctx->json({ ok => 1 });
79 return $ctx->json($data, status => 201);
80 return $ctx->redirect('/login');
81  
82 Shorthands that set the body — and any trailing options such as C<status>,
83 C<headers>, or C<content_type> — on this context's L</response>, then return the
84 L<PAGI::Response> value, so a handler can build and return its response in a
85 single call. C<< $ctx->json($data) >> is exactly C<< $ctx->response->json($data) >>.
86 They operate on the one response accumulator, so you can still reach for
87 C<< $ctx->response >> to set cookies or extra headers alongside them.
88  
89 =cut
90  
91 8 ▶sub request {
Subroutine request: called CC=2 SCAR=7.0
Pod documented
92 8 my ($self) = @_;
93 8 ▶ return $self->{_request} //= do {
Condition66%2/3
$self->{"_request"} //= do { require PAGI::Request; "PAGI::Request"->new($self->{"scope"}, $self->{"receive"}) }
l!l&&r!l&&!r
260
MC/DC0%0/2
$self->{"_request"} //= do { require PAGI::Request; "PAGI::Request"->new($self->{"scope"}, $self->{"receive"}) }
$self->{"_request"} do { require PAGI::Request; "PAGI::Request"->new($self->{"scope"}, $self->{"receive"}) }
Truth table66%2/3
A //= B
A= $self->{"_request"}B= do { require PAGI::Request; "PAGI::Request"->new($self->{"scope"}, $self->{"receive"}) }
ABresult
1X1
011
000
94 6 require PAGI::Request;
95 6 PAGI::Request->new($self->{scope}, $self->{receive});
96 };
97 }
98  
99 52 ▶sub response {
Subroutine response: called CC=2 SCAR=7.0
Pod documented
100 52 my ($self) = @_;
101 52 ▶ return $self->{_response} //= do {
Condition66%2/3
$self->{"_response"} //= do { require PAGI::Response; "PAGI::Response"->new($self->{"scope"}) }
l!l&&r!l&&!r
4480
MC/DC0%0/2
$self->{"_response"} //= do { require PAGI::Response; "PAGI::Response"->new($self->{"scope"}) }
$self->{"_response"} do { require PAGI::Response; "PAGI::Response"->new($self->{"scope"}) }
Truth table66%2/3
A //= B
A= $self->{"_response"}B= do { require PAGI::Response; "PAGI::Response"->new($self->{"scope"}) }
ABresult
1X1
011
000
102 48 require PAGI::Response;
103 48 PAGI::Response->new($self->{scope}); # detached accumulator; no $send
104 };
105 }
106  
107 48 ▶sub respond {
Subroutine respond: called CC=7 SCAR=19.5
Pod documented
108 48 my ($self, $res) = @_;
109 48 ▶ my $conn = $self->{scope} ? $self->{scope}{'pagi.connection'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'scope'} ? : 48 0 48
110 48 ▶ if ($conn && !$conn->can('response_started')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn and not $conn->can("response_started")) 1 47 48
Condition100%3/3
$conn and not $conn->can("response_started")
!ll&&!rl&&r
3891
MC/DC100%2/2
$conn and not $conn->can("response_started")
$conn not $conn->can("response_started")
Truth table100%3/3
A and not(B)
A= $connB= not $conn->can("response_started")
ABresult
0X0
100
111
111 1 croak("pagi.connection lacks response_started (non-conforming server)");
112 }
113 croak("response already sent")
114 47 ▶ if $self->{_responded} || ($conn && $conn->response_started); # mutex + cross-context read
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_responded"} or $conn and $conn->response_started 3 44 47
Condition100%6/6
$self->{"_responded"} or $conn and $conn->response_started
l!l&&r!l&&!r
2144
$conn and $conn->response_started
!ll&&!rl&&r
3771
MC/DC100%3/3
$self->{"_responded"} or $conn and $conn->response_started
$self->{"_responded"} $conn $conn->response_started
Truth table100%4/4
A or B and C
A= $self->{"_responded"}B= $connC= $conn->response_started
ABCresult
1XX1
0111
00X0
0100
115 44 $self->{_responded} = 1; # synchronous, self-owned
116 44 return $res->respond($self->{send});
117 }
118  
119 14 ▶sub method { shift->{scope}{method} }
Subroutine method: called CC=1 SCAR=0.0
Pod documented
120  
121 1 ▶sub req { shift->request }
Subroutine req: called CC=1 SCAR=0.0
Pod documented
122 1 ▶sub resp { shift->response }
Subroutine resp: called CC=1 SCAR=0.0
Pod documented
123  
124 # Value-contract response shorthands: set the body (+ opts) on the cached
125 # response accumulator and return it, so handlers can `return $ctx->json(...)`.
126 2 ▶sub text { shift->response->text(@_) }
Subroutine text: called CC=1 SCAR=0.0
Pod documented
127 2 ▶sub html { shift->response->html(@_) }
Subroutine html: called CC=1 SCAR=0.0
Pod documented
128 3 ▶sub json { shift->response->json(@_) }
Subroutine json: called CC=1 SCAR=0.0
Pod documented
129 1 ▶sub redirect { shift->response->redirect(@_) }
Subroutine redirect: called CC=1 SCAR=0.0
Pod documented
130  
131 28 1;
132  
133 __END__
« lib/PAGI/App/Cascade.pm ↑ Summary blib/lib/PAGI/Test/Response.pm »