Add Preprocessor to HTML (Probably in Apache) - php

I would like to add a preprocessor to HTML pages. Basically, I have a program that takes the name of an HTML file containing preprocessor instructions and outputs the contents of the file after preprocessing to stdout. This mechanism could change if it makes things easier. All I want to do is hook this into Apache so that all the files that my website serves get put through the preprocessor before going out to the browser. A solution that works with other HTTP servers than Apache would be preferred, but is not required.
If my understanding is correct, this is roughly what PHP does.
If it makes any difference, the preprocessor is written in Python.

If you have Apache and a "preprocessor" written in python, why not go for mod_python?

Related

How to run PHP and Perl program in one server(Apache)

I want to put my PHP program on Apache server(there are existing Perl program running there).
And I want to invoke those programs independently.
For example)
"http://my-address/existing-program" goes to Perl program,
on the other hand, "http://my-address/new-program" goes to PHP program.
I might need to modify "httpd.conf" but I am not sure how.
Any advise would be helpful.
Kind regards.
You could use mod_rewrite calls in your httpd.conf file to intercept specific URLs and make sure they're routed and handled accordingly.
Hidden features of mod_rewrite

Possible to write an Apache file handler in Php?

I wonder if and how it is possible to write a custom "file handler" (parsing a file and rendering it with bonuses) for Apache 2 in PHP? Files are text files, and they could be large, so I'm not thinking of loading them entirely in memory, but processing them line by line.
I'm comfortable with Java and other languages but still rookie in PHP; I chose PHP because it's light and especially deployable on every Apache-capable machine (even small NAS), and, well, I like PHP.
Thank you for your hints.
Its not possible to write a file handler in php.
However you could use the rewrite engine to redirect those files that you want to handle to a php script that, then, does the job.
the original file can be obtained from the server variables.

Setting up a Server-side interpreter

OK. So, let's take PHP as an example.
I'm entering a URL in the browser, requesting a php script. (e.g. http://www.somedomain.com/index.php)
The browser sends that request to the appropriate server.
The server recognizes - by its extension (.php) - that's it's a PHP
file.
The PHP interpreter processes the file and outputs the results.
The server sends back the output.
Now, let's say we are dealing with a... home-made interpreter XYZ (with its binary,etc), dealing e.g. with .xyz files.
How should I go about that, so all the above things are valid for my XYZ interpreter?
How should I set this whole thing up, so that it works properly?
Hint : I'm not requesting help on how to write an interpreter, but on how to make it function the above way...
Your options are to either use an existing, standard interface and write your software to conform to it, or to write a new module for the server.
For the former, CGI is simple but inefficient while FastCGI is more modern but relatively complicated.
If you want to write a new module, then the specifics will depend on the server you are dealing with.
This totally depends on what web server you are using. However, if that is Apache, you should look into apxs for building and installing your own apache modules. Google will help you find online tutorials on apxs.
For adding a handler for a specific file ending, you should do something like this in your apache configuration (after your iterpreter/apache module is built):
AddHandler my_handler .end

Is there a PHPish way of doing webpages with Ruby?

