1 public class WebServerSocket {
6 //Filename requested by the client
12 public WebServerSocket(){
13 parsed = new String[4];
16 //Send the http header for web browser display
17 public void httpresponse(Socket s){
18 StringBuffer header = new StringBuffer("HTTP/1.0 200 OK\n");
19 header.append("Content-type: text/html\n");
20 header.append("\n\n");
21 String temp_str = new String(header);
22 s.write(temp_str.getBytes());
27 // Send the html file , read from file one byte at a time
28 public void sendfile(Socket s) {
29 StringBuffer req_file = new StringBuffer("./htmlfiles/");
30 req_file.append(filename);
31 String filepath = new String(req_file);
32 FileInputStream def_file = new FileInputStream(filepath);
33 int status = def_file.getfd();//Checks if the file is present in
37 StringBuffer response = new StringBuffer("404: not found: ");//Send 404 error if
39 response.append(filename);
40 String buffer = new String(response);
41 s.write(buffer.getBytes());
45 byte buf[] = new byte[16];
48 while ((ret = def_file.read(buf)) > 0) {// Read from file and write
49 // one byte at a time into the socket
50 byte tosend[] = new byte[ret];
51 for (int i = 0; i < ret; i++) {
55 //String str = new String(tosend);
60 //Read the client request and extract the filename from it
61 public boolean clientrequest(Socket s){
62 byte b1[] = new byte[1024];
63 int numbytes=s.read(b1);//Read client request from web server socket
64 String curr=(new String(b1)).subString(0, numbytes);
66 StringBuffer sb=new StringBuffer(prefix);
71 if(prefix.indexOf("\r\n\r\n")>=0) {
73 int index = prefix.indexOf('/');//Parse the GET client request to find filename
74 int end = prefix.indexOf('H');
75 filename = prefix.subString((index+1), (end-1));
81 // Parse for the prefix in the client request
82 // This is helpful to find if the prefix is a special transaction
83 public boolean checktrans(){
84 if (filename.startsWith("portal") == true) {
91 //Parse for the substrings in the filename and use it to obtain the
92 //kind of operation, name of item, quantity of item, price of item
93 //e.g. trans_add_car_2_10000 is the filename
94 //store in the parsed[] string , add,car,2,1000
95 public int parseTransaction(){
96 int start = filename.indexOf('_');
97 String s = filename.subString(start+1);
99 if (s.startsWith("add")==true){
100 // System.printString("DEBUG > ADD\n");
101 int i1 = s.indexOf('_');
102 parsed[0] = new String(s.subString(0,i1));
104 int i2 = s.indexOf('_',i1+1);
105 parsed[1] = new String(s.subString(i1+1,i2));
107 int i3 = s.indexOf('_',i2+1);
108 parsed[2] = new String(s.subString(i2+1,i3));
110 String s3 = s.subString(i3+1);
116 if (s.startsWith("buy")==true){
117 // System.printString("DEBUG > BUY\n");
118 int i1 = s.indexOf('_');
119 parsed[0] = s.subString(0,i1);
121 int i2 = s.indexOf('_', i1+1);
122 parsed[1] = s.subString(i1+1,i2);
124 String s2 = s.subString(i2+1);
131 if (s.startsWith("inventory")==true){
132 // System.printString("DEBUG > INVENTORY\n");