嵌入式Jetty不提供空文件

时间:2015-01-20 03:45:29

标签: jetty embedded-jetty

设置:我已经设置了嵌入式jetty(v9.1)来提供setDirectoriesListed(true)的静态文件,我正在使用的代码如下:

    // Create a basic Jetty server object that will listen on port 8080.  Note that if you set this to port 0
    // then a randomly available port will be assigned that you can either look in the logs for the port,
    // or programmatically obtain it for use in test cases.
    Server server = new Server(9090);

    // Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
    // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
    ResourceHandler resource_handler = new ResourceHandler();
    // Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
    // In this example it is the current directory but it can be configured to anything that the jvm has access to.
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[]{ "index.html" });
    resource_handler.setResourceBase(".");

    // Add the ResourceHandler to the server.
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
    server.setHandler(handlers);

    // Start things up! By using the server.join() the server thread will join with the current thread.
    // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
    server.start();
    server.join();

此代码最初来自here

当我导航到地址http://localhost:9090/时,我会看到目录中列出的文件,并且我可以单击并打开单个文本文件。

问题:出于某种莫名其妙的原因,只有当我点击0字节的文件(也就是它是一个空文件,但仍然显示在浏览器中)时,连接会尝试加载但最终会加载out(30秒)我在safari中得到一个回应,称“服务器意外断开连接”。另外,当我对0字节文件进行HttpURLConnection时,我得到的内容长度为-1;这当然只适用于空文件。

在独立Jetty中看到的预期行为:当我使用独立的jetty并提供相同的文件时,我能够“打开”空文件,它只是在Web浏览器中返回一个空白页面。使用HttpURLConnection时,我的内容长度为0。

虽然这似乎是一个“毫无意义”的任务,但是一台服务器以编程方式与嵌入式jetty服务器同步(所以我希望这些空文件同步)。我想它与资源处理程序有关,因为它为静态内容提供0字节,但是我不太确定如何获得独立的jetty服务器的相同行为,因为它在尝试拉动时会出错空文件。

谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码至少在Jetty 9.2.7.v20140116

上按原样运行

我使用的完整示例:

package jetty;

import java.io.File;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;

public class SimpleResources
{
    public static void main(String[] args)
    {
        Server server = new Server(9090);

        String resourceBase = System.getProperty("resourceBase", ".");

        System.err.printf("Resource Base is: %s%n", new File(resourceBase).getAbsolutePath());

        ResourceHandler resource_handler = new ResourceHandler();
        resource_handler.setDirectoriesListed(true);
        resource_handler.setWelcomeFiles(new String[] { "index.html" });
        resource_handler.setResourceBase(resourceBase);

        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
        server.setHandler(handlers);

        try
        {
            server.start();
            server.join();
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.err);
        }
    }
}

我运行它将-DresourceBase系统属性指向具有以下内容的目录...

$ ls -la
total 8
drwxrwxr-x. 2 joakim joakim 4096 Jan 20 11:53 .
drwxrwxr-x. 3 joakim joakim 4096 Jan 20 11:53 ..
-rw-rw-r--. 1 joakim joakim    0 Jan 20 11:53 foo.txt

一旦运行控制台显示......

2015-01-20 11:55:07.788:INFO::main: Logging initialized @68ms
Resource Base is: /home/joakim/code/Jetty/empties
2015-01-20 11:55:07.837:INFO:oejs.Server:main: jetty-9.2.7.v20150116
2015-01-20 11:55:07.860:INFO:oejs.ServerConnector:main: Started ServerConnector@5461eda{HTTP/1.1}{0.0.0.0:9090}
2015-01-20 11:55:07.861:INFO:oejs.Server:main: Started @144ms

有这样的测试请求......

$ curl --dump-header - http://localhost:9090/foo.txt
HTTP/1.1 200 OK
Date: Tue, 20 Jan 2015 18:55:39 GMT
Content-Type: text/plain
Content-Length: 0
Server: Jetty(9.2.7.v20150116)

<强>更新

按原样工作,对以下版本的jetty没有任何修改(没有对版本进行详尽的测试,只有少数旧的版本)

  • 9.2.6.v20141205 - 相同的结果
  • 9.2.4.v20141103 - 相同的结果
  • 9.2.1.v20140609 - 相同的结果
  • 9.1.5.v20140505 - 响应标头中没有Date,其余是相同的(是的,它还会发送Content-Length: 0