io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException #io.netty.handler.codec.http.multipart.HttpData源码实例Demo

下面列出了 io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException #io.netty.handler.codec.http.multipart.HttpData 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。


/**
 * 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;
}
 

@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());
}
 

/**
 * 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;
}