Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
LinuxSuRen
jenkins
提交
0c16c857
J
jenkins
项目概览
LinuxSuRen
/
jenkins
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jenkins
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0c16c857
编写于
5月 13, 2016
作者:
V
Vincent Latombe
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[JENKINS-23507] Make FilePath.installIfNecessaryFrom follow redirects (#2331)
上级
782804f3
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
82 addition
and
6 deletion
+82
-6
core/src/main/java/hudson/FilePath.java
core/src/main/java/hudson/FilePath.java
+58
-6
core/src/test/java/hudson/FilePathTest.java
core/src/test/java/hudson/FilePathTest.java
+24
-0
未找到文件。
core/src/main/java/hudson/FilePath.java
浏览文件 @
0c16c857
...
...
@@ -25,6 +25,7 @@
*/
package
hudson
;
import
com.google.common.annotations.VisibleForTesting
;
import
com.jcraft.jzlib.GZIPInputStream
;
import
com.jcraft.jzlib.GZIPOutputStream
;
import
hudson.Launcher.LocalLauncher
;
...
...
@@ -71,6 +72,8 @@ import org.apache.tools.ant.Project;
import
org.apache.tools.ant.types.FileSet
;
import
org.apache.tools.zip.ZipEntry
;
import
org.apache.tools.zip.ZipFile
;
import
org.kohsuke.accmod.Restricted
;
import
org.kohsuke.accmod.restrictions.NoExternalUse
;
import
org.kohsuke.stapler.Stapler
;
import
javax.annotation.CheckForNull
;
...
...
@@ -91,6 +94,7 @@ import java.io.RandomAccessFile;
import
java.io.Serializable
;
import
java.io.Writer
;
import
java.net.HttpURLConnection
;
import
java.net.MalformedURLException
;
import
java.net.URI
;
import
java.net.URL
;
import
java.net.URLConnection
;
...
...
@@ -188,6 +192,11 @@ import org.jenkinsci.remoting.RoleSensitive;
* @see VirtualFile
*/
public
final
class
FilePath
implements
Serializable
{
/**
* Maximum http redirects we will follow. This defaults to the same number as Firefox/Chrome tolerates.
*/
private
static
final
int
MAX_REDIRECTS
=
20
;
/**
* When this {@link FilePath} represents the remote path,
* this field is always non-null on master (the field represents
...
...
@@ -755,6 +764,10 @@ public final class FilePath implements Serializable {
* @since 1.299
*/
public
boolean
installIfNecessaryFrom
(
@Nonnull
URL
archive
,
@CheckForNull
TaskListener
listener
,
@Nonnull
String
message
)
throws
IOException
,
InterruptedException
{
return
installIfNecessaryFrom
(
archive
,
listener
,
message
,
MAX_REDIRECTS
);
}
private
boolean
installIfNecessaryFrom
(
@Nonnull
URL
archive
,
@CheckForNull
TaskListener
listener
,
@Nonnull
String
message
,
int
maxRedirects
)
throws
InterruptedException
,
IOException
{
try
{
FilePath
timestamp
=
this
.
child
(
".timestamp"
);
long
lastModified
=
timestamp
.
lastModified
();
...
...
@@ -777,9 +790,22 @@ public final class FilePath implements Serializable {
}
}
if
(
lastModified
!=
0
&&
con
instanceof
HttpURLConnection
)
{
if
(
con
instanceof
HttpURLConnection
)
{
HttpURLConnection
httpCon
=
(
HttpURLConnection
)
con
;
int
responseCode
=
httpCon
.
getResponseCode
();
if
(
responseCode
==
HttpURLConnection
.
HTTP_MOVED_PERM
||
responseCode
==
HttpURLConnection
.
HTTP_MOVED_TEMP
)
{
// follows redirect
if
(
maxRedirects
>
0
)
{
String
location
=
httpCon
.
getHeaderField
(
"Location"
);
listener
.
getLogger
().
println
(
"Following redirect "
+
archive
.
toExternalForm
()
+
" -> "
+
location
);
return
installIfNecessaryFrom
(
getUrlFactory
().
newURL
(
location
),
listener
,
message
,
maxRedirects
-
1
);
}
else
{
listener
.
getLogger
().
println
(
"Skipping installation of "
+
archive
+
" to "
+
remote
+
" due to too many redirects."
);
return
false
;
}
}
if
(
lastModified
!=
0
)
{
if
(
responseCode
==
HttpURLConnection
.
HTTP_NOT_MODIFIED
)
{
return
false
;
}
else
if
(
responseCode
!=
HttpURLConnection
.
HTTP_OK
)
{
...
...
@@ -787,6 +813,7 @@ public final class FilePath implements Serializable {
return
false
;
}
}
}
long
sourceTimestamp
=
con
.
getLastModified
();
...
...
@@ -2520,6 +2547,31 @@ public final class FilePath implements Serializable {
});
}
private
static
final
UrlFactory
DEFAULT_URL_FACTORY
=
new
UrlFactory
();
@Restricted
(
NoExternalUse
.
class
)
static
class
UrlFactory
{
public
URL
newURL
(
String
location
)
throws
MalformedURLException
{
return
new
URL
(
location
);
}
}
private
UrlFactory
urlFactory
;
@VisibleForTesting
@Restricted
(
NoExternalUse
.
class
)
void
setUrlFactory
(
UrlFactory
urlFactory
)
{
this
.
urlFactory
=
urlFactory
;
}
private
UrlFactory
getUrlFactory
()
{
if
(
urlFactory
!=
null
)
{
return
urlFactory
;
}
else
{
return
DEFAULT_URL_FACTORY
;
}
}
/**
* Short for {@code validateFileMask(path, value, true)}
*/
...
...
core/src/test/java/hudson/FilePathTest.java
浏览文件 @
0c16c857
...
...
@@ -38,9 +38,11 @@ import java.io.OutputStream;
import
java.io.RandomAccessFile
;
import
java.net.ConnectException
;
import
java.net.HttpURLConnection
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.net.URLConnection
;
import
java.net.URLStreamHandler
;
import
java.net.URLStreamHandlerFactory
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.HashSet
;
...
...
@@ -637,6 +639,28 @@ public class FilePathTest {
assertTrue
(
log
,
log
.
contains
(
"504 Gateway Timeout"
));
}
@Issue
(
"JENKINS-23507"
)
@Test
public
void
installIfNecessaryFollowsRedirects
()
throws
Exception
{
File
tmp
=
temp
.
getRoot
();
final
FilePath
d
=
new
FilePath
(
tmp
);
FilePath
.
UrlFactory
urlFactory
=
mock
(
FilePath
.
UrlFactory
.
class
);
d
.
setUrlFactory
(
urlFactory
);
final
HttpURLConnection
con
=
mock
(
HttpURLConnection
.
class
);
final
HttpURLConnection
con2
=
mock
(
HttpURLConnection
.
class
);
final
URL
url
=
someUrlToZipFile
(
con
);
when
(
con
.
getResponseCode
()).
thenReturn
(
HttpURLConnection
.
HTTP_MOVED_TEMP
);
URL
url2
=
someUrlToZipFile
(
con2
);
String
someUrl
=
url2
.
toExternalForm
();
when
(
con
.
getHeaderField
(
"Location"
)).
thenReturn
(
someUrl
);
when
(
urlFactory
.
newURL
(
someUrl
)).
thenReturn
(
url2
);
when
(
con2
.
getResponseCode
()).
thenReturn
(
HttpURLConnection
.
HTTP_OK
);
when
(
con2
.
getInputStream
()).
thenReturn
(
someZippedContent
());
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
String
message
=
"going ahead"
;
assertTrue
(
d
.
installIfNecessaryFrom
(
url
,
new
StreamTaskListener
(
baos
),
message
));
}
private
URL
someUrlToZipFile
(
final
URLConnection
con
)
throws
IOException
{
final
URLStreamHandler
urlHandler
=
new
URLStreamHandler
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录