use of io.undertow.server.handlers.BlockingHandler in project undertow by undertow-io.
the class FormDataParserTestCase method handlerChains.
@Parameterized.Parameters
public static Collection<Object[]> handlerChains() {
List<Object[]> ret = new ArrayList<>();
final FormParserFactory parserFactory = FormParserFactory.builder().build();
HttpHandler fd = new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final FormDataParser parser = parserFactory.createParser(exchange);
parser.parse(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
FormData data = exchange.getAttachment(FormDataParser.FORM_DATA);
Iterator<String> it = data.iterator();
while (it.hasNext()) {
String fd = it.next();
for (FormData.FormValue val : data.get(fd)) {
exchange.getResponseHeaders().add(new HttpString(fd), val.getValue());
}
}
}
});
}
};
ret.add(new Object[] { fd });
final BlockingHandler blocking = new BlockingHandler();
blocking.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final FormDataParser parser = parserFactory.createParser(exchange);
try {
FormData data = parser.parseBlocking();
Iterator<String> it = data.iterator();
while (it.hasNext()) {
String fd = it.next();
for (FormData.FormValue val : data.get(fd)) {
exchange.getResponseHeaders().add(new HttpString(fd), val.getValue());
}
}
} catch (IOException e) {
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
}
}
});
ret.add(new Object[] { blocking });
return ret;
}
use of io.undertow.server.handlers.BlockingHandler in project undertow by undertow-io.
the class MultipartFormDataParserTestCase method testFileUpload.
@Test
public void testFileUpload() throws Exception {
DefaultServer.setRootHandler(new BlockingHandler(createHandler()));
TestHttpClient client = new TestHttpClient();
try {
HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
//post.setHeader(Headers.CONTENT_TYPE, MultiPartHandler.MULTIPART_FORM_DATA);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
entity.addPart("file", new FileBody(new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile())));
post.setEntity(entity);
HttpResponse result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.BlockingHandler in project undertow by undertow-io.
the class MultipartFormDataParserTestCase method testQuotedBoundary.
@Test
public void testQuotedBoundary() throws Exception {
DefaultServer.setRootHandler(new BlockingHandler(createHandler()));
TestHttpClient client = new TestHttpClient();
try {
HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
post.setHeader(Headers.CONTENT_TYPE_STRING, "multipart/form-data; boundary=\"s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje\"");
StringEntity entity = new StringEntity("--s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje\r\n" + "Content-Disposition: form-data; name=\"formValue\"\r\n" + "\r\n" + "myValue\r\n" + "--s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"uploadfile.txt\"\r\n" + "Content-Type: application/octet-stream\r\n" + "\r\n" + "file contents\r\n" + "\r\n" + "--s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje--\r\n");
post.setEntity(entity);
HttpResponse result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.BlockingHandler in project undertow by undertow-io.
the class SimpleBlockingServerTestCase method setup.
@BeforeClass
public static void setup() {
final BlockingHandler blockingHandler = new BlockingHandler();
DefaultServer.setRootHandler(blockingHandler);
blockingHandler.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) {
try {
if (exchange.getRequestMethod().equals(Methods.POST)) {
//for a post we just echo back what was sent
//we need to fully buffer it, as otherwise the send buffer fills up, and the client will still be blocked
//on writing and will never read
byte[] buffer = new byte[1024];
final ByteArrayOutputStream b = new ByteArrayOutputStream();
int r = 0;
final OutputStream outputStream = exchange.getOutputStream();
final InputStream inputStream = exchange.getInputStream();
while ((r = inputStream.read(buffer)) > 0) {
b.write(buffer, 0, r);
}
outputStream.write(b.toByteArray());
outputStream.close();
} else {
if (exchange.getQueryParameters().containsKey("useFragmentedSender")) {
//we send it byte at a time
exchange.getResponseSender().send("", new IoCallback() {
int i = 0;
@Override
public void onComplete(final HttpServerExchange exchange, final Sender sender) {
if (i == message.length()) {
sender.close();
exchange.endExchange();
} else {
sender.send("" + message.charAt(i++), this);
}
}
@Override
public void onException(final HttpServerExchange exchange, final Sender sender, final IOException exception) {
exchange.endExchange();
}
});
} else if (exchange.getQueryParameters().containsKey("useSender")) {
exchange.getResponseSender().send(message, IoCallback.END_EXCHANGE);
} else {
final OutputStream outputStream = exchange.getOutputStream();
outputStream.write(message.getBytes());
outputStream.close();
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
use of io.undertow.server.handlers.BlockingHandler in project undertow by undertow-io.
the class MaxRequestSizeTestCase method setup.
@BeforeClass
public static void setup() {
final BlockingHandler blockingHandler = new BlockingHandler();
DefaultServer.setRootHandler(blockingHandler);
blockingHandler.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final OutputStream outputStream = exchange.getOutputStream();
final InputStream inputStream = exchange.getInputStream();
String m = HttpClientUtils.readResponse(inputStream);
Assert.assertEquals(A_MESSAGE, m);
inputStream.close();
outputStream.close();
}
});
}
Aggregations