Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2014-08

Translating php://input into fcl-web

12 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Leonardo M. Ramé

Hi, I'm trying to translate this PHP code to fcl-web (actually brookframework):

<?php
$content = file_get_contents('php://input');
$fh = fopen('output.wav', 'w') or die("can't open file");
fwrite($fh, $content);
fclose($fh);
?>

The difficult part is the line with "file_get_contents('php://input')", it contains the whole POST content as binary.

What I already did was this:

procedure TActAudio.Post;
var
   lWav: TMemoryStream;
begin
   lWav := TMemoryStream.Create;
   try
     lWav.Write(HttpRequest.Content[1], HttpRequest.ContentLength);
     lWav.Position:= 0;
     lWav.SaveToFile('/tmp/output.wav');
   finally
     lWav.Free;
   end;
end;

But the output.wav result is corrupt, while the php one is correct.

Any hint?.

Regards,

Reply by email

silvioprog
to Leonardo M. Ramé
"Leonardo M. Ram?" wrote 35 lines
Hi, I'm trying to translate this PHP code to fcl-web (actually brookframework): <?php $content = file_get_contents('php://input'); $fh = fopen('output.wav', 'w') or die("can't open file"); fwrite($fh, $content); fclose($fh); ?> The difficult part is the line with "file_get_contents('php://input')", it contains the whole POST content as binary. What I already did was this: procedure TActAudio.Post; var lWav: TMemoryStream; begin lWav := TMemoryStream.Create; try lWav.Write(HttpRequest.Content[1], HttpRequest.ContentLength); lWav.Position:= 0; lWav.SaveToFile('/tmp/output.wav'); finally lWav.Free; end; end; But the output.wav result is corrupt, while the php one is correct. Any hint?. Regards,

Worked fine here:

procedure TMyAction.Post;
begin
  with TFileStream.Create('out.txt', fmCreate) do
  try
    Write(TheRequest.Content[1], TheRequest.ContentLength);
  finally
    Free;
  end;
end;

POST /cgi1.br HTTP/1.1

{"key1":"value1","key2":"value2"}

out.txt:
{"key1":"value1","key2":"value2"}

Or:

procedure TMyAction.Post;
begin
  with TMemoryStream.Create do
  try
    Write(TheRequest.Content[1], TheRequest.ContentLength);
    SaveToFile('post.txt');
  finally
    Free;
  end;
end;

P.S.: php://input is not available with enctype="multipart/form-data".

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140823/ac22f440/attachment.html>

Reply by email

Leonardo M. Ramé
to silvioprog
silvioprog wrote 74 lines
On Sat, Aug 23, 2014 at 3:12 PM, "Leonardo M. Ram?" < <mailto:>> wrote: Hi, I'm trying to translate this PHP code to fcl-web (actually brookframework): <?php $content = file_get_contents('php://__input'); $fh = fopen('output.wav', 'w') or die("can't open file"); fwrite($fh, $content); fclose($fh); ?> The difficult part is the line with "file_get_contents('php://__input')", it contains the whole POST content as binary. What I already did was this: procedure TActAudio.Post; var lWav: TMemoryStream; begin lWav := TMemoryStream.Create; try lWav.Write(HttpRequest.__Content[1], HttpRequest.ContentLength); lWav.Position:= 0; lWav.SaveToFile('/tmp/output.__wav'); finally lWav.Free; end; end; But the output.wav result is corrupt, while the php one is correct. Any hint?. Regards, Worked fine here: procedure TMyAction.Post; begin with TFileStream.Create('out.txt', fmCreate) do try Write(TheRequest.Content[1], TheRequest.ContentLength); finally Free; end; end; POST /cgi1.br <http://cgi1.br> HTTP/1.1 {"key1":"value1","key2":"value2"} out.txt: {"key1":"value1","key2":"value2"} Or: procedure TMyAction.Post; begin with TMemoryStream.Create do try Write(TheRequest.Content[1], TheRequest.ContentLength); SaveToFile('post.txt'); finally Free; end; end; P.S.: php://input is not available with enctype="multipart/form-data".

Yes, with plain text everything works ok, the problem raises when I try to receive binary data, in this case a WAV file, enctype: "audio/x-wav".

The client (a Flash app running on the browser) is sending a POST message without params, just the binary data.

Reply by email

Leonardo M. Ramé
to Leonardo M. Ramé
"Leonardo M. Ram?" wrote 85 lines
On 23/08/14 17:18, silvioprog wrote: > On Sat, Aug 23, 2014 at 3:12 PM, "Leonardo M. Ram?" < > <mailto:>> wrote: > > Hi, I'm trying to translate this PHP code to fcl-web (actually > brookframework): > > <?php > $content = file_get_contents('php://__input'); > $fh = fopen('output.wav', 'w') or die("can't open file"); > fwrite($fh, $content); > fclose($fh); > ?> > > The difficult part is the line with > "file_get_contents('php://__input')", it contains the whole POST > content as binary. > > What I already did was this: > > procedure TActAudio.Post; > var > lWav: TMemoryStream; > begin > lWav := TMemoryStream.Create; > try > lWav.Write(HttpRequest.__Content[1], > HttpRequest.ContentLength); > lWav.Position:= 0; > lWav.SaveToFile('/tmp/output.__wav'); > finally > lWav.Free; > end; > end; > > But the output.wav result is corrupt, while the php one is correct. > > Any hint?. > > Regards, > > > Worked fine here: > > procedure TMyAction.Post; > begin > with TFileStream.Create('out.txt', fmCreate) do > try > Write(TheRequest.Content[1], TheRequest.ContentLength); > finally > Free; > end; > end; > > POST /cgi1.br <http://cgi1.br> HTTP/1.1 > > {"key1":"value1","key2":"value2"} > > out.txt: > {"key1":"value1","key2":"value2"} > > Or: > > procedure TMyAction.Post; > begin > with TMemoryStream.Create do > try > Write(TheRequest.Content[1], TheRequest.ContentLength); > SaveToFile('post.txt'); > finally > Free; > end; > end; > > P.S.: php://input is not available with enctype="multipart/form-data". > Yes, with plain text everything works ok, the problem raises when I try to receive binary data, in this case a WAV file, enctype: "audio/x-wav". The client (a Flash app running on the browser) is sending a POST message without params, just the binary data.

I made an example using fcl-web (without Brook) and the problem persists.

I attached an example for anyone to test, please, copy the following files to your apache documents folder (/var/www/html on Ubuntu):

testwami.html
swfobject.js
wamirecorder.js

Also please download the file Wami.sfw from here:
https://wami-recorder.googlecode.com/hg/example/client/Wami.swf and copy to the same folder as the previous files. I cannot attach it here because of email size restrictions of the mailing list.

Also please compile the project cgiproject.lpi contained in wami.tar.gz, then copy the executable to your /cgi-bin/ directory.

Regards,

Leonardo M. Ram? http://leonardorame.blogspot.com -------------- next part -------------- A non-text attachment was scrubbed... Name: wami.tar.gz Type: application/gzip Size: 8115 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140823/a45949cd/attachment-0001.bin>

Reply by email

