use of org.jboss.netty.handler.codec.http.HttpRequest in project load-balancer by RestComm.
the class Server method createRequest.
private HttpRequest createRequest(String command) {
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
ChannelBuffer buf = ChannelBuffers.copiedBuffer(getStringFromJson(command), Charset.forName("UTF-8"));
request.setHeader(HttpHeaders.Names.CONTENT_TYPE, APPLICATION_JSON);
request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, buf.readableBytes());
request.setContent(buf);
return request;
}
use of org.jboss.netty.handler.codec.http.HttpRequest in project graylog2-server by Graylog2.
the class HttpTransportHandlerTest method messageReceivedReturns404ForWrongPath.
@Test
public void messageReceivedReturns404ForWrongPath() throws Exception {
final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
httpRequest.headers().add("Host", "localhost");
httpRequest.headers().add("Origin", "http://example.com");
httpRequest.headers().add("Connection", "close");
channel.offer(httpRequest);
channel.finish();
final HttpResponse httpResponse = channel.poll();
assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.NOT_FOUND);
final HttpHeaders headers = httpResponse.headers();
assertThat(headers.get(HttpHeaders.Names.CONTENT_LENGTH)).isEqualTo("0");
assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://example.com");
assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isEqualTo("true");
assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS)).isEqualTo("Authorization, Content-Type");
}
use of org.jboss.netty.handler.codec.http.HttpRequest in project graylog2-server by Graylog2.
the class HttpTransportHandlerTest method messageReceivedReturns405ForInvalidMethod.
@Test
public void messageReceivedReturns405ForInvalidMethod() throws Exception {
final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
httpRequest.headers().add("Host", "localhost");
httpRequest.headers().add("Origin", "http://example.com");
httpRequest.headers().add("Connection", "close");
channel.offer(httpRequest);
channel.finish();
final HttpResponse httpResponse = channel.poll();
assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.METHOD_NOT_ALLOWED);
final HttpHeaders headers = httpResponse.headers();
assertThat(headers.get(HttpHeaders.Names.CONTENT_LENGTH)).isEqualTo("0");
assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://example.com");
assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isEqualTo("true");
assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS)).isEqualTo("Authorization, Content-Type");
}
use of org.jboss.netty.handler.codec.http.HttpRequest in project graylog2-server by Graylog2.
the class HttpTransportHandlerTest method messageReceivedSuccessfullyProcessesOPTIONSRequest.
@Test
public void messageReceivedSuccessfullyProcessesOPTIONSRequest() throws Exception {
final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS, "/gelf");
httpRequest.headers().add("Host", "localhost");
httpRequest.headers().add("Origin", "http://example.com");
httpRequest.headers().add("Connection", "close");
channel.offer(httpRequest);
channel.finish();
final HttpResponse httpResponse = channel.poll();
assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.OK);
final HttpHeaders headers = httpResponse.headers();
assertThat(headers.get(HttpHeaders.Names.CONTENT_LENGTH)).isEqualTo("0");
assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://example.com");
assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isEqualTo("true");
assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS)).isEqualTo("Authorization, Content-Type");
}
use of org.jboss.netty.handler.codec.http.HttpRequest in project hadoop by apache.
the class TestShuffleHandler method testMaxConnections.
/**
* Validate the limit on number of shuffle connections.
*
* @throws Exception exception
*/
@Test(timeout = 10000)
public void testMaxConnections() throws Exception {
Configuration conf = new Configuration();
conf.setInt(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY, 0);
conf.setInt(ShuffleHandler.MAX_SHUFFLE_CONNECTIONS, 3);
ShuffleHandler shuffleHandler = new ShuffleHandler() {
@Override
protected Shuffle getShuffle(Configuration conf) {
// replace the shuffle handler with one stubbed for testing
return new Shuffle(conf) {
@Override
protected MapOutputInfo getMapOutputInfo(String mapId, int reduce, String jobId, String user) throws IOException {
// Do nothing.
return null;
}
@Override
protected void populateHeaders(List<String> mapIds, String jobId, String user, int reduce, HttpRequest request, HttpResponse response, boolean keepAliveParam, Map<String, MapOutputInfo> infoMap) throws IOException {
// Do nothing.
}
@Override
protected void verifyRequest(String appid, ChannelHandlerContext ctx, HttpRequest request, HttpResponse response, URL requestUri) throws IOException {
// Do nothing.
}
@Override
protected ChannelFuture sendMapOutput(ChannelHandlerContext ctx, Channel ch, String user, String mapId, int reduce, MapOutputInfo info) throws IOException {
// send a shuffle header and a lot of data down the channel
// to trigger a broken pipe
ShuffleHeader header = new ShuffleHeader("dummy_header", 5678, 5678, 1);
DataOutputBuffer dob = new DataOutputBuffer();
header.write(dob);
ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
dob = new DataOutputBuffer();
for (int i = 0; i < 100000; ++i) {
header.write(dob);
}
return ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
}
};
}
};
shuffleHandler.init(conf);
shuffleHandler.start();
// setup connections
int connAttempts = 3;
HttpURLConnection[] conns = new HttpURLConnection[connAttempts];
for (int i = 0; i < connAttempts; i++) {
String URLstring = "http://127.0.0.1:" + shuffleHandler.getConfig().get(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY) + "/mapOutput?job=job_12345_1&reduce=1&map=attempt_12345_1_m_" + i + "_0";
URL url = new URL(URLstring);
conns[i] = (HttpURLConnection) url.openConnection();
conns[i].setRequestProperty(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME);
conns[i].setRequestProperty(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION);
}
// Try to open numerous connections
for (int i = 0; i < connAttempts; i++) {
conns[i].connect();
}
//Ensure first connections are okay
conns[0].getInputStream();
int rc = conns[0].getResponseCode();
Assert.assertEquals(HttpURLConnection.HTTP_OK, rc);
conns[1].getInputStream();
rc = conns[1].getResponseCode();
Assert.assertEquals(HttpURLConnection.HTTP_OK, rc);
// This connection should be closed because it to above the limit
try {
rc = conns[2].getResponseCode();
Assert.assertEquals("Expected a too-many-requests response code", ShuffleHandler.TOO_MANY_REQ_STATUS.getCode(), rc);
long backoff = Long.valueOf(conns[2].getHeaderField(ShuffleHandler.RETRY_AFTER_HEADER));
Assert.assertTrue("The backoff value cannot be negative.", backoff > 0);
conns[2].getInputStream();
Assert.fail("Expected an IOException");
} catch (IOException ioe) {
LOG.info("Expected - connection should not be open");
} catch (NumberFormatException ne) {
Assert.fail("Expected a numerical value for RETRY_AFTER header field");
} catch (Exception e) {
Assert.fail("Expected a IOException");
}
shuffleHandler.stop();
}
Aggregations