Reputation: 14705
I've written a script that reads the contents of a https page by sending a POST request, and dumps it to a text file. It works fine under Linux, and works in my Windows machine too after installing Shining Light OpenSSL. But the client says they cannot install any exe on their Windows machine, and is asking for an alternate solution.
Do we have any alternative here? Perhaps some proxy that takes a POST request, gets the https content and delivers it as http? A lot of wishful thinking, I know, but I just want to explore any possible options for me here.
Upvotes: 2
Views: 509
Reputation: 118138
Use Win32::IE::Mechanize. These days, all Windows systems have some version of Internet Explorer (although, on some systems, administrators may block functionality).
You could also use Win32::OLE to drive IE 'manually', so to speak ;-)
The following code "works" on my ancient Windows XP SP3 system. I am not sure if it would work with Vista or Windows 7 UAC. showcgi.pl
simply dumps the state of the $cgi
object. You can see the output below.
I explain what's going in more detail in my blog post.
I don't have an SSL server set up for testing, but please let me know if the script works after these modifications.
#!/usr/bin/env perl
package My::Poster;
use strict; use warnings;
use Const::Fast;
use Try::Tiny;
use Win32::OLE;
use Win32::OLE::Variant;
local $Win32::OLE::Warn = 3;
# http://msdn.microsoft.com/en-us/library/aa768360%28v=vs.85%29.aspx
const my %BrowserNavConstants => (
navOpenInNewWindow => 0x1,
navNoHistory => 0x2,
navNoReadFromCache => 0x4,
navNoWriteToCache => 0x8,
navAllowAutosearch => 0x10,
navBrowserBar => 0x20,
navHyperlink => 0x40,
navEnforceRestricted => 0x80,
navNewWindowsManaged => 0x0100,
navUntrustedForDownload => 0x0200,
navTrustedForActiveX => 0x0400,
navOpenInNewTab => 0x0800,
navOpenInBackgroundTab => 0x1000,
navKeepWordWheelText => 0x2000,
navVirtualTab => 0x4000,
navBlockRedirectsXDomain => 0x8000,
navOpenNewForegroundTab => 0x10000,
);
sub new {
my $class = shift;
my $self = bless {} => $class;
$self->init;
return $self;
}
sub ie {
my $self = shift;
my $ie = shift;
return $self->{ie} unless defined $ie;
$self->{ie} = $ie;
return;
}
sub init {
my $self = shift;
$self->ie(
Win32::OLE->new(
'InternetExplorer.Application',
sub {
my $ie = shift;
try { $ie->Quit if $ie } catch { warn "$_\n" };
},
)
);
return;
}
sub post {
my $self = shift;
my ($url, $data) = @_;
my $ie = $self->ie;
my $flags = $BrowserNavConstants{navNoHistory} |
$BrowserNavConstants{navNoReadFromCache} |
$BrowserNavConstants{navNoWriteToCache} |
$BrowserNavConstants{navEnforceRestricted} |
$BrowserNavConstants{navNewWindowsManaged} |
$BrowserNavConstants{navUntrustedForDownload} |
$BrowserNavConstants{navBlockRedirectsXDomain}
;
# $postdata should also be converted to bytes here
# I don't remember the right way to do it, and it works for ASCII
# so it should be good enough for now.
my $postdata = join '&', map join('=', @$_), @$data;
my $vPostData = Variant(VT_ARRAY|VT_UI1, length $postdata);
$vPostData->Put($postdata);
# http://msdn.microsoft.com/en-us/library/aa752133%28v=vs.85%29.aspx
$ie->Navigate(
$url,
$flags,
'_self',
$vPostData,
"Content-Type: application/x-www-form-urlencoded\015\012",
);
while ($ie->{ReadyState} != 4) {
sleep 1;
}
return $ie->Document->documentElement->innerHTML;
}
sub DESTROY {
my $self = shift;
try { $self->ie->Quit } catch { warn "$_\n" };
return;
}
package main;
use strict; use warnings;
my $poster = My::Poster->new;
my $html = $poster->post(
'http://test.localdomain:8080/cgi-bin/showcgi.pl',
[
[ thisvar => 'thatvalue', ],
[ anothervar => 'anothervalue', ],
[ oh => 'lala', ],
],
);
print $html if defined $html;
Output:
$VAR1 = bless( { '.parameters' => [ 'thisvar', 'anothervar', 'oh' ], '.crlf' => ' ', '.globals' => { 'DEBUG' => 0, 'NO_UNDEF_PARAMS' => 0, 'NO_NULL' => 1, 'FATAL' => -1, 'USE_PARAM_SEMICOLONS' => 0, 'PARAM_UTF8' => 0, 'DISABLE_UPLOADS' => 1, 'USE_CGI_PM_DEFAULTS' => 0, 'NPH' => 0, 'POST_MAX' => 102400, 'HEADERS_ONCE' => 0 }, 'anothervar' => [ 'anothervalue' ], 'oh' => [ 'lala' ], 'thisvar' => [ 'thatvalue' ], '.fieldnames' => { 'anothervar' => 1, 'oh' => 1, 'thisvar' => 1 }, '.header_printed' => 1 }, 'CGI::Simple' );
Upvotes: 2