#!/usr/bin/perl use Text::Aspell; my ($identifier, $word, @suggestions, $i, $l, %input, $speller); # Create speller object and set options $speller = Text::Aspell->new; $speller->set_option('lang','en_US'); $speller->set_option('sug-mode','fast'); # Parse CGI input parseInput(); # Send header print "Content-type: text/javascript; charset=UTF-8\n\n"; print "data = ["; # Parse request and generate response for ($i = 0; ; $i++) { $identifier = $i; $word = $input{$i}; if ($word eq '') { last; } if ($i) { print ','; } if ($speller->check($word)) { print '[1]'; } else { print '[0,['; @suggestions = $speller->suggest($word); $l = 0; foreach (@suggestions) { if ($l) { print ','; } print '"' . $_ . '"'; $l++; } print ']]'; } } print "];\n"; # Internal support functions sub parseInput { my $input = \%input; my ($data, @pairs, $pair, $key, $value, $len); if ($ENV{'REQUEST_METHOD'} eq 'GET') { $data = $ENV{'QUERY_STRING'}; } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') { $len = $ENV{'CONTENT_LENGTH'}; if ($len > 102400) { $len = 102400; } # Limit input to 100 kB read(STDIN, $data, $ENV{'CONTENT_LENGTH'}); } # Parse urlencoded GET/POST input if (length($data)) { if ($ENV{'CONTENT_TYPE'} =~ m/urlencoded/ || $ENV{'REQUEST_METHOD'} eq 'GET') { # Replace ampersands in URI's with semicolons to comply to the w3c # uri recommendations while maintaining backwards compatibility. $data =~ s/\&/;/g; @pairs = split(/;/ ,$data); foreach $pair (@pairs) { if ($pair =~ m/([^=]+)=(.*)/) { $key = $1; $value = $2; _decodeInput(\$value); if (grep(/^$key$/, keys(%{$input}))) { unless (ref($input->{$key}) =~ /ARRAY/) { my $originalValue = $input->{$key}; undef $input->{$key}; $input->{$key}[0] = $originalValue; } push(@{$input->{$key}}, $value); } else { $input->{$key} = $value; } } } } } } sub _decodeInput { ${$_[0]} =~ s/\+/ /g; ${$_[0]} =~ s/%([\dA-Fa-f]{2})/pack("C", hex($1))/eg; ${$_[0]} =~ s/\\/\\\\/g; }