streamfler doesn't process all the stream

27 views
Skip to first unread message

Elías Breijo

unread,
Mar 14, 2013, 5:09:08 AM3/14/13
to streamfly...@googlegroups.com
Hi, i'm doing a reverse proxy using streamflyer for rewrite the server urls. This is my URL rewriting code:
    
    @Override
    protected long rewriteContent(final HttpServletRequest req, final HttpServletResponse servResp,
            final InputStream input,
            final OutputStream output) throws IOException {

        final Charset charset = Charset.forName(servResp.getCharacterEncoding());
        final Server server = getServer(req);
        final String contextPath = req.getContextPath();
        final String proxyPath = server.getRule().backward("/");
        final String ownHostName = getOwnHostName(req);

        final String regex = "((https?://)([^/]+))?(" + server.getPath() + ")";
        final String replacement = "$2" + ownHostName + contextPath + proxyPath;
        final FastRegexModifier modifier = new FastRegexModifier(regex, CASE_INSENSITIVE | CANON_EQ, replacement);

        final Reader reader = new ModifyingReader(new InputStreamReader(input, charset), modifier);
        final Writer writer = new OutputStreamWriter(output, charset);

        final int copied = IOUtils.copy(reader, writer);

        return copied;

    }

But, apparently, streamflyer doesn't process all the stream (it doesn't reached the </html> tag). Any sugestion?

Thanks.

rw...@gmx.de

unread,
Mar 15, 2013, 3:24:08 PM3/15/13
to streamfly...@googlegroups.com
Hi Elías, some suggestions:

1.

>   final int copied = IOUtils.copy(reader, writer);

Call writer.flush() after you copied the contents from the reader to the writer. Java's OutputStreamWriter is buffering.
I am just adding a test to the new streamflyer module streamflyer-support that shows that you must call flush(), otherwise you don't get the entire content.

2.

>   final String regex = "((https?://)([^/]+))?(" + server.getPath() + ")";

The content of the third group is going to match the entire end of stream if there are no more slashes in the file and the server.getPath() is empty.
Change it like this:

>   final String regex = "((https?://)([^/]+/))?(" + server.getPath() + ")";

3.

>   final String regex = "((https?://)([^/]+))?(" + server.getPath() + ")";

If you want to match the content of a String variable literally, prefer Pattern.quote():

>   final String regex = "((https?://)([^/]+))?(" + Pattern.quote(server.getPath()) + ")";

Otherwise server.getPath() might contain data that could be interpreted as regular expression construct, like "?".

Does this help?

Rod
Reply all
Reply to author
Forward
0 new messages