1 public class WebServerSocket {
2 //Filename requested by the client
8 public WebServerSocket(){
9 parsed = new String[4];
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());
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
33 StringBuffer response = new StringBuffer("404: not found: ");//Send 404 error if
35 response.append(filename);
36 String buffer = new String(response);
37 s.write(buffer.getBytes());
41 byte buf[] = new byte[16];
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++) {
51 //String str = new String(tosend);
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);
62 StringBuffer sb=new StringBuffer(prefix);
67 if(prefix.indexOf("\r\n\r\n")>=0) {
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));
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) {
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);
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));
100 int i2 = s.indexOf('_',i1+1);
101 parsed[1] = new String(s.subString(i1+1,i2));
103 int i3 = s.indexOf('_',i2+1);
104 parsed[2] = new String(s.subString(i2+1,i3));
106 String s3 = s.subString(i3+1);
112 if (s.startsWith("buy")==true){
113 // System.printString("DEBUG > BUY\n");
114 int i1 = s.indexOf('_');
115 parsed[0] = s.subString(0,i1);
117 int i2 = s.indexOf('_', i1+1);
118 parsed[1] = s.subString(i1+1,i2);
120 String s2 = s.subString(i2+1);
127 if (s.startsWith("inventory")==true){
128 // System.printString("DEBUG > INVENTORY\n");