Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7050413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:08:25+00:00 2026-05-28T03:08:25+00:00

I’m working with Apache mod_dav compiled on my own server. My client is built-from-scratch

  • 0

I’m working with Apache mod_dav compiled on my own server. My client is built-from-scratch custom HTTP parsing code in Java. I’ve been using this server and code base for years, synchronizing gigabytes of data on the server.

Today I ran into a problem that has never cropped up before: the dreaded SAX “content is not allowed in trailing section” error. When doing WebDAV PROPFINDs throughout my whole server resource tree, I always get this error at the same location.

I’ve tested and retested my HTTP parsing code, but it’s pretty simple: Apache is sending back chunked content, and the chunks indicate the number of bytes to consume.

The place it fails is the XML response that happens to use 110 chunks—significantly larger than most other responses (this is a very large directory). However, in my logs I can see that there is no “trailing content”—each XML response (that producing an error and the ones that do not) ends with a simple linefeed character.

But even more distressing: I have an input stream that parses the HTTP chunked content and sends back a simple string of bytes. When I pass this input stream directly to the XML parser, I get the following error. However: if I take the same input stream and bleed all the bytes from it, put them in a ByteArrayInputStream, and then give the ByteArrayInputStream (which should contain the exact same data!) to the parser, no error occurs! What is it about parsing directly from the incoming data that causes the error?

My XML parser is pretty straightforward:

final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setValidating(false);

Anyone seen this before? (I searched for “mod_dav XML bug”—and just got the unrelated bug I filed five years ago.)

Here is the relevant part of the stack trace:

Cause:org.xml.sax.SAXParseException: Content is not allowed in trailing section.
    com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    com.globalmentor.net.http.HTTPClientTCPConnection.readResponseBodyXML(HTTPClientTCPConnection.java:666)
    com.globalmentor.net.http.webdav.WebDAVResource.propFind(WebDAVResource.java:453)

Update: I’ve done this test over and over. Finally I added code to walk the stack trace and print out the SAX parse information I get:

Public Id: null System Id: null Line# 21937 Column# 1

I copy the XML from the log file, sure enough, line 21937 is the end of the file—but there is nothing there!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:08:25+00:00Added an answer on May 28, 2026 at 3:08 am

    Oh, man—this is one of the most aggravating and subtle bugs I’ve ever worked on! I was so tempted just to read the response XML into bytes and return a ByteArrayInputStream and return that, although I didn’t know why that fixed the problem. It turns out that it was my fault, kind of, technically, but still…

    So it turns out that if you read the API contract of InputStream.read(byte b[], int off, int len), the method is never supposed to return zero bytes! If it reaches the end of the data, it should return -1, or block until data is available. (What it should do if the caller requests a len of zero is unclear, as that doesn’t seem to be prohibited by the API. A more modern API would specify that an IllegalArgumentException should be thrown if len<1, but I digress.)

    My HTTPChunkedInputStream automatically parses out the chunks for an HTTP chunked response. The way it was written, if the caller of HTTPChunkedInputStream.read(byte b[], int off, int len) requested exactly the number of bytes available in the last chunk, then the input stream would not proactively try to load further chunks and recognize the end of the stream. That in itself is not a problem, but the next time the caller wants more bytes, the way the algorithm was written, my input stream would try to read another chunk, recognize that there were no more chunks left, and then indicate that zero bytes were read! (Mind you, this only occurred if the called first requested exactly the number of bytes in the last chunk, and then later asked for more bytes.) Any time after this it would return -1, as the end of the data had been hit.

    So in this particular case, for whatever reason, the XML parser asked for exactly the remaining bytes in the XML response from WebDAV PROPFIND. Then the parser wanted to check to see if there were further characters. The actual reading happens in UTF8Reader; when my input stream returned that zero bytes were read, this was passed up XMLEntityScanner. Neither of these classes know how to handle “no bytes were read”—it just assumes something was read. Lastly, XMLDocumentScannerImpl checks to see what that “something” was on line 1453:

    int ch = fEntityScanner.peekChar();
    if (ch == -1) {
        setScannerState(SCANNER_STATE_TERMINATED);
        return XMLEvent.END_DOCUMENT ;
    } else{
        reportFatalError("ContentIllegalInTrailingMisc",
                null);
        fEntityScanner.scanChar();
        setScannerState(SCANNER_STATE_TRAILING_MISC);
        return XMLEvent.CHARACTERS;
    }
    

    Because the end of the stream wasn’t indicated (it doesn’t know how to handle “nothing”), it assumed there was “something” there, and this something must be illegal trailing content.

    Whew! I’ve fixed my HTTPChunkedInputStream class to never return zero bytes from read(). I am exhausted—this is one of the things that never even turn up except infrequently under certain conditions. And when I read the bytes and returned them in a ByteArrayInputStream, this didn’t show up because my code to suck the bytes out of the HTTPChunkedInputStream never requested exactly the number of bytes in the last chunk—and if it did, it still knew how to suck out those zero bytes and put them in the buffer, along with the others.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am currently running into a problem where an element is coming back from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an XML file, the creators of it stuck in a bunch social

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.