start of new file
[IRC.git] / Robust / src / Benchmarks / Spider / BR / Query.java
1 public class Query extends Socket {
2     flag requested;
3     flag processed;
4     flag received;
5     public int state;
6
7     private String hostname;
8     private String path;
9
10     private StringBuffer response;
11
12     public Query(String hostname, String path) {
13         this.hostname=hostname;
14         this.path=path;
15         response=new StringBuffer();
16         state=0;
17     }
18
19     public void makeConnection() {
20         InetAddress address=InetAddress.getByName(hostname);
21         int port=80;
22         fd=nativeBind(address.getAddress(), port);
23         nativeConnect(fd, address.getAddress(), port);
24     }
25
26     public String getHostName() {
27         return hostname;
28     }
29
30     public String getPath() {
31         return path;
32     }
33
34     public void outputFile() {
35         StringBuffer sb=new StringBuffer(hostname);
36         sb.append(path);
37         FileOutputStream fos=new FileOutputStream(sb.toString().replace('/','#'));
38         fos.write(response.toString().getBytes());
39         fos.close();
40     }
41     
42     public String makewebcanonical(String page) {
43         StringBuffer b=new StringBuffer(getHostName(page));
44         b.append("/");
45         b.append(getPathName(page));
46         return b.toString();
47     }
48
49     public String getHostName(String page) {
50         String http=new String("http://");
51         if (page.indexOf(http)==-1) {
52             return getHostName();
53         } else {
54             int beginindex=page.indexOf(http)+http.length();
55             int endindex=page.indexOf('/',beginindex+1);
56             if ((beginindex==-1)) {
57                 System.printString("ERROR");
58             }
59             if (endindex==-1)
60                 endindex=page.length();
61             return page.subString(beginindex, endindex);
62         }
63     }
64
65     public String getPathName(String page) {
66         String http=new String("http://");
67         if (page.indexOf(http)==-1) {
68             String path=getPath();
69             int lastindex=path.lastindexOf('/');
70             if (lastindex==-1)
71                 return page;
72             
73             StringBuffer sb=new StringBuffer(path.subString(0,lastindex+1));
74             sb.append(page);
75             return sb.toString();
76         } else {
77             int beginindex=page.indexOf(http)+http.length();
78             int nextindex=page.indexOf('/',beginindex+1);
79             if ((beginindex==-1)||(nextindex==-1))
80                 return new String("index.html");
81             return page.subString(nextindex+1, page.length()-1);
82         }
83     }
84 }