Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ public interface CallHeaders {
/** Get the value of a metadata key. If multiple values are present, then get the last one. */
byte[] getByte(String key);

/** Get all values present for the given metadata key. */
/**
* Get all values present for the given metadata key.
*
* @param key the metadata key
* @return an iterable of all values for the key. Returns an empty iterable if no value to return.
*/
Iterable<String> getAll(String key);

/** Get all values present for the given metadata key. */
/**
* Get all values present for the given metadata key.
*
* @param key the metadata key
* @return an iterable of all values for the key. Returns an empty iterable if no value to return.
*/
Iterable<byte[]> getAllByte(String key);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,18 @@ public ServerSessionMiddleware onCallStarted(
String sessionId = null;

final Iterable<String> it = incomingHeaders.getAll("cookie");
if (it != null) {
findIdCookie:
for (final String headerValue : it) {
for (final String cookie : headerValue.split(" ;")) {
final String[] cookiePair = cookie.split("=");
if (cookiePair.length != 2) {
// Soft failure: Ignore invalid cookie list field
break;
}

if (sessionCookieName.equals(cookiePair[0]) && cookiePair[1].length() > 0) {
sessionId = cookiePair[1];
break findIdCookie;
}
findIdCookie:
for (final String headerValue : it) {
for (final String cookie : headerValue.split(" ;")) {
final String[] cookiePair = cookie.split("=");
if (cookiePair.length != 2) {
// Soft failure: Ignore invalid cookie list field
break;
}

if (sessionCookieName.equals(cookiePair[0]) && cookiePair[1].length() > 0) {
sessionId = cookiePair[1];
break findIdCookie;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) {

@Override
public void onHeadersReceived(CallHeaders incomingHeaders) {
final Iterable<String> setCookieHeaders = incomingHeaders.getAll(SET_COOKIE_HEADER);
if (setCookieHeaders != null) {
factory.updateCookies(setCookieHeaders);
}
factory.updateCookies(incomingHeaders.getAll(SET_COOKIE_HEADER));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.grpc.Metadata;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -53,13 +54,15 @@ public byte[] getByte(String key) {

@Override
public Iterable<String> getAll(String key) {
return this.metadata.getAll(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
final Iterable<String> all = this.metadata.getAll(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
return all != null ? all : Collections.emptyList();
}

@Override
public Iterable<byte[]> getAllByte(String key) {
if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
return this.metadata.getAll(Metadata.Key.of(key, Metadata.BINARY_BYTE_MARSHALLER));
final Iterable<byte[]> all = this.metadata.getAll(Metadata.Key.of(key, Metadata.BINARY_BYTE_MARSHALLER));
return all != null ? all : Collections.emptyList();
}
return StreamSupport.stream(getAll(key).spliterator(), false)
.map(String::getBytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down Expand Up @@ -110,6 +111,16 @@ public void mixedProperties() {
testHeaders(headers);
}

@Test
public void getAllReturnsEmptyIterableForMissingKey() {
FlightCallHeaders headers = new FlightCallHeaders();

assertNotNull(headers.getAll("missing"));
assertFalse(headers.getAll("missing").iterator().hasNext());
assertNotNull(headers.getAllByte("missing-bin"));
assertFalse(headers.getAllByte("missing-bin").iterator().hasNext());
}

private void testHeaders(CallHeaders headers) {
try (BufferAllocator a = new RootAllocator(Long.MAX_VALUE);
HeaderProducer producer = new HeaderProducer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.apache.arrow.flight.Location.forGrpcInsecure;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -119,6 +120,16 @@ public void testFlightMetadata() throws Exception {
}
}

@Test
public void getAllReturnsEmptyIterableForMissingKey() {
ErrorFlightMetadata metadata = new ErrorFlightMetadata();

assertNotNull(metadata.getAll("missing"));
assertFalse(metadata.getAll("missing").iterator().hasNext());
assertNotNull(metadata.getAllByte("missing-bin"));
assertFalse(metadata.getAllByte("missing-bin").iterator().hasNext());
}

private static class StatusRuntimeExceptionProducer extends NoOpFlightProducer {
private final PerfOuterClass.Perf perf;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.arrow.flight.grpc;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import io.grpc.Metadata;
import org.junit.jupiter.api.Test;

public class TestMetadataAdapter {

@Test
public void getAllReturnsEmptyIterableForMissingKey() {
MetadataAdapter headers = new MetadataAdapter(new Metadata());

assertNotNull(headers.getAll("missing"));
assertFalse(headers.getAll("missing").iterator().hasNext());
assertNotNull(headers.getAllByte("missing-bin"));
assertFalse(headers.getAllByte("missing-bin").iterator().hasNext());
}
}
Loading