设置PHP Mime类型?

时间:2015-04-10 15:08:46

标签: php perl mime

我正在尝试与Web服务器等实际操作,所以我决定看看我能用旧的perl Web服务器做些什么。我需要用httpd-php提供php文件而不仅仅是纯文本,在google上做一些挖掘后我发现" application / x-httpd-php"应该做的,但它不是..

jpg image/jpeg
html    text/html
htm text/html
pdf application/pdf
gif image/gif
pl  text/plain
sh  text/plain
txt text/plain
mpeg    video/mpeg
pas text/plain
php     application/x-httpd-php

如何告诉mime使用PHP提供php文件?

#!/usr/bin/perl

#
# $Header: /export/cvs/www.ogris.de/httpd/httpd.pl_,v 1.2 2002/11/10 22:55:45 fjo Exp $
#

use IO::Socket;

$PROGRAM_NAME = "Simple-HTTP";
$PIDFILE = "$PROGRAM_NAME.pid";
$TIMEZONE = "+0200";
%HTTPCODE = (
  200 => "Ok",
  404 => "Not found"
);
@MONTH = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
$PORT = 8001;
$DEFAULT_MIME = "text/plain";
$MIME = $ARGV[0] or $MIME = "mime.txt";

$SIG{TERM} = \&cleanup;
$SIG{KILL} = \&cleanup;
$SIG{INT}  = \&cleanup;
$SIG{HUP}  = sub { &load_mime("reloading mime types...") };
$SIG{CHLD} = sub { wait };

if (open(FH,"$PIDFILE")) {
  $pid = <FH>;
  close(FH);
  if (kill 0,$pid) {
    &error("$PROGRAM_NAME already running (pid=$pid)");
  }
  else {
    &warning("removing old pid file");
  }
}
if (open(FH,">$PIDFILE")) {
  print FH $$;
  close(FH);
}
else {
  &warning("can not write pid file $PIDFILE: $!");
}

&load_mime();

$listen = IO::Socket::INET->new(LocalPort=>$PORT, ReuseAddr=>1, Proto=>'tcp',
                                Listen=>1024) ||
  &error("can not create socket: $!");

&warning("$PROGRAM_NAME starting...");
while (my $socket = $listen->accept()) {
  $pid = fork();
  if ($pid == 0) {
    while ((my $s = <$socket>) ne "\r\n") {
      if ($s =~ /^get ([^\s]*)/io) {
        $url = $1;
        ($request = $s) =~ s/\r|\n//go;
      }
    }
    (my $file = ".$url") =~ s/\.\.//go;
    if (-d $file) {
      $file .= "/" if $file !~ /\/$/o;
      $mime = "html";
      if (($content = &read_file($file."index.html")) ne "") {
        $code = 200;
      }
      elsif (!opendir(DH,$file)) {
        $code = 404;
        $content = &http_error($socket,$code,$url);
      }
      else {
        @dir = readdir(DH);
        closedir(DH);
        $code = 200;
        $content = "<html><head><title>Index of $url</title></head><body>";
        $content.= "<h1>Index of $url</h1><pre><table>";
        foreach (sort @dir) {
          $content .= "<tr><td><a href=\"$_";
          if (-d "$file$_") {
            $content .= "/\">$_/</a></td><td align=right>-";
          }
          else {
            my $size = -s "$file$_";
            $content .= "\">$_</a></td><td align=right>";
            $content .= &sizef($size);
          }
          $content .= "</td></tr>";
        }
        $content.= "</pre></table>".&address($socket);
      }
    }
    elsif (($content = &read_file($file)) ne "") {
      $code = 200;
      $mime = substr($file,rindex($file,'.')+1);
    }
    else {
      $code = 404;
      $mime = "html";
      $content = &http_error($socket,$code,$url);
    }
    my $len = length($content);
    print $socket &http_out($code,$mime,$len,$content);
    print &common_log($socket,$request,$code,$len);
    $socket->close();
    &byebye();
  }
}

sub read_file
{
 my $file = shift;
 if ( (-f $file) && (open(FH,$file)) ) {
   binmode FH;
   while (<FH>) { $content .= $_; }
   close(FH);
   binmode $socket;
   return $content;
 }
 else {
   return "";
 }
}

sub http_error
{
  my $socket = shift;
  my $code = shift;
  my $url = shift;

  my $ret = "<html><head><title>$code $HTTPCODE{$code}</title></head><body>";
  $ret .= "<h1>$HTTPCODE{$code}</h1><p>The requested URL $url was not found on";
  $ret .= " this server.</p>".&address($socket);
  return $ret;
}

