Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * HttpServer.java | ||
3 | */ | ||
4 | package lab.scd.net.httpexample; | ||
5 | |||
6 | /** | ||
7 | * Class created by @author Mihai HULEA at Feb 23, 2005. | ||
8 | * | ||
9 | * This class is part of the laborator2_sockettest project. | ||
10 | * | ||
11 | */ | ||
12 | import java.io.*; | ||
13 | import java.net.*; | ||
14 | import java.util.StringTokenizer; | ||
15 | |||
16 | public class HttpServer extends Thread | ||
17 | { | ||
18 | //portul standard | ||
19 | private final static int PORT = 80; | ||
20 | |||
21 | private final String iniContext="c:/temp/ServerHTTP/webdocs"; | ||
22 | private boolean alive; | ||
23 | |||
24 | private ServerSocket ss; | ||
25 | |||
26 | //constructor | ||
27 | HttpServer()throws Exception{ | ||
28 | System.out.println("Start server http."); | ||
29 | ss = new ServerSocket(PORT); | ||
30 | alive=true; | ||
31 | start(); | ||
32 | } | ||
33 | |||
34 | public void run(){ | ||
35 | while(alive){ | ||
36 | |||
37 | //asteapta conexiuni | ||
38 | try{ | ||
39 | System.out.println("Server asteapta..."); | ||
40 | new ProcesRequest(ss.accept(),iniContext); | ||
41 | |||
42 | }catch(IOException e){System.err.println("EROARE CONECTARE:"+e.getMessage());} | ||
43 | //..reia bucla de asteptare dupa ce am creat un fir pentru client | ||
44 | } | ||
45 | System.out.println("STOP SERVER"); | ||
46 | } | ||
47 | public static void main(String[] args)throws Exception | ||
48 | { | ||
49 | try{ | ||
50 | new HttpServer(); | ||
51 | }catch(Exception e){e.printStackTrace();} | ||
52 | } | ||
53 | |||
54 | } | ||
55 | |||
56 | |||
57 | |||
58 | class ProcesRequest extends Thread | ||
59 | { | ||
60 | private PrintWriter outStr; | ||
61 | private BufferedReader inStr; | ||
62 | private Socket s; | ||
63 | private DataOutputStream dout; | ||
64 | private String iniContext; | ||
65 | |||
66 | ProcesRequest(Socket s, String iContext){ | ||
67 | try{ | ||
68 | outStr = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true); | ||
69 | inStr = new BufferedReader(new InputStreamReader(s.getInputStream())); | ||
70 | dout = new DataOutputStream(s.getOutputStream()); | ||
71 | iniContext = iContext; | ||
72 | this.s = s; | ||
73 | start(); | ||
74 | }catch(IOException e) | ||
75 | {System.err.println("EROARE CONECTARE: "+e.getMessage());} | ||
76 | } | ||
77 | |||
78 | public void run(){ | ||
79 | try{ | ||
80 | String fileName=null; | ||
81 | String request = inStr.readLine(); | ||
82 | System.out.print(request); | ||
83 | if(request.lastIndexOf("GET")==0) fileName = interpretGET(request); | ||
84 | else throw new Exception("BAU"); | ||
85 | byte[] data = readFile(fileName); | ||
86 | dout.write(data); | ||
87 | dout.flush(); | ||
88 | |||
89 | } | ||
90 | catch(IOException e){outStr.println("<HTML><BODY><P>403 Forbidden<P></BODY></HTML>");} | ||
91 | catch(Exception e2){outStr.println("<HTML><BODY><P>"+e2.getMessage()+"<P></BODY></HTML>");} | ||
92 | finally{ | ||
93 | try{s.close();}catch(Exception e){} | ||
94 | } | ||
95 | } | ||
96 | |||
97 | private String interpretGET(String rqst) throws Exception{ | ||
98 | StringTokenizer strT = new StringTokenizer(rqst); | ||
99 | String tmp=""; | ||
100 | String fileN=iniContext; | ||
101 | tmp=strT.nextToken(); | ||
102 | if(!tmp.equals("GET")) throw new Exception("Comanda GET invalida ."); | ||
103 | |||
104 | tmp=strT.nextToken(); | ||
105 | if((tmp.equals("/")) || (tmp.endsWith("/"))) { | ||
106 | fileN = fileN+tmp+"index.htm"; | ||
107 | System.err.println("CERERE:"+fileN); | ||
108 | return fileN; | ||
109 | } | ||
110 | |||
111 | fileN = fileN+ tmp; | ||
112 | System.err.println("CERERE:"+fileN); | ||
113 | return fileN; | ||
114 | } | ||
115 | |||
116 | private byte[] readFile(String fileN) throws Exception{ | ||
117 | fileN.replace('/','\\'); | ||
118 | File f = new File(fileN); | ||
119 | if(!f.canRead()) throw new Exception("Fisierul "+fileN+" nu poate fi citit"); | ||
120 | FileInputStream fstr = new FileInputStream(f); | ||
121 | byte[] data = new byte[fstr.available()]; | ||
122 | fstr.read(data); | ||
123 | return data; | ||
124 | } | ||
125 | } | ||
126 |