Bug 983350 - Include User-Agent header for FxAccount and TokenServer requests. r=rnewman

--HG--
extra : rebase_source : cb622c61646be2752e4900fd6e480874e9ba5406
This commit is contained in:
Nick Alexander 2014-03-13 17:13:58 -07:00
Родитель 3dca6ed1e7
Коммит 220e4b14a1
25 изменённых файлов: 115 добавлений и 14 удалений

Просмотреть файл

@ -45,6 +45,6 @@ public class AnnouncementsConstants {
public static long LATEST_ACCEPTED_LAUNCH_TIMESTAMP_MSEC = GlobalConstants.BUILD_TIMESTAMP_MSEC +
4 * 365 * MILLISECONDS_PER_DAY;
public static String ANNOUNCE_USER_AGENT = "Firefox Announcements " + GlobalConstants.MOZ_APP_VERSION;
public static String USER_AGENT = "Firefox Announcements " + GlobalConstants.MOZ_APP_VERSION;
public static String ANNOUNCE_CHANNEL = GlobalConstants.MOZ_UPDATE_CHANNEL.replace("default", GlobalConstants.MOZ_OFFICIAL_BRANDING ? "release" : "dev");
}

Просмотреть файл

@ -46,12 +46,16 @@ public class AnnouncementsFetchResourceDelegate extends BaseResourceDelegate {
this.delegate = delegate;
}
@Override
public String getUserAgent() {
return delegate.getUserAgent();
}
@Override
public void addHeaders(HttpRequestBase request, DefaultHttpClient client) {
super.addHeaders(request, client);
// The basics.
request.addHeader("User-Agent", delegate.getUserAgent());
request.addHeader("Accept-Language", delegate.getLocale().toString());
request.addHeader("Accept", ACCEPT_HEADER);
@ -176,4 +180,4 @@ public class AnnouncementsFetchResourceDelegate extends BaseResourceDelegate {
public AuthHeaderProvider getAuthHeaderProvider() {
return null;
}
}
}

Просмотреть файл

