私は自宅からウェブページをホストしています。Java を使用して独自の HTTP サーバーを作成しました。これは SSCCE です。
if(command.startsWith("GET"))
{
//client is a socket on which I reply.
PrintWriter pw = new PrintWriter(client.getOutputStream(), true);
String commule = command.split(" ");
if(commule[0].equals("GET"))
{
if(commule[1].contains("."))
{
File file = new File(GEQO_SERVER_ROOT + commule[1].substring(1).replaceAll("%20", " "));
if(file.exists())
{
OutputStream out = client.getOutputStream();
InputStream stream = new FileInputStream(file);
String response = new String();
response += "HTTP/1.1 200 OK\r\n";
response += "Date: Thu, 08 Aug 2013 08:49:37 GMT\r\n";
response += "Content-Type: text/html\r\n";
response += "Content-Length: " + file.length() + "\r\n";
response += "Connection: keep-alive\r\n";
response += "\r\n";
pw.write(response); //Assume I already initialized pw as a PrintWriter
pw.flush();
copy(stream, out);
stream.close();
out.close();
}
else
{
pw.write("<html><h1>The request 404ed.</h1>");
pw.write("<body>The requested URL <b>" + commule[1] + "</b> could not be found on this server.</body></html>");
pw.flush();
}
}
else
{
BufferedReader br = new BufferedReader(new FileReader(GEQO_SERVER_ROOT + commule[1].substring(1) + "main.html"));
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null)
{
pw.print(sCurrentLine);
}
br.close();
}
}
else
{
pw.println("Unrecognized HTTP command.");
}
}
これは main.html ソースです:
<html>
<title>Geqo Server</title>
<body>Geqo server online and functioning!</body>
</html>
問題は、Chrome を使用してこのページにアクセスしようとすると、正しく表示されることです (少なくとも 127.0.0.1 を使用している場合)。しかし、127.0.0.1 の Firefox でアクセスしようとすると、動作しますが、html ソースしか表示されません。IEもソースしか教えてくれません。Firefox と IE がソースを解析するのではなく、ソースのみを表示する理由を誰か教えてもらえますか?
これにはいくつかの手がかりが含まれていると思います(Firebugのスクリーンショット):
私のソースは<pre>
タグに入っているようです。理由はわかりませんが、そういう問題ではないですか?
ポート転送しました。ここにページがあります: http://110.172.170.83:17416/
(申し訳ありませんが、Stackoverflow は数値リンクを許可していません。)
編集:問題が見つかりました。しかし、説明する前に、SSCCE のBartに感謝します。SSCCE は、以前は自分のコードと比較していました。これが問題ですif
。8 行目のステートメントif(commule[1].contains("."))
により、コードはここのコードの大部分をスキップします。そのそれぞれelse
のブロックには、ヘッダーを送信するコマンドさえありません。それを指摘してくれたartbristolに感謝します。
前もって感謝します。