From e003236a038822c57201f3807958b77562e88ef1 Mon Sep 17 00:00:00 2001
From: Sergey Lyubka <valenok@gmail.com>
Date: Fri, 19 Jul 2013 07:57:47 +0100
Subject: [PATCH] Made SSI #include file=  relative to the current doc, as per
 spec

---
 UserManual.md   | 6 ++++--
 mongoose.c      | 5 +++--
 test/ssi1.shtml | 2 +-
 test/ssi5.shtml | 2 +-
 test/ssi6.shtml | 2 +-
 test/ssi9.shtml | 2 +-
 test/test.pl    | 2 +-
 7 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/UserManual.md b/UserManual.md
index 8590dcea3..e5474a3f6 100644
--- a/UserManual.md
+++ b/UserManual.md
@@ -143,8 +143,10 @@ directives are supported, `<!--#include ...>` and
 three path specifications:
 
     <!--#include virtual="path">  Path is relative to web server root
-    <!--#include file="path">     Path is relative to web server working dir
-    <!--#include "path">          Path is relative to current document
+    <!--#include abspath="path">  Path is absolute or relative to
+                                  web server working dir
+    <!--#include file="path">,    Path is relative to current document
+    <!--#include "path">
 
 The `include` directive may be used to include the contents of a file or the
 result of running a CGI script. The `exec` directive is used to execute a
diff --git a/mongoose.c b/mongoose.c
index 54bee01de..89ed4144d 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -3611,11 +3611,12 @@ static void do_ssi_include(struct mg_connection *conn, const char *ssi,
     // File name is relative to the webserver root
     (void) mg_snprintf(conn, path, sizeof(path), "%s%c%s",
         conn->ctx->config[DOCUMENT_ROOT], '/', file_name);
-  } else if (sscanf(tag, " file=\"%[^\"]\"", file_name) == 1) {
+  } else if (sscanf(tag, " abspath=\"%[^\"]\"", file_name) == 1) {
     // File name is relative to the webserver working directory
     // or it is absolute system path
     (void) mg_snprintf(conn, path, sizeof(path), "%s", file_name);
-  } else if (sscanf(tag, " \"%[^\"]\"", file_name) == 1) {
+  } else if (sscanf(tag, " file=\"%[^\"]\"", file_name) == 1 ||
+             sscanf(tag, " \"%[^\"]\"", file_name) == 1) {
     // File name is relative to the currect document
     (void) mg_snprintf(conn, path, sizeof(path), "%s", ssi);
     if ((p = strrchr(path, '/')) != NULL) {
diff --git a/test/ssi1.shtml b/test/ssi1.shtml
index 0dd64a2e7..9bffef9c1 100644
--- a/test/ssi1.shtml
+++ b/test/ssi1.shtml
@@ -1,5 +1,5 @@
 <html><pre>
 ssi_begin
-<!--#include file="Makefile" -->
+<!--#include file="../Makefile" -->
 ssi_end
 </pre></html>
diff --git a/test/ssi5.shtml b/test/ssi5.shtml
index de3e49b80..1db2a7823 100644
--- a/test/ssi5.shtml
+++ b/test/ssi5.shtml
@@ -1,5 +1,5 @@
 <html><pre>
 ssi_begin
-<!--#include file="/etc/passwd" -->
+<!--#include abspath="/etc/passwd" -->
 ssi_end
 </pre></html>
diff --git a/test/ssi6.shtml b/test/ssi6.shtml
index 2fd8b5108..4359baafa 100644
--- a/test/ssi6.shtml
+++ b/test/ssi6.shtml
@@ -1,5 +1,5 @@
 <html><pre>
 ssi_begin
-<!--#include file="c:\boot.ini" -->
+<!--#include abspath="c:\boot.ini" -->
 ssi_end
 </pre></html>
diff --git a/test/ssi9.shtml b/test/ssi9.shtml
index 92030759f..4e8377826 100644
--- a/test/ssi9.shtml
+++ b/test/ssi9.shtml
@@ -1,3 +1,3 @@
 ssi_begin
-<!--#include file="Makefile" -->
+<!--#include file="../Makefile" -->
 ssi_end
diff --git a/test/test.pl b/test/test.pl
index 45d1865b4..d455741e5 100644
--- a/test/test.pl
+++ b/test/test.pl
@@ -403,7 +403,7 @@ unless (scalar(@ARGV) > 0 and $ARGV[0] eq "basic_tests") {
   my $abs_path = on_windows() ? 'ssi6.shtml' : 'ssi5.shtml';
   my $word = on_windows() ? 'boot loader' : 'root';
   o("GET /$abs_path HTTP/1.0\n\n",
-    "ssi_begin.+$word.+ssi_end", 'SSI #include file= (absolute)');
+    "ssi_begin.+$word.+ssi_end", 'SSI #include abspath');
   o("GET /ssi7.shtml HTTP/1.0\n\n",
     'ssi_begin.+Unit test.+ssi_end', 'SSI #include "..."');
   o("GET /ssi8.shtml HTTP/1.0\n\n",
-- 
GitLab