PeanutButter
Loading...
Searching...
No Matches
PeanutButter.SimpleHTTPServer.HttpServer Class Reference

Provides the simple HTTP server you may use ad-hoc. More...

Inheritance diagram for PeanutButter.SimpleHTTPServer.HttpServer:
PeanutButter.SimpleHTTPServer.HttpServerBase PeanutButter.SimpleHTTPServer.IHttpServer PeanutButter.SimpleTcpServer.TcpServer PeanutButter.SimpleHTTPServer.IHttpServerBase PeanutButter.SimpleHTTPServer.IHttpServerBase PeanutButter.SimpleTcpServer.ITrackingDisposable

Public Member Functions

 HttpServer (int port, bool autoStart, Action< string > logAction)
 HttpServer (int port, Action< string > logAction)
 Constructs an HttpServer listening on the configured port with the given logAction.
 HttpServer ()
 Constructs an HttpServer with autoStart true and no logAction.
 HttpServer (Action< string > logAction)
 Constructs an HttpServer with autoStart true and provided logAction.
 HttpServer (bool autoStart)
 Constructs an HttpServer based on passed in autoStart, with no logAction.
 HttpServer (bool autoStart, Action< string > logAction)
 Constructs an HttpServer.
Guid AddHandler (Func< HttpProcessor, Stream, HttpServerPipelineResult > handler)
 Adds a handler to the pipeline.
Guid AddFileHandler (Func< HttpProcessor, Stream, byte[]> handler, string contentType=HttpConstants.MimeTypes.BYTES)
 Adds a handler for providing a file download.
Guid AddDocumentHandler (Func< HttpProcessor, Stream, string > handler)
 Adds a handler to serve a text document, with (limited) automatic content-type detection.
Guid AddHtmlDocumentHandler (Func< HttpProcessor, Stream, string > handler)
 Specifically add a handler to serve an HTML document.
Guid AddJsonDocumentHandler (Func< HttpProcessor, Stream, object > handler)
 Specifically add a handler to serve a JSON document.
override void HandleRequestWithoutBody (HttpProcessor p, string method)
 Handles a request that does not contain a body (as of the HTTP spec).
Parameters
pThe HTTP processor.
methodThe HTTP method.

override void HandleRequestWithBody (HttpProcessor p, MemoryStream inputData, string method)
 Handles a general request with a request body.
Parameters
pThe HTTP processor.
inputDataThe stream to read the request body from.
methodThe HTTP method.

void ServeDocument (string queryPath, XDocument doc, HttpMethods method=HttpMethods.Any)
 Serves an XDocument from the provided path, for the provided method.
void ServeDocument (string queryPath, string doc, HttpMethods method=HttpMethods.Any)
 Serves an XDocument from the provided path, for the provided method.
Guid ServeDocument (string queryPath, Func< string > doc, HttpMethods method=HttpMethods.Any)
 Serves an XDocument from the provided path, for the provided method.
Guid ServeDocument (string queryPath, Func< XDocument > docFactory, HttpMethods method=HttpMethods.Any)
 Serves an XDocument from the provided path, for the provided method.
Guid ServeJsonDocument (string path, object data, HttpMethods method=HttpMethods.Any)
 Serves a JSON document with the provided data at the provided path for the provided method.
Guid ServeJsonDocument (string path, Func< object > dataFactory, HttpMethods method=HttpMethods.Any)
 Serves a JSON document with the provided data at the provided path for the provided method.
Guid ServeFile (string path, byte[] data, string contentType=HttpConstants.MimeTypes.BYTES)
 Serves an arbitrary file from the provided path for the provided content type (defaults to application/octet-stream)
Guid ServeFile (string path, Func< byte[]> dataFactory, string contentType=HttpConstants.MimeTypes.BYTES)
 Serves a file via a factory Func.
void Reset ()
 Clears any registered handlers & log actions so the server can be re-used with completely different logic / handlers.
