-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jef King
committed
May 5, 2017
1 parent
0e45783
commit d53f91a
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "anonymous", | ||
"name": "req", | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"methods": [ | ||
"get" | ||
] | ||
}, | ||
{ | ||
"name": "$return", | ||
"type": "http", | ||
"direction": "out" | ||
} | ||
], | ||
"disabled": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"frameworks": { | ||
"net46":{ | ||
"dependencies": { | ||
"MediaTypeMap": "2.1.0" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System.Net; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using System.IO; | ||
using MimeTypes; | ||
|
||
const string staticFilesFolder = "www"; | ||
static string defaultPage = string.IsNullOrEmpty(GetEnvironmentVariable("DEFAULT_PAGE")) ? | ||
"index.html" : GetEnvironmentVariable("DEFAULT_PAGE"); | ||
|
||
public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log) | ||
{ | ||
try | ||
{ | ||
var filePath = GetFilePath(req, log); | ||
|
||
var response = new HttpResponseMessage(HttpStatusCode.OK); | ||
var stream = new FileStream(filePath, FileMode.Open); | ||
response.Content = new StreamContent(stream); | ||
response.Content.Headers.ContentType = new MediaTypeHeaderValue(GetMimeType(filePath)); | ||
return response; | ||
} | ||
catch | ||
{ | ||
return new HttpResponseMessage(HttpStatusCode.NotFound); | ||
} | ||
} | ||
|
||
private static string GetScriptPath() | ||
=> Path.Combine(GetEnvironmentVariable("HOME"), @"site\wwwroot"); | ||
|
||
private static string GetEnvironmentVariable(string name) | ||
=> System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process); | ||
|
||
private static string GetFilePath(HttpRequestMessage req, TraceWriter log) | ||
{ | ||
var pathValue = req.GetQueryNameValuePairs() | ||
.FirstOrDefault(q => string.Compare(q.Key, "file", true) == 0) | ||
.Value; | ||
|
||
var path = pathValue ?? ""; | ||
|
||
var staticFilesPath = Path.GetFullPath(Path.Combine(GetScriptPath(), staticFilesFolder)); | ||
var fullPath = Path.GetFullPath(Path.Combine(staticFilesPath, path)); | ||
|
||
if (!IsInDirectory(staticFilesPath, fullPath)) | ||
{ | ||
throw new ArgumentException("Invalid path"); | ||
} | ||
|
||
var isDirectory = Directory.Exists(fullPath); | ||
if (isDirectory) | ||
{ | ||
fullPath = Path.Combine(fullPath, defaultPage); | ||
} | ||
|
||
return fullPath; | ||
} | ||
|
||
private static bool IsInDirectory(string parentPath, string childPath) | ||
{ | ||
var parent = new DirectoryInfo(parentPath); | ||
var child = new DirectoryInfo(childPath); | ||
|
||
var dir = child; | ||
do | ||
{ | ||
if (dir.FullName == parent.FullName) | ||
{ | ||
return true; | ||
} | ||
dir = dir.Parent; | ||
} while (dir != null); | ||
|
||
return false; | ||
} | ||
|
||
private static string GetMimeType(string filePath) | ||
{ | ||
var fileInfo = new FileInfo(filePath); | ||
return MimeTypeMap.GetMimeType(fileInfo.Extension); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"proxies": { | ||
"files": { | ||
"matchCondition": { | ||
"route": "{*path}" | ||
}, | ||
"backendUri": "https://%WEBSITE_SITE_NAME%.azurewebsites.net/api/StaticFileServer?file={path}" | ||
} | ||
} | ||
} |