Relay Pages: Incoming to old IIS URL -> Request to new PHP Server for content -> Out to client

Discussion in 'ASP .Net' started by Electric Co., Dec 4, 2006.

  1. Electric Co.

    Electric Co. Guest

    Hello,

    note: This is for a Faculty web site that is undergoing a migration to
    an open source solution so my motives are legit.

    I need to build a relay from IIS handling URL_A to a PHP server
    (URL_B), get the content from the PHP server (if it exists) and then
    serve it out via IIS. If the content does not exist then I need to
    pass the request through to the IIS server and serve the pages from
    there. All of this needs to be done while retaining the existing URL_A
    on the client side as we do not want to lose/change the url.



    The steps I am thinking to accomplish this wonderful hack are:

    1.) Trap requests to URL from IIS in a HttpModule

    2.) Send a request from the HttpModule to PHP server for page content

    3.) If page content is returned from PHP server then serve it out from
    IIS (first changing any URL_B links to URL_A so that end user does not
    see the temporary url being used by the PHP server and the IIS server
    will still trap requests)

    4.) If page content is not returned from PHP then pass through request
    on IIS server and let it handle it.

    I have had limited success trying the following classes:

    HttpWebRequest,
    HttpWebResponse and
    HttpContext.Current.Response.OutputStream.Write.

    Unfortunately, after testing my code successfully on one part of the
    new web site and I am now getting -1 returned at the end of the
    following code:

    HttpWebResponse myHttpWebResponse=
    (HttpWebResponse)myRequest.GetResponse();
    Stream streamResponse=myHttpWebResponse.GetResponseStream()
    (int)myHttpWebResponse.ContentLength

    Everything else in myHttpWebResponse looks to be fine and dandy.

    It looks like I may have more success with:

    WebClient.DownloadFile



    What implications will I have with PostBacks and what if anything do I
    need to do to forward/return headers?

    Does anyone have experience with doing something like this and if so
    could I get some pointers/heads up?

    Is there a name to what I am trying to do that will help me find some
    info on the internet?

    Thanx in advance

    Jim
    Electric Co., Dec 4, 2006
    #1
    1. Advertising

  2. Re: Relay Pages: Incoming to old IIS URL -> Request to new PHP Server for content -> Out to client

    Jim,

    Let me make a few suggestions. I created a project which does url
    mapping for ASP.NET using an HTTP module. It is also implemented with
    as a provider, so you can change how the mappings are handled by
    creating your own custom implementation. It handles much of the
    trouble you come across when doing url mapping. You can get the full
    source here.

    http://svn.offwhite.net/svn/SmallSharpTools.UrlMapper/trunk/

    If you just want to look at some working code, you can see the module
    here.

    http://svn.offwhite.net/svn/SmallSharpTools.UrlMapper/trunk/ClassLibrary/UrlMapperModule.cs

    The default provider is a static 1 to 1 mapping which takes a
    configured path and maps it to a single endpoint, like
    /mappedEndpoint/*.aspx to /masterTemplate.aspx. If you were to map
    multiple endpoints to ASPX pages you could have those pages load
    content from the PHP pages, perhaps caching the content whenever you
    can to enhance performance. (see System.Web.Caching) Note that with
    IIS you need the .aspx extention to have the .NET runtime handle the
    request. Otherwise the HTTP module will not be reached. With IIS 6
    you can configure different extensions to be handled by the .NET
    runtime.

    More here on IIS and .aspx extensions:
    http://www.zorched.net/2006/01/20/aspnet-web-application-without-aspx-extension/

    I do not know enough about your requirements to know if this is the
    right approach though. Instead what you may want to do is redirect the
    pages to the new locations. I run both IIS and Apache websites and
    host my blog on an Apache server. I was running MovableType for my
    blog but eventually moved to WordPress. I moved to WordPress because
    it named the blog entries by the title of the page, I needed a way to
    map requests to the old locations to the new one. I think this is what
    you are looking to do.

    To do it I used features within Apache to handle the redirects. My old
    blog was moved to /mtblog/ while my new blog stayed at /blog/.

    First I mapped MovableType urls in /blog/ to the new WordPress urls
    with the RedirectMatch to do the a clean move from /blog/ to /mtblog/.

    RedirectMatch atom.xml$ http://brennan.offwhite.net/blog/atom/
    RedirectMatch index.xml$ http://brennan.offwhite.net/blog/feed/
    RedirectMatch archives.html$
    http://brennan.offwhite.net/mtblog/archives.html
    RedirectMatch archives/(.*)$
    http://brennan.offwhite.net/mtblog/archives/$1

    It just happened that MovableType generated static pages which I was
    able to move over the the mtblog directory. But for the more popular
    urls I wanted them to get back to the WordPress pages which had an
    updated theme. So I added a url specific mapping to redirect them over
    to the WordPress pages.

    Redirect /mtblog/archives/000303.html
    http://brennan.offwhite.net/blog/2006/01/23/working-with-javascript/

    In your case you would be mapping .aspx pages to .php so the
    RedirectMatch directive will come in handy. To do this without
    redirecting visitors to the new page (hiding the .php pages) you could
    instead use a Url Rewriting feature. Look into the Apache module
    mod_rewrite. You can have it magically accept the old url but
    internally map it to a different internal url just like I do with the
    UrlMapper project I listed above.

    Brennan Stehling
    http://brennan.offwhite.net/blog/
    ASP.NET JAD (Just Another Developer)


    Electric Co. wrote:
    > Hello,
    >
    > note: This is for a Faculty web site that is undergoing a migration to
    > an open source solution so my motives are legit.
    >
    > I need to build a relay from IIS handling URL_A to a PHP server
    > (URL_B), get the content from the PHP server (if it exists) and then
    > serve it out via IIS. If the content does not exist then I need to
    > pass the request through to the IIS server and serve the pages from
    > there. All of this needs to be done while retaining the existing URL_A
    > on the client side as we do not want to lose/change the url.
    >
    >
    >
    > The steps I am thinking to accomplish this wonderful hack are:
    >
    > 1.) Trap requests to URL from IIS in a HttpModule
    >
    > 2.) Send a request from the HttpModule to PHP server for page content
    >
    > 3.) If page content is returned from PHP server then serve it out from
    > IIS (first changing any URL_B links to URL_A so that end user does not
    > see the temporary url being used by the PHP server and the IIS server
    > will still trap requests)
    >
    > 4.) If page content is not returned from PHP then pass through request
    > on IIS server and let it handle it.
    >
    > I have had limited success trying the following classes:
    >
    > HttpWebRequest,
    > HttpWebResponse and
    > HttpContext.Current.Response.OutputStream.Write.
    >
    > Unfortunately, after testing my code successfully on one part of the
    > new web site and I am now getting -1 returned at the end of the
    > following code:
    >
    > HttpWebResponse myHttpWebResponse=
    > (HttpWebResponse)myRequest.GetResponse();
    > Stream streamResponse=myHttpWebResponse.GetResponseStream()
    > (int)myHttpWebResponse.ContentLength
    >
    > Everything else in myHttpWebResponse looks to be fine and dandy.
    >
    > It looks like I may have more success with:
    >
    > WebClient.DownloadFile
    >
    >
    >
    > What implications will I have with PostBacks and what if anything do I
    > need to do to forward/return headers?
    >
    > Does anyone have experience with doing something like this and if so
    > could I get some pointers/heads up?
    >
    > Is there a name to what I am trying to do that will help me find some
    > info on the internet?
    >
    > Thanx in advance
    >
    > Jim
    Brennan Stehling, Dec 5, 2006
    #2
    1. Advertising

  3. Electric Co.

    Electric Co. Guest

    Re: Relay Pages: Incoming to old IIS URL -> Request to new PHP Server for content -> Out to client

    Thanx Brennan,

    Quick question, what exactly fires the

    PostMapRequestHandler.

    I can find much information on this handler.

    Jim
    Electric Co., Dec 6, 2006
    #3
  4. Electric Co.

    Electric Co. Guest

    Re: Relay Pages: Incoming to old IIS URL -> Request to new PHP Server for content -> Out to client

    Further to my original post.

    The name for what I am trying to do is "Reverse Proxy"

    Lots of resources on the internet using this as a search term.

    Jim
    Electric Co., Dec 6, 2006
    #4
  5. Re: Relay Pages: Incoming to old IIS URL -> Request to new PHP Server for content -> Out to client

    Look for the ASP.NET lifecycle. You will find a full list of the
    events that fire.

    For the UrlMapper I had to place those events at just the right stage
    in the cycle.

    Brennan Stehling
    http://brennan.offwhite.net/blog/


    Electric Co. wrote:
    > Thanx Brennan,
    >
    > Quick question, what exactly fires the
    >
    > PostMapRequestHandler.
    >
    > I can find much information on this handler.
    >
    > Jim
    Brennan Stehling, Dec 6, 2006
    #5
  6. Re: Relay Pages: Incoming to old IIS URL -> Request to new PHP Server for content -> Out to client

    I created a reverse proxy system with Apache using mod_perl a long time
    ago. Perhaps you could use the Apache 2 features for proxying as they
    are not built-in and quite powerful.

    Look over this page...

    http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

    What I use to do with IIS6 was front the databases with an Apache 2
    server set up as a reverse proxy with the gzip compression features on.
    It was a great way to speed up the data transfer. With some fancy
    reverse proxy and url rewriting you may be able to achieve what you
    want. But combining those features together can be tough. They may
    call that module chaining. I have not read up on all of the latest 2.2
    features.

    Brennan Stehling
    http://brennan.offwhite.net/blog/

    Electric Co. wrote:
    > Further to my original post.
    >
    > The name for what I am trying to do is "Reverse Proxy"
    >
    > Lots of resources on the internet using this as a search term.
    >
    > Jim
    Brennan Stehling, Dec 6, 2006
    #6
  7. Re: Relay Pages: Incoming to old IIS URL -> Request to new PHP Server for content -> Out to client

    typo...

    now instead not built-in...


    Brennan Stehling wrote:
    > I created a reverse proxy system with Apache using mod_perl a long time
    > ago. Perhaps you could use the Apache 2 features for proxying as they
    > are not built-in and quite powerful.
    >
    > Look over this page...
    >
    > http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
    >
    > What I use to do with IIS6 was front the databases with an Apache 2
    > server set up as a reverse proxy with the gzip compression features on.
    > It was a great way to speed up the data transfer. With some fancy
    > reverse proxy and url rewriting you may be able to achieve what you
    > want. But combining those features together can be tough. They may
    > call that module chaining. I have not read up on all of the latest 2.2
    > features.
    >
    > Brennan Stehling
    > http://brennan.offwhite.net/blog/
    >
    > Electric Co. wrote:
    > > Further to my original post.
    > >
    > > The name for what I am trying to do is "Reverse Proxy"
    > >
    > > Lots of resources on the internet using this as a search term.
    > >
    > > Jim
    Brennan Stehling, Dec 6, 2006
    #7
  8. Electric Co.

    Electric Co. Guest

    Re: Relay Pages: Incoming to old IIS URL -> Request to new PHP Server for content -> Out to client

    Thanx Brennan,

    I am now looking at doing the Reverse Proxy on the Apache side.

    Apache seems to be a little more friendlier then IIS. That said from
    first glance it looks like you can get a bit more control with IIS - at
    a price though.

    Seeing as we are moving away from IIS and towards APACHE it does make
    sense to go whole hog right from the start.

    Thanks again.

    Jim
    Electric Co., Dec 7, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jinsong Liu
    Replies:
    2
    Views:
    398
    Jinsong Liu
    Sep 10, 2004
  2. baroque Chou
    Replies:
    5
    Views:
    467
    Sonu Kapoor [MVP]
    Mar 10, 2006
  3. tshad
    Replies:
    1
    Views:
    877
    bruce barker
    May 5, 2008
  4. Rajive Narain
    Replies:
    0
    Views:
    1,553
    Rajive Narain
    Sep 18, 2009
  5. tito
    Replies:
    0
    Views:
    95
Loading...

Share This Page