bool RemoveHandler (Guid identifier)
 Removes a previously-registered handler by it's id.
Public Member Functions inherited from PeanutButter.SimpleHTTPServer.HttpServerBase
void HandleRequestWithoutBody (HttpProcessor p, string method)
 Handles a request that does not contain a body (as of the HTTP spec).
Parameters
pThe HTTP processor.
methodThe HTTP method.

void HandleRequestWithBody (HttpProcessor p, MemoryStream inputData, string method)
 Handles a general request with a request body.
Parameters
pThe HTTP processor.
inputDataThe stream to read the request body from.
methodThe HTTP method.

string GetFullUrlFor (string relativeUrl)
 Resolves the full url to the provided path on the current server.
Parameters
relativeUrl
Returns

Public Member Functions inherited from PeanutButter.SimpleTcpServer.TcpServer
void Start ()
 Start the server.
void Stop ()
 Stop the server.
virtual void Dispose ()
 Disposes the server (stops it if it is running)
Public Member Functions inherited from PeanutButter.SimpleHTTPServer.IHttpServerBase
void Start ()
 Start the server.
void Stop ()
 Stop the server.

Protected Member Functions

override void Init ()
Protected Member Functions inherited from PeanutButter.SimpleHTTPServer.HttpServerBase
 HttpServerBase (int port)
 HttpServerBase ()
override IProcessor CreateProcessorFor (TcpClient client)
Protected Member Functions inherited from PeanutButter.SimpleTcpServer.TcpServer
 TcpServer (int minPort=5000, int maxPort=32000)
 Construct the server with a random port within the provided range.
 TcpServer (int port)
 Construct the server with the explicitly-provided port.
void Init ()
 Override in derived classes: this initializes the server system.
void Log (string message, params object[] parameters)
 Provides a convenience logging mechanism which outputs via the established LogAction.
IProcessor CreateProcessorFor (TcpClient client)
 Create a processor for a particular TCP client.
virtual int NextRandomPort ()
 Guesses the next random port to attempt to bind to.

Properties

int HandlerCount [get]
 Used in debug display.
Properties inherited from PeanutButter.SimpleHTTPServer.HttpServerBase
Action< RequestLogItemRequestLogAction = null [get, set]
 Log action used for requests.
HttpVersion Version [get, set]
 HTTP version reported by the server in responses Note that this does not change behavior of the server, only the exact format of the response.
string BaseUrl [get]
 Provides the base url from which the server serves.
Properties inherited from PeanutButter.SimpleTcpServer.TcpServer
bool Disposed [get]
 Flag: when true, this server has been disposed.
int MaxShutDownTime = 1000 [get, set]
 Maximum time, in milliseconds, to wait on the listener task when shutting down.
bool LogRandomPortDiscovery [get, set]
 Whether or not to log random port discovery processes.
Action< string > LogAction = Console.WriteLine [get, set]
 Action to employ when logging (defaults to logging to the console)
int Port [get, protected set]
 Port which this server has bound to.
bool IsListening [get]
 Flag exposing listening state.
Properties inherited from PeanutButter.SimpleHTTPServer.IHttpServerBase
bool Disposed [get]
 Flag: when true, this server has been disposed.
int MaxShutDownTime [get, set]
 Maximum time, in milliseconds, to wait on the listener task when shutting down.
bool LogRandomPortDiscovery [get, set]
 Whether or not to log random port discovery processes.
Action< string > LogAction [get, set]
 Action to employ when logging (defaults to logging to the console)
int Port [get]
 Port which this server has bound to.
bool IsListening [get]
 Flag exposing listening state.

Detailed Description

Provides the simple HTTP server you may use ad-hoc.

Constructor & Destructor Documentation

◆ HttpServer() [1/4]

PeanutButter.SimpleHTTPServer.HttpServer.HttpServer ( int port,
Action< string > logAction )

