4 # a minimal webserver for testing the scep CGI script
6 # Written by Alexander Klink for the OpenXPKI project
7 # Copyright (c) 2006 by The OpenXPKI project
18 # If there is an argument, use it as the portnumber.
19 # If there is none, the default port is 8087
20 my $port = shift || 8087;
22 my $daemon = HTTP
::Daemon
->new(
23 LocalAddr
=> '127.0.0.1',
28 while (my $conn = $daemon->accept()) {
29 while (my $req = $conn->get_request()) {
31 # if GET, the data to post to the CGI script is just the URI
32 if ($req->method() eq 'GET') {
33 $post_data = $req->uri();
35 # if POST, the data is the content of the request
36 if ($req->method() eq 'POST') {
37 $post_data = $req->content();
40 # create a new temp file for the output of the scep CGI binary
41 my $tmp_file = File
::Temp
->new(
42 TEMPLATE
=> 'cgioutXXXXX',
45 my $tmp_filename = $tmp_file->filename();
46 #$tmp_filename = '/tmp/foo';
48 # call the CGI script and pass it the options using STDIN
49 # (CGI qw( -debug ) in scep)
50 open my $CGI_HANDLE, "| REMOTE_ADDR=127.0.0.1 ./scep > $tmp_filename 2>/dev/null";
51 print $CGI_HANDLE $post_data;
54 $conn->send_error( 501, 'scep CGI problem' );
57 # pass on the response of the CGI script to the HTTP client
58 my $response = OpenXPKI
->read_file($tmp_filename);
60 $conn->send_error( 502, 'scep response file empty' );
62 $conn->send_response($response);