use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.
the class MultiPartContentProviderTest method testFieldWithOverridenContentType.
@Test
public void testFieldWithOverridenContentType() throws Exception {
String name = "field";
String value = "è";
Charset encoding = StandardCharsets.ISO_8859_1;
start(new AbstractMultiPartHandler() {
@Override
protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Collection<Part> parts = request.getParts();
Assert.assertEquals(1, parts.size());
Part part = parts.iterator().next();
Assert.assertEquals(name, part.getName());
String contentType = part.getContentType();
Assert.assertNotNull(contentType);
int equal = contentType.lastIndexOf('=');
Charset charset = Charset.forName(contentType.substring(equal + 1));
Assert.assertEquals(encoding, charset);
Assert.assertEquals(value, IO.toString(part.getInputStream(), charset));
}
});
MultiPartContentProvider multiPart = new MultiPartContentProvider();
HttpFields fields = new HttpFields();
fields.put(HttpHeader.CONTENT_TYPE, "text/plain;charset=" + encoding.name());
BytesContentProvider content = new BytesContentProvider(value.getBytes(encoding));
multiPart.addFieldPart(name, content, fields);
multiPart.close();
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send();
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.
the class MultiPartContentProviderTest method testFieldWithFile.
@Test
public void testFieldWithFile() throws Exception {
// Prepare a file to upload.
byte[] data = new byte[1024];
new Random().nextBytes(data);
Path tmpDir = MavenTestingUtils.getTargetTestingPath();
Path tmpPath = Files.createTempFile(tmpDir, "multipart_", ".txt");
try (OutputStream output = Files.newOutputStream(tmpPath, StandardOpenOption.CREATE)) {
output.write(data);
}
String field = "field";
String value = "€";
String fileField = "file";
Charset encoding = StandardCharsets.UTF_8;
String contentType = "text/plain;charset=" + encoding.name();
String headerName = "foo";
String headerValue = "bar";
start(new AbstractMultiPartHandler() {
@Override
protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Part> parts = new ArrayList<>(request.getParts());
Assert.assertEquals(2, parts.size());
Part fieldPart = parts.get(0);
Part filePart = parts.get(1);
if (!field.equals(fieldPart.getName())) {
Part swap = filePart;
filePart = fieldPart;
fieldPart = swap;
}
Assert.assertEquals(field, fieldPart.getName());
Assert.assertEquals(contentType, fieldPart.getContentType());
Assert.assertEquals(value, IO.toString(fieldPart.getInputStream(), encoding));
Assert.assertEquals(headerValue, fieldPart.getHeader(headerName));
Assert.assertEquals(fileField, filePart.getName());
Assert.assertEquals("application/octet-stream", filePart.getContentType());
Assert.assertEquals(tmpPath.getFileName().toString(), filePart.getSubmittedFileName());
Assert.assertEquals(Files.size(tmpPath), filePart.getSize());
Assert.assertArrayEquals(data, IO.readBytes(filePart.getInputStream()));
}
});
MultiPartContentProvider multiPart = new MultiPartContentProvider();
HttpFields fields = new HttpFields();
fields.put(headerName, headerValue);
multiPart.addFieldPart(field, new StringContentProvider(value, encoding), fields);
multiPart.addFilePart(fileField, tmpPath.getFileName().toString(), new PathContentProvider(tmpPath), null);
multiPart.close();
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send();
Assert.assertEquals(200, response.getStatus());
Files.delete(tmpPath);
}
use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.
the class HttpChannelOverFCGI method exchangeTerminated.
@Override
public void exchangeTerminated(HttpExchange exchange, Result result) {
super.exchangeTerminated(exchange, result);
idle.onClose();
HttpFields responseHeaders = result.getResponse().getHeaders();
if (result.isFailed())
connection.close(result.getFailure());
else if (!connection.closeByHTTP(responseHeaders))
release();
}
use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.
the class HttpReceiverOverHTTP2 method onHeaders.
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
HttpExchange exchange = getHttpExchange();
if (exchange == null)
return;
HttpResponse response = exchange.getResponse();
MetaData.Response metaData = (MetaData.Response) frame.getMetaData();
response.version(metaData.getHttpVersion()).status(metaData.getStatus()).reason(metaData.getReason());
if (responseBegin(exchange)) {
HttpFields headers = metaData.getFields();
for (HttpField header : headers) {
if (!responseHeader(exchange, header))
return;
}
if (responseHeaders(exchange)) {
int status = metaData.getStatus();
boolean informational = HttpStatus.isInformational(status) && status != HttpStatus.SWITCHING_PROTOCOLS_101;
if (frame.isEndStream() || informational)
responseSuccess(exchange);
}
}
}
use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.
the class TrailersTest method testTrailersSentByServer.
@Test
public void testTrailersSentByServer() throws Exception {
start(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
HttpFields responseFields = new HttpFields();
responseFields.put("X-Response", "true");
MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, responseFields);
HeadersFrame responseFrame = new HeadersFrame(stream.getId(), response, null, false);
stream.headers(responseFrame, new Callback() {
@Override
public void succeeded() {
HttpFields trailerFields = new HttpFields();
trailerFields.put("X-Trailer", "true");
MetaData trailer = new MetaData(HttpVersion.HTTP_2, trailerFields);
HeadersFrame trailerFrame = new HeadersFrame(stream.getId(), trailer, null, true);
stream.headers(trailerFrame, NOOP);
}
});
return null;
}
});
Session session = newClient(new Session.Listener.Adapter());
MetaData.Request request = newRequest("GET", new HttpFields());
HeadersFrame requestFrame = new HeadersFrame(request, null, true);
CountDownLatch latch = new CountDownLatch(1);
session.newStream(requestFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {
private boolean responded;
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
if (!responded) {
MetaData.Response response = (MetaData.Response) frame.getMetaData();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertTrue(response.getFields().containsKey("X-Response"));
Assert.assertFalse(frame.isEndStream());
responded = true;
} else {
MetaData trailer = frame.getMetaData();
Assert.assertTrue(trailer.getFields().containsKey("X-Trailer"));
Assert.assertTrue(frame.isEndStream());
latch.countDown();
}
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Aggregations