-
Notifications
You must be signed in to change notification settings - Fork 668
/
URL.java
1767 lines (1561 loc) · 69.7 KB
/
URL.java
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
37
38
39
40
41
42
43
44
45
46
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.net;
import java.io.IOException;
import java.io.InputStream;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream.GetField;
import java.io.ObjectStreamException;
import java.io.ObjectStreamField;
import java.io.Serializable;
import java.net.spi.URLStreamHandlerProvider;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import jdk.internal.misc.JavaNetURLAccess;
import jdk.internal.misc.SharedSecrets;
import jdk.internal.misc.VM;
import sun.net.ApplicationProxy;
import sun.security.action.GetPropertyAction;
import sun.security.util.SecurityConstants;
/**
* Class {@code URL} represents a Uniform Resource
* Locator, a pointer to a "resource" on the World
* Wide Web. A resource can be something as simple as a file or a
* directory, or it can be a reference to a more complicated object,
* such as a query to a database or to a search engine. More
* information on the types of URLs and their formats can be found at:
* <a href=
* "http://web.archive.org/web/20051219043731/http://archive.ncsa.uiuc.edu/SDG/Software/Mosaic/Demo/url-primer.html">
* <i>Types of URL</i></a>
* <p>
* In general, a URL can be broken into several parts. Consider the
* following example:
* <blockquote><pre>
* http://www.example.com/docs/resource1.html
* </pre></blockquote>
* <p>
* The URL above indicates that the protocol to use is
* {@code http} (HyperText Transfer Protocol) and that the
* information resides on a host machine named
* {@code www.example.com}. The information on that host
* machine is named {@code /docs/resource1.html}. The exact
* meaning of this name on the host machine is both protocol
* dependent and host dependent. The information normally resides in
* a file, but it could be generated on the fly. This component of
* the URL is called the <i>path</i> component.
* <p>
* A URL can optionally specify a "port", which is the
* port number to which the TCP connection is made on the remote host
* machine. If the port is not specified, the default port for
* the protocol is used instead. For example, the default port for
* {@code http} is {@code 80}. An alternative port could be
* specified as:
* <blockquote><pre>
* http://www.example.com:1080/docs/resource1.html
* </pre></blockquote>
* <p>
* The syntax of {@code URL} is defined by <a
* href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC 2396: Uniform
* Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a
* href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC 2732: Format for
* Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format
* also supports scope_ids. The syntax and usage of scope_ids is described
* <a href="Inet6Address.html#scoped">here</a>.
* <p>
* A URL may have appended to it a "fragment", also known
* as a "ref" or a "reference". The fragment is indicated by the sharp
* sign character "#" followed by more characters. For example,
* <blockquote><pre>
* http://java.sun.com/index.html#chapter1
* </pre></blockquote>
* <p>
* This fragment is not technically part of the URL. Rather, it
* indicates that after the specified resource is retrieved, the
* application is specifically interested in that part of the
* document that has the tag {@code chapter1} attached to it. The
* meaning of a tag is resource specific.
* <p>
* An application can also specify a "relative URL",
* which contains only enough information to reach the resource
* relative to another URL. Relative URLs are frequently used within
* HTML pages. For example, if the contents of the URL:
* <blockquote><pre>
* http://java.sun.com/index.html
* </pre></blockquote>
* contained within it the relative URL:
* <blockquote><pre>
* FAQ.html
* </pre></blockquote>
* it would be a shorthand for:
* <blockquote><pre>
* http://java.sun.com/FAQ.html
* </pre></blockquote>
* <p>
* The relative URL need not specify all the components of a URL. If
* the protocol, host name, or port number is missing, the value is
* inherited from the fully specified URL. The file component must be
* specified. The optional fragment is not inherited.
* <p>
* The URL class does not itself encode or decode any URL components
* according to the escaping mechanism defined in RFC2396. It is the
* responsibility of the caller to encode any fields, which need to be
* escaped prior to calling URL, and also to decode any escaped fields,
* that are returned from URL. Furthermore, because URL has no knowledge
* of URL escaping, it does not recognise equivalence between the encoded
* or decoded form of the same URL. For example, the two URLs:<br>
* <pre> http://foo.com/hello world/ and http://foo.com/hello%20world</pre>
* would be considered not equal to each other.
* <p>
* Note, the {@link java.net.URI} class does perform escaping of its
* component fields in certain circumstances. The recommended way
* to manage the encoding and decoding of URLs is to use {@link java.net.URI},
* and to convert between these two classes using {@link #toURI()} and
* {@link URI#toURL()}.
* <p>
* The {@link URLEncoder} and {@link URLDecoder} classes can also be
* used, but only for HTML form encoding, which is not the same
* as the encoding scheme defined in RFC2396.
*
* @author James Gosling
* @since 1.0
*/
/*
* 统一资源定位符URL,属于URI的一种分类
*
* URL = [scheme:][//authority]path[?query][#reference]
* authority = [userinfo@]host[:port]
*/
public final class URL implements Serializable {
static final long serialVersionUID = -7627629688361524110L;
static final String BUILTIN_HANDLERS_PREFIX = "sun.net.www.protocol";
/**
* The property which specifies the package prefix list to be scanned
* for protocol handlers. The value of this property (if any) should
* be a vertical bar delimited list of package names to search through
* for a protocol handler to load. The policy of this class is that
* all protocol handlers will be in a class called <protocolname>.Handler,
* and each package in the list is examined in turn for a matching
* handler. If none are found (or the property is not specified), the
* default package prefix, sun.net.www.protocol, is used. The search
* proceeds from the first package in the list to the last and stops
* when a match is found.
*/
private static final String protocolPathProp = "java.protocol.handler.pkgs";
// The protocol to use (ftp, http, nntp, ... etc.)
private String protocol; // 协议
// The authority part of this URL
private String authority; // 登录信息
// The userinfo part of this URL
private transient String userInfo; // 用户信息
// The host name to connect to
private String host; // 主机地址
// The protocol port to connect to
private int port = -1; // 端口号
// The path part of this URL
private transient String path; // 路径
// The query part of this URL
private transient String query; // 查询串
// #reference
private String ref; // 锚点,相当于URI中的fragment
/**
* The specified file name on that host.
* {@code file} is defined as {@code path[?query]}
*/
private String file; // 文件路径,通常是path[?query]
/**
* The host's IP address, used in equals and hashCode.
* Computed on demand. An uninitialized or unknown hostAddress is null.
*/
transient InetAddress hostAddress; // 主机地址
/**
* The URLStreamHandler for this URL.
*/
// 流协议处理器
transient URLStreamHandler handler;
/*
* A table of protocol handlers
*/
// 缓存使用过的协议及其对应的流协议处理器
static Hashtable<String, URLStreamHandler> handlers = new Hashtable<>();
/**
* The URLStreamHandler factory
*/
// 流协议处理器的工厂
private static volatile URLStreamHandlerFactory factory;
// 内置的流协议处理器工厂
private static final URLStreamHandlerFactory defaultFactory = new DefaultFactory();
private static final Object streamHandlerLock = new Object();
// Thread-local gate to prevent recursive provider lookups
private static ThreadLocal<Object> gate = new ThreadLocal<>();
/**
* Our hash code.
*
* @serial
*/
private int hashCode = -1;
// 存储反序列化对象
private transient UrlDeserializedState tempState;
static {
SharedSecrets.setJavaNetURLAccess(new JavaNetURLAccess() {
@Override
public URLStreamHandler getHandler(URL url) {
return url.handler;
}
});
}
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Creates a URL from the specified {@code protocol}
* name, {@code host} name, and {@code file} name. The
* default port for the specified protocol is used.
* <p>
* This constructor is equivalent to the four-argument
* constructor with the only difference of using the
* default port for the specified protocol.
*
* No validation of the inputs is performed by this constructor.
*
* @param protocol the name of the protocol to use.
* @param host the name of the host.
* @param file the file on the host.
*
* @throws MalformedURLException if an unknown protocol is specified.
* @see java.net.URL#URL(java.lang.String, java.lang.String,
* int, java.lang.String)
*/
public URL(String protocol, String host, String file) throws MalformedURLException {
this(protocol, host, -1, file);
}
/**
* Creates a {@code URL} object from the specified
* {@code protocol}, {@code host}, {@code port}
* number, and {@code file}.<p>
*
* {@code host} can be expressed as a host name or a literal
* IP address. If IPv6 literal address is used, it should be
* enclosed in square brackets ({@code '['} and {@code ']'}), as
* specified by <a
* href="http://www.ietf.org/rfc/rfc2732.txt">RFC 2732</a>;
* However, the literal IPv6 address format defined in <a
* href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC 2373: IP
* Version 6 Addressing Architecture</i></a> is also accepted.<p>
*
* Specifying a {@code port} number of {@code -1}
* indicates that the URL should use the default port for the
* protocol.<p>
*
* If this is the first URL object being created with the specified
* protocol, a <i>stream protocol handler</i> object, an instance of
* class {@code URLStreamHandler}, is created for that protocol:
* <ol>
* <li>If the application has previously set up an instance of
* {@code URLStreamHandlerFactory} as the stream handler factory,
* then the {@code createURLStreamHandler} method of that instance
* is called with the protocol string as an argument to create the
* stream protocol handler.
* <li>If no {@code URLStreamHandlerFactory} has yet been set up,
* or if the factory's {@code createURLStreamHandler} method
* returns {@code null}, then the {@linkplain java.util.ServiceLoader
* ServiceLoader} mechanism is used to locate {@linkplain
* java.net.spi.URLStreamHandlerProvider URLStreamHandlerProvider}
* implementations using the system class
* loader. The order that providers are located is implementation
* specific, and an implementation is free to cache the located
* providers. A {@linkplain java.util.ServiceConfigurationError
* ServiceConfigurationError}, {@code Error} or {@code RuntimeException}
* thrown from the {@code createURLStreamHandler}, if encountered, will
* be propagated to the calling thread. The {@code
* createURLStreamHandler} method of each provider, if instantiated, is
* invoked, with the protocol string, until a provider returns non-null,
* or all providers have been exhausted.
* <li>If the previous step fails to find a protocol handler, the
* constructor reads the value of the system property:
* <blockquote>{@code
* java.protocol.handler.pkgs
* }</blockquote>
* If the value of that system property is not {@code null},
* it is interpreted as a list of packages separated by a vertical
* slash character '{@code |}'. The constructor tries to load
* the class named:
* <blockquote>{@code
* <package>.<protocol>.Handler
* }</blockquote>
* where {@code <package>} is replaced by the name of the package
* and {@code <protocol>} is replaced by the name of the protocol.
* If this class does not exist, or if the class exists but it is not
* a subclass of {@code URLStreamHandler}, then the next package
* in the list is tried.
* <li>If the previous step fails to find a protocol handler, then the
* constructor tries to load a built-in protocol handler.
* If this class does not exist, or if the class exists but it is not a
* subclass of {@code URLStreamHandler}, then a
* {@code MalformedURLException} is thrown.
* </ol>
*
* <p>Protocol handlers for the following protocols are guaranteed
* to exist on the search path :-
* <blockquote><pre>
* http, https, file, and jar
* </pre></blockquote>
* Protocol handlers for additional protocols may also be available.
* Some protocol handlers, for example those used for loading platform
* classes or classes on the class path, may not be overridden. The details
* of such restrictions, and when those restrictions apply (during
* initialization of the runtime for example), are implementation specific
* and therefore not specified
*
* <p>No validation of the inputs is performed by this constructor.
*
* @param protocol the name of the protocol to use.
* @param host the name of the host.
* @param port the port number on the host.
* @param file the file on the host
*
* @throws MalformedURLException if an unknown protocol or the port
* is a negative number other than -1
* @see java.lang.System#getProperty(java.lang.String)
* @see java.net.URL#setURLStreamHandlerFactory(
*java.net.URLStreamHandlerFactory)
* @see java.net.URLStreamHandler
* @see java.net.URLStreamHandlerFactory#createURLStreamHandler(
*java.lang.String)
*/
public URL(String protocol, String host, int port, String file) throws MalformedURLException {
this(protocol, host, port, file, null);
}
/**
* Creates a {@code URL} object from the specified
* {@code protocol}, {@code host}, {@code port}
* number, {@code file}, and {@code handler}. Specifying
* a {@code port} number of {@code -1} indicates that
* the URL should use the default port for the protocol. Specifying
* a {@code handler} of {@code null} indicates that the URL
* should use a default stream handler for the protocol, as outlined
* for:
* java.net.URL#URL(java.lang.String, java.lang.String, int,
* java.lang.String)
*
* <p>If the handler is not null and there is a security manager,
* the security manager's {@code checkPermission}
* method is called with a
* {@code NetPermission("specifyStreamHandler")} permission.
* This may result in a SecurityException.
*
* No validation of the inputs is performed by this constructor.
*
* @param protocol the name of the protocol to use.
* @param host the name of the host.
* @param port the port number on the host.
* @param file the file on the host
* @param handler the stream handler for the URL.
*
* @throws MalformedURLException if an unknown protocol or the port
* is a negative number other than -1
* @throws SecurityException if a security manager exists and its
* {@code checkPermission} method doesn't allow
* specifying a stream handler explicitly.
* @see java.lang.System#getProperty(java.lang.String)
* @see java.net.URL#setURLStreamHandlerFactory(
*java.net.URLStreamHandlerFactory)
* @see java.net.URLStreamHandler
* @see java.net.URLStreamHandlerFactory#createURLStreamHandler(
*java.lang.String)
* @see SecurityManager#checkPermission
* @see java.net.NetPermission
*/
public URL(String protocol, String host, int port, String file, URLStreamHandler handler) throws MalformedURLException {
if(handler != null) {
SecurityManager sm = System.getSecurityManager();
if(sm != null) {
// check for permission to specify a handler
checkSpecifyHandler(sm);
}
}
// 协议名转小写
protocol = toLowerCase(protocol);
this.protocol = protocol;
if(host != null) {
/* if host is a literal IPv6 address, we will make it conform to RFC 2732 */
if(host.indexOf(':') >= 0 && !host.startsWith("[")) {
host = "[" + host + "]";
}
this.host = host;
// 有host时必须有port
if(port<-1) {
throw new MalformedURLException("Invalid port number :" + port);
}
this.port = port;
authority = (port == -1) ? host : host + ":" + port;
}
int index = file.indexOf('#');
this.ref = index<0 ? null : file.substring(index + 1);
file = index<0 ? file : file.substring(0, index);
int q = file.lastIndexOf('?');
if(q != -1) {
this.query = file.substring(q + 1);
this.path = file.substring(0, q);
this.file = path + "?" + query;
} else {
this.path = file;
this.file = path;
}
/*
* Note: we don't do validation of the URL here.
* Too risky to change right now, but worth considering for future reference.
*/
// 获取指定协议的URLStreamHandler
if(handler == null && (handler = getURLStreamHandler(protocol)) == null) {
throw new MalformedURLException("unknown protocol: " + protocol);
}
this.handler = handler;
}
/**
* Creates a {@code URL} object from the {@code String} representation.
* <p>
* This constructor is equivalent to a call to the two-argument
* constructor with a {@code null} first argument.
*
* @param spec the {@code String} to parse as a URL.
*
* @throws MalformedURLException if no protocol is specified, or an
* unknown protocol is found, or {@code spec} is {@code null},
* or the parsed URL fails to comply with the specific syntax
* of the associated protocol.
* @see java.net.URL#URL(java.net.URL, java.lang.String)
*/
// 返回使用spec构造的URL
public URL(String spec) throws MalformedURLException {
this(null, spec);
}
/**
* Creates a URL by parsing the given spec within a specified context.
*
* The new URL is created from the given context URL and the spec
* argument as described in
* RFC2396 "Uniform Resource Identifiers : Generic * Syntax" :
* <blockquote><pre>
* <scheme>://<authority><path>?<query>#<fragment>
* </pre></blockquote>
* The reference is parsed into the scheme, authority, path, query and
* fragment parts. If the path component is empty and the scheme,
* authority, and query components are undefined, then the new URL is a
* reference to the current document. Otherwise, the fragment and query
* parts present in the spec are used in the new URL.
* <p>
* If the scheme component is defined in the given spec and does not match
* the scheme of the context, then the new URL is created as an absolute
* URL based on the spec alone. Otherwise the scheme component is inherited
* from the context URL.
* <p>
* If the authority component is present in the spec then the spec is
* treated as absolute and the spec authority and path will replace the
* context authority and path. If the authority component is absent in the
* spec then the authority of the new URL will be inherited from the
* context.
* <p>
* If the spec's path component begins with a slash character
* "/" then the
* path is treated as absolute and the spec path replaces the context path.
* <p>
* Otherwise, the path is treated as a relative path and is appended to the
* context path, as described in RFC2396. Also, in this case,
* the path is canonicalized through the removal of directory
* changes made by occurrences of ".." and ".".
* <p>
* For a more detailed description of URL parsing, refer to RFC2396.
*
* @param context the context in which to parse the specification.
* @param spec the {@code String} to parse as a URL.
*
* @throws MalformedURLException if no protocol is specified, or an
* unknown protocol is found, or {@code spec} is {@code null},
* or the parsed URL fails to comply with the specific syntax
* of the associated protocol.
* @see java.net.URL#URL(java.lang.String, java.lang.String,
* int, java.lang.String)
* @see java.net.URLStreamHandler
* @see java.net.URLStreamHandler#parseURL(java.net.URL,
* java.lang.String, int, int)
*/
// 返回使用spec构造的基于context的URL
public URL(URL context, String spec) throws MalformedURLException {
this(context, spec, null);
}
/**
* Creates a URL by parsing the given spec with the specified handler within a specified context.
* If the handler is null, the parsing occurs as with the two argument constructor.
*
* @param context the context in which to parse the specification.
* @param spec the {@code String} to parse as a URL.
* @param handler the stream handler for the URL.
*
* @throws MalformedURLException if no protocol is specified, or an
* unknown protocol is found, or {@code spec} is {@code null},
* or the parsed URL fails to comply with the specific syntax
* of the associated protocol.
* @throws SecurityException if a security manager exists and its
* {@code checkPermission} method doesn't allow
* specifying a stream handler.
* @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
* @see java.net.URLStreamHandler
* @see java.net.URLStreamHandler#parseURL(java.net.URL, java.lang.String, int, int)
*/
// 基于context解析spec,从spec中解析到的URL组件会覆盖到context中以形成新的URL返回,如果spec是相对路径,则会追加在context原有路径上;handler显式指定了URLStreamHandler
public URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException {
String original = spec;
int i, limit, c;
int start = 0;
String newProtocol = null;
boolean aRef = false;
boolean isRelative = false;
// Check for permission to specify a handler
if(handler != null) {
SecurityManager sm = System.getSecurityManager();
if(sm != null) {
checkSpecifyHandler(sm);
}
}
try {
limit = spec.length();
// 忽略spec后面的空白
while((limit>0) && (spec.charAt(limit - 1)<=' ')) {
limit--; //eliminate trailing whitespace
}
// 忽略spec前面的空白
while((start<limit) && (spec.charAt(start)<=' ')) {
start++; // eliminate leading whitespace
}
// 忽略spec前面的"url:"
if(spec.regionMatches(true, start, "url:", 0, 4)) {
start += 4;
}
// 如果此时spec以"#"开头,则说明是spec锚点
if(start<spec.length() && spec.charAt(start) == '#') {
/*
* we're assuming this is a ref relative to the context URL.
* This means protocols cannot start w/ '#', but we must parse ref URL's like: "hello:there" w/ a ':' in them.
*/
aRef = true;
}
// 在spec不是锚点的情形下,继续向后查找":",如果遇到"/"则结束
for(i = start; !aRef && (i<limit) && ((c = spec.charAt(i)) != '/'); i++) {
if(c != ':') {
continue;
}
/* 至此,找到了":" */
// 截取":"之前的部分
String s = toLowerCase(spec.substring(start, i));
// spec中遇到了协议名称
if(isValidProtocol(s)) {
newProtocol = s;
start = i + 1;
}
break;
}
// Only use our context if the protocols match.
protocol = newProtocol; // 保存新协议名称
if((context != null) && ((newProtocol == null) || newProtocol.equalsIgnoreCase(context.protocol))) {
// inherit the protocol handler from the context if not specified to the constructor
if(handler == null) {
handler = context.handler;
}
// If the context is a hierarchical URL scheme and the spec
// contains a matching scheme then maintain backwards
// compatibility and treat it as if the spec didn't contain
// the scheme; see 5.2.3 of RFC2396
if(context.path != null && context.path.startsWith("/")) {
newProtocol = null;
}
if(newProtocol == null) {
protocol = context.protocol;
authority = context.authority;
userInfo = context.userInfo;
host = context.host;
port = context.port;
file = context.file;
path = context.path;
isRelative = true;
}
}
if(protocol == null) {
throw new MalformedURLException("no protocol: " + original);
}
/* Get the protocol handler if not specified or the protocol of the context could not be used */
// 获取指定协议的URLStreamHandler
if(handler == null && (handler = getURLStreamHandler(protocol)) == null) {
throw new MalformedURLException("unknown protocol: " + protocol);
}
this.handler = handler;
i = spec.indexOf('#', start);
if(i >= 0) {
ref = spec.substring(i + 1, limit);
limit = i;
}
// Handle special case inheritance of query and fragment implied by RFC2396 section 5.2.2
if(isRelative && start == limit) {
query = context.query;
if(ref == null) {
ref = context.ref;
}
}
// 基于当前url解析spec中指定范围内的字符串,从spec中解析到的URL组件会覆盖到当前url中以形成新的URL返回,如果spec是相对路径,则会追加在当前url的原有路径上
handler.parseURL(this, spec, start, limit);
} catch(MalformedURLException e) {
throw e;
} catch(Exception e) {
MalformedURLException exception = new MalformedURLException(e.getMessage());
exception.initCause(e);
throw exception;
}
}
/*▲ 构造器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ URI组成部分 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Gets the protocol name of this {@code URL}.
*
* @return the protocol of this {@code URL}.
*/
// 返回协议(protocol)
public String getProtocol() {
return protocol;
}
/**
* Gets the authority part of this {@code URL}.
*
* @return the authority part of this {@code URL}
*
* @since 1.3
*/
// 返回登录信息(authority)
public String getAuthority() {
return authority;
}
/**
* Gets the userInfo part of this {@code URL}.
*
* @return the userInfo part of this {@code URL}, or
* <CODE>null</CODE> if one does not exist
*
* @since 1.3
*/
// 返回用户信息(userinfo)
public String getUserInfo() {
return userInfo;
}
/**
* Gets the host name of this {@code URL}, if applicable.
* The format of the host conforms to RFC 2732, i.e. for a
* literal IPv6 address, this method will return the IPv6 address
* enclosed in square brackets ({@code '['} and {@code ']'}).
*
* @return the host name of this {@code URL}.
*/
// 返回主机(host)
public String getHost() {
return host;
}
/**
* Gets the port number of this {@code URL}.
*
* @return the port number, or -1 if the port is not set
*/
// 返回端口(port)
public int getPort() {
return port;
}
/**
* Gets the path part of this {@code URL}.
*
* @return the path part of this {@code URL}, or an
* empty string if one does not exist
*
* @since 1.3
*/
// 返回路径(path)
public String getPath() {
return path;
}
/**
* Gets the query part of this {@code URL}.
*
* @return the query part of this {@code URL},
* or <CODE>null</CODE> if one does not exist
*
* @since 1.3
*/
// 返回查询串(query)
public String getQuery() {
return query;
}
/**
* Gets the anchor (also known as the "reference") of this
* {@code URL}.
*
* @return the anchor (also known as the "reference") of this
* {@code URL}, or <CODE>null</CODE> if one does not exist
*/
// 返回锚点(fragment)
public String getRef() {
return ref;
}
/**
* Gets the default port number of the protocol associated
* with this {@code URL}. If the URL scheme or the URLStreamHandler
* for the URL do not define a default port number,
* then -1 is returned.
*
* @return the port number
*
* @since 1.4
*/
// 返回当前协议下的默认端口,有些协议需要端口支持
public int getDefaultPort() {
return handler.getDefaultPort();
}
/**
* Gets the file name of this {@code URL}.
* The returned file portion will be
* the same as <CODE>getPath()</CODE>, plus the concatenation of
* the value of <CODE>getQuery()</CODE>, if any. If there is
* no query portion, this method and <CODE>getPath()</CODE> will
* return identical results.
*
* @return the file name of this {@code URL},
* or an empty string if one does not exist
*/
// 返回文件名
public String getFile() {
return file;
}
/*▲ URI组成部分 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 流协议处理器的工厂 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Sets an application's {@code URLStreamHandlerFactory}.
* This method can be called at most once in a given Java Virtual
* Machine.
*
* <p> The {@code URLStreamHandlerFactory} instance is used to
* construct a stream protocol handler from a protocol name.
*
* <p> If there is a security manager, this method first calls
* the security manager's {@code checkSetFactory} method
* to ensure the operation is allowed.
* This could result in a SecurityException.
*
* @param fac the desired factory.
*
* @throws Error if the application has already set a factory.
* @throws SecurityException if a security manager exists and its
* {@code checkSetFactory} method doesn't allow
* the operation.
* @see java.net.URL#URL(java.lang.String, java.lang.String,
* int, java.lang.String)
* @see java.net.URLStreamHandlerFactory
* @see SecurityManager#checkSetFactory
*/
// 设置流协议处理器的工厂,这是静态方法,需要在构造URL对象之前调用
public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) {
synchronized(streamHandlerLock) {
if(factory != null) {
throw new Error("factory already defined");
}
SecurityManager security = System.getSecurityManager();
if(security != null) {
security.checkSetFactory();
}
handlers.clear();
// safe publication of URLStreamHandlerFactory with volatile write
factory = fac;
}
}
/*▲ 流协议处理器的工厂 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 资源连接 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns a {@link java.net.URLConnection URLConnection} instance that
* represents a connection to the remote object referred to by the
* {@code URL}.
*
* <P>A new instance of {@linkplain java.net.URLConnection URLConnection} is
* created every time when invoking the
* {@linkplain java.net.URLStreamHandler#openConnection(URL)
* URLStreamHandler.openConnection(URL)} method of the protocol handler for
* this URL.</P>
*
* <P>It should be noted that a URLConnection instance does not establish
* the actual network connection on creation. This will happen only when
* calling {@linkplain java.net.URLConnection#connect() URLConnection.connect()}.</P>
*
* <P>If for the URL's protocol (such as HTTP or JAR), there
* exists a public, specialized URLConnection subclass belonging
* to one of the following packages or one of their subpackages:
* java.lang, java.io, java.util, java.net, the connection
* returned will be of that subclass. For example, for HTTP an
* HttpURLConnection will be returned, and for JAR a
* JarURLConnection will be returned.</P>
*
* @return a {@link java.net.URLConnection URLConnection} linking
* to the URL.
*
* @throws IOException if an I/O exception occurs.
* @see java.net.URL#URL(java.lang.String, java.lang.String,
* int, java.lang.String)
*/
// 打开URL资源连接
public URLConnection openConnection() throws IOException {
return handler.openConnection(this);
}
/**
* Same as {@link #openConnection()}, except that the connection will be
* made through the specified proxy; Protocol handlers that do not
* support proxing will ignore the proxy parameter and make a
* normal connection.
*
* Invoking this method preempts the system's default
* {@link java.net.ProxySelector ProxySelector} settings.
*
* @param proxy the Proxy through which this connection
* will be made. If direct connection is desired,
* Proxy.NO_PROXY should be specified.
*
* @return a {@code URLConnection} to the URL.
*
* @throws IOException if an I/O exception occurs.
* @throws SecurityException if a security manager is present
* and the caller doesn't have permission to connect
* to the proxy.
* @throws IllegalArgumentException will be thrown if proxy is null,
* or proxy has the wrong type
* @throws UnsupportedOperationException if the subclass that
* implements the protocol handler doesn't support
* this method.
* @see java.net.URL#URL(java.lang.String, java.lang.String,
* int, java.lang.String)
* @see java.net.URLConnection
* @see java.net.URLStreamHandler#openConnection(java.net.URL,
* java.net.Proxy)
* @since 1.5
*/
// 使用指定的代理打开URL资源连接
public URLConnection openConnection(java.net.Proxy proxy) throws java.io.IOException {
if(proxy == null) {
throw new IllegalArgumentException("proxy can not be null");
}
// Create a copy of Proxy as a security measure
Proxy p = (proxy == Proxy.NO_PROXY) ? Proxy.NO_PROXY : ApplicationProxy.create(proxy);
SecurityManager sm = System.getSecurityManager();
if(sm != null && p.type() != Proxy.Type.DIRECT) {
InetSocketAddress epoint = (InetSocketAddress) p.address();
// 如果是"未解析"的socket地址
if(epoint.isUnresolved()) {
sm.checkConnect(epoint.getHostName(), epoint.getPort());
} else {
sm.checkConnect(epoint.getAddress().getHostAddress(), epoint.getPort());
}
}
// 返回连接
return handler.openConnection(this, p);
}
/*▲ 资源连接 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 字节流 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Opens a connection to this {@code URL} and returns an
* {@code InputStream} for reading from that connection. This
* method is a shorthand for:
* <blockquote><pre>
* openConnection().getInputStream()
* </pre></blockquote>
*