RT notifier: parse templates without header correctly
[openxpki.git] / trunk / clients / perl / OpenXPKI-Client-SCEP / t / http_server.pl
blobcacb16121456d56d3197156fbaf990415307559e
1 #!/usr/bin/perl
2 # http_server.pl
3 #
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
9 use strict;
10 use warnings;
11 use English;
13 use HTTP::Daemon;
14 use CGI;
15 use File::Temp;
16 use OpenXPKI;
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',
24 LocalPort => $port,
25 ReuseAddr => 1,
28 while (my $conn = $daemon->accept()) {
29 while (my $req = $conn->get_request()) {
30 my $post_data;
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',
43 DIR => '/tmp',
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;
52 close $CGI_HANDLE;
53 if ($CHILD_ERROR) {
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);
59 if (! $response) {
60 $conn->send_error( 502, 'scep response file empty' );
62 $conn->send_response($response);
64 $conn->close();
65 undef($conn);