diff options
author | mkoch <mkoch@138bc75d-0d04-0410-961f-82ee72b054a4> | 2003-12-02 14:13:46 +0000 |
---|---|---|
committer | mkoch <mkoch@138bc75d-0d04-0410-961f-82ee72b054a4> | 2003-12-02 14:13:46 +0000 |
commit | 0edcf674fd6d3d2921af996f5b8b8e6080f84e12 (patch) | |
tree | 62bb52cd750b9a2150b095f29f129a90c4a518a2 /libjava/gnu/java/net/protocol/http/Connection.java | |
parent | 7f082fcfc2e1a9ae1121433efb560f807a90a339 (diff) | |
download | ppe42-gcc-0edcf674fd6d3d2921af996f5b8b8e6080f84e12.tar.gz ppe42-gcc-0edcf674fd6d3d2921af996f5b8b8e6080f84e12.zip |
2003-12-02 Michael Koch <konqueror@gmx.de>
* gnu/java/net/protocol/http/Connection.java
(Connection): Initialize doOutput to false;
(connect): Initialize inputStream, moved "send request" code to new
method.
(sendRequest): New method.
(getHttpHeaders): Don't reinitialize inputStream.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@74177 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/gnu/java/net/protocol/http/Connection.java')
-rw-r--r-- | libjava/gnu/java/net/protocol/http/Connection.java | 57 |
1 files changed, 39 insertions, 18 deletions
diff --git a/libjava/gnu/java/net/protocol/http/Connection.java b/libjava/gnu/java/net/protocol/http/Connection.java index ac6cc692287..ae13dff8506 100644 --- a/libjava/gnu/java/net/protocol/http/Connection.java +++ b/libjava/gnu/java/net/protocol/http/Connection.java @@ -117,6 +117,9 @@ public final class Connection extends HttpURLConnection protected Connection(URL url) { super(url); + + /* Set up some variables */ + doOutput = false; } public void setRequestProperty(String key, String value) @@ -160,17 +163,15 @@ public final class Connection extends HttpURLConnection socket = new Socket(url.getHost(), port); } - PrintWriter out = new PrintWriter(socket.getOutputStream()); + // Originally tried using a BufferedReader here to take advantage of + // the readLine method and avoid the following, but the buffer read + // past the end of the headers so the first part of the content was lost. + // It is probably more robust than it needs to be, e.g. the byte[] + // is unlikely to overflow and a '\r' should always be followed by a '\n', + // but it is better to be safe just in case. + inputStream = new BufferedInputStream(socket.getInputStream()); - // Send request including any request properties that were set. - out.print(getRequestMethod() + " " + url.getFile() + " HTTP/1.0\r\n"); - out.print("Host: " + url.getHost() + ":" + port + "\r\n"); - Enumeration reqKeys = requestProperties.keys(); - Enumeration reqVals = requestProperties.elements(); - while (reqKeys.hasMoreElements()) - out.print(reqKeys.nextElement() + ": " + reqVals.nextElement() + "\r\n"); - out.print("\r\n"); - out.flush(); + sendRequest(); getHttpHeaders(); connected = true; } @@ -195,6 +196,34 @@ public final class Connection extends HttpURLConnection } /** + * Write HTTP request header and content to outputWriter. + */ + void sendRequest() throws IOException + { + // Create PrintWriter for easier sending of headers. + PrintWriter outputWriter = new PrintWriter(socket.getOutputStream()); + + // Send request including any request properties that were set. + outputWriter.print (getRequestMethod() + " " + url.getFile() + + " HTTP/1.0\r\n"); + + // Set additional HTTP headers. + if (getRequestProperty ("Host") == null) + setRequestProperty ("Host", url.getHost()); + + // Write all req_props name-value pairs to the output writer. + Enumeration reqKeys = requestProperties.keys(); + Enumeration reqVals = requestProperties.elements(); + + while (reqKeys.hasMoreElements()) + outputWriter.print (reqKeys.nextElement() + ": " + reqVals.nextElement() + "\r\n"); + + // One more CR-LF indicates end of header. + outputWriter.print ("\r\n"); + outputWriter.flush(); + } + + /** * Return a boolean indicating whether or not this connection is * going through a proxy * @@ -318,14 +347,6 @@ public final class Connection extends HttpURLConnection */ private void getHttpHeaders() throws IOException { - // Originally tried using a BufferedReader here to take advantage of - // the readLine method and avoid the following, but the buffer read - // past the end of the headers so the first part of the content was lost. - // It is probably more robust than it needs to be, e.g. the byte[] - // is unlikely to overflow and a '\r' should always be followed by a '\n', - // but it is better to be safe just in case. - inputStream = new BufferedInputStream(socket.getInputStream()); - int buflen = 100; byte[] buf = new byte[buflen]; String line = ""; |