hydra

Bit of a strange question I know, but say you had a standalone application which manages a site(generating static html). You couldn't edit the application, or the outputted code, only modify the code when served.

In effect:-
1. Page 01 is requested
2. Page 01 is opened by ... and analysed
3. Functions performed on Page 01 by ...
4. Result is outputted in desired format

Couple of examples could be :-

Ex1. Externalising

Javascript

  (on the fly) i.e. any inline JS between <script></script> tags into js/file_method_1.js, js/file_method_2.js and replacing code with an external JS file/method call.

Ex2. Removing any HTML comments

perkiset

1) Put the "application" on another port on your

apache

  server.
2) in

Apache

 , using mod_rewrite, if the request is for HTML (or the app, whatever that looks like) rewrite the request into a

PHP

  handler. If it's not (ie it's a graphic request or something) then let it flow through.
3) In your

php

  handler, rethrow the request into

Apache

  on the new port and collect the output of the application into a string variable.
4) Process as necessary, echo/print the string in

PHP

  and it will go out to the original requestor.

Post again if these steps don't make sense... and I'd love to hear if someone has another method that is more efficient...

/p

hydra

Cheers Perk - will be testing over next few days and let you know how it goes

hydra

/* p.s please move this to

php

  suggestions - from code repository */

<>Reply from Perkiset
1) Put the "application" on another port on your

apache

  server.
2) in

Apache

 , using mod_rewrite, if the request is for HTML (or the app, whatever that looks like) rewrite the request into a

PHP

  handler. If it's not (ie it's a graphic request or something) then let it flow through.
3) In your

php

  handler, rethrow the request into

Apache

  on the new port and collect the output of the application into a string variable.
4) Process as necessary, echo/print the string in

PHP

  and it will go out to the original requestor.

Post again if these steps don't make sense... and I'd love to hear if someone has another method that is more efficient...

------------------------------

Would different ports make a difference?
Am currently working on same port, but put 'application' in `app` folder.
Rewrites point to folder instead of different port

------------------------------

p.s. could you possibly post mini samples of step 2/3/4 code. Think I may be on wrong path.

perkiset

quote author=hydra link=topic=291.msg2210#msg2210 date=1181951357

2) in

Apache

 , using mod_rewrite, if the request is for HTML (or the app, whatever that looks like) rewrite the request into a

PHP

  handler. If it's not (ie it's a graphic request or something) then let it flow through.


The important part about this, is that you're going to hijack all requests to <the old application> into your

PHP

  handler... which is going to throw the request back into

Apache

 . The only reason you'd need to use a different port is if you can't put another domain on the box, or perhaps you don't want to use 127.0.0.1 ... or something. You could make "internal.mydomain.com" be "127.0.0.1" and then put the handler for the application on THAT virtual host, rather than your main domain. Since all calls are coming into your

PHP

  handler rather than the application, you'll need to call the application yourself to let it do it's thing before you can manipulate it.

Although this is slightly different (and I  REALLY don't want to know if this is in fact what you're up to), the functional metaphor for this structure would be a "Man In The Middle" attack... you're hijacking requests, then proxying them to the actual handler, getting your hands on the answer before sending it back to the user.

Recapping now: calls to the original domain get routed into a

PHP

  handler, as illustrated below. Calls for graphics and such are immediately handled and don't get rewritten. The

PHP

  routine rethrows the request into the server, so when results come back they are now in your hands in the

PHP

  script, where you can have your way with it before you send it back to the surfer.


RewriteEngine on
# First - if the call is in the /graphics dir, or it is suffixed by .gif or .jpg then
# do no further rewriting...
RewriteCond  %{REQUEST_URI}  /graphics  [OR]
RewriteCond  %{REQUEST_URI    .gif      [OR]
RewriteCond  %{REQUEST_URI}  .jpg
RewriteRule    ^(.*)$    -      <>

# Rewrite all else into the app...
RewriteRule    ^(.*)$    /www/sites/mainhandler.

php

 ?$1


Now in

PHP

  you use any number of ways to throw a request right back up against the server ie., cURL, fopen, file_get_contents, there's a bunch of ways. There's even a webrequest class in the

php

  repository here. You'll need another virtual host:


<VirtualHost 127.0.0.1:80>
  ServerName  127.0.0.1
  DocumentRoot  /aDir/aDir
</VirtualHost>


This host is the one that is now answering requests for your application.

In

PHP

 , when you get the "answer" back from the application, it'll be all text and then you can have your way with it... finally simply doing a "echo $theModifiedStringBuffer;" so that the final output that you want to go to the user, does.

Is that any clearer? I know for some this is difficult to wrap their brains around.

Good luck,
/p


Perkiset's Place Home   Politics @ Perkiset's