Skip to content

Commit

Permalink
'fix:修复解析HTTP请求正则错误'
Browse files Browse the repository at this point in the history
  • Loading branch information
KeKe12030 committed Aug 31, 2020
1 parent 71703fd commit bc2f9bc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
2 changes: 1 addition & 1 deletion demo/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Main {

public static void main(String[] args) {

NAServer naServer = new NAServer(50000);
NAServer naServer = new NAServer(50001);
naServer.setHandler(new NAServerHandler() {
@Override
public void handle(Request request, Response response) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/nullatom/httpserver/NAServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void start() {
throw new RuntimeException("请添加NAServerHandler");
}
try {
serverSocket = new ServerSocket(50000);
serverSocket = new ServerSocket(port);
isRunning = true;
receive();
} catch (IOException e) {
Expand Down
58 changes: 32 additions & 26 deletions src/com/nullatom/httpserver/utils/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class Request {
//状态
private boolean isOK = true;//链接是否正常

private final String pattern = "content-length:\\s(\\d+)";
private final String pattern = "content-length:\\s(\\d+)\r\n";
private final Pattern r = Pattern.compile(pattern);
private Matcher m = null;

Expand All @@ -50,36 +50,42 @@ public class Request {
public Request(InputStream is) {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
parameterMap = new HashMap<String,List<String>>();
byte[] bytes = new byte[1024];
int len = 0;
int temp = 0;
byte[] bytes = new byte[1024];//一次读取1024字节
StringBuilder requestInfoSb = new StringBuilder();
try {
String[] infos = null;//存放数据大小的数组,[1]是数据大小,记得trim一下再转换为Integer
while(true){
String info = br.readLine();
requestInfoSb.append(info+CRLF);
if (this.method==null && info.contains("/")) {
this.method = info.substring(0, info.indexOf("/")).toLowerCase().trim();//获取请求方法
}
String group = "";
if(this.method.equals("post")) {//如果是POST方式,则\r\n\r\n有参数
if (info.toLowerCase().contains("content-length:")) {
m = r.matcher(info.toLowerCase());
if (m.matches()) {
group = m.group();
infos = group.split(":");
} else {
continue;
}
while((temp=is.read(bytes))!=-1){
String[] infos = null;//存放数据大小的数组,[1]是数据大小,记得trim一下然后再换为Integer
requestInfoSb.append(new String(bytes,0,temp));

if (requestInfoSb.toString().contains("\r\n\r\n")) {//如果头部信息读取完毕
if(this.method==null) {
this.method = requestInfoSb.substring(0, requestInfoSb.indexOf("/")).toLowerCase().trim();//获取请求方法
}
//如果sb的大小等于Content-length的大小,则结束循环
if (infos != null && Integer.valueOf(infos[1].trim()) == requestInfoSb.substring(requestInfoSb.indexOf("\r\n\r\n") + 2, requestInfoSb.length()).toString().getBytes().length) {
break;
if (infos==null) {
String group = "";
if (this.method.equals("post")) {//如果是POST方式,则\r\n\r\n有参数
if (requestInfoSb.toString().toLowerCase().contains("content-length:")) {
m = r.matcher(requestInfoSb.toString().toLowerCase());
while(m.find()) {
infos = m.group().split(":");
}
}
} else {//如果是GET或者其他方式,则是以\r\n\r\n结尾的
if (requestInfoSb.toString().contains("\r\n\r\n")) {
break;
}
}
}
}else{//如果是GET或者其他方式,则是以\r\n\r\n结尾的
if(requestInfoSb.toString().contains("\r\n\r\n")){

break;
}
if(infos != null){
//如果sb的大小等于Content-length的大小,则结束循环
if (infos != null ) {
if (Integer.valueOf(infos[1].trim()) == requestInfoSb.substring(requestInfoSb.indexOf("\r\n\r\n") + 4, requestInfoSb.length()).toString().getBytes().length) {
//如果包内的内容长度和报头的content-length长度一样,则停止读取
break;
}
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/com/nullatom/httpserver/utils/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class Response {

private final static String CRLF = "\r\n";//回车


private String responseType = "text/html";//默认返回内容为HTML

/**
* 无参构造器,初始化变量
* */
Expand Down Expand Up @@ -118,9 +121,17 @@ private void createHeadInfo(int code) {
//2、响应头(最后一行存在空行):
headInfo.append("Date:").append(new Date()).append(CRLF);
headInfo.append("Server:").append("NAHTTPServer;charset=UTF-8").append(CRLF);
headInfo.append("Content-type:text/html").append(CRLF);
headInfo.append("Content-type:"+responseType).append(CRLF);
headInfo.append("Content-length:").append(len).append(CRLF);
headInfo.append(CRLF);
}

/**
* 设置服务器返回给客户端的返回内容类型,默认为 text/html
* @param responseType 需要设置的返回类型
* */
public void setResponseType(String responseType) {
this.responseType = responseType;
}

}

0 comments on commit bc2f9bc

Please sign in to comment.