use of io.netty.handler.codec.http.DefaultHttpContent in project netty by netty.
the class HttpPostRequestEncoder method nextChunk.
/**
* Returns the next available HttpChunk. The caller is responsible to test if this chunk is the last one (isLast()),
* in order to stop calling this getMethod.
*
* @return the next available HttpChunk
* @throws ErrorDataEncoderException
* if the encoding is in error
*/
private HttpContent nextChunk() throws ErrorDataEncoderException {
if (isLastChunk) {
isLastChunkSent = true;
return LastHttpContent.EMPTY_LAST_CONTENT;
}
ByteBuf buffer;
int size = HttpPostBodyUtil.chunkSize;
// first test if previous buffer is not empty
if (currentBuffer != null) {
size -= currentBuffer.readableBytes();
}
if (size <= 0) {
// NextChunk from buffer
buffer = fillByteBuf();
return new DefaultHttpContent(buffer);
}
// size > 0
if (currentData != null) {
// continue to read data
if (isMultipart) {
HttpContent chunk = encodeNextChunkMultipart(size);
if (chunk != null) {
return chunk;
}
} else {
HttpContent chunk = encodeNextChunkUrlEncoded(size);
if (chunk != null) {
// NextChunk Url from currentData
return chunk;
}
}
size = HttpPostBodyUtil.chunkSize - currentBuffer.readableBytes();
}
if (!iterator.hasNext()) {
isLastChunk = true;
// NextChunk as last non empty from buffer
buffer = currentBuffer;
currentBuffer = null;
return new DefaultHttpContent(buffer);
}
while (size > 0 && iterator.hasNext()) {
currentData = iterator.next();
HttpContent chunk;
if (isMultipart) {
chunk = encodeNextChunkMultipart(size);
} else {
chunk = encodeNextChunkUrlEncoded(size);
}
if (chunk == null) {
// not enough
size = HttpPostBodyUtil.chunkSize - currentBuffer.readableBytes();
continue;
}
// NextChunk from data
return chunk;
}
// end since no more data
isLastChunk = true;
if (currentBuffer == null) {
isLastChunkSent = true;
// LastChunk with no more data
return LastHttpContent.EMPTY_LAST_CONTENT;
}
// Previous LastChunk with no more data
buffer = currentBuffer;
currentBuffer = null;
return new DefaultHttpContent(buffer);
}
use of io.netty.handler.codec.http.DefaultHttpContent in project netty by netty.
the class HttpPostRequestDecoderTest method testBinaryStreamUpload.
private static void testBinaryStreamUpload(boolean withSpace) throws Exception {
final String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO";
final String contentTypeValue;
if (withSpace) {
contentTypeValue = "multipart/form-data; boundary=" + boundary;
} else {
contentTypeValue = "multipart/form-data;boundary=" + boundary;
}
final DefaultHttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost");
req.setDecoderResult(DecoderResult.SUCCESS);
req.headers().add(HttpHeaderNames.CONTENT_TYPE, contentTypeValue);
req.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
// Force to use memory-based data.
final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false);
for (String data : Arrays.asList("", "\r", "\r\r", "\r\r\r")) {
final String body = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"tmp-0.txt\"\r\n" + "Content-Type: image/gif\r\n" + "\r\n" + data + "\r\n" + "--" + boundary + "--\r\n";
// Create decoder instance to test.
final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);
decoder.offer(new DefaultHttpContent(Unpooled.copiedBuffer(body, CharsetUtil.UTF_8)));
decoder.offer(new DefaultHttpContent(Unpooled.EMPTY_BUFFER));
// Validate it's enough chunks to decode upload.
assertTrue(decoder.hasNext());
// Decode binary upload.
MemoryFileUpload upload = (MemoryFileUpload) decoder.next();
// Validate data has been parsed correctly as it was passed into request.
assertEquals("Invalid decoded data [data=" + data.replaceAll("\r", "\\\\r") + ", upload=" + upload + ']', data, upload.getString(CharsetUtil.UTF_8));
upload.release();
decoder.destroy();
}
}
use of io.netty.handler.codec.http.DefaultHttpContent in project netty by netty.
the class HttpPostRequestDecoderTest method testChunkCorrect.
// See https://github.com/netty/netty/issues/2305
@Test
public void testChunkCorrect() throws Exception {
String payload = "town=794649819&town=784444184&town=794649672&town=794657800&town=" + "794655734&town=794649377&town=794652136&town=789936338&town=789948986&town=" + "789949643&town=786358677&town=794655880&town=786398977&town=789901165&town=" + "789913325&town=789903418&town=789903579&town=794645251&town=794694126&town=" + "794694831&town=794655274&town=789913656&town=794653956&town=794665634&town=" + "789936598&town=789904658&town=789899210&town=799696252&town=794657521&town=" + "789904837&town=789961286&town=789958704&town=789948839&town=789933899&town=" + "793060398&town=794659180&town=794659365&town=799724096&town=794696332&town=" + "789953438&town=786398499&town=794693372&town=789935439&town=794658041&town=" + "789917595&town=794655427&town=791930372&town=794652891&town=794656365&town=" + "789960339&town=794645586&town=794657688&town=794697211&town=789937427&town=" + "789902813&town=789941130&town=794696907&town=789904328&town=789955151&town=" + "789911570&town=794655074&town=789939531&town=789935242&town=789903835&town=" + "789953800&town=794649962&town=789939841&town=789934819&town=789959672&town=" + "794659043&town=794657035&town=794658938&town=794651746&town=794653732&town=" + "794653881&town=786397909&town=794695736&town=799724044&town=794695926&town=" + "789912270&town=794649030&town=794657946&town=794655370&town=794659660&town=" + "794694617&town=799149862&town=789953234&town=789900476&town=794654995&town=" + "794671126&town=789908868&town=794652942&town=789955605&town=789901934&town=" + "789950015&town=789937922&town=789962576&town=786360170&town=789954264&town=" + "789911738&town=789955416&town=799724187&town=789911879&town=794657462&town=" + "789912561&town=789913167&town=794655195&town=789938266&town=789952099&town=" + "794657160&town=789949414&town=794691293&town=794698153&town=789935636&town=" + "789956374&town=789934635&town=789935475&town=789935085&town=794651425&town=" + "794654936&town=794655680&town=789908669&town=794652031&town=789951298&town=" + "789938382&town=794651503&town=794653330&town=817675037&town=789951623&town=" + "789958999&town=789961555&town=794694050&town=794650241&town=794656286&town=" + "794692081&town=794660090&town=794665227&town=794665136&town=794669931";
DefaultHttpRequest defaultHttpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(defaultHttpRequest);
int firstChunk = 10;
int middleChunk = 1024;
HttpContent part1 = new DefaultHttpContent(Unpooled.wrappedBuffer(payload.substring(0, firstChunk).getBytes()));
HttpContent part2 = new DefaultHttpContent(Unpooled.wrappedBuffer(payload.substring(firstChunk, firstChunk + middleChunk).getBytes()));
HttpContent part3 = new DefaultHttpContent(Unpooled.wrappedBuffer(payload.substring(firstChunk + middleChunk, firstChunk + middleChunk * 2).getBytes()));
HttpContent part4 = new DefaultHttpContent(Unpooled.wrappedBuffer(payload.substring(firstChunk + middleChunk * 2).getBytes()));
decoder.offer(part1);
decoder.offer(part2);
decoder.offer(part3);
decoder.offer(part4);
}
use of io.netty.handler.codec.http.DefaultHttpContent in project netty by netty.
the class Http2ServerDowngraderTest method testUpgradeChunk.
@Test
public void testUpgradeChunk() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new Http2ServerDowngrader());
ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
HttpContent content = new DefaultHttpContent(hello);
assertTrue(ch.writeOutbound(content));
Http2DataFrame dataFrame = ch.readOutbound();
try {
assertThat(dataFrame.content().toString(CharsetUtil.UTF_8), is("hello world"));
assertFalse(dataFrame.isEndStream());
} finally {
dataFrame.release();
}
assertThat(ch.readOutbound(), is(nullValue()));
assertFalse(ch.finish());
}
use of io.netty.handler.codec.http.DefaultHttpContent in project netty-socketio by mrniko.
the class EncoderHandler method sendMessage.
private void sendMessage(HttpMessage msg, Channel channel, ByteBuf out, HttpResponse res, ChannelPromise promise) {
channel.write(res);
if (log.isTraceEnabled()) {
if (msg.getSessionId() != null) {
log.trace("Out message: {} - sessionId: {}", out.toString(CharsetUtil.UTF_8), msg.getSessionId());
} else {
log.trace("Out message: {}", out.toString(CharsetUtil.UTF_8));
}
}
if (out.isReadable()) {
channel.write(new DefaultHttpContent(out));
} else {
out.release();
}
channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT, promise).addListener(ChannelFutureListener.CLOSE);
}
Aggregations