Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore Content-Type in HttpTransport #3508

Merged
merged 1 commit into from Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -58,7 +58,6 @@
import org.jboss.netty.util.HashedWheelTimer;

import javax.inject.Named;
import javax.ws.rs.core.MediaType;
import java.util.LinkedHashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
Expand All @@ -67,7 +66,6 @@
import java.util.concurrent.TimeUnit;

import static com.codahale.metrics.MetricRegistry.name;
import static com.google.common.base.Strings.isNullOrEmpty;
import static org.jboss.netty.channel.Channels.fireMessageReceived;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Values;
Expand All @@ -76,7 +74,6 @@
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.METHOD_NOT_ALLOWED;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.OK;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.UNSUPPORTED_MEDIA_TYPE;

public class HttpTransport extends AbstractTcpTransport {
static final int DEFAULT_MAX_INITIAL_LINE_LENGTH = 4096;
Expand Down Expand Up @@ -214,15 +211,9 @@ public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent
final ChannelBuffer buffer = request.getContent();

final boolean correctPath = "/gelf".equals(request.getUri());
final String contentType = request.headers().get(Names.CONTENT_TYPE);
final boolean correctContentType = isNullOrEmpty(contentType)
|| MediaType.APPLICATION_JSON.equals(contentType)
|| MediaType.APPLICATION_FORM_URLENCODED.equals(contentType);

if (!correctPath) {
writeResponse(channel, keepAlive, httpRequestVersion, NOT_FOUND, origin);
} else if (!correctContentType) {
writeResponse(channel, keepAlive, httpRequestVersion, UNSUPPORTED_MEDIA_TYPE, origin);
} else {
// send on to raw message handler
writeResponse(channel, keepAlive, httpRequestVersion, ACCEPTED, origin);
Expand Down
Expand Up @@ -37,7 +37,7 @@
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -65,7 +65,7 @@ public class GELFHttpHandlerTest {

@Before
public void setUp() throws Exception {
ChannelBuffer channelBuffer = ChannelBuffers.copiedBuffer("{}", Charset.defaultCharset());
ChannelBuffer channelBuffer = ChannelBuffers.copiedBuffer("{}", StandardCharsets.UTF_8);

when(headers.get(HttpHeaders.Names.CONNECTION)).thenReturn(HttpHeaders.Values.CLOSE);

Expand Down Expand Up @@ -224,4 +224,37 @@ public void testNoCorsHeadersForOriginIfDisabled() throws Exception {
assertNull(response.headers().get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS));
assertNull(response.headers().get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS));
}


@Test
public void testWithJavascriptContentType() throws Exception {
when(headers.get(HttpHeaders.Names.CONTENT_TYPE)).thenReturn("application/json");
HttpTransport.Handler handler = new HttpTransport.Handler(true);

handler.messageReceived(ctx, evt);

ArgumentCaptor<HttpResponse> argument = ArgumentCaptor.forClass(HttpResponse.class);
verify(channel).write(argument.capture());
verify(ctx, atMost(1)).sendUpstream(any(ChannelEvent.class));

HttpResponse response = argument.getValue();
assertEquals(HttpResponseStatus.ACCEPTED, response.getStatus());
assertEquals(response.headers().get(HttpHeaders.Names.CONNECTION), HttpHeaders.Values.CLOSE);
}

@Test
public void testWithArbitraryContentType() throws Exception {
when(headers.get(HttpHeaders.Names.CONTENT_TYPE)).thenReturn("foo/bar");
HttpTransport.Handler handler = new HttpTransport.Handler(true);

handler.messageReceived(ctx, evt);

ArgumentCaptor<HttpResponse> argument = ArgumentCaptor.forClass(HttpResponse.class);
verify(channel).write(argument.capture());
verify(ctx, atMost(1)).sendUpstream(any(ChannelEvent.class));

HttpResponse response = argument.getValue();
assertEquals(HttpResponseStatus.ACCEPTED, response.getStatus());
assertEquals(response.headers().get(HttpHeaders.Names.CONNECTION), HttpHeaders.Values.CLOSE);
}
}