perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.63k stars 1.56k forks source link

Bad request on multiple Static file locations #1005

Closed RyanSusana closed 5 years ago

RyanSusana commented 6 years ago

Hello friends,

I have been using sparkjava for a while now and am developing a framework (Elepy using your framework.

The problem that I am running into is that when I have multiple jars with static resources on different Services, I get a Bad Request 400 on all but the first staticfile location. The different Services are hosted on port 80 and 1337 and get instantiated with ignite()

This means that per JVM I can only have one staticfile location? It does recognize that the resource exists, otherwise it would give me a 404 instead.

I looked through the source code and it has something to do with DirectoryTraversalDetection.

tipsy commented 6 years ago

Hi @RyanSusana. I'm guessing this is because StaticFilesFolder has static variables:

private static volatile String local;
private static volatile String external;
RyanSusana commented 6 years ago

Are there any plans on changing this or must I resort to multiple JVM's?

RyanSusana commented 6 years ago

Maybe we can make it per Service instance instead? Or is this not possible?

tipsy commented 6 years ago

It's possible. Spark used to be completely static, so I'm guessing this is just a "leftover". It should be possible to fix, but it would break backwards compatibility for anyone actually relying on this behavior 🤔

RyanSusana commented 6 years ago

Why would it break backwards compatability?

tipsy commented 6 years ago

Any change in current behavior is really a change in backwards compatibility. I don't remember what the current behavior is, but based on reading the code it looks like you can do:

val serviceA = ignite().apply { staticFiles.location("/public") }
val serviceB = ignite()

Then do a GET for static files to serviceB . If this is the case, people will have to add staticFiles.location("/public") to their second service too.

I'll fire up IntelliJ and check.

tipsy commented 6 years ago

It didn't work without calling staticFiles.location(), so that's good. Also, I found some strange behavior:

Works:

Service serviceA = Service.ignite();
serviceA.port(4001);
serviceA.staticFiles.location("/public/pages");
serviceA.init();

serviceA.awaitInitialization();

Service serviceB = Service.ignite();
serviceB.staticFiles.location("/public");
serviceB.port(4002);
serviceB.init();

Bad request:

Service serviceA = Service.ignite();
serviceA.port(4001);
serviceA.staticFiles.location("/public");
serviceA.init();

serviceA.awaitInitialization();

Service serviceB = Service.ignite();
serviceB.staticFiles.location("/public/pages");
serviceB.port(4002);
serviceB.init();

This can be fixed as a bug then, I can't imagine anyone really relying on this behavior.

RyanSusana commented 6 years ago

Thank you for the help!

perwendel commented 5 years ago

Fixed by #1036