# FHTTP Kit by Xianur0
# Copyright (C) 2011 Oscar García López (http://hackingtelevision.blogspot.com) 
#  
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#   
#    xianur0.null@gmail.com
#    http://hackingtelevision.blogspot.com/

package parser;

my $paquete;
my @encabezadoss = ("User-Agent: Mozilla/5.0 (X11; U; Linux i686; es-ES; rv:1.9.1.8) Gecko/20100214 Ubuntu/9.10 (karmic) Firefox/3.5.8");


sub new  {
my $class = shift;
my $self={};
bless $self, $class;
return $self;
}

sub soportados {
	my ($self,$lineaurl,$host,$puerto,$ssl,$max, $mmetodo,$proxy,$pproxy) = @_;
	my $metodo = ($mmetodo eq "") ? "GET" : $mmetodo;
	my $tmppaquete = http->new($metodo,$lineaurl,"1.1");
	my $cantidad = ($max > 0) ? $max : 100;
	$tmppaquete->agregarencabezados(0,@encabezadoss);
	$tmppaquete->modo(1);
	for($i = 0; $i < $cantidad; $i++) {
		$tmppaquete->siguiente($metodo,$lineaurl,"1.1")
	}
	my %estructuratmp = $tmppaquete->enviar((($proxy eq "") ? $host : $proxy), (($pproxy eq "") ? $puerto : $pproxy),$ssl);
	return ($estructuratmp{total},%estructuratmp);
}

sub parsear {
my($paquete,$http,@respuestas) = @_;
my %encabezados = ();
my $retornar = "";
my $largosalto = 4;
$http =~ s/^(\r\n|\n)//g;
@parteshttp = split(/\r?\n\r?\n/,$http);
@encabezadosa = split(/\r?\n/,$parteshttp[0]);
#if($http =~ /\r\n\r\n/)
foreach $encabezado (@encabezadosa) {
($name,$valor) = split(": ",$encabezado);
$name =~ s/^Content-Length$/Content-Length/i;
$encabezados{$name} = $valor;
}
$largoinicial = length($parteshttp[0])+4;
$retornar = $parteshttp[0]."\r\n\r\n";
if($encabezados{'Content-Length'} ne "") {
my $resto  = substr($http, $largoinicial, $encabezados{'Content-Length'});
$retornar .= $resto;
push(@respuestas, $retornar);
$total = $largoinicial + $encabezados{'Content-Length'};
my $resto = substr($http, $total);
if($resto ne "") {
@respuestas = $paquete->parsear($resto,@respuestas);
}
}
else {
$temporal = $http;
$temporal =~ s/^HTTP\/1\.1//i;
$ubicacion = index($temporal,"\r\nHTTP/1.1 ");
if($ubicacion eq "-1") { push(@respuestas, $http); }
else {
my $primera = substr($http, 0, $ubicacion+10);
push(@respuestas, $primera);
my $resto = substr($temporal, $ubicacion);
if($resto ne "") {
@respuestas = $paquete->parsear($resto,@respuestas);
}
}}
return @respuestas;
}

1;
