//copyright Cycom Limited 1997 //package cycom; import java.util.*; import java.io.*; import java.net.UnknownHostException; import java.net.InetAddress; import java.net.URL; import java.net.FileNameMap; import java.net.ServerSocket; import java.net.Socket; import java.text.*; import java.rmi.*; import java.rmi.server.*; public class CycomServer { public static void main(String args[]){ try { CycomServer me = new CycomServer(); } catch (IOException ioe){ System.out.println("failed to create server socket"); ioe.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } } CycomServer() throws IOException { try { InetAddress inaddr = InetAddress.getLocalHost(); System.out.println("HostName="+inaddr.getHostName()); } catch(UnknownHostException uhe){ uhe.printStackTrace(); } ServerSocket sock = new ServerSocket(80); sock.setSoTimeout(30000); do { try { Socket con = sock.accept(); CycomServerHandler h = new CycomServerHandler(con); new Thread(h).start(); } catch (Exception e){ e.printStackTrace(); } } while(true); } } class CycomServerHandler implements FileNameMap, Runnable { Socket con=null; static final String GET="GET"; static final String HEAD="HEAD"; static final String PUT="PUT"; static final String POST="POST"; static final String DELETE="DELETE"; static final String DOCUMENTROOT="C:\\site3\\cycom"; static final String DEFAULTDOC="index.html"; static final String CHARSET8859_1 = "8859_1"; static final String BADRESPONSE = "HTTP/1.0 404 Not found\r\n"; static final String GOODRESPONSE = "HTTP/1.0 200 Ok\r\n"; static final String NOCONTENT = "HTTP/1.0 204 No Content\r\n"; static final String CREATED = "HTTP/1.0 201 Created\r\n"; static final String MOVEDTEMPORARILY = "HTTP/1.0 302 Moved Temporarily\r\n"; static final String TEXTHTML= "text/html"; static final String MOVEDMESSAGE= "
You missed off the trailing '/' from the directory name."+ " Try here
\r\n"; static final String NOTFOUNDMESSAGE = "The document that you requested does not exist.
\r\n"; static final String CONTENTLENGTH = "Content-Length:"; static final String CONTENTTYPE= "Content-Type:"; static final String CONTENTLOCATION= "Content-Location:"; static final String CONTENTMD5="Content-MD5:"; static final String LOCATION= "Location:"; static final String EOLPATT="\r\n"; static final String HTML=".html"; static final String HTM=".htm"; static final String JPG=".jpg"; static final String GIF=".gif"; static final String TXT=".txt"; static final String JAR=".jar"; static final String ZIP=".zip"; static final String CLASS=".class"; public int getInt(String s){ int n=0; try { n = Integer.parseInt(s); } catch(NumberFormatException nfe){ nfe.printStackTrace(); } return n; } public String getHeaderValue(String line, String key){ StringTokenizer st=new StringTokenizer(line," \t\n\r",false); String r = null; if(st.hasMoreTokens()){ String prop=st.nextToken(); if(prop.equalsIgnoreCase(key)){ if(st.hasMoreTokens()){ r=st.nextToken(); } } } return r; } public String getContentTypeFor(String fileName){ int li = fileName.lastIndexOf("."); String mimeType=null; if(li>0){ String ext = fileName.substring(li).toLowerCase(); if(HTML.equals(ext) || HTM.equals(ext)) mimeType = "text/html"; else if(JPG.equals(ext)) mimeType = "image/jpeg"; else if(GIF.equals(ext)) mimeType = "image/gif"; else if(JAR.equals(ext)) mimeType = "application/octet-stream"; else if(ZIP.equals(ext)) mimeType = "application/octet-stream"; else if(CLASS.equals(ext)) mimeType = "application/octet-stream"; } if(mimeType==null) mimeType="text/plain"; return mimeType; } CycomServerHandler(Socket con) throws java.net.SocketException { this.con = con; con.setSoTimeout(30000); } public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),CHARSET8859_1)); String firstLine=br.readLine(); System.out.println(firstLine); String hostname=null; String line; int contentLength=0; do {//read rest of header to first blank line line=br.readLine(); if(line==null || line.length()<=0) break; System.out.println(line); String v=null; if((v=getHeaderValue(line,"Host:"))!=null){ hostname= v; } else if((v=getHeaderValue(line,"Content-Length:"))!=null){ contentLength = getInt(v); } } while(line!=null && line.length()>0); StringTokenizer st=new StringTokenizer(firstLine," \t\n\r",false); OutputStream cos=con.getOutputStream(); String v=null; if((v=getHeaderValue(firstLine,GET))!=null){ sendFile(v,cos, true,hostname); } else if((v=getHeaderValue(firstLine,HEAD))!=null){ sendFile(v,cos, false, hostname); } else if((v=getHeaderValue(firstLine,PUT))!=null){ receiveFile(v,cos,br, hostname, contentLength); } else if((v=getHeaderValue(firstLine,POST))!=null){ postFile(v,cos,br, hostname, contentLength); } cos.flush(); cos.close(); con.close(); } catch(InterruptedIOException iioe){ //We are not busy so garbage collect System.out.println("gc"); Runtime.getRuntime().gc(); } catch(Exception e){ System.out.println(e.toString()); e.printStackTrace(); } } void debugWrite(OutputStream os, String s) throws IOException, UnsupportedEncodingException { System.out.print(s); os.write(s.getBytes(CHARSET8859_1)); System.out.flush(); os.flush(); } void sendString(OutputStream ostr, String mess, String mimeType) throws IOException { debugWrite(ostr,CONTENTLENGTH+mess.length()+EOLPATT); debugWrite(ostr,CONTENTTYPE+mimeType+EOLPATT); debugWrite(ostr,EOLPATT); debugWrite(ostr,mess); } void sendFile(String fileName, OutputStream ostr, boolean send, String hostname) throws IOException { if(fileName.endsWith("/")) fileName += DEFAULTDOC; String fName=(DOCUMENTROOT+fileName).replace('/',File.separatorChar); File f = new File(fName); if(f.isDirectory()){ debugWrite(ostr,MOVEDTEMPORARILY); String urlform = f.getCanonicalPath().replace('\\','/').substring(DOCUMENTROOT.length()); if(!urlform.endsWith("/")) urlform = urlform+"/"; MessageFormat mf=new MessageFormat(MOVEDMESSAGE); String trueUrl= "http://"+hostname+urlform; Object [] args = { trueUrl }; String mess = mf.format(args); debugWrite(ostr,LOCATION+trueUrl+EOLPATT); sendString(ostr,mess,TEXTHTML); } else if(!f.isFile()){ debugWrite(ostr,BADRESPONSE); sendString(ostr,NOTFOUNDMESSAGE,TEXTHTML); } else { debugWrite(ostr,GOODRESPONSE); String urlform = f.getCanonicalPath().replace('\\','/').substring(DOCUMENTROOT.length()); debugWrite(ostr,CONTENTLOCATION+"http://"+hostname+urlform+EOLPATT); debugWrite(ostr,CONTENTLENGTH+f.length()+EOLPATT); debugWrite(ostr,CONTENTTYPE+getContentTypeFor(f.getName())+EOLPATT); debugWrite(ostr,EOLPATT); if(send){ FileInputStream fis = new FileInputStream(f); byte buf [] = new byte[1024]; int n=0; while((n=fis.read(buf,0,1024))>0){ ostr.write(buf,0,n); } fis.close(); } } } void receiveFile(String fileName, OutputStream ostr, BufferedReader br, String hostname, int contentLength) throws IOException { boolean wantDir=false; if(fileName.endsWith("/")) wantDir=true; String fName=(DOCUMENTROOT+fileName).replace('/',File.separatorChar); File f = new File(fName); if(wantDir){ f.mkdirs(); } else { String parentName = f.getParent(); if(parentName!=null){ File parent = new File(parentName); if(!parent.exists()) parent.mkdirs(); } FileOutputStream fos = new FileOutputStream(f); byte buf [] = new byte[1024]; char cBuf[] = new char[1024]; int n=0; int tot=0; while(tot