| 1 | package Langertha::Knarr::PSGI::FakeReq; | ||||||||||||||
| 2 | # ABSTRACT: Header access on a PSGI environment, shaped like a Net::Async::HTTP::Server request | ||||||||||||||
| 3 | 28 | our $VERSION = '1.102'; | |||||||||||||
| 4 | 28 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 28 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | |||||||||||||||
| 8 | 501 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 9 | 501 | my ($class, $env) = @_; | |||||||||||||
| 10 | 501 | return bless { env => $env }, $class; | |||||||||||||
| 11 | } | ||||||||||||||
| 12 | |||||||||||||||
| 13 | |||||||||||||||
| 14 | 83 | ▶ | sub path { $_[0]{env}{PATH_INFO} // '/' } | ||||||||||||
|
Subroutine
path: called CC=2 SCAR=7.6
Pod
documented
Condition50%1/2
$_[0]{"env"}{"PATH_INFO"} // "/"
MC/DC0%0/1
$_[0]{"env"}{"PATH_INFO"} // "/"
$_[0]{"env"}{"PATH_INFO"}
Truth table50%1/2
A
A= $_[0]{"env"}{"PATH_INFO"}
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | |||||||||||||||
| 17 | 853 | ▶ | sub header { | ||||||||||||
|
Subroutine
header: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 18 | 853 | my ($self, $name) = @_; | |||||||||||||
| 19 | 853 | ( my $key = uc $name ) =~ tr/-/_/; | |||||||||||||
| 20 | 853 | return $self->{env}{"HTTP_$key"}; | |||||||||||||
| 21 | } | ||||||||||||||
| 22 | |||||||||||||||
| 23 | |||||||||||||||
| 24 | # The request headers as [ name, value ] pairs, like | ||||||||||||||
| 25 | # Net::Async::HTTP::Server::Request->headers. PSGI keeps only the CGI form | ||||||||||||||
| 26 | # of a header name, so it comes back lower-cased with dashes (x-api-key). | ||||||||||||||
| 27 | 96 | ▶ | sub headers { | ||||||||||||
|
Subroutine
headers: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 28 | 96 | my ($self) = @_; | |||||||||||||
| 29 | 96 | my $env = $self->{env}; | |||||||||||||
| 30 | 96 | my @pairs; | |||||||||||||
| 31 | 96 | for my $key ( sort keys %$env ) { | |||||||||||||
| 32 | 2356 | ▶ | my $name = $key =~ /\AHTTP_(.+)\z/ ? $1 | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 33 | : $key =~ /\A(CONTENT_TYPE)\z/ ? $1 : next; | ||||||||||||||
| 34 | 340 | ( $name = lc $name ) =~ tr/_/-/; | |||||||||||||
| 35 | 340 | push @pairs, [ $name, $env->{$key} ]; | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | 96 | return @pairs; | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | |||||||||||||||
| 41 | 28 | 1; | |||||||||||||
| 42 | |||||||||||||||
| 43 | __END__ | ||||||||||||||
| 44 | |||||||||||||||
| 45 | =pod | ||||||||||||||
| 46 | |||||||||||||||
| 47 | =encoding UTF-8 | ||||||||||||||
| 48 | |||||||||||||||
| 49 | =head1 NAME | ||||||||||||||
| 50 | |||||||||||||||
| 51 | Langertha::Knarr::PSGI::FakeReq - Header access on a PSGI environment, shaped like a Net::Async::HTTP::Server request | ||||||||||||||
| 52 | |||||||||||||||
| 53 | =head1 VERSION | ||||||||||||||
| 54 | |||||||||||||||
| 55 | version 1.102 | ||||||||||||||
| 56 | |||||||||||||||
| 57 | =head1 DESCRIPTION | ||||||||||||||
| 58 | |||||||||||||||
| 59 | Internal to L<Langertha::Knarr::PSGI>: wraps a PSGI C<$env> so the Knarr | ||||||||||||||
| 60 | code shared with the native server (auth check, protocol parsers, raw | ||||||||||||||
| 61 | passthrough) can read request headers and the path the way it reads them from a | ||||||||||||||
| 62 | L<Net::Async::HTTP::Server::Request>. | ||||||||||||||
| 63 | |||||||||||||||
| 64 | =head2 new | ||||||||||||||
| 65 | |||||||||||||||
| 66 | my $req = Langertha::Knarr::PSGI::FakeReq->new($env); | ||||||||||||||
| 67 | |||||||||||||||
| 68 | =head2 path | ||||||||||||||
| 69 | |||||||||||||||
| 70 | my $path = $req->path; # /api/generate | ||||||||||||||
| 71 | |||||||||||||||
| 72 | The request path (C<PATH_INFO>), like | ||||||||||||||
| 73 | L<Net::Async::HTTP::Server::Request/path>; the Ollama protocol reads it to | ||||||||||||||
| 74 | tell C</api/generate> from C</api/chat>. | ||||||||||||||
| 75 | |||||||||||||||
| 76 | =head2 header | ||||||||||||||
| 77 | |||||||||||||||
| 78 | my $value = $req->header('x-api-key'); | ||||||||||||||
| 79 | |||||||||||||||
| 80 | The value of one request header, C<undef> when it was not sent. | ||||||||||||||
| 81 | |||||||||||||||
| 82 | =head2 headers | ||||||||||||||
| 83 | |||||||||||||||
| 84 | for my $pair ( $req->headers ) { my ($name, $value) = @$pair; ... } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | All request headers as C<[ name, value ]> pairs, names lower-cased with | ||||||||||||||
| 87 | dashes (PSGI keeps only the CGI form of a name). | ||||||||||||||
| 88 | |||||||||||||||
| 89 | =head1 SUPPORT | ||||||||||||||
| 90 | |||||||||||||||
| 91 | =head2 Issues | ||||||||||||||
| 92 | |||||||||||||||
| 93 | Please report bugs and feature requests on GitHub at | ||||||||||||||
| 94 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||
| 95 | |||||||||||||||
| 96 | =head2 IRC | ||||||||||||||
| 97 | |||||||||||||||
| 98 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =head1 CONTRIBUTING | ||||||||||||||
| 101 | |||||||||||||||
| 102 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||
| 103 | |||||||||||||||
| 104 | =head1 AUTHOR | ||||||||||||||
| 105 | |||||||||||||||
| 106 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 109 | |||||||||||||||
| 110 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||
| 111 | |||||||||||||||
| 112 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 113 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 114 | |||||||||||||||
| 115 | =cut | ||||||||||||||