This commit was manufactured by cvs2svn to create tag 'buildscript'.
[IRC.git] /
1 public class QueryThread extends Thread {
2     QueryQueue toprocess;
3     QueryList ql;
4     public QueryThread(QueryQueue qq, QueryList ql) {
5         toprocess=qq;
6         this.ql=ql;
7     }
8
9     public void run() {
10         while(true) {
11             Query q=null;
12             while(q==null) {
13                 q=toprocess.getQuery();
14                 if (q==null)
15                     Thread.sleep(2);
16             }
17             String hostname=q.getHostName();
18             System.printString("Processing ");
19             System.printString(hostname);
20             System.printString(" ");
21             System.printString(q.getPath());
22             System.printString("\n");
23             Socket s=new Socket(hostname, 80);
24             requestQuery(q, s);
25             readResponse(q, s);
26             q.outputFile();
27             processPage(q, ql);
28             s.close();
29         }
30     }
31
32     void requestQuery(Query q, Socket sock) {
33         StringBuffer req=new StringBuffer("GET "); 
34         req.append("/");
35         req.append(q.getPath());
36         req.append(" HTTP/1.1\r\nHost:");
37         req.append(q.getHostName());
38         req.append("\r\n\r\n");
39         sock.write(req.toString().getBytes());
40     }
41
42     void readResponse(Query q, Socket sock) {
43         //    state 0 - nothing
44         //    state 1 - \r
45         //    state 2 - \r\n
46         //    state 3 - \r\n\r
47         //    state 4 - \r\n\r\n
48         int state=0;
49         while(true) {
50             if (state<4) {
51                 if (state==0) {
52                     byte[] b=new byte[1];
53                     int numchars=sock.read(b);
54                     if ((numchars==1)) {
55                         if (b[0]=='\r') {
56                             state++;
57                         }
58                     } else
59                         return;
60                 } else if (state==1) {
61                     byte[] b=new byte[1];
62                     int numchars=sock.read(b);
63                     if (numchars==1) {
64                         if (b[0]=='\n')
65                             state++;
66                         else
67                             state=0;
68                     } else return;
69                 } else if (state==2) {
70                     byte[] b=new byte[1];
71                     int numchars=sock.read(b);
72                     if (numchars==1) {
73                         if (b[0]=='\r')
74                             state++;
75                         else
76                             state=0;
77                     } else return;
78                 } else if (state==3) {
79                     byte[] b=new byte[1];
80                     int numchars=sock.read(b);
81                     if (numchars==1) {
82                         if (b[0]=='\n')
83                             state++;
84                         else
85                             state=0;
86                     } else return;
87                 }
88             } else {
89                 byte[] buffer=new byte[1024];
90                 int numchars=sock.read(buffer);
91                 if (numchars==0)
92                     return;
93                 else {
94                     String curr=(new String(buffer)).subString(0,numchars);
95                     q.response.append(curr);
96                 }
97             }
98         }
99     }
100
101     void processPage(Query q, QueryList ql) {
102         int index=0;
103         String href=new String("href=\"");
104         String searchstr=q.response.toString();
105         boolean cont=true;
106         while(cont) {
107             int mindex=searchstr.indexOf(href,index);
108             if (mindex!=-1) {
109                 
110                 int endquote=searchstr.indexOf('"', mindex+href.length());
111                 if (endquote!=-1) {
112                     String match=searchstr.subString(mindex+href.length(), endquote);
113                     String match2=q.makewebcanonical(match);
114                     if (match2!=null&&!ql.checkQuery(match2)) {
115                         ql.addQuery(match2);
116                         System.printString(q.getHostName(match));
117                         System.printString("        ");
118                         System.printString(q.getPathName(match));
119                         System.printString("\n");
120                         Query newq=new Query(q.getHostName(match), q.getPathName(match));
121                         toprocess.addQuery(newq);
122                     }
123                     index=endquote;
124                 } else cont=false;
125             } else cont=false;
126         }
127     }
128
129 }