Skip to content

Instantly share code, notes, and snippets.

@mattn
Created June 9, 2009 13:40
Show Gist options
  • Save mattn/126487 to your computer and use it in GitHub Desktop.
Save mattn/126487 to your computer and use it in GitHub Desktop.
Index: lib/background.pl
===================================================================
--- lib/background.pl (revision 15)
+++ lib/background.pl (working copy)
@@ -6,6 +6,7 @@
my $pipe = IO::Pipe->new;
sub start_job_server{
+ return if $ENV{'GATEWAY_INTERFACE'};
return if $ENV{'SINATRA_ENVIRONMENT'} eq "TASK";
my $pid = fork;
if (! defined $pid) {
@@ -43,4 +44,4 @@
}
}
-1;
\ No newline at end of file
+;
Index: lib/sinatra.pl
===================================================================
--- lib/sinatra.pl (revision 15)
+++ lib/sinatra.pl (working copy)
@@ -203,7 +203,10 @@
delete $self->{headers}->{'Content-type'};
}
$self->header('Content-length', length($self->body()));
- my $output = 'HTTP/1.1 ' . $self->status . " " . status_message($self->status) . "\n";
+ my $output = '';
+ unless ($ENV{'GATEWAY_INTERFACE'}) {
+ $output = 'HTTP/1.1 ' . $self->status . " " . status_message($self->status) . "\n";
+ }
$output .= join("\n", map {$_ . ": " . $self->{headers}->{$_}} keys %{$self->{headers}}) . "\n\n";
$output .= ($self->body || '');
return $output;
@@ -263,6 +266,14 @@
}
}
+sub run_cgi {
+ my $self = shift;
+ use CGI;
+
+ $ENV{'QUERY_STRING'} = ((split(/\?/,$ENV{'REQUEST_URI'}))[1] || '') if ($ENV{'QUERY_STRING'} eq "");
+ print $self->dispatch(CGI->new())->output;
+}
+
sub define_event{
my ($self, $method, $path, $options, $code) = @_;
push(@{$events{$method}}, Sinatra::Event->new($path, $options, $code));
@@ -364,23 +375,27 @@
$ENV{'SINATRA_ENVIRONMENT'} = 'task' if defined $ARGV[0];
}
END {
- my $taskname = 'task_' . ($ARGV[0] || '');
- if (main->can($taskname)) {
- print "Running task " . $ARGV[0] . " on pid:" . $$ . "\n";
- set_option('in_task', 1);
- main->$taskname();
- } else{
- $application->run();
- }
+ if ($ENV{'GATEWAY_INTERFACE'}) {
+ $application->run_cgi();
+ } else {
+ my $taskname = 'task_' . ($ARGV[0] || '');
+ if (main->can($taskname)) {
+ print "Running task " . $ARGV[0] . " on pid:" . $$ . "\n";
+ set_option('in_task', 1);
+ main->$taskname();
+ } else{
+ $application->run_cgi();
+ }
+ }
}
#extensions for CGI::Minimal to support certain methods
-package CGI::Minimal;
+package CGI;
sub request_method {return $ENV{'REQUEST_METHOD'};}
sub path_info {return $ENV{'PATH_INFO'};}
sub user_agent {return $ENV{'HTTP_USER_AGENT'};}
sub remote_host {return $ENV{'REMOTE_HOST'} || $ENV{'REMOTE_ADDR'} || 'localhost';}
-sub request_uri{return $ENV{'REQUEST_URI'};}
+sub request_uri{return ($ENV{'REQUEST_URI'} =~ /^\Q$ENV{'SCRIPT_NAME'}(.*)\E$/)[0] || $ENV{'REQUEST_URI'}; }
sub path{(split(/\?/,request_uri))[0];}
-1; #the magnificent always return true
\ No newline at end of file
+1; #the magnificent always return true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment