Commit 94a5ee4a authored by Ricki Hirner's avatar Ricki Hirner

Upgrade to okhttp3

parent c67d40c4
......@@ -22,10 +22,10 @@ android {
}
dependencies {
compile 'com.squareup.okhttp:okhttp:2.7.2'
compile 'com.squareup.okhttp:logging-interceptor:2.7.2'
compile 'com.squareup.okhttp3:okhttp:3.0.1+'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1+'
provided 'org.projectlombok:lombok:1.16.6'
compile 'org.slf4j:slf4j-android:1.7.13'
androidTestCompile 'com.squareup.okhttp:mockwebserver:2.7.2'
androidTestCompile 'com.squareup.okhttp3:mockwebserver:3.0.1+'
}
......@@ -8,9 +8,9 @@
package at.bitfire.dav4android;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import junit.framework.TestCase;
......
package at.bitfire.dav4android;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.ResponseBody;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import junit.framework.TestCase;
......@@ -29,7 +29,9 @@ public class DavResourceTest extends TestCase {
private static final String
sampleText = "SAMPLE RESPONSE";
private OkHttpClient httpClient = new OkHttpClient();
private OkHttpClient httpClient = new OkHttpClient.Builder()
.followRedirects(false)
.build();
private MockWebServer mockServer = new MockWebServer();
......
/*
* Copyright © 2013 – 2016 Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.dav4android;
import junit.framework.TestCase;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
public class OkHttpTest extends TestCase {
private OkHttpClient httpClient = new OkHttpClient();
private MockWebServer mockServer = new MockWebServer();
@Override
public void setUp() throws IOException {
mockServer.start();
}
@Override
public void tearDown() throws IOException {
mockServer.shutdown();
}
public void testMultipleHeaders() throws IOException {
mockServer.enqueue(new MockResponse()
.setResponseCode(200)
.addHeader("Test", "A, B")
.addHeader("Test", "C, D")
);
Response response = httpClient.newCall(new Request.Builder()
.url(mockServer.url("/"))
.build()).execute();
assertEquals(200, response.code());
//assertEquals("A, B, C, D", response.header("Test"));
}
}
package at.bitfire.dav4android;
import com.squareup.okhttp.HttpUrl;
import okhttp3.HttpUrl;
import junit.framework.TestCase;
......
......@@ -8,10 +8,10 @@
package at.bitfire.dav4android.exception;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.internal.http.HttpDate;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.http.HttpDate;
import junit.framework.TestCase;
......
......@@ -10,20 +10,19 @@ package at.bitfire.dav4android;
import android.text.TextUtils;
import com.squareup.okhttp.Authenticator;
import com.squareup.okhttp.Credentials;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.net.Proxy;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.NonNull;
import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.Route;
import okio.Buffer;
import okio.ByteString;
......@@ -55,10 +54,10 @@ public class BasicDigestAuthenticator implements Authenticator {
@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
public Request authenticate(Route route, Response response) throws IOException {
Request request = response.request();
if (host != null && !request.httpUrl().host().equalsIgnoreCase(host)) {
if (host != null && !request.url().host().equalsIgnoreCase(host)) {
Constants.log.warn("Not authenticating against " + host + " for security reasons!");
return null;
}
......@@ -82,7 +81,7 @@ public class BasicDigestAuthenticator implements Authenticator {
// credentials didn't work last time, and they won't work now -> stop here
return null;
Constants.log.debug("Adding Digest authorization request for {}", request.httpUrl());
Constants.log.debug("Adding Digest authorization request for {}", request.url());
return authorizationRequest(request, digestAuth);
} else if (basicAuth != null) {
......@@ -90,7 +89,7 @@ public class BasicDigestAuthenticator implements Authenticator {
if (triedBefore) // credentials didn't work last time, and they won't work now -> stop here
return null;
Constants.log.debug("Adding Basic authorization header for {}", request.httpUrl());
Constants.log.debug("Adding Basic authorization header for {}", request.url());
return request.newBuilder()
.header(HEADER_AUTHORIZATION, Credentials.basic(username, password))
.build();
......@@ -101,12 +100,6 @@ public class BasicDigestAuthenticator implements Authenticator {
return null;
}
@Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return null;
}
protected Request authorizationRequest(Request request, HttpUtils.AuthScheme digest) {
String realm = digest.params.get("realm"),
opaque = digest.params.get("opaque"),
......@@ -135,7 +128,7 @@ public class BasicDigestAuthenticator implements Authenticator {
params.add("algorithm=" + quotedString(algorithm.name));
final String method = request.method();
final String digestURI = request.httpUrl().encodedPath();
final String digestURI = request.url().encodedPath();
params.add("uri=" + quotedString(digestURI));
if (qop != null) {
......
......@@ -8,12 +8,11 @@
package at.bitfire.dav4android;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.slf4j.Logger;
import org.xmlpull.v1.XmlSerializer;
......@@ -23,6 +22,7 @@ import java.io.StringWriter;
import at.bitfire.dav4android.exception.DavException;
import at.bitfire.dav4android.exception.HttpException;
import okhttp3.HttpUrl;
public class DavAddressBook extends DavResource {
......@@ -56,9 +56,6 @@ public class DavAddressBook extends DavResource {
serializer.endTag(XmlUtils.NS_CARDDAV, "addressbook-query");
serializer.endDocument();
// redirects must not followed automatically (as it may rewrite REPORT requests to GET requests)
httpClient.setFollowRedirects(false);
Response response = httpClient.newCall(new Request.Builder()
.url(location)
.method("REPORT", RequestBody.create(MIME_XML, writer.toString()))
......@@ -105,9 +102,6 @@ public class DavAddressBook extends DavResource {
serializer.endTag(XmlUtils.NS_CARDDAV, "addressbook-multiget");
serializer.endDocument();
// redirects must not followed automatically (as it may rewrite REPORT requests to GET requests)
httpClient.setFollowRedirects(false);
Response response = httpClient.newCall(new Request.Builder()
.url(location)
.method("REPORT", RequestBody.create(MIME_XML, writer.toString()))
......
......@@ -8,12 +8,12 @@
package at.bitfire.dav4android;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.slf4j.Logger;
import org.xmlpull.v1.XmlSerializer;
......@@ -67,9 +67,6 @@ public class DavCalendar extends DavResource {
serializer.endTag(XmlUtils.NS_CALDAV, "calendar-query");
serializer.endDocument();
// redirects must not followed automatically (as it may rewrite REPORT requests to GET requests)
httpClient.setFollowRedirects(false);
Response response = httpClient.newCall(new Request.Builder()
.url(location)
.method("REPORT", RequestBody.create(MIME_XML, writer.toString()))
......@@ -111,9 +108,6 @@ public class DavCalendar extends DavResource {
serializer.endTag(XmlUtils.NS_CALDAV, "calendar-multiget");
serializer.endDocument();
// redirects must not followed automatically (as it may rewrite REPORT requests to GET requests)
httpClient.setFollowRedirects(false);
Response response = httpClient.newCall(new Request.Builder()
.url(location)
.method("REPORT", RequestBody.create(MIME_XML, writer.toString()))
......
......@@ -10,16 +10,6 @@ package at.bitfire.dav4android;
import android.text.TextUtils;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import com.squareup.okhttp.internal.http.StatusLine;
import org.slf4j.Logger;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
......@@ -48,6 +38,15 @@ import at.bitfire.dav4android.property.GetETag;
import at.bitfire.dav4android.property.ResourceType;
import lombok.Cleanup;
import lombok.NonNull;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.http.StatusLine;
public class DavResource {
......@@ -68,8 +67,9 @@ public class DavResource {
/**
* Creates a new DavResource which represents a WebDAV resource at the given location.
* @param log #{@link Logger} which will be used for logging, or null for default
* @param httpClient #{@link OkHttpClient} to access this object
* @param httpClient #{@link OkHttpClient} to access this object, must not follow redirects
* @param location location of the WebDAV resource
* @throws IllegalArgumentException when httpClient follows redirects
*/
public DavResource(Logger log, OkHttpClient httpClient, HttpUrl location) {
this.log = log != null ? log : Constants.log;
......@@ -78,7 +78,8 @@ public class DavResource {
// Don't follow redirects (only useful for GET/POST).
// This means we have to handle 30x responses manually.
httpClient.setFollowRedirects(false);
if (httpClient.followRedirects())
throw new IllegalArgumentException();
}
......@@ -230,9 +231,6 @@ public class DavResource {
serializer.endTag(XmlUtils.NS_WEBDAV, "propfind");
serializer.endDocument();
// redirects must not followed automatically (as it may rewrite PROPFIND requests to GET requests)
httpClient.setFollowRedirects(false);
Response response = null;
for (int attempt = 0; attempt < MAX_REDIRECTS; attempt++) {
response = httpClient.newCall(new Request.Builder()
......
......@@ -2,7 +2,7 @@ package at.bitfire.dav4android;
import android.text.TextUtils;
import com.squareup.okhttp.HttpUrl;
import okhttp3.HttpUrl;
import java.net.URI;
import java.net.URISyntaxException;
......
......@@ -8,7 +8,7 @@
package at.bitfire.dav4android.exception;
import com.squareup.okhttp.Response;
import okhttp3.Response;
import java.net.HttpURLConnection;
......
......@@ -8,13 +8,12 @@
package at.bitfire.dav4android.exception;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.io.Serializable;
import okhttp3.Headers;
import okhttp3.Request;
import okhttp3.Response;
import okio.Buffer;
public class HttpException extends Exception implements Serializable {
......@@ -54,7 +53,7 @@ public class HttpException extends Exception implements Serializable {
// format request
Request request = response.request();
StringBuilder formatted = new StringBuilder();
formatted.append(request.method() + " " + request.urlString() + "\n");
formatted.append(request.method() + " " + request.url().encodedPath() + "\n");
Headers headers = request.headers();
for (String name : headers.names())
for (String value : headers.values(name))
......
......@@ -8,7 +8,7 @@
package at.bitfire.dav4android.exception;
import com.squareup.okhttp.Response;
import okhttp3.Response;
import java.net.HttpURLConnection;
......
......@@ -8,7 +8,7 @@
package at.bitfire.dav4android.exception;
import com.squareup.okhttp.Response;
import okhttp3.Response;
import java.net.HttpURLConnection;
......
......@@ -8,8 +8,8 @@
package at.bitfire.dav4android.exception;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.internal.http.HttpDate;
import okhttp3.Response;
import okhttp3.internal.http.HttpDate;
import java.net.HttpURLConnection;
import java.util.Calendar;
......
......@@ -8,7 +8,7 @@
package at.bitfire.dav4android.exception;
import com.squareup.okhttp.Response;
import okhttp3.Response;
public class UnauthorizedException extends HttpException {
......
......@@ -10,7 +10,7 @@ package at.bitfire.dav4android.property;
import android.util.Log;
import com.squareup.okhttp.MediaType;
import okhttp3.MediaType;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
......
......@@ -2,7 +2,7 @@ package at.bitfire.dav4android.property;
import android.util.Log;
import com.squareup.okhttp.MediaType;
import okhttp3.MediaType;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment