Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
武汉红喜
whatsmars
提交
4cf0ce05
W
whatsmars
项目概览
武汉红喜
/
whatsmars
通知
3
Star
0
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
whatsmars
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4cf0ce05
编写于
4月 14, 2016
作者:
武汉红喜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
connector
上级
3deeb4f6
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
303 addition
and
61 deletion
+303
-61
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/connector/HttpHeader.java
.../main/java/com/whatsmars/tomcat/connector/HttpHeader.java
+43
-1
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/connector/HttpProcessor.java
...in/java/com/whatsmars/tomcat/connector/HttpProcessor.java
+75
-37
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/connector/HttpRequest.java
...main/java/com/whatsmars/tomcat/connector/HttpRequest.java
+50
-16
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/connector/HttpRequestLine.java
.../java/com/whatsmars/tomcat/connector/HttpRequestLine.java
+54
-0
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/connector/HttpResponse.java
...ain/java/com/whatsmars/tomcat/connector/HttpResponse.java
+13
-0
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/connector/SocketInputStream.java
...ava/com/whatsmars/tomcat/connector/SocketInputStream.java
+66
-5
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/servlet/RequestFacade.java
...main/java/com/whatsmars/tomcat/servlet/RequestFacade.java
+1
-1
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/servlet/ResponseFacade.java
...ain/java/com/whatsmars/tomcat/servlet/ResponseFacade.java
+1
-1
未找到文件。
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/connector/HttpHeader.java
浏览文件 @
4cf0ce05
...
...
@@ -3,5 +3,47 @@ package com.whatsmars.tomcat.connector;
/**
* Created by shenhongxi on 16/4/13.
*/
public
class
HttpHeader
{
public
final
class
HttpHeader
{
public
static
final
int
INITIAL_NAME_SIZE
=
32
;
public
static
final
int
INITIAL_VALUE_SIZE
=
64
;
public
static
final
int
MAX_NAME_SIZE
=
128
;
public
static
final
int
MAX_VALUE_SIZE
=
4096
;
public
char
[]
name
;
public
int
nameEnd
;
public
char
[]
value
;
public
int
valueEnd
;
protected
int
hashCode
=
0
;
public
HttpHeader
()
{
this
(
new
char
[
INITIAL_NAME_SIZE
],
0
,
new
char
[
INITIAL_VALUE_SIZE
],
0
);
}
public
HttpHeader
(
char
[]
name
,
int
nameEnd
,
char
[]
value
,
int
valueEnd
)
{
this
.
name
=
name
;
this
.
nameEnd
=
nameEnd
;
this
.
value
=
value
;
this
.
valueEnd
=
valueEnd
;
}
public
HttpHeader
(
String
name
,
String
value
)
{
this
.
name
=
name
.
toLowerCase
().
toCharArray
();
this
.
nameEnd
=
name
.
length
();
this
.
value
=
value
.
toCharArray
();
this
.
valueEnd
=
value
.
length
();
}
/**
* Release all object references, and initialize instance variables, in
* preparation for reuse of this object.
*/
public
void
recycle
()
{
nameEnd
=
0
;
valueEnd
=
0
;
hashCode
=
0
;
}
}
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/connector/HttpProcessor.java
浏览文件 @
4cf0ce05
...
...
@@ -3,6 +3,7 @@ package com.whatsmars.tomcat.connector;
import
com.whatsmars.tomcat.servlet.ServletProcessor
;
import
com.whatsmars.tomcat.servlet.StaticResourceProcessor
;
import
javax.servlet.ServletException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.net.Socket
;
...
...
@@ -12,9 +13,10 @@ import java.net.Socket;
*/
public
class
HttpProcessor
{
HttpConnector
connector
;
HttpRequest
request
;
HttpResponse
response
;
private
HttpConnector
connector
;
private
HttpRequest
request
;
private
HttpResponse
response
;
private
HttpRequestLine
requestLine
=
new
HttpRequestLine
();
public
HttpProcessor
(
HttpConnector
connector
)
{
this
.
connector
=
connector
;
...
...
@@ -35,9 +37,8 @@ public class HttpProcessor {
parseRequest
(
input
,
output
);
// 解析请求行,即HTTP请求的第一行内容
parseHeaders
(
input
);
// 解析请求头
// request.addHeader(name, value); // 将请求头的名/值添加到request对象的HashMap请求头中
if
(
request
.
get
Uri
().
startsWith
(
"/servlet/"
))
{
if
(
request
.
get
RequestURI
().
startsWith
(
"/servlet/"
))
{
ServletProcessor
processor
=
new
ServletProcessor
();
//processor.process(request, response);
}
else
{
...
...
@@ -51,46 +52,83 @@ public class HttpProcessor {
}
}
private
void
parseHeaders
(
SocketInputStream
input
)
{
int
line
=
-
1
;
while
(
line
!=
-
1
)
{
// 一行一行解析完hear
HttpHeader
httpHeader
=
new
HttpHeader
();
input
.
readHeader
(
httpHeader
);
private
void
parseHeaders
(
SocketInputStream
input
)
throws
IOException
,
ServletException
{
while
(
true
)
{
// 一行一行解析完header
HttpHeader
header
=
new
HttpHeader
();
// Read the next header
input
.
readHeader
(
header
);
if
(
header
.
nameEnd
==
0
)
{
if
(
header
.
valueEnd
==
0
)
{
return
;
}
else
{
throw
new
ServletException
(
"httpProcessor parseHeaders colon"
);
}
}
String
name
=
new
String
(
header
.
name
,
0
,
header
.
nameEnd
);
String
value
=
new
String
(
header
.
value
,
0
,
header
.
valueEnd
);
request
.
addHeader
(
name
,
value
);
// do something for some headers, ignore others.
if
(
name
.
equals
(
"cookie"
))
{
// ...
// request.addCookie(cookies[i]);
}
else
if
(
name
.
equals
(
"content-length"
))
{
int
n
=
-
1
;
try
{
n
=
Integer
.
parseInt
(
value
);
}
catch
(
Exception
e
)
{
throw
new
ServletException
(
"httpProcessor.parseHeaders.contentLength"
);
}
request
.
setContentLength
(
n
);
}
else
if
(
name
.
equals
(
"content-type"
))
{
request
.
setContentType
(
value
);
}
}
}
private
void
parseRequest
(
SocketInputStream
input
,
OutputStream
output
)
{
StringBuffer
requestStr
=
new
StringBuffer
(
2048
);
int
i
;
byte
[]
buffer
=
new
byte
[
2048
];
try
{
i
=
input
.
read
(
buffer
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
i
=
-
1
;
private
void
parseRequest
(
SocketInputStream
input
,
OutputStream
output
)
throws
IOException
,
ServletException
{
input
.
readRequestLine
(
requestLine
);
String
method
=
new
String
(
requestLine
.
method
,
0
,
requestLine
.
methodEnd
);
String
uri
=
null
;
String
protocol
=
new
String
(
requestLine
.
protocol
,
0
,
requestLine
.
protocolEnd
);
// Validate the incoming request line
if
(
method
.
length
()
<
1
)
{
throw
new
ServletException
(
"Missing HTTP request method"
);
}
else
if
(
requestLine
.
uriEnd
<
1
)
{
throw
new
ServletException
(
"Missing HTTP request URI"
);
}
for
(
int
j
=
0
;
j
<
i
;
j
++)
{
requestStr
.
append
((
char
)
buffer
[
j
]);
// Parse any query parameters out of the request URI
int
question
=
requestLine
.
indexOf
(
"?"
);
if
(
question
>=
0
)
{
request
.
setQueryString
(
new
String
(
requestLine
.
uri
,
question
+
1
,
requestLine
.
uriEnd
-
question
-
1
));
uri
=
new
String
(
requestLine
.
uri
,
0
,
question
);
}
else
{
request
.
setQueryString
(
null
);
uri
=
new
String
(
requestLine
.
uri
,
0
,
requestLine
.
uriEnd
);
}
System
.
out
.
println
(
requestStr
.
toString
());
String
uri
=
parseUri
(
requestStr
.
toString
());
request
.
setUri
(
uri
);
String
normalizedUri
=
normalize
(
uri
);
// 填充HttpRequest对象
((
HttpRequest
)
request
).
setMethod
(
method
);
request
.
setProtocol
(
protocol
);
if
(
normalizedUri
!=
null
)
{
((
HttpRequest
)
request
).
setRequestURI
(
normalizedUri
);
}
else
{
((
HttpRequest
)
request
).
setRequestURI
(
uri
);
}
if
(
normalizedUri
==
null
)
{
throw
new
ServletException
(
"Invalid URI: "
+
uri
+
"'"
);
}
}
private
String
parseUri
(
String
requestStr
)
{
// GET /index.html HTTP/1.1
// Accept: text/plain; text/html
// Return a context-relative path, beginning with a "/"
protected
String
normalize
(
String
path
)
{
if
(
path
==
null
)
return
null
;
String
normalized
=
path
;
// ...
int
index1
=
requestStr
.
indexOf
(
' '
);
int
index2
;
if
(
index1
!=
-
1
)
{
index2
=
requestStr
.
indexOf
(
' '
,
index1
+
1
);
if
(
index2
>
index1
)
{
return
requestStr
.
substring
(
index1
+
1
,
index2
);
}
}
return
null
;
return
path
;
}
}
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/connector/HttpRequest.java
浏览文件 @
4cf0ce05
...
...
@@ -6,28 +6,62 @@ import java.io.BufferedReader;
import
java.io.IOException
;
import
java.io.UnsupportedEncodingException
;
import
java.security.Principal
;
import
java.util.Collection
;
import
java.util.Enumeration
;
import
java.util.Locale
;
import
java.util.Map
;
import
java.util.*
;
/**
* Created by shenhongxi on 16/4/11.
*/
public
class
HttpRequest
implements
HttpServletRequest
{
SocketInputStream
input
;
String
uri
;
private
String
requestURI
;
private
int
contentLength
;
private
String
contentType
;
private
String
queryString
;
private
String
method
;
private
String
protocol
;
protected
Map
headers
=
new
HashMap
();
protected
SocketInputStream
input
;
public
HttpRequest
(
SocketInputStream
input
)
{
this
.
input
=
input
;
}
public
String
getUri
()
{
return
uri
;
public
void
addHeader
(
String
name
,
String
value
)
{
name
=
name
.
toLowerCase
();
synchronized
(
headers
)
{
ArrayList
values
=
(
ArrayList
)
headers
.
get
(
name
);
if
(
values
==
null
)
{
values
=
new
ArrayList
();
headers
.
put
(
name
,
values
);
}
values
.
add
(
value
);
}
}
public
void
setRequestURI
(
String
requestURI
)
{
this
.
requestURI
=
requestURI
;
}
public
void
setUri
(
String
uri
)
{
this
.
uri
=
uri
;
public
void
setContentLength
(
int
contentLength
)
{
this
.
contentLength
=
contentLength
;
}
public
void
setContentType
(
String
contentType
)
{
this
.
contentType
=
contentType
;
}
public
void
setQueryString
(
String
queryString
)
{
this
.
queryString
=
queryString
;
}
public
void
setMethod
(
String
method
)
{
this
.
method
=
method
;
}
public
void
setProtocol
(
String
protocol
)
{
this
.
protocol
=
protocol
;
}
public
String
getAuthType
()
{
...
...
@@ -59,7 +93,7 @@ public class HttpRequest implements HttpServletRequest {
}
public
String
getMethod
()
{
return
null
;
return
method
;
}
public
String
getPathInfo
()
{
...
...
@@ -75,7 +109,7 @@ public class HttpRequest implements HttpServletRequest {
}
public
String
getQueryString
()
{
return
null
;
return
queryString
;
}
public
String
getRemoteUser
()
{
...
...
@@ -95,7 +129,7 @@ public class HttpRequest implements HttpServletRequest {
}
public
String
getRequestURI
()
{
return
null
;
return
requestURI
;
}
public
StringBuffer
getRequestURL
()
{
...
...
@@ -167,11 +201,11 @@ public class HttpRequest implements HttpServletRequest {
}
public
int
getContentLength
()
{
return
0
;
return
this
.
contentLength
;
}
public
String
getContentType
()
{
return
null
;
return
contentType
;
}
public
ServletInputStream
getInputStream
()
throws
IOException
{
...
...
@@ -195,7 +229,7 @@ public class HttpRequest implements HttpServletRequest {
}
public
String
getProtocol
()
{
return
nul
l
;
return
protoco
l
;
}
public
String
getScheme
()
{
...
...
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/connector/HttpRequestLine.java
0 → 100644
浏览文件 @
4cf0ce05
package
com.whatsmars.tomcat.connector
;
/**
* Created by shenhongxi on 16/4/13.
*/
public
final
class
HttpRequestLine
{
public
static
final
int
INITIAL_METHOD_SIZE
=
8
;
public
static
final
int
INITIAL_URI_SIZE
=
64
;
public
static
final
int
INITIAL_PROTOCOL_SIZE
=
8
;
public
static
final
int
MAX_METHOD_SIZE
=
1024
;
public
static
final
int
MAX_URI_SIZE
=
32768
;
public
static
final
int
MAX_PROTOCOL_SIZE
=
1024
;
public
char
[]
method
;
public
int
methodEnd
;
public
char
[]
uri
;
public
int
uriEnd
;
public
char
[]
protocol
;
public
int
protocolEnd
;
public
HttpRequestLine
()
{
this
(
new
char
[
INITIAL_METHOD_SIZE
],
0
,
new
char
[
INITIAL_URI_SIZE
],
0
,
new
char
[
INITIAL_PROTOCOL_SIZE
],
0
);
}
public
HttpRequestLine
(
char
[]
method
,
int
methodEnd
,
char
[]
uri
,
int
uriEnd
,
char
[]
protocol
,
int
protocolEnd
)
{
this
.
method
=
method
;
this
.
methodEnd
=
methodEnd
;
this
.
uri
=
uri
;
this
.
uriEnd
=
uriEnd
;
this
.
protocol
=
protocol
;
this
.
protocolEnd
=
protocolEnd
;
}
public
int
indexOf
(
String
str
)
{
// ...
return
-
1
;
}
/**
* Release all object references, and initialize instance variables, in
* preparation for reuse of this object.
*/
public
void
recycle
()
{
methodEnd
=
0
;
uriEnd
=
0
;
protocolEnd
=
0
;
}
}
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/connector/HttpResponse.java
浏览文件 @
4cf0ce05
...
...
@@ -15,6 +15,7 @@ import java.util.Locale;
public
class
HttpResponse
implements
HttpServletResponse
{
OutputStream
output
;
HttpRequest
request
;
PrintWriter
writer
;
public
HttpResponse
(
OutputStream
output
)
{
this
.
output
=
output
;
...
...
@@ -24,6 +25,18 @@ public class HttpResponse implements HttpServletResponse {
this
.
request
=
request
;
}
/**
* call this method to send headers and response to the output
*/
public
void
finishResponse
()
{
// sendHeaders();
// Flush and close the appropriate output mechanism
if
(
writer
!=
null
)
{
writer
.
flush
();
writer
.
close
();
}
}
public
void
addCookie
(
Cookie
cookie
)
{
}
...
...
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/connector/SocketInputStream.java
浏览文件 @
4cf0ce05
...
...
@@ -5,24 +5,85 @@ import java.io.InputStream;
/**
* Created by shenhongxi on 16/4/11.
* Extends InputStream to be more efficient reading lines during HTTP header processing.
*/
public
class
SocketInputStream
extends
InputStream
{
/**
* Underlying input stream.
*/
private
InputStream
input
;
private
int
size
;
/**
* Internal buffer.
*/
protected
byte
[]
buf
;
public
SocketInputStream
(
InputStream
input
,
int
size
)
{
/**
* Last valid byte.
*/
protected
int
count
;
/**
* Position in the buffer.
*/
protected
int
pos
;
public
SocketInputStream
(
InputStream
input
,
int
bufferSize
)
{
this
.
input
=
input
;
this
.
size
=
size
;
this
.
buf
=
new
byte
[
bufferSize
];
}
// input => buf => HttpRequestLine
public
void
readRequestLine
(
HttpRequestLine
requestLine
)
throws
IOException
{
// Recycling check
if
(
requestLine
.
methodEnd
!=
0
)
requestLine
.
recycle
();
// Checking for a blank line
// Reading the method name
// Reading URI
// Reading protocol
}
public
void
readHeader
(
HttpHeader
httpHeader
)
{
// input => buf => HttpHeader
public
void
readHeader
(
HttpHeader
header
)
throws
IOException
{
// Recycling check
if
(
header
.
nameEnd
!=
0
)
header
.
recycle
();
// Checking for a blank line
// Reading the header name
// Reading the header value (which can be spanned over multiple lines)
}
@Override
public
int
read
()
throws
IOException
{
return
0
;
if
(
pos
>=
count
)
{
fill
();
if
(
pos
>=
count
)
return
-
1
;
}
return
buf
[
pos
++]
&
0xff
;
}
/**
* Fill the internal buffer using data from the undelying input stream.
*/
protected
void
fill
()
throws
IOException
{
pos
=
0
;
count
=
0
;
int
nRead
=
input
.
read
(
buf
,
0
,
buf
.
length
);
if
(
nRead
>
0
)
{
count
=
nRead
;
}
}
}
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/servlet/RequestFacade.java
浏览文件 @
4cf0ce05
...
...
@@ -15,7 +15,7 @@ public class RequestFacade implements ServletRequest {
private
ServletRequest
request
;
public
RequestFacade
(
Servlet
Request
request
)
{
public
RequestFacade
(
Request
request
)
{
this
.
request
=
request
;
}
...
...
whatsmars-tomcat/src/main/java/com/whatsmars/tomcat/servlet/ResponseFacade.java
浏览文件 @
4cf0ce05
...
...
@@ -13,7 +13,7 @@ public class ResponseFacade implements ServletResponse {
private
ServletResponse
response
;
public
ResponseFacade
(
Servlet
Response
response
)
{
public
ResponseFacade
(
Response
response
)
{
this
.
response
=
response
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录