blib/lib/GetWeb/Unformat.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 218 CC 12 SCAR 44.5
CC
12
Cov
0%
processPage
156.0
HTTP::Status::RC_BAD_REQUEST_CLIENT
2.0
refHash
2.0
CRAP 86.0
1 use HTTP::Status;
2  
3 sub HTTP::Status::RC_BAD_REQUEST_CLIENT
4 {
5 400;
6 }
7  
8 package GetWeb::Unformat;
9  
10 use GetWeb::Util;
11 use MailBot::UI;
12 use MailBot::Config;
13 use HTML::Parse;
14 use URI::Escape;
15 use strict;
16  
17 sub refHash
18 {
19 my $type = shift;
20 my $scalar = shift;
21  
22 my %hRef = {};
23  
24 #print STDERR "scalar is $scalar\n";
25
26 # parse appended links at bottom of document
27 while ($scalar =~ s!^\s*\[(\S+)\]([^\[\]]+)!!)
28 {
29 my ($key, $action) = ($1,$2);
30 $action =~ s/\\\s+//g;
31 $hRef{$key} = $action;
32 }
33 \%hRef;
34 }
35  
36 sub splitOffSuffix
37 {
38 my ($type, $pBody, $tag) = @_;
39  
40 chomp($tag);
41 my $qTag = quotemeta($tag);
42  
43 my ($suffix, $err);
44 ($$pBody, $suffix, $err) = split(/$qTag(?!.+$qTag)/,$$pBody);
45 $err and
46 die "tag $tag found more than once. I can only handle one document at a time!\n";
47  
48 $suffix =~ /\n\s*\n/ and $suffix = $`; # chop after double-newline
49 #print STDERR "suffix is $suffix\n";
50 $suffix =~ s/\n/ /g;
51 $suffix;
52 }
53  
54 sub processPage
55 {
56 my $type = shift;
57 my $incoming = shift;
58  
59 my $paBody = $incoming -> body;
60  
61 my $origRefLine = &GetWeb::Util::getRefTag;
62 my $refLine = $origRefLine;
63 chomp($refLine);
64 $refLine = quotemeta($refLine);
65  
66 my $foundTag = 0;
67 my $formTag = 0;
68 foreach (@$paBody)
69 {
70 if (/\<GETWEB: FORM/)
71 {
72 $formTag = 1;
73 }
74  
75 if (/$refLine/)
76 {
77 $foundTag = 1;
78 last;
79 }
80 }
81  
82 if (! $foundTag)
83 {
84 $formTag and
85 die "CORRUPT: You must forward the entire document to GetWeb, not just the form\n";
86 return undef;
87 }
88  
89 my $mainBody = join('',@$paBody);
90 $mainBody =~ s/^\>\s*//gm;
91  
92 if ($mainBody =~ /Search the entire archive: ProMED-mail/)
93 {
94 die "The form in the FORMS help document was just an example; to use the real ProMED search form, go to http://www.healthnet.org/programs/promed.html\n";
95 }
96  
97 my $refList = $type -> splitOffSuffix(\$mainBody,$origRefLine);
98  
99 my $formTag = &GetWeb::Util::getFormRefTag;
100 my $form = $type -> splitOffSuffix(\$mainBody,$formTag);
101  
102 $mainBody =~ s/\n/ /g;
103  
104 my $phRef = $type -> refHash($refList);
105 undef $refList;
106  
107 my $ui = MailBot::UI::current;
108 my $profile = $ui -> getProfile;
109 if ($profile -> getProfileVal("refuse_check"))
110 {
111 die "ACCESS DENIED: You cannot return the text of the original message in your e-mail to the\nGetWeb MailBot due to bandwidth limitations. This means that returning\ndocuments with links checked is disabled.\n\nPlease use the GET command interface instead\n";
112 }
113  
114 # jfjf solve problem of links within forms: [X]
115  
116 my @aRefNum = ();
117 foreach (@$paBody)
118 {
119 # j check against orginal document
120 while (s/\[(\d*)X(\d*)\]//i)
121 {
122 my $refNum = $1.$2;
123 next if $refNum eq "";
124 push(@aRefNum,$refNum);
125 }
126 }
127  
128 # jfjf modify to get commands, rather than modify text
129  
130 my @aRequest = ();
131  
132 if (defined $form)
133 {
134 my $parsed = parse_html($form);
135 my $formatter = new GetWeb::UnformatForm(\$mainBody);
136 $formatter -> format($parsed);
137  
138 my $died = $formatter -> {myDieText};
139 die $died if defined $died;
140  
141 my $paFilledForm = $formatter -> {paForm};
142 defined $paFilledForm or die "CORRUPT: could not read forms";
143 my $filledForm;
144 foreach $filledForm (@$paFilledForm)
145 {
146 next unless $filledForm -> {getweb_submit};
147  
148 my $action = $filledForm -> attr('action');
149 my $method = uc $filledForm -> attr('method');
150  
151 $method eq '' and $method = 'GET';
152 $method eq 'POST' or $method eq 'GET' or
153 die "UNAVAILABLE: GetWeb does not support method $method in HTML forms\n";
154  
155 my $orig = $phRef -> {orig};
156 #print "orig is $orig\n";
157 my $url = new URI::URL($action,$orig);
158
159 $orig ne "" or defined $url -> host or
160 die "CORRUPT: Could not find [orig] reference\n";
161  
162 my $phQuery = $filledForm -> {phQuery};
163 defined $phQuery or die "CORRUPT: no query hash\n";
164  
165 my $urlString = $url -> abs;
166 my $queryURL = new URI::URL($urlString);
167 $queryURL -> query_form(%$phQuery);
168  
169 #print "string is " . $queryURL . "\n";
170 #print "method is $method\n";
171  
172 my $request;
173 if ($method eq 'GET')
174 {
175 my $urlString = "$queryURL";
176 $urlString = uri_escape($urlString,'+ \(\)\[\]');
177
178 $request = &GetWeb::Util::safeRequest ($urlString,
179 0,
180 $method);
181 }
182 else
183 {
184 my $equery = $queryURL -> equery;
185 my $urlString = $url -> abs;
186 $request = &GetWeb::Util::safeRequest ($urlString,
187 0,
188 $method);
189 #print "adding return\n";
190 #$equery .= "\n";
191 #print "content is $equery.\n";
192  
193 $request -> header('Content-Length',length $equery);
194 $request -> header('Content-Type',
195 'application/x-www-form-urlencoded');
196 $request -> content($equery);
197  
198 }
199  
200 push(@aRequest,$request);
201 }
202 $parsed -> delete; # avoid memory leak from circular refs
203 }
204  
205 my $refNum;
206 foreach $refNum (@aRefNum)
207 {
208 $refNum =~ s/\s+//g;
209 my $action = $phRef -> {$refNum};
210 defined $action or die "SYNTAX ERROR: no such link: $refNum\n";
211 $action =~ /not supported/ and
212 die "$action is not supported\n";
213 my $request = &GetWeb::Util::safeRequest ($action,
214 0,
215 'GET');
216 defined $request or die "linked to invalid URL: $action\n";
217 push(@aRequest,$request);
218 }
219
220 unless (@aRequest)
221 {
222 die "SYNTAX ERROR: You must check a link or a 'submit' button when you send a document back to GetWeb!\n\nMake sure you forwarded the whole document, including any section marked **Form section (ignore)**\n";
223 }
224 #$incoming -> body(\@aNewBody);
225  
226 my $cmdIter = new GetWeb::CmdIter;
227 my $request;
228 foreach $request (@aRequest)
229 {
230 my $fetcher = new GetWeb::Fetcher;
231 $fetcher -> {REQUEST} = $request;
232  
233 my $cmd = new GetWeb::Cmd;
234 $cmd -> {currentFetcher} = $fetcher;
235 my $url = $request -> url;
236 my $urlString = "$url";
237 $cmd -> addCanon("<url:$urlString> #link");
238  
239 $cmdIter -> pushCmd($cmd);
240 }
241
242 $cmdIter;
243 }
244  
245 1;