Leonardo M. Ramé
to Leonardo M. Ramé: Yes, with plain text everything works ok, the problem raises when I try to receive binary…
"Leonardo M. Ram?" wrote 85 lines
On 23/08/14 17:18, silvioprog wrote: > On Sat, Aug 23, 2014 at 3:12 PM, "Leonardo M. Ram?" < > <mailto:>> wrote: > > Hi, I'm trying to translate this PHP code to fcl-web (actually > brookframework): > > <?php > $content = file_get_contents('php://__input'); > $fh = fopen('output.wav', 'w') or die("can't open file"); > fwrite($fh, $content); > fclose($fh); > ?> > > The difficult part is the line with > "file_get_contents('php://__input')", it contains the whole POST > content as binary. > > What I already did was this: > > procedure TActAudio.Post; > var > lWav: TMemoryStream; > begin > lWav := TMemoryStream.Create; > try > lWav.Write(HttpRequest.__Content[1], > HttpRequest.ContentLength); > lWav.Position:= 0; > lWav.SaveToFile('/tmp/output.__wav'); > finally > lWav.Free; > end; > end; > > But the output.wav result is corrupt, while the php one is correct. > > Any hint?. > > Regards, > > > Worked fine here: > > procedure TMyAction.Post; > begin > with TFileStream.Create('out.txt', fmCreate) do > try > Write(TheRequest.Content[1], TheRequest.ContentLength); > finally > Free; > end; > end; > > POST /cgi1.br <http://cgi1.br> HTTP/1.1 > > {"key1":"value1","key2":"value2"} > > out.txt: > {"key1":"value1","key2":"value2"} > > Or: > > procedure TMyAction.Post; > begin > with TMemoryStream.Create do > try > Write(TheRequest.Content[1], TheRequest.ContentLength); > SaveToFile('post.txt'); > finally > Free; > end; > end; > > P.S.: php://input is not available with enctype="multipart/form-data". > Yes, with plain text everything works ok, the problem raises when I try to receive binary data, in this case a WAV file, enctype: "audio/x-wav". The client (a Flash app running on the browser) is sending a POST message without params, just the binary data.

I made an example using fcl-web (without Brook) and the problem persists.

As I cannot attach files here, please download this:

http://tempsend.com/6088747E14/DBFE/wami.tar.gz

That's an example for anyone to test, please uncompress and copy the following files to your apache documents folder (/var/www/html on Ubuntu):

testwami.html
swfobject.js
wamirecorder.js

Also please download the file Wami.sfw from here:
https://wami-recorder.googlecode.com/hg/example/client/Wami.swf and copy to the same folder as the previous files. I cannot attach it here because of email size restrictions of the mailing list.

Also please compile the project cgiproject.lpi contained in wami.tar.gz, then copy the executable to your /cgi-bin/ directory.

Regards,

Reply by email

silvioprog
to Leonardo M. Ramé
"Leonardo M. Ram?" wrote
I made an example using fcl-web (without Brook) and the problem persists.

[...]

Strange. I made some changes to work here in my PC and it worked fine:

http://tempsend.com/63FEC90D7A

Listen my ugly "audio3.wav" file. :-P

Lazarus 1.2.4 r45510 FPC 2.6.4 i386-win32-win32/win64

My Apache:
https://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.22-win32-x86-openssl-0.9.8t.msi

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140823/ad217a93/attachment.html>

Reply by email

Leonardo M. Ramé
to silvioprog
silvioprog wrote 16 lines
On Sat, Aug 23, 2014 at 8:09 PM, "Leonardo M. Ram?" < <mailto:>> wrote: > I made an example using fcl-web (without Brook) and the problem persists. [...] Strange. I made some changes to work here in my PC and it worked fine: http://tempsend.com/63FEC90D7A Listen my ugly "audio3.wav" file. :-P Lazarus 1.2.4 r45510 FPC 2.6.4 i386-win32-win32/win64 My Apache: https://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.22-win32-x86-openssl-0.9.8t.msi

Thank you very much Silvio for testing.

My setup is XUbuntu 14.04 64bits, and Apache/2.4.7 (Ubuntu). Also I'm testing on Firefox 31 For Ubuntu with the latest version of Flash Plugin.

What confuses me is the same setup, but instead of the CGI, if I use PHP, the file is stored with the complete audio.

Leonardo M. Ram? http://leonardorame.blogspot.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140823/73fee072/attachment.html>

Reply by email

silvioprog
to Leonardo M. Ramé
"Leonardo M. Ram?" wrote 9 lines
[...] Thank you very much Silvio for testing. My setup is XUbuntu 14.04 64bits, and Apache/2.4.7 (Ubuntu). Also I'm testing on Firefox 31 For Ubuntu with the latest version of Flash Plugin. What confuses me is the same setup, but instead of the CGI, if I use PHP, the file is stored with the complete audio.

Worked on Ubuntu too, please try it online here:

*https://brookframework.org/leo/ <https://brookframework.org/leo/>*

I used "chmod 777 -R /var/www/html/leo/tmp/" to my CGI can write waves into this directory.

The modified source code (i'm using jQuery in this demo to less coding hehe):

http://tempsend.com/06FD41FF11

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140824/e07bb79d/attachment.html>

Reply by email

silvioprog
to silvioprog

Oops,

silvioprog wrote 25 lines
On Sat, Aug 23, 2014 at 9:07 PM, "Leonardo M. Ram?" wrote: > [...] > Thank you very much Silvio for testing. > > My setup is XUbuntu 14.04 64bits, and Apache/2.4.7 (Ubuntu). Also I'm > testing on Firefox 31 For Ubuntu with the latest version of Flash Plugin. > > What confuses me is the same setup, but instead of the CGI, if I use PHP, > the file is stored with the complete audio. > Worked on Ubuntu too, please try it online here: *https://brookframework.org/leo/ <https://brookframework.org/leo/>* I used "chmod 777 -R /var/www/html/leo/tmp/" to my CGI can write waves into this directory. The modified source code (i'm using jQuery in this demo to less coding hehe): http://tempsend.com/06FD41FF11

Please try online demo again. And in your code change this lines:

...
stopButton.on('click', function(e) {
e.preventDefault();
e.stopPropagation();
 $(this).prop('disabled', true);
startButton.prop('disabled', false);
Wami.stopRecording();
 setTimeout(function() {
downloadLink.removeClass('hidden');
}, 3000);
 });
...
Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140824/04919c27/attachment.html>

Reply by email

Leonardo M. Ramé
to silvioprog
silvioprog wrote 50 lines
Oops, On Sun, Aug 24, 2014 at 12:02 AM, silvioprog < <mailto:>> wrote: On Sat, Aug 23, 2014 at 9:07 PM, "Leonardo M. Ram?" < <mailto:>> wrote: [...] Thank you very much Silvio for testing. My setup is XUbuntu 14.04 64bits, and Apache/2.4.7 (Ubuntu). Also I'm testing on Firefox 31 For Ubuntu with the latest version of Flash Plugin. What confuses me is the same setup, but instead of the CGI, if I use PHP, the file is stored with the complete audio. Worked on Ubuntu too, please try it online here: _https://brookframework.org/leo/_ I used "chmod 777 -R /var/www/html/leo/tmp/" to my CGI can write waves into this directory. The modified source code (i'm using jQuery in this demo to less coding hehe): http://tempsend.com/06FD41FF11 Please try online demo again. And in your code change this lines: ... stopButton.on('click', function(e) { e.preventDefault(); e.stopPropagation(); $(this).prop('disabled', true); startButton.prop('disabled', false); Wami.stopRecording(); setTimeout(function() { downloadLink.removeClass('hidden'); }, 3000); }); ... -- Silvio Cl?cio My public projects - github.com/silvioprog <http://github.com/silvioprog>

Thanks, this works perfectly. I'm using the same browser, the same swf but your Ubuntu server and your CGI example. Can you send me your cgiproject1.bf (the compiled version) to check it in my PC?.

I deployed my example to an external Ubuntu 12.04 server, with the CGI compiled in my machine, and the result is a file of correct size, playable with mplayer but just about 1 second of audio then mute until the playing ends, the same result as my local config.

I'm starting to think there must be something wrong in my fpc version...

Reply by email

