|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionOriginally I was intrigued what it would take to write a simple webserver. But as I progressed I realized that a tiny web server could be quite useful for a number of applications that need to serve specialized web pages and where the overhead of writing an ASP.NET application is not warranted (or where it is not possible to host ASP.NET). A good example is a news aggregator, which serves a single page containing the current news feeds. This is included as an example in this project. Using the codeThe core of the application is the class TinyServer. This class provides a simple web server that only supports GET requests (no forms) and serves web pages from a directory. To run the sample webserver, you need to build the WebServer project and configure its App.Config file. <appSettings>
<add key="WebRoot" value="E:\src\DotNet\WebServer\root" />
<!-- location of the web pages to serve -->
<add key="Default" value="default.html" />
<!-- name of the default page -->
<add key="TemplatePath" value="E:\src\DotNet\WebServer\html" />
<!-- location of special templates -->
<add key="Port" value="81" />
<!-- Port to server on -->
<add key="LogFile" value="" />
<!-- filepath, set to "" for console logging -->
<add key="LogLevel" value="All" />
<!--All, Warning, Error, None -->
</appSettings>
Once the webServer application starts it instantiates TinyServer and calls Run().
This spins off the server in a separate thread. Calling Stop() terminates
the thread.
Building your own WebServer
Most likely you would want to build your own version of this webserver. You
need to subclass TinyServer and then override the necessary functions. The most
important to override is the method This is the default implementation: protected virtual void doGet(string argument)
{
string url = getUrl(argument);
if (url.StartsWith("/"))
url = url.Substring(1);
if (url.Length == 0)
url = defaultPageName;
string path = Path.Combine(webRootPath, url);
if (File.Exists(path))
{
sendOk();
sendfile(path);
}
else
sendError(404, "File Not Found");
}
To implement your version there are a number of utility functions at hand:
The RssAggregator Sample ApplicationTo demonstrate this ability I have written a simple news aggregator that regularly downloads the RSS feeds from the sources. The RssAggregator does two things:
The first part uses the RssReader class created by smallguy78. It runs in its own thread and will download feeds once the current copy is older than one hour. The second part is implemented by a subclass of TinyServer called AggServer.
AggServer only ever returns one page that contains the newsfeeds abstracts and
links to the articles. So the protected override void doGet(string argument)
{
this.sendOk();
this.sendString(writeLinkPage());
}
The smarts to create the webpage is in the method Points of InterestAcknowledgements to smallguy78 whose RssReader code I used. You can find more about it in this RSS Reader article .
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||