Web Servers General Talk - Opera hangs when reading chunked replies

This is Interesting: Free IT Magazines  
Home > Archive > Web Servers General Talk > May 2006 > Opera hangs when reading chunked replies





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Opera hangs when reading chunked replies
Dave Griffiths

2006-05-10, 1:18 pm

Hi, I submitted the following message to an Opera forum but no reply
yet. I'm trying to find out if the bug is with Opera or with my use of
chunked encoding. Is chunked encoding on persistent connections not the
norm yet for servers? (The docs all give the impression it is). Any
help would be much appreciated!

The attached Java program implements a simple server using chunked http
transfer encoding. It hangs and displays a blank page in Opera but
works fine in IE and Firefox. This is with Opera 8.54. If you modify
the program to pass chunked == false to the reply function then it
works. I'm pretty sure the chunked format is correct. Just wondered if
anyone else had seen this and knew the reason?

To use the program, just run it from the command line and then enter
"http://localhost:8080/" in the browser window. I also turned off
cacheing in Opera.

Cheers,

Dave

import java.io.*;
import java.net.*;
import java.util.*;

public class ChunkedTest implements Runnable {

Socket socket;
BufferedReader in;
OutputStream out;

public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8080);
while (true) {
Socket socket = ss.accept();
new Thread(new ChunkedTest(socket)).start();
}
} catch (Exception e) {
throw new Error("oops: " + e);
}
}

ChunkedTest(Socket socket) {
this.socket = socket;
try {
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
out = socket.getOutputStream();
} catch (Exception e) {
throw new Error("oops: " + e);
}
}

public void run() {
while (true) {
try {
System.out.println("about to read next request");
String line = in.readLine();
System.out.println("got request: " + line);
if (line == null) {
/* EOF on socket so just exit */
System.out.println("socket closed");
return;
}
String[] tokens = line.split(" ");
String request = tokens[0];
String uri = tokens[1];
while ((line = in.readLine()).length() != 0) {
System.out.println("read header line: " + line);
}
if (request.equals("GET")) {
if (uri.equals("/")) {
System.out.println("sending root reply");
reply("200 OK", "<html><head><script
src=\"dummy.js\"></script></head>" +
"<body><h1>If you can see this, the test
worked!</h1><p>Current time: " +
new Date() + "</body></html>", "text/html",
true);
} else if (uri.equals("/dummy.js")) {
System.out.println("sending dummy.js reply");
reply("200 OK", "// dummy",
"application/x-javascript", true);
} else {
System.out.println("could not find uri: " +
uri);
reply("404 Not Found",
"<html><head></head><body><h1>Could not find " +
uri + "</h1></body></html>", "text/html",
false);
}
} else {
throw new Error("Unknown request: " + request);
}
} catch (SocketException e) {
/* Presumably the socket has closed */
System.out.println("socket exception: " + e);
return;
} catch (Exception e) {
e.printStackTrace();
throw new Error("oops: " + e);
}
}
}

void reply(String status, String body, String contentType, boolean
chunked) {
try {
println("HTTP/1.1 " + status);
println("Content-Type: " + contentType);
byte[] b = body.getBytes();
if (chunked) {
println("Transfer-Encoding: chunked");
} else {
println("Content-Length: " + b.length);
}
println("");
if (chunked) {
println(Integer.toHexString(b.length));
}
out.write(b);
if (chunked) {
println("");
println("0");
println("");
}
out.flush();
} catch (IOException e) {
throw new Error("oops: " + e);
}
}

void println(String line) throws IOException {
out.write((line + "\r\n").getBytes());
out.flush();
}
}

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com