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