提交 a1fdb68a 编写于 作者: F fanjiefeng

增加长头部支持

上级 2c48cb60
......@@ -5,5 +5,5 @@
<classpathentry kind="src" path="lib"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="tools.jar"/>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="output" path="src"/>
</classpath>
......@@ -13,7 +13,7 @@ public class ConsolePrintStream extends PrintStream {
private PrintStream oldPrintStream;
private SiftedQueue infoQueue;
private ArrayList<String> infos;
private boolean isLog=true;
public boolean isLog=true;
public ConsolePrintStream(File file) throws FileNotFoundException {
this(file, null);
}
......@@ -23,18 +23,19 @@ public class ConsolePrintStream extends PrintStream {
infoQueue=new SiftedQueue(200);
infos=new ArrayList<String>();
}
@Override
public void println(String text) {
if(isLog) {
Date date = new Date();
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
text=formatter.format(date)+": "+text;
text=formatter.format(date)+":\r\n"+text;
if(oldPrintStream!=null) {
oldPrintStream.println(text);
}
super.println(text);
infoQueue.push(text);
if(text.indexOf("/@ServerConsole")<0) {
infoQueue.push(text);
}
}
}
......@@ -50,4 +51,9 @@ public class ConsolePrintStream extends PrintStream {
}
return infos;
}
public void pushInfos(String[] infos) {
for(int i=0;i<infos.length;i++) {
infoQueue.push(infos[i]);
}
}
}
......@@ -373,16 +373,36 @@ input{
position: absolute;
visibility: hidden;
}
.Console {
#ConsoleMain{
text-align: left;
background: rgb(240,240,240);
}
.Console #ConsoleText{
width: 100%;
width: 99%;
height:400px;
overflow-y: scroll;
color: rgb(240,240,240);
background: rgb(0,0,0);
}
.Console .bottom{
width: 100%;
height: 80px;
text-align: left;
}
.Console #AnalogHTTP{
width:94%;
height:80px;
overflow-y: scroll;
color: rgb(0,0,0);
background: rgb(240,240,240);
}
.Console #AnalogButton{
position: absolute;
width: 30px;
height: 86px;
right: 0px;
bottom: 4px;
}
#ListDiv{
position: absolute;
width: 100%;
......
......@@ -247,8 +247,12 @@
<div id="fileHandle"></div>
<script src="js/file/file.js" type="text/javascript" charset="utf-8"></script>
</div>
<div class="Console content">
<div class="Console content" id="ConsoleMain">
<textarea id="ConsoleText" rows="" cols="" readonly="true"></textarea>
<div class="bottom">
<textarea id="AnalogHTTP" rows="" cols="" spellcheck="false"></textarea>
<button type="button" id="AnalogButton" onclick="analogHttp()"><br><br><br></button>
</div>
<script src="js/console/console.js" type="text/javascript" charset="utf-8"></script>
</div>
<div class="Help content">
......
......@@ -13,10 +13,18 @@ function showInfos(text) {
var cons = document.getElementById("ConsoleText");
for(let i=0;i<infos.length;i++){
var info = infos[i];
if(info.indexOf("/@ServerConsole") < 0){
cons.innerHTML += infos[i] + "\r\n";
}
cons.innerHTML += infos[i] + "\r\n";
}
if(infos.length > 0){
cons.scrollTop = cons.scrollHeight;
}
}
function analogHttp() {
var analogHttp = document.getElementById("AnalogHTTP");
var analogText = analogHttp.value;
analogText = analogText.replace("\n","\r\n");
send("POST","/@ServerConsole",true,analogText);
}
setInterval(getInfos, 500);
......
......@@ -41,4 +41,14 @@ public class ConsoleApplication extends Application {
return consolePrintStream.getInfos(20);
}
}
public void pushInfos(String[] infos) {
if(consolePrintStream!=null) {
consolePrintStream.pushInfos(infos);
}
}
public void setIsLog(boolean isLog) {
if(consolePrintStream!=null) {
consolePrintStream.isLog=isLog;
}
}
}
......@@ -34,6 +34,8 @@ public class ManageServer extends ApplicationServer {
Object logined = null;
if (session != null) {
logined = httpRequest.getSession().getData("logined");
}else {
this.getWebProject().getSessionServer().createSession(httpRequest, httpResponse);
}
if (url.equals("/@login")) {
String[] strs = httpRequest.getBody().split("=");
......
package servlet;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import application.ConsoleApplication;
import pub.fjf.http.HttpRequest;
import pub.fjf.http.HttpResponse;
import pub.fjf.http.HttpServlet;
import pub.fjf.http.HttpServletRequest;
import pub.fjf.http.HttpServletResponse;
......@@ -27,7 +32,27 @@ public class ServerConsole extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
byte[] bytes=null;
((HttpRequest)req).readWholeBody();
try {
bytes = req.getBody().getBytes(req.getCharset());
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
HttpRequest analogReq=new HttpRequest(bytes,((HttpRequest)req).getIs(),req.getUrlCharset(),req.getCharset());
analogReq.setClient(req.getClient());
HttpResponse analogResp=new HttpResponse((OutputStream)null,req.getCharset());
System.out.println("ģ:\r\n"+analogReq.toHttpText());
analogResp.setHttpRequset(analogReq);
req.getClient().getServer().handleHttpRequest(analogReq, analogResp);
System.out.println("ģӦ:\r\n"+analogResp.toHttpText());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
resp.sendData("OK");
}
}
......@@ -3,6 +3,7 @@ package pub.fjf.http;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import pub.fjf.tools.Bytes;
......@@ -131,6 +132,51 @@ public class HttpRequest implements HttpServletRequest{
throw new IOException();
}
}
public static byte[] readEndHead(int receiveTimeOut, InputStream is) throws IOException{
Bytes headAndBody=new Bytes("\r\n\r\n".getBytes());
long lastReceiveTime=System.currentTimeMillis();
boolean headEnd=false;
byte[][] bytesList=new byte[16][];
int bytesCount=0;
int byteCount=0;
byte[] bytes=null;
while(!headEnd) {
if(is.available()>0){
if(bytesCount>=bytesList.length) {
throw new IOException("头部过长,超过"+byteCount+"字节!");
}
bytes=new byte[is.available()];
bytesList[bytesCount]=bytes;
is.read(bytes);
byteCount+=bytes.length;
bytesCount++;
if(new Bytes(bytes).indexOf(headAndBody)>=0) {
headEnd=true;
}
lastReceiveTime=System.currentTimeMillis();
}else {
if(System.currentTimeMillis()-lastReceiveTime>receiveTimeOut) {
break;
}
}
}
if(bytesCount>0) {
if(bytesCount>1) {
bytes=new byte[byteCount];
int b=0;
for(int i=0;i<bytesCount;i++) {
for(int j=0;j<bytesList[i].length;j++) {
bytes[b]=bytesList[i][j];
b++;
}
}
}
}else {
throw new IOException();
}
return bytes;
}
public String getCharset() {
return charset;
}
......@@ -258,6 +304,32 @@ public class HttpRequest implements HttpServletRequest{
return new String(this.body);
}
}
public void readWholeBody() {
if(this.haveData()) {
StringBuffer sb=new StringBuffer();
if(this.body!=null) {
try {
sb.append(new String(this.body,charset));
} catch (UnsupportedEncodingException e) {
sb.append(new String(this.body));
}
}
while(this.haveData()) {
byte[] bytes=this.readData();
try {
sb.append(new String(bytes,charset));
} catch (UnsupportedEncodingException e) {
sb.append(new String(bytes));
}
}
String bodyString=sb.toString();
try {
this.body=bodyString.getBytes(charset);
} catch (UnsupportedEncodingException e) {
this.body=bodyString.getBytes();
}
}
}
/**
* 设置该请求报文的请求体文本
* @param body 请求体文本
......@@ -300,6 +372,13 @@ public class HttpRequest implements HttpServletRequest{
public void setSession(Session session) {
this.session = session;
}
public InputStream getIs() {
return is;
}
public void setIs(InputStream is) {
this.is = is;
}
/**
* 判断是否还有数据未接收
*/
......
......@@ -291,15 +291,17 @@ public class HttpResponse implements HttpServletResponse{
this.setStateCode("100");
}
}
try {
this.os.write(bytes);
this.os.flush();
result=true;
} catch (IOException e) {
// TODO Auto-generated catch block
// if(Monitor.HttpMonitor.using) {
// Monitor.HttpMonitor.getInform("发送应答报文失败!\n");
// }
if(this.os!=null) {
try {
this.os.write(bytes);
this.os.flush();
result=true;
} catch (IOException e) {
// TODO Auto-generated catch block
// if(Monitor.HttpMonitor.using) {
// Monitor.HttpMonitor.getInform("发送应答报文失败!\n");
// }
}
}
return result;
}
......
......@@ -292,7 +292,6 @@ public class HttpServer {
OutputStream os=null;
HttpRequest httpRequest=null;
HttpResponse httpResponse=null;
byte[] bytes=null;
long lastReceiveTime=System.currentTimeMillis();
String connection=null;
boolean keepAlive=true;
......@@ -302,10 +301,9 @@ public class HttpServer {
os=clientSocket.getOutputStream();
while(clientSocket.isConnected()&&keepAlive){
if(is.available()>0){
bytes=new byte[is.available()];
is.read(bytes);
httpResponse=new HttpResponse(os,this.charset);
try {
byte[] bytes=HttpRequest.readEndHead(receiveTimeOut, is);
if(StringTools.STR2str(this.urlCharset).equals("default")) {
httpRequest=new HttpRequest(bytes,is,this.charset,this.charset);
}else {
......@@ -356,7 +354,7 @@ public class HttpServer {
}
}
private void handleHttpRequest(HttpRequest httpRequest,HttpResponse httpResponse){
public void handleHttpRequest(HttpRequest httpRequest,HttpResponse httpResponse){
boolean handed=false;
String url=httpRequest.getUrl();
String restUrl=null;
......
......@@ -74,6 +74,16 @@ public class SessionServer extends HttpRequestFilter {
}
}
}
public Session createSession(HttpRequest httpRequest, HttpResponse httpResponse) {
Session session=new Session();
this.sessionMap.put(session.getSessionID(), session);
System.out.println(this.getWebProject().getName()+" 会话"+session.getSessionID()+"增加,目前会话数:"+this.getSessionCount());
String setCookie=this.SESSION_ID+"="+session.getSessionID();
httpResponse.setKeyValue("Set-Cookie", new String(Base64.code(setCookie, httpResponse.getCharset())));
httpRequest.setSession(session);
return session;
}
@Override
synchronized public boolean handleHttpRequest(String url, HttpRequest httpRequest, HttpResponse httpResponse) {
......@@ -102,15 +112,11 @@ public class SessionServer extends HttpRequestFilter {
}
}
}
}
if(session==null) {
session=new Session();
this.sessionMap.put(session.getSessionID(), session);
System.out.println(this.getWebProject().getName()+" 会话"+session.getSessionID()+"增加,目前会话数:"+this.getSessionCount());
String setCookie=this.SESSION_ID+"="+session.getSessionID();
httpResponse.setKeyValue("Set-Cookie", new String(Base64.code(setCookie, httpResponse.getCharset())));
}else {
session.setLastVisitedTime(System.currentTimeMillis());
if(session==null) {
session=createSession(httpRequest, httpResponse);
}else {
session.setLastVisitedTime(System.currentTimeMillis());
}
}
httpRequest.setSession(session);
return result;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册