|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Collections.Generic; |
| 4 | + |
| 5 | +using RipcordSoftware.HttpWebServer; |
| 6 | + |
| 7 | +namespace HttpWebServerExample |
| 8 | +{ |
| 9 | + public class StaticWebServer |
| 10 | + { |
| 11 | + private class ContentTypeInfo |
| 12 | + { |
| 13 | + public ContentTypeInfo(string extension, string contentType, bool compressible) |
| 14 | + { |
| 15 | + Extension = extension; |
| 16 | + ContentType = contentType; |
| 17 | + Compressible = compressible; |
| 18 | + } |
| 19 | + |
| 20 | + public string Extension { get; protected set; } |
| 21 | + |
| 22 | + public string ContentType { get; protected set; } |
| 23 | + |
| 24 | + public bool Compressible { get; protected set; } |
| 25 | + } |
| 26 | + |
| 27 | + private static readonly Dictionary<string, ContentTypeInfo> contentTypeLookup = new Dictionary<string, ContentTypeInfo>() |
| 28 | + { |
| 29 | + { "jpg", new ContentTypeInfo("jpg", @"image/jpg", false) }, |
| 30 | + { "png", new ContentTypeInfo("png", @"image/png", false) }, |
| 31 | + { "gif", new ContentTypeInfo("gif", @"image/gif", false) }, |
| 32 | + { "ico", new ContentTypeInfo("ico", @"image/ico", true) }, |
| 33 | + { "bmp", new ContentTypeInfo("bmp", @"image/x-ms-bmp", true) }, |
| 34 | + { "html", new ContentTypeInfo("html", @"text/html", true) }, |
| 35 | + { "htm", new ContentTypeInfo("htm", @"text/html", true) }, |
| 36 | + { "css", new ContentTypeInfo("css", @"text/css", true) }, |
| 37 | + { "js", new ContentTypeInfo("js", @"text/javascript", true) }, |
| 38 | + { "txt", new ContentTypeInfo("txt", @"text/plain", true) }, |
| 39 | + { "xml", new ContentTypeInfo("xml", @"text/xml", true) }, |
| 40 | + { "woff", new ContentTypeInfo("woff", @"application/font-woff", false) }, |
| 41 | + { "svg", new ContentTypeInfo("svg", @"image/svg+xml", true) } |
| 42 | + }; |
| 43 | + |
| 44 | + private static ContentTypeInfo GetContentTypeInfo(string extn) |
| 45 | + { |
| 46 | + ContentTypeInfo info = null; |
| 47 | + |
| 48 | + if (extn.Contains("/")) |
| 49 | + { |
| 50 | + extn = System.IO.Path.GetExtension(extn); |
| 51 | + } |
| 52 | + |
| 53 | + if (extn.StartsWith(".")) |
| 54 | + { |
| 55 | + extn = extn.Substring(1); |
| 56 | + } |
| 57 | + |
| 58 | + contentTypeLookup.TryGetValue(extn, out info); |
| 59 | + return info; |
| 60 | + } |
| 61 | + |
| 62 | + private static string UrlAppendPath(string url, string path) |
| 63 | + { |
| 64 | + var appendedUrl = url; |
| 65 | + |
| 66 | + if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(path)) |
| 67 | + { |
| 68 | + var terminated = url.EndsWith("/"); |
| 69 | + var prefixed = path.StartsWith("/"); |
| 70 | + |
| 71 | + if (terminated && prefixed) |
| 72 | + { |
| 73 | + appendedUrl = url + path.Substring(1); |
| 74 | + } |
| 75 | + else if (!terminated && !prefixed) |
| 76 | + { |
| 77 | + appendedUrl = url + "/" + path; |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + appendedUrl = url + path; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return appendedUrl; |
| 86 | + } |
| 87 | + |
| 88 | + public static bool HandleRequest(HttpWebRequest request, HttpWebResponse response) |
| 89 | + { |
| 90 | + bool handled = false; |
| 91 | + |
| 92 | + if (request.HttpMethod == "GET") |
| 93 | + { |
| 94 | + try |
| 95 | + { |
| 96 | + var filePath = "www" + request.Uri; |
| 97 | + var fileInfo = new FileInfo(filePath); |
| 98 | + |
| 99 | + if (fileInfo.Attributes == FileAttributes.Directory) |
| 100 | + { |
| 101 | + var redirect = UrlAppendPath(request.Uri, "index.html"); |
| 102 | + response.Redirect(redirect); |
| 103 | + handled = true; |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + var contentTypeInfo = GetContentTypeInfo(filePath); |
| 108 | + if (contentTypeInfo != null) |
| 109 | + { |
| 110 | + if (fileInfo != null) |
| 111 | + { |
| 112 | + // we are going to return something, so let the caller know it happened |
| 113 | + handled = true; |
| 114 | + |
| 115 | + // the client may already have the file, so check the timestamps |
| 116 | + bool clientHasFile = false; |
| 117 | + var ifModifiedSince = request.Headers["If-Modified-Since"]; |
| 118 | + if (!string.IsNullOrEmpty(ifModifiedSince)) |
| 119 | + { |
| 120 | + try |
| 121 | + { |
| 122 | + var lastModifiedTimestamp = DateTime.Parse(ifModifiedSince); |
| 123 | + clientHasFile = fileInfo.LastWriteTime <= lastModifiedTimestamp; |
| 124 | + } |
| 125 | + catch |
| 126 | + { |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (!clientHasFile) |
| 131 | + { |
| 132 | + // read the file from disk and return the contents to the client |
| 133 | + using (var inFile = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) |
| 134 | + { |
| 135 | + // send the last modified file time so we can cache any further requests from this client |
| 136 | + response.Headers["Last-Modified"] = fileInfo.LastWriteTimeUtc.ToString("r"); |
| 137 | + |
| 138 | + // set the content type |
| 139 | + response.ContentType = contentTypeInfo.ContentType; |
| 140 | + |
| 141 | + using (var stream = response.GetResponseStream(request.AcceptEncoding)) |
| 142 | + { |
| 143 | + inFile.CopyTo(stream); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + // the client has the file, so send a 304 |
| 150 | + response.StatusCode = 304; |
| 151 | + response.ContentType = contentTypeInfo.ContentType; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + catch (System.Exception ex) |
| 158 | + { |
| 159 | + // TODO: do something more useful here |
| 160 | + Console.WriteLine("ERROR: " + ex.Message); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return handled; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
0 commit comments