1 public class WebServerSocket extends Socket {
8 //Filename requested by the client
14 public WebServerSocket(){
15 parsed = new String[4];
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());
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
39 StringBuffer response = new StringBuffer("404: not found: ");//Send 404 error if
41 response.append(filename);
42 String buffer = new String(response);
43 write(buffer.getBytes());
47 byte buf[] = new byte[16];
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++) {
57 //String str = new String(tosend);
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);
68 StringBuffer sb=new StringBuffer(prefix);
73 if(prefix.indexOf("\r\n\r\n")>=0) {
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");
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) {
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);
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));
107 int i2 = s.indexOf('_',i1+1);
108 parsed[1] = new String(s.subString(i1+1,i2));
110 int i3 = s.indexOf('_',i2+1);
111 parsed[2] = new String(s.subString(i2+1,i3));
113 String s3 = s.subString(i3+1);
119 if (s.startsWith("buy")==true){
120 // System.printString("DEBUG > BUY\n");
121 int i1 = s.indexOf('_');
122 parsed[0] = s.subString(0,i1);
124 int i2 = s.indexOf('_', i1+1);
125 parsed[1] = s.subString(i1+1,i2);
127 String s2 = s.subString(i2+1);
134 if (s.startsWith("inventory")==true){
135 // System.printString("DEBUG > INVENTORY\n");