Skip to content

Commit

Permalink
function code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jef King committed May 5, 2017
1 parent 0e45783 commit d53f91a
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
19 changes: 19 additions & 0 deletions StaticFileServer/function.json
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
}
9 changes: 9 additions & 0 deletions StaticFileServer/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"frameworks": {
"net46":{
"dependencies": {
"MediaTypeMap": "2.1.0"
}
}
}
}
82 changes: 82 additions & 0 deletions StaticFileServer/run.csx
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);
}
1 change: 1 addition & 0 deletions host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
10 changes: 10 additions & 0 deletions proxies.json
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}"
}
}
}

0 comments on commit d53f91a

Please sign in to comment.