I want to write a test cgi program in C++. But when I set the configuration file as follow:
ScriptAlias /cgi-bin/ "F:/workbench/cgi-bin/"
<Directory "F:/workbench/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>
AddHandler cgi-script .exe .pl .cgi
then write a helloworld program as follows:
#include <stdio.h>
int main(void)
{
printf("\n");
printf("Hello, World!\n");
return 0;
}
and compile it with g++ hello.cpp -o hello.cgi
when I restart the server and visit the cgi: localhost\cgi-bin\hello.cgi
it didn't work.
You need to add an HTTP header to describe the output. The Http Header and Http body are separated by two new-line chars. I think your code will work if you include a simple header like "content-type: text/plain". In other words, try with:
#include <stdio.h>
int main(void) {
printf("Content-type: text/plain\n");
printf("\n");
printf("Hello, World!\n");
return 0;
}
Related
So I am working on trying to make my own apache module. Right now I trying to get it to return a 403 just to test it out, but it seems that apache just ignores the module entirely and returns the default page. Here is the relevant parts of my code:
static int request_hook(request_rec* r){
return HTTP_FORBIDDEN;
}
/* ********************************************
Register module to Apache
******************************************** */
static void register_hooks(apr_pool_t *p)
{
// We want to hook first so we can issue a deny ASAP if needed
ap_hook_log_transaction( request_hook, NULL, NULL, APR_HOOK_REALLY_FIRST);
}
module AP_MODULE_DECLARE_DATA my_module = {
STANDARD20_MODULE_STUFF,
NULL, /* dir config creater */
NULL, /* dir merger --- default is to override */
NULL, /* server config */
NULL, /* merge server configs */
NULL, /* command apr_table_t */
register_hooks /* register hooks */
};
And my apache configuration file looks like this:
<VirtualHost *:80>
DocumentRoot /var/www/html
SetHandler my_module
</VirtualHost>
It was compiled by doing
sudo apxs -i -a -c my_module.c && sudo service apache2 restart
Fixed. The problem was the function:
ap_hook_log_transaction( request_hook, NULL, NULL, APR_HOOK_REALLY_FIRST);
should have been:
ap_hook_handler( request_hook, NULL, NULL, APR_HOOK_REALLY_FIRST);
I am trying to export an SVG graph as an attachment to download. I am using http://d3export.housegordon.org/ to achieve the same. The perl file returns a status message of 200OK. Neither the apache log nor the perl log is showing any errors. Also, I am able to print the output SVG string of the perl file in my console which I assume is indicative of the request to perl file being successful. However, instead of getting an SVG attachment file to download as response, an extra tab gets opened displaying an APACHE ERROR which reads..."Not Found
The requested URL /[object XMLDocument] was not found on this server.". I have made a few modifications in my perl script wrt the perl script provided on the link mentioned above. My modified perl script is as below:
use strict;
use warnings;
use CGI qw/:standard/;
use CGI::Carp qw/fatalsToBrowser/;
use autodie qw(:all);
use File::Temp qw/tempfile/;
use File::Slurp qw/read_file write_file/;
$CGI::POST_MAX = 1024 * 5000;
my $output_format = param("output_format")
or die "Missing 'output_format' parameter";
die "Invalid output_format value"
unless $output_format eq "svg" ||
$output_format eq "pdf" ||
$output_format eq "png";
my $data = param("data")
or die "Missing 'data' parameter";
die "Invalid data value"
unless $data =~ /^[\x20-\x7E\t\n\r ]+$/;
my $timestamp = time;
my $random = int(rand(99999));
my $dnldfile = "/dnld/d3Momentum" . $timestamp . "_" . $random . ".svg";
my $filename = "/var/www/html/Project/Project_frontend/public".$dnldfile;
my $q = CGI->new;
if ($output_format eq "svg") {
print $q->header(-type=>"image/svg+xml", -attachment=>$filename,);
print $data;
exit(0);
}
elsif ($output_format eq "pdf" || $output_format eq "png") {
my (undef, $input_file) = tempfile("d3export.svg.XXXXXXX", OPEN=>0, TMPDIR=>1, UNLINK=>1);
my (undef, $output_file) = tempfile("d3export.out.XXXXXXX", OPEN=>0, TMPDIR=>1, UNLINK=>1);
write_file( $input_file, $data );
my $zoom = ($output_format eq "png")?10:1;
system("rsvg-convert -o '$output_file' -z '$zoom' -f '$output_format' '$input_file'");
my $pdf_data = read_file( $output_file, {binmode=>':raw'});
my $mime_type = ($output_format eq "pdf")?"application/x-pdf":"image/png";
print header(-type=>$mime_type,
-attachment=>"d3js_export_demo.$output_format");
print $pdf_data;
exit(0);
}
Request Parameters being passed are..
'ouput_format' : 'svg';
'data' : (Entire SVG Element parsed using XMLSerializer.serializeToString() as mentioned on http://d3export.housegordon.org/).
Below is my Virtual Host Configuration:
<VirtualHost *:80>
ServerName project-v4.co
ServerAlias project-v4.co
Alias /awstatsclasses "/usr/share/awstats/lib/"
Alias /awstats-icon "/usr/share/awstats/icon/"
Alias /awstatscss "/usr/share/doc/awstats/examples/css"
ScriptAlias /awstats/ /usr/lib/cgi-bin/
DocumentRoot /var/www/html/Project-v4/Project-v4_frontend/public
SetEnv APPLICATION_ENV "production"
<Directory /var/www/html/Project-v4/Project-v4_frontend/public>
DirectoryIndex index.html
AllowOverride All
Order allow,deny
Allow from all
AddHandler cgi-script .bin
Header set Access-Control-Allow-Origin "*"
</Directory>
ScriptAlias /cgi-bin/ /var/www/html/Project-v4/Project-v4_frontend/public/cgi-bin/
<Directory "/var/www/html/Project-v4/Project-v4_frontend/public/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#ErrorLog ${APACHE_LOG_DIR}/live-error.log
ErrorLog /var/www/live-error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
#<Location /perl/>
# SetHandler perl-script
# PerlHandler ModPerl::PerlRun
# Options ExecCGI
#</Location>
Below is the Ajax Call to the perl script.
var oData = {"output_format" : "svg", "data" : svg_xml};
$.ajax({
url: '/cgi-bin/d3export.pl',
method: "POST",
data: oData,
success: function(oResult) {
console.log(oResult);
window.open(oResult);
}
})
The Perl script is located in the cgi-bin directory, which is located in the public folder of my Project (Project-v4_frontend/public/cgi-bin/d3export.pl). I am using the MEAN stack for my project.
Any help to resolve the apache NOT FOUND error would be appreciated sincerely.
Thank You.
I am trying to write a sample Apache module to read config file whose file path is specified in httpd.conf like that:
<Location ~ /(?!.*\.(png|jpeg|jpg|gif|bmp|tif)$)>
SetInputFilter SAMPLE_INPUT_FILTER
SetOutputFilter SAMPLE_OUTPUT_FILTER
ConfigFilePath "/etc/httpd/conf.d/sample/sample.config"
</Location>
At command record structure, I do:
static const command_rec config_check_cmds[] =
{
AP_INIT_TAKE1( "ConfigFilePath", read_config_file, NULL, OR_ALL, "sample config"),
{ NULL }
};
I also set:
module AP_MODULE_DECLARE_DATA SAMPLE_module = {
STANDARD20_MODULE_STUFF,
create_dir_config, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
config_check_cmds, /* table of config file commands */
sample_register_hooks /* register hooks */
};
I could read config file path successfully. And now I want to check that if "ConfigFilePath" is not specified in httpd.conf, It will show error at console when I use "service httpd restart"
How could I do that?
You'd register a ap_hook_post_config hook and do the verification of required settings there. Be aware that that hook is called twice as documented in the answer here: Init modules in apache2
See an example post config hook implementation here: http://svn.apache.org/repos/asf/httpd/httpd/tags/2.3.0/modules/examples/mod_example_hooks.c
i am running wamp on windows 7
i am trying to upload some images via a form into my MVC applIcation. i am working from my laptop, so the Wamp is installed on my laptop
my problem is that i keep getting this message:
Warning: move_uploaded_file(C:\Users\test\zend\\module\guest\src\guest/pics/holdover/pic.jpg): failed to open stream: Permission denied in
my problem is that i have not restricted any previged so i dont knwo why it would be restricted.
not-with-standing this, where do i go on my WAMP to enable access to the folder ?
thank you in advanced for your advise
my Code:
the aim of the file_upload is to transfer the file (currently held in a temp folder) to another folder. its also given a new name.
everything else work. the problem is with the permission of the receiving folder; permission is being denied
if ($form->isValid())
{
$size = new Size(array('min'=>2000)); //minimum bytes filesize
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->setValidators(array($size), $data['fileupload']['name']);
if (!$adapter->isValid())
{
$dataError = $adapter->getMessages();
$error = array();
foreach($dataError as $key=>$row)
{
$error[] = $row;
}
$form->setMessages(array('fileupload'=>$error ));
}
else
{
$fileName = $data['fileupload']['name'];
$fileTmpLoc = $data['fileupload']['tmp_name'];
$fileType = $data['fileupload']['type'];
$fileSize = $data['fileupload']['size'];
$fileErrorMsg = $data['fileupload']['error'];
$kaboom = explode(".", $fileName);
$this->fileExt = end($kaboom);
$this->fileName = "user_{$this->getAbbriviation($data)}{$this->getUserId()}.$this->fileExt";
$moveResult = move_uploaded_file($fileTmpLoc, dirname(__DIR__)."/pics/member/holdover/$this->fileName");
if ($moveResult != true)
{
echo "ERROR: File not uploaded. Try again.";
unlink($this->fileTmpLoc);
exit();
}
$this->processAndUploadPhotos($data);
// var_dump($moveResult); die();
$adapter->setDestination(dirname(__DIR__).'/testImage');
if ($adapter->receive($data['fileupload']['name'])) {
$profile->exchangeArray($form->getData());
echo 'Profile Name '.$profile->profilename.' upload ';
}
}
}
}
And the important bit
<VirtualHost *:80>
ServerName Zend
DocumentRoot "C:\Users\zend\testingZend2\public"
SetEnv APPLICATION_ENV "development"
<Directory "C:\Users\zend\testingZend2\public">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
You can set the folder permissions using the chmod command try this command in you php script file.
if( chmod($path, 0777) ) {
move_uploaded_file($path)
}
else
echo "Couldn't do it.";
The problem is that your Virtual Host definition is giving access to "C:\Users\zend\testingZend2\public" but you are trying to store the images in a totally different folder C:\Users\test\zend\\module\guest\src\guest/pics/holdover/pic.jpg.
Not withstanding the double \\ between zend and module, you will also have to give Apache access to this other folder structure.
So you need to add another <Directory.. definition so Apache knows this site has access to the other folder structur as well as the folders it is running from.
<VirtualHost *:80>
ServerName Zend
DocumentRoot "C:\Users\zend\testingZend2\public"
SetEnv APPLICATION_ENV "development"
<Directory "C:\Users\zend\testingZend2\public">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "C:/Users/test/zend/module/guest/src/guest/pics/holdover">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Its normal to do this with an alias like so
<VirtualHost *:80>
ServerName Zend
DocumentRoot "C:\Users\zend\testingZend2\public"
SetEnv APPLICATION_ENV "development"
<Directory "C:\Users\zend\testingZend2\public">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Alias /pictures "C:/Users/test/zend/module/guest/src/guest/pics/holdover"
<Directory "C:/Users/test/zend/module/guest/src/guest/pics/holdover">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
This allows you to use the alias i.e. pictures in your code rather than the full directory name.
Its also a good idea to stick to the Unix directory seperator in any files/directory information as PHP will do any necessary conversions to the DOS seperator automatically if it is running on DOS.
I have created a hello FastCGI prog in C
#include <fcgi_stdio.h>
#include <stdlib.h>
int count;
void initialize(void)
{
count=0;
}
int main(void)
{
initialize();
while (FCGI_Accept() >= 0)
{
printf("Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello! (C, fcgi_stdio library)</title>"
"<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>"
"Request number %d running on host <i>%s</i>\n",
++count, getenv("REMOTE_HOST"));
}
return 1;
}
Then I compiled the program using "gcc -o hello1 hello1.c -lfcgi"
This created "hello1" executable file in my home directory (in ubuntu)
When I ran this file, I got output as:
Content-type: text/html
<title>FastCGI Hello! (C, fcgi_stdio library)</title><h1>FastCGI Hello! (C, fcgi_stdio library)</h1>Request number 1 running on host <i>(null)</i>
I want to run this file from firefox. Since I am new to this, I dont have any idea about it. Can any one, provide me with detailed ans, what all steps I need to follow to run it through web browser.
I tried typing the URL as "http://localhost/fcgi-bin/hello1" after copying the 'hello1" file to /etc/apache/fcgi-bin/hello1.fcgi but it gave 404 error
You'd still need to include the .fcgi extension on the url:
http://localhost/fcgi-bin/hello1.fcgi