line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::RequestId; |
2
|
2
|
|
|
2
|
|
13687
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
47
|
|
3
|
2
|
|
|
2
|
|
7
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
41
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
427
|
use parent 'Plack::Middleware'; |
|
2
|
|
|
|
|
219
|
|
|
2
|
|
|
|
|
9
|
|
6
|
2
|
|
|
2
|
|
11242
|
use Plack::Util; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
40
|
|
7
|
2
|
|
|
|
|
7
|
use Plack::Util::Accessor qw/ |
8
|
|
|
|
|
|
|
psgi_env_key |
9
|
|
|
|
|
|
|
http_header |
10
|
|
|
|
|
|
|
req_http_header |
11
|
|
|
|
|
|
|
id_generator |
12
|
|
|
|
|
|
|
force_generate_id |
13
|
|
|
|
|
|
|
env_key |
14
|
2
|
|
|
2
|
|
6
|
/; |
|
2
|
|
|
|
|
3
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $request_id; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub prepare_app { |
21
|
7
|
|
|
7
|
1
|
8464
|
my ($self) = @_; |
22
|
|
|
|
|
|
|
|
23
|
7
|
100
|
|
|
|
11
|
unless ($self->psgi_env_key) { |
24
|
6
|
|
|
|
|
56
|
$self->psgi_env_key('psgix.request_id'); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
7
|
100
|
|
|
|
31
|
unless ($self->http_header) { |
28
|
5
|
|
|
|
|
18
|
$self->http_header('X-Request-Id'); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
7
|
|
|
|
|
26
|
my $req_http_header = 'HTTP_'. uc $self->http_header; |
32
|
7
|
|
|
|
|
34
|
$req_http_header =~ s/-/_/g; |
33
|
7
|
|
|
|
|
10
|
$self->req_http_header($req_http_header); |
34
|
|
|
|
|
|
|
|
35
|
7
|
100
|
|
|
|
25
|
unless ($self->id_generator) { |
36
|
3
|
|
|
|
|
439
|
require Data::UUID; |
37
|
3
|
|
|
|
|
607
|
Data::UUID->import; |
38
|
3
|
|
|
|
|
738
|
$self->{_uuid_obj} = Data::UUID->new; |
39
|
|
|
|
|
|
|
$self->id_generator(sub { |
40
|
2
|
|
|
2
|
|
141
|
substr $self->{_uuid_obj}->create_hex, 2, 32; |
41
|
3
|
|
|
|
|
21
|
}); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub call { |
46
|
7
|
|
|
7
|
1
|
14171
|
my($self, $env) = @_; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$request_id |
49
|
|
|
|
|
|
|
= $env->{$self->psgi_env_key} |
50
|
|
|
|
|
|
|
= (!$self->force_generate_id && $env->{$self->req_http_header}) |
51
|
7
|
100
|
100
|
|
|
15
|
? $env->{$self->req_http_header} |
52
|
|
|
|
|
|
|
: $self->id_generator->($env); |
53
|
|
|
|
|
|
|
|
54
|
7
|
100
|
|
|
|
86
|
if ($self->env_key) { |
55
|
1
|
|
|
|
|
4
|
$ENV{$self->env_key} = $request_id; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
7
|
|
|
|
|
36
|
my $res = $self->app->($env); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$self->response_cb($res, sub { |
61
|
7
|
|
|
7
|
|
68
|
my $res = shift; |
62
|
7
|
50
|
|
|
|
12
|
if ($res) { |
63
|
|
|
|
|
|
|
Plack::Util::header_push( |
64
|
|
|
|
|
|
|
$res->[1], |
65
|
|
|
|
|
|
|
$self->http_header, |
66
|
7
|
|
|
|
|
14
|
$env->{$self->psgi_env_key}, |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
} |
69
|
7
|
|
|
|
|
307
|
}); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
__END__ |