Leonardo M. Ramé
to Leonardo M. Ramé
"Leonardo M. Ram?" wrote 64 lines
On 24/08/14 00:08, silvioprog wrote: > Oops, > > On Sun, Aug 24, 2014 at 12:02 AM, silvioprog < > <mailto:>> wrote: > > On Sat, Aug 23, 2014 at 9:07 PM, "Leonardo M. Ram?" > < <mailto:>> wrote: > > [...] > Thank you very much Silvio for testing. > > My setup is XUbuntu 14.04 64bits, and Apache/2.4.7 (Ubuntu). > Also I'm testing on Firefox 31 For Ubuntu with the latest > version of Flash Plugin. > > What confuses me is the same setup, but instead of the CGI, if I > use PHP, the file is stored with the complete audio. > > > Worked on Ubuntu too, please try it online here: > > _https://brookframework.org/leo/_ > > I used "chmod 777 -R /var/www/html/leo/tmp/" to my CGI can write > waves into this directory. > > The modified source code (i'm using jQuery in this demo to less > coding hehe): > > http://tempsend.com/06FD41FF11 > > > Please try online demo again. And in your code change this lines: > > ... > stopButton.on('click', function(e) { > e.preventDefault(); > e.stopPropagation(); > $(this).prop('disabled', true); > startButton.prop('disabled', false); > Wami.stopRecording(); > setTimeout(function() { > downloadLink.removeClass('hidden'); > }, 3000); > }); > ... > -- > Silvio Cl?cio > My public projects - github.com/silvioprog <http://github.com/silvioprog> > Thanks, this works perfectly. I'm using the same browser, the same swf but your Ubuntu server and your CGI example. Can you send me your cgiproject1.bf (the compiled version) to check it in my PC?. I deployed my example to an external Ubuntu 12.04 server, with the CGI compiled in my machine, and the result is a file of correct size, playable with mplayer but just about 1 second of audio then mute until the playing ends, the same result as my local config. I'm starting to think there must be something wrong in my fpc version...

Confirmed!, I completely removed my fpc and Lazarus, then downloaded from svn and rebuilded everything and it works now.

Thanks you Silvio again for your help.

Reply by email

silvioprog
to Leonardo M. Ramé
"Leonardo M. Ram?" wrote 73 lines
On 24/08/14 07:31, "Leonardo M. Ram?" wrote: > On 24/08/14 00:08, silvioprog wrote: > >> Oops, >> >> On Sun, Aug 24, 2014 at 12:02 AM, silvioprog < >> <mailto:>> wrote: >> >> On Sat, Aug 23, 2014 at 9:07 PM, "Leonardo M. Ram?" >> < <mailto:>> wrote: >> >> [...] >> Thank you very much Silvio for testing. >> >> My setup is XUbuntu 14.04 64bits, and Apache/2.4.7 (Ubuntu). >> Also I'm testing on Firefox 31 For Ubuntu with the latest >> version of Flash Plugin. >> >> What confuses me is the same setup, but instead of the CGI, if I >> use PHP, the file is stored with the complete audio. >> >> >> Worked on Ubuntu too, please try it online here: >> >> _https://brookframework.org/leo/_ >> >> I used "chmod 777 -R /var/www/html/leo/tmp/" to my CGI can write >> waves into this directory. >> >> The modified source code (i'm using jQuery in this demo to less >> coding hehe): >> >> http://tempsend.com/06FD41FF11 >> >> >> Please try online demo again. And in your code change this lines: >> >> ... >> stopButton.on('click', function(e) { >> e.preventDefault(); >> e.stopPropagation(); >> $(this).prop('disabled', true); >> startButton.prop('disabled', false); >> Wami.stopRecording(); >> setTimeout(function() { >> downloadLink.removeClass('hidden'); >> }, 3000); >> }); >> ... >> -- >> Silvio Cl?cio >> My public projects - github.com/silvioprog <http://github.com/silvioprog >> > >> >> > Thanks, this works perfectly. I'm using the same browser, the same swf > but your Ubuntu server and your CGI example. Can you send me your > cgiproject1.bf (the compiled version) to check it in my PC?. > > I deployed my example to an external Ubuntu 12.04 server, with the CGI > compiled in my machine, and the result is a file of correct size, > playable with mplayer but just about 1 second of audio then mute until > the playing ends, the same result as my local config. > > I'm starting to think there must be something wrong in my fpc version... > > > Confirmed!, I completely removed my fpc and Lazarus, then downloaded from svn and rebuilded everything and it works now. Thanks you Silvio again for your help.

You're welcome! :-)

My environment: Lazarus 1.2.2 rUnknown FPC 2.6.4 x86_64-linux-gtk 2 (will update to Lazarus 1.2.4)

After some problems using unstable FPC, now I use only current stable versions.

My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140824/3bf817b7/attachment.html>

Reply by email