KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > client > framework > HTTPParser


1 package com.quikj.client.framework;
2
3 import java.io.*;
4 import java.util.*;
5 import java.net.*;
6
7 public class HTTPParser
8 {
9     public static final int INTERRUPTED = 0;
10     public static final int EOF = 1;
11     public static final int FORMAT_ERROR = 2;
12     public static final int IO_ERROR = 3;
13     
14     public HTTPParser(Socket socket)
15     throws SocketException, IOException
16     {
17         reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
18         socket.setSoTimeout(1000); // check for interruption every 1000 millseconds
19
}
20     
21     public HTTPMessage read()
22     {
23         boolean req = true;
24         HTTPReqMessage req_msg = null;
25         HTTPRspMessage rsp_message = null;
26         HTTPMessage message = null;
27         
28         String JavaDoc line = null;
29         try
30         {
31             // ignore any blank lines in the beginning
32
while (true)
33             {
34                 try
35                 {
36                     line = reader.readLine();
37                 }
38                 catch (InterruptedIOException ex)
39                 {
40                     // because of a timeout
41
if ((Thread.currentThread().isInterrupted() == true) ||
42                         (isInterrupted() == true))
43                     {
44                         errorNumber = INTERRUPTED;
45                         return null;
46                     }
47                     else
48                     {
49                         continue;
50                     }
51                 }
52                 
53                 
54                 if (line == null) // connection closed
55
{
56                     errorNumber = EOF;
57                     return null;
58                 }
59                 
60                 if (line.length() > 0) // not a blank line
61
{
62                     // System.out.println ("--" + line);
63
break;
64                 }
65             }
66             
67             // we have got the request/response line
68
StringTokenizer tok = new StringTokenizer(line, " ");
69             int num_tokens = tok.countTokens();
70             
71             // there should be at-least two tokens
72
if (num_tokens < 2)
73             {
74                 errorNumber = FORMAT_ERROR;
75                 return null;
76             }
77             
78             String JavaDoc tok1 = tok.nextToken();
79             String JavaDoc tok2 = tok.nextToken();
80             
81             if (tok1.toUpperCase().startsWith("HTTP/") == true)
82             {
83                 // must be a response
84
try
85                 {
86                     int status = Integer.parseInt(tok2);
87                 }
88                 catch (NumberFormatException JavaDoc ex)
89                 {
90                     errorNumber = FORMAT_ERROR;
91                     return null;
92                 }
93                 
94                 req = false;
95                 rsp_message = new HTTPRspMessage();
96                 
97                 rsp_message.setVersion(tok1.substring(5));
98                 rsp_message.setStatus(tok2);
99                 
100                 if (num_tokens > 2) // there is a reason phrase
101
{
102                     StringBuffer JavaDoc reason = new StringBuffer JavaDoc();
103                     boolean first_time = true;
104                     
105                     for (int i = 0; i < num_tokens - 2; i++)
106                     {
107                         if (first_time == false)
108                         {
109                             reason.append(' ');
110                         }
111                         reason.append(tok.nextToken());
112                         
113                         first_time = false;
114                     }
115                     
116                     rsp_message.setReason(reason.toString());
117                     
118                     message = rsp_message;
119                 }
120             }
121             else
122             {
123                 if (num_tokens != 3)
124                 {
125                     errorNumber = FORMAT_ERROR;
126                     return null;
127                 }
128                 
129                 // must be a request
130
req = true;
131                 HTTPReqMessage req_message = new HTTPReqMessage();
132                 
133                 req_message.setMethod(tok1);
134                 req_message.setURL(tok2);
135                 
136                 String JavaDoc version_s = tok.nextToken();
137                 if (version_s.toUpperCase().startsWith("HTTP/") == true)
138                 {
139                     req_message.setVersion(version_s.substring(5));
140                 }
141                 else
142                 {
143                     errorNumber = FORMAT_ERROR;
144                     return null;
145                 }
146                 
147                 message = req_message;
148             }
149             
150             // next collect the headers
151
int content_length = 0;
152             while (true)
153             {
154                 try
155                 {
156                     line = reader.readLine();
157                 }
158                 catch (InterruptedIOException ex)
159                 {
160                     // because of a timeout
161
if (Thread.currentThread().isInterrupted() == true)
162                     {
163                         errorNumber = INTERRUPTED;
164                         return null;
165                     }
166                     else
167                     {
168                         continue;
169                     }
170                 }
171                 
172                 if (line == null)
173                 {
174                     errorNumber = EOF;
175                     return null;
176                 }
177                 
178                 // System.out.println ("--" + line);
179
if (line.length() > 0)
180                 {
181                     tok = new StringTokenizer(line, ":");
182                     
183                     num_tokens = tok.countTokens();
184                     
185                     if (num_tokens < 2)
186                     {
187                         errorNumber = FORMAT_ERROR;
188                         return null;
189                     }
190                     
191                     String JavaDoc key = tok.nextToken().trim();
192                     StringBuffer JavaDoc value = new StringBuffer JavaDoc();
193                     boolean first_time = true;
194                     for (int i = 0; i < num_tokens - 1; i++)
195                     {
196                         if (first_time == false)
197                         {
198                             value.append(":");
199                         }
200                         
201                         value.append(tok.nextToken().trim());
202                         first_time = false;
203                     }
204                     
205                     String JavaDoc value_s = value.toString();
206                     if (key.equalsIgnoreCase("CONTENT-LENGTH") == true)
207                     {
208                         try
209                         {
210                             content_length = Integer.parseInt(value_s);
211                         }
212                         catch (NumberFormatException JavaDoc ex)
213                         {
214                             errorNumber = FORMAT_ERROR;
215                             return null;
216                         }
217                     }
218                     else
219                     {
220                         message.addHeader(key, value_s);
221                     }
222                     
223                 }
224                 else // blank line, end of the header
225
{
226                     break;
227                 }
228             }
229             
230             // finally, get the body
231
if (content_length > 0) // if there is a body
232
{
233                 char body[] = new char[content_length];
234                 
235                 int length_read = 0;
236                 
237                 while (length_read < content_length)
238                 {
239                     int len = 0;
240                     try
241                     {
242                         len = reader.read(body, length_read, content_length - length_read);
243                     }
244                     catch (InterruptedIOException ex)
245                     {
246                         // because of a timeout
247
if (Thread.currentThread().isInterrupted() == true)
248                         {
249                             errorNumber = INTERRUPTED;
250                             return null;
251                         }
252                         else
253                         {
254                             continue;
255                         }
256                     }
257                     
258                     if (len == -1)
259                     {
260                         errorNumber = EOF;
261                         return null;
262                     }
263                     
264                     length_read += len;
265                 }
266                 message.setBody(body);
267             }
268         }
269         catch (IOException ex)
270         {
271             errorNumber = IO_ERROR;
272             return null;
273         }
274         return message;
275         
276     }
277     
278     public int getErrorNumber()
279     {
280         return errorNumber;
281     }
282     
283     /** Getter for property interrupted.
284      * @return Value of property interrupted.
285      *
286      */

287     public boolean isInterrupted()
288     {
289         return interrupted;
290     }
291     
292     /** Setter for property interrupted.
293      * @param interrupted New value of property interrupted.
294      *
295      */

296     public void setInterrupted(boolean interrupted)
297     {
298         this.interrupted = interrupted;
299     }
300     
301     private int errorNumber;
302     private BufferedReader reader;
303     private boolean interrupted = false;
304 }
305
306
307
308
309
310
Popular Tags