Intercepting RO/.NET HttpServer requests

With the Spring 2010 release, RemObjects SDK for .NET lets you intercept any configurable path on any of the non-super http Server channels and do something completely different with the http request. This feature was added to support REST in Data Abstract, but can also easily be used to host custom pages or whole websites from within any of the http components (IpHttpServerChannel, HttpSysServerChannel, WebProcessor).

How it Works

Each of the HTTP servers implements *IHttpServer *and exposes a *Register *and *Unregister *method. An Extended Http Dispatcher passes it’s implementation of IHttpDispatcher that gets called for each request, returns if it can handle a specific url, and if so, is asked to process the request. Else the next dispatcher from the list gets called, until none are left and the default helpers get called.

Creating Your Own Dispatcher

Writing a custom dispatcher for a given url is quite simple. You simply need to write a new class descending from the ExtendedHttpDispatcher class and override the Process method.

class MyDispatcher: ExtendedHttpDispatcher {publicoverridevoid Process( IHttpRequest aRequest, IHttpResponse aResponse, Stream aRequestData, Stream aResponseData){ StreamWriter sw =new StreamWriter(aResponseData); aResponse.ContentType="text/html"; sw.WriteLine(@" Hello World Hello World "); sw.Flush();}}   //... MyDispatcher disp =new MyDispatcher(); disp.Path="/hello"; disp.Server= ipHttpServer1;
Given this code, all requests to `/hello` or ones that start with `/hello/` will get a nice “Hello World” page.

REST

The *RestSchemaDispatcher *class that ships with Data Abstract essentially does the same as above. It intercepts all requests to a given path. the first identifier after the path url is the table name for that request, and anything after that is used to find a record by primary key. More on the features of the *RestSchemaDispatcher *will be in one of my next blog posts.