@ -167,6 +167,7 @@ public class AnnouncementsService extends BackgroundService implements Announcem
this.getSharedPreferences().edit().putLong(AnnouncementsConstants.PREF_LAST_FETCH_LOCAL_TIME, fetch).commit();
}
@Override
public long getLastFetch() {
return this.getSharedPreferences().getLong(AnnouncementsConstants.PREF_LAST_FETCH_LOCAL_TIME, 0L);
}
@ -225,7 +226,7 @@ public class AnnouncementsService extends BackgroundService implements Announcem
@Override
public String getUserAgent() {
return AnnouncementsConstants.ANNOUNCE_USER_AGENT;
return AnnouncementsConstants.USER_AGENT;
}
protected void persistTimes(long fetched, String date) {

Просмотреть файл

@ -166,6 +166,11 @@ public class BagheeraClient {
this.delegate = delegate;
}
@Override
public String getUserAgent() {
return delegate.getUserAgent();
}
@Override
public int socketTimeout() {
return DEFAULT_SOCKET_TIMEOUT_MSEC;

Просмотреть файл

@ -10,4 +10,6 @@ public interface BagheeraRequestDelegate {
void handleSuccess(int status, String namespace, String id, HttpResponse response);
void handleError(Exception e);
void handleFailure(int status, String namespace, HttpResponse response);
public String getUserAgent();
}

Просмотреть файл

@ -20,6 +20,7 @@ import javax.crypto.Mac;
import org.json.simple.JSONObject;
import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientMalformedResponseException;
import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException;
import org.mozilla.gecko.fxa.FxAccountConstants;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.Utils;
import org.mozilla.gecko.sync.crypto.HKDF;
@ -205,6 +206,11 @@ public class FxAccountClient10 {
return super.getAuthHeaderProvider();
}
@Override
public String getUserAgent() {
return FxAccountConstants.USER_AGENT;
}
@Override
public void handleHttpResponse(HttpResponse response) {
try {

Просмотреть файл

@ -11,6 +11,8 @@ public class HealthReportConstants {
public static final String HEALTH_AUTHORITY = "@ANDROID_PACKAGE_NAME@.health";
public static final String GLOBAL_LOG_TAG = "GeckoHealth";
public static final String USER_AGENT = "Firefox-Android-HealthReport/ (" + GlobalConstants.MOZ_APP_DISPLAYNAME + " " + GlobalConstants.MOZ_APP_VERSION + ")";
/**
* The earliest allowable value for the last ping time, corresponding to May 2nd 2013.
* Used for sanity checks.

Просмотреть файл

@ -204,6 +204,11 @@ public class AndroidSubmissionClient implements SubmissionClient {
this.id = id;
}
@Override
public String getUserAgent() {
return HealthReportConstants.USER_AGENT;
}
@Override
public void handleSuccess(int status, String namespace, String id, HttpResponse response) {
BaseResource.consumeEntity(response);

Просмотреть файл

@ -9,7 +9,6 @@ import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -37,6 +36,11 @@ public class BrowserIDRemoteVerifierClient implements BrowserIDVerifierClient {
this.delegate = delegate;
}
@Override
public String getUserAgent() {
return null;
}
@Override
public void handleHttpResponse(HttpResponse response) {
SyncResponse res = new SyncResponse(response);

Просмотреть файл

@ -5,6 +5,7 @@
package org.mozilla.gecko.fxa;
import org.mozilla.gecko.background.common.GlobalConstants;
import org.mozilla.gecko.background.common.log.Logger;
public class FxAccountConstants {
@ -31,4 +32,6 @@ public class FxAccountConstants {
// You must wait 15 minutes after failing an age check before trying to create a different account.
public static final long MINIMUM_TIME_TO_WAIT_AFTER_AGE_CHECK_FAILED_IN_MILLISECONDS = 15 * 60 * 1000;
public static final String USER_AGENT = "Firefox-Android-FxAccounts/ (" + GlobalConstants.MOZ_APP_DISPLAYNAME + " " + GlobalConstants.MOZ_APP_VERSION + ")";
}

Просмотреть файл

@ -297,6 +297,11 @@ public class FxAccountSyncAdapter extends AbstractThreadedSyncAdapter {
final TokenServerClientDelegate delegate = new TokenServerClientDelegate() {
private boolean didReceiveBackoff = false;
@Override
public String getUserAgent() {
return FxAccountConstants.USER_AGENT;
}
@Override
public void handleSuccess(final TokenServerToken token) {
FxAccountConstants.pii(LOG_TAG, "Got token! uid is " + token.uid + " and endpoint is " + token.endpoint + ".");

Просмотреть файл

@ -18,9 +18,9 @@ public class SyncConstants {
GlobalConstants.MOZ_APP_VERSION + "." +
SYNC_MINOR_VERSION;
public static final String SYNC_USER_AGENT = "Firefox AndroidSync " +
SYNC_VERSION_STRING + " (" +
GlobalConstants.MOZ_APP_DISPLAYNAME + ")";
public static final String USER_AGENT = "Firefox AndroidSync " +
SYNC_VERSION_STRING + " (" +
GlobalConstants.MOZ_APP_DISPLAYNAME + ")";
public static final String ACCOUNTTYPE_SYNC = "@MOZ_ANDROID_SHARED_ACCOUNT_TYPE@";

Просмотреть файл

@ -9,6 +9,7 @@ import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.SyncConstants;
import org.mozilla.gecko.sync.jpake.JPakeClient;
import org.mozilla.gecko.sync.net.BaseResource;
import org.mozilla.gecko.sync.net.BaseResourceDelegate;
@ -36,6 +37,10 @@ public class DeleteChannel {
return;
}
httpResource.delegate = new BaseResourceDelegate(httpResource) {
@Override
public String getUserAgent() {
return SyncConstants.USER_AGENT;
}
@Override
public void addHeaders(HttpRequestBase request, DefaultHttpClient client) {

Просмотреть файл

@ -10,6 +10,7 @@ import java.security.GeneralSecurityException;
import org.json.simple.parser.JSONParser;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.SyncConstants;
import org.mozilla.gecko.sync.jpake.JPakeClient;
import org.mozilla.gecko.sync.net.BaseResource;
import org.mozilla.gecko.sync.net.BaseResourceDelegate;
@ -80,6 +81,10 @@ public class GetChannelStage extends JPakeStage {
private void makeChannelRequest(final GetChannelStageDelegate callbackDelegate, String getChannelUrl, final String clientId) throws URISyntaxException {
final BaseResource httpResource = new BaseResource(getChannelUrl);
httpResource.delegate = new BaseResourceDelegate(httpResource) {
@Override
public String getUserAgent() {
return SyncConstants.USER_AGENT;
}
@Override
public void addHeaders(HttpRequestBase request, DefaultHttpClient client) {

Просмотреть файл

@ -11,6 +11,7 @@ import java.util.Timer;
import java.util.TimerTask;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.SyncConstants;
import org.mozilla.gecko.sync.jpake.JPakeClient;
import org.mozilla.gecko.sync.net.BaseResource;
import org.mozilla.gecko.sync.net.BaseResourceDelegate;
@ -101,6 +102,10 @@ public class GetRequestStage extends JPakeStage {
private Resource createGetRequest(final GetRequestStageDelegate callbackDelegate, final JPakeClient jpakeClient) throws URISyntaxException {
BaseResource httpResource = new BaseResource(jpakeClient.channelUrl);
httpResource.delegate = new BaseResourceDelegate(httpResource) {
@Override
public String getUserAgent() {
return SyncConstants.USER_AGENT;
}
@Override
public void addHeaders(HttpRequestBase request, DefaultHttpClient client) {

Просмотреть файл

@ -12,10 +12,11 @@ import java.util.Timer;
import java.util.TimerTask;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.SyncConstants;
import org.mozilla.gecko.sync.jpake.JPakeClient;
import org.mozilla.gecko.sync.net.BaseResource;
import org.mozilla.gecko.sync.net.Resource;
import org.mozilla.gecko.sync.net.BaseResourceDelegate;
import org.mozilla.gecko.sync.net.Resource;
import org.mozilla.gecko.sync.setup.Constants;
import ch.boye.httpclientandroidlib.Header;
@ -92,6 +93,10 @@ public class PutRequestStage extends JPakeStage {
private Resource createPutRequest(final PutRequestStageDelegate callbackDelegate, final JPakeClient jpakeClient) throws URISyntaxException {
BaseResource httpResource = new BaseResource(jpakeClient.channelUrl);
httpResource.delegate = new BaseResourceDelegate(httpResource) {
@Override
public String getUserAgent() {
return SyncConstants.USER_AGENT;
}
@Override
public void addHeaders(HttpRequestBase request, DefaultHttpClient client) {

Просмотреть файл

@ -177,6 +177,10 @@ public class BaseResource implements Resource {
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpProtocolParams.setContentCharset(params, charset);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
final String userAgent = delegate.getUserAgent();
if (userAgent != null) {
HttpProtocolParams.setUserAgent(params, userAgent);
}
delegate.addHeaders(request, client);
}

Просмотреть файл

@ -26,6 +26,13 @@ public interface ResourceDelegate {
AuthHeaderProvider getAuthHeaderProvider();
void addHeaders(HttpRequestBase request, DefaultHttpClient client);
/**
* The value of the User-Agent header to include with the request.
*
* @return User-Agent header value; null means do not set User-Agent header.
*/
public String getUserAgent();
// Response handling.
/**

Просмотреть файл

@ -18,7 +18,6 @@ import ch.boye.httpclientandroidlib.HttpResponse;
import ch.boye.httpclientandroidlib.client.ClientProtocolException;
import ch.boye.httpclientandroidlib.client.methods.HttpRequestBase;
import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient;
import ch.boye.httpclientandroidlib.params.CoreProtocolPNames;
public class SyncStorageRequest implements Resource {
public static HashMap<String, String> SERVER_ERROR_MESSAGES;
@ -110,6 +109,11 @@ public class SyncStorageRequest implements Resource {
return request.delegate.getAuthHeaderProvider();
}
@Override
public String getUserAgent() {
return SyncConstants.USER_AGENT;
}
@Override
public void handleHttpResponse(HttpResponse response) {
Logger.debug(LOG_TAG, "SyncStorageResourceDelegate handling response: " + response.getStatusLine() + ".");
@ -146,8 +150,6 @@ public class SyncStorageRequest implements Resource {
@Override
public void addHeaders(HttpRequestBase request, DefaultHttpClient client) {
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, SyncConstants.SYNC_USER_AGENT);
// Clients can use their delegate interface to specify X-If-Unmodified-Since.
String ifUnmodifiedSince = this.request.delegate.ifUnmodifiedSince();
if (ifUnmodifiedSince != null) {
@ -173,18 +175,22 @@ public class SyncStorageRequest implements Resource {
return new SyncStorageResourceDelegate(request);
}
@Override
public void get() {
this.resource.get();
}
@Override
public void delete() {
this.resource.delete();
}
@Override
public void post(HttpEntity body) {
this.resource.post(body);
}
@Override
public void put(HttpEntity body) {
this.resource.put(body);
}

Просмотреть файл

@ -96,13 +96,16 @@ public class AuthenticateAccountStage implements AuthenticatorStage {
public void authenticateAccount(final AuthenticateAccountStageDelegate callbackDelegate, final String authRequestUrl, final String authHeader) throws URISyntaxException {
final BaseResource httpResource = new BaseResource(authRequestUrl);
httpResource.delegate = new BaseResourceDelegate(httpResource) {
@Override
public String getUserAgent() {
return SyncConstants.USER_AGENT;
}
@Override
public void addHeaders(HttpRequestBase request, DefaultHttpClient client) {
// Make reference to request, to abort if necessary.
httpRequest = request;
client.log.enableDebug(true);
request.setHeader(new BasicHeader("User-Agent", SyncConstants.SYNC_USER_AGENT));
// Host header is not set for some reason, so do it explicitly.
try {
URI authServerUri = new URI(authRequestUrl);

Просмотреть файл

@ -13,6 +13,7 @@ import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.SyncConstants;
import org.mozilla.gecko.sync.net.BaseResource;
import org.mozilla.gecko.sync.net.BaseResourceDelegate;
@ -56,6 +57,10 @@ public class EnsureUserExistenceStage implements AuthenticatorStage {
String userRequestUrl = aa.nodeServer + "user/1.0/" + aa.username;
final BaseResource httpResource = new BaseResource(userRequestUrl);
httpResource.delegate = new BaseResourceDelegate(httpResource) {
@Override
public String getUserAgent() {
return SyncConstants.USER_AGENT;
}
@Override
public void handleHttpResponse(HttpResponse response) {

Просмотреть файл

@ -12,6 +12,7 @@ import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.SyncConstants;
import org.mozilla.gecko.sync.Utils;
import org.mozilla.gecko.sync.net.BaseResource;
import org.mozilla.gecko.sync.net.BaseResourceDelegate;
@ -78,6 +79,10 @@ public class FetchUserNodeStage implements AuthenticatorStage {
// Fetch node containing user.
final BaseResource httpResource = new BaseResource(fetchNodeUrl);
httpResource.delegate = new BaseResourceDelegate(httpResource) {
@Override
public String getUserAgent() {
return SyncConstants.USER_AGENT;
}
@Override
public void handleHttpResponse(HttpResponse response) {

Просмотреть файл

@ -15,6 +15,7 @@ import java.security.GeneralSecurityException;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.NodeAuthenticationException;
import org.mozilla.gecko.sync.NullClusterURLException;
import org.mozilla.gecko.sync.SyncConstants;
import org.mozilla.gecko.sync.ThreadPool;
import org.mozilla.gecko.sync.delegates.NodeAssignmentCallback;
import org.mozilla.gecko.sync.net.BaseResource;
@ -76,6 +77,10 @@ public class EnsureClusterURLStage extends AbstractNonRepositorySyncStage {
BaseResource resource = new BaseResource(nodeWeaveURL);
resource.delegate = new BaseResourceDelegate(resource) {
@Override
public String getUserAgent() {
return SyncConstants.USER_AGENT;
}
/**
* Handle the response for GET https://server/pathname/version/username/node/weave.
@ -179,6 +184,7 @@ public class EnsureClusterURLStage extends AbstractNonRepositorySyncStage {
resource.get();
}
@Override
public void execute() throws NoSuchStageException {
final URI oldClusterURL = session.config.getClusterURL();
final boolean wantNodeAssignment = callback.wantNodeAssignment();

Просмотреть файл

@ -256,6 +256,11 @@ public class TokenServerClient {
this.conditionsAccepted = conditionsAccepted;
}
@Override
public String getUserAgent() {
return delegate.getUserAgent();
}
@Override
public void handleHttpResponse(HttpResponse response) {
// Skew.

Просмотреть файл

@ -4,6 +4,7 @@
package org.mozilla.gecko.tokenserver;
public interface TokenServerClientDelegate {
void handleSuccess(TokenServerToken token);
void handleFailure(TokenServerException e);
@ -13,4 +14,6 @@ public interface TokenServerClientDelegate {
* Might be called multiple times, in addition to the other terminating handler methods.
*/
void handleBackoff(int backoffSeconds);
}
public String getUserAgent();
}