1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 package HTTPClient;
37
38 import java.net.URL;
39 import java.net.ProtocolException;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.io.OutputStream;
43 import java.io.ByteArrayOutputStream;
44 import java.util.Date;
45 import java.util.Hashtable;
46 import java.util.Enumeration;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public class HttpURLConnection extends java.net.HttpURLConnection
115 {
116
117 protected static Hashtable connections = new Hashtable();
118
119
120 protected HTTPConnection con;
121
122
123 private String urlString;
124
125
126 private String resource;
127
128
129 private String method;
130
131
132 private boolean method_set;
133
134
135 private static NVPair[] default_headers = new NVPair[0];
136
137
138 private NVPair[] headers;
139
140
141 protected HTTPResponse resp;
142
143
144 private boolean do_redir;
145
146
147 private static Class redir_mod;
148
149
150 private OutputStream output_stream;
151
152
153 static
154 {
155
156
157 try
158 {
159 if (Boolean.getBoolean("HTTPClient.HttpURLConnection.AllowUI"))
160 setDefaultAllowUserInteraction(true);
161 }
162 catch (SecurityException se)
163 { }
164
165
166 try
167 { redir_mod = Class.forName("HTTPClient.RedirectionModule"); }
168 catch (ClassNotFoundException cnfe)
169 { throw new NoClassDefFoundError(cnfe.getMessage()); }
170
171
172 try
173 {
174 String agent = System.getProperty("http.agent");
175 if (agent != null)
176 setDefaultRequestProperty("User-Agent", agent);
177 }
178 catch (SecurityException se)
179 { }
180 }
181
182
183
184
185 private static String non_proxy_hosts = "";
186 private static String proxy_host = "";
187 private static int proxy_port = -1;
188
189
190
191
192
193
194
195
196
197
198
199 public HttpURLConnection(URL url)
200 throws ProtocolNotSuppException, IOException
201 {
202 super(url);
203
204
205 try
206 {
207 String hosts = System.getProperty("http.nonProxyHosts", "");
208 if (!hosts.equalsIgnoreCase(non_proxy_hosts))
209 {
210 connections.clear();
211 non_proxy_hosts = hosts;
212 String[] list = Util.splitProperty(hosts);
213 for (int idx=0; idx<list.length; idx++)
214 HTTPConnection.dontProxyFor(list[idx]);
215 }
216 }
217 catch (ParseException pe)
218 { throw new IOException(pe.toString()); }
219 catch (SecurityException se)
220 { }
221
222 try
223 {
224 String host = System.getProperty("http.proxyHost", "");
225 int port = Integer.getInteger("http.proxyPort", -1).intValue();
226 if (!host.equalsIgnoreCase(proxy_host) || port != proxy_port)
227 {
228 connections.clear();
229 proxy_host = host;
230 proxy_port = port;
231 HTTPConnection.setProxyServer(host, port);
232 }
233 }
234 catch (SecurityException se)
235 { }
236
237
238 con = getConnection(url);
239 method = "GET";
240 method_set = false;
241 resource = url.getFile();
242 headers = default_headers;
243 do_redir = getFollowRedirects();
244 output_stream = null;
245
246 urlString = url.toString();
247 }
248
249
250
251
252
253
254
255
256
257
258
259 protected HTTPConnection getConnection(URL url)
260 throws ProtocolNotSuppException
261 {
262
263
264 String php = url.getProtocol() + ":" + url.getHost() + ":" +
265 ((url.getPort() != -1) ? url.getPort() :
266 URI.defaultPort(url.getProtocol()));
267 php = php.toLowerCase();
268
269 HTTPConnection con = (HTTPConnection) connections.get(php);
270 if (con != null) return con;
271
272
273
274
275 con = new HTTPConnection(url);
276 connections.put(php, con);
277
278 return con;
279 }
280
281
282
283
284
285
286
287
288
289
290
291 public void setRequestMethod(String method) throws ProtocolException
292 {
293 if (connected)
294 throw new ProtocolException("Already connected!");
295
296 Log.write(Log.URLC, "URLC: (" + urlString + ") Setting request method: " +
297 method);
298
299 this.method = method.trim().toUpperCase();
300 method_set = true;
301 }
302
303
304
305
306
307
308
309 public String getRequestMethod()
310 {
311 return method;
312 }
313
314
315
316
317
318
319
320 public int getResponseCode() throws IOException
321 {
322 if (!connected) connect();
323
324 try
325 { return resp.getStatusCode(); }
326 catch (ModuleException me)
327 { throw new IOException(me.toString()); }
328 }
329
330
331
332
333
334
335
336
337 public String getResponseMessage() throws IOException
338 {
339 if (!connected) connect();
340
341 try
342 { return resp.getReasonLine(); }
343 catch (ModuleException me)
344 { throw new IOException(me.toString()); }
345 }
346
347
348
349
350
351
352
353
354 public String getHeaderField(String name)
355 {
356 try
357 {
358 if (!connected) connect();
359 return resp.getHeader(name);
360 }
361 catch (Exception e)
362 { return null; }
363 }
364
365
366
367
368
369
370
371
372
373
374
375 public int getHeaderFieldInt(String name, int def)
376 {
377 try
378 {
379 if (!connected) connect();
380 return resp.getHeaderAsInt(name);
381 }
382 catch (Exception e)
383 { return def; }
384 }
385
386
387
388
389
390
391
392
393
394
395
396
397 public long getHeaderFieldDate(String name, long def)
398 {
399 try
400 {
401 if (!connected) connect();
402 return resp.getHeaderAsDate(name).getTime();
403 }
404 catch (Exception e)
405 { return def; }
406 }
407
408
409 private String[] hdr_keys, hdr_values;
410
411
412
413
414
415
416
417
418
419 public String getHeaderFieldKey(int n)
420 {
421 if (hdr_keys == null)
422 fill_hdr_arrays();
423
424 if (n >= 0 && n < hdr_keys.length)
425 return hdr_keys[n];
426 else
427 return null;
428 }
429
430
431
432
433
434
435
436
437
438 public String getHeaderField(int n)
439 {
440 if (hdr_values == null)
441 fill_hdr_arrays();
442
443 if (n >= 0 && n < hdr_values.length)
444 return hdr_values[n];
445 else
446 return null;
447 }
448
449
450
451
452
453 private void fill_hdr_arrays()
454 {
455 try
456 {
457 if (!connected) connect();
458
459
460 int num = 1;
461
462
463
464
465
466
467
468 Enumeration e = resp.listHeaders();
469 while (e.hasMoreElements())
470 {
471 num++;
472 e.nextElement();
473 }
474
475
476
477 hdr_keys = new String[num];
478 hdr_values = new String[num];
479
480
481
482
483
484
485
486 e = resp.listHeaders();
487 for (int idx=1; idx<num; idx++)
488 {
489 hdr_keys[idx] = (String) e.nextElement();
490
491 hdr_values[idx] = resp.getHeader(hdr_keys[idx]);
492 }
493
494
495 hdr_values[0] = resp.getVersion() + " " + resp.getStatusCode() +
496 " " + resp.getReasonLine();
497 }
498 catch (Exception e)
499 { hdr_keys = hdr_values = new String[0]; }
500 }
501
502
503
504
505
506
507
508
509
510
511 public InputStream getInputStream() throws IOException
512 {
513 if (!doInput)
514 throw new ProtocolException("Input not enabled! (use setDoInput(true))");
515
516 if (!connected) connect();
517
518 InputStream stream;
519 try
520 { stream = resp.getInputStream(); }
521 catch (ModuleException e)
522 { throw new IOException(e.toString()); }
523
524 return stream;
525 }
526
527
528
529
530
531
532
533
534
535
536
537
538
539 public InputStream getErrorStream()
540 {
541 try
542 {
543 if (!doInput || !connected || resp.getStatusCode() < 300 ||
544 resp.getHeaderAsInt("Content-length") <= 0)
545 return null;
546
547 return resp.getInputStream();
548 }
549 catch (Exception e)
550 { return null; }
551 }
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575 public synchronized OutputStream getOutputStream() throws IOException
576 {
577 if (connected)
578 throw new ProtocolException("Already connected!");
579
580 if (!doOutput)
581 throw new ProtocolException("Output not enabled! (use setDoOutput(true))");
582 if (!method_set)
583 method = "POST";
584 else if (method.equals("HEAD") || method.equals("GET") ||
585 method.equals("TRACE"))
586 throw new ProtocolException("Method "+method+" does not support output!");
587
588 if (getRequestProperty("Content-type") == null)
589 setRequestProperty("Content-type", "application/x-www-form-urlencoded");
590
591 if (output_stream == null)
592 {
593 Log.write(Log.URLC, "URLC: (" + urlString + ") creating output stream");
594
595 String cl = getRequestProperty("Content-Length");
596 if (cl != null)
597 output_stream = new HttpOutputStream(Integer.parseInt(cl.trim()));
598 else
599 {
600
601
602
603
604 if (getRequestProperty("Content-type").equals(
605 "application/x-www-form-urlencoded"))
606 output_stream = new ByteArrayOutputStream(300);
607 else
608 output_stream = new HttpOutputStream();
609 }
610
611 if (output_stream instanceof HttpOutputStream)
612 connect();
613 }
614
615 return output_stream;
616 }
617
618
619
620
621
622
623
624
625 public URL getURL()
626 {
627 if (connected)
628 {
629 try
630 { return resp.getEffectiveURI().toURL(); }
631 catch (Exception e)
632 { return null; }
633 }
634
635 return url;
636 }
637
638
639
640
641
642
643
644 public void setIfModifiedSince(long time)
645 {
646 super.setIfModifiedSince(time);
647 setRequestProperty("If-Modified-Since", Util.httpDate(new Date(time)));
648 }
649
650
651
652
653
654
655
656
657 public void setRequestProperty(String name, String value)
658 {
659 Log.write(Log.URLC, "URLC: (" + urlString + ") Setting request property: " +
660 name + " : " + value);
661
662 int idx;
663 for (idx=0; idx<headers.length; idx++)
664 {
665 if (headers[idx].getName().equalsIgnoreCase(name))
666 break;
667 }
668
669 if (idx == headers.length)
670 headers = Util.resizeArray(headers, idx+1);
671
672 headers[idx] = new NVPair(name, value);
673 }
674
675
676
677
678
679
680
681
682 public String getRequestProperty(String name)
683 {
684 for (int idx=0; idx<headers.length; idx++)
685 {
686 if (headers[idx].getName().equalsIgnoreCase(name))
687 return headers[idx].getValue();
688 }
689
690 return null;
691 }
692
693
694
695
696
697
698
699
700
701 public static void setDefaultRequestProperty(String name, String value)
702 {
703 Log.write(Log.URLC, "URLC: Setting default request property: " +
704 name + " : " + value);
705
706 int idx;
707 for (idx=0; idx<default_headers.length; idx++)
708 {
709 if (default_headers[idx].getName().equalsIgnoreCase(name))
710 break;
711 }
712
713 if (idx == default_headers.length)
714 default_headers = Util.resizeArray(default_headers, idx+1);
715
716 default_headers[idx] = new NVPair(name, value);
717 }
718
719
720
721
722
723
724
725
726 public static String getDefaultRequestProperty(String name)
727 {
728 for (int idx=0; idx<default_headers.length; idx++)
729 {
730 if (default_headers[idx].getName().equalsIgnoreCase(name))
731 return default_headers[idx].getValue();
732 }
733
734 return null;
735 }
736
737
738
739
740
741
742
743
744 public void setInstanceFollowRedirects(boolean set)
745 {
746 if (connected)
747 throw new IllegalStateException("Already connected!");
748
749 do_redir = set;
750 }
751
752
753
754
755
756
757 public boolean getInstanceFollowRedirects()
758 {
759 return do_redir;
760 }
761
762
763
764
765
766
767 public synchronized void connect() throws IOException
768 {
769 if (connected) return;
770
771 Log.write(Log.URLC, "URLC: (" + urlString + ") Connecting ...");
772
773
774
775 synchronized (con)
776 {
777 con.setAllowUserInteraction(allowUserInteraction);
778 if (do_redir)
779 con.addModule(redir_mod, 2);
780 else
781 con.removeModule(redir_mod);
782
783 try
784 {
785 if (output_stream instanceof ByteArrayOutputStream)
786 resp = con.ExtensionMethod(method, resource,
787 ((ByteArrayOutputStream) output_stream).toByteArray(),
788 headers);
789 else
790 resp = con.ExtensionMethod(method, resource,
791 (HttpOutputStream) output_stream, headers);
792 }
793 catch (ModuleException e)
794 { throw new IOException(e.toString()); }
795 }
796
797 connected = true;
798 }
799
800
801
802
803
804 public void disconnect()
805 {
806 Log.write(Log.URLC, "URLC: (" + urlString + ") Disconnecting ...");
807
808 con.stop();
809 }
810
811
812
813
814
815
816
817 public boolean usingProxy()
818 {
819 return (con.getProxyHost() != null);
820 }
821
822
823
824
825
826
827 public String toString()
828 {
829 return getClass().getName() + "[" + url + "]";
830 }
831 }