| 1 | package Crop::Server::FastCGI; | ||
| 2 | use base qw/ Crop::Server /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Server::FastCGI | ||
| 6 | Server uses FastCGI protocol. | ||
| 7 | =cut | ||
| 8 | |||
| 9 | use v5.14; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | use CGI::Fast; | ||
| 13 | |||
| 14 | use Crop::Debug; | ||
| 15 | use Crop::HTTP::Request::FastCGI; | ||
| 16 | |||
| 17 | =begin nd | ||
| 18 | Variable: our %Attributes | ||
| 19 | Class attributes: | ||
| 20 | |||
| 21 | cgi - object of Common Gateway Interface | ||
| 22 | =cut | ||
| 23 | our %Attributes = ( | ||
| 24 | cgi => {mode => 'read'}, | ||
| 25 | ); | ||
| 26 | |||
| 27 | =begin nd | ||
| 28 | Method: _flush ( ) | ||
| 29 | Send the result back to a client. | ||
| 30 | =cut | ||
| 31 | sub _flush { | ||
| 32 | my $self = shift; | ||
| 33 | |||
| 34 | $self->{redirect} // print $self->{cgi}->header(@{$self->{headers}}), $self->{content}; | ||
| 35 | } | ||
| 36 | |||
| 37 | =begin | ||
| 38 | Method: _init_httprequest ( ) | ||
| 39 | Create Request object. | ||
| 40 | |||
| 41 | Set start time to Request. | ||
| 42 | =cut | ||
| 43 | sub _init_httprequest { | ||
| 44 | my $self = shift; | ||
| 45 | |||
| 46 | $self->{request} = Crop::HTTP::Request::FastCGI->new(tik => $self->{tik}->timestamp); | ||
| 47 | } | ||
| 48 | |||
| 49 | =begin nd | ||
| 50 | Method: listen ( ) | ||
| 51 | Main loop is specific for Gateway Interface. | ||
| 52 | |||
| 53 | Returns: | ||
| 54 | Nerver returns. | ||
| 55 | =cut | ||
| 56 | sub listen { | ||
| 57 | my $self = shift; | ||
| 58 | |||
| 59 | while ($self->{cgi} = CGI::Fast->new) { | ||
| 60 | $self->_work; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | =begin nd | ||
| 65 | Method: _redirect ( ) | ||
| 66 | Send redirect to a client. | ||
| 67 | =cut | ||
| 68 | sub _redirect { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | print $self->{cgi}->redirect( | ||
| 72 | -uri => $self->{redirect}, | ||
| 73 | -status => 303, | ||
| 74 | ); | ||
| 75 | } | ||
| 76 | |||
| 77 | =begin nd | ||
| 78 | Method: _sendfile ( ) | ||
| 79 | Send X-Accel-Redirect to Nginx. | ||
| 80 | |||
| 81 | This method fetches file data from an structure of <Crop::File::Send> type stored in the 'Server.sendfile' attribute. | ||
| 82 | =cut | ||
| 83 | sub _sendfile { | ||
| 84 | my $self = shift; | ||
| 85 | my $sendfile = $self->sendfile; | ||
| 86 | |||
| 87 | print $self->{cgi}->redirect( | ||
| 88 | "-X-Accel-Redirect" => $sendfile->url, | ||
| 89 | "Content-Disposition" => $sendfile->disposition . '; filename=' . $sendfile->name, | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | |||
| 93 | =begin nd | ||
| 94 | Method: upload_size ($field) | ||
| 95 | Get upload from multipart-form. | ||
| 96 | |||
| 97 | Parameters: | ||
| 98 | $field - name of field in multipart-form | ||
| 99 | |||
| 100 | Returns: | ||
| 101 | Size in bytes. | ||
| 102 | =cut | ||
| 103 | sub upload_size { | ||
| 104 | my ($self, $field) = @_; | ||
| 105 | |||
| 106 | -s $self->cgi->upload($field); | ||
| 107 | } | ||
| 108 | |||
| 109 | 1; |