From 46c4184bc54fa4783037e252d5fa5513528cf03c Mon Sep 17 00:00:00 2001 From: b Date: Tue, 1 Dec 2015 23:40:42 +0000 Subject: [PATCH] proxy: (de)compression. library: cnofig file reading html entity decoding git-svn-id: svn://botcastle1b/yplom/proxy@12 05ce6ebb-7522-4a6e-a768-0026ae12be9f --- cleararch.1.pl | 4 +- proxy.1.pl | 4 +- proxy_lib.1.pm | 302 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 304 insertions(+), 6 deletions(-) diff --git a/cleararch.1.pl b/cleararch.1.pl index 63c1b5f..25dae8c 100644 --- a/cleararch.1.pl +++ b/cleararch.1.pl @@ -11,7 +11,7 @@ use POSIX qw(strftime); ###TIMEOUT_ARCH; $time = time(); -print strftime("%d.%m.%Y %H:%M:%S", gmtime($time))."\n"; +print strftime('%d.%m.%Y %H:%M:%S', gmtime($time))."\n"; cleardir(ARCH_PATH); sub cleardir { @@ -25,7 +25,7 @@ sub cleardir { print "Failed to open: $dirpath\/\n"; return 0; } - while ($subpath = readdir $dir) { + while (defined($subpath = readdir $dir)) { $subpathfull=$dirpath.$subpath; if ($subpath =~ /^\.\.?$/) { next; diff --git a/proxy.1.pl b/proxy.1.pl index 0de727f..af0b02c 100755 --- a/proxy.1.pl +++ b/proxy.1.pl @@ -118,7 +118,7 @@ sub proxy { my $contentlength; - my @curl_arg=(CURL_PATH,'-i','-#'); # the command line arguments for curl + my @curl_arg=(CURL_PATH,'-i','-#','--compressed'); # the command line arguments for curl my @reqhead; # the HTTP request header my @resphead; @@ -625,7 +625,7 @@ sub unlock { # All unlocking access must be logged: time, IP, user. The server owner must # be able to know who did what with the proxy (in case of legal problems). open ($logfile, ">>", UNLOCK_LOG) or return unlockpage("Couldn't log your action.","Status: 500 Internal Server Error\n"); - print $logfile strftime("%d.%m.%Y %H:%M:%S", gmtime($accesstime))." $IP $CGI{'username'}\n"; + print $logfile strftime('%d.%m.%Y %H:%M:%S', gmtime($accesstime))." $IP $CGI{'username'}\n"; close($logfile); # Ok, unlocked. Create access file. diff --git a/proxy_lib.1.pm b/proxy_lib.1.pm index f3bf29a..82fc153 100644 --- a/proxy_lib.1.pm +++ b/proxy_lib.1.pm @@ -10,16 +10,271 @@ use strict; use Exporter; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); +use constant entitycode => { + 'amp' => '&', + 'gt' => '>', + 'lt' => '<', + 'quot' => '"', + 'acute' => '´', + 'cedil' => '¸', + 'circ' => 'ˆ', + 'macr' => '¯', + 'middot' => '·', + 'tilde' => '˜', + 'uml' => '¨', + 'Aacute' => 'Á', + 'aacute' => 'á', + 'Acirc' => 'Â', + 'acirc' => 'â', + 'AElig' => 'Æ', + 'aelig' => 'æ', + 'Agrave' => 'À', + 'agrave' => 'à', + 'Aring' => 'Å', + 'aring' => 'å', + 'Atilde' => 'Ã', + 'atilde' => 'ã', + 'Auml' => 'Ä', + 'auml' => 'ä', + 'Ccedil' => 'Ç', + 'ccedil' => 'ç', + 'Eacute' => 'É', + 'eacute' => 'é', + 'Ecirc' => 'Ê', + 'ecirc' => 'ê', + 'Egrave' => 'È', + 'egrave' => 'è', + 'ETH' => 'Ð', + 'eth' => 'ð', + 'Euml' => 'Ë', + 'euml' => 'ë', + 'Iacute' => 'Í', + 'iacute' => 'í', + 'Icirc' => 'Î', + 'icirc' => 'î', + 'Igrave' => 'Ì', + 'igrave' => 'ì', + 'Iuml' => 'Ï', + 'iuml' => 'ï', + 'Ntilde' => 'Ñ', + 'ntilde' => 'ñ', + 'Oacute' => 'Ó', + 'oacute' => 'ó', + 'Ocirc' => 'Ô', + 'ocirc' => 'ô', + 'OElig' => 'Œ', + 'oelig' => 'œ', + 'Ograve' => 'Ò', + 'ograve' => 'ò', + 'Oslash' => 'Ø', + 'oslash' => 'ø', + 'Otilde' => 'Õ', + 'otilde' => 'õ', + 'Ouml' => 'Ö', + 'ouml' => 'ö', + 'Scaron' => 'Š', + 'scaron' => 'š', + 'szlig' => 'ß', + 'THORN' => 'Þ', + 'thorn' => 'þ', + 'Uacute' => 'Ú', + 'uacute' => 'ú', + 'Ucirc' => 'Û', + 'ucirc' => 'û', + 'Ugrave' => 'Ù', + 'ugrave' => 'ù', + 'Uuml' => 'Ü', + 'uuml' => 'ü', + 'Yacute' => 'Ý', + 'yacute' => 'ý', + 'yuml' => 'ÿ', + 'Yuml' => 'Ÿ', + 'cent' => '¢', + 'curren' => '¤', + 'euro' => '€', + 'pound' => '£', + 'yen' => '¥', + 'brvbar' => '¦', + 'bull' => '•', + 'copy' => '©', + 'dagger' => '†', + 'Dagger' => '‡', + 'frasl' => '⁄', + 'hellip' => '…', + 'iexcl' => '¡', + 'image' => 'ℑ', + 'iquest' => '¿', + 'lrm' => '‎', + 'mdash' => '—', + 'ndash' => '–', + 'not' => '¬', + 'oline' => '‾', + 'ordf' => 'ª', + 'ordm' => 'º', + 'para' => '¶', + 'permil' => '‰', + 'prime' => '′', + 'Prime' => '″', + 'real' => 'ℜ', + 'reg' => '®', + 'rlm' => '‏', + 'sect' => '§', + 'shy' => '­', + 'sup1' => '¹', + 'trade' => '™', + 'weierp' => '℘', + 'bdquo' => '„', + 'laquo' => '«', + 'ldquo' => '“', + 'lsaquo' => '‹', + 'lsquo' => '‘', + 'raquo' => '»', + 'rdquo' => '”', + 'rsaquo' => '›', + 'rsquo' => '’', + 'sbquo' => '‚', + 'emsp' => ' ', + 'ensp' => ' ', + 'nbsp' => ' ', + 'thinsp' => ' ', + 'zwj' => '‍', + 'zwnj' => '‌', + 'deg' => '°', + 'divide' => '÷', + 'frac12' => '½', + 'frac14' => '¼', + 'frac34' => '¾', + 'ge' => '≥', + 'le' => '≤', + 'minus' => '−', + 'sup2' => '²', + 'sup3' => '³', + 'times' => '×', + 'alefsym' => 'ℵ', + 'and' => '∧', + 'ang' => '∠', + 'asymp' => '≈', + 'cap' => '∩', + 'cong' => '≅', + 'cup' => '∪', + 'empty' => '∅', + 'equiv' => '≡', + 'exist' => '∃', + 'fnof' => 'ƒ', + 'forall' => '∀', + 'infin' => '∞', + 'int' => '∫', + 'isin' => '∈', + 'lang' => '⟨', + 'lceil' => '⌈', + 'lfloor' => '⌊', + 'lowast' => '∗', + 'micro' => 'µ', + 'nabla' => '∇', + 'ne' => '≠', + 'ni' => '∋', + 'notin' => '∉', + 'nsub' => '⊄', + 'oplus' => '⊕', + 'or' => '∨', + 'otimes' => '⊗', + 'part' => '∂', + 'perp' => '⊥', + 'plusmn' => '±', + 'prod' => '∏', + 'prop' => '∝', + 'radic' => '√', + 'rang' => '⟩', + 'rceil' => '⌉', + 'rfloor' => '⌋', + 'sdot' => '⋅', + 'sim' => '∼', + 'sub' => '⊂', + 'sube' => '⊆', + 'sum' => '∑', + 'sup' => '⊃', + 'supe' => '⊇', + 'there4' => '∴', + 'Alpha' => 'Α', + 'alpha' => 'α', + 'Beta' => 'Β', + 'beta' => 'β', + 'Chi' => 'Χ', + 'chi' => 'χ', + 'Delta' => 'Δ', + 'delta' => 'δ', + 'Epsilon' => 'Ε', + 'epsilon' => 'ε', + 'Eta' => 'Η', + 'eta' => 'η', + 'Gamma' => 'Γ', + 'gamma' => 'γ', + 'Iota' => 'Ι', + 'iota' => 'ι', + 'Kappa' => 'Κ', + 'kappa' => 'κ', + 'Lambda' => 'Λ', + 'lambda' => 'λ', + 'Mu' => 'Μ', + 'mu' => 'μ', + 'Nu' => 'Ν', + 'nu' => 'ν', + 'Omega' => 'Ω', + 'omega' => 'ω', + 'Omicron' => 'Ο', + 'omicron' => 'ο', + 'Phi' => 'Φ', + 'phi' => 'φ', + 'Pi' => 'Π', + 'pi' => 'π', + 'piv' => 'ϖ', + 'Psi' => 'Ψ', + 'psi' => 'ψ', + 'Rho' => 'Ρ', + 'rho' => 'ρ', + 'Sigma' => 'Σ', + 'sigma' => 'σ', + 'sigmaf' => 'ς', + 'Tau' => 'Τ', + 'tau' => 'τ', + 'Theta' => 'Θ', + 'theta' => 'θ', + 'thetasym' => 'ϑ', + 'upsih' => 'ϒ', + 'Upsilon' => 'Υ', + 'upsilon' => 'υ', + 'Xi' => 'Ξ', + 'xi' => 'ξ', + 'Zeta' => 'Ζ', + 'zeta' => 'ζ', + 'crarr' => '↵', + 'darr' => '↓', + 'dArr' => '⇓', + 'harr' => '↔', + 'hArr' => '⇔', + 'larr' => '←', + 'lArr' => '⇐', + 'rarr' => '→', + 'rArr' => '⇒', + 'uarr' => '↑', + 'uArr' => '⇑', + 'clubs' => '♣', + 'diams' => '♦', + 'hearts' => '♥', + 'spades' => '♠', + 'loz' => '◊', +}; + ###ARCH_PATH; ###ACCESS_PATH; ###TIMEOUT_UNLOCK; ###TIMEOUT_INACT; ###UNLOCK_PROXY_HOST; -$VERSION = 0.000002; +$VERSION = 0.000003; @ISA = qw(Exporter); @EXPORT = (); -@EXPORT_OK = qw(access divideurl formatheader getcgi joinurl path2url url2path path2urldiv urldecode urldiv2path); +@EXPORT_OK = qw(access divideurl entitydecode formatheader getcgi joinurl path2url url2path path2urldiv readconfigfile urldecode urldiv2path); %EXPORT_TAGS = (); # This function checks if the user has unlocked the proxy. Nonzero means yes. @@ -192,6 +447,28 @@ sub urldecode { return $t; } +sub entitydecode { + my $t = $_[0]; + $t =~ s/&(#?[a-zA-Z0-9]+);/entitydecode1ch($1)/eg; + return $t; +} + +sub entitydecode1ch { + my $t = $_[0]; + if ($t =~ /^#([0-9]+)$/) { + return chr($1); + } + elsif ($t =~ /^#x([0-9A-Fa-f]+)$/) { + return chr(hex($1)); + } + elsif ($t =~ /^([A-Za-z0-9]+)$/) { + return entitycode->{$1}; + } + else { + return ''; + } +} + # The function to get CGI parameters from string. # Format is: name=url_encoded_value&name=url_encoded_value& ... &name=url_encoded_value sub getcgi { @@ -534,4 +811,25 @@ sub path2url { return joinurl(path2urldiv($_[0])); } +sub readconfigfile { + (my $configpath) = @_; + my $configfile; + my %config; + + if (open ($configfile, "<", $configpath)) { + while (defined(my $line = <$configfile>)) { + $line =~ s/[\r\n]//g; + $line =~ s/#.*$//; + if ($line =~ /^ *([a-zA-Z0-9_\-\.]+) *= *(.*)$/){ + my $name=$1; + my $value=$2; + $value =~ s/ *$//; + $config{$name}=$value; + } + } + close ($configfile); + } + return %config; +} + 1; -- 2.30.2