Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell11
提交
d8d57e1b
D
dragonwell11
项目概览
openanolis
/
dragonwell11
通知
7
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell11
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d8d57e1b
编写于
5月 05, 2010
作者:
C
chegar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
6886723: light weight http server doesn't return correct status code for HEAD requests
Reviewed-by: michaelm
上级
7a2c2e58
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
151 addition
and
20 deletion
+151
-20
jdk/src/share/classes/sun/net/httpserver/ExchangeImpl.java
jdk/src/share/classes/sun/net/httpserver/ExchangeImpl.java
+34
-20
jdk/test/com/sun/net/httpserver/bugs/HeadTest.java
jdk/test/com/sun/net/httpserver/bugs/HeadTest.java
+117
-0
未找到文件。
jdk/src/share/classes/sun/net/httpserver/ExchangeImpl.java
浏览文件 @
d8d57e1b
...
...
@@ -26,16 +26,12 @@
package
sun.net.httpserver
;
import
java.io.*
;
import
java.nio.*
;
import
java.nio.channels.*
;
import
java.net.*
;
import
javax.net.ssl.*
;
import
java.util.*
;
import
java.util.logging.Logger
;
import
java.text.*
;
import
sun.net.www.MessageHeader
;
import
com.sun.net.httpserver.*
;
import
com.sun.net.httpserver.spi.*
;
class
ExchangeImpl
{
...
...
@@ -65,6 +61,8 @@ class ExchangeImpl {
df
.
setTimeZone
(
tz
);
}
private
static
final
String
HEAD
=
"HEAD"
;
/* streams which take care of the HTTP protocol framing
* and are passed up to higher layers
*/
...
...
@@ -116,6 +114,10 @@ class ExchangeImpl {
return
connection
.
getHttpContext
();
}
private
boolean
isHeadRequest
()
{
return
HEAD
.
equals
(
getRequestMethod
());
}
public
void
close
()
{
if
(
closed
)
{
return
;
...
...
@@ -220,24 +222,36 @@ class ExchangeImpl {
}
contentLen
=
-
1
;
}
if
(
contentLen
==
0
)
{
if
(
http10
)
{
o
.
setWrappedStream
(
new
UndefLengthOutputStream
(
this
,
ros
));
close
=
true
;
}
else
{
rspHdrs
.
set
(
"Transfer-encoding"
,
"chunked"
);
o
.
setWrappedStream
(
new
ChunkedOutputStream
(
this
,
ros
));
}
}
else
{
if
(
contentLen
==
-
1
)
{
noContentToSend
=
true
;
contentLen
=
0
;
if
(
isHeadRequest
())
{
/* HEAD requests should not set a content length by passing it
* through this API, but should instead manually set the required
* headers.*/
if
(
contentLen
>=
0
)
{
final
Logger
logger
=
server
.
getLogger
();
String
msg
=
"sendResponseHeaders: being invoked with a content length for a HEAD request"
;
logger
.
warning
(
msg
);
}
/* content len might already be set, eg to implement HEAD resp */
if
(
rspHdrs
.
getFirst
(
"Content-length"
)
==
null
)
{
rspHdrs
.
set
(
"Content-length"
,
Long
.
toString
(
contentLen
));
noContentToSend
=
true
;
contentLen
=
0
;
}
else
{
/* not a HEAD request */
if
(
contentLen
==
0
)
{
if
(
http10
)
{
o
.
setWrappedStream
(
new
UndefLengthOutputStream
(
this
,
ros
));
close
=
true
;
}
else
{
rspHdrs
.
set
(
"Transfer-encoding"
,
"chunked"
);
o
.
setWrappedStream
(
new
ChunkedOutputStream
(
this
,
ros
));
}
}
else
{
if
(
contentLen
==
-
1
)
{
noContentToSend
=
true
;
contentLen
=
0
;
}
rspHdrs
.
set
(
"Content-length"
,
Long
.
toString
(
contentLen
));
o
.
setWrappedStream
(
new
FixedLengthOutputStream
(
this
,
ros
,
contentLen
));
}
o
.
setWrappedStream
(
new
FixedLengthOutputStream
(
this
,
ros
,
contentLen
));
}
write
(
rspHdrs
,
tmpout
);
this
.
rspContentLen
=
contentLen
;
...
...
jdk/test/com/sun/net/httpserver/bugs/HeadTest.java
0 → 100644
浏览文件 @
d8d57e1b
/*
* Copyright 2010 Sun Microsystems, Inc. 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.
*
* 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/**
* @test
* @bug 6886723
* @summary light weight http server doesn't return correct status code for HEAD requests
*/
import
java.net.InetSocketAddress
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.io.IOException
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
com.sun.net.httpserver.HttpContext
;
import
com.sun.net.httpserver.HttpExchange
;
import
com.sun.net.httpserver.HttpHandler
;
import
com.sun.net.httpserver.HttpServer
;
public
class
HeadTest
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
server
();
}
static
void
server
()
throws
Exception
{
InetSocketAddress
inetAddress
=
new
InetSocketAddress
(
0
);
HttpServer
server
=
HttpServer
.
create
(
inetAddress
,
5
);
try
{
server
.
setExecutor
(
Executors
.
newFixedThreadPool
(
5
));
HttpContext
chunkedContext
=
server
.
createContext
(
"/chunked"
);
chunkedContext
.
setHandler
(
new
HttpHandler
()
{
@Override
public
void
handle
(
HttpExchange
msg
)
{
try
{
try
{
if
(
msg
.
getRequestMethod
().
equals
(
"HEAD"
))
{
msg
.
getRequestBody
().
close
();
msg
.
getResponseHeaders
().
add
(
"Transfer-encoding"
,
"chunked"
);
msg
.
sendResponseHeaders
(
200
,
-
1
);
}
}
catch
(
IOException
ioe
)
{
ioe
.
printStackTrace
();
}
}
finally
{
msg
.
close
();
}
}
});
HttpContext
clContext
=
server
.
createContext
(
"/content"
);
clContext
.
setHandler
(
new
HttpHandler
()
{
@Override
public
void
handle
(
HttpExchange
msg
)
{
try
{
try
{
if
(
msg
.
getRequestMethod
().
equals
(
"HEAD"
))
{
msg
.
getRequestBody
().
close
();
msg
.
getResponseHeaders
().
add
(
"Content-length"
,
"1024"
);
msg
.
sendResponseHeaders
(
200
,
-
1
);
}
}
catch
(
IOException
ioe
)
{
ioe
.
printStackTrace
();
}
}
finally
{
msg
.
close
();
}
}
});
server
.
start
();
String
urlStr
=
"http://localhost:"
+
server
.
getAddress
().
getPort
()
+
"/"
;
System
.
out
.
println
(
"Server is at "
+
urlStr
);
// Run the chunked client
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
runClient
(
urlStr
+
"chunked/"
);
}
// Run the content length client
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
runClient
(
urlStr
+
"content/"
);
}
}
finally
{
// Stop the server
((
ExecutorService
)
server
.
getExecutor
()).
shutdown
();
server
.
stop
(
0
);
}
}
static
void
runClient
(
String
urlStr
)
throws
Exception
{
HttpURLConnection
conn
=
(
HttpURLConnection
)
new
URL
(
urlStr
).
openConnection
();
conn
.
setRequestMethod
(
"HEAD"
);
int
status
=
conn
.
getResponseCode
();
if
(
status
!=
200
)
{
throw
new
RuntimeException
(
"HEAD request doesn't return 200, but returns "
+
status
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录