| 1 | package Apache::Mojo; | ||
| 2 | our $VERSION = '0.003'; | ||
| 3 | |||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Apache::Constants qw(OK); | ||
| 9 | use Apache::URI; | ||
| 10 | |||
| 11 | use Mojo::Loader; | ||
| 12 | |||
| 13 | |||
| 14 | my $_app = undef; | ||
| 15 | |||
| 16 | sub _app { | ||
| 17 | if($ENV{MOJO_RELOAD} and $_app) { | ||
| 18 | Mojo::Loader->reload; | ||
| 19 | $_app = undef; | ||
| 20 | } | ||
| 21 | $_app ||= Mojo::Loader->load_build($ENV{MOJO_APP} || 'Mojo::HelloWorld'); | ||
| 22 | return $_app; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub handler { | ||
| 26 | my $r = shift; | ||
| 27 | |||
| 28 | chdir($ENV{MOJO_PATH}) if $ENV{MOJO_PATH}; | ||
| 29 | |||
| 30 | my $app = _app; | ||
| 31 | my $tx = $app->build_tx; | ||
| 32 | my $req = $tx->req; | ||
| 33 | |||
| 34 | # Request | ||
| 35 | _request($r, $req); | ||
| 36 | |||
| 37 | # Handler | ||
| 38 | $app->handler($tx); | ||
| 39 | |||
| 40 | my $res = $tx->res; | ||
| 41 | |||
| 42 | # Response | ||
| 43 | _response($r, $res); | ||
| 44 | |||
| 45 | return OK; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub _request { | ||
| 49 | my ($r, $req) = @_; | ||
| 50 | |||
| 51 | my $url = $req->url; | ||
| 52 | my $base = $url->base; | ||
| 53 | |||
| 54 | # headers | ||
| 55 | my $headers = $r->headers_in; | ||
| 56 | foreach my $key (keys %$headers) { | ||
| 57 | $req->headers->header($key, $headers->get($key)); | ||
| 58 | } | ||
| 59 | |||
| 60 | # path | ||
| 61 | $url->path->parse($r->path_info); | ||
| 62 | |||
| 63 | # query | ||
| 64 | $url->query->parse($r->parsed_uri->query); | ||
| 65 | |||
| 66 | # method | ||
| 67 | $req->method($r->method); | ||
| 68 | |||
| 69 | # base path | ||
| 70 | $base->path->parse($r->location); | ||
| 71 | |||
| 72 | # host/port | ||
| 73 | my $host = $r->get_server_name; | ||
| 74 | my $port = $r->get_server_port; | ||
| 75 | $url->host($host); | ||
| 76 | $url->port($port); | ||
| 77 | $base->host($host); | ||
| 78 | $base->port($port); | ||
| 79 | |||
| 80 | # scheme | ||
| 81 | $url->scheme($r->parsed_uri->scheme); | ||
| 82 | $base->scheme($r->parsed_uri->scheme); | ||
| 83 | |||
| 84 | # version | ||
| 85 | if ($r->protocol =~ m|^HTTP/(\d+\.\d+)$|) { | ||
| 86 | $req->version($1); | ||
| 87 | } else { | ||
| 88 | $req->version('0.9'); | ||
| 89 | } | ||
| 90 | |||
| 91 | # body | ||
| 92 | $req->state('content'); | ||
| 93 | $req->content->state('body'); | ||
| 94 | |||
| 95 | # handle application/x-www-form-encoded content | ||
| 96 | my $post_params = $r->content; | ||
| 97 | $req->parse($post_params) if $post_params; | ||
| 98 | |||
| 99 | # handle any body content that isn't application/x-www-form-encoded | ||
| 100 | my $offset = 0; | ||
| 101 | while (!$req->is_state(qw/done error/)) { | ||
| 102 | last unless (my $read = $r->read(my $buffer, 4096, $offset)); | ||
| 103 | $offset += $read; | ||
| 104 | $req->parse($buffer); | ||
| 105 | } | ||
| 106 | |||
| 107 | } | ||
| 108 | |||
| 109 | sub _response { | ||
| 110 | my ($r, $res) = @_; | ||
| 111 | |||
| 112 | # status | ||
| 113 | $r->status($res->code); | ||
| 114 | |||
| 115 | # headers | ||
| 116 | my $headers = $res->headers; | ||
| 117 | foreach my $key (@{$headers->names}) { | ||
| 118 | my @value = $headers->header($key); | ||
| 119 | next unless @value; | ||
| 120 | $r->headers_out->set($key => shift @value); | ||
| 121 | $r->headers_out->add($key => $_) foreach (@value); | ||
| 122 | } | ||
| 123 | |||
| 124 | # content-type gets ignored in headers_out() | ||
| 125 | $r->content_type($headers->header('Content-Type')); | ||
| 126 | |||
| 127 | # Response body | ||
| 128 | my $offset = 0; | ||
| 129 | while (1) { | ||
| 130 | my $chunk = $res->get_body_chunk($offset); | ||
| 131 | |||
| 132 | # No content yet, try again | ||
| 133 | unless (defined $chunk) { | ||
| 134 | sleep 1; | ||
| 135 | next; | ||
| 136 | } | ||
| 137 | |||
| 138 | # End of content | ||
| 139 | last unless length $chunk; | ||
| 140 | |||
| 141 | # Content | ||
| 142 | my $written = STDOUT->syswrite($chunk); | ||
| 143 | $offset += $written; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | 1; | ||
| 149 | |||
| 150 | __END__ |