Skip to content

Commit

Permalink
Send an :authority header with requests. Closes jchambers#296.
Browse files Browse the repository at this point in the history
  • Loading branch information
jchambers committed May 27, 2016
1 parent 95064c4 commit 05d8c27
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pushy/src/main/java/com/relayrides/pushy/apns/ApnsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.security.KeyStore.PrivateKeyEntry;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -455,6 +456,7 @@ protected void configurePipeline(final ChannelHandlerContext context, final Stri
final ApnsClientHandler<T> apnsClientHandler = new ApnsClientHandler.ApnsClientHandlerBuilder<T>()
.server(false)
.apnsClient(ApnsClient.this)
.authority(((InetSocketAddress) context.channel().remoteAddress()).getHostName())
.maxUnflushedNotifications(ApnsClient.this.maxUnflushedNotifications)
.encoderEnforceMaxConcurrentStreams(true)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -65,7 +66,8 @@ class ApnsClientHandler<T extends ApnsPushNotification> extends Http2ConnectionH
private final Map<Integer, T> pushNotificationsByStreamId = new HashMap<>();
private final Map<Integer, Http2Headers> headersByStreamId = new HashMap<>();

private ApnsClient<T> apnsClient;
private final ApnsClient<T> apnsClient;
private final String authority;

private long nextPingId = new Random().nextLong();
private ScheduledFuture<?> pingTimeoutFuture;
Expand Down Expand Up @@ -93,6 +95,7 @@ class ApnsClientHandler<T extends ApnsPushNotification> extends Http2ConnectionH
public static class ApnsClientHandlerBuilder<S extends ApnsPushNotification> extends AbstractHttp2ConnectionHandlerBuilder<ApnsClientHandler<S>, ApnsClientHandlerBuilder<S>> {

private ApnsClient<S> apnsClient;
private String authority;
private int maxUnflushedNotifications = 0;

public ApnsClientHandlerBuilder<S> apnsClient(final ApnsClient<S> apnsClient) {
Expand All @@ -104,6 +107,15 @@ public ApnsClient<S> apnsClient() {
return this.apnsClient;
}

public ApnsClientHandlerBuilder<S> authority(final String authority) {
this.authority = authority;
return this;
}

public String authority() {
return this.authority;
}

public ApnsClientHandlerBuilder<S> maxUnflushedNotifications(final int maxUnflushedNotifications) {
this.maxUnflushedNotifications = maxUnflushedNotifications;
return this;
Expand All @@ -125,7 +137,9 @@ public ApnsClientHandlerBuilder<S> encoderEnforceMaxConcurrentStreams(final bool

@Override
public ApnsClientHandler<S> build(final Http2ConnectionDecoder decoder, final Http2ConnectionEncoder encoder, final Http2Settings initialSettings) {
final ApnsClientHandler<S> handler = new ApnsClientHandler<>(decoder, encoder, initialSettings, this.apnsClient(), this.maxUnflushedNotifications());
Objects.requireNonNull(this.authority(), "Authority must be set before building an ApnsClientHandler.");

final ApnsClientHandler<S> handler = new ApnsClientHandler<>(decoder, encoder, initialSettings, this.apnsClient(), this.authority(), this.maxUnflushedNotifications());
this.frameListener(handler.new ApnsClientHandlerFrameAdapter());
return handler;
}
Expand Down Expand Up @@ -210,10 +224,11 @@ public void onGoAwayRead(final ChannelHandlerContext context, final int lastStre
}
}

protected ApnsClientHandler(final Http2ConnectionDecoder decoder, final Http2ConnectionEncoder encoder, final Http2Settings initialSettings, final ApnsClient<T> apnsClient, final int maxUnflushedNotifications) {
protected ApnsClientHandler(final Http2ConnectionDecoder decoder, final Http2ConnectionEncoder encoder, final Http2Settings initialSettings, final ApnsClient<T> apnsClient, final String authority, final int maxUnflushedNotifications) {
super(decoder, encoder, initialSettings);

this.apnsClient = apnsClient;
this.authority = authority;
this.maxUnflushedNotifications = maxUnflushedNotifications;
}

Expand All @@ -228,6 +243,7 @@ public void write(final ChannelHandlerContext context, final Object message, fin

final Http2Headers headers = new DefaultHttp2Headers()
.method(HttpMethod.POST.asciiName())
.authority(this.authority)
.path(APNS_PATH_PREFIX + pushNotification.getToken())
.addInt(APNS_EXPIRATION_HEADER, pushNotification.getExpiration() == null ? 0 : (int) (pushNotification.getExpiration().getTime() / 1000));

Expand Down

0 comments on commit 05d8c27

Please sign in to comment.