Gazolinia - Life of Software Engineer
Creators of future world....
Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

A TRACE method HTTP request initiates a "loopback" diagnostic at the destination server. The destination server bounces back a TRACE response, with the virgin request message it received in the body of its response. A client can then check whether the original request correctly reached destination server or somewhere got changed by proxy or others.

The following diagram clearly explains the TRACE request :



The TRACE method is used primarily for diagnostics; i.e., verifying that requests are going through the request/response chain as intended. It's also a good tool for seeing the effects of proxies and other applications on your requests. For more click here

The simple perl program implementation of http TRACE method is as follows :
#!/usr/local/bin/perl -w
use strict;
use HTTP::Status;
use HTTP::Response;
use LWP::UserAgent;
use URI::URL;

sub sendandreceive( ) {

my ($method, $path) = @_;

# Create a User Agent object
my $ua = new LWP::UserAgent;
$ua->agent("hcat/1.0");

# Ask the User Agent object to request a URL.
# Results go into the response object (HTTP::Reponse).

my $request = new HTTP::Request($method,$path);
my $response = $ua->request($request);

# Parse/convert the response object for "easier reading"

my $code=$response->code;
my $desc = HTTP::Status::status_message($code);
my $headers=$response->headers_as_string;
my $body = $response->content;
$body = $response->error_as_HTML if ($response->is_error);
return ($code, $desc, $headers, $body);
}

print "Enter the url \n";
my $url=<STDIN>;
chomp $url;
my ($code, $desc, $headers, $body)=&sendandreceive('GET', $url);
print $code,"\n",$headers,"\n",$desc,"\n",$body,"\n";

Bookmark and Share

 

This simple program on execution ask for url , on giving the url it sends the http request to the particular server and get the response.

#!/usr/local/bin/perl -w
use strict;
use HTTP::Status;
use HTTP::Response;
use LWP::UserAgent;
use URI::URL;

sub sendandreceive( ) {

my ($method, $path) = @_;

# Create a User Agent object
my $ua = new LWP::UserAgent;
$ua->agent("hcat/1.0");

# Ask the User Agent object to request a URL.
# Results go into the response object (HTTP::Reponse).

my $request = new HTTP::Request($method,$path);
my $response = $ua->request($request);

# Parse/convert the response object for "easier reading"

my $code=$response->code;
my $desc = HTTP::Status::status_message($code);
my $headers=$response->headers_as_string;
my $body = $response->content;
$body = $response->error_as_HTML if ($response->is_error);
return ($code, $desc, $headers, $body);
}

print "Enter the url \n";
my $url=;
chomp $url;
my ($code, $desc, $headers, $body)=&sendandreceive('GET', $url);

#you can print and see the variables $code $desc $headers $body

Bookmark and Share

 

The following is a simple perl code to open a file and read line by line .

#!/usr/local/bin/perl -w

open("FILE","%filename");
my line;

while(defined($line=%FILE*))
{
print FILE $line;
}

close(FILE);

The following is a simple perl code to open a file and read line by line and write the content to another file .


#!/usr/local/bin/perl -w

open(FILE_READ,"%filename1");
open(FILE_WRITE,"%filename2");
my $line;
while(defined($line=%file_read*))
{
print FILE_WRITE $line;
}

close(FILE_READ);
close(FILE_WRITE);

Note: For the above 2 programs change % with <>.

Perl is case sensitive , so please be careful while writing the program .

Bookmark and Share