提交 4cf0ce05 编写于 作者: 武汉红喜's avatar 武汉红喜

connector

上级 3deeb4f6
......@@ -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;
}
}
......@@ -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.getUri().startsWith("/servlet/")) {
if (request.getRequestURI().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;
}
}
......@@ -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 null;
return protocol;
}
public String getScheme() {
......
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;
}
}
......@@ -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) {
}
......
......@@ -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;
}
}
}
......@@ -15,7 +15,7 @@ public class RequestFacade implements ServletRequest {
private ServletRequest request;
public RequestFacade(ServletRequest request) {
public RequestFacade(Request request) {
this.request = request;
}
......
......@@ -13,7 +13,7 @@ public class ResponseFacade implements ServletResponse {
private ServletResponse response;
public ResponseFacade(ServletResponse 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.
先完成此消息的编辑!
想要评论请 注册