Is there a framework or something out there so that I can develop webpages in Ruby the same way I can as PHP. Something like
<html><head></head><body>
<?ruby
puts '<p> Hello there!</p>'
?>
</body></html>
The only thing I'm seeing for using Ruby in webpages is huge complex frameworks that is completely different from how PHP works. I mean, sure that's all fine and dandy with the 3 tier model and such but when your just wanting a few simple things done(which are trivial in PHP) in a webpage, to setup such a large framework just doesn't seem right. Especially when you only really want like 1 page made in Ruby and the rest being plain HTML.
The default behavior for PHP is to run as CGI scripts, which means that the web server calls php-cgi <path/to/php-script> or something similar, passing quite a lot of environment variables. To do the same with Ruby, you need to setup a script to handle .rb files. This varies wildly depending on your web server, but if you are using Apache 2.2, put this in your httpd.conf or .htaccess file:
Action ruby-cgi /path/to/ruby-cgi
AddHandler ruby-cgi .rb
# You might want to add this too:
DirectoryIndex index.rb index.html
You could either specify the path to your ruby executable (run which ruby to get the path), or to any other script that accepts a filename as the first parameter. If you use the ruby executable, nothing magical happens, and you can't insert erb into the file without adding some ERB compiling yourself. However, you could use my ruby-cgi script, which does several things:
First, it takes the file and interprets it as ERB, this make the syntax look more like PHP (see below for an example).
Second, it initializes the CGI object into the global variable $CGI. See below for an example on how to use this.
This is a simple example script on how you can use the ruby-cgi "magic":
<% header "Content-Type" => "text/html" %>
<html>
<head>
<title><%= $CGI['title'] %>
</head>
<body>
<h1><%= $CGI['title'] %>
</body>
</html>
Let's say you put this into the webroot with the name example.rb. If you then access this with a URL similar to http://example.com/example.rb?title=Hello%20world it should set the title to "Hello world", and it should display a <h1> with "Hello world" in it.
If you find any bugs with the script, feel free to fork the gist and update it.
Two words: Sinatra and ERB
(For lightweight sites, at least).
Sinatra is a simple HTTP server, ERB is a templating system that acts similar to templating in PHP.
Have you seen Nanoc? It is very simple ruby compiler that outputs static pages through a template engine. May be too simple for your needs but it is handy some times.
You can, with https://github.com/migrs/rack-server-pages
Instruction at that page.
Summarizing:
gem install rack-server-pages
Create config.ru in the root folder of your "application":
require 'rack-server-pages'
run Rack::ServerPages
Create public/index.erb:
<h1>Hello rack!</h1>
<p><%= Time.now %></p>
execute rackup, your Ruby powered pages are ready to be served.
It also works with other application servers, for example, for passenger standalone, you just need to go to that folder and run passenger start.

C++ Serve PHP documents?

I am writing a small web server, nothing fancy, I basically just want to be able to show some files. I would like to use PHP though, and im wondering if just putting the php code inside of the html will be fine, or if I need to actually use some type of PHP library?
http://www.adp-gmbh.ch/win/misc/webserver.html
I just downloaded that and I am going to use that to work off of. Basically I am writing a serverside game plugin that will allow game server owners to access a web control panel for their server. Some features would be possible with PHP so this is my goal. Any help would be appreciated, thanks!
The PHP won't serve itself. What happens in a web server like Apache is before the PHP is served to the user it is passed through a PHP parser. That PHP parser reads, understands and executes anything between (or even ) tags depending on configuration. The resultant output, usually still HTML, is served by the web server.
There are a number of ways to achieve this. Modules to process PHP have been written by Apache but you do not have to use these. PHP.exe on windows, installed from windows.php.net, will do this for you. Given a PHP file as an argument it will parse the PHP and spit the result back out on the standard output.
So, one option for you is to start PHP.exe from within your web server with a re-directed standard output to your program, and serve the result.
How to create a child process with re-directed IO: http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx however, you won't be writing the child process, that'll be PHP.exe
Caveat: I am not sure from a security / in production use perspective if this is the most secure approach, but it would work.
PHP needs to be processed by the PHP runtime. I'm assuming the case you're talking about is that you have a C++ server answering HTTP queries, and you want to write PHP code out with the HTML when you respond to clients.
I'm not aware of any general-purpose PHP library. The most straightforward solution is probably to use PHP as a CGI program.
Here's a link that might be useful for that: http://osdir.com/ml/php-general/2009-06/msg00473.html
This method is nice because you don't need to write the HTML+PHP out to a file first; you can stream it to PHP.
You need execute the PHP page to serve the page it generates.
The easiest thing for you to do would be to add CGI support to your webserver in some basic form. This is non-trivial, but not too difficult. Basically you need to pass PHP an environment and input, and retrieve the output.
Once you have CGI support you can just use any executable, including PHP, to generate webpages.

Categories