Re: RewriteMap PRG By Example Using C

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

Good explanation !!!

But, How do you improve this C program ?

On Wed, Apr 29, 2009 at 3:09 PM, Brian Mearns <mearns.b@xxxxxxxxx> wrote:
On Wed, Apr 29, 2009 at 1:58 PM, ricardo figueiredo
<ricardoogrande@xxxxxxxxx> wrote:
> Sorry,
>
> Let me explain my situation again.
> I'm using directive RewriteMap using external rewriting program.
> This program is developed in language C.
>
> When I require a page in the browser is shown an error (Bad Request 400).
>
> My program in C to rewriting URL
>
> #include <stdio.h>
>   int main() {
>   int id = 0;
>     fscanf(stdin, "%d", &id);
>     switch(id) {
>        case 111: fprintf(stdout, "/index.html");  break;
>        case 222: fprintf(stdout, "/new.html");  break;
>        case 333: fprintf(stdout, "/teste.php?var=4/8"); break;
>     }
>
>     fflush(stdout);
>     return 0;
> }
>
> My httpd.conf is:
>
>   RewriteEngine on
>   RewriteLock /usr/local/apache2/logs/file.lock
>   RewriteMap prgmap prg:/usr/local/apache2/file
>   RewriteRule ^/(.*) ${prgmap:$1}
>
> My rewritelog:
>
> [localhost/sid#8b4cd68][rid#8ce45a0/initial] (2) init rewrite engine with
> requested uri /111
> [localhost/sid#8b4cd68][rid#8ce45a0/initial] (3) applying pattern '^/(.*)'
> to uri '/111'
> [localhost/sid#8b4cd68][rid#8ce45a0/initial] (5) map lookup OK: map=prgmap
> key=111 -> val=
> [localhost/sid#8b4cd68][rid#8ce45a0/initial] (2) rewrite '/111' -> ''
> [localhost/sid#8b4cd68][rid#8ce45a0/initial] (2) local path result:
>
> Why is this error ??
>
> I hope have given all information.
>
> Ricardo
>
>

First of all, like Eric said yesterday, you need a loop in your main
function. The program is not invoked every time the rewrite needs to
be applied, it is started once when the Apache server starts, and just
gets a new line of input for each request to be mapped.

Second, you need to terminate your output with a newline. Apparently,
apache gave up waiting for your program to give it a valid output (one
that is terminated by a newline) and decided to go with an empty
value, instead. Using fflush is probably a good idea, but you'll want
to make sure it's inside the loop, so the output gets flushed for
every mapping.

To make this a proper program for this use, it should read an entire
line up to and including the linebreak (which Eric also said). I think
gets or fgets will do the job just fine, and then if you want to use
scanf, you can use sscanf to scan the string that you read with fgets.
Second, you'll want some way to terminate the loop, so it doesn't run
forever. I'm not sure exactly how this is normally handled (or maybe
apache even forcibly kills the program when it's done, but that's
unlikely). My best guess would be if you read an EOF on standard
input, you can quit.

-Brian

--
Feel free to contact me using PGP Encryption:
Key Id: 0x3AA70848
Available from: http://keys.gnupg.net

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@xxxxxxxxxxxxxxxx
  "   from the digest: users-digest-unsubscribe@xxxxxxxxxxxxxxxx
For additional commands, e-mail: users-help@xxxxxxxxxxxxxxxx




--
Muito Obrigado

Ricardo

[Index of Archives]     [Open SSH Users]     [Linux ACPI]     [Linux Kernel]     [Linux Laptop]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Squid]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]

  Powered by Linux