Results 1 to 4 of 4
Hi,
I have a download button in my cgi c script which when clicked opens another page by calling its cgi script.
printf("<button type = \"button\" name = \"Download\" onClick ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 09-11-2012 #1Just Joined!
- Join Date
- Jun 2012
- Posts
- 9
cgi c script - download a text file
Hi,
I have a download button in my cgi c script which when clicked opens another page by calling its cgi script.
printf("<button type = \"button\" name = \"Download\" onClick = window.open(\"/path/to/cgi/script\")>Download</button>");
I am able to open cgi scripts. I am able to download cgi scripts in server with the following html headers.
printf("Content-Type: application/force-download\r\n\r\n");
printf("Content-Disposition: attachment; filename=file.ext\r\n\r\n");
I am not able to download text files in my server in the same way. I get a not compliant error. how do i download a text file? I am using boa server. Where do i place my text file in the server?
- 09-15-2012 #2Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,746
Hi,
Here's a way to do that in Perl. I've tested it w/boa 0.94 on Fedora 17.
The HTML itself might not be exactly what you want, but you should be able to adapt it to your needs.Code:#!/usr/bin/perl use strict; use warnings; use CGI; # get CGI parameters passed in query string my %qs; my $cgi = new CGI; for my $key($cgi->param()){ $qs{$key} = $cgi->param($key); } my $file = defined($qs{'file'}) ? $qs{'file'} : ''; if($file){ print <<"EOF"; Content-type: octet/stream Content-disposition: attachment; filename="$file" EOF }else{ print <<"EOF"; Content-type: text/html <button type="button" name="Download" onClick=window.open("?file=/foo/test.txt")>Download</button> EOF } exit(0);
- 09-16-2012 #3Just Joined!
- Join Date
- Aug 2011
- Posts
- 18
hi, I was just reading this thread and I was astounded by the simplicity of CGI reporting back to a web page what ever it printed to stdout! I'm a rubyist and a rails developer and I never realized that's all CGI was.
I found it difficult finding an installation tut for boa, but I did an apt install boa just now. If anyone else is who's feeling intrigued, but not quite familiar, here's my procedure/notes which might make this thread more fun for you:
Create a file to be webserved, which links to a cgi file for dynamic content:Code:# install boa $ apt install boa # read the first actually well written man file I've ever seen $ man boa # look over single configuration file $ vi /etc/boa.conf
(/var/www/index.html)
Code:<html> <body> <h1>HELLO</h1> <a href='#' onclick="window.open('/cgi-bin/cgi.pl')">clicky</a> </body> </html>
Create the cgi file:
(/usr/lib/cgi-bin/cgi.pl)
Make it so anyone can execute that cgi script or you'll get errors.Code:#!/usr/bin/perl use strict; use warnings; use CGI; # get CGI parameters passed in query string my %qs; my $cgi = new CGI; for my $key($cgi->param()){ $qs{$key} = $cgi->param($key); } my $file = defined($qs{'file'}) ? $qs{'file'} : ''; if($file){ print <<"EOF"; Content-type: octet/stream Content-disposition: attachment; filename="$file" EOF }else{ print <<"EOF"; Content-type: text/html <p>This came from printf!!! All this time I thought that was just for CLI apps and debugging pre-alpha code!</p> <button type="button" name="Download" onClick=window.open("?file=/foo/test.txt")>Download</button> EOF } exit(0);
Code:$ sudo chmod 0777 /usr/lib/cgi-bin/cgi.pl
I don't understand any of that hardcore perl stuff that's going on, but the else statement could reduce to the c program...
(/usr/lib/cgi-bin/cgi.c)
Code:/* Hello World program */ #include<stdio.h> main() { printf("Content-type: text/html\n"); printf("<p>This came from printf!!! All this time I thought that was just for boring CLI apps and debugging pre-alpha code!</p>\n"); printf("<button type=\"button\" name=\"Download\" onClick=window.open(\"?file=/foo/test.txt\")>Download</button>"); }
I still don't fully grasp this question
but hopefully that's been resolved by Atreyu already because things seem to berocking for me with my rig.I am not able to download text files in my server in the same way. I get a not compliant error. how do i download a text file? I am using boa server. Where do i place my text file in the server?
Ironically, for maybe a half hour or so actually, I was struggling to use a ruby script as a CGI, and was getting the error message the OP was getting:
here's my ruby code, which I did remember to chmod to 0777502 Bad Gateway
The CGI was not CGI/1.1 compliant.
and it turned out the user that was in charge of running boa (www-data) didn't have rvm because I installed it to a local non-root user. The fix was actually to use LinuxMint's gui Software Manager to install ruby 1.9 which somehow didn't completely mess everything up (although sloppy stuff like this is going to get me in trouble down the road when strange things start happening).Code:#!/usr/bin/env ruby puts "Content-type: text/html" puts puts "This came from puts!"
Anyway, after that, everything worked out great! Cool thread guys!
- 09-16-2012 #4Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,746
heh, do u mean this?
That is just grabbing the form variables passed to the CGI script (via ?file=test.txt, etc. in the URL) and turning them into variables usable by perl. in this case, it is passing the name of the file that is the downloadable/clicked file.Code:# get CGI parameters passed in query string my %qs; my $cgi = new CGI; for my $key($cgi->param()){ $qs{$key} = $cgi->param($key); }


Reply With Quote