sub address
{
  my $socket = shift;
  my $server = $socket->sockhost();
  my $port = $socket->sockport();

  my $ret =  "<hr><address>Simple HTTP on $server:$port <br>";
  $ret .= scalar localtime(time)."</address></body></html>";
  return $ret;
}

sub http_out
{
  my $code = shift;
  my $mime = shift;
  my $len = shift;
  my $content = shift;
  my $ret = "HTTP/0.9 $code $HTTPCODE{$code}\r\nContent-type: ";
  $ret .= &get_mime($mime)."\r\nContent-length: ".$len."\r\n";
  $ret .= "Connection: close\r\n\r\n$content";
}

sub common_log
{
  my $socket = shift;
  my $request = shift;
  my $code = shift;
  my $len = shift;

  my $jetzt = time();
  my $remotehost = $socket->peerhost();

  my $ret = "$remotehost - - [".&datetimestamp($jetzt);
  $ret .= "] \"$request\" $code $len\n";
}

sub datetimestamp
{
  my $jetzt = shift;
  my ($sec,$min,$hour,$day,$mon,$year,@rest) = localtime($jetzt);
  $year += 1900;
  foreach ($sec,$min,$hour,$day) {
    $_ = "0$_" if $_ < 10;
  }
  return "$day/$MONTH[$mon]/$year:$hour:$min:$sec $TIMEZONE";
}

sub cleanup
{
  if (!unlink $PIDFILE) {
    &warning("can not remove pid file $PIDFILE: $!");
  }
  &warning("$PROGRAM_NAME shutting down...");
  &byebye();
}

sub load_mime
{
  %MIME = ();
  if (open(MFH,$MIME)) {
    &warning($_[0]) if $_[0];
    my @inhalt = <MFH>;
    close (MFH);
    %MIME = map {
      s/\r|\n//go;
      my @split = split(/\t/,$_);
      $split[0] => $split[1]
    } @inhalt;
  }
  else {
    &warning("can not read $MIME: $!");
  }
}

sub get_mime
{
  my $ret = $MIME{$_[0]};
  $ret = $DEFAULT_MIME if $ret eq "";
  return $ret;
}

sub error
{
  &warning($_[0]);
  &byebye();
}

sub warning
{
  print STDERR "[".&datetimestamp(time)."] $_[0]\n";
}

sub sizef
{
  my $size = shift;
  my $i = 0;
  my @SIZES = ("","k","M","G","T");
  while ($size > 1024) {
    $size /= 1024;
    $i++;
  }
  return sprintf("%.0f",$size).$SIZES[$i];
}

sub byebye
{
  close (STDERR);
  close (STDOUT);
  exit;
}

来自http://www.ogris.de/httpd/

1 个答案:

答案 0 :(得分:0)

  

如何告诉mime使用PHP提供php文件?

PHP应该作为CGI script投放,您的网络服务器显然不支持。{/ p>

(为简单起见,我省略了所有安全注意事项。)

粗略地解释说,一个CGI脚本的页面,而不是逐字发送到客户端,由HTTP服务器作为应用程序执行,并将其输出发送到客户端。

要以最简单的形式实现CGI脚本支持,您需要:

  1. 添加作为CGI脚本的MIME类型的配置表。

  2. 在将文件发送到客户端之前,请检查MIME类型是否与CGI相对应。如果是,并且文件是可执行的,则运行它,并将stdout传送到客户端套接字。

  3. 要测试它,您可以为(规范扩展名).cgi文件添加MIME类型,并创建简单的shell或Perl脚本(扩展名为.cgi),该脚本应该:

    • 打印HTTP标头(由"\n\r"分隔),包括(最重要的)Content-Type字段。

    • 打印额外"\n\r\n\r"作为标题与内容之间的分隔符,

    • 打印内容(例如,只需打印&#34; Hello World&#34;)。

    在完整的CGI实现中,服务器通过the environment variables将额外信息传递给CGI脚本。 Web服务器还需要读取和处理由CGI脚本生成的HTTP标头,以使用附加HTTP field扩展它,或者只是检查标头是否有效。如果脚本无法启动或生成空输出或因错误而终止,请使用HTTP状态代码500向客户端报告问题。

    现在到PHP。大多数PHP页面并非设计为CGI,因为PHP大部分时间都配置为由Web服务器作为共享库(或FastCGI)加载。因此,即使您实现了完整正确的CGI接口,大多数.php文件仍然无法工作。您必须使用自己的&#34;集成&#34;扩展CGI支持。对于PHP。 to execute a PHP file最简单的方法是使用php -f <file.php>运行它。换句话说,您需要向Web服务器添加其他配置,告知某些CGI MIME类型不应该直接运行,而是在外部进程的帮助下执行。

    P.S。您可以检查的其他Web服务器实现: