use of java.net.ProtocolException in project robovm by robovm.
the class RawHeaders method setStatusLine.
/** Sets the response status line (like "HTTP/1.0 200 OK"). */
public void setStatusLine(String statusLine) throws IOException {
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
if (this.responseMessage != null) {
throw new IllegalStateException("statusLine is already set");
}
// We allow empty message without leading white space since some servers
// do not send the white space when the message is empty.
boolean hasMessage = statusLine.length() > 13;
if (!statusLine.startsWith("HTTP/1.") || statusLine.length() < 12 || statusLine.charAt(8) != ' ' || (hasMessage && statusLine.charAt(12) != ' ')) {
throw new ProtocolException("Unexpected status line: " + statusLine);
}
int httpMinorVersion = statusLine.charAt(7) - '0';
if (httpMinorVersion < 0 || httpMinorVersion > 9) {
throw new ProtocolException("Unexpected status line: " + statusLine);
}
int responseCode;
try {
responseCode = Integer.parseInt(statusLine.substring(9, 12));
} catch (NumberFormatException e) {
throw new ProtocolException("Unexpected status line: " + statusLine);
}
this.responseMessage = hasMessage ? statusLine.substring(13) : "";
this.responseCode = responseCode;
this.statusLine = statusLine;
this.httpMinorVersion = httpMinorVersion;
}
use of java.net.ProtocolException in project robovm by robovm.
the class HttpURLConnectionImpl method processResponseHeaders.
/**
* Returns the retry action to take for the current response headers. The
* headers, proxy and target URL or this connection may be adjusted to
* prepare for a follow up request.
*/
private Retry processResponseHeaders() throws IOException {
Proxy selectedProxy = httpEngine.connection != null ? httpEngine.connection.getRoute().getProxy() : client.getProxy();
final int responseCode = getResponseCode();
switch(responseCode) {
case HTTP_PROXY_AUTH:
if (selectedProxy.type() != Proxy.Type.HTTP) {
throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
}
// fall-through
case HTTP_UNAUTHORIZED:
boolean credentialsFound = HttpAuthenticator.processAuthHeader(client.getAuthenticator(), getResponseCode(), httpEngine.getResponseHeaders().getHeaders(), rawRequestHeaders, selectedProxy, url);
return credentialsFound ? Retry.SAME_CONNECTION : Retry.NONE;
case HTTP_MULT_CHOICE:
case HTTP_MOVED_PERM:
case HTTP_MOVED_TEMP:
case HTTP_SEE_OTHER:
case HTTP_TEMP_REDIRECT:
if (!getInstanceFollowRedirects()) {
return Retry.NONE;
}
if (++redirectionCount > MAX_REDIRECTS) {
throw new ProtocolException("Too many redirects: " + redirectionCount);
}
if (responseCode == HTTP_TEMP_REDIRECT && !method.equals("GET") && !method.equals("HEAD")) {
// the user agent MUST NOT automatically redirect the request"
return Retry.NONE;
}
String location = getHeaderField("Location");
if (location == null) {
return Retry.NONE;
}
URL previousUrl = url;
url = new URL(previousUrl, location);
if (!url.getProtocol().equals("https") && !url.getProtocol().equals("http")) {
// Don't follow redirects to unsupported protocols.
return Retry.NONE;
}
boolean sameProtocol = previousUrl.getProtocol().equals(url.getProtocol());
if (!sameProtocol && !client.getFollowProtocolRedirects()) {
// This client doesn't follow redirects across protocols.
return Retry.NONE;
}
boolean sameHost = previousUrl.getHost().equals(url.getHost());
boolean samePort = getEffectivePort(previousUrl) == getEffectivePort(url);
if (sameHost && samePort && sameProtocol) {
return Retry.SAME_CONNECTION;
} else {
return Retry.DIFFERENT_CONNECTION;
}
default:
return Retry.NONE;
}
}
use of java.net.ProtocolException in project robovm by robovm.
the class HttpURLConnectionImpl method getInputStream.
@Override
public final InputStream getInputStream() throws IOException {
if (!doInput) {
throw new ProtocolException("This protocol does not support input");
}
HttpEngine response = getResponse();
// file types
if (getResponseCode() >= HTTP_BAD_REQUEST) {
throw new FileNotFoundException(url.toString());
}
InputStream result = response.getResponseBody();
if (result == null) {
throw new ProtocolException("No response body exists; responseCode=" + getResponseCode());
}
return result;
}
use of java.net.ProtocolException in project robovm by robovm.
the class HttpURLConnectionImpl method getOutputStream.
@Override
public final OutputStream getOutputStream() throws IOException {
connect();
OutputStream out = httpEngine.getRequestBody();
if (out == null) {
throw new ProtocolException("method does not support a request body: " + method);
} else if (httpEngine.hasResponse()) {
throw new ProtocolException("cannot write request body after response has been read");
}
return out;
}
use of java.net.ProtocolException in project robovm by robovm.
the class URLConnectionTest method testGetOutputAfterGetInputStreamFails.
public void testGetOutputAfterGetInputStreamFails() throws Exception {
server.enqueue(new MockResponse());
server.play();
HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
connection.setDoOutput(true);
try {
connection.getInputStream();
connection.getOutputStream();
fail();
} catch (ProtocolException expected) {
}
}
Aggregations