Constructs an HttpServer listening on the configured port with the given logAction.

Parameters
port
logAction

◆ HttpServer() [2/4]

PeanutButter.SimpleHTTPServer.HttpServer.HttpServer ( Action< string > logAction)

Constructs an HttpServer with autoStart true and provided logAction.

Parameters
logActionAction to log messages with

◆ HttpServer() [3/4]

PeanutButter.SimpleHTTPServer.HttpServer.HttpServer ( bool autoStart)

Constructs an HttpServer based on passed in autoStart, with no logAction.

Parameters
autoStartStart immediately?

◆ HttpServer() [4/4]

PeanutButter.SimpleHTTPServer.HttpServer.HttpServer ( bool autoStart,
Action< string > logAction )

Constructs an HttpServer.

Parameters
autoStartStart immediately?
logActionAction to log messages

Member Function Documentation

◆ AddDocumentHandler()

Guid PeanutButter.SimpleHTTPServer.HttpServer.AddDocumentHandler ( Func< HttpProcessor, Stream, string > handler)

Adds a handler to serve a text document, with (limited) automatic content-type detection.

Parameters
handler

Implements PeanutButter.SimpleHTTPServer.IHttpServer.

◆ AddFileHandler()

Guid PeanutButter.SimpleHTTPServer.HttpServer.AddFileHandler ( Func< HttpProcessor, Stream, byte[]> handler,
string contentType = HttpConstants::MimeTypes::BYTES )

Adds a handler for providing a file download.

Parameters
handler
contentType

Implements PeanutButter.SimpleHTTPServer.IHttpServer.

◆ AddHandler()

Guid PeanutButter.SimpleHTTPServer.HttpServer.AddHandler ( Func< HttpProcessor, Stream, HttpServerPipelineResult > handler)

Adds a handler to the pipeline.

Parameters
handler

Implements PeanutButter.SimpleHTTPServer.IHttpServer.

◆ AddHtmlDocumentHandler()

Guid PeanutButter.SimpleHTTPServer.HttpServer.AddHtmlDocumentHandler ( Func< HttpProcessor, Stream, string > handler)

Specifically add a handler to serve an HTML document.

Parameters
handler

Implements PeanutButter.SimpleHTTPServer.IHttpServer.

◆ AddJsonDocumentHandler()

Guid PeanutButter.SimpleHTTPServer.HttpServer.AddJsonDocumentHandler ( Func< HttpProcessor, Stream, object > handler)

Specifically add a handler to serve a JSON document.

Parameters
handler

Implements PeanutButter.SimpleHTTPServer.IHttpServer.

◆ HandleRequestWithBody()

override void PeanutButter.SimpleHTTPServer.HttpServer.HandleRequestWithBody ( HttpProcessor p,
MemoryStream inputData,
string method )

Handles a general request with a request body.

Parameters
pThe HTTP processor.
inputDataThe stream to read the request body from.
methodThe HTTP method.

Implements PeanutButter.SimpleHTTPServer.IHttpServerBase.

◆ HandleRequestWithoutBody()

override void PeanutButter.SimpleHTTPServer.HttpServer.HandleRequestWithoutBody ( HttpProcessor p,
string method )

Handles a request that does not contain a body (as of the HTTP spec).

Parameters
pThe HTTP processor.
methodThe HTTP method.

Implements PeanutButter.SimpleHTTPServer.IHttpServerBase.

◆ RemoveHandler()

bool PeanutButter.SimpleHTTPServer.HttpServer.RemoveHandler ( Guid identifier)

Removes a previously-registered handler by it's id.

Parameters
identifier
Returns
Exceptions
NotImplementedException

◆ Reset()

void PeanutButter.SimpleHTTPServer.HttpServer.Reset ( )

Clears any registered handlers & log actions so the server can be re-used with completely different logic / handlers.

Implements PeanutButter.SimpleHTTPServer.IHttpServer.

