1 |
/* HttpURLConnection.java -- URLConnection class for HTTP protocol |
/* HttpURLConnection.java -- URLConnection class for HTTP protocol |
2 |
Copyright (C) 1998 Free Software Foundation, Inc. |
Copyright (C) 1998, 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
44 |
import java.net.ProtocolException; |
import java.net.ProtocolException; |
45 |
import java.io.InputStream; |
import java.io.InputStream; |
46 |
import java.io.OutputStream; |
import java.io.OutputStream; |
47 |
|
import java.io.BufferedOutputStream; |
48 |
import java.io.DataInputStream; |
import java.io.DataInputStream; |
49 |
|
import java.io.BufferedInputStream; |
50 |
|
import java.io.ByteArrayOutputStream; |
51 |
import java.io.OutputStreamWriter; |
import java.io.OutputStreamWriter; |
52 |
import java.io.PrintWriter; |
import java.io.PrintWriter; |
53 |
import java.io.IOException; |
import java.io.IOException; |
54 |
|
import java.util.Iterator; |
55 |
|
import java.util.Map; |
56 |
|
|
57 |
/** |
/** |
58 |
* This subclass of java.net.URLConnection models a URLConnection via |
* This subclass of java.net.URLConnection models a URLConnection via |
87 |
private OutputStream out_stream; |
private OutputStream out_stream; |
88 |
|
|
89 |
/** |
/** |
90 |
|
* buffered_out_stream is a buffer to contain content of the HTTP request, |
91 |
|
* and will be written to out_stream all at once |
92 |
|
*/ |
93 |
|
private ByteArrayOutputStream buffered_out_stream; |
94 |
|
|
95 |
|
/** |
96 |
* The PrintWriter for this connection (used internally) |
* The PrintWriter for this connection (used internally) |
97 |
*/ |
*/ |
98 |
private PrintWriter out_writer; |
private PrintWriter out_writer; |
140 |
else |
else |
141 |
socket = new Socket(url.getHost(), url.getPort()); |
socket = new Socket(url.getHost(), url.getPort()); |
142 |
|
|
143 |
out_stream = socket.getOutputStream(); |
out_stream = new BufferedOutputStream(socket.getOutputStream()); |
|
in_stream = new DataInputStream(socket.getInputStream()); |
|
|
|
|
144 |
out_writer = new PrintWriter(new OutputStreamWriter(out_stream, "8859_1")); |
out_writer = new PrintWriter(new OutputStreamWriter(out_stream, "8859_1")); |
145 |
|
|
146 |
|
connected = true; |
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
|
* write HTTP request header and content to out_writer |
151 |
|
*/ |
152 |
|
void SendRequest() throws IOException |
153 |
|
{ |
154 |
// Send the request |
// Send the request |
155 |
out_writer.print(getRequestMethod() + " " + getURL().getFile() + |
out_writer.print(getRequestMethod() + " " + getURL().getFile() + |
156 |
" HTTP/1.1\r\n"); |
" HTTP/1.1\r\n"); |
157 |
|
|
158 |
String propval = getRequestProperty("host"); |
if (getRequestProperty("host") == null){ |
159 |
if (propval == null) |
setRequestProperty("Host", getURL().getHost()); |
160 |
out_writer.print("Host: " + getURL().getHost() + "\r\n"); |
} |
161 |
else |
if (getRequestProperty("Connection") == null){ |
162 |
out_writer.print("Host: " + propval + "\r\n"); |
setRequestProperty("Connection", "Close"); |
163 |
out_writer.print("Connection: close" + "\r\n"); |
} |
164 |
|
if (getRequestProperty("user-agent") == null){ |
165 |
propval = getRequestProperty("user-agent"); |
setRequestProperty("user-agent", |
166 |
if (propval == null) |
"gnu-classpath/" + System.getProperty("classpath.version")); |
167 |
out_writer.print("User-Agent: jcl/0.0\r\n"); |
} |
168 |
else |
if (getRequestProperty("accept") == null){ |
169 |
out_writer.print("User-Agent: " + propval + "\r\n"); |
setRequestProperty("accept", "*/*"); |
170 |
|
} |
171 |
propval = getRequestProperty("accept"); |
if (getRequestProperty("Content-type") == null){ |
172 |
if (propval == null) |
setRequestProperty("Content-type", "application/x-www-form-urlencoded"); |
173 |
out_writer.print("Accept: */*\r\n"); |
} |
174 |
else |
|
175 |
out_writer.print("Accept: " + propval + "\r\n"); |
// Write all req_props name-value pairs to the output writer |
176 |
|
Iterator itr = getRequestProperties().entrySet().iterator(); |
177 |
|
while(itr.hasNext()){ |
178 |
|
Map.Entry e = (Map.Entry) itr.next(); |
179 |
|
out_writer.print(e.getKey() + ": " + e.getValue() + "\r\n"); |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
// Write Content-type and length |
184 |
|
if(buffered_out_stream != null){ |
185 |
|
out_writer.print("Content-type: application/x-www-form-urlencoded\r\n"); |
186 |
|
out_writer.print("Content-length: " |
187 |
|
+ String.valueOf(buffered_out_stream.size()) + "\r\n"); |
188 |
|
} |
189 |
|
|
190 |
|
// One more CR-LF indicates end of header |
191 |
out_writer.print("\r\n"); |
out_writer.print("\r\n"); |
192 |
out_writer.flush(); |
out_writer.flush(); |
193 |
|
|
194 |
|
// Write content |
195 |
|
if(buffered_out_stream != null){ |
196 |
|
buffered_out_stream.writeTo(out_stream); |
197 |
|
out_stream.flush(); |
198 |
|
} |
199 |
|
} |
200 |
|
|
201 |
|
/** |
202 |
|
* Read HTTP reply from in_stream |
203 |
|
*/ |
204 |
|
void ReceiveReply() throws IOException |
205 |
|
{ |
206 |
// Parse the reply |
// Parse the reply |
207 |
String line = in_stream.readLine(); |
String line = in_stream.readLine(); |
208 |
String saveline = line; |
String saveline = line; |
238 |
do |
do |
239 |
{ |
{ |
240 |
if (line.length() == 1) |
if (line.length() == 1) |
241 |
throw new IOException("Server header lines were unparseable: " + |
throw new IOException("Server header lines were unparseable: " |
242 |
line); |
+ line); |
243 |
|
|
244 |
line = line.substring(1); |
line = line.substring(1); |
245 |
} |
} |
259 |
// Parse out key and value |
// Parse out key and value |
260 |
idx = line.indexOf(":"); |
idx = line.indexOf(":"); |
261 |
if ((idx == -1) || (line.length() < (idx + 2))) |
if ((idx == -1) || (line.length() < (idx + 2))) |
262 |
throw new IOException("Server header lines were unparseable: " + line); |
throw new IOException("Server header lines were unparseable: " |
263 |
|
+ line); |
264 |
|
|
265 |
key = line.substring(0, idx); |
key = line.substring(0, idx); |
266 |
value = line.substring(idx + 1); |
value = line.substring(idx + 1); |
269 |
while (value.startsWith(" ") || value.startsWith("\t")) |
while (value.startsWith(" ") || value.startsWith("\t")) |
270 |
{ |
{ |
271 |
if (value.length() == 1) |
if (value.length() == 1) |
272 |
throw new IOException("Server header lines were unparseable: " + |
throw new IOException("Server header lines were unparseable: " |
273 |
line); |
+ line); |
274 |
|
|
275 |
value = value.substring(1); |
value = value.substring(1); |
276 |
} |
} |
281 |
headers.addHeaderField(key, value); |
headers.addHeaderField(key, value); |
282 |
} |
} |
283 |
} |
} |
|
|
|
284 |
/*************************************************************************/ |
/*************************************************************************/ |
285 |
|
|
286 |
/** |
/** |
310 |
setRequestMethod(String method) throws ProtocolException |
setRequestMethod(String method) throws ProtocolException |
311 |
{ |
{ |
312 |
method = method.toUpperCase(); |
method = method.toUpperCase(); |
313 |
if (method.equals("GET") || method.equals("HEAD")) |
if (method.equals("GET") || method.equals("HEAD") || method.equals("POST")) |
314 |
super.setRequestMethod(method); |
super.setRequestMethod(method); |
315 |
else |
else |
316 |
throw new ProtocolException("Unsupported or unknown request method " + |
throw new ProtocolException("Unsupported or unknown request method " + |
379 |
public InputStream |
public InputStream |
380 |
getInputStream() throws IOException |
getInputStream() throws IOException |
381 |
{ |
{ |
382 |
|
if(in_stream != null) |
383 |
|
return in_stream; |
384 |
|
|
385 |
if (!connected) |
if (!connected) |
386 |
connect(); |
connect(); |
387 |
|
|
388 |
|
in_stream |
389 |
|
= new DataInputStream(new BufferedInputStream(socket.getInputStream())); |
390 |
|
|
391 |
|
SendRequest(); |
392 |
|
ReceiveReply(); |
393 |
|
|
394 |
return(in_stream); |
return(in_stream); |
395 |
} |
} |
396 |
|
|
397 |
|
public java.io.OutputStream |
398 |
|
getOutputStream() throws java.io.IOException |
399 |
|
{ |
400 |
|
if(!doOutput) |
401 |
|
throw new ProtocolException |
402 |
|
("Want output stream while haven't setDoOutput(true)"); |
403 |
|
if(!method.equals("POST")) //But we might support "PUT" in future |
404 |
|
setRequestMethod("POST"); |
405 |
|
|
406 |
|
if (!connected) |
407 |
|
connect(); |
408 |
|
|
409 |
|
if(buffered_out_stream == null) |
410 |
|
buffered_out_stream = new ByteArrayOutputStream(256); //default is too small |
411 |
|
|
412 |
|
return buffered_out_stream; |
413 |
|
} |
414 |
|
|
415 |
} // class HttpURLConnection |
} // class HttpURLConnection |
416 |
|
|