use of io.netty.handler.codec.http.DefaultHttpRequest in project netty by netty.
the class WebSocketServerProtocolHandlerTest method writeUpgradeRequest.
private static void writeUpgradeRequest(EmbeddedChannel ch, boolean full) {
HttpRequest request = WebSocketRequestBuilder.successful();
if (full) {
ch.writeInbound(request);
} else {
if (request instanceof FullHttpRequest) {
FullHttpRequest fullHttpRequest = (FullHttpRequest) request;
HttpRequest req = new DefaultHttpRequest(fullHttpRequest.protocolVersion(), fullHttpRequest.method(), fullHttpRequest.uri(), fullHttpRequest.headers().copy());
ch.writeInbound(req);
ch.writeInbound(new DefaultHttpContent(fullHttpRequest.content().copy()));
ch.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
fullHttpRequest.release();
} else {
ch.writeInbound(request);
}
}
}
use of io.netty.handler.codec.http.DefaultHttpRequest in project netty by netty.
the class HttpPostRequestDecoderTest method testMultipartFormDataContentType.
@Test
public void testMultipartFormDataContentType() {
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
assertFalse(HttpPostRequestDecoder.isMultipart(request));
String multipartDataValue = HttpHeaderValues.MULTIPART_FORM_DATA + ";" + "boundary=gc0p4Jq0M2Yt08jU534c0p";
request.headers().set(HttpHeaderNames.CONTENT_TYPE, ";" + multipartDataValue);
assertFalse(HttpPostRequestDecoder.isMultipart(request));
request.headers().set(HttpHeaderNames.CONTENT_TYPE, multipartDataValue);
assertTrue(HttpPostRequestDecoder.isMultipart(request));
}
use of io.netty.handler.codec.http.DefaultHttpRequest in project netty by netty.
the class HttpPostRequestDecoderTest method testNoZeroOut.
// See https://github.com/netty/netty/issues/1848
@Test
public void testNoZeroOut() throws Exception {
final String boundary = "E832jQp_Rq2ErFmAduHSR8YlMSm0FCY";
final DefaultHttpDataFactory aMemFactory = new DefaultHttpDataFactory(false);
DefaultHttpRequest aRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost");
aRequest.headers().set(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary);
aRequest.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
HttpPostRequestDecoder aDecoder = new HttpPostRequestDecoder(aMemFactory, aRequest);
final String aData = "some data would be here. the data should be long enough that it " + "will be longer than the original buffer length of 256 bytes in " + "the HttpPostRequestDecoder in order to trigger the issue. Some more " + "data just to be on the safe side.";
final String body = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"root\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + aData + "\r\n" + "--" + boundary + "--\r\n";
byte[] aBytes = body.getBytes();
int split = 125;
ByteBufAllocator aAlloc = new UnpooledByteBufAllocator(true);
ByteBuf aSmallBuf = aAlloc.heapBuffer(split, split);
ByteBuf aLargeBuf = aAlloc.heapBuffer(aBytes.length - split, aBytes.length - split);
aSmallBuf.writeBytes(aBytes, 0, split);
aLargeBuf.writeBytes(aBytes, split, aBytes.length - split);
aDecoder.offer(new DefaultHttpContent(aSmallBuf));
aDecoder.offer(new DefaultHttpContent(aLargeBuf));
aDecoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);
assertTrue(aDecoder.hasNext(), "Should have a piece of data");
InterfaceHttpData aDecodedData = aDecoder.next();
assertEquals(InterfaceHttpData.HttpDataType.Attribute, aDecodedData.getHttpDataType());
Attribute aAttr = (Attribute) aDecodedData;
assertEquals(aData, aAttr.getValue());
aDecodedData.release();
aDecoder.destroy();
aSmallBuf.release();
aLargeBuf.release();
}
use of io.netty.handler.codec.http.DefaultHttpRequest in project netty by netty.
the class HttpPostMultiPartRequestDecoderTest method commonNotBadReleaseBuffersDuringDecoding.
private static void commonNotBadReleaseBuffersDuringDecoding(HttpDataFactory factory, boolean inMemory) throws IOException {
int nbItems = 20;
int bytesPerItem = 1000;
int maxMemory = 500;
String prefix1 = "\n--861fbeab-cd20-470c-9609-d40a0f704466\n" + "Content-Disposition: form-data; name=\"image";
String prefix2 = "\"; filename=\"guangzhou.jpeg\"\n" + "Content-Type: image/jpeg\n" + "Content-Length: " + bytesPerItem + "\n" + "\n";
String suffix = "\n--861fbeab-cd20-470c-9609-d40a0f704466--\n";
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload");
request.headers().set("content-type", "multipart/form-data; boundary=861fbeab-cd20-470c-9609-d40a0f704466");
request.headers().set("content-length", nbItems * (prefix1.length() + prefix2.length() + 2 + bytesPerItem) + suffix.length());
HttpPostMultipartRequestDecoder decoder = new HttpPostMultipartRequestDecoder(factory, request);
decoder.setDiscardThreshold(maxMemory);
for (int rank = 0; rank < nbItems; rank++) {
byte[] bp1 = prefix1.getBytes(CharsetUtil.UTF_8);
byte[] bp2 = prefix2.getBytes(CharsetUtil.UTF_8);
byte[] prefix = new byte[bp1.length + 2 + bp2.length];
for (int i = 0; i < bp1.length; i++) {
prefix[i] = bp1[i];
}
byte[] brank = Integer.toString(10 + rank).getBytes(CharsetUtil.UTF_8);
prefix[bp1.length] = brank[0];
prefix[bp1.length + 1] = brank[1];
for (int i = 0; i < bp2.length; i++) {
prefix[bp1.length + 2 + i] = bp2[i];
}
ByteBuf buf = Unpooled.wrappedBuffer(prefix);
DefaultHttpContent httpContent = new DefaultHttpContent(buf);
decoder.offer(httpContent);
httpContent.release();
byte[] body = new byte[bytesPerItem];
Arrays.fill(body, (byte) rank);
ByteBuf content = Unpooled.wrappedBuffer(body, 0, bytesPerItem);
httpContent = new DefaultHttpContent(content);
decoder.offer(httpContent);
httpContent.release();
}
byte[] lastbody = suffix.getBytes(CharsetUtil.UTF_8);
ByteBuf content2 = Unpooled.wrappedBuffer(lastbody, 0, lastbody.length);
DefaultHttpContent httpContent = new DefaultHttpContent(content2);
decoder.offer(httpContent);
httpContent.release();
decoder.offer(new DefaultLastHttpContent());
for (int rank = 0; rank < nbItems; rank++) {
FileUpload data = (FileUpload) decoder.getBodyHttpData("image" + (10 + rank));
assertEquals(bytesPerItem, data.length());
assertEquals(inMemory, data.isInMemory());
byte[] body = new byte[bytesPerItem];
Arrays.fill(body, (byte) rank);
assertTrue(Arrays.equals(body, data.get()));
}
// Not mandatory since implicitely called during destroy of decoder
for (InterfaceHttpData httpData : decoder.getBodyHttpDatas()) {
httpData.release();
factory.removeHttpDataFromClean(request, httpData);
}
factory.cleanAllHttpData();
decoder.destroy();
}
use of io.netty.handler.codec.http.DefaultHttpRequest in project netty by netty.
the class HttpPostRequestEncoderTest method testEncodeChunkedContent.
@Test
public void testEncodeChunkedContent() throws Exception {
HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(req, false);
int length = 8077 + 8096;
char[] array = new char[length];
Arrays.fill(array, 'a');
String longText = new String(array);
encoder.addBodyAttribute("data", longText);
encoder.addBodyAttribute("moreData", "abcd");
assertNotNull(encoder.finalizeRequest());
while (!encoder.isEndOfInput()) {
encoder.readChunk((ByteBufAllocator) null).release();
}
assertTrue(encoder.isEndOfInput());
encoder.cleanFiles();
}
Aggregations