start of new file
[IRC.git] / Robust / src / Benchmarks / Conglomerator / Java / WebServerSocket.java
1 public class WebServerSocket {
2         //Filename requested by the client 
3         String filename;
4         String[] parsed; 
5         String prefix;
6                 
7         //Constructor
8         public WebServerSocket(){
9                 parsed = new String[4];
10         }
11         
12         //Send the http header for web browser display  
13         public void httpresponse(Socket s){
14                 StringBuffer header = new StringBuffer("HTTP/1.0 200 OK\n");
15                 header.append("Content-type: text/html\n");
16                 header.append("\n\n");
17                 String temp_str = new String(header);
18                 s.write(temp_str.getBytes());
19                 return;
20
21         }
22         
23         // Send the html file , read from file one byte at a time       
24         public void sendfile(Socket s) {
25                 StringBuffer req_file = new StringBuffer("./htmlfiles/");
26                 req_file.append(filename);
27                 String filepath = new String(req_file);
28                 FileInputStream def_file = new FileInputStream(filepath);
29                 int status = def_file.getfd();//Checks if the file is present in 
30                                               //current directory       
31                 httpresponse(s);
32                 if (status == -1){
33                         StringBuffer response = new StringBuffer("404: not found: ");//Send 404 error if
34                                                                                      // file not found
35                         response.append(filename);
36                         String buffer = new String(response);
37                         s.write(buffer.getBytes());
38                         def_file.close();
39                         return;
40                 }
41                 byte buf[] = new byte[16];
42                 int ret;
43                 
44                 while ((ret = def_file.read(buf)) > 0) {// Read from file and write 
45                                                         // one byte at a time into the socket 
46                         byte tosend[] = new byte[ret];
47                         for (int i = 0; i < ret; i++) {
48                                 tosend[i] = buf[i];
49                         }
50                         s.write(tosend);
51                         //String str = new String(tosend);
52                 }
53                 def_file.close();
54         }
55
56         //Read the client request and extract the filename from it      
57         public boolean clientrequest(Socket s){
58                 byte b1[] = new byte[1024];
59                 int numbytes=s.read(b1);//Read client request from web server socket
60                 String curr=(new String(b1)).subString(0, numbytes);
61                 if (prefix!=null) {
62                     StringBuffer sb=new StringBuffer(prefix);
63                     sb.append(curr);
64                     curr=sb.toString();
65                 }
66                 prefix=curr;
67                 if(prefix.indexOf("\r\n\r\n")>=0) {
68
69                     int index = prefix.indexOf('/');//Parse the GET client request to find filename
70                     int end = prefix.indexOf('H');
71                     filename = prefix.subString((index+1), (end-1));
72                     return true;
73                 }
74                 return false;
75         }
76         
77         // Parse  for the prefix in the client request
78         // This is helpful to find if the prefix is a special transaction
79         public boolean checktrans(){
80                 if (filename.startsWith("portal") == true) {
81                         return true;
82                 } else {
83                         return false;
84                 }
85         }
86
87         //Parse for the substrings in the filename and use it to obtain the
88         //kind of operation, name of item, quantity of item, price of item
89         //e.g. trans_add_car_2_10000 is the filename
90         //store in the parsed[] string , add,car,2,1000
91     public int parseTransaction(){
92         int start = filename.indexOf('_');
93         String s = filename.subString(start+1);
94         
95         if (s.startsWith("add")==true){
96             //          System.printString("DEBUG > ADD\n");
97             int i1 = s.indexOf('_');
98             parsed[0] = new String(s.subString(0,i1));
99             
100             int i2 = s.indexOf('_',i1+1);
101             parsed[1] = new String(s.subString(i1+1,i2));
102             
103             int i3 = s.indexOf('_',i2+1);
104             parsed[2] = new String(s.subString(i2+1,i3));
105             
106             String s3 = s.subString(i3+1);
107             parsed[3] = s3;
108             
109             return 0;
110             
111         }
112         if (s.startsWith("buy")==true){
113             //          System.printString("DEBUG > BUY\n");
114             int i1 = s.indexOf('_');
115             parsed[0] = s.subString(0,i1);
116             
117             int i2 = s.indexOf('_', i1+1);
118             parsed[1] = s.subString(i1+1,i2);
119             
120             String s2 = s.subString(i2+1);
121             parsed[2] = s2;
122             
123             parsed[3] = "";
124             
125             return 1;
126         }
127         if (s.startsWith("inventory")==true){
128             //          System.printString("DEBUG > INVENTORY\n");
129             return 2;
130             
131         }
132         // Error transaction
133         return -1;
134     }
135 }