Project

General

Profile

[Solved] Example code for replacing a string in the response body? (alternative solution uses Set-Cookie)

Added by thatseattleguy about 2 years ago

Greetings again. Just getting back to the wonderful world of lighttpd after a long time. Client is RHEL 7 with lighty 1.4.54 (ssl).

We have a use case where we want to insert the client's original IP address in the output page (n.b.: these are PRIVATE devices on an internal corporate network - not public users!). So far, I've successfully enabled magnet and lua support and have been able to use those to insert a custom HTTP header in the response, which is cool. New custom header looks like:

X-Corporate-IP: {device's static ip address, eg 192.168.55.102}

However, I was too much of a n00b to have realized that the page's Javascript can't access this (or any) headers (not without a second Ajax call, which isn't going to work for other reasons here). So what I really want to do is replace a given string in the actual response body with the IP address. In other words, where the stored page lighttpd is serving up has a line like:

  <script>var OrigCorpIP = "{replace this with caller IP}"</script>

the device would actually get:

  <script>var OrigCorpIP = "192.168.55.102"</script>

Questions:
1) Can I do the above with a lua script?
2) If yes, can someone point me to a working example of munging the output/response body in this way?

Honestly, I've searched and not found this anywhere yet - apologies if I've completely missed it.

thanks! and thanks for lighty!


Replies (6)

RE: Example code for replacing a string in the response body? - Added by gstrauss about 2 years ago

lighttpd mod_magnet can do many things to manipulate headers and mod_magnet can generate a response, but it is not designed to filter responses created by other handlers. While that may be a future feature, it will probably be in a new, separate lighttpd module.

Is your backend CGI, FastCGI, SCGI, or AJP13? The REMOTE_ADDR environment variable set by lighttpd may be useful for the backend to insert this information into the response. If using mod_proxy, then the backend might look into the headers added by mod_proxy, which include Forwarded and X-Forwarded-For and insert that into the response.

If your backend is unable to insert this information into the response, you might create a FastCGI script as an intermediate, which handles the request from lighttpd, contacts the backend, gets the response from the backend, modifies the response, and then returns the modified response to lighttpd.

RE: Example code for replacing a string in the response body? - Added by thatseattleguy about 2 years ago

Thanks for the quick reply – and confirmation of what I was suspecting after I'd researched a bit more (that mod_magnet couldn't do this). [edited per note below]

Right now there's no backend at all, not even CGI - originally this application had only static pages being returned. I might go that route, but for now I will just settle for lighttpd injecting a Set-Cookie header (via lua) and having the page's javascript code pick up that cookie to get the original IP address. Thanks again.

RE: Example code for replacing a string in the response body? - Added by thatseattleguy about 2 years ago

sorry - typo - meant that to read "that mod_magnet couldn't do this. sure wish we could edit our forum posts...

RE: Example code for replacing a string in the response body? - Added by gstrauss about 2 years ago

Unfortunately, post edits are not allowed due to lots of spam from SEO miscreants.

If there is no backend, then a quick server-side solution might be using mod_rewrite to rewrite to an server-side include page served by mod_ssi which inserts REMOTE_ADDR and then includes the target page.

Your solution using Set-Cookie seems like a clean solution, too.

RE: [Solved] Example code for replacing a string in the response body? (alternative solution uses Set-Cookie) - Added by gstrauss almost 2 years ago

FYI: lighttpd 1.4.65 (next lighttpd release) will provide experimental support for obtaining the response body and allowing you to edit it, though YMMV since the mechanism is simple and reads the entire response into memory, which means this feature is not appropriate for all uses. However, the feature should be fine for editing reasonably-sized HTML pages.

I still think the Set-Cookie method that you settled upon is a good solution.

RE: [Solved] Example code for replacing a string in the response body? (alternative solution uses Set-Cookie) - Added by thatseattleguy almost 2 years ago

Awesome! The Set-Cookie method is working fine for me, but I will greatly look forward to 1.4.65 and testing the new response body munging capability. In our application, the HTML pages are small, and this may be perfect for something else we want to add to the feature set.

Thanks again for the reply and for lighttpd itself, of course.

    (1-6/6)