aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2021-09-01 21:14:35 +0100
committerJuan J. Martinez <jjm@usebox.net>2021-09-01 21:14:35 +0100
commitcfbb8d48a22bd8c905b5e247c52a949d73c363be (patch)
tree9dd863be5d129cb1cc47731462963391b665196f
parent05b5bb6b77877cf8165d85560d5dec145a9369e1 (diff)
downloadspacebeans-cfbb8d48a22bd8c905b5e247c52a949d73c363be.tar.gz
spacebeans-cfbb8d48a22bd8c905b5e247c52a949d73c363be.zip
FIX: index file can be a CGI and should be executed
-rw-r--r--server/src/net/usebox/gemini/server/Response.scala2
-rw-r--r--server/src/net/usebox/gemini/server/Server.scala10
-rw-r--r--server/test/src/ServerSpec.scala50
3 files changed, 59 insertions, 3 deletions
diff --git a/server/src/net/usebox/gemini/server/Response.scala b/server/src/net/usebox/gemini/server/Response.scala
index edb6cdc..1c2071f 100644
--- a/server/src/net/usebox/gemini/server/Response.scala
+++ b/server/src/net/usebox/gemini/server/Response.scala
@@ -115,6 +115,8 @@ case class Cgi(
"REMOTE_HOST" -> remoteAddr
).toSeq
+ logger.debug(s"CGI env: $env")
+
val (status: Int, meta: String, body: String) = {
val output = new java.io.ByteArrayOutputStream
Try {
diff --git a/server/src/net/usebox/gemini/server/Server.scala b/server/src/net/usebox/gemini/server/Server.scala
index f402191..a14d829 100644
--- a/server/src/net/usebox/gemini/server/Server.scala
+++ b/server/src/net/usebox/gemini/server/Server.scala
@@ -104,12 +104,16 @@ case class Server(conf: ServiceConf) {
.getDefault()
.getPath(root, path)
.normalize()
- val cgi = vhost.getCgi(resource)
+ val cgi = vhost
+ .getCgi(resource) match {
+ case None => vhost.getCgi(resource.resolve(vhost.indexFile))
+ case cgi => cgi
+ }
logger.debug(s"requesting: '$resource', cgi is '$cgi'")
resource.toFile() match {
- case file
+ case _
if cgi
.map(_.toFile())
.map(f => f.isFile() && f.canExecute())
@@ -120,7 +124,7 @@ case class Server(conf: ServiceConf) {
val queryString =
if (uri.getQuery() == null) "" else uri.getQuery()
val pathInfo =
- if (cgiFile.compareTo(resource) == 0) ""
+ if (cgiFile.compareTo(resource) >= 0) ""
else
"/" + resource
.subpath(
diff --git a/server/test/src/ServerSpec.scala b/server/test/src/ServerSpec.scala
index d3027e3..ea40798 100644
--- a/server/test/src/ServerSpec.scala
+++ b/server/test/src/ServerSpec.scala
@@ -621,6 +621,46 @@ class ServerSpec extends AnyFlatSpec with Matchers {
cgi.body should include("env1=value")
}
+ it should "execute a CGI when it is the index document" in {
+ val cgi = Server(TestData.cgiIndexConf)
+ .handleReq(
+ "gemini://localhost/dir/",
+ "127.0.0.1"
+ )
+ .asInstanceOf[Cgi]
+
+ cgi.status should be(20)
+ cgi.meta should be("text/gemini")
+ cgi.body should include("GATEWAY_INTERFACE=CGI/1.1")
+ }
+
+ it should "execute a CGI when it is the index document (full name)" in {
+ val cgi = Server(TestData.cgiIndexConf)
+ .handleReq(
+ "gemini://localhost/dir/cgi",
+ "127.0.0.1"
+ )
+ .asInstanceOf[Cgi]
+
+ cgi.status should be(20)
+ cgi.meta should be("text/gemini")
+ cgi.body should include("GATEWAY_INTERFACE=CGI/1.1")
+ }
+
+ it should "execute a CGI when it is the index document (full name, path info)" in {
+ val cgi = Server(TestData.cgiIndexConf)
+ .handleReq(
+ "gemini://localhost/dir/cgi/path/info",
+ "127.0.0.1"
+ )
+ .asInstanceOf[Cgi]
+
+ cgi.status should be(20)
+ cgi.meta should be("text/gemini")
+ cgi.body should include("GATEWAY_INTERFACE=CGI/1.1")
+ cgi.body should include("PATH_INFO=/path/info")
+ }
+
object TestData {
val host = "localhost"
@@ -672,6 +712,16 @@ class ServerSpec extends AnyFlatSpec with Matchers {
)
)
+ val cgiIndexConf = cgiConf.copy(virtualHosts =
+ List(
+ cgiConf
+ .virtualHosts(0)
+ .copy(
+ indexFile = "cgi"
+ )
+ )
+ )
+
val confUserDir = conf.copy(virtualHosts =
List(
conf