Questa pagina è usata per testare l'introduzione dei MOOC su Wikiversità e proviene da en.wikiversity. Non modificarla se non sai quello che stai facendo
/* * Copyright (C) 2013 Rene Pickhardt * Contact via http://www.rene-pickhardt.de or rene@rene-pickhardt.de * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */packagedemo;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.Reader;importjava.net.ServerSocket;importjava.net.Socket;/** * The purpose is to demonstrate how to build a simple Web Server on top * of TCP / IP. Therefore, we just make a very simple HTTP GET request. */publicclassSimpleWebServer{/** * @param args */publicstaticvoidmain(String[]args){try{ServerSockethttpServerSocket=newServerSocket(8080);SocketincommingHttpConnection=null;while((incommingHttpConnection=httpServerSocket.accept())!=null){BufferedReaderbr=newBufferedReader(newInputStreamReader(incommingHttpConnection.getInputStream()));Stringline=br.readLine();// GET /test/simiple.html HTTP/1.0Stringpath=processFirstLine(line,incommingHttpConnection);if(path!=null){SimpleWebRequestrequest=newSimpleWebRequest(path);while((line=br.readLine())!=null){if(line.equals("")){makeResponse(request,incommingHttpConnection);break;}// Example// Host:studywebscience.org\r\nrequest.addHeaderField(line);}}br.close();incommingHttpConnection.close();incommingHttpConnection=null;}}catch(IOExceptione){// TODO Auto-generated catch blocke.printStackTrace();}}privatestaticvoidmakeResponse(SimpleWebRequestrequest,SocketincommingHttpConnection){// /test/simple.htmlStringbody=fetchAsciiFile(request.getPath());if(body==null){makeErrorResponse("404 Not Found",incommingHttpConnection);}else{SimpleWebResponseresp=newSimpleWebResponse(incommingHttpConnection);resp.setStatusCode("200 Ok");resp.setBody(body);resp.writeToSocket();}}privatestaticStringfetchAsciiFile(Stringpath){try{StringBuilderbody=newStringBuilder(100000);BufferedReaderbr=newBufferedReader(newFileReader(newFile(System.getProperty("user.dir")+"/webdirectory"+path)));Stringtmp=null;while((tmp=br.readLine())!=null){body.append(tmp+"\n");}br.close();returnbody.toString();}catch(IOExceptione){// TODO Auto-generated catch blocke.printStackTrace();}returnnull;}privatestaticStringprocessFirstLine(Stringline,SocketincommingHttpConnection){if(line==null)returnnull;Stringpath=null;if(!line.startsWith("GET")&&line.split("\\s").length!=3){makeErrorResponse("400 Bad Request",incommingHttpConnection);}else{String[]values=line.split("\\s");path=values[1];if(!(values[2].equals("HTTP/1.0")||values[2].equals("HTTP/1.1"))){makeErrorResponse("400 Bad Request",incommingHttpConnection);}}returnpath;}privatestaticvoidmakeErrorResponse(StringerrorCode,SocketincommingHttpConnection){SimpleWebResponseresp=newSimpleWebResponse(incommingHttpConnection);resp.setStatusCode(errorCode);resp.writeToSocket();}}
/* * Copyright (C) 2013 Rene Pickhardt * Contact via http://www.rene-pickhardt.de or rene@rene-pickhardt.de * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */packagedemo;importjava.util.HashMap;/** * The purpose is to demonstrate how to build a simple Web Server on top * of TCP / IP. Therefore, we just make a very simple HTTP GET request. */publicclassSimpleWebRequest{privateHashMap<String,String>headerFields=null;publicSimpleWebRequest(Stringpath){headerFields=newHashMap<String,String>();headerFields.put("path",path);// TODO Auto-generated constructor stub}publicStringgetPath(){returnheaderFields.get("path");}publicvoidaddHeaderField(Stringline){intindex=line.indexOf(":");if(index<0)return;Stringkey=line.substring(0,index).trim();Stringvalue=line.substring(index+1,line.length());headerFields.put(key,value);}}
/* * Copyright (C) 2013 Rene Pickhardt * Contact via http://www.rene-pickhardt.de or rene@rene-pickhardt.de * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */packagedemo;importjava.io.IOException;importjava.io.OutputStream;importjava.net.Socket;importjava.util.Date;/** * The purpose is to demonstrate how to build a simple Web Server on top * of TCP / IP. Therefore, we just make a very simple HTTP GET request. */publicclassSimpleWebResponse{privateSocketconnection=null;privateStringstatusCode=null;privateStringbody="";publicSimpleWebResponse(SocketincommingHttpConnection){connection=incommingHttpConnection;}publicvoidsetStatusCode(StringstatusCode){this.statusCode=statusCode;}publicvoidsetBody(Stringbody){this.body=body;}publicvoidwriteToSocket(){try{OutputStreamos=connection.getOutputStream();Stringresponse="HTTP/1.0 "+statusCode+"\r\n";response+="Date:"+newDate(System.currentTimeMillis())+"\r\n";response+="\r\n";response+=body;os.write(response.getBytes());os.flush();os.close();}catch(IOExceptione){// TODO Auto-generated catch blocke.printStackTrace();}}}