/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 4507412
* @bug 4506998
* @summary Check that a 304 "Not-Modified" response from a server
* doesn't cause http client to close a keep-alive
* connection.
* Check that a content-length of 0 results in an
* empty input stream.
*/
public class ZeroContentLength {
/*
* Is debugging enabled - start with -d to enable.
*/
static boolean debug = false;
if (debug)
}
/*
* The response string and content-length that
* the server should return;
*/
static int contentLength;
contentLength = cl;
}
return response;
}
static synchronized int getContentLength() {
return contentLength;
}
/*
* Worker thread to service single connection - can service
* multiple http requests on same connection.
*/
Socket s;
int id;
this.s = s;
}
public void run() {
try {
s.setSoTimeout(2000);
for (;;) {
// read entire request from client, until CR LF CR LF
int c, total=0;
try {
total++;
if (c == CR) {
total++;
if (c == LF) {
total++;
if (c == CR) {
total++;
if (c == LF) {
break;
}
}
}
}
}
}
}
}
} catch (SocketTimeoutException e) {}
": Read request from client " +
if (total == 0) {
return;
}
// response to client
new BufferedOutputStream(
s.getOutputStream() ));
int clen = getContentLength();
if (clen >= 0) {
"\r\n");
}
for (int i=0; i<clen; i++) {
}
": Sent response to client, length: " + clen);
if (--max == 0) {
s.close();
return;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
s.close();
} catch (Exception e) { }
}
}
}
/*
* Server thread to accept connection and create worker threads
* to service each connection.
*/
int connectionCount;
boolean shutdown = false;
}
public synchronized int connectionCount() {
return connectionCount;
}
public synchronized void shutdown() {
shutdown = true;
}
public void run() {
try {
for (;;) {
Socket s;
try {
debug("server: Waiting for connections");
} catch (SocketTimeoutException te) {
synchronized (this) {
if (shutdown) {
debug("server: Shuting down.");
return;
}
}
continue;
}
int id;
synchronized (this) {
id = connectionCount++;
}
w.start();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
} catch (Exception e) { }
}
}
}
/*
* Make a single http request and return the content length
* received. Also do sanity check to ensure that the
* content-length header matches the total received on
* the input stream.
*/
byte b[] = new byte[100];
int total = 0;
int n;
do {
if (n > 0) total += n;
} while (n > 0);
throw new Exception("Content-length didn't match actual received");
}
return total;
}
/*
* Send http requests to "server" and check that they all
* use the same network connection and that the content
* length corresponds to the content length expected.
* stream.
*/
/* start the server */
"/foo.html";
int expectedTotal = 0;
int actualTotal = 0;
for (int i=0; i<5; i++) {
expectedTotal += 1024;
}
for (int i=0; i<5; i++) {
}
for (int i=0; i<5; i++) {
}
for (int i=0; i<5; i++) {
}
// shutdown server - we're done.
if (actualTotal == expectedTotal) {
} else {
throw new Exception("Actual total != Expected total!!!");
}
if (cnt == 1) {
} else {
throw new Exception("Test failed: Number of connections " +
}
}
debug = true;
}
new ZeroContentLength();
}
}