start of new file
[IRC.git] / Robust / src / Benchmarks / Jhttpp2 / BR / Jhttpp2ClientInputStream.java
1 /* Written and copyright 2001-2003 Benjamin Kohl.\r
2  * Distributed under the GNU General Public License; see the README file.\r
3  * This code comes with NO WARRANTY.\r
4  */\r
5 \r
6 import java.io.IOException;\r
7 import java.io.InputStream;\r
8 import java.io.BufferedInputStream;\r
9 import java.net.InetAddress;\r
10 import java.net.UnknownHostException;\r
11 \r
12 \r
13 /**\r
14         File: Jhttpp2BufferedFilterStream.java\r
15         @author Benjamin Kohl\r
16 */\r
17 public class Jhttpp2ClientInputStream {\r
18     private boolean filter;\r
19     /**\r
20      * This is set to true with requests with bodies, like "POST"\r
21      */\r
22     \r
23     public boolean ssl;\r
24     flag foo;\r
25     \r
26     private void init() {\r
27         ssl = false;\r
28     }\r
29 \r
30     public Jhttpp2ClientInputStream() {\r
31         init();\r
32     }\r
33 \r
34 \r
35     public Request read(String str, Jhttpp2HTTPSession connection, tag ct, tag tc) {\r
36         //can check for ssl first\r
37         String s=readstr(str, connection);\r
38         if (s!=null) {\r
39             return new Request(s){}{ct, tc};\r
40         } else\r
41             return null;\r
42     }\r
43 \r
44     public Request readfirst(String str, Jhttpp2HTTPSession connection, tag ct, tag tc) {\r
45         String s=readstr(str, connection);\r
46         if (s!=null) {\r
47             return new Request(s){first}{ct, tc};\r
48         } else\r
49             return null;\r
50     }\r
51     \r
52     public String readstr(String str, Jhttpp2HTTPSession connection) {\r
53         String rq="";\r
54         int content_len=0;\r
55         boolean tempssl=false;\r
56 \r
57         if (ssl) \r
58             return str;\r
59 \r
60         String buf = getLine(str); // reads the first line\r
61         str = updateBuffer(str, buf);\r
62         if (buf==null)\r
63             return null;\r
64         rq += buf;\r
65         if (buf.startsWith("CONNECT"))\r
66             tempssl=true;\r
67         boolean cnt=true;\r
68         while(cnt) {\r
69             buf = getLine(str); // reads the first line\r
70             str = updateBuffer(str, buf);\r
71             if (buf==null)\r
72                 return null;\r
73             rq += buf;\r
74             \r
75             if (buf.length()<=2) {\r
76                 cnt=false;\r
77             } else {\r
78                 if (buf.toUpperCase().startsWith("CONTENT-LENGTH")) {\r
79                     String clen=buf.substring(16);\r
80                     if (clen.indexOf("\r")!=-1) \r
81                         clen=clen.substring(0,clen.indexOf("\r"));\r
82                     else \r
83                         if(clen.indexOf("\n")!=-1) clen=clen.substring(0,clen.indexOf("\n"));\r
84                     content_len=Integer.parseInt(clen);\r
85                 }\r
86             }\r
87         }\r
88         if (!tempssl) {\r
89             buf=getAdditional(str, content_len);\r
90             str = updateBuffer(str, buf);\r
91             if (buf==null)\r
92                 return null;\r
93             rq+=buf;\r
94         }\r
95         ssl=tempssl;\r
96         return rq;\r
97     }\r
98 \r
99 \r
100     /**\r
101      * reads a line\r
102      * @exception IOException\r
103      */\r
104     public static String getLine(String str) {\r
105         int l=str.indexOf('\n');\r
106         if (l!=-1)\r
107             return str.substring(0, l+1);\r
108         else\r
109             return null;\r
110     }\r
111     \r
112     public static String getAdditional(String str, int content_len) {\r
113         if (content_len>str.length())\r
114             return null;\r
115         else\r
116             return str.substring(0, content_len);\r
117 \r
118     }\r
119 \r
120     public static String updateBuffer(String buf, String str) {\r
121         if (str!=null) {\r
122             return buf.substring(str.length(), buf.length());\r
123         } else\r
124             return buf;\r
125     }\r
126 \r
127         /**\r
128         * @return boolean whether the actual connection was established with the CONNECT method.\r
129         * @since 0.2.21\r
130         */\r
131         public boolean isTunnel() {\r
132           return ssl;\r
133         }\r
134 }\r