Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2015-2019 SoftIndex LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.datakernel.http;
import io.datakernel.bytebuf.ByteBuf;
import io.datakernel.bytebuf.ByteBufPool;
import io.datakernel.bytebuf.ByteBufQueue;
import io.datakernel.common.ApplicationSettings;
import io.datakernel.common.Recyclable;
import io.datakernel.common.exception.StacklessException;
import io.datakernel.common.ref.Ref;
import io.datakernel.csp.ChannelConsumer;
import io.datakernel.csp.ChannelConsumers;
import io.datakernel.csp.ChannelSupplier;
import io.datakernel.csp.binary.BinaryChannelSupplier;
import io.datakernel.csp.binary.ByteBufsParser;
import io.datakernel.http.MultipartParser.MultipartFrame;
import io.datakernel.promise.Promise;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import static io.datakernel.bytebuf.ByteBufStrings.CR;
import static io.datakernel.bytebuf.ByteBufStrings.LF;
import static io.datakernel.common.MemSize.kilobytes;
import static io.datakernel.common.Utils.nullify;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toMap;
/**
* Util class that allows to parse some binary channel (mainly, the request body stream) into a channel of multipart frames.
*/
public final class MultipartParser implements ByteBufsParser {
private static final int MAX_META_SIZE = ApplicationSettings.getMemSize(MultipartParser.class, "maxMetaBuffer", kilobytes(4)).toInt();
@Nullable
private List readingHeaders = null;
private final byte[] boundary;
private final byte[] lastBoundary;
private MultipartParser(String boundary) {
this.boundary = ("--" + boundary).getBytes(UTF_8);
this.lastBoundary = ("--" + boundary + "--").getBytes(UTF_8);
}
public static MultipartParser create(String boundary) {
return new MultipartParser(boundary);
}
/**
* Converts resulting channel of frames into a binary channel, ignoring any multipart headers.
*/
public ByteBufsParser ignoreHeaders() {
return bufs -> {
MultipartFrame frame = tryParse(bufs);
if (frame == null || frame.isHeaders()) {
return null;
}
return frame.getData();
};
}
private Promise