⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linkedthread.java

📁 能调用CGI的简易WEBserver 设计
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.util.Date;
/* 
 * thread LinkedThread use to deal with the request from client.
 *Author  
 *2008/12/30
 */

public class LinkedThread
{
	Socket client;
	
  public LinkedThread(Socket Csocket)
	{
		client=Csocket;
	}
	
  public void run()
	{
		try
		{
		  PrintStream outStream =new PrintStream (client.getOutputStream());
		  BufferedReader inStream=new BufferedReader(new InputStreamReader(client.getInputStream()));
		  String Quest=inStream.readLine().trim();
		  System.out.println("Received:"+Quest);
		  
		  if(Quest.substring(0,3).equals("GET"))
		  {
		  	//	Boolean cgiflag = true;
		  		String fileName = getfilename(Quest);
		  		int point=fileName.indexOf(".");
		  		String ifCGI = fileName.substring(point+1,point+4);
		  	//	System.out.println(ifCGI);
		  		if(ifCGI.equals("cgi"))
		  		{
		  			Runtime run = Runtime.getRuntime();
		  			Process CGI = run.exec(fileName);
		  			BufferedReader CGIstream = new BufferedReader(new InputStreamReader(CGI.getInputStream()));
		  			String content = CGIstream.readLine();
		  			String content_part=CGIstream.readLine();
		  			while(content_part!=null)
		  			{
		  				content+=content_part;
		  				content_part=CGIstream.readLine();
		  			}
		  			System.out.println(content);
		  			System.out.println(fileName+" requested.");
		  			outStream.println("HTTP/1.0 200 OK");
					outStream.println("MIME_version:1.0");
					outStream.println("Content_Type:text/html");
					int len=content.length();
					outStream.println("Content_Length:"+len);
					outStream.println("");
					outStream.println(content);
					outStream.flush();
		  			
		  		}
		  		else
		  		{
		  			File file=new File(fileName);
		  			if(file.exists())
		  			{
		  			    Date date=new Date();
		  				String dateTemp=date.toString();
		  				
				    	outStream.println("HTTP/1.0 200 OK");
				    	outStream.println("Date: "+dateTemp+" GMT");
				    	outStream.println("Server: WEBServer 1.0");
					    outStream.println("Content_Type:text/html");
			    		int len=(int)file.length();
				    	outStream.println("Content_Length:"+len);
				    	outStream.println("");
			    		Send(outStream,file);
			    		outStream.flush();
		  			}
		  			else
		  			{   
		  			    Date date=new Date();
		  				String dateTemp=date.toString();
		    			outStream.println("HTTP/1.0 404 NO FOUND");
		    			outStream.println("Date: "+dateTemp+" GMT");
		    			outStream.println("Server: WEBserver 1.0");
		    			outStream.println("Content Type: NO ");
	    				outStream.println("Content_Length: 0");
	    				outStream.println("");
	    				outStream.println("ERROR : FILE "+fileName+" NO FOUND");
		    			outStream.flush();
		  			}
		  		}
		  		
		  }
		  
		  client.close();
			
		}
		catch(IOException e)
		{
			System.out.println("Exception"+e);
		}
	}
	
	
	String getfilename(String s)
	{
		int first_null=s.indexOf(' ');
		int second_null=s.indexOf(' ',first_null+1);
		int other1=s.indexOf('?',first_null);
		int other2=s.indexOf('%',first_null);
	//	System.out.println(first_null);
	//	System.out.println(second_null);
	//	System.out.println(other1);
	///	System.out.println(other2);
	//	
	//	int other3=s.indexOf('/',first_null);
		if(second_null>other1&&other1!=-1)
			second_null=other1;
		if(second_null>other2&&other2!=-1)
		    second_null=other2;
		//if(second_null>other3)
		   // second_null=other3;
		   
		 //  System.out.println(second_null);
		    
		
		
		String filename=s.substring(first_null+1,second_null);
	   // System.out.println(filename);
		try
		{
			if(filename.charAt(0)=='/')
				filename=filename.substring(1);
		}
		catch (StringIndexOutOfBoundsException e)
		{
			System.out.println("Exception"+e);
		}
		if(filename.equals(""))
			filename="index.html";
		return filename;
	}
	
	
	void Send(PrintStream outs,File file)
	{
		try
		{
			DataInputStream in = new DataInputStream(new FileInputStream(file));
			int len=(int)file.length();
			byte buff[]=new byte[len];
			in.readFully(buff);
			outs.write(buff,0,len);
			outs.flush();
			in.close();
		}
		catch(Exception e)
		{
			System.out.println("Error retrieving file.");
		}
	}
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -