Sunday, December 30, 2012

PAW - Using other Languages with CGI

This post is about using other languages apart from BeanShell and PHP. PAW includes a CGI Handler to execute scripts by using the Common Gateway Protocol (CGI). This handler is also used by the PHP plugin to call the CGI version of PHP. The handler is defined inside the file PAW_HOME/conf/handler.XML. Instead of using PHP we will use Perl to create a simple web page.

Finding the Executable Path

First thing to do is to install the SL4A and Perl APK from the Android Scripting Project: http://code.google.com/p/android-scripting/downloads/list After installing the APKs and extracting Perl it's time to find out where the Perl executable is located. One way to do it is to execute the Perl interpreter and to use a terminal emulator and run the ps command. In the case of Perl the executable is located here: /data/data/com.googlecode.perlforandroid/files/perl/perl

CGI Configuration

Now it's time to edit the PAW handler configuration. Open the file PAW_HOME/conf/handler.XML and add the following lines below the Basic Auth Handler configuration:
 <handler status="active">
    <name>Perl CGI  Handler</name>
    <description>Perl CGI  Handler</description>
    <removable>true</removable>
    <id>perlCgiHandler</id>
    <files />
    <params>
      <param name="perlCgiHandler.class" value="org.paw.handler.AndroidCgiHandler" />
      <param name="perlCgiHandler.root" value="[PAW_HOME]/html" />
      <param name="perlCgiHandler.suffix" value=".pl" />
      <param name="perlCgiHandler.runwith" value="/data/data/com.googlecode.perlforandroid/files/perl/perl" />
      <param name="perlCgiHandler.sl4a" value="true" />
      <param name="perlCgiHandler.ENV_TMPDIR" value="[PAW_HOME]/tmp" />
    </params>
  </handler>
Let's have a look at the parameters. The class parameter defines the handler to use. In our case this is the CGI Handler. The next parameter is the root parameter which specifies the directory the scripts are located. Subdirectories are also included. The suffix parameter defines the filename extension for the scripts. The runwith parameter is the most important one, it defines the location of the CGI binary. If the sl4a is set to true the environment variables AP_HOST and AP_PORT are set. Because this information is read from logcat entries, SL4A should be started right before the startup of PAW. Otherwise it might not possible for the handler to extract the SL4A configuration. The last parameter sets the TMPDIR environment variable. All parameters starting with ENV_ are used to set environment variables. Now the PAW configuration is complete and we can start to write a test CGI script.

The CGI Script

We will create a test CGI script inside the PAW_HOME/html directory called test.pl. The test script looks like this:
print "Content-type: text/html\n\n";

print "Hello world!\n";
After starting PAW and entering the URL http://<ip-address>:8080/test.pl into the address bar of the browser the following text should be displayed:
Hello world!

Conclusion

The Android Scripting Project offers many more interpreters. It should be possible use many of them together with PAW, as long as you can get them to work from the command line.

10 comments:

  1. Hello,

    Congratulations for this app and work. I had SL4A and perl already installed in my phone, and I followed these steps and got perl scripts working on paw server!

    The only problem is with forms: I can't send any variable via post or get method, the enviromental variable "QUERY_STRING" is always empty, and I can't use libraries as CGI, any suggestion to do this?

    By the way, I have tried a sample perl script provided by SL4A using android library and it works under SL4A but not from Paw Server, could it be a problem about permissions??

    Thank you very much

    ReplyDelete
    Replies
    1. The QUERY_STRING works for me. Just checked.
      Strange that it isn't working for you.

      Delete
  2. Is there a way of having sql lite?

    ReplyDelete
    Replies
    1. Not out if the box, but if you manage to compile the CPAN module this should work.
      I haven't tried, I don't use Perl much.

      Delete
  3. QUERY_STRING is working if I write the complete URL, like localhost?var=1
    But if I send a form with get method the variable is blank, have you tried this?
    And would have been there any way to receive with post method?

    ReplyDelete
  4. Hello friends,

    It works with native CGI? (executable compiled in C++)

    Thanks,

    --
    Silvio Clécio
    My public projects - github.com/silvioprog

    ReplyDelete
  5. Hi!
    Is possible to edit default 404 error page?

    Thanks!

    ReplyDelete
    Replies
    1. Hi,

      technically that should be possible, but that's not implemented. I don't think that I have the time to implement that anytime soon.
      Actually you could do that yourself, but there is some internal knowledge necessary, that you don't have.
      I'll put that on my todo list.

      Delete
  6. Hi
    I place perl cgi scripts in html folder but its not executing it.

    ReplyDelete
    Replies
    1. You can try to turn on debugging and see if the perlCgiHandler is used.

      Delete

Note: Only a member of this blog may post a comment.