From 38db0b0ff31600c034cb057c94442e08ba58ab5f Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Thu, 28 Apr 2011 17:57:30 +0000
Subject: [PATCH]     * UPnP: Strip trailing nulls from incoming XML to prevent
       "content not allowed in trailing section" exceptions       from stupid
 routers

---
 .../cybergarage/xml/parser/JaxpParser.java    | 27 ++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/router/java/src/org/cybergarage/xml/parser/JaxpParser.java b/router/java/src/org/cybergarage/xml/parser/JaxpParser.java
index 8de6b06b89..7b220ef0c4 100644
--- a/router/java/src/org/cybergarage/xml/parser/JaxpParser.java
+++ b/router/java/src/org/cybergarage/xml/parser/JaxpParser.java
@@ -17,6 +17,8 @@
 
 package org.cybergarage.xml.parser;
 
+import java.io.FilterInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 
 import javax.xml.parsers.DocumentBuilder;
@@ -102,7 +104,7 @@ public class JaxpParser extends Parser
 		try {
 			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 			DocumentBuilder builder = factory.newDocumentBuilder();
-			InputSource inSrc = new InputSource(inStream);
+			InputSource inSrc = new InputSource(new NullFilterInputStream(inStream));
 			Document doc = builder.parse(inSrc);
 
 			org.w3c.dom.Element docElem = doc.getDocumentElement();
@@ -124,4 +126,27 @@ public class JaxpParser extends Parser
 		return root;
 	}
 
+	/**
+	 *  I2P -
+	 *  Filter out nulls, hopefully to avoid
+	 *  SAXParserException "Content not allowed in trailing section",
+	 *  which is apparently caused by nulls.
+	 *  Alternative is to remove all stuff between '>' and '<',
+         *  which isn't so hard if we assume no CDATA.
+	 */
+	private static class NullFilterInputStream extends FilterInputStream {
+
+		public NullFilterInputStream(InputStream is) {
+			super(is);
+		}
+
+		@Override
+		public int read() throws IOException {
+			int rv;
+			while ((rv = super.read()) == 0) {
+				// try again
+			}
+			return rv;
+		}
+	}
 }
-- 
GitLab