Commit e3d3b036 authored by rygelgit's avatar rygelgit

Added logic to handle chunked response for device xml.

Chunk response has <chunk-size followed by \r\n>
parent 4b5d1d9b
#!/usr/bin/perl
use strict;
use warnings;
# temp.txt will have the device xml response from server.
my $filename = 'temp.txt';
open(my $fh, '<', $filename) or die "Could not open file '$filename' $!";
print "done opening file\n";
my $temp_content;
while (<$fh>) {
my $chunked = $_;
$chunked =~ s/^([0-9]+)\r\n//;
print $chunked;
$temp_content .= $chunked;
}
print "\n\n\n NEW CONTENT : $temp_content\n";
close $fh;
1;
......@@ -10,7 +10,17 @@ use Net::UPnP::HTTP;
use Net::UPnP::Device;
sub search {
my $ssdp_header = "M-SEARCH * HTTP/1.1\r\nST: upnp:rootdevice\r\nMX: 5\r\nMAN: \"ssdp:discover\"\r\nHOST: $Net::UPnP::SSDP_ADDR:$Net::UPnP::SSDP_PORT\r\nContent-Length: 0\r\n\r\n";
# Few clients don't understand the HOST header.
#my $ssdp_header = "M-SEARCH * HTTP/1.1\r\nST: upnp:rootdevice\r\nMan: \"ssdp:discover\"\r\nMX: 5\r\nHost: $Net::UPnP::SSDP_ADDR:$Net::UPnP::SSDP_PORT\r\n\r\n";
my $ssdp_header = "M-SEARCH * HTTP/1.1\r\nST: upnp:rootdevice\r\nMan: \"ssdp:discover\"\r\nMX: 5\r\n\r\n";
# The below format fails in some clients. But keeping it for now.
#my $ssdp_header = "M-SEARCH * HTTP/1.1\r\n
#ST: upnp:rootdevice\r\n
#Man: \"ssdp:discover\"\r\n
#MX: 5\r\n
#Host: $Net::UPnP::SSDP_ADDR:$Net::UPnP::SSDP_PORT\r\n\r\n";
print "******************** SSDP Message ********************\n";
print $ssdp_header;
......@@ -47,10 +57,34 @@ while( select($reader_output = $reader_input, undef, undef, 10) ) {
my $post_response = $http_request->post($host_address, $host_port, "GET", $dev_path, "", "");
my $post_content = $post_response->getcontent();
#print "$post_content";
#print $post_content;
my $dev = Net::UPnP::Device->new();
$dev->setssdp($ssdp_response);
$dev->setdescription($post_content);
# Handle Chunked response from server.
if (index(lc($post_content),"transfer-encoding: chunked") != -1) {
$post_content = substr($post_content,index($post_content,"<?xml version=\"1.0\" encoding=\"utf-8\" ?>") ,length($post_content));
my $filename = 'temp.txt';
open(my $fh, '<', $filename) or die "Could not open file '$filename' $!";
my $chunked_content;
while (<$fh>) {
my $replace_str = $_;
$replace_str =~ s/^([0-9]+)\r\n//;
print $replace_str;
$chunked_content .= $replace_str;
}
$chunked_content =~ s/\r\n//g;
$dev->setdescription($chunked_content);
# Close and delete the file
close $fh;
unlink $fh;
} else {
# Non-Chunked response.
$dev->setdescription($post_content);
}
push(@device_list, $dev);
push(@device_addr,$host_address);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment