soapdispatch.cgi 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #! /usr/bin/perl
  2. ################################################################################
  3. #
  4. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #################################################################################
  18. use strict;
  19. use warnings;
  20. use Cwd qw(cwd);
  21. use Config::Simple qw();
  22. use SOAP::Transport::HTTP qw();
  23. my ($logfile, $libpath, $maxsleep);
  24. BEGIN
  25. {
  26. my $cfgfile = 'soapserver.ini';
  27. my $cfg = new Config::Simple();
  28. $cfg->read($cfgfile) or die("Could not read config file $cfgfile: $!");
  29. $logfile = $cfg->param('logfile') or die("No logfile in config file $cfgfile");
  30. $libpath = $cfg->param('libpath') or die("No libpath in config file $cfgfile");
  31. $maxsleep = $cfg->param('maxsleep') || 0;
  32. }
  33. use lib ($libpath);
  34. my $time = localtime();
  35. my $method = $ENV{REQUEST_METHOD} || 'NONE';
  36. unless($method eq 'POST')
  37. {
  38. print("Content-type: text/plain\n\n");
  39. print("Expected POST request, received $method\n");
  40. open(OUT, '>>', $logfile) or die("could not write to $logfile: $!");
  41. print(OUT "Bad request at $time: method was $method\n");
  42. close(OUT);
  43. exit(1);
  44. }
  45. open(OUT, '>>', $logfile) or die("Could not write to $logfile: $!");
  46. print(OUT "Request at $time\n");
  47. print(OUT "[$_]=[$ENV{$_}]\n") foreach (sort(keys(%ENV)));
  48. print(OUT "[current directory]=[", cwd(), "]\n");
  49. if($maxsleep)
  50. {
  51. my $sec = rand($maxsleep)+1;
  52. print(OUT "Sleeping for $sec seconds\n");
  53. sleep($sec);
  54. }
  55. print(OUT "Dispatching\n");
  56. my $soap = SOAP::Transport::HTTP::CGI->dispatch_to('TestSOAP::TestService');
  57. $soap->on_dispatch(\&my_on_dispatch);
  58. eval { $soap->handle(); };
  59. if($@)
  60. {
  61. print(OUT "Transport error: $@\n\n");
  62. }
  63. else
  64. {
  65. my $resp = $soap->response();
  66. print(OUT "Response status: ", $resp->status_line(), "\n");
  67. print(OUT "Response content: ", $resp->decoded_content(), "\n");
  68. print(OUT "OK\n\n");
  69. }
  70. close(OUT);
  71. sub my_on_dispatch($)
  72. {
  73. my ($request) = @_;
  74. $request->match((ref($request))->method());
  75. my ($method_uri, $method_name) = ($request->namespaceuriof() || '', $request->dataof()->name());
  76. $method_name =~ s/Request$//;
  77. return ($method_uri, $method_name);
  78. }