Skip to main content

HttpServer and big content length

9 replies [Last post]
matbe
Offline
Joined: 2005-04-27

The new httpserver functionality seems very nice and a is a well done addition IMHO.
For those that have missed it, for now(?) it is under com.som.* :
http://download.java.net/jdk6/docs/guide/net/httpserver/spec/

Having played around with it some now (b74 Win IA32) I have one issue/question/RFE and that is the content length. It can only be specified as an int (meaning signed 32bit integer) This means that we can only serve files as large as around 2GB. Could this simply be changed to a long?

If I remember correctly, having implemented simple http servers before, that at least 4GB (corresponding to unsigned 32bit integer) files work fine with Internet Explorer. Though, again if I remember correctly, larger than 2GB files where considered of negative size. Still worked fine up to the next 4GB limit were it would just cycle around to "size % 4GByte" of course.
Clients that use 64bit integers for example would work perfectly.

File.length() returns a long as well. We can skip a cast there then too :)

The HTTP 1.1 RFC does not limit this field in any other way than >= 0.

If it's easy to avoid it why not? Certainly not that uncommon to have files bigger than 2Gb now a days. Consider popular download sites, linux dvd images etc.
Though one could discuss the usage of a http server, especially one based on this, to serve them, but still, seems like an unnecessery limit.

I don't see how it could make it worse. This will not work for all http clients or browsers due to the exact implementation (how the content length is parsed & stored in those clients) but it would not work for them anyway!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mindless
Offline
Joined: 2007-10-30

This covers sending a response.. what about accepting data? If I try to POST/PUT more than 2GB of data I see:

<h1>400 Bad Request</h1>NumberFormatException thrown

And it never reaches my HttpHandler code. This is with 1.6.0_03.

matbe
Offline
Joined: 2005-04-27

It has not it seems, despite that reference in the bug database to build 81.

I just tried it with build 84(change the 0L to 0 and it works).

C:\Temp>"c:\Program Files\Java\jdk1.6.0\bin\javac" Test.java
Test.java:32: sendResponseHeaders(int,int) in com.sun.net.httpserver.HttpExchang
e cannot be applied to (int,long)
exchange.sendResponseHeaders(200, 0L);
^
1 error

C:\Temp>java -version
java version "1.6.0-beta2"
Java(TM) SE Runtime Environment (build 1.6.0-beta2-b84)
Java HotSpot(TM) Client VM (build 1.6.0-beta2-b84, mixed mode, sharing)

C:\Temp>

Test program used:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class Test {

public static void main(String[] args) throws IOException {
HttpServer httpServer;
httpServer = HttpServer.create(new InetSocketAddress(81), 5);
httpServer.createContext("/", new Handler());

httpServer.start();

}

static class Handler implements HttpHandler {

public void handle(HttpExchange exchange) throws IOException {
Headers requestHeaders = exchange.getRequestHeaders();
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");

exchange.sendResponseHeaders(200, 0L);

OutputStream responseBody = new BufferedOutputStream(exchange.getResponseBody(), 64*1024);

responseBody.write("Hello!".getBytes());

responseBody.close();
exchange.close();
}
}

}

mikemcm
Offline
Joined: 2006-05-23

Sorry. We meant to fix it in b81, but it did not get approved in time.

It won't be in beta2 either, but we will try to get it into a later build before FCS.

matbe
Offline
Joined: 2005-04-27

Ah ok. Thanks for the update. Sounds good.

alanb
Offline
Joined: 2005-08-08

Here's the link to the bug for this:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6405050

and it seems to have been fixed in b81.

matbe
Offline
Joined: 2005-04-27

Aah, perfect. So someone actually read (?) the initial post (march 24) and created a bug (march 28), but didn't mention it here, and fixed it as well.
Perfect.

Only one more slight improvement I would like to request then. If the above flow was the correct one (reading the post etc) it would be nice to have that person mention the bug creation here. :)

podlesh
Offline
Joined: 2004-07-26

This is obviously design flaw. No matter what the "supposed use" of the HTTP server is, 32bit signed int is too narrow.

matbe
Offline
Joined: 2005-04-27

Noone else has any feedback about this? I find it to be a limit for a simple application that tries to "web expose" (and keep track of) files easily upon user request

prunge
Offline
Joined: 2004-05-06

I thought this web server was mainly used for web services, can it be used for other things too?

That being said, I don't find it impossible for a web service to return > 2GB of data - certainly not a good idea for most web services but it is possible. For example, I know of a case where gigs of router data was transferred through FTP every night. If this for some reason was to be turned into a web service that returned all the data in one response would they be out of luck?