This repository was archived by the owner on Nov 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashRepositoryModule.cs
More file actions
90 lines (86 loc) · 3.03 KB
/
Copy pathHashRepositoryModule.cs
File metadata and controls
90 lines (86 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using Nancy;
using Caching;
namespace DSTHashRepository {
public class HashRepositoryModule : Nancy.NancyModule {
private LRUCache<string, string> HashCache = new LRUCache<string, string>(1024, 24);
private string ToHashName(string rawHash) => String.Concat(rawHash.Where(c => c != '/' && c != '.' && c != '\\'));//no navigation characters
private string FilesDir => Path.Combine(".", "files");
private string ToFilePath(string hashName) => Path.Combine(FilesDir, hashName);
private string ToHashPath(string rawHash) {
string hex;
if (HashCache.TryGetValue(rawHash, out hex)) {
return hex;
}
byte[] hashHash;
using (var sha2 = new SHA256Managed()) {
hashHash = sha2.ComputeHash(Encoding.UTF8.GetBytes(rawHash));
}
hex = BytesToHex(hashHash);
HashCache.Add(rawHash, hex);
return hex;
}
public static string BytesToHex(byte[] bytes) {
var sb = new StringBuilder(bytes.Length * 2);
foreach (var b in bytes) {
sb.AppendFormat("{0:x2}", b);
}
return sb.ToString();
}
public HashRepositoryModule(IHashRepositoryConfig config) {
if (!Directory.Exists(FilesDir)) {
Directory.CreateDirectory(FilesDir);
}
Get["/"] = _ => "DST Hash Repository Prototype";
Head["/{hash*}"] = _ => {
var hashPath = ToHashPath((string)_.hash);
Console.WriteLine("HEAD: {0}", hashPath);
if (!File.Exists(hashPath)) {
return HttpStatusCode.NotFound;
}
return HttpStatusCode.OK;
};
Get["/{hash*}"] = _ => {
var hashPath = ToHashPath((string)_.hash);
Console.WriteLine("GET: {0}", hashPath);
if (!File.Exists(hashPath)) {
return HttpStatusCode.NotFound;
}
return Response.FromStream(File.OpenRead(hashPath), "application/binary");
};
Put["/{hash*}", true] = async (_, ct) => {
var hashPath = ToHashPath((string)_.hash);
var secretHeader = Request.Headers["secret"].FirstOrDefault();
if (!config.CheckCredentials("", secretHeader)) {
Console.WriteLine("PUT|AUTHFAIL: {0}", hashPath);
return HttpStatusCode.Forbidden;
}
Console.WriteLine("PUT: {0}", hashPath);
using (var destFile = File.Create(hashPath)) {
await Request.Body.CopyToAsync(destFile);
}
return HttpStatusCode.OK;
};
Delete["/{hash*}"] = _ => {
var hashPath = ToHashPath((string)_.hash);
var secretHeader = Request.Headers["secret"].FirstOrDefault();
if (!config.CheckCredentials("", secretHeader)) {
Console.WriteLine("DELETE|AUTHFAIL: {0}", hashPath);
return HttpStatusCode.Forbidden;
}
Console.WriteLine("DELETE: {0}", hashPath);
if (!File.Exists(hashPath)) {
return HttpStatusCode.NotFound;
}
File.Delete(hashPath);
return HttpStatusCode.OK;
};
}
}
}