danielcweeks commented on code in PR #17457: URL: https://github.com/apache/iceberg/pull/17457#discussion_r3918598762
########## core/src/main/java/org/apache/iceberg/io/http/HttpInputFile.java: ########## @@ -0,0 +1,149 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.iceberg.io.http; + +import java.io.IOException; +import java.util.Locale; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.iceberg.exceptions.ForbiddenException; +import org.apache.iceberg.exceptions.NotFoundException; +import org.apache.iceberg.exceptions.RuntimeIOException; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.io.SeekableInputStream; +import org.apache.iceberg.metrics.MetricsContext; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; + +/** + * An {@link InputFile} backed by an HTTP URL, typically a pre-signed object-store URL that encodes + * auth in its query parameters. + * + * <p>A known content length is returned directly; otherwise it is fetched lazily via a {@code GET + * Range: bytes=0-0} request, which (unlike HEAD) works with pre-signed GET URLs. + */ +class HttpInputFile implements InputFile { + private final CloseableHttpClient client; + private final String location; + private final String url; + private final int chunkSize; + private final MetricsContext metrics; + + private long length; + + HttpInputFile( + CloseableHttpClient client, + String location, + String url, + int chunkSize, + MetricsContext metrics) { + this(client, location, url, HttpHeaderUtil.UNKNOWN_LENGTH, chunkSize, metrics); + } + + HttpInputFile( + CloseableHttpClient client, + String location, + String url, + long length, + int chunkSize, + MetricsContext metrics) { + Preconditions.checkNotNull(client, "Invalid HTTP client: null"); + Preconditions.checkNotNull(location, "Invalid location: null"); + Preconditions.checkNotNull(url, "Invalid url: null"); + Preconditions.checkNotNull(metrics, "Invalid metrics context: null"); + this.client = client; + this.location = location; + this.url = url; + this.chunkSize = chunkSize; + this.length = length; + this.metrics = metrics; + } + + @Override + public long getLength() { + if (length == HttpHeaderUtil.UNKNOWN_LENGTH) { + this.length = fetchContentLength(); + } + + return length; Review Comment: This logic doesn't look quite right. If we see `UNKNOWN_LENGTH` then we try to fetch the content length, but that can result in an `UNKNOWN_LENGTH` which would then produce the incorrect length from this call. If can't establish the length, we should throw. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
