Comments (3)
There is not way right now, but I'm thinking we may include in the constructor a dictionary with the custom headers to response.
from embedio.
Issue resolved in new Nuget (version 1.0.20). Let me know if you get any issue.
You cant use the DefaultHeaders property from the StaticFilesModule like this:
WebServer.Module<StaticFilesModule>().DefaultHeaders.Add(Constants.HeaderPragma, HeaderPragmaValue);
Or include your headers like a Dictionary<string, string> in the StaticFilesModule constructor.
from embedio.
Thank you once again @geoperez. This is also working very well.
I am now using the following class to wrap your implementation in my application:
class StaticWebServer : IDisposable
{
// TODO: Find a random port between a certain range.
const string DefaultUrl = "http://127.0.0.1:9696/";
public StaticWebServer()
{
_server = CreateServer();
}
WebServer _server;
WebServer CreateServer()
{
var logger = new DebugLogger();
var server = new WebServer(DefaultUrl, logger);
var cwd = System.IO.Directory.GetCurrentDirectory();
var browsePath = System.IO.Path.Combine(cwd, "browse");
Debug.Print("Serving static files from: {0}", browsePath);
var headers = new Dictionary<string, string>()
{
{ Constants.HeaderCacheControl, "no-cache, no-store, must-revalidate" },
{ Constants.HeaderPragma, "no-cache" },
{ Constants.HeaderExpires, "0" }
};
var staticFileMod = new StaticFilesModule(browsePath, headers);
staticFileMod.DefaultExtension = ".html";
server.RegisterModule(staticFileMod);
return server;
}
public void RunAsync()
{
_server.RunAsync();
}
public void Dispose()
{
Dispose(true);
}
void Dispose(bool disposing)
{
if (!disposing)
return;
var server = _server;
_server = null;
if (server == null)
return;
server.Dispose();
}
#region DebugLogger
/// <summary>
/// Provides a simple logger for Debug output.
/// </summary>
class DebugLogger : ILog
{
/// <summary>
/// Writes the given line. This method is used by all other methods and it is asynchronous.
/// </summary>
/// <param name="color">The color.</param>
/// <param name="format">The format.</param>
/// <param name="args">The arguments.</param>
private static void WriteLine(ConsoleColor color, string format, params object[] args)
{
var d = DateTime.Now;
var dateTimeString = string.Format("{0}-{1}-{2} {3}:{4}:{5}.{6}",
d.Year.ToString("0000"), d.Month.ToString("00"), d.Day.ToString("00"), d.Hour.ToString("00"),
d.Minute.ToString("00"), d.Second.ToString("00"), d.Millisecond.ToString("000"));
format = dateTimeString + "\t" + format;
//ThreadPool.QueueUserWorkItem((context) =>
//{
// var current = Console.ForegroundColor;
// Console.ForegroundColor = color;
// Console.WriteLine(format, args);
// Console.ForegroundColor = current;
//});
if (args != null)
Debug.Print(format, args);
else
Debug.Print(format);
}
/// <summary>
/// Writes an Info level message
/// </summary>
/// <param name="message"></param>
public virtual void Info(object message)
{
InfoFormat(message.ToString(), null);
}
/// <summary>
/// Writes an Error level message
/// </summary>
/// <param name="message"></param>
public virtual void Error(object message)
{
ErrorFormat(message.ToString(), null);
}
/// <summary>
/// Writes an Error message with Exception
/// </summary>
/// <param name="message"></param>
/// <param name="exception"></param>
public virtual void Error(object message, Exception exception)
{
ErrorFormat(message.ToString(), null);
ErrorFormat(exception.ToString(), null);
}
/// <summary>
/// Writes an Info level message with format
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
public virtual void InfoFormat(string format, params object[] args)
{
WriteLine(ConsoleColor.Gray, format, args);
}
/// <summary>
/// Writes a Warning level message with format
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
public virtual void WarnFormat(string format, params object[] args)
{
WriteLine(ConsoleColor.DarkYellow, format, args);
}
/// <summary>
/// Writes an Error level message with format
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
public virtual void ErrorFormat(string format, params object[] args)
{
WriteLine(ConsoleColor.Red, format, args);
}
/// <summary>
/// Writes an Debug level message with format
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
public virtual void DebugFormat(string format, params object[] args)
{
WriteLine(ConsoleColor.Green, format, args);
}
}
#endregion
}
And it's easy to use in a WinForms application:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (var webServer = new StaticWebServer())
{
webServer.RunAsync();
Application.Run(new Views.MainView());
}
}
from embedio.
Related Issues (20)
- When requesting a mp4 embedio is responding with identity in the content encoding header.
- Cannot control charset in ContentType header HOT 3
- How to send statuscode 202 HOT 1
- EmbedIO 3.5.0 NuGet package references wrong/old version of Unosquare.Swan.Lite HOT 22
- System.ObjectDisposedException when processing multiple requests at once HOT 2
- WithZipFile should open a read only handle(or have the option to) HOT 2
- FileModule or StaticFolderModule get stuck returning medium files HOT 4
- Add XML as response type
- EmbedIO Dual-Stack Localhost Listener Causes Empty Replies HOT 1
- How can I custom the Server header HOT 2
- Disable request logging only
- Cookies issue
- EmbedIO.Samples console app does not work when PublishAot is set to true HOT 2
- Web Server does not work in Xamarin.iOS (IOS-17) HOT 3
- How to report progess via api - event-stream
- HttpContext.OnClose not firing HOT 1
- Getting rid of double quotes around strings in a Web API response (turning off JSON serialization) HOT 1
- Issue Serving Static Folder Xamarin ( 3.5.2) Forms on Android
- Request.Url mixing up http and https
- server stop responding after some time HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from embedio.