◆ ServeDocument() [1/4]

Guid PeanutButter.SimpleHTTPServer.HttpServer.ServeDocument ( string queryPath,
Func< string > doc,
HttpMethods method = HttpMethods::Any )

Serves an XDocument from the provided path, for the provided method.

Parameters
queryPathAbsolute path to serve the document for
docXDocument to serve
methodWhich http method to respond to

Implements PeanutButter.SimpleHTTPServer.IHttpServer.

◆ ServeDocument() [2/4]

Guid PeanutButter.SimpleHTTPServer.HttpServer.ServeDocument ( string queryPath,
Func< XDocument > docFactory,
HttpMethods method = HttpMethods::Any )

Serves an XDocument from the provided path, for the provided method.

Parameters
queryPathAbsolute path to serve the document for
docFactoryFactory function to get the document contents
methodWhich http method to respond to

Implements PeanutButter.SimpleHTTPServer.IHttpServer.

◆ ServeDocument() [3/4]

void PeanutButter.SimpleHTTPServer.HttpServer.ServeDocument ( string queryPath,
string doc,
HttpMethods method = HttpMethods::Any )

Serves an XDocument from the provided path, for the provided method.

Parameters
queryPathAbsolute path to serve the document for
docXDocument to serve
methodWhich http method to respond to

Implements PeanutButter.SimpleHTTPServer.IHttpServer.

◆ ServeDocument() [4/4]

void PeanutButter.SimpleHTTPServer.HttpServer.ServeDocument ( string queryPath,
XDocument doc,
HttpMethods method = HttpMethods::Any )

Serves an XDocument from the provided path, for the provided method.

Parameters
queryPathAbsolute path to serve the document for
docXDocument to serve
methodWhich http method to respond to

Implements PeanutButter.SimpleHTTPServer.IHttpServer.

◆ ServeFile() [1/2]

Guid PeanutButter.SimpleHTTPServer.HttpServer.ServeFile ( string path,
byte[] data,
string contentType = HttpConstants::MimeTypes::BYTES )

Serves an arbitrary file from the provided path for the provided content type (defaults to application/octet-stream)

Parameters
pathAbsolute path matched for this file
dataData to provide
contentTypeContent type of the data

Implements PeanutButter.SimpleHTTPServer.IHttpServer.

◆ ServeFile() [2/2]

Guid PeanutButter.SimpleHTTPServer.HttpServer.ServeFile ( string path,
Func< byte[]> dataFactory,
string contentType = HttpConstants::MimeTypes::BYTES )

Serves a file via a factory Func.

Parameters
pathAbsolute path matched for this file
dataFactoryFactory for the data
contentTypeContent type

Implements PeanutButter.SimpleHTTPServer.IHttpServer.

◆ ServeJsonDocument() [1/2]

Guid PeanutButter.SimpleHTTPServer.HttpServer.ServeJsonDocument ( string path,
Func< object > dataFactory,
HttpMethods method = HttpMethods::Any )

Serves a JSON document with the provided data at the provided path for the provided method.

Parameters
pathAbsolute path matched for this document
dataFactoryFactory function returning any object which will be serialized into JSON for you
methodWhich http method to respond to

Implements PeanutButter.SimpleHTTPServer.IHttpServer.

◆ ServeJsonDocument() [2/2]

Guid PeanutButter.SimpleHTTPServer.HttpServer.ServeJsonDocument ( string path,
object data,
HttpMethods method = HttpMethods::Any )

Serves a JSON document with the provided data at the provided path for the provided method.

Parameters
pathAbsolute path matched for this document
dataAny object which will be serialized into JSON for you
methodWhich http method to respond to

Implements PeanutButter.SimpleHTTPServer.IHttpServer.


The documentation for this class was generated from the following file:
  • source/SimpleServers/PeanutButter.SimpleHTTPServer/HttpServer.cs