adding scripts for automating results
[IRC.git] / Robust / src / Tests / 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("./Tests/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                     System.printString("\n");
79                     return true;
80                 }
81                 return false;
82         }
83         
84         // Parse  for the prefix in the client request
85         // This is helpful to find if the prefix is a special transaction
86         public boolean checktrans(){
87                 if (filename.startsWith("trans") == true) {
88                         return true;
89                 } else {
90                         return false;
91                 }
92         }
93
94         //Parse for the substrings in the filename and use it to obtain the
95         //kind of operation, name of item, quantity of item, price of item
96         //e.g. trans_add_car_2_10000 is the filename
97         //store in the parsed[] string , add,car,2,1000
98         public int parseTransaction(){
99                 int start = filename.indexOf('_');
100                 String s = filename.subString(start+1);
101
102                 if (s.startsWith("add")==true){
103                         System.printString("DEBUG > ADD\n");
104                         int i1 = s.indexOf('_');
105                         parsed[0] = new String(s.subString(0,i1));
106
107                         String s1 = s.subString(i1+1);
108                         int i2 = s1.indexOf('_');
109                         parsed[1] = new String(s1.subString(0,i2));
110                         
111                         String s2 = s1.subString(i2+1);
112                         int i3 = s2.indexOf('_');
113                         parsed[2] = new String(s2.subString(0,i3));
114                         
115                         String s3 = s2.subString(i3+1);
116                         parsed[3] = s3;
117                         
118                         return 0;
119                         
120                 }
121                 if (s.startsWith("buy")==true){
122                         System.printString("DEBUG > BUY\n");
123                         int i1 = s.indexOf('_');
124                         parsed[0] = s.subString(0,i1);
125
126                         String s1 = s.subString(i1+1);
127                         int i2 = s1.indexOf('_');
128                         parsed[1] = s1.subString(0,i2);
129                         
130                         String s2 = s1.subString(i2+1);
131                         parsed[2] = s2;
132                         
133                         parsed[3] = "";
134                 
135                         return 1;
136                 }
137                 if (s.startsWith("inventory")==true){
138                         System.printString("DEBUG > INVENTORY\n");
139                         return 2;
140
141                 }
142                 // Error transaction
143                 return -1;
144         }
145 }