1 | /******************************************************************************
|
---|
2 | *
|
---|
3 | * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
|
---|
4 | * www.mindbright.se, [email protected]
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | *****************************************************************************
|
---|
17 | * $Author: mats $
|
---|
18 | * $Date: 2000/01/30 16:56:27 $
|
---|
19 | * $Name: rel1-2-1 $
|
---|
20 | *****************************************************************************/
|
---|
21 | package mindbright.net;
|
---|
22 |
|
---|
23 | import java.io.InputStream;
|
---|
24 | import java.io.OutputStream;
|
---|
25 | import java.io.IOException;
|
---|
26 | import java.util.Hashtable;
|
---|
27 | import java.util.Enumeration;
|
---|
28 |
|
---|
29 | import mindbright.util.Base64;
|
---|
30 |
|
---|
31 | public class HttpHeader {
|
---|
32 | String startLine;
|
---|
33 | Hashtable headerFields;
|
---|
34 | boolean isResponse;
|
---|
35 |
|
---|
36 | private final String processLine(String line, String lastHeaderName) throws IOException {
|
---|
37 | String name, value;
|
---|
38 | char c = line.charAt(0);
|
---|
39 | if(c == ' ' || c == '\t') {
|
---|
40 | name = lastHeaderName;
|
---|
41 | value = getHeaderField(lastHeaderName) + " " + line.trim();
|
---|
42 | } else {
|
---|
43 | int n = line.indexOf(':');
|
---|
44 | if(n == -1)
|
---|
45 | throw new IOException("HttpHeader, corrupt header-field: '" + line + "'");
|
---|
46 | name = line.substring(0, n).toLowerCase();
|
---|
47 | value = line.substring(n + 1).trim();
|
---|
48 | }
|
---|
49 |
|
---|
50 | setHeaderField(name, value);
|
---|
51 | return name;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public HttpHeader(String fullHeader) throws IOException {
|
---|
55 | this();
|
---|
56 | String lastHeaderName = null;
|
---|
57 | boolean readStart = false;
|
---|
58 | int l = 0, r = 0, n = 0;
|
---|
59 | while(true) {
|
---|
60 | r = fullHeader.indexOf("\r\n", l);
|
---|
61 | if(l == r || r == -1)
|
---|
62 | break;
|
---|
63 | String line = fullHeader.substring(l, r);
|
---|
64 | if(readStart) {
|
---|
65 | lastHeaderName = processLine(line, lastHeaderName);
|
---|
66 | } else {
|
---|
67 | startLine = line;
|
---|
68 | readStart = true;
|
---|
69 | }
|
---|
70 | l = r + 2;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public HttpHeader(InputStream input) throws IOException {
|
---|
75 | this();
|
---|
76 | StringBuffer lineBuf = new StringBuffer();
|
---|
77 | boolean readStart = false;
|
---|
78 | String lastHeaderName = null;
|
---|
79 | int c;
|
---|
80 | while(true) {
|
---|
81 | c = input.read();
|
---|
82 | if(c == -1)
|
---|
83 | throw new IOException("HttpHeader, corrupt header, input stream closed");
|
---|
84 | if(c == '\n')
|
---|
85 | continue;
|
---|
86 | if(c != '\r') {
|
---|
87 | lineBuf.append((char)c);
|
---|
88 | } else {
|
---|
89 | if(lineBuf.length() != 0) {
|
---|
90 | String line = new String(lineBuf);
|
---|
91 | if(readStart) {
|
---|
92 | lastHeaderName = processLine(line, lastHeaderName);
|
---|
93 | } else {
|
---|
94 | startLine = line;
|
---|
95 | readStart = true;
|
---|
96 | }
|
---|
97 | lineBuf.setLength(0);
|
---|
98 | } else {
|
---|
99 | break;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | /* Strip the last \n */
|
---|
104 | c = input.read();
|
---|
105 | }
|
---|
106 |
|
---|
107 | public HttpHeader() {
|
---|
108 | headerFields = new Hashtable();
|
---|
109 | }
|
---|
110 |
|
---|
111 | public String getStartLine() {
|
---|
112 | return startLine;
|
---|
113 | }
|
---|
114 |
|
---|
115 | public void setStartLine(String startLine) {
|
---|
116 | this.startLine = startLine;
|
---|
117 | }
|
---|
118 |
|
---|
119 | public Hashtable getHeaderFields() {
|
---|
120 | return headerFields;
|
---|
121 | }
|
---|
122 |
|
---|
123 | public String getHeaderField(String headerName) {
|
---|
124 | return (String)headerFields.get(headerName.toLowerCase());
|
---|
125 | }
|
---|
126 |
|
---|
127 | public void setHeaderField(String headerName, String value) {
|
---|
128 | headerFields.put(headerName.toLowerCase(), value);
|
---|
129 | }
|
---|
130 |
|
---|
131 | public void writeTo(OutputStream output) throws IOException {
|
---|
132 | String fullHeader = toString();
|
---|
133 | output.write(fullHeader.getBytes());
|
---|
134 | output.flush();
|
---|
135 | }
|
---|
136 |
|
---|
137 | public int getStatus() {
|
---|
138 | int pos;
|
---|
139 | int status = -1;
|
---|
140 | if((pos = startLine.indexOf(" ")) > 0) {
|
---|
141 | try {
|
---|
142 | status = new Integer(startLine.substring(pos + 1, pos + 4)).intValue();
|
---|
143 | } catch (NumberFormatException e) {
|
---|
144 | status = -1;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | return status;
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void setBasicProxyAuth(String username, String password) {
|
---|
151 | String authStr = username + ":" + password;
|
---|
152 | byte[] authB64enc = Base64.encode(authStr.getBytes());
|
---|
153 | setHeaderField("Proxy-Authorization", "Basic " +
|
---|
154 | (new String(authB64enc)));
|
---|
155 | }
|
---|
156 |
|
---|
157 | public String getProxyAuthMethod() {
|
---|
158 | String challenge = getHeaderField("Proxy-Authenticate");
|
---|
159 | String method = null;
|
---|
160 | if(challenge != null) {
|
---|
161 | int n = challenge.indexOf(' ');
|
---|
162 | method = challenge.substring(0, n);
|
---|
163 | }
|
---|
164 | return method;
|
---|
165 | }
|
---|
166 |
|
---|
167 | public String getProxyAuthRealm() {
|
---|
168 | String challenge = getHeaderField("Proxy-Authenticate");
|
---|
169 | String realm = null;
|
---|
170 | if(challenge != null) {
|
---|
171 | int l, r = challenge.indexOf('=');
|
---|
172 | while(r >= 0) {
|
---|
173 | l = challenge.lastIndexOf(' ', r);
|
---|
174 | realm = challenge.substring(l + 1, r);
|
---|
175 | if(realm.equalsIgnoreCase("realm")) {
|
---|
176 | l = r + 2;
|
---|
177 | r = challenge.indexOf('"', l);
|
---|
178 | realm = challenge.substring(l, r);
|
---|
179 | break;
|
---|
180 | }
|
---|
181 | r = challenge.indexOf('=', r + 1);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | return realm;
|
---|
185 | }
|
---|
186 |
|
---|
187 | public String toString() {
|
---|
188 | String fullHeader = startLine + "\r\n";
|
---|
189 | Enumeration headerNames = headerFields.keys();
|
---|
190 | while(headerNames.hasMoreElements()) {
|
---|
191 | String fieldName = (String)headerNames.nextElement();
|
---|
192 | fullHeader += fieldName + ": " + headerFields.get(fieldName) + "\r\n";
|
---|
193 | }
|
---|
194 | fullHeader += "\r\n";
|
---|
195 | return fullHeader;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /* !!! REMOVE
|
---|
199 | public static void main(String[] argv) {
|
---|
200 |
|
---|
201 | try {
|
---|
202 | HttpHeader requestHeader = new HttpHeader();
|
---|
203 |
|
---|
204 | requestHeader.setStartLine("CONNECT " + "www.foobar.com" + ":" + 4711 + " HTTP/1.0");
|
---|
205 | requestHeader.setHeaderField("User-Agent", "Proxologist/0.1");
|
---|
206 | requestHeader.setHeaderField("Pragma", "No-Cache");
|
---|
207 | requestHeader.setHeaderField("Proxy-Connection", "Keep-Alive");
|
---|
208 |
|
---|
209 | String authStr = "foobar" + ":" + "zippo";
|
---|
210 | byte[] authB64enc = Base64.encode(authStr.getBytes());
|
---|
211 | requestHeader.setHeaderField("Proxy-Authorization", "Basic " + (new String(authB64enc)));
|
---|
212 |
|
---|
213 | System.out.println("HTTP header:");
|
---|
214 |
|
---|
215 | String req = requestHeader.toString();
|
---|
216 | System.out.print(req);
|
---|
217 |
|
---|
218 | requestHeader = new HttpHeader(req);
|
---|
219 |
|
---|
220 | System.out.println("HTTP startline: " + requestHeader.getStartLine());
|
---|
221 | System.out.println("HTTP proxy-auth: " + requestHeader.getHeaderField("proxy-authorization"));
|
---|
222 | } catch (Exception e) {
|
---|
223 | System.out.println("Error: " + e);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | */
|
---|
227 |
|
---|
228 | }
|
---|