Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3003 lines
83 KiB

  1. #######################################################################
  2. #
  3. # Win32::Internet - Perl Module for Internet Extensions
  4. # ^^^^^^^^^^^^^^^
  5. # This module creates an object oriented interface to the Win32
  6. # Internet Functions (WININET.DLL).
  7. #
  8. # Version: 0.08 (14 Feb 1997)
  9. # Version: 0.081 (25 Sep 1999)
  10. # Version: 0.082 (04 Sep 2001)
  11. #
  12. #######################################################################
  13. # changes:
  14. # - fixed 2 bugs in Option(s) related subs
  15. # - works with build 30x also
  16. package Win32::Internet;
  17. require Exporter; # to export the constants to the main:: space
  18. require DynaLoader; # to dynuhlode the module.
  19. # use Win32::WinError; # for windows constants.
  20. @ISA= qw( Exporter DynaLoader );
  21. @EXPORT = qw(
  22. HTTP_ADDREQ_FLAG_ADD
  23. HTTP_ADDREQ_FLAG_REPLACE
  24. HTTP_QUERY_ALLOW
  25. HTTP_QUERY_CONTENT_DESCRIPTION
  26. HTTP_QUERY_CONTENT_ID
  27. HTTP_QUERY_CONTENT_LENGTH
  28. HTTP_QUERY_CONTENT_TRANSFER_ENCODING
  29. HTTP_QUERY_CONTENT_TYPE
  30. HTTP_QUERY_COST
  31. HTTP_QUERY_CUSTOM
  32. HTTP_QUERY_DATE
  33. HTTP_QUERY_DERIVED_FROM
  34. HTTP_QUERY_EXPIRES
  35. HTTP_QUERY_FLAG_REQUEST_HEADERS
  36. HTTP_QUERY_FLAG_SYSTEMTIME
  37. HTTP_QUERY_LANGUAGE
  38. HTTP_QUERY_LAST_MODIFIED
  39. HTTP_QUERY_MESSAGE_ID
  40. HTTP_QUERY_MIME_VERSION
  41. HTTP_QUERY_PRAGMA
  42. HTTP_QUERY_PUBLIC
  43. HTTP_QUERY_RAW_HEADERS
  44. HTTP_QUERY_RAW_HEADERS_CRLF
  45. HTTP_QUERY_REQUEST_METHOD
  46. HTTP_QUERY_SERVER
  47. HTTP_QUERY_STATUS_CODE
  48. HTTP_QUERY_STATUS_TEXT
  49. HTTP_QUERY_URI
  50. HTTP_QUERY_USER_AGENT
  51. HTTP_QUERY_VERSION
  52. HTTP_QUERY_WWW_LINK
  53. ICU_BROWSER_MODE
  54. ICU_DECODE
  55. ICU_ENCODE_SPACES_ONLY
  56. ICU_ESCAPE
  57. ICU_NO_ENCODE
  58. ICU_NO_META
  59. ICU_USERNAME
  60. INTERNET_FLAG_PASSIVE
  61. INTERNET_FLAG_ASYNC
  62. INTERNET_HYPERLINK
  63. INTERNET_FLAG_KEEP_CONNECTION
  64. INTERNET_FLAG_MAKE_PERSISTENT
  65. INTERNET_FLAG_NO_AUTH
  66. INTERNET_FLAG_NO_AUTO_REDIRECT
  67. INTERNET_FLAG_NO_CACHE_WRITE
  68. INTERNET_FLAG_NO_COOKIES
  69. INTERNET_FLAG_READ_PREFETCH
  70. INTERNET_FLAG_RELOAD
  71. INTERNET_FLAG_RESYNCHRONIZE
  72. INTERNET_FLAG_TRANSFER_ASCII
  73. INTERNET_FLAG_TRANSFER_BINARY
  74. INTERNET_INVALID_PORT_NUMBER
  75. INTERNET_INVALID_STATUS_CALLBACK
  76. INTERNET_OPEN_TYPE_DIRECT
  77. INTERNET_OPEN_TYPE_PROXY
  78. INTERNET_OPEN_TYPE_PROXY_PRECONFIG
  79. INTERNET_OPTION_CONNECT_BACKOFF
  80. INTERNET_OPTION_CONNECT_RETRIES
  81. INTERNET_OPTION_CONNECT_TIMEOUT
  82. INTERNET_OPTION_CONTROL_SEND_TIMEOUT
  83. INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT
  84. INTERNET_OPTION_DATA_SEND_TIMEOUT
  85. INTERNET_OPTION_DATA_RECEIVE_TIMEOUT
  86. INTERNET_OPTION_HANDLE_SIZE
  87. INTERNET_OPTION_LISTEN_TIMEOUT
  88. INTERNET_OPTION_PASSWORD
  89. INTERNET_OPTION_READ_BUFFER_SIZE
  90. INTERNET_OPTION_USER_AGENT
  91. INTERNET_OPTION_USERNAME
  92. INTERNET_OPTION_VERSION
  93. INTERNET_OPTION_WRITE_BUFFER_SIZE
  94. INTERNET_SERVICE_FTP
  95. INTERNET_SERVICE_GOPHER
  96. INTERNET_SERVICE_HTTP
  97. INTERNET_STATUS_CLOSING_CONNECTION
  98. INTERNET_STATUS_CONNECTED_TO_SERVER
  99. INTERNET_STATUS_CONNECTING_TO_SERVER
  100. INTERNET_STATUS_CONNECTION_CLOSED
  101. INTERNET_STATUS_HANDLE_CLOSING
  102. INTERNET_STATUS_HANDLE_CREATED
  103. INTERNET_STATUS_NAME_RESOLVED
  104. INTERNET_STATUS_RECEIVING_RESPONSE
  105. INTERNET_STATUS_REDIRECT
  106. INTERNET_STATUS_REQUEST_COMPLETE
  107. INTERNET_STATUS_REQUEST_SENT
  108. INTERNET_STATUS_RESOLVING_NAME
  109. INTERNET_STATUS_RESPONSE_RECEIVED
  110. INTERNET_STATUS_SENDING_REQUEST
  111. );
  112. #######################################################################
  113. # This AUTOLOAD is used to 'autoload' constants from the constant()
  114. # XS function. If a constant is not found then control is passed
  115. # to the AUTOLOAD in AutoLoader.
  116. #
  117. sub AUTOLOAD {
  118. my($constname);
  119. ($constname = $AUTOLOAD) =~ s/.*:://;
  120. #reset $! to zero to reset any current errors.
  121. $!=0;
  122. my $val = constant($constname, @_ ? $_[0] : 0);
  123. if ($! != 0) {
  124. # [dada] This results in an ugly Autoloader error
  125. #if ($! =~ /Invalid/) {
  126. # $AutoLoader::AUTOLOAD = $AUTOLOAD;
  127. # goto &AutoLoader::AUTOLOAD;
  128. #} else {
  129. # [dada] ... I prefer this one :)
  130. ($pack,$file,$line) = caller; undef $pack;
  131. die "Win32::Internet::$constname is not defined, used at $file line $line.";
  132. #}
  133. }
  134. eval "sub $AUTOLOAD { $val }";
  135. goto &$AUTOLOAD;
  136. }
  137. #######################################################################
  138. # STATIC OBJECT PROPERTIES
  139. #
  140. $VERSION = "0.082";
  141. %callback_code = ();
  142. %callback_info = ();
  143. #######################################################################
  144. # PUBLIC METHODS
  145. #
  146. #======== ### CLASS CONSTRUCTOR
  147. sub new {
  148. #========
  149. my($class, $useragent, $opentype, $proxy, $proxybypass, $flags) = @_;
  150. my $self = {};
  151. if(ref($useragent) and ref($useragent) eq "HASH") {
  152. $opentype = $useragent->{'opentype'};
  153. $proxy = $useragent->{'proxy'};
  154. $proxybypass = $useragent->{'proxybypass'};
  155. $flags = $useragent->{'flags'};
  156. my $myuseragent = $useragent->{'useragent'};
  157. undef $useragent;
  158. $useragent = $myuseragent;
  159. }
  160. $useragent = "Perl-Win32::Internet/".$VERSION unless defined($useragent);
  161. $opentype = constant("INTERNET_OPEN_TYPE_DIRECT",0) unless defined($opentype);
  162. $proxy = "" unless defined($proxy);
  163. $proxybypass = "" unless defined($proxybypass);
  164. $flags = 0 unless defined($flags);
  165. my $handle = InternetOpen($useragent, $opentype, $proxy, $proxybypass, $flags);
  166. if ($handle) {
  167. $self->{'connections'} = 0;
  168. $self->{'pasv'} = 0;
  169. $self->{'handle'} = $handle;
  170. $self->{'useragent'} = $useragent;
  171. $self->{'proxy'} = $proxy;
  172. $self->{'proxybypass'} = $proxybypass;
  173. $self->{'flags'} = $flags;
  174. $self->{'Type'} = "Internet";
  175. # [dada] I think it's better to call SetStatusCallback explicitly...
  176. #if($flags & constant("INTERNET_FLAG_ASYNC",0)) {
  177. # my $callbackresult=InternetSetStatusCallback($handle);
  178. # if($callbackresult==&constant("INTERNET_INVALID_STATUS_CALLBACK",0)) {
  179. # $self->{'Error'} = -2;
  180. # }
  181. #}
  182. bless $self;
  183. } else {
  184. $self->{'handle'} = undef;
  185. bless $self;
  186. }
  187. $self;
  188. }
  189. #============
  190. sub OpenURL {
  191. #============
  192. my($self,$new,$URL) = @_;
  193. return undef unless ref($self);
  194. my $newhandle=InternetOpenUrl($self->{'handle'},$URL,"",0,0,0);
  195. if(!$newhandle) {
  196. $self->{'Error'} = "Cannot open URL.";
  197. return undef;
  198. } else {
  199. $self->{'connections'}++;
  200. $_[1] = _new($newhandle);
  201. $_[1]->{'Type'} = "URL";
  202. $_[1]->{'URL'} = $URL;
  203. return $newhandle;
  204. }
  205. }
  206. #================
  207. sub TimeConvert {
  208. #================
  209. my($self, $sec, $min, $hour, $day, $mon, $year, $wday, $rfc) = @_;
  210. return undef unless ref($self);
  211. if(!defined($rfc)) {
  212. return InternetTimeToSystemTime($sec);
  213. } else {
  214. return InternetTimeFromSystemTime($sec, $min, $hour,
  215. $day, $mon, $year,
  216. $wday, $rfc);
  217. }
  218. }
  219. #=======================
  220. sub QueryDataAvailable {
  221. #=======================
  222. my($self) = @_;
  223. return undef unless ref($self);
  224. return InternetQueryDataAvailable($self->{'handle'});
  225. }
  226. #=============
  227. sub ReadFile {
  228. #=============
  229. my($self, $buffersize) = @_;
  230. return undef unless ref($self);
  231. my $howmuch = InternetQueryDataAvailable($self->{'handle'});
  232. $buffersize = $howmuch unless defined($buffersize);
  233. return InternetReadFile($self->{'handle'}, ($howmuch<$buffersize) ? $howmuch
  234. : $buffersize);
  235. }
  236. #===================
  237. sub ReadEntireFile {
  238. #===================
  239. my($handle) = @_;
  240. my $content = "";
  241. my $buffersize = 16000;
  242. my $howmuch = 0;
  243. my $buffer = "";
  244. $handle = $handle->{'handle'} if defined($handle) and ref($handle);
  245. $howmuch = InternetQueryDataAvailable($handle);
  246. # print "\nReadEntireFile: $howmuch bytes to read...\n";
  247. while($howmuch>0) {
  248. $buffer = InternetReadFile($handle, ($howmuch<$buffersize) ? $howmuch
  249. : $buffersize);
  250. # print "\nReadEntireFile: ", length($buffer), " bytes read...\n";
  251. if(!defined($buffer)) {
  252. return undef;
  253. } else {
  254. $content .= $buffer;
  255. }
  256. $howmuch = InternetQueryDataAvailable($handle);
  257. # print "\nReadEntireFile: still $howmuch bytes to read...\n";
  258. }
  259. return $content;
  260. }
  261. #=============
  262. sub FetchURL {
  263. #=============
  264. # (OpenURL+Read+Close)...
  265. my($self, $URL) = @_;
  266. return undef unless ref($self);
  267. my $newhandle = InternetOpenUrl($self->{'handle'}, $URL, "", 0, 0, 0);
  268. if(!$newhandle) {
  269. $self->{'Error'} = "Cannot open URL.";
  270. return undef;
  271. } else {
  272. my $content = ReadEntireFile($newhandle);
  273. InternetCloseHandle($newhandle);
  274. return $content;
  275. }
  276. }
  277. #================
  278. sub Connections {
  279. #================
  280. my($self) = @_;
  281. return undef unless ref($self);
  282. return $self->{'connections'} if $self->{'Type'} eq "Internet";
  283. return undef;
  284. }
  285. #================
  286. sub GetResponse {
  287. #================
  288. my($num, $text) = InternetGetLastResponseInfo();
  289. return $text;
  290. }
  291. #===========
  292. sub Option {
  293. #===========
  294. my($self, $option, $value) = @_;
  295. return undef unless ref($self);
  296. my $retval = 0;
  297. $option = constant("INTERNET_OPTION_USER_AGENT", 0) unless defined($option);
  298. if(!defined($value)) {
  299. $retval = InternetQueryOption($self->{'handle'}, $option);
  300. } else {
  301. $retval = InternetSetOption($self->{'handle'}, $option, $value);
  302. }
  303. return $retval;
  304. }
  305. #==============
  306. sub UserAgent {
  307. #==============
  308. my($self, $value) = @_;
  309. return undef unless ref($self);
  310. return Option($self, constant("INTERNET_OPTION_USER_AGENT", 0), $value);
  311. }
  312. #=============
  313. sub Username {
  314. #=============
  315. my($self, $value) = @_;
  316. return undef unless ref($self);
  317. if($self->{'Type'} ne "HTTP" and $self->{'Type'} ne "FTP") {
  318. $self->{'Error'} = "Username() only on FTP or HTTP sessions.";
  319. return undef;
  320. }
  321. return Option($self, constant("INTERNET_OPTION_USERNAME", 0), $value);
  322. }
  323. #=============
  324. sub Password {
  325. #=============
  326. my($self, $value)=@_;
  327. return undef unless ref($self);
  328. if($self->{'Type'} ne "HTTP" and $self->{'Type'} ne "FTP") {
  329. $self->{'Error'} = "Password() only on FTP or HTTP sessions.";
  330. return undef;
  331. }
  332. return Option($self, constant("INTERNET_OPTION_PASSWORD", 0), $value);
  333. }
  334. #===================
  335. sub ConnectTimeout {
  336. #===================
  337. my($self, $value) = @_;
  338. return undef unless ref($self);
  339. return Option($self, constant("INTERNET_OPTION_CONNECT_TIMEOUT", 0), $value);
  340. }
  341. #===================
  342. sub ConnectRetries {
  343. #===================
  344. my($self, $value) = @_;
  345. return undef unless ref($self);
  346. return Option($self, constant("INTERNET_OPTION_CONNECT_RETRIES", 0), $value);
  347. }
  348. #===================
  349. sub ConnectBackoff {
  350. #===================
  351. my($self,$value)=@_;
  352. return undef unless ref($self);
  353. return Option($self, constant("INTERNET_OPTION_CONNECT_BACKOFF", 0), $value);
  354. }
  355. #====================
  356. sub DataSendTimeout {
  357. #====================
  358. my($self,$value) = @_;
  359. return undef unless ref($self);
  360. return Option($self, constant("INTERNET_OPTION_DATA_SEND_TIMEOUT", 0), $value);
  361. }
  362. #=======================
  363. sub DataReceiveTimeout {
  364. #=======================
  365. my($self, $value) = @_;
  366. return undef unless ref($self);
  367. return Option($self, constant("INTERNET_OPTION_DATA_RECEIVE_TIMEOUT", 0), $value);
  368. }
  369. #==========================
  370. sub ControlReceiveTimeout {
  371. #==========================
  372. my($self, $value) = @_;
  373. return undef unless ref($self);
  374. return Option($self, constant("INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT", 0), $value);
  375. }
  376. #=======================
  377. sub ControlSendTimeout {
  378. #=======================
  379. my($self, $value) = @_;
  380. return undef unless ref($self);
  381. return Option($self, constant("INTERNET_OPTION_CONTROL_SEND_TIMEOUT", 0), $value);
  382. }
  383. #================
  384. sub QueryOption {
  385. #================
  386. my($self, $option) = @_;
  387. return undef unless ref($self);
  388. return InternetQueryOption($self->{'handle'}, $option);
  389. }
  390. #==============
  391. sub SetOption {
  392. #==============
  393. my($self, $option, $value) = @_;
  394. return undef unless ref($self);
  395. return InternetSetOption($self->{'handle'}, $option, $value);
  396. }
  397. #=============
  398. sub CrackURL {
  399. #=============
  400. my($self, $URL, $flags) = @_;
  401. return undef unless ref($self);
  402. $flags = constant("ICU_ESCAPE", 0) unless defined($flags);
  403. my @newurl = InternetCrackUrl($URL, $flags);
  404. if(!defined($newurl[0])) {
  405. $self->{'Error'} = "Cannot crack URL.";
  406. return undef;
  407. } else {
  408. return @newurl;
  409. }
  410. }
  411. #==============
  412. sub CreateURL {
  413. #==============
  414. my($self, $scheme, $hostname, $port,
  415. $username, $password,
  416. $path, $extrainfo, $flags) = @_;
  417. return undef unless ref($self);
  418. if(ref($scheme) and ref($scheme) eq "HASH") {
  419. $flags = $hostname;
  420. $hostname = $scheme->{'hostname'};
  421. $port = $scheme->{'port'};
  422. $username = $scheme->{'username'};
  423. $password = $scheme->{'password'};
  424. $path = $scheme->{'path'};
  425. $extrainfo = $scheme->{'extrainfo'};
  426. my $myscheme = $scheme->{'scheme'};
  427. undef $scheme;
  428. $scheme = $myscheme;
  429. }
  430. $hostname = "" unless defined($hostname);
  431. $port = 0 unless defined($port);
  432. $username = "" unless defined($username);
  433. $password = "" unless defined($password);
  434. $path = "" unless defined($path);
  435. $extrainfo = "" unless defined($extrainfo);
  436. $flags = constant("ICU_ESCAPE", 0) unless defined($flags);
  437. my $newurl = InternetCreateUrl($scheme, $hostname, $port,
  438. $username, $password,
  439. $path, $extrainfo, $flags);
  440. if(!defined($newurl)) {
  441. $self->{'Error'} = "Cannot create URL.";
  442. return undef;
  443. } else {
  444. return $newurl;
  445. }
  446. }
  447. #====================
  448. sub CanonicalizeURL {
  449. #====================
  450. my($self, $URL, $flags) = @_;
  451. return undef unless ref($self);
  452. my $newurl = InternetCanonicalizeUrl($URL, $flags);
  453. if(!defined($newurl)) {
  454. $self->{'Error'} = "Cannot canonicalize URL.";
  455. return undef;
  456. } else {
  457. return $newurl;
  458. }
  459. }
  460. #===============
  461. sub CombineURL {
  462. #===============
  463. my($self, $baseURL, $relativeURL, $flags) = @_;
  464. return undef unless ref($self);
  465. my $newurl = InternetCombineUrl($baseURL, $relativeURL, $flags);
  466. if(!defined($newurl)) {
  467. $self->{'Error'} = "Cannot combine URL(s).";
  468. return undef;
  469. } else {
  470. return $newurl;
  471. }
  472. }
  473. #======================
  474. sub SetStatusCallback {
  475. #======================
  476. my($self) = @_;
  477. return undef unless ref($self);
  478. my $callback = InternetSetStatusCallback($self->{'handle'});
  479. print "callback=$callback, constant=",constant("INTERNET_INVALID_STATUS_CALLBACK", 0), "\n";
  480. if($callback == constant("INTERNET_INVALID_STATUS_CALLBACK", 0)) {
  481. return undef;
  482. } else {
  483. return $callback;
  484. }
  485. }
  486. #======================
  487. sub GetStatusCallback {
  488. #======================
  489. my($self, $context) = @_;
  490. $context = $self if not defined $context;
  491. return($callback_code{$context}, $callback_info{$context});
  492. }
  493. #==========
  494. sub Error {
  495. #==========
  496. my($self) = @_;
  497. return undef unless ref($self);
  498. my $errtext = "";
  499. my $tmp = "";
  500. my $errnum = Win32::GetLastError();
  501. if($errnum < 12000) {
  502. $errtext = Win32::FormatMessage($errnum);
  503. $errtext =~ s/[\r\n]//g;
  504. } elsif($errnum == 12003) {
  505. ($tmp, $errtext) = InternetGetLastResponseInfo();
  506. chomp $errtext;
  507. 1 while($errtext =~ s/(.*)\n//); # the last line should be significative...
  508. # otherwise call GetResponse() to get it whole
  509. } elsif($errnum >= 12000) {
  510. $errtext = FormatMessage($errnum);
  511. $errtext =~ s/[\r\n]//g;
  512. } else {
  513. $errtext="Error";
  514. }
  515. if($errnum == 0 and defined($self->{'Error'})) {
  516. if($self->{'Error'} == -2) {
  517. $errnum = -2;
  518. $errtext = "Asynchronous operations not available.";
  519. } else {
  520. $errnum = -1;
  521. $errtext = $self->{'Error'};
  522. }
  523. }
  524. return (wantarray)? ($errnum, $errtext) : "\[".$errnum."\] ".$errtext;
  525. }
  526. #============
  527. sub Version {
  528. #============
  529. my $dll = InternetDllVersion();
  530. $dll =~ s/\0//g;
  531. return (wantarray)? ($Win32::Internet::VERSION, $dll)
  532. : $Win32::Internet::VERSION."/".$dll;
  533. }
  534. #==========
  535. sub Close {
  536. #==========
  537. my($self, $handle) = @_;
  538. if(!defined($handle)) {
  539. return undef unless ref($self);
  540. $handle = $self->{'handle'};
  541. }
  542. InternetCloseHandle($handle);
  543. }
  544. #######################################################################
  545. # FTP CLASS METHODS
  546. #
  547. #======== ### FTP CONSTRUCTOR
  548. sub FTP {
  549. #========
  550. my($self, $new, $server, $username, $password, $port, $pasv, $context) = @_;
  551. return undef unless ref($self);
  552. if(ref($server) and ref($server) eq "HASH") {
  553. $port = $server->{'port'};
  554. $username = $server->{'username'};
  555. $password = $password->{'host'};
  556. my $myserver = $server->{'server'};
  557. $pasv = $server->{'pasv'};
  558. $context = $server->{'context'};
  559. undef $server;
  560. $server = $myserver;
  561. }
  562. $server = "" unless defined($server);
  563. $username = "anonymous" unless defined($username);
  564. $password = "" unless defined($password);
  565. $port = 21 unless defined($port);
  566. $context = 0 unless defined($context);
  567. $pasv = $self->{'pasv'} unless defined $pasv;
  568. $pasv = $pasv ? constant("INTERNET_FLAG_PASSIVE",0) : 0;
  569. my $newhandle = InternetConnect($self->{'handle'}, $server, $port,
  570. $username, $password,
  571. constant("INTERNET_SERVICE_FTP", 0),
  572. $pasv, $context);
  573. if($newhandle) {
  574. $self->{'connections'}++;
  575. $_[1] = _new($newhandle);
  576. $_[1]->{'Type'} = "FTP";
  577. $_[1]->{'Mode'} = "bin";
  578. $_[1]->{'pasv'} = $pasv;
  579. $_[1]->{'username'} = $username;
  580. $_[1]->{'password'} = $password;
  581. $_[1]->{'server'} = $server;
  582. return $newhandle;
  583. } else {
  584. return undef;
  585. }
  586. }
  587. #========
  588. sub Pwd {
  589. #========
  590. my($self) = @_;
  591. return undef unless ref($self);
  592. if($self->{'Type'} ne "FTP" or !defined($self->{'handle'})) {
  593. $self->{'Error'} = "Pwd() only on FTP sessions.";
  594. return undef;
  595. }
  596. return FtpGetCurrentDirectory($self->{'handle'});
  597. }
  598. #=======
  599. sub Cd {
  600. #=======
  601. my($self, $path) = @_;
  602. return undef unless ref($self);
  603. if($self->{'Type'} ne "FTP" || !defined($self->{'handle'})) {
  604. $self->{'Error'} = "Cd() only on FTP sessions.";
  605. return undef;
  606. }
  607. my $retval = FtpSetCurrentDirectory($self->{'handle'}, $path);
  608. if(!defined($retval)) {
  609. return undef;
  610. } else {
  611. return $path;
  612. }
  613. }
  614. #====================
  615. sub Cwd { Cd(@_); }
  616. sub Chdir { Cd(@_); }
  617. #====================
  618. #==========
  619. sub Mkdir {
  620. #==========
  621. my($self, $path) = @_;
  622. return undef unless ref($self);
  623. if($self->{'Type'} ne "FTP" or !defined($self->{'handle'})) {
  624. $self->{'Error'} = "Mkdir() only on FTP sessions.";
  625. return undef;
  626. }
  627. my $retval = FtpCreateDirectory($self->{'handle'}, $path);
  628. $self->{'Error'} = "Can't create directory." unless defined($retval);
  629. return $retval;
  630. }
  631. #====================
  632. sub Md { Mkdir(@_); }
  633. #====================
  634. #=========
  635. sub Mode {
  636. #=========
  637. my($self, $value) = @_;
  638. return undef unless ref($self);
  639. if($self->{'Type'} ne "FTP" or !defined($self->{'handle'})) {
  640. $self->{'Error'} = "Mode() only on FTP sessions.";
  641. return undef;
  642. }
  643. if(!defined($value)) {
  644. return $self->{'Mode'};
  645. } else {
  646. my $modesub = ($value =~ /^a/i) ? "Ascii" : "Binary";
  647. $self->$modesub($_[0]);
  648. }
  649. return $self->{'Mode'};
  650. }
  651. #==========
  652. sub Rmdir {
  653. #==========
  654. my($self, $path) = @_;
  655. return undef unless ref($self);
  656. if($self->{'Type'} ne "FTP" or !defined($self->{'handle'})) {
  657. $self->{'Error'} = "Rmdir() only on FTP sessions.";
  658. return undef;
  659. }
  660. my $retval = FtpRemoveDirectory($self->{'handle'}, $path);
  661. $self->{'Error'} = "Can't remove directory." unless defined($retval);
  662. return $retval;
  663. }
  664. #====================
  665. sub Rd { Rmdir(@_); }
  666. #====================
  667. #=========
  668. sub Pasv {
  669. #=========
  670. my($self, $value) = @_;
  671. return undef unless ref($self);
  672. if(defined($value) and $self->{'Type'} eq "Internet") {
  673. if($value == 0) {
  674. $self->{'pasv'} = 0;
  675. } else {
  676. $self->{'pasv'} = 1;
  677. }
  678. }
  679. return $self->{'pasv'};
  680. }
  681. #=========
  682. sub List {
  683. #=========
  684. my($self, $pattern, $retmode) = @_;
  685. return undef unless ref($self);
  686. my $retval = "";
  687. my $size = "";
  688. my $attr = "";
  689. my $ctime = "";
  690. my $atime = "";
  691. my $mtime = "";
  692. my $csec = 0; my $cmin = 0; my $chou = 0; my $cday = 0; my $cmon = 0; my $cyea = 0;
  693. my $asec = 0; my $amin = 0; my $ahou = 0; my $aday = 0; my $amon = 0; my $ayea = 0;
  694. my $msec = 0; my $mmin = 0; my $mhou = 0; my $mday = 0; my $mmon = 0; my $myea = 0;
  695. my $newhandle = 0;
  696. my $nextfile = 1;
  697. my @results = ();
  698. my ($filename, $altname, $file);
  699. if($self->{'Type'} ne "FTP") {
  700. $self->{'Error'} = "List() only on FTP sessions.";
  701. return undef;
  702. }
  703. $pattern = "" unless defined($pattern);
  704. $retmode = 1 unless defined($retmode);
  705. if($retmode == 2) {
  706. ( $newhandle,$filename, $altname, $size, $attr,
  707. $csec, $cmin, $chou, $cday, $cmon, $cyea,
  708. $asec, $amin, $ahou, $aday, $amon, $ayea,
  709. $msec, $mmin, $mhou, $mday, $mmon, $myea
  710. ) = FtpFindFirstFile($self->{'handle'}, $pattern, 0, 0);
  711. if(!$newhandle) {
  712. $self->{'Error'} = "Can't read FTP directory.";
  713. return undef;
  714. } else {
  715. while($nextfile) {
  716. $ctime = join(",", ($csec, $cmin, $chou, $cday, $cmon, $cyea));
  717. $atime = join(",", ($asec, $amin, $ahou, $aday, $amon, $ayea));
  718. $mtime = join(",", ($msec, $mmin, $mhou, $mday, $mmon, $myea));
  719. push(@results, $filename, $altname, $size, $attr, $ctime, $atime, $mtime);
  720. ( $nextfile, $filename, $altname, $size, $attr,
  721. $csec, $cmin, $chou, $cday, $cmon, $cyea,
  722. $asec, $amin, $ahou, $aday, $amon, $ayea,
  723. $msec, $mmin, $mhou, $mday, $mmon, $myea
  724. ) = InternetFindNextFile($newhandle);
  725. }
  726. InternetCloseHandle($newhandle);
  727. return @results;
  728. }
  729. } elsif($retmode == 3) {
  730. ( $newhandle,$filename, $altname, $size, $attr,
  731. $csec, $cmin, $chou, $cday, $cmon, $cyea,
  732. $asec, $amin, $ahou, $aday, $amon, $ayea,
  733. $msec, $mmin, $mhou, $mday, $mmon, $myea
  734. ) = FtpFindFirstFile($self->{'handle'}, $pattern, 0, 0);
  735. if(!$newhandle) {
  736. $self->{'Error'} = "Can't read FTP directory.";
  737. return undef;
  738. } else {
  739. while($nextfile) {
  740. $ctime = join(",", ($csec, $cmin, $chou, $cday, $cmon, $cyea));
  741. $atime = join(",", ($asec, $amin, $ahou, $aday, $amon, $ayea));
  742. $mtime = join(",", ($msec, $mmin, $mhou, $mday, $mmon, $myea));
  743. $file = { "name" => $filename,
  744. "altname" => $altname,
  745. "size" => $size,
  746. "attr" => $attr,
  747. "ctime" => $ctime,
  748. "atime" => $atime,
  749. "mtime" => $mtime,
  750. };
  751. push(@results, $file);
  752. ( $nextfile, $filename, $altname, $size, $attr,
  753. $csec, $cmin, $chou, $cday, $cmon, $cyea,
  754. $asec, $amin, $ahou, $aday, $amon, $ayea,
  755. $msec, $mmin, $mhou, $mday, $mmon, $myea
  756. ) = InternetFindNextFile($newhandle);
  757. }
  758. InternetCloseHandle($newhandle);
  759. return @results;
  760. }
  761. } else {
  762. ($newhandle, $filename) = FtpFindFirstFile($self->{'handle'}, $pattern, 0, 0);
  763. if(!$newhandle) {
  764. $self->{'Error'} = "Can't read FTP directory.";
  765. return undef;
  766. } else {
  767. while($nextfile) {
  768. push(@results, $filename);
  769. ($nextfile, $filename) = InternetFindNextFile($newhandle);
  770. # print "List.no more files\n" if !$nextfile;
  771. }
  772. InternetCloseHandle($newhandle);
  773. return @results;
  774. }
  775. }
  776. }
  777. #====================
  778. sub Ls { List(@_); }
  779. sub Dir { List(@_); }
  780. #====================
  781. #=================
  782. sub FileAttrInfo {
  783. #=================
  784. my($self,$attr) = @_;
  785. my @attrinfo = ();
  786. push(@attrinfo, "READONLY") if $attr & 1;
  787. push(@attrinfo, "HIDDEN") if $attr & 2;
  788. push(@attrinfo, "SYSTEM") if $attr & 4;
  789. push(@attrinfo, "DIRECTORY") if $attr & 16;
  790. push(@attrinfo, "ARCHIVE") if $attr & 32;
  791. push(@attrinfo, "NORMAL") if $attr & 128;
  792. push(@attrinfo, "TEMPORARY") if $attr & 256;
  793. push(@attrinfo, "COMPRESSED") if $attr & 2048;
  794. return (wantarray)? @attrinfo : join(" ", @attrinfo);
  795. }
  796. #===========
  797. sub Binary {
  798. #===========
  799. my($self) = @_;
  800. return undef unless ref($self);
  801. if($self->{'Type'} ne "FTP") {
  802. $self->{'Error'} = "Binary() only on FTP sessions.";
  803. return undef;
  804. }
  805. $self->{'Mode'} = "bin";
  806. return undef;
  807. }
  808. #======================
  809. sub Bin { Binary(@_); }
  810. #======================
  811. #==========
  812. sub Ascii {
  813. #==========
  814. my($self) = @_;
  815. return undef unless ref($self);
  816. if($self->{'Type'} ne "FTP") {
  817. $self->{'Error'} = "Ascii() only on FTP sessions.";
  818. return undef;
  819. }
  820. $self->{'Mode'} = "asc";
  821. return undef;
  822. }
  823. #=====================
  824. sub Asc { Ascii(@_); }
  825. #=====================
  826. #========
  827. sub Get {
  828. #========
  829. my($self, $remote, $local, $overwrite, $flags, $context) = @_;
  830. return undef unless ref($self);
  831. if($self->{'Type'} ne "FTP") {
  832. $self->{'Error'} = "Get() only on FTP sessions.";
  833. return undef;
  834. }
  835. my $mode = ($self->{'Mode'} eq "asc" ? 1 : 2);
  836. $remote = "" unless defined($remote);
  837. $local = $remote unless defined($local);
  838. $overwrite = 0 unless defined($overwrite);
  839. $flags = 0 unless defined($flags);
  840. $context = 0 unless defined($context);
  841. my $retval = FtpGetFile($self->{'handle'},
  842. $remote,
  843. $local,
  844. $overwrite,
  845. $flags,
  846. $mode,
  847. $context);
  848. $self->{'Error'} = "Can't get file." unless defined($retval);
  849. return $retval;
  850. }
  851. #===========
  852. sub Rename {
  853. #===========
  854. my($self, $oldname, $newname) = @_;
  855. return undef unless ref($self);
  856. if($self->{'Type'} ne "FTP") {
  857. $self->{'Error'} = "Rename() only on FTP sessions.";
  858. return undef;
  859. }
  860. my $retval = FtpRenameFile($self->{'handle'}, $oldname, $newname);
  861. $self->{'Error'} = "Can't rename file." unless defined($retval);
  862. return $retval;
  863. }
  864. #======================
  865. sub Ren { Rename(@_); }
  866. #======================
  867. #===========
  868. sub Delete {
  869. #===========
  870. my($self, $filename) = @_;
  871. return undef unless ref($self);
  872. if($self->{'Type'} ne "FTP") {
  873. $self->{'Error'} = "Delete() only on FTP sessions.";
  874. return undef;
  875. }
  876. my $retval = FtpDeleteFile($self->{'handle'}, $filename);
  877. $self->{'Error'} = "Can't delete file." unless defined($retval);
  878. return $retval;
  879. }
  880. #======================
  881. sub Del { Delete(@_); }
  882. #======================
  883. #========
  884. sub Put {
  885. #========
  886. my($self, $local, $remote, $context) = @_;
  887. return undef unless ref($self);
  888. if($self->{'Type'} ne "FTP") {
  889. $self->{'Error'} = "Put() only on FTP sessions.";
  890. return undef;
  891. }
  892. my $mode = ($self->{'Mode'} eq "asc" ? 1 : 2);
  893. $context = 0 unless defined($context);
  894. my $retval = FtpPutFile($self->{'handle'}, $local, $remote, $mode, $context);
  895. $self->{'Error'} = "Can't put file." unless defined($retval);
  896. return $retval;
  897. }
  898. #######################################################################
  899. # HTTP CLASS METHODS
  900. #
  901. #========= ### HTTP CONSTRUCTOR
  902. sub HTTP {
  903. #=========
  904. my($self, $new, $server, $username, $password, $port, $flags, $context) = @_;
  905. return undef unless ref($self);
  906. if(ref($server) and ref($server) eq "HASH") {
  907. my $myserver = $server->{'server'};
  908. $username = $server->{'username'};
  909. $password = $password->{'host'};
  910. $port = $server->{'port'};
  911. $flags = $server->{'flags'};
  912. $context = $server->{'context'};
  913. undef $server;
  914. $server = $myserver;
  915. }
  916. $server = "" unless defined($server);
  917. $username = "anonymous" unless defined($username);
  918. $password = "" unless defined($username);
  919. $port = 80 unless defined($port);
  920. $flags = 0 unless defined($flags);
  921. $context = 0 unless defined($context);
  922. my $newhandle = InternetConnect($self->{'handle'}, $server, $port,
  923. $username, $password,
  924. constant("INTERNET_SERVICE_HTTP", 0),
  925. $flags, $context);
  926. if($newhandle) {
  927. $self->{'connections'}++;
  928. $_[1] = _new($newhandle);
  929. $_[1]->{'Type'} = "HTTP";
  930. $_[1]->{'username'} = $username;
  931. $_[1]->{'password'} = $password;
  932. $_[1]->{'server'} = $server;
  933. $_[1]->{'accept'} = "text/*\0image/gif\0image/jpeg";
  934. return $newhandle;
  935. } else {
  936. return undef;
  937. }
  938. }
  939. #================
  940. sub OpenRequest {
  941. #================
  942. # alternatively to Request:
  943. # it creates a new HTTP_Request object
  944. # you can act upon it with AddHeader, SendRequest, ReadFile, QueryInfo, Close, ...
  945. my($self, $new, $path, $method, $version, $referer, $accept, $flags, $context) = @_;
  946. return undef unless ref($self);
  947. if($self->{'Type'} ne "HTTP") {
  948. $self->{'Error'} = "OpenRequest() only on HTTP sessions.";
  949. return undef;
  950. }
  951. if(ref($path) and ref($path) eq "HASH") {
  952. $method = $path->{'method'};
  953. $version = $path->{'version'};
  954. $referer = $path->{'referer'};
  955. $accept = $path->{'accept'};
  956. $flags = $path->{'flags'};
  957. $context = $path->{'context'};
  958. my $mypath = $path->{'path'};
  959. undef $path;
  960. $path = $mypath;
  961. }
  962. $method = "GET" unless defined($method);
  963. $path = "/" unless defined($path);
  964. $version = "HTTP/1.0" unless defined($version);
  965. $referer = "" unless defined($referer);
  966. $accept = $self->{'accept'} unless defined($accept);
  967. $flags = 0 unless defined($flags);
  968. $context = 0 unless defined($context);
  969. $path = "/".$path if substr($path,0,1) ne "/";
  970. my $newhandle = HttpOpenRequest($self->{'handle'},
  971. $method,
  972. $path,
  973. $version,
  974. $referer,
  975. $accept,
  976. $flags,
  977. $context);
  978. if($newhandle) {
  979. $_[1] = _new($newhandle);
  980. $_[1]->{'Type'} = "HTTP_Request";
  981. $_[1]->{'method'} = $method;
  982. $_[1]->{'request'} = $path;
  983. $_[1]->{'accept'} = $accept;
  984. return $newhandle;
  985. } else {
  986. return undef;
  987. }
  988. }
  989. #================
  990. sub SendRequest {
  991. #================
  992. my($self, $postdata) = @_;
  993. return undef unless ref($self);
  994. if($self->{'Type'} ne "HTTP_Request") {
  995. $self->{'Error'} = "SendRequest() only on HTTP requests.";
  996. return undef;
  997. }
  998. $postdata = "" unless defined($postdata);
  999. return HttpSendRequest($self->{'handle'}, "", $postdata);
  1000. }
  1001. #==============
  1002. sub AddHeader {
  1003. #==============
  1004. my($self, $header, $flags) = @_;
  1005. return undef unless ref($self);
  1006. if($self->{'Type'} ne "HTTP_Request") {
  1007. $self->{'Error'} = "AddHeader() only on HTTP requests.";
  1008. return undef;
  1009. }
  1010. $flags = constant("HTTP_ADDREQ_FLAG_ADD", 0) if (!defined($flags) or $flags == 0);
  1011. return HttpAddRequestHeaders($self->{'handle'}, $header, $flags);
  1012. }
  1013. #==============
  1014. sub QueryInfo {
  1015. #==============
  1016. my($self, $header, $flags) = @_;
  1017. return undef unless ref($self);
  1018. if($self->{'Type'} ne "HTTP_Request") {
  1019. $self->{'Error'}="QueryInfo() only on HTTP requests.";
  1020. return undef;
  1021. }
  1022. $flags = constant("HTTP_QUERY_CUSTOM", 0) if (!defined($flags) and defined($header));
  1023. my @queryresult = HttpQueryInfo($self->{'handle'}, $flags, $header);
  1024. return (wantarray)? @queryresult : join(" ", @queryresult);
  1025. }
  1026. #============
  1027. sub Request {
  1028. #============
  1029. # HttpOpenRequest+HttpAddHeaders+HttpSendRequest+InternetReadFile+HttpQueryInfo
  1030. my($self, $path, $method, $version, $referer, $accept, $flags, $postdata) = @_;
  1031. return undef unless ref($self);
  1032. if($self->{'Type'} ne "HTTP") {
  1033. $self->{'Error'} = "Request() only on HTTP sessions.";
  1034. return undef;
  1035. }
  1036. if(ref($path) and ref($path) eq "HASH") {
  1037. $method = $path->{'method'};
  1038. $version = $path->{'version'};
  1039. $referer = $path->{'referer'};
  1040. $accept = $path->{'accept'};
  1041. $flags = $path->{'flags'};
  1042. $postdata = $path->{'postdata'};
  1043. my $mypath = $path->{'path'};
  1044. undef $path;
  1045. $path = $mypath;
  1046. }
  1047. my $content = "";
  1048. my $result = "";
  1049. my @queryresult = ();
  1050. my $statuscode = "";
  1051. my $headers = "";
  1052. $path = "/" unless defined($path);
  1053. $method = "GET" unless defined($method);
  1054. $version = "HTTP/1.0" unless defined($version);
  1055. $referer = "" unless defined($referer);
  1056. $accept = $self->{'accept'} unless defined($accept);
  1057. $flags = 0 unless defined($flags);
  1058. $postdata = "" unless defined($postdata);
  1059. $path = "/".$path if substr($path,0,1) ne "/";
  1060. my $newhandle = HttpOpenRequest($self->{'handle'},
  1061. $method,
  1062. $path,
  1063. $version,
  1064. $referer,
  1065. $accept,
  1066. $flags,
  1067. 0);
  1068. if($newhandle) {
  1069. $result = HttpSendRequest($newhandle, "", $postdata);
  1070. if(defined($result)) {
  1071. $statuscode = HttpQueryInfo($newhandle,
  1072. constant("HTTP_QUERY_STATUS_CODE", 0), "");
  1073. $headers = HttpQueryInfo($newhandle,
  1074. constant("HTTP_QUERY_RAW_HEADERS_CRLF", 0), "");
  1075. $content = ReadEntireFile($newhandle);
  1076. InternetCloseHandle($newhandle);
  1077. return($statuscode, $headers, $content);
  1078. } else {
  1079. return undef;
  1080. }
  1081. } else {
  1082. return undef;
  1083. }
  1084. }
  1085. #######################################################################
  1086. # END OF THE PUBLIC METHODS
  1087. #
  1088. #========= ### SUB-CLASSES CONSTRUCTOR
  1089. sub _new {
  1090. #=========
  1091. my $self = {};
  1092. if ($_[0]) {
  1093. $self->{'handle'} = $_[0];
  1094. bless $self;
  1095. } else {
  1096. undef($self);
  1097. }
  1098. $self;
  1099. }
  1100. #============ ### CLASS DESTRUCTOR
  1101. sub DESTROY {
  1102. #============
  1103. my($self) = @_;
  1104. # print "Closing handle $self->{'handle'}...\n";
  1105. InternetCloseHandle($self->{'handle'});
  1106. # [dada] rest in peace
  1107. }
  1108. #=============
  1109. sub callback {
  1110. #=============
  1111. my($name, $status, $info) = @_;
  1112. $callback_code{$name} = $status;
  1113. $callback_info{$name} = $info;
  1114. }
  1115. #######################################################################
  1116. # dynamically load in the Internet.pll module.
  1117. #
  1118. bootstrap Win32::Internet;
  1119. # Preloaded methods go here.
  1120. #Currently Autoloading is not implemented in Perl for win32
  1121. # Autoload methods go after __END__, and are processed by the autosplit program.
  1122. 1;
  1123. __END__
  1124. =head1 NAME
  1125. Win32::Internet - Access to WININET.DLL functions
  1126. =head1 INTRODUCTION
  1127. This extension to Perl implements the Win32 Internet APIs (found in
  1128. F<WININET.DLL>). They give a complete support for HTTP, FTP and GOPHER
  1129. connections.
  1130. See the L<"Version History"> and the L<"Functions Table"> for a list
  1131. of the currently supported features. You should also get a copy of the
  1132. L<"Microsoft Win32 Internet Functions"> documentation.
  1133. =head1 REFERENCE
  1134. To use this module, first add the following line at the beginning of
  1135. your script:
  1136. use Win32::Internet;
  1137. Then you have to open an Internet connection with this command:
  1138. $Connection = new Win32::Internet();
  1139. This is required to use any of the function of this module. It will
  1140. create an Internet object in Perl on which you can act upon with the
  1141. L<"General Internet Functions"> explained later.
  1142. The objects available are:
  1143. =over
  1144. =item *
  1145. Internet connections (the main object, see C<new>)
  1146. =item *
  1147. URLs (see C<OpenURL>)
  1148. =item *
  1149. FTP sessions (see C<FTP>)
  1150. =item *
  1151. HTTP sessions (see C<HTTP>)
  1152. =item *
  1153. HTTP requests (see C<OpenRequest>)
  1154. =back
  1155. As in the good Perl tradition, there are in this extension different
  1156. ways to do the same thing; there are, in fact, different levels of
  1157. implementation of the Win32 Internet Functions. Some routines use
  1158. several Win32 API functions to perform a complex task in a single
  1159. call; they are simpler to use, but of course less powerful.
  1160. There are then other functions that implement nothing more and nothing
  1161. less than the corresponding API function, so you can use all of their
  1162. power, but with some additional programming steps.
  1163. To make an example, there is a function called C<FetchURL> that you
  1164. can use to fetch the content of any HTTP, FTP or GOPHER URL with this
  1165. simple commands:
  1166. $INET = new Win32::Internet();
  1167. $file = $INET->FetchURL("http://www.yahoo.com");
  1168. You can have the same result (and this is actually what is done by
  1169. C<FetchURL>) this way:
  1170. $INET = new Win32::Internet();
  1171. $URL = $INET->OpenURL("http://www.yahoo.com");
  1172. $file = $URL->ReadFile();
  1173. $URL->Close();
  1174. Or, you can open a complete HTTP session:
  1175. $INET = new Win32::Internet();
  1176. $HTTP = $INET->HTTP("www.yahoo.com", "anonymous", "[email protected]");
  1177. ($statuscode, $headers, $file) = $HTTP->Request("/");
  1178. $HTTP->Close();
  1179. Finally, you can choose to manage even the HTTP request:
  1180. $INET = new Win32::Internet();
  1181. $HTTP = $INET->HTTP("www.yahoo.com", "anonymous", "[email protected]");
  1182. $HTTP->OpenRequest($REQ, "/");
  1183. $REQ->AddHeader("If-Modified-Since: Saturday, 16-Nov-96 15:58:50 GMT");
  1184. $REQ->SendRequest();
  1185. $statuscode = $REQ->QueryInfo("",HTTP_QUERY_STATUS_CODE);
  1186. $lastmodified = $REQ->QueryInfo("Last-Modified");
  1187. $file = $REQ->ReadEntireFile();
  1188. $REQ->Close();
  1189. $HTTP->Close();
  1190. To open and control a complete FTP session, type:
  1191. $Connection->FTP($Session, "ftp://ftp.activeware.com", "anonymous", "dada\@divinf.it");
  1192. This will create an FTP object in Perl to which you can apply the L<"FTP
  1193. functions"> provided by the package:
  1194. $Session->Cd("/ntperl/perl5.001m/CurrentBuild");
  1195. $Session->Ascii();
  1196. $Session->Get("110-i86.zip");
  1197. $Session->Close();
  1198. For a more complete example, see the TEST.PL file that comes with the
  1199. package.
  1200. =head2 General Internet Functions
  1201. B<General Note>
  1202. All methods assume that you have the line:
  1203. use Win32::Internet;
  1204. somewhere before the method calls, and that you have an Internet
  1205. object called $INET which was created using this call:
  1206. $INET = new Win32::Internet();
  1207. See C<new> for more information.
  1208. B<Methods>
  1209. =over
  1210. =item CanonicalizeURL URL, [flags]
  1211. Converts a URL to a canonical format, which includes converting unsafe
  1212. characters to escape sequences. Returns the canonicalized URL or
  1213. C<undef> on errors. For the possible values of I<flags>, refer to the
  1214. L<"Microsoft Win32 Internet Functions"> document. See also
  1215. C<CombineURL> and C<OpenURL>.
  1216. Example:
  1217. $cURL = $INET->CanonicalizeURL($URL);
  1218. $URL = $INET->CanonicalizeURL($cURL, ICU_DECODE);
  1219. =item Close
  1220. =item Close object
  1221. Closes an Internet connection. This can be applied to any
  1222. Win32::Internet object (Internet connections, URLs, FTP sessions,
  1223. etc.). Note that it is not "strictly" required to close the
  1224. connections you create, since the Win32::Internet objects are
  1225. automatically closed when the program ends (or when you elsehow
  1226. destroy such an object).
  1227. Example:
  1228. $INET->Close();
  1229. $FTP->Close();
  1230. $INET->Close($FTP); # same as above...
  1231. =item CombineURL baseURL, relativeURL, [flags]
  1232. Combines a base and relative URL into a single URL. Returns the
  1233. (canonicalized) combined URL or C<undef> on errors. For the possible
  1234. values of I<flags>, refer to the L<"Microsoft Win32 Internet
  1235. Functions"> document. See also C<CombineURL> and C<OpenURL>.
  1236. Example:
  1237. $URL = $INET->CombineURL("http://www.divinf.it/dada/perl/internet", "..");
  1238. =item ConnectBackoff [value]
  1239. Reads or sets the delay value, in milliseconds, to wait between
  1240. connection retries. If no I<value> parameter is specified, the
  1241. current value is returned; otherwise, the delay between retries is set
  1242. to I<value>. See also C<ConnectTimeout>, C<ConnectRetries>,
  1243. C<QueryOption> and C<SetOption>.
  1244. Example:
  1245. $HTTP->ConnectBackoff(2000);
  1246. $backoff = $HTTP->ConnectBackoff();
  1247. =item ConnectRetries [value]
  1248. Reads or sets the number of times a connection is retried before
  1249. considering it failed. If no I<value> parameter is specified, the
  1250. current value is returned; otherwise, the number of retries is set to
  1251. I<value>. The default value for C<ConnectRetries> is 5. See also
  1252. C<ConnectBackoff>, C<ConnectTimeout>, C<QueryOption> and C<SetOption>.
  1253. Example:
  1254. $HTTP->ConnectRetries(20);
  1255. $retries = $HTTP->ConnectRetries();
  1256. =item ConnectTimeout [value]
  1257. Reads or sets the timeout value (in milliseconds) before a connection
  1258. is considered failed. If no I<value> parameter is specified, the
  1259. current value is returned; otherwise, the timeout is set to I<value>.
  1260. The default value for C<ConnectTimeout> is infinite. See also
  1261. C<ConnectBackoff>, C<ConnectRetries>, C<QueryOption> and C<SetOption>.
  1262. Example:
  1263. $HTTP->ConnectTimeout(10000);
  1264. $timeout = $HTTP->ConnectTimeout();
  1265. =item ControlReceiveTimeout [value]
  1266. Reads or sets the timeout value (in milliseconds) to use for non-data
  1267. (control) receive requests before they are canceled. Currently, this
  1268. value has meaning only for C<FTP> sessions. If no I<value> parameter
  1269. is specified, the current value is returned; otherwise, the timeout is
  1270. set to I<value>. The default value for C<ControlReceiveTimeout> is
  1271. infinite. See also C<ControlSendTimeout>, C<QueryOption> and
  1272. C<SetOption>.
  1273. Example:
  1274. $HTTP->ControlReceiveTimeout(10000);
  1275. $timeout = $HTTP->ControlReceiveTimeout();
  1276. =item ControlSendTimeout [value]
  1277. Reads or sets the timeout value (in milliseconds) to use for non-data
  1278. (control) send requests before they are canceled. Currently, this
  1279. value has meaning only for C<FTP> sessions. If no I<value> parameter
  1280. is specified, the current value is returned; otherwise, the timeout is
  1281. set to I<value>. The default value for C<ControlSendTimeout> is
  1282. infinite. See also C<ControlReceiveTimeout>, C<QueryOption> and
  1283. C<SetOption>.
  1284. Example:
  1285. $HTTP->ControlSendTimeout(10000);
  1286. $timeout = $HTTP->ControlSendTimeout();
  1287. =item CrackURL URL, [flags]
  1288. Splits an URL into its component parts and returns them in an array.
  1289. Returns C<undef> on errors, otherwise the array will contain the
  1290. following values: I<scheme, host, port, username, password, path,
  1291. extrainfo>.
  1292. For example, the URL "http://www.divinf.it/index.html#top" can be
  1293. splitted in:
  1294. http, www.divinf.it, 80, anonymous, dada@divinf.it, /index.html, #top
  1295. If you don't specify a I<flags> parameter, ICU_ESCAPE will be used by
  1296. default; for the possible values of I<flags> refer to the L<"Microsoft
  1297. Win32 Internet Functions"> documentation. See also C<CreateURL>.
  1298. Example:
  1299. @parts=$INET->CrackURL("http://www.activeware.com");
  1300. ($scheme, $host, $port, $user, $pass, $path, $extra) =
  1301. $INET->CrackURL("http://www.divinf.it:80/perl-win32/index.sht#feedback");
  1302. =item CreateURL scheme, hostname, port, username, password, path, extrainfo, [flags]
  1303. =item CreateURL hashref, [flags]
  1304. Creates a URL from its component parts. Returns C<undef> on errors,
  1305. otherwise the created URL.
  1306. If you pass I<hashref> (a reference to an hash array), the following
  1307. values are taken from the array:
  1308. %hash=(
  1309. "scheme" => "scheme",
  1310. "hostname" => "hostname",
  1311. "port" => port,
  1312. "username" => "username",
  1313. "password" => "password",
  1314. "path" => "path",
  1315. "extrainfo" => "extrainfo",
  1316. );
  1317. If you don't specify a I<flags> parameter, ICU_ESCAPE will be used by
  1318. default; for the other possible values of I<flags> refer to the
  1319. L<"Microsoft Win32 Internet Functions"> documentation. See also
  1320. C<CrackURL>.
  1321. Example:
  1322. $URL=$I->CreateURL("http", "www.divinf.it", 80, "", "", "/perl-win32/index.sht", "#feedback");
  1323. $URL=$I->CreateURL(\%params);
  1324. =item DataReceiveTimeout [value]
  1325. Reads or sets the timeout value (in milliseconds) to use for data
  1326. receive requests before they are canceled. If no I<value> parameter
  1327. is specified, the current value is returned; otherwise, the timeout is
  1328. set to I<value>. The default value for DataReceiveTimeout is
  1329. infinite. See also C<DataSendTimeout>, C<QueryOption> and
  1330. C<SetOption>.
  1331. Example:
  1332. $HTTP->DataReceiveTimeout(10000);
  1333. $timeout = $HTTP->DataReceiveTimeout();
  1334. =item DataSendTimeout [value]
  1335. Reads or sets the timeout value (in milliseconds) to use for data send
  1336. requests before they are canceled. If no I<value> parameter is
  1337. specified, the current value is returned; otherwise, the timeout is
  1338. set to I<value>. The default value for DataSendTimeout is infinite.
  1339. See also C<DataReceiveTimeout>, C<QueryOption> and C<SetOption>.
  1340. Example:
  1341. $HTTP->DataSendTimeout(10000);
  1342. $timeout = $HTTP->DataSendTimeout();
  1343. =item Error
  1344. Returns the last recorded error in the form of an array or string
  1345. (depending upon the context) containing the error number and an error
  1346. description. Can be applied on any Win32::Internet object (FTP
  1347. sessions, etc.). There are 3 types of error you can encounter; they
  1348. are recognizable by the error number returned:
  1349. =over
  1350. =item * -1
  1351. A "trivial" error has occurred in the package. For example, you tried
  1352. to use a method on the wrong type of object.
  1353. =item * 1 .. 11999
  1354. A generic error has occurred and the Win32::GetLastError error message
  1355. is returned.
  1356. =item * 12000 and higher
  1357. An Internet error has occurred; the extended Win32 Internet API error
  1358. message is returned.
  1359. =back
  1360. See also C<GetResponse>.
  1361. Example:
  1362. die $INET->Error(), qq(\n);
  1363. ($ErrNum, $ErrText) = $INET->Error();
  1364. =item FetchURL URL
  1365. Fetch the content of an HTTP, FTP or GOPHER URL. Returns the content
  1366. of the file read (or C<undef> if there was an error and nothing was
  1367. read). See also C<OpenURL> and C<ReadFile>.
  1368. Example:
  1369. $file = $INET->FetchURL("http://www.yahoo.com/");
  1370. $file = $INET->FetchURL("ftp://www.activeware.com/contrib/internet.zip");
  1371. =item FTP ftpobject, server, username, password, [port, pasv, context]
  1372. =item FTP ftpobject, hashref
  1373. Opens an FTP connection to server logging in with the given
  1374. I<username> and I<password>.
  1375. The parameters and their values are:
  1376. =over
  1377. =item * server
  1378. The server to connect to. Default: I<none>.
  1379. =item * username
  1380. The username used to login to the server. Default: anonymous.
  1381. =item * password
  1382. The password used to login to the server. Default: I<none>.
  1383. =item * port
  1384. The port of the FTP service on the server. Default: 21.
  1385. =item * pasv
  1386. If it is a value other than 0, use passive transfer mode. Default is
  1387. taken from the parent Internet connection object; you can set this
  1388. value with the C<Pasv> method.
  1389. =item * context
  1390. A number to identify this operation if it is asynchronous. See
  1391. C<SetStatusCallback> and C<GetStatusCallback> for more info on
  1392. asynchronous operations. Default: I<none>.
  1393. =back
  1394. If you pass I<hashref> (a reference to an hash array), the following
  1395. values are taken from the array:
  1396. %hash=(
  1397. "server" => "server",
  1398. "username" => "username",
  1399. "password" => "password",
  1400. "port" => port,
  1401. "pasv" => pasv,
  1402. "context" => context,
  1403. );
  1404. This method returns C<undef> if the connection failed, a number
  1405. otherwise. You can then call any of the L<"FTP functions"> as methods
  1406. of the newly created I<ftpobject>.
  1407. Example:
  1408. $result = $INET->FTP($FTP, "ftp.activeware.com", "anonymous", "dada\@divinf.it");
  1409. # and then for example...
  1410. $FTP->Cd("/ntperl/perl5.001m/CurrentBuild");
  1411. $params{"server"} = "ftp.activeware.com";
  1412. $params{"password"} = "dada\@divinf.it";
  1413. $params{"pasv"} = 0;
  1414. $result = $INET->FTP($FTP,\%params);
  1415. =item GetResponse
  1416. Returns the text sent by a remote server in response to the last
  1417. function executed. It applies on any Win32::Internet object,
  1418. particularly of course on L<FTP sessions|"FTP functions">. See also
  1419. the C<Error> function.
  1420. Example:
  1421. print $INET->GetResponse();
  1422. $INET->FTP($FTP, "ftp.activeware.com", "anonymous", "dada\@divinf.it");
  1423. print $FTP->GetResponse();
  1424. =item GetStatusCallback context
  1425. Returns information about the progress of the asynchronous operation
  1426. identified by I<context>; those informations consist of two values: a
  1427. status code (one of the INTERNET_STATUS_* L<"Constants">) and an
  1428. additional value depending on the status code; for example, if the
  1429. status code returned is INTERNET_STATUS_HANDLE_CREATED, the second
  1430. value will hold the handle just created. For more informations on
  1431. those values, please refer to the L<"Microsoft Win32 Internet
  1432. Functions"> documentation. See also C<SetStatusCallback>.
  1433. Example:
  1434. ($status, $info) = $INET->GetStatusCallback(1);
  1435. =item HTTP httpobject, server, username, password, [port, flags, context]
  1436. =item HTTP httpobject, hashref
  1437. Opens an HTTP connection to I<server> logging in with the given
  1438. I<username> and I<password>.
  1439. The parameters and their values are:
  1440. =over
  1441. =item * server
  1442. The server to connect to. Default: I<none>.
  1443. =item * username
  1444. The username used to login to the server. Default: anonymous.
  1445. =item * password
  1446. The password used to login to the server. Default: I<none>.
  1447. =item * port
  1448. The port of the HTTP service on the server. Default: 80.
  1449. =item * flags
  1450. Additional flags affecting the behavior of the function. Default:
  1451. I<none>.
  1452. =item * context
  1453. A number to identify this operation if it is asynchronous. See
  1454. C<SetStatusCallback> and C<GetStatusCallback> for more info on
  1455. asynchronous operations. Default: I<none>.
  1456. =back
  1457. Refer to the L<"Microsoft Win32 Internet Functions"> documentation for
  1458. more details on those parameters.
  1459. If you pass I<hashref> (a reference to an hash array), the following
  1460. values are taken from the array:
  1461. %hash=(
  1462. "server" => "server",
  1463. "username" => "username",
  1464. "password" => "password",
  1465. "port" => port,
  1466. "flags" => flags,
  1467. "context" => context,
  1468. );
  1469. This method returns C<undef> if the connection failed, a number
  1470. otherwise. You can then call any of the L<"HTTP functions"> as
  1471. methods of the newly created I<httpobject>.
  1472. Example:
  1473. $result = $INET->HTTP($HTTP,"www.activeware.com","anonymous","dada\@divinf.it");
  1474. # and then for example...
  1475. ($statuscode, $headers, $file) = $HTTP->Request("/gifs/camel.gif");
  1476. $params{"server"} = "www.activeware.com";
  1477. $params{"password"} = "dada\@divinf.it";
  1478. $params{"flags"} = INTERNET_FLAG_RELOAD;
  1479. $result = $INET->HTTP($HTTP,\%params);
  1480. =item new Win32::Internet [useragent, opentype, proxy, proxybypass, flags]
  1481. =item new Win32::Internet [hashref]
  1482. Creates a new Internet object and initializes the use of the Internet
  1483. functions; this is required before any of the functions of this
  1484. package can be used. Returns C<undef> if the connection fails, a number
  1485. otherwise. The parameters and their values are:
  1486. =over
  1487. =item * useragent
  1488. The user agent passed to HTTP requests. See C<OpenRequest>. Default:
  1489. Perl-Win32::Internet/I<version>.
  1490. =item * opentype
  1491. How to access to the Internet (eg. directly or using a proxy).
  1492. Default: INTERNET_OPEN_TYPE_DIRECT.
  1493. =item * proxy
  1494. Name of the proxy server (or servers) to use. Default: I<none>.
  1495. =item * proxybypass
  1496. Optional list of host names or IP addresses, or both, that are known
  1497. locally. Default: I<none>.
  1498. =item * flags
  1499. Additional flags affecting the behavior of the function. Default:
  1500. I<none>.
  1501. =back
  1502. Refer to the L<"Microsoft Win32 Internet Functions"> documentation for
  1503. more details on those parameters. If you pass I<hashref> (a reference to
  1504. an hash array), the following values are taken from the array:
  1505. %hash=(
  1506. "useragent" => "useragent",
  1507. "opentype" => "opentype",
  1508. "proxy" => "proxy",
  1509. "proxybypass" => "proxybypass",
  1510. "flags" => flags,
  1511. );
  1512. Example:
  1513. $INET = new Win32::Internet();
  1514. die qq(Cannot connect to Internet...\n) if ! $INET;
  1515. $INET = new Win32::Internet("Mozilla/3.0", INTERNET_OPEN_TYPE_PROXY, "www.microsoft.com", "");
  1516. $params{"flags"} = INTERNET_FLAG_ASYNC;
  1517. $INET = new Win32::Internet(\%params);
  1518. =item OpenURL urlobject, URL
  1519. Opens a connection to an HTTP, FTP or GOPHER Uniform Resource Location
  1520. (URL). Returns C<undef> on errors or a number if the connection was
  1521. succesful. You can then retrieve the URL content by applying the
  1522. methods C<QueryDataAvailable> and C<ReadFile> on the newly created
  1523. I<urlobject>. See also C<FetchURL>.
  1524. Example:
  1525. $INET->OpenURL($URL, "http://www.yahoo.com/");
  1526. $bytes = $URL->QueryDataAvailable();
  1527. $file = $URL->ReadEntireFile();
  1528. $URL->Close();
  1529. =item Password [password]
  1530. Reads or sets the password used for an C<FTP> or C<HTTP> connection.
  1531. If no I<password> parameter is specified, the current value is
  1532. returned; otherwise, the password is set to I<password>. See also
  1533. C<Username>, C<QueryOption> and C<SetOption>.
  1534. Example:
  1535. $HTTP->Password("splurfgnagbxam");
  1536. $password = $HTTP->Password();
  1537. =item QueryDataAvailable
  1538. Returns the number of bytes of data that are available to be read
  1539. immediately by a subsequent call to C<ReadFile> (or C<undef> on
  1540. errors). Can be applied to URL or HTTP request objects. See
  1541. C<OpenURL> or C<OpenRequest>.
  1542. Example:
  1543. $INET->OpenURL($URL, "http://www.yahoo.com/");
  1544. $bytes = $URL->QueryDataAvailable();
  1545. =item QueryOption option
  1546. Queries an Internet option. For the possible values of I<option>,
  1547. refer to the L<"Microsoft Win32 Internet Functions"> document. See
  1548. also C<SetOption>.
  1549. Example:
  1550. $value = $INET->QueryOption(INTERNET_OPTION_CONNECT_TIMEOUT);
  1551. $value = $HTTP->QueryOption(INTERNET_OPTION_USERNAME);
  1552. =item ReadEntireFile
  1553. Reads all the data available from an opened URL or HTTP request
  1554. object. Returns what have been read or C<undef> on errors. See also
  1555. C<OpenURL>, C<OpenRequest> and C<ReadFile>.
  1556. Example:
  1557. $INET->OpenURL($URL, "http://www.yahoo.com/");
  1558. $file = $URL->ReadEntireFile();
  1559. =item ReadFile bytes
  1560. Reads I<bytes> bytes of data from an opened URL or HTTP request
  1561. object. Returns what have been read or C<undef> on errors. See also
  1562. C<OpenURL>, C<OpenRequest>, C<QueryDataAvailable> and
  1563. C<ReadEntireFile>.
  1564. B<Note:> be careful to keep I<bytes> to an acceptable value (eg. don't
  1565. tell him to swallow megabytes at once...). C<ReadEntireFile> in fact
  1566. uses C<QueryDataAvailable> and C<ReadFile> in a loop to read no more
  1567. than 16k at a time.
  1568. Example:
  1569. $INET->OpenURL($URL, "http://www.yahoo.com/");
  1570. $chunk = $URL->ReadFile(16000);
  1571. =item SetOption option, value
  1572. Sets an Internet option. For the possible values of I<option>, refer to
  1573. the L<"Microsoft Win32 Internet Functions"> document. See also
  1574. C<QueryOption>.
  1575. Example:
  1576. $INET->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,10000);
  1577. $HTTP->SetOption(INTERNET_OPTION_USERNAME,"dada");
  1578. =item SetStatusCallback
  1579. Initializes the callback routine used to return data about the
  1580. progress of an asynchronous operation.
  1581. Example:
  1582. $INET->SetStatusCallback();
  1583. This is one of the step required to perform asynchronous operations;
  1584. the complete procedure is:
  1585. # use the INTERNET_FLAG_ASYNC when initializing
  1586. $params{'flags'}=INTERNET_FLAG_ASYNC;
  1587. $INET = new Win32::Internet(\%params);
  1588. # initialize the callback routine
  1589. $INET->SetStatusCallback();
  1590. # specify the context parameter (the last 1 in this case)
  1591. $INET->HTTP($HTTP, "www.yahoo.com", "anonymous", "dada\@divinf.it", 80, 0, 1);
  1592. At this point, control returns immediately to Perl and $INET->Error()
  1593. will return 997, which means an asynchronous I/O operation is
  1594. pending. Now, you can call
  1595. $HTTP->GetStatusCallback(1);
  1596. in a loop to verify what's happening; see also C<GetStatusCallback>.
  1597. =item TimeConvert time
  1598. =item TimeConvert seconds, minute, hours, day, month, year,
  1599. day_of_week, RFC
  1600. The first form takes a HTTP date/time string and returns the date/time
  1601. converted in the following array: I<seconds, minute, hours, day,
  1602. month, year, day_of_week>.
  1603. The second form does the opposite (or at least it should, because
  1604. actually seems to be malfunctioning): it takes the values and returns
  1605. an HTTP date/time string, in the RFC format specified by the I<RFC>
  1606. parameter (OK, I didn't find yet any accepted value in the range
  1607. 0..2000, let me know if you have more luck with it).
  1608. Example:
  1609. ($sec, $min, $hour, $day, $mday, $year, $wday) =
  1610. $INET->TimeConvert("Sun, 26 Jan 1997 20:01:52 GMT");
  1611. # the opposite DOESN'T WORK! which value should $RFC have???
  1612. $time = $INET->TimeConvert(52, 1, 20, 26, 1, 1997, 0, $RFC);
  1613. =item UserAgent [name]
  1614. Reads or sets the user agent used for HTTP requests. If no I<name>
  1615. parameter is specified, the current value is returned; otherwise, the
  1616. user agent is set to I<name>. See also C<QueryOption> and
  1617. C<SetOption>.
  1618. Example:
  1619. $INET->UserAgent("Mozilla/3.0");
  1620. $useragent = $INET->UserAgent();
  1621. =item Username [name]
  1622. Reads or sets the username used for an C<FTP> or C<HTTP> connection.
  1623. If no I<name> parameter is specified, the current value is returned;
  1624. otherwise, the username is set to I<name>. See also C<Password>,
  1625. C<QueryOption> and SetOption.
  1626. Example:
  1627. $HTTP->Username("dada");
  1628. $username = $HTTP->Username();
  1629. =item Version
  1630. Returns the version numbers for the Win32::Internet package and the
  1631. WININET.DLL version, as an array or string, depending on the context.
  1632. The string returned will contain "package_version/DLL_version", while
  1633. the array will contain: "package_version", "DLL_version".
  1634. Example:
  1635. $version = $INET->Version(); # should return "0.06/4.70.1215"
  1636. @version = $INET->Version(); # should return ("0.06", "4.70.1215")
  1637. =back
  1638. =head2 FTP Functions
  1639. B<General Note>
  1640. All methods assume that you have the following lines:
  1641. use Win32::Internet;
  1642. $INET = new Win32::Internet();
  1643. $INET->FTP($FTP, "hostname", "username", "password");
  1644. somewhere before the method calls; in other words, we assume that you
  1645. have an Internet object called $INET and an open FTP session called
  1646. $FTP.
  1647. See C<new> and C<FTP> for more information.
  1648. B<Methods>
  1649. =over
  1650. =item Ascii
  1651. =item Asc
  1652. Sets the ASCII transfer mode for this FTP session. It will be applied
  1653. to the subsequent C<Get> functions. See also the C<Binary> and
  1654. C<Mode> function.
  1655. Example:
  1656. $FTP->Ascii();
  1657. =item Binary
  1658. =item Bin
  1659. Sets the binary transfer mode for this FTP session. It will be
  1660. applied to the subsequent C<Get> functions. See also the C<Ascii> and
  1661. C<Mode> function.
  1662. Example:
  1663. $FTP->Binary();
  1664. =item Cd path
  1665. =item Cwd path
  1666. =item Chdir path
  1667. Changes the current directory on the FTP remote host. Returns I<path>
  1668. or C<undef> on error.
  1669. Example:
  1670. $FTP->Cd("/pub");
  1671. =item Delete file
  1672. =item Del file
  1673. Deletes a file on the FTP remote host. Returns C<undef> on error.
  1674. Example:
  1675. $FTP->Delete("110-i86.zip");
  1676. =item Get remote, [local, overwrite, flags, context]
  1677. Gets the I<remote> FTP file and saves it locally in I<local>. If
  1678. I<local> is not specified, it will be the same name as I<remote>.
  1679. Returns C<undef> on error. The parameters and their values are:
  1680. =over
  1681. =item * remote
  1682. The name of the remote file on the FTP server. Default: I<none>.
  1683. =item * local
  1684. The name of the local file to create. Default: remote.
  1685. =item * overwrite
  1686. If 0, overwrites I<local> if it exists; with any other value, the
  1687. function fails if the I<local> file already exists. Default: 0.
  1688. =item * flags
  1689. Additional flags affecting the behavior of the function. Default:
  1690. I<none>.
  1691. =item * context
  1692. A number to identify this operation if it is asynchronous. See
  1693. C<SetStatusCallback> and C<GetStatusCallback> for more info on
  1694. asynchronous operations. Default: I<none>.
  1695. =back
  1696. Refer to the L<"Microsoft Win32 Internet Functions"> documentation for
  1697. more details on those parameters.
  1698. Example:
  1699. $FTP->Get("110-i86.zip");
  1700. $FTP->Get("/pub/perl/languages/CPAN/00index.html", "CPAN_index.html");
  1701. =item List [pattern, listmode]
  1702. =item Ls [pattern, listmode]
  1703. =item Dir [pattern, listmode]
  1704. Returns a list containing the files found in this directory,
  1705. eventually matching the given I<pattern> (which, if omitted, is
  1706. considered "*.*"). The content of the returned list depends on the
  1707. I<listmode> parameter, which can have the following values:
  1708. =over
  1709. =item * listmode=1 (or omitted)
  1710. the list contains the names of the files found. Example:
  1711. @files = $FTP->List();
  1712. @textfiles = $FTP->List("*.txt");
  1713. foreach $file (@textfiles) {
  1714. print "Name: ", $file, "\n";
  1715. }
  1716. =item * listmode=2
  1717. the list contains 7 values for each file, which respectively are:
  1718. =over
  1719. =item * the file name
  1720. =item * the DOS short file name, aka 8.3
  1721. =item * the size
  1722. =item * the attributes
  1723. =item * the creation time
  1724. =item * the last access time
  1725. =item * the last modified time
  1726. =back
  1727. Example:
  1728. @files = $FTP->List("*.*", 2);
  1729. for($i=0; $i<=$#files; $i+=7) {
  1730. print "Name: ", @files[$i], "\n";
  1731. print "Size: ", @files[$i+2], "\n";
  1732. print "Attr: ", @files[$i+3], "\n";
  1733. }
  1734. =item * listmode=3
  1735. the list contains a reference to an hash array for each found file;
  1736. each hash contains:
  1737. =over
  1738. =item * name => the file name
  1739. =item * altname => the DOS short file name, aka 8.3
  1740. =item * size => the size
  1741. =item * attr => the attributes
  1742. =item * ctime => the creation time
  1743. =item * atime => the last access time
  1744. =item * mtime => the last modified time
  1745. =back
  1746. Example:
  1747. @files = $FTP->List("*.*", 3);
  1748. foreach $file (@files) {
  1749. print $file->{'name'}, " ", $file->{'size'}, " ", $file->{'attr'}, "\n";
  1750. }
  1751. B<Note:> all times are reported as strings of the following format:
  1752. I<second, hour, minute, day, month, year>.
  1753. Example:
  1754. $file->{'mtime'} == "0,10,58,9,12,1996" stands for 09 Dec 1996 at 10:58:00
  1755. =back
  1756. =item Mkdir name
  1757. =item Md name
  1758. Creates a directory on the FTP remote host. Returns C<undef> on error.
  1759. Example:
  1760. $FTP->Mkdir("NextBuild");
  1761. =item Mode [mode]
  1762. If called with no arguments, returns the current transfer mode for
  1763. this FTP session ("asc" for ASCII or "bin" for binary). The I<mode>
  1764. argument can be "asc" or "bin", in which case the appropriate transfer
  1765. mode is selected. See also the Ascii and Binary functions. Returns
  1766. C<undef> on errors.
  1767. Example:
  1768. print "Current mode is: ", $FTP->Mode();
  1769. $FTP->Mode("asc"); # ... same as $FTP->Ascii();
  1770. =item Pasv [mode]
  1771. If called with no arguments, returns 1 if the current FTP session has
  1772. passive transfer mode enabled, 0 if not.
  1773. You can call it with a I<mode> parameter (0/1) only as a method of a
  1774. Internet object (see C<new>), in which case it will set the default
  1775. value for the next C<FTP> objects you create (read: set it before,
  1776. because you can't change this value once you opened the FTP session).
  1777. Example:
  1778. print "Pasv is: ", $FTP->Pasv();
  1779. $INET->Pasv(1);
  1780. $INET->FTP($FTP,"ftp.activeware.com", "anonymous", "dada\@divinf.it");
  1781. $FTP->Pasv(0); # this will be ignored...
  1782. =item Put local, [remote, context]
  1783. Upload the I<local> file to the FTP server saving it under the name
  1784. I<remote>, which if if omitted is the same name as I<local>. Returns
  1785. C<undef> on error.
  1786. I<context> is a number to identify this operation if it is asynchronous.
  1787. See C<SetStatusCallback> and C<GetStatusCallback> for more info on
  1788. asynchronous operations.
  1789. Example:
  1790. $FTP->Put("internet.zip");
  1791. $FTP->Put("d:/users/dada/temp.zip", "/temp/dada.zip");
  1792. =item Pwd
  1793. Returns the current directory on the FTP server or C<undef> on errors.
  1794. Example:
  1795. $path = $FTP->Pwd();
  1796. =item Rename oldfile, newfile
  1797. =item Ren oldfile, newfile
  1798. Renames a file on the FTP remote host. Returns C<undef> on error.
  1799. Example:
  1800. $FTP->Rename("110-i86.zip", "68i-011.zip");
  1801. =item Rmdir name
  1802. =item Rd name
  1803. Removes a directory on the FTP remote host. Returns C<undef> on error.
  1804. Example:
  1805. $FTP->Rmdir("CurrentBuild");
  1806. =back
  1807. =head2 HTTP Functions
  1808. B<General Note>
  1809. All methods assume that you have the following lines:
  1810. use Win32::Internet;
  1811. $INET = new Win32::Internet();
  1812. $INET->HTTP($HTTP, "hostname", "username", "password");
  1813. somewhere before the method calls; in other words, we assume that you
  1814. have an Internet object called $INET and an open HTTP session called
  1815. $HTTP.
  1816. See C<new> and C<HTTP> for more information.
  1817. B<Methods>
  1818. =over
  1819. =item AddHeader header, [flags]
  1820. Adds HTTP request headers to an HTTP request object created with
  1821. C<OpenRequest>. For the possible values of I<flags> refer to the
  1822. L<"Microsoft Win32 Internet Functions"> document.
  1823. Example:
  1824. $HTTP->OpenRequest($REQUEST,"/index.html");
  1825. $REQUEST->AddHeader("If-Modified-Since: Sunday, 17-Nov-96 11:40:03 GMT");
  1826. $REQUEST->AddHeader("Accept: text/html\r\n", HTTP_ADDREQ_FLAG_REPLACE);
  1827. =item OpenRequest requestobject, [path, method, version, referer, accept, flags, context]
  1828. =item OpenRequest requestobject, hashref
  1829. Opens an HTTP request. Returns C<undef> on errors or a number if the
  1830. connection was succesful. You can then use one of the C<AddHeader>,
  1831. C<SendRequest>, C<QueryInfo>, C<QueryDataAvailable> and C<ReadFile>
  1832. methods on the newly created I<requestobject>. The parameters and
  1833. their values are:
  1834. =over
  1835. =item * path
  1836. The object to request. This is generally a file name, an executable
  1837. module, etc. Default: /
  1838. =item * method
  1839. The method to use; can actually be GET, POST, HEAD or PUT. Default:
  1840. GET
  1841. =item * version
  1842. The HTTP version. Default: HTTP/1.0
  1843. =item * referer
  1844. The URL of the document from which the URL in the request was
  1845. obtained. Default: I<none>
  1846. =item * accept
  1847. The content types accepted. They must be separated by a "\0" (ASCII
  1848. zero). Default: text/* image/gif image/jpeg
  1849. =item * flags
  1850. Additional flags affecting the behavior of the function. Default:
  1851. I<none>
  1852. =item * context
  1853. A number to identify this operation if it is asynchronous. See
  1854. C<SetStatusCallback> and C<GetStatusCallback> for more info on
  1855. asynchronous operations. Default: I<none>
  1856. =back
  1857. Refer to the L<"Microsoft Win32 Internet Functions"> documentation for
  1858. more details on those parameters. If you pass I<hashref> (a reference to
  1859. an hash array), the following values are taken from the array:
  1860. %hash=(
  1861. "path" => "path",
  1862. "method" => "method",
  1863. "version" => "version",
  1864. "referer" => "referer",
  1865. "accept" => "accept",
  1866. "flags" => flags,
  1867. "context" => context,
  1868. );
  1869. See also C<Request>.
  1870. Example:
  1871. $HTTP->OpenRequest($REQUEST, "/index.html");
  1872. $HTTP->OpenRequest($REQUEST, "/index.html", "GET", "HTTP/0.9");
  1873. $params{"path"} = "/index.html";
  1874. $params{"flags"} = "
  1875. $HTTP->OpenRequest($REQUEST, \%params);
  1876. =item QueryInfo header, [flags]
  1877. Queries information about an HTTP request object created with
  1878. C<OpenRequest>. You can specify an I<header> (for example,
  1879. "Content-type") and/or one or more I<flags>. If you don't specify
  1880. I<flags>, HTTP_QUERY_CUSTOM will be used by default; this means that
  1881. I<header> should contain a valid HTTP header name. For the possible
  1882. values of I<flags> refer to the L<"Microsoft Win32 Internet
  1883. Functions"> document.
  1884. Example:
  1885. $HTTP->OpenRequest($REQUEST,"/index.html");
  1886. $statuscode = $REQUEST->QueryInfo("", HTTP_QUERY_STATUS_CODE);
  1887. $headers = $REQUEST->QueryInfo("", HTTP_QUERY_RAW_HEADERS_CRLF); # will get all the headers
  1888. $length = $REQUEST->QueryInfo("Content-length");
  1889. =item Request [path, method, version, referer, accept, flags]
  1890. =item Request hashref
  1891. Performs an HTTP request and returns an array containing the status
  1892. code, the headers and the content of the file. It is a one-step
  1893. procedure that makes an C<OpenRequest>, a C<SendRequest>, some
  1894. C<QueryInfo>, C<ReadFile> and finally C<Close>. For a description of
  1895. the parameters, see C<OpenRequest>.
  1896. Example:
  1897. ($statuscode, $headers, $file) = $HTTP->Request("/index.html");
  1898. ($s, $h, $f) = $HTTP->Request("/index.html", "GET", "HTTP/1.0");
  1899. =item SendRequest [postdata]
  1900. Send an HTTP request to the destination server. I<postdata> are any
  1901. optional data to send immediately after the request header; this is
  1902. generally used for POST or PUT requests. See also C<OpenRequest>.
  1903. Example:
  1904. $HTTP->OpenRequest($REQUEST, "/index.html");
  1905. $REQUEST->SendRequest();
  1906. # A POST request...
  1907. $HTTP->OpenRequest($REQUEST, "/cgi-bin/somescript.pl", "POST");
  1908. #This line is a must -> (thanks Philip :)
  1909. $REQUEST->AddHeader("Content-Type: application/x-www-form-urlencoded");
  1910. $REQUEST->SendRequest("key1=value1&key2=value2&key3=value3");
  1911. =back
  1912. =head1 APPENDIX
  1913. =head2 Microsoft Win32 Internet Functions
  1914. Complete documentation for the Microsoft Win32 Internet Functions can
  1915. be found, in both HTML and zipped Word format, at this address:
  1916. http://www.microsoft.com/intdev/sdk/docs/wininet/
  1917. =head2 Functions Table
  1918. This table reports the correspondence between the functions offered by
  1919. WININET.DLL and their implementation in the Win32::Internet
  1920. extension. Functions showing a "---" are not currently
  1921. implemented. Functions enclosed in parens ( ) aren't implemented
  1922. straightforwardly, but in a higher-level routine, eg. together with
  1923. other functions.
  1924. WININET.DLL Win32::Internet
  1925. InternetOpen new Win32::Internet
  1926. InternetConnect FTP / HTTP
  1927. InternetCloseHandle Close
  1928. InternetQueryOption QueryOption
  1929. InternetSetOption SetOption
  1930. InternetSetOptionEx ---
  1931. InternetSetStatusCallback SetStatusCallback
  1932. InternetStatusCallback GetStatusCallback
  1933. InternetConfirmZoneCrossing ---
  1934. InternetTimeFromSystemTime TimeConvert
  1935. InternetTimeToSystemTime TimeConvert
  1936. InternetAttemptConnect ---
  1937. InternetReadFile ReadFile
  1938. InternetSetFilePointer ---
  1939. InternetFindNextFile (List)
  1940. InternetQueryDataAvailable QueryDataAvailable
  1941. InternetGetLastResponseInfo GetResponse
  1942. InternetWriteFile ---
  1943. InternetCrackUrl CrackURL
  1944. InternetCreateUrl CreateURL
  1945. InternetCanonicalizeUrl CanonicalizeURL
  1946. InternetCombineUrl CombineURL
  1947. InternetOpenUrl OpenURL
  1948. FtpFindFirstFile (List)
  1949. FtpGetFile Get
  1950. FtpPutFile Put
  1951. FtpDeleteFile Delete
  1952. FtpRenameFile Rename
  1953. FtpOpenFile ---
  1954. FtpCreateDirectory Mkdir
  1955. FtpRemoveDirectory Rmdir
  1956. FtpSetCurrentDirectory Cd
  1957. FtpGetCurrentDirectory Pwd
  1958. HttpOpenRequest OpenRequest
  1959. HttpAddRequestHeaders AddHeader
  1960. HttpSendRequest SendRequest
  1961. HttpQueryInfo QueryInfo
  1962. InternetErrorDlg ---
  1963. Actually, I don't plan to add support for Gopher, Cookie and Cache
  1964. functions. I will if there will be consistent requests to do so.
  1965. There are a number of higher-level functions in the Win32::Internet
  1966. that simplify some usual procedures, calling more that one WININET API
  1967. function. This table reports those functions and the relative WININET
  1968. functions they use.
  1969. Win32::Internet WININET.DLL
  1970. FetchURL InternetOpenUrl
  1971. InternetQueryDataAvailable
  1972. InternetReadFile
  1973. InternetCloseHandle
  1974. ReadEntireFile InternetQueryDataAvailable
  1975. InternetReadFile
  1976. Request HttpOpenRequest
  1977. HttpSendRequest
  1978. HttpQueryInfo
  1979. InternetQueryDataAvailable
  1980. InternetReadFile
  1981. InternetCloseHandle
  1982. List FtpFindFirstFile
  1983. InternetFindNextFile
  1984. =head2 Constants
  1985. Those are the constants exported by the package in the main namespace
  1986. (eg. you can use them in your scripts); for their meaning and proper
  1987. use, refer to the Microsoft Win32 Internet Functions document.
  1988. HTTP_ADDREQ_FLAG_ADD
  1989. HTTP_ADDREQ_FLAG_REPLACE
  1990. HTTP_QUERY_ALLOW
  1991. HTTP_QUERY_CONTENT_DESCRIPTION
  1992. HTTP_QUERY_CONTENT_ID
  1993. HTTP_QUERY_CONTENT_LENGTH
  1994. HTTP_QUERY_CONTENT_TRANSFER_ENCODING
  1995. HTTP_QUERY_CONTENT_TYPE
  1996. HTTP_QUERY_COST
  1997. HTTP_QUERY_CUSTOM
  1998. HTTP_QUERY_DATE
  1999. HTTP_QUERY_DERIVED_FROM
  2000. HTTP_QUERY_EXPIRES
  2001. HTTP_QUERY_FLAG_REQUEST_HEADERS
  2002. HTTP_QUERY_FLAG_SYSTEMTIME
  2003. HTTP_QUERY_LANGUAGE
  2004. HTTP_QUERY_LAST_MODIFIED
  2005. HTTP_QUERY_MESSAGE_ID
  2006. HTTP_QUERY_MIME_VERSION
  2007. HTTP_QUERY_PRAGMA
  2008. HTTP_QUERY_PUBLIC
  2009. HTTP_QUERY_RAW_HEADERS
  2010. HTTP_QUERY_RAW_HEADERS_CRLF
  2011. HTTP_QUERY_REQUEST_METHOD
  2012. HTTP_QUERY_SERVER
  2013. HTTP_QUERY_STATUS_CODE
  2014. HTTP_QUERY_STATUS_TEXT
  2015. HTTP_QUERY_URI
  2016. HTTP_QUERY_USER_AGENT
  2017. HTTP_QUERY_VERSION
  2018. HTTP_QUERY_WWW_LINK
  2019. ICU_BROWSER_MODE
  2020. ICU_DECODE
  2021. ICU_ENCODE_SPACES_ONLY
  2022. ICU_ESCAPE
  2023. ICU_NO_ENCODE
  2024. ICU_NO_META
  2025. ICU_USERNAME
  2026. INTERNET_FLAG_PASSIVE
  2027. INTERNET_FLAG_ASYNC
  2028. INTERNET_FLAG_HYPERLINK
  2029. INTERNET_FLAG_KEEP_CONNECTION
  2030. INTERNET_FLAG_MAKE_PERSISTENT
  2031. INTERNET_FLAG_NO_AUTH
  2032. INTERNET_FLAG_NO_AUTO_REDIRECT
  2033. INTERNET_FLAG_NO_CACHE_WRITE
  2034. INTERNET_FLAG_NO_COOKIES
  2035. INTERNET_FLAG_READ_PREFETCH
  2036. INTERNET_FLAG_RELOAD
  2037. INTERNET_FLAG_RESYNCHRONIZE
  2038. INTERNET_FLAG_TRANSFER_ASCII
  2039. INTERNET_FLAG_TRANSFER_BINARY
  2040. INTERNET_INVALID_PORT_NUMBER
  2041. INTERNET_INVALID_STATUS_CALLBACK
  2042. INTERNET_OPEN_TYPE_DIRECT
  2043. INTERNET_OPEN_TYPE_PROXY
  2044. INTERNET_OPEN_TYPE_PROXY_PRECONFIG
  2045. INTERNET_OPTION_CONNECT_BACKOFF
  2046. INTERNET_OPTION_CONNECT_RETRIES
  2047. INTERNET_OPTION_CONNECT_TIMEOUT
  2048. INTERNET_OPTION_CONTROL_SEND_TIMEOUT
  2049. INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT
  2050. INTERNET_OPTION_DATA_SEND_TIMEOUT
  2051. INTERNET_OPTION_DATA_RECEIVE_TIMEOUT
  2052. INTERNET_OPTION_HANDLE_TYPE
  2053. INTERNET_OPTION_LISTEN_TIMEOUT
  2054. INTERNET_OPTION_PASSWORD
  2055. INTERNET_OPTION_READ_BUFFER_SIZE
  2056. INTERNET_OPTION_USER_AGENT
  2057. INTERNET_OPTION_USERNAME
  2058. INTERNET_OPTION_VERSION
  2059. INTERNET_OPTION_WRITE_BUFFER_SIZE
  2060. INTERNET_SERVICE_FTP
  2061. INTERNET_SERVICE_GOPHER
  2062. INTERNET_SERVICE_HTTP
  2063. INTERNET_STATUS_CLOSING_CONNECTION
  2064. INTERNET_STATUS_CONNECTED_TO_SERVER
  2065. INTERNET_STATUS_CONNECTING_TO_SERVER
  2066. INTERNET_STATUS_CONNECTION_CLOSED
  2067. INTERNET_STATUS_HANDLE_CLOSING
  2068. INTERNET_STATUS_HANDLE_CREATED
  2069. INTERNET_STATUS_NAME_RESOLVED
  2070. INTERNET_STATUS_RECEIVING_RESPONSE
  2071. INTERNET_STATUS_REDIRECT
  2072. INTERNET_STATUS_REQUEST_COMPLETE
  2073. INTERNET_STATUS_REQUEST_SENT
  2074. INTERNET_STATUS_RESOLVING_NAME
  2075. INTERNET_STATUS_RESPONSE_RECEIVED
  2076. INTERNET_STATUS_SENDING_REQUEST
  2077. =head1 VERSION HISTORY
  2078. =over
  2079. =item * 0.082 (4 Sep 2001)
  2080. =over
  2081. =item *
  2082. Fix passive FTP mode. INTERNET_FLAG_PASSIVE was misspelled in earlier
  2083. versions (as INTERNET_CONNECT_FLAG_PASSIVE) and wouldn't work. Found
  2084. by Steve Raynesford <[email protected]>.
  2085. =back
  2086. =item * 0.081 (25 Sep 1999)
  2087. =over
  2088. =item *
  2089. Documentation converted to pod format by Jan Dubois <[email protected]>.
  2090. =item *
  2091. Minor changes from Perl 5.005xx compatibility.
  2092. =back
  2093. =item * 0.08 (14 Feb 1997)
  2094. =over
  2095. =item *
  2096. fixed 2 more bugs in Option(s) related subs (thanks to Brian
  2097. Helterline!).
  2098. =item *
  2099. Error() now gets error messages directly from WININET.DLL.
  2100. =item *
  2101. The PLL file now comes in 2 versions, one for Perl version 5.001
  2102. (build 100) and one for Perl version 5.003 (build 300 and
  2103. higher). Everybody should be happy now :)
  2104. =item *
  2105. added an installation program.
  2106. =back
  2107. =item * 0.07 (10 Feb 1997)
  2108. =over
  2109. =item *
  2110. fixed a bug in Version() introduced with 0.06...
  2111. =item *
  2112. completely reworked PM file, fixed *lots* of minor bugs, and removed
  2113. almost all the warnings with "perl -w".
  2114. =back
  2115. =item * 0.06 (26 Jan 1997)
  2116. =over
  2117. =item *
  2118. fixed another hideous bug in "new" (the 'class' parameter was still
  2119. missing).
  2120. =item *
  2121. added support for asynchronous operations (work still in embryo).
  2122. =item *
  2123. removed the ending \0 (ASCII zero) from the DLL version returned by
  2124. "Version".
  2125. =item *
  2126. added a lot of constants.
  2127. =item *
  2128. added safefree() after every safemalloc() in C... wonder why I didn't
  2129. it before :)
  2130. =item *
  2131. added TimeConvert, which actually works one way only.
  2132. =back
  2133. =item * 0.05f (29 Nov 1996)
  2134. =over
  2135. =item *
  2136. fixed a bug in "new" (parameters passed were simply ignored).
  2137. =item *
  2138. fixed another bug: "Chdir" and "Cwd" were aliases of RMDIR instead of
  2139. CD..
  2140. =back
  2141. =item * 0.05 (29 Nov 1996)
  2142. =over
  2143. =item *
  2144. added "CrackURL" and "CreateURL".
  2145. =item *
  2146. corrected an error in TEST.PL (there was a GetUserAgent instead of
  2147. UserAgent).
  2148. =back
  2149. =item * 0.04 (25 Nov 1996)
  2150. =over
  2151. =item *
  2152. added "Version" to retrieve package and DLL versions.
  2153. =item *
  2154. added proxies and other options to "new".
  2155. =item *
  2156. changed "OpenRequest" and "Request" to read parameters from a hash.
  2157. =item *
  2158. added "SetOption/QueryOption" and a lot of relative functions
  2159. (connect, username, password, useragent, etc.).
  2160. =item *
  2161. added "CanonicalizeURL" and "CombineURL".
  2162. =item *
  2163. "Error" covers a wider spectrum of errors.
  2164. =back
  2165. =item * 0.02 (18 Nov 1996)
  2166. =over
  2167. =item *
  2168. added support for HTTP sessions and requests.
  2169. =back
  2170. =item * 0.01 (11 Nov 1996)
  2171. =over
  2172. =item *
  2173. fetching of HTTP, FTP and GOPHER URLs.
  2174. =item *
  2175. complete set of commands to manage an FTP session.
  2176. =back
  2177. =back
  2178. =head1 AUTHOR
  2179. Version 0.08 (14 Feb 1997) by Aldo Calpini <[email protected]>
  2180. =head1 CREDITS
  2181. Win32::Internet is based on the Win32::Registry code written by Jesse
  2182. Dougherty.
  2183. Additional thanks to: Carl Tichler for his help in the initial
  2184. development; Tore Haraldsen, Brian Helterline for the bugfixes; Dave
  2185. Roth for his great source code examples.
  2186. =head1 DISCLAIMER
  2187. This program is FREE; you can redistribute, modify, disassemble, or
  2188. even reverse engineer this software at your will. Keep in mind,
  2189. however, that NOTHING IS GUARANTEED to work and everything you do is
  2190. AT YOUR OWN RISK - I will not take responsability for any damage, loss
  2191. of money and/or health that may arise from the use of this program!
  2192. This is distributed under the terms of Larry Wall's Artistic License.