<commit_before>package RSGet::HTTPServer;

use strict;
use warnings;
use IO::Socket;
use RSGet::Tools;
use RSGet::HTTPRequest;
set_rev qq$Id$;

sub new
{
	my $class = shift;
	my $port = shift;
	my $socket = IO::Socket::INET->new(
		Proto => 'tcp',
		LocalPort => $port,
		Listen => SOMAXCONN,
		Reuse => 1,
		Blocking => 0,
	) || return undef;

	my $self = \$socket;
	return bless $self, $class;
}

sub perform
{
	my $self = shift;
	my $socket = $$self;

	for ( my $i = 0; $i < 5; $i++ ) {
		my $client = $socket->accept() or last;
		last unless request( $client );
	}
}

sub request
{
	my $client = shift;
	my $request;
	my @headers;
	my $post = "";
	my $OK = 0;
	eval {
		local $SIG{ALRM} = sub { die "HTTP: Frozen !\n"; };
		alarm 2;
		$request = <$client>;

		my $len = 0;
		while ( $_ = <$client> ) {
			if ( /^\s*$/ ) {
				$OK = 1;
				last;
			}
			push @headers, $_;
			$len = $1 if /^Content-Length:\s*(\d+)/i;
		}

		$client->read( $post, $len ) if $len;
		$OK++;
	};
	alarm 0;
	if ( $@ ) {
		warn "HTTP error: $@\n" unless $@ eq "HTTP: Frozen !\n";
		close $client;
		return undef;
	}
	unless ( $OK == 2 ) {
		warn "Some HTTP problem\n";
		close $client;
		return undef;
	}
	
	my( $method, $file, $ignore ) = split /\s+/, $request;
	$file =~ s#^/+##;

	my %post;
	if ( uc $method eq "POST" and length $post ) {
		foreach ( split /&/, $post ) {
			s/^(.*?)=//;
			my $key = $1;
			tr/+/ /;
			s/%(..)/chr hex $1/eg;
			$post{ $key } = $_;
		}
	} elsif ( $file =~ s/\?(.*)// ) {
		my $get = $1;
		%post = map /^(.*?)=(.*)/, split /;+/, $get;
	}
	my $print;
	if ( my $func = $RSGet::HTTPRequest::handlers{$file} ) {
		$print = "HTTP/1.1 200 OK\r\n";<commit_msg>- unescape GET query arguments<commit_after>package RSGet::HTTPServer;

use strict;
use warnings;
use IO::Socket;
use RSGet::Tools;
use RSGet::HTTPRequest;
use URI::Escape;
set_rev qq$Id$;

sub new
{
	my $class = shift;
	my $port = shift;
	my $socket = IO::Socket::INET->new(
		Proto => 'tcp',
		LocalPort => $port,
		Listen => SOMAXCONN,
		Reuse => 1,
		Blocking => 0,
	) || return undef;

	my $self = \$socket;
	return bless $self, $class;
}

sub perform
{
	my $self = shift;
	my $socket = $$self;

	for ( my $i = 0; $i < 5; $i++ ) {
		my $client = $socket->accept() or last;
		last unless request( $client );
	}
}

sub request
{
	my $client = shift;
	my $request;
	my @headers;
	my $post = "";
	my $OK = 0;
	eval {
		local $SIG{ALRM} = sub { die "HTTP: Frozen !\n"; };
		alarm 2;
		$request = <$client>;

		my $len = 0;
		while ( $_ = <$client> ) {
			if ( /^\s*$/ ) {
				$OK = 1;
				last;
			}
			push @headers, $_;
			$len = $1 if /^Content-Length:\s*(\d+)/i;
		}

		$client->read( $post, $len ) if $len;
		$OK++;
	};
	alarm 0;
	if ( $@ ) {
		warn "HTTP error: $@\n" unless $@ eq "HTTP: Frozen !\n";
		close $client;
		return undef;
	}
	unless ( $OK == 2 ) {
		warn "Some HTTP problem\n";
		close $client;
		return undef;
	}
	
	my( $method, $file, $ignore ) = split /\s+/, $request;
	$file =~ s#^/+##;

	my %post;
	if ( uc $method eq "POST" and length $post ) {
		foreach ( split /&/, $post ) {
			s/^(.*?)=//;
			my $key = $1;
			tr/+/ /;
			s/%(..)/chr hex $1/eg;
			$post{ $key } = $_;
		}
	} elsif ( $file =~ s/\?(.*)// ) {
		my $get = $1;
		%post = map { /^(.*?)=(.*)/; (uri_unescape( $1 ), uri_unescape( $2 )) } split /;+/, $get;
	}
	my $print;
	if ( my $func = $RSGet::HTTPRequest::handlers{$file} ) {
		$print = "HTTP/1.1 200 OK\r\n";
{'fraction_non_alphanumeric': 0.23076923076923078, 'fraction_numerical': 0.011699327288680901, 'mean_word_length': 2.6228813559322033, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2533570', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1476, 'n_words': 399}