*/
Semaphore okToRecieve = null;
+ /** true iff this is the first request to be made on this socket */
+ boolean firstRequest = true;
+
/** cache for resolveAndCheckIfFirewalled() */
static Hashtable resolvedHosts = new Hashtable();
- /** if any request encounters an IOException, the entire HTTP connection is invalidated */
- boolean invalid = false;
-
/** true iff we are allowed to skip the resolve check (only allowed when we're downloading the PAC script) */
boolean skipResolveCheck = false;
Semaphore blockOn = null;
Semaphore releaseMe = null;
synchronized(this) {
- if (invalid) throw new HTTPException("connection failed on a previous pipelined call");
try {
connect();
sendRequest(contentType, content);
} catch (IOException e) {
- invalid = true;
+ reset();
throw e;
}
blockOn = okToRecieve;
boolean doRelease = true;
try {
if (blockOn != null) blockOn.block();
- if (invalid) throw new HTTPException("connection failed on a previous pipelined call");
+
+ // previous call wrecked the socket connection, but we already sent our request, so we can't just retry --
+ // this could cause the server to receive the request twice, which could be bad (think of the case where the
+ // server call causes Amazon.com to ship you an item with one-click purchasing).
+ if (sock == null)
+ throw new HTTPException("a previous pipelined call messed up the socket");
Hashtable h = in == null ? null : parseHeaders(in);
if (h == null) {
+ if (firstRequest) throw new HTTPException("server closed the socket with no response");
// sometimes the server chooses to close the stream between requests
- in = null; sock = null;
+ reset();
releaseMe.release();
return makeRequest(contentType, content);
}
if (h.get("HTTP").equals("1.0") && h.get("content-length") == null) {
if (Log.on) Log.log(this, "proxy returned an HTTP/1.0 reply with no content-length...");
- in = null; sock = null;
+ reset();
} else {
int cl = h.get("content-length") == null ? -1 : Integer.parseInt(h.get("content-length").toString());
new HTTPInputStream(in, cl, releaseMe).close();
}
- } catch (IOException e) { invalid = true; throw e;
+ } catch (IOException e) { reset(); throw e;
} finally { if (doRelease) releaseMe.release();
}
}
// Everything Else ////////////////////////////////////////////////////////////////////////////
private synchronized void connect() throws IOException {
+ if (originalUrl.equals("stdio:")) {
+ in = new BufferedInputStream(System.in);
+ return;
+ }
if (sock != null) {
if (in == null) in = new BufferedInputStream(sock.getInputStream());
return;
if (Log.verbose) Log.log(this, "creating HTTP object for connection to " + host + ":" + port);
Proxy pi = Platform.detectProxy();
- if (sock == null && pi != null && pi.proxyAutoConfigFunction != null) sock = attemptPAC(pi.proxyAutoConfigFunction);
- if (sock == null && pi != null && ssl && pi.httpsProxyHost != null) sock = attemptHttpProxy(pi.httpsProxyHost, pi.httpsProxyPort);
- if (sock == null && pi != null && pi.httpProxyHost != null) sock = attemptHttpProxy(pi.httpProxyHost, pi.httpProxyPort);
- if (sock == null && pi != null && pi.socksProxyHost != null) sock = attemptSocksProxy(pi.socksProxyHost, pi.socksProxyPort);
+ OUTER: do {
+ if (pi != null) {
+ for(int i=0; i<pi.excluded.length; i++) if (host.equals(pi.excluded[i])) break OUTER;
+ if (sock == null && pi.proxyAutoConfigFunction != null) sock = attemptPAC(pi.proxyAutoConfigFunction);
+ if (sock == null && ssl && pi.httpsProxyHost != null) sock = attemptHttpProxy(pi.httpsProxyHost, pi.httpsProxyPort);
+ if (sock == null && pi.httpProxyHost != null) sock = attemptHttpProxy(pi.httpProxyHost, pi.httpProxyPort);
+ if (sock == null && pi.socksProxyHost != null) sock = attemptSocksProxy(pi.socksProxyHost, pi.socksProxyPort);
+ }
+ } while (false);
proxied = sock != null;
if (sock == null) sock = attemptDirect();
if (sock == null) throw new HTTPException("unable to contact host " + host);
public void sendRequest(String contentType, String content) throws IOException {
- PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
+ PrintWriter pw = new PrintWriter(new OutputStreamWriter(originalUrl.equals("stdio:") ? System.out : sock.getOutputStream()));
if (content != null) {
pw.print("POST " + path + " HTTP/1.1\r\n");
int contentLength = content.substring(0, 2).equals("\r\n") ?
}
return ret;
} finally {
- if (!good) invalid = true;
+ if (!good) reset();
}
}
}
}
+ void reset() {
+ firstRequest = true;
+ in = null;
+ sock = null;
+ }
+
// Misc Helpers ///////////////////////////////////////////////////////////////////////////////////
public void perform() {
Box b = new Box();
Template t = Template.getTemplate((Res)Main.builtin.get("org/xwt/builtin/proxy_authorization.xwt"));
- t.apply(b, null, 0, 0, null);
+ t.apply(b, null, null);
b.put("realm", realm);
b.put("proxyIP", proxyIP);
}