📄 httpserverconnection.java
字号:
/**
*
*/
package edu.sysu.http.impl;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.SocketException;
import java.util.Calendar;
import java.util.Date;
import edu.sysu.http.intf.HttpConnection;
import edu.sysu.http.intf.HttpHandler;
import edu.sysu.http.util.HttpGrammarException;
import edu.sysu.http.util.HttpResourceException;
import edu.sysu.http.util.HttpService;
/**
* @author Administrator
*
*/
public class HttpServerConnection implements HttpConnection {
private BufferedReader input;
private BufferedWriter output;
private Socket socket;
/**
* @throws IOException
*
*/
public HttpServerConnection(Socket socket) throws IOException {
// TODO Auto-generated constructor stub
this.socket = socket;
this.setInput(new BufferedReader(new InputStreamReader(this.socket
.getInputStream(), "GBK")));
this.setOutput(new BufferedWriter(new OutputStreamWriter(this.socket
.getOutputStream())));
}
public HttpServerConnection() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) {
// TODO Auto-generated method stub
HttpRequestLine request = null;
try {
request = new HttpRequestLine("GET",
"http://127.0.0.1:8080/example.jpg");
} catch (HttpGrammarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpServerConnection conn = new HttpServerConnection();
conn.setInput(new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(request.toString().getBytes()))));
HttpRequest req = null;
try {
req = conn.parseRequest();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (HttpGrammarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String method = req.getRequestLine().getMethod().toString();
HttpHandler handler = null;
try {
handler = HttpService.getInstance().loadMethodClass(
"Http" + method.substring(0, 1)
+ method.substring(1).toLowerCase()
+ "Handler.class");
} catch (HttpResourceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Date date = (Date) Calendar.getInstance().getTime();
HttpResponse rep = handler.handle(req);
rep.setStatusLine(new HttpStatusLine("1.0", HttpStatus.SC_OK
.getStatusCode(), HttpStatus.SC_OK.getReasonPhrase()));
rep.getHeaderItems().add(
new HttpGeneralHeader("Date", date.toGMTString()));
rep.getHeaderItems().add(
new HttpEntityHeader("Content-Length", rep.getEntity()
.getLength().toString()));
rep.getHeaderItems().add(
new HttpEntityHeader("Content-Type", rep.getEntity()
.getContentType().toString()));
System.out.println(rep.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (HttpGrammarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void bind(String host, int port) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void close() throws IOException {
// TODO Auto-generated method stub
this.socket.close();
}
@Override
public String getHost() {
// TODO Auto-generated method stub
return this.socket.getInetAddress().getHostAddress();
}
@Override
public int getPort() {
// TODO Auto-generated method stub
return this.socket.getLocalPort();
}
public int getSoTimeout() throws IOException {
// TODO Auto-generated method stub
return this.socket.getSoTimeout();
}
@Override
public boolean isClosed() {
// TODO Auto-generated method stub
return this.socket.isClosed();
}
public boolean isConnected(){
return this.socket.isConnected();
}
public void setSoTimeout(int timeout) throws SocketException {
// TODO Auto-generated method stub
this.socket.setSoTimeout(timeout);
}
/**
* @param input
* the input to set
*/
public void setInput(BufferedReader input) {
this.input = input;
}
/**
* @return the input
*/
public BufferedReader getInput() {
return input;
}
/**
* @param bufferedWriter
* the output to set
*/
public void setOutput(BufferedWriter bufferedWriter) {
this.output = bufferedWriter;
}
/**
* @return the output
*/
public BufferedWriter getOutput() {
return output;
}
public Socket getSocket() {
return socket;
}
public String receive() throws IOException {
return this.input.readLine();
}
public void send(HttpResponse response) throws IOException {
this.output.write(response.toString(), 0, response.toString().length());
this.output.flush();
this.output.close();
}
public HttpRequest parseRequest() throws IOException, HttpGrammarException {
String[] res = this.receive().split("\\" + HttpRules.SP);
try {
if (res.length == 2) {
HttpRequest request = new HttpRequest(res[0], res[1]);
return request;
}
else
return null;
} catch (HttpGrammarException e) {
throw new HttpGrammarException("Method-Name invalid.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -