io.netty.handler.codec.http.multipart.HttpData Java Examples

The following examples show how to use io.netty.handler.codec.http.multipart.HttpData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpPostMultipartRequestDecoder.java    From dorado with Apache License 2.0 5 votes vote down vote up
/**
 * Load the field value or file data from a Multipart request
 *
 * @return {@code true} if the last chunk is loaded (boundary delimiter found),
 *         {@code false} if need more chunks
 * @throws ErrorDataDecoderException
 */
private static boolean loadDataMultipartStandard(ByteBuf undecodedChunk, String delimiter, HttpData httpData) {
	final int startReaderIndex = undecodedChunk.readerIndex();
	final int delimeterLength = delimiter.length();
	int index = 0;
	int lastPosition = startReaderIndex;
	byte prevByte = HttpConstants.LF;
	boolean delimiterFound = false;
	while (undecodedChunk.isReadable()) {
		final byte nextByte = undecodedChunk.readByte();
		// Check the delimiter
		if (prevByte == HttpConstants.LF && nextByte == delimiter.codePointAt(index)) {
			index++;
			if (delimeterLength == index) {
				delimiterFound = true;
				break;
			}
			continue;
		}
		lastPosition = undecodedChunk.readerIndex();
		if (nextByte == HttpConstants.LF) {
			index = 0;
			lastPosition -= (prevByte == HttpConstants.CR) ? 2 : 1;
		}
		prevByte = nextByte;
	}
	if (prevByte == HttpConstants.CR) {
		lastPosition--;
	}
	ByteBuf content = undecodedChunk.copy(startReaderIndex, lastPosition - startReaderIndex);
	try {
		httpData.addContent(content, delimiterFound);
	} catch (IOException e) {
		throw new ErrorDataDecoderException(e);
	}
	undecodedChunk.readerIndex(lastPosition);
	return delimiterFound;
}
 
Example #2
Source File: VerifyMultipartRequestsWorkComponentTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
@Override
public @NotNull CompletableFuture<ResponseInfo<String>> execute(
    @NotNull RequestInfo<String> request,
    @NotNull Executor longRunningTaskExecutor,
    @NotNull ChannelHandlerContext ctx
) {
    List<String> hashesFound = new ArrayList<>();

    for (InterfaceHttpData multipartData : request.getMultipartParts()) {
        String name = multipartData.getName();
        byte[] payloadBytes;
        try {
            payloadBytes = ((HttpData)multipartData).get();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        String filename = null;
        switch (multipartData.getHttpDataType()) {
            case Attribute:
                // Do nothing - filename stays null
                break;
            case FileUpload:
                filename = ((FileUpload)multipartData).getFilename();
                break;
            default:
                throw new RuntimeException("Unsupported multipart type: " + multipartData.getHttpDataType().name());
        }

        hashesFound.add(getHashForMultipartPayload(name, filename, payloadBytes));
    }

    return CompletableFuture.completedFuture(ResponseInfo.newBuilder(StringUtils.join(hashesFound, ",")).build());
}
 
Example #3
Source File: HttpPostMultipartRequestDecoder.java    From dorado with Apache License 2.0 4 votes vote down vote up
/**
 * Load the field value from a Multipart request
 *
 * @return {@code true} if the last chunk is loaded (boundary delimiter found),
 *         {@code false} if need more chunks
 * @throws ErrorDataDecoderException
 */
private static boolean loadDataMultipart(ByteBuf undecodedChunk, String delimiter, HttpData httpData) {
	if (!undecodedChunk.hasArray()) {
		return loadDataMultipartStandard(undecodedChunk, delimiter, httpData);
	}
	final SeekAheadOptimize sao = new SeekAheadOptimize(undecodedChunk);
	final int startReaderIndex = undecodedChunk.readerIndex();
	final int delimeterLength = delimiter.length();
	int index = 0;
	int lastRealPos = sao.pos;
	byte prevByte = HttpConstants.LF;
	boolean delimiterFound = false;
	while (sao.pos < sao.limit) {
		final byte nextByte = sao.bytes[sao.pos++];
		// Check the delimiter
		if (prevByte == HttpConstants.LF && nextByte == delimiter.codePointAt(index)) {
			index++;
			if (delimeterLength == index) {
				delimiterFound = true;
				break;
			}
			continue;
		}
		lastRealPos = sao.pos;
		if (nextByte == HttpConstants.LF) {
			index = 0;
			lastRealPos -= (prevByte == HttpConstants.CR) ? 2 : 1;
		}
		prevByte = nextByte;
	}
	if (prevByte == HttpConstants.CR) {
		lastRealPos--;
	}
	final int lastPosition = sao.getReadPosition(lastRealPos);
	final ByteBuf content = undecodedChunk.copy(startReaderIndex, lastPosition - startReaderIndex);
	try {
		httpData.addContent(content, delimiterFound);
	} catch (IOException e) {
		throw new ErrorDataDecoderException(e);
	}
	undecodedChunk.readerIndex(lastPosition);
	return delimiterFound;
}