I have built a file server in Java and I’m having difficulties with understanding when I should start serving the file data. As you can see below are two different client requests. Some provide two request and I’m not sure when I should start serving the data and when I should “ignore”.
Client #1:
06-15 08:06:21.290: VERBOSE/HttpSession(9425): SESSION 1: STARTED
06-15 08:06:21.290: INFO/HttpSession(9425): CLIENT:
06-15 08:06:21.290: INFO/HttpSession(9425): GET /stream HTTP/1.1
06-15 08:06:21.290: INFO/HttpSession(9425): Host: 127.0.0.1:37914
06-15 08:06:21.290: INFO/HttpSession(9425): User-Agent: (Linux)
06-15 08:06:21.290: INFO/HttpSession(9425): SERVER:
06-15 08:06:21.290: INFO/HttpSession(9425): HTTP/1.1 200 OK
06-15 08:06:21.290: INFO/HttpSession(9425): Date: Wed, 15 Jun 2011 12:06:21 GMT
06-15 08:06:21.290: INFO/HttpSession(9425): Server: HTTP Server
06-15 08:06:21.290: INFO/HttpSession(9425): Last-Modified: Mon, 06 Aug 2009 01:02:23 GMT
06-15 08:06:21.290: INFO/HttpSession(9425): Accept-Ranges: none
06-15 08:06:21.290: INFO/HttpSession(9425): Content-Type: audio/mpeg
06-15 08:06:21.290: INFO/HttpSession(9425):
06-15 08:06:26.720: VERBOSE/HttpSession(9425): SESSION 2: STARTED
06-15 08:06:26.730: INFO/HttpSession(9425): CLIENT:
06-15 08:06:26.730: INFO/HttpSession(9425): GET /stream HTTP/1.1
06-15 08:06:26.730: INFO/HttpSession(9425): Host: 127.0.0.1:37914
06-15 08:06:26.730: INFO/HttpSession(9425): Accept: */*
06-15 08:06:26.730: INFO/HttpSession(9425): SERVER:
06-15 08:06:26.730: INFO/HttpSession(9425): HTTP/1.1 200 OK
06-15 08:06:26.730: INFO/HttpSession(9425): Date: Wed, 15 Jun 2011 12:06:26 GMT
06-15 08:06:26.730: INFO/HttpSession(9425): Server: HTTP Server
06-15 08:06:26.730: INFO/HttpSession(9425): Last-Modified: Mon, 06 Aug 2009 01:02:23 GMT
06-15 08:06:26.730: INFO/HttpSession(9425): Accept-Ranges: none
06-15 08:06:26.730: INFO/HttpSession(9425): Content-Type: audio/mpeg
06-15 08:06:26.730: INFO/HttpSession(9425):
06-15 08:06:26.730: VERBOSE/HttpSession(9425): SESSION 2: ENDED
Client #2:
06-15 05:03:58.079 I/HttpSession(18335): CLIENT:
06-15 05:03:58.079 I/HttpSession(18335): GET /stream HTTP/1.1
06-15 05:03:58.079 I/HttpSession(18335): Host: 127.0.0.1
06-15 05:03:58.079 I/HttpSession(18335): Accept: */*
06-15 05:03:58.079 I/HttpSession(18335): Icy-MetaData:1
06-15 05:03:58.079 I/HttpSession(18335): User-Agent: QuickTime;NvMM HTTP Client v0.1
06-15 05:03:58.089 I/HttpSession(18335): SERVER:
06-15 05:03:58.089 I/HttpSession(18335): HTTP/1.1 200 OK
06-15 05:03:58.089 I/HttpSession(18335): Date: Wed, 15 Jun 2011 09:03:58 GMT
06-15 05:03:58.089 I/HttpSession(18335): Server: HTTP Server
06-15 05:03:58.089 I/HttpSession(18335): Last-Modified: Mon, 06 Aug 2009 01:02:23 GMT
06-15 05:03:58.089 I/HttpSession(18335): Accept-Ranges: bytes
06-15 05:03:58.089 I/HttpSession(18335): Content-Type: audio/mpeg
If you’re trying to serve audio or video it’s likely that the client is trying to leverage HTTP’s Range header. The Range header allows features like skipping to the middle of an audio/video stream, scrubbing, etc. Desktop browsers often issue a couple of requests, one to discover if the Range header is supported, and a further request to actually begin streaming content. As long as your server can handle the client terminating socket connections you’re probably safe to start streaming content for each connection.
If you’re interested you could take a look at Brisket – it’s a lightweight, Java-based HTTP server with Range header support.