Skip to content

Commit

Permalink
Update the original commit to not throw exception on non set ops
Browse files Browse the repository at this point in the history
- For get, contains, and remove do not throw IllegalArgumentException
when the header name is invalid.  Just ignore the header and do not
created a HttpHeaderKeys object.
- Update HttpTrailerGeneratorImpl to do the correct equals.  The code
never would work as it was written.
  • Loading branch information
jhanders34 committed Feb 28, 2023
1 parent e150dcc commit b86e84a
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public void appendHeader(String header, byte[] value) {
if (this.bHeaderValidation) {
checkHeaderValue(value, 0, value.length);
}
HeaderElement elem = getElement(findKey(header));
HeaderElement elem = getElement(findKey(header, false));
elem.setByteArrayValue(value);
addHeader(elem, FILTER_YES);
}
Expand All @@ -407,7 +407,7 @@ public void appendHeader(String header, byte[] value, int offset, int length) {
if (this.bHeaderValidation) {
checkHeaderValue(value, offset, length);
}
HeaderElement elem = getElement(findKey(header));
HeaderElement elem = getElement(findKey(header, false));
elem.setByteArrayValue(value, offset, length);
addHeader(elem, FILTER_YES);
}
Expand All @@ -426,7 +426,7 @@ public void appendHeader(byte[] header, byte[] value) {
if (this.bHeaderValidation) {
checkHeaderValue(value, 0, value.length);
}
HeaderElement elem = getElement(findKey(header));
HeaderElement elem = getElement(findKey(header, false));
elem.setByteArrayValue(value);
addHeader(elem, FILTER_YES);
}
Expand All @@ -445,7 +445,7 @@ public void appendHeader(byte[] header, byte[] value, int offset, int length) {
if (this.bHeaderValidation) {
checkHeaderValue(value, offset, length);
}
HeaderElement elem = getElement(findKey(header));
HeaderElement elem = getElement(findKey(header, false));
elem.setByteArrayValue(value, offset, length);
addHeader(elem, FILTER_YES);
}
Expand Down Expand Up @@ -505,7 +505,7 @@ public void appendHeader(String header, String value) {
else
checkHeaderValue(value);
}
HeaderElement elem = getElement(findKey(header));
HeaderElement elem = getElement(findKey(header, false));
elem.setStringValue(value);
addHeader(elem, FILTER_YES);
}
Expand All @@ -527,7 +527,7 @@ public void appendHeader(byte[] header, String value) {
else
checkHeaderValue(value);
}
HeaderElement elem = getElement(findKey(header));
HeaderElement elem = getElement(findKey(header, false));
elem.setStringValue(value);
addHeader(elem, FILTER_YES);
}
Expand Down Expand Up @@ -930,7 +930,8 @@ public HeaderField getHeader(String header) {
if (null == header) {
throw new IllegalArgumentException("Null input provided");
}
HeaderElement elem = findHeader(findKey(header));
HeaderKeys key = findKey(header, true);
HeaderElement elem = key == null ? null : findHeader(key);
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "getHeader(s): " + header + " " + elem);
}
Expand All @@ -948,10 +949,10 @@ public HeaderField getHeader(byte[] header) {
if (null == header) {
throw new IllegalArgumentException("Null input provided");
}
HeaderKeys key = findKey(header);
HeaderElement elem = findHeader(key);
HeaderKeys key = findKey(header, true);
HeaderElement elem = key == null ? null : findHeader(key);
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "getHeader(b): " + key.getName() + " " + elem);
Tr.debug(tc, "getHeader(b): " + new String(header) + " " + elem);
}
if (null == elem) {
return NULL_HEADER;
Expand All @@ -968,16 +969,16 @@ public List<HeaderField> getHeaders(byte[] header) {
throw new IllegalArgumentException("Null input provided");
}
List<HeaderField> list = new ArrayList<HeaderField>();
HeaderKeys key = findKey(header);
HeaderElement elem = findHeader(key);
HeaderKeys key = findKey(header, true);
HeaderElement elem = key == null ? null : findHeader(key);
while (null != elem) {
if (!elem.wasRemoved()) {
list.add(elem);
}
elem = elem.nextInstance;
}
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "getHeaders(b): " + key.getName() + " " + list.size());
Tr.debug(tc, "getHeaders(b): " + new String(header) + " " + list.size());
}
return list;
}
Expand Down Expand Up @@ -1017,7 +1018,8 @@ public List<HeaderField> getHeaders(String header) {
throw new IllegalArgumentException("Null input provided");
}
List<HeaderField> list;
HeaderElement elem = findHeader(findKey(header));
HeaderKeys key = findKey(header, true);
HeaderElement elem = key == null ? null : findHeader(key);
if (null == elem) {
list = Collections.emptyList();
} else if (elem.nextInstance == null) {
Expand Down Expand Up @@ -1049,7 +1051,8 @@ public int getNumberOfHeaderInstances(String header) {
if (null == header) {
throw new IllegalArgumentException("Null input provided");
}
return countInstances(findHeader(findKey(header)));
HeaderKeys key = findKey(header, true);
return key == null ? 0 : countInstances(findHeader(key));
}

/**
Expand All @@ -1060,7 +1063,8 @@ public int getNumberOfHeaderInstances(byte[] header) {
if (null == header) {
throw new IllegalArgumentException("Null input provided");
}
return countInstances(findHeader(findKey(header)));
HeaderKeys key = findKey(header, true);
return key == null ? 0 : countInstances(findHeader(key));
}

/**
Expand All @@ -1087,7 +1091,8 @@ public boolean containsHeader(String header) {
if (null == header) {
throw new IllegalArgumentException("Null input provided");
}
return (null != findHeader(findKey(header)));
HeaderKeys key = findKey(header, true);
return (key != null && null != findHeader(key));
}

/**
Expand All @@ -1098,7 +1103,8 @@ public boolean containsHeader(byte[] header) {
if (null == header) {
throw new IllegalArgumentException("Null input provided");
}
return (null != findHeader(findKey(header)));
HeaderKeys key = findKey(header, true);
return key != null && (null != findHeader(key));
}

/**
Expand Down Expand Up @@ -1596,7 +1602,7 @@ public boolean parseBinaryHeaders(WsByteBuffer buff, HeaderKeys keys) throws Mal

case GenericConstants.PARSING_HDR_NAME_VALUE:
// parse the unknown header name
this.currentElem = getElement(findKey(this.parsedToken));
this.currentElem = getElement(findKey(this.parsedToken, false));
this.binaryParsingState = GenericConstants.PARSING_HDR_VALUE_LEN;
resetCacheToken(4);
break;
Expand Down Expand Up @@ -1796,7 +1802,10 @@ public void removeHeader(String header) {
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "removeHeader(s): " + header);
}
removeHdrInstances(findHeader(findKey(header)), FILTER_YES);
HeaderKeys key = findKey(header, true);
if (key != null) {
removeHdrInstances(findHeader(key), FILTER_YES);
}
}

/**
Expand All @@ -1807,11 +1816,13 @@ public void removeHeader(byte[] header) {
if (null == header) {
throw new IllegalArgumentException("Null input provided");
}
HeaderKeys key = findKey(header);
HeaderKeys key = findKey(header, true);
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "removeHeader(b): " + key.getName());
Tr.debug(tc, "removeHeader(b): " + new String(header));
}
if (key != null) {
removeHdrInstances(findHeader(key), FILTER_YES);
}
removeHdrInstances(findHeader(key), FILTER_YES);
}

/**
Expand Down Expand Up @@ -1839,7 +1850,10 @@ public void removeHeader(String header, int instance) {
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "removeHeader(s,i): " + header + " " + instance);
}
removeHdr(findHeader(findKey(header), instance));
HeaderKeys key = findKey(header, true);
if (key != null) {
removeHdr(findHeader(key, instance));
}
}

/**
Expand All @@ -1850,11 +1864,13 @@ public void removeHeader(byte[] header, int instance) {
if (null == header) {
throw new IllegalArgumentException("Null input provided");
}
HeaderKeys key = findKey(header);
HeaderKeys key = findKey(header, true);
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "removeHeader(b,i): " + key.getName() + " " + instance);
Tr.debug(tc, "removeHeader(b,i): " + new String(header) + " " + instance);
}
if (key != null) {
removeHdr(findHeader(key, instance));
}
removeHdr(findHeader(key, instance));
}

/**
Expand Down Expand Up @@ -1896,7 +1912,7 @@ public void setHeader(String header, byte[] value) {
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "setHeader(s,b): " + header);
}
setHeader(findKey(header), value);
setHeader(findKey(header, false), value);
}

/**
Expand All @@ -1910,7 +1926,7 @@ public void setHeader(String header, byte[] value, int offset, int length) {
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "setHeader(s,b,i,i): " + header);
}
setHeader(findKey(header), value, offset, length);
setHeader(findKey(header, false), value, offset, length);
}

/**
Expand All @@ -1921,7 +1937,7 @@ public void setHeader(byte[] header, byte[] value) {
if (null == header || null == value) {
throw new IllegalArgumentException("Null input provided");
}
HeaderKeys key = findKey(header);
HeaderKeys key = findKey(header, false);
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "setHeader(b,b): " + key.getName());
}
Expand All @@ -1936,7 +1952,7 @@ public void setHeader(byte[] header, byte[] value, int offset, int length) {
if (null == header || null == value) {
throw new IllegalArgumentException("Null input provided");
}
HeaderKeys key = findKey(header);
HeaderKeys key = findKey(header, false);
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "setHeader(b,b,i,i): " + key.getName());
}
Expand Down Expand Up @@ -2179,7 +2195,7 @@ public void setHeader(String header, String value) {
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "setHeader(s,s): " + header);
}
setHeader(findKey(header), value);
setHeader(findKey(header, false), value);
}

/**
Expand All @@ -2190,7 +2206,7 @@ public void setHeader(byte[] header, String value) {
if (null == header || null == value) {
throw new IllegalArgumentException("Null input provided");
}
HeaderKeys key = findKey(header);
HeaderKeys key = findKey(header, false);
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, "setHeader(b,s): " + key.getName());
}
Expand Down Expand Up @@ -2333,7 +2349,7 @@ protected void freeElement(HeaderElement elem) {
* @param name
* @return HeaderKeys
*/
protected abstract HeaderKeys findKey(String name);
protected abstract HeaderKeys findKey(String name, boolean returnNullOnFullValidation);

/**
* Subclasses will provide the match of the input name against a defined key.
Expand All @@ -2342,7 +2358,7 @@ protected void freeElement(HeaderElement elem) {
* @param name
* @return HeaderKeys
*/
protected abstract HeaderKeys findKey(byte[] name);
protected abstract HeaderKeys findKey(byte[] name, boolean returnNullOnFullValidation);

/**
* Subclasses will provide the match of the input name against a defined key.
Expand All @@ -2353,7 +2369,7 @@ protected void freeElement(HeaderElement elem) {
* @param length - length from that offset
* @return HeaderKeys
*/
protected abstract HeaderKeys findKey(byte[] data, int offset, int length);
protected abstract HeaderKeys findKey(byte[] data, int offset, int length, boolean returnNullOnFullValidation);

/**
* Find the specific instance of this header in storage.
Expand Down Expand Up @@ -3968,7 +3984,7 @@ private boolean parseHeaderName(WsByteBuffer buff) throws MalformedMessageExcept
length = data.length;
}
// otherwise we found the entire length of the name
this.currentElem = getElement(findKey(data, start, length));
this.currentElem = getElement(findKey(data, start, length, false));

// Reset all the global variables once HeaderElement has been instantiated
if (HeaderStorage.NOTSET != this.headerChangeLimit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,28 +232,28 @@ public void destroy() {

/*
* @see com.ibm.ws.genericbnf.internal.BNFHeadersImpl#findKey(byte[], int,
* int)
* int, boolean)
*/
@Override
protected HeaderKeys findKey(byte[] data, int offset, int length) {
return HttpHeaderKeys.find(data, offset, length);
protected HeaderKeys findKey(byte[] data, int offset, int length, boolean returnNullOnFullValidation) {
return HttpHeaderKeys.find(data, offset, length, returnNullOnFullValidation);
}

/*
* see com.ibm.ws.genericbnf.impl.BNFHeadersImpl#findKey(byte[])
* see com.ibm.ws.genericbnf.impl.BNFHeadersImpl#findKey(byte[], boolean)
*/
@Override
protected HeaderKeys findKey(byte[] name) {
return HttpHeaderKeys.find(name, 0, name.length);
protected HeaderKeys findKey(byte[] name, boolean returnNullOnFullValidation) {
return HttpHeaderKeys.find(name, 0, name.length, returnNullOnFullValidation);
}

/*
* @see
* com.ibm.ws.genericbnf.internal.BNFHeadersImpl#findKey(java.lang.String)
* com.ibm.ws.genericbnf.internal.BNFHeadersImpl#findKey(java.lang.String, boolean)
*/
@Override
protected HeaderKeys findKey(String name) {
return HttpHeaderKeys.find(name);
protected HeaderKeys findKey(String name, boolean returnNullOnFullValidation) {
return HttpHeaderKeys.find(name, returnNullOnFullValidation);
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2017 IBM Corporation and others.
* Copyright (c) 2017, 2023 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand Down Expand Up @@ -64,9 +64,9 @@ public byte[] generateTrailerValue(HeaderKeys hdr, HttpTrailers message) {
@Override
public byte[] generateTrailerValue(String hdr, HttpTrailers message) {

HeaderKeys key = HttpHeaderKeys.find(hdr);
HeaderKeys key = HttpHeaderKeys.find(hdr, false);

if (key != null && hdr.equals(_key)) {
if (key != null && key.equals(_key)) {
if (tc.isDebugEnabled()) {
Tr.debug(tc, "generateTrailerValue(String,HttpTrailers): hdr = " + hdr + ", value = " + _value);
}
Expand Down
Loading

0 comments on commit b86e84a

Please sign in to comment.