<commit_before>package org.voovan.test.http;

import java.io.IOException;
import java.nio.ByteBuffer;

import org.voovan.http.server.HttpRequest;
import org.voovan.http.server.HttpServer;
import org.voovan.http.server.websocket.WebSocketBizHandler;
import org.voovan.tools.TDateTime;
import org.voovan.tools.TFile;
import org.voovan.tools.log.Logger;

public class HttpServerDemo {
	private static byte[] fileContent = TFile.loadResource("org/voovan/test/http/test.htm");
	
	
	public static void main(String[] args) {
		try {
			HttpServer httpServer = HttpServer.newInstance();
			
			//普通 GET 请求
			httpServer.get("/", (req, resp) -> {
				if (req.getSession() != null && req.getSession().getAttributes("Time") != null) {
					Logger.simple("Session saved time is: " + req.getSession().getAttributes("Time"));
				}
				req.getSession().setAttribute("Time", TDateTime.now());

				resp.write(fileContent);
				resp.write("{"
						+ "\"Method\":\"NormalGET\","
						+ "\"name\":\""+req.getParameter("name")+"\","
						+ "\"age\":\""+req.getParameter("age")+"\""
				 + "}");
			});
			
			//带路劲参数的 GET 请求
			httpServer.get("/:name/:age", (req, resp) -> {
				if (req.getSession() != null && req.getSession().getAttributes("Time") != null) {
					Logger.simple("Session saved time is: " + req.getSession().getAttributes("Time"));
				}System.out.println(req);
				req.getSession().setAttribute("Time", TDateTime.now());
				resp.write(fileContent);
				resp.write("{"
								+ "\"Method\":\"PathGET\","
								+ "\"name\":\""+req.getParameter("name")+"\","
								+ "\"age\":\""+req.getParameter("age")+"\""
						 + "}");
			});
			
			
			// 重定向
			httpServer.get("/redirect", (req, resp) -> {
				resp.redirct("http://www.baidu.com");
			});

			//普通 POST 请求
			httpServer.post("/", (req, resp) -> {
				if (req.getSession() != null && req.getSession().getAttributes("Time") != null) {
					Logger.simple("Session saved time is: " + req.getSession().getAttributes("Time"));
				}
				req.getSession().setAttribute("Time", TDateTime.now());
				resp.write(fileContent);
				String contentType = req.header().get("Content-Type").split(";")[0];
				resp.write("{"
						+ "\"Method\":\""+contentType+"\","
						+ "\"name\":\""+req.getParameter("name")+"\","
						+ "\"age\":\""+req.getParameter("age")+"\""
				 + "}");
			});
			
			httpServer.socket("/websocket", new WebSocketBizHandler() {
				
				@Override
				public ByteBuffer onRecived(HttpRequest upgradeRequest, ByteBuffer message) {
					Logger.info(new String(message.array()));
					String msg = "This is server message. Client message: \r\n\t\""+new String(message.array())+"\"";
					return ByteBuffer.wrap(msg.getBytes());
				}
				
				@Override<commit_msg>测试代码修改<commit_after>package org.voovan.test.http;

import java.io.IOException;
import java.nio.ByteBuffer;

import org.voovan.http.server.HttpRequest;
import org.voovan.http.server.HttpServer;
import org.voovan.http.server.websocket.WebSocketBizHandler;
import org.voovan.tools.TDateTime;
import org.voovan.tools.TFile;
import org.voovan.tools.log.Logger;

public class HttpServerDemo {
	private static byte[] fileContent = TFile.loadResource("org/voovan/test/http/test.htm");
	
	
	public static void main(String[] args) {
		try {
			HttpServer httpServer = HttpServer.newInstance();
			
			//普通 GET 请求
			httpServer.get("/", (req, resp) -> {
				Logger.simple(req.getRemoteAddres()+":"+req.getRemotePort());
				if (req.getSession() != null && req.getSession().getAttributes("Time") != null) {
					Logger.simple("Session saved time is: " + req.getSession().getAttributes("Time"));
				}
				req.getSession().setAttribute("Time", TDateTime.now());

				resp.write(fileContent);
				resp.write("{"
						+ "\"Method\":\"NormalGET\","
						+ "\"name\":\""+req.getParameter("name")+"\","
						+ "\"age\":\""+req.getParameter("age")+"\""
				 + "}");
			});
			
			//带路劲参数的 GET 请求
			httpServer.get("/:name/:age", (req, resp) -> {
				Logger.simple(req.getRemoteAddres()+":"+req.getRemotePort());
				if (req.getSession() != null && req.getSession().getAttributes("Time") != null) {
					Logger.simple("Session saved time is: " + req.getSession().getAttributes("Time"));
				}System.out.println(req);
				req.getSession().setAttribute("Time", TDateTime.now());
				resp.write(fileContent);
				resp.write("{"
								+ "\"Method\":\"PathGET\","
								+ "\"name\":\""+req.getParameter("name")+"\","
								+ "\"age\":\""+req.getParameter("age")+"\""
						 + "}");
			});
			
			
			// 重定向
			httpServer.get("/redirect", (req, resp) -> {
				Logger.simple(req.getRemoteAddres()+":"+req.getRemotePort());
				resp.redirct("http://www.baidu.com");
			});

			//普通 POST 请求
			httpServer.post("/", (req, resp) -> {
				Logger.simple(req.getRemoteAddres()+":"+req.getRemotePort());
				if (req.getSession() != null && req.getSession().getAttributes("Time") != null) {
					Logger.simple("Session saved time is: " + req.getSession().getAttributes("Time"));
				}
				req.getSession().setAttribute("Time", TDateTime.now());
				resp.write(fileContent);
				String contentType = req.header().get("Content-Type").split(";")[0];
				resp.write("{"
						+ "\"Method\":\""+contentType+"\","
						+ "\"name\":\""+req.getParameter("name")+"\","
						+ "\"age\":\""+req.getParameter("age")+"\""
				 + "}");
			});
			
			httpServer.socket("/websocket", new WebSocketBizHandler() {
				
				@Override
				public ByteBuffer onRecived(HttpRequest upgradeRequest, ByteBuffer message) {
					Logger.info(new String(message.array()));
					String msg = "This is server message. Client message: \r\n\t\""+new String(message.array())+"\"";
					return ByteBuffer.wrap(msg.getBytes());
				}
				
				@Override
{'fraction_non_alphanumeric': 0.2183406113537118, 'fraction_numerical': 0.00034934497816593884, 'mean_word_length': 3.442203258339798, 'pattern_counts': {'":': 22, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2009955', 'n_tokens_mistral': 2322, 'n_tokens_neox': 1896, 'n_words': 279}