Skip to content

Commit

Permalink
fix(platform-vertx): Fix NPE where HTTP reasonCode or Exception messa…
Browse files Browse the repository at this point in the history
…ges are not set.

fix(platform-vertx): Ensure that Host header is not suppressed before the connector

Resolves APIMAN-1271
  • Loading branch information
msavy committed Jun 21, 2017
1 parent 98316d7 commit 9d45f2c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
Expand Up @@ -60,8 +60,8 @@ public void write(ApiRequest request, Throwable error, IApiClientResponse respon
if (request != null && request.getApi() != null && "xml".equals(request.getApi().getEndpointContentType())) {
isXml = true;
}

response.setHeader("X-Gateway-Error", error.getMessage());
String message = (error.getMessage() == null) ? "" : error.getMessage(); // TODO get and/or print ultimate cause?
response.setHeader("X-Gateway-Error", message);
response.setStatusCode(500);

EngineErrorResponse eer = createErrorResponse(error);
Expand Down
Expand Up @@ -49,7 +49,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map.Entry;
import java.util.Set;

Expand All @@ -65,15 +65,17 @@
@SuppressWarnings("nls")
class HttpConnector implements IApiConnectionResponse, IApiConnection {

private static final Set<String> SUPPRESSED_HEADERS = new HashSet<>();
private static final Set<String> SUPPRESSED_REQUEST_HEADERS = new LinkedHashSet<>();
private static final Set<String> SUPPRESSED_RESPONSE_HEADERS = new LinkedHashSet<>();

static {
SUPPRESSED_HEADERS.add("Transfer-Encoding");
SUPPRESSED_HEADERS.add("X-API-Key");
SUPPRESSED_HEADERS.add("Host");
SUPPRESSED_REQUEST_HEADERS.add("X-API-Key");
SUPPRESSED_REQUEST_HEADERS.add("Host");

SUPPRESSED_RESPONSE_HEADERS.add("Connection");
}

private Logger logger = LoggerFactory.getLogger(this.getClass());

private ApiRequest apiRequest;
private ApiResponse apiResponse;

Expand Down Expand Up @@ -171,7 +173,7 @@ private void doConnection() {
// Pause until we're given permission to xfer the response.
vxClientResponse.pause();

apiResponse = HttpApiFactory.buildResponse(vxClientResponse, SUPPRESSED_HEADERS);
apiResponse = HttpApiFactory.buildResponse(vxClientResponse, SUPPRESSED_RESPONSE_HEADERS);

vxClientResponse.handler((Handler<Buffer>) chunk -> {
bodyHandler.handle(new VertxApimanBuffer(chunk));
Expand All @@ -195,7 +197,12 @@ private void doConnection() {
clientRequest.setChunked(true);
}

apiRequest.getHeaders().forEach(e -> clientRequest.headers().add(e.getKey(), e.getValue()));
apiRequest.getHeaders()
.forEach(e -> {
if (!SUPPRESSED_REQUEST_HEADERS.contains(e.getKey())) {
clientRequest.headers().add(e.getKey(), e.getValue());
}
});

addMandatoryRequestHeaders(clientRequest.headers());

Expand Down
Expand Up @@ -45,7 +45,7 @@ public class HttpApiFactory {
private final static Set<String> IGNORESET = new HashSet<>();
static {
IGNORESET.add(ApimanPathUtils.X_API_VERSION_HEADER);
IGNORESET.add("Host");
//IGNORESET.add("Host");
}

private static IApiRequestPathParser requestPathParser;
Expand All @@ -57,7 +57,7 @@ public static void init(IApiRequestPathParser requestPathParser) {
public static ApiResponse buildResponse(HttpClientResponse response, Set<String> suppressHeaders) {
ApiResponse apimanResponse = new ApiResponse();
apimanResponse.setCode(response.statusCode());
apimanResponse.setMessage(response.statusMessage());
apimanResponse.setMessage(response.statusMessage() == null ? "" : response.statusMessage());
multimapToMap(apimanResponse.getHeaders(), response.headers(), suppressHeaders);
return apimanResponse;
}
Expand All @@ -69,7 +69,7 @@ public static void buildResponse(HttpServerResponse httpServerResponse, ApiRespo
}
});
httpServerResponse.setStatusCode(amanResponse.getCode());
httpServerResponse.setStatusMessage(amanResponse.getMessage());
httpServerResponse.setStatusMessage(amanResponse.getMessage() == null ? "" : amanResponse.getMessage());
}

public static ApiRequest buildRequest(HttpServerRequest req, boolean isTransportSecure) {
Expand Down

0 comments on commit 9d45f2c

Please sign in to comment.