SSLSocket BufferedReader blocking problem

S

sean.antony

I have a client program which connects to an Apache server, sends some
HTTP messages and retrieves the responses.

An example message is:

OPTIONS / HTTP/1.1
Host: 127.0.0.1
Accept: */*


The response from Apache is:

HTTP/1.1 200 OK
Date: Wed, 07 Mar 2007 14:57:16 GMT
Server: Apache/2.2.3 (Unix) mod_ssl/2.2.3 OpenSSL/0.9.8a DAV/2
DAV: 1,2
DAV: <http://apache.org/dav/propset/fs/1>
MS-Author-Via: DAV
Allow:
OPTIONS,GET,HEAD,POST,DELETE,TRACE,PROPFIND,PROPPATCH,COPY,MOVE,LOCK,UNLOCK
Content-Length: 0
Content-Type: httpd/unix-directory


This is the code that gets the response from Apache, the in variable
is a BufferedReader connected to the InputStream of the SSLSocket.


private String getResponse() throws IOException {
String response = "";
int c = in.read();
char ch;
long start = System.currentTimeMillis();
while (in.ready() && c != -1) {
ch = (char) c;
response += ch;
c = in.read();
}
long finish = System.currentTimeMillis();
System.out.println(finish - start);
ch = (char) c;
response += ch;
return response;
}


The output to the console is how long it took to get the whole message
from the InputStream. In HTTP (Socket) mode this number is between
0-20 milliseconds but when i use HTTPS (SSLSocket) mode it is approx
5000! This number is independent of the time it takes to handshake
with the server.

In SSL mode the above method fails actually, because in.ready()
returns false the first time, instead i have to modify it to:


while ((c = in.read()) != -1) {
ch = (char) c;
response += ch;
System.out.print(ch);
}


In which case, i get the whole message printed to the console as fast
as HTTP mode but on the final evaluation of in.read() it takes the
previously stated 5000 ms to finally exit the loop and return the
String response.

I am certain my Apache server is configured correctly as if i use a
web browser to navigate the site in HTTPS the response is instant as
like in unsecure HTTP mode.

So why am i seeing this 5000 ms delay, when i reach the end of the
stream in the BufferedReader?

There should be no difference in reading the BufferedReader when using
either HTTP or HTTPS mode, given that i've accounted for the time it
takes to handshake, right?
 
S

sean.antony

I have a client program which connects to an Apache server, sends some
HTTP messages and retrieves the responses.

An example message is:

OPTIONS / HTTP/1.1
Host: 127.0.0.1
Accept: */*

The response from Apache is:

HTTP/1.1 200 OK
Date: Wed, 07 Mar 2007 14:57:16 GMT
Server: Apache/2.2.3 (Unix) mod_ssl/2.2.3 OpenSSL/0.9.8a DAV/2
DAV: 1,2
DAV: <http://apache.org/dav/propset/fs/1>
MS-Author-Via: DAV
Allow:
OPTIONS,GET,HEAD,POST,DELETE,TRACE,PROPFIND,PROPPATCH,COPY,MOVE,LOCK,UNLOCK
Content-Length: 0
Content-Type: httpd/unix-directory

This is the code that gets the response from Apache, the in variable
is a BufferedReader connected to the InputStream of the SSLSocket.

private String getResponse() throws IOException {
String response = "";
int c = in.read();
char ch;
long start = System.currentTimeMillis();
while (in.ready() && c != -1) {
ch = (char) c;
response += ch;
c = in.read();
}
long finish = System.currentTimeMillis();
System.out.println(finish - start);
ch = (char) c;
response += ch;
return response;

}

The output to the console is how long it took to get the whole message
from the InputStream. In HTTP (Socket) mode this number is between
0-20 milliseconds but when i use HTTPS (SSLSocket) mode it is approx
5000! This number is independent of the time it takes to handshake
with the server.

In SSL mode the above method fails actually, because in.ready()
returns false the first time, instead i have to modify it to:

while ((c = in.read()) != -1) {
ch = (char) c;
response += ch;
System.out.print(ch);

}

In which case, i get the whole message printed to the console as fast
as HTTP mode but on the final evaluation of in.read() it takes the
previously stated 5000 ms to finally exit the loop and return the
String response.

I am certain my Apache server is configured correctly as if i use a
web browser to navigate the site in HTTPS the response is instant as
like in unsecure HTTP mode.

So why am i seeing this 5000 ms delay, when i reach the end of the
stream in the BufferedReader?

There should be no difference in reading the BufferedReader when using
either HTTP or HTTPS mode, given that i've accounted for the time it
takes to handshake, right?

Solution found at http://forum.java.sun.com/thread.jspa?messageID=9537105

Case closed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top