2
* $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java,v 1.1.2.5 2004/02/22 18:21:18 olegk Exp $
4
* $Date: 2004/02/22 18:21:18 $
6
* ====================================================================
8
* Copyright 1999-2004 The Apache Software Foundation
10
* Licensed under the Apache License, Version 2.0 (the "License");
11
* you may not use this file except in compliance with the License.
12
* You may obtain a copy of the License at
14
* http://www.apache.org/licenses/LICENSE-2.0
16
* Unless required by applicable law or agreed to in writing, software
17
* distributed under the License is distributed on an "AS IS" BASIS,
18
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
* See the License for the specific language governing permissions and
20
* limitations under the License.
21
* ====================================================================
23
* This software consists of voluntary contributions made by many
24
* individuals on behalf of the Apache Software Foundation. For more
25
* information on the Apache Software Foundation, please see
26
* <http://www.apache.org/>.
28
* [Additional notices, if required by prior licensing conditions]
32
package org.apache.commons.httpclient.server;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.io.OutputStream;
37
import java.io.UnsupportedEncodingException;
38
import java.net.Socket;
39
import java.net.SocketException;
41
import org.apache.commons.httpclient.Header;
42
import org.apache.commons.httpclient.HttpParser;
43
import org.apache.commons.httpclient.HttpStatus;
44
import org.apache.commons.logging.Log;
45
import org.apache.commons.logging.LogFactory;
48
* A connection to the SimpleHttpServer.
50
* @author Christian Kohlschuetter
52
public class SimpleHttpServerConnection implements Runnable {
54
private static final Log LOG = LogFactory.getLog(SimpleHttpServerConnection.class);
56
private SimpleHttpServer server;
57
private Socket socket;
58
private InputStream in;
59
private OutputStream out;
61
private int requestNo = 0;
63
private boolean keepAlive = false;
65
private RequestLine requestLine;
67
private Header[] headers;
69
public SimpleHttpServerConnection(SimpleHttpServer server, Socket socket) throws IOException {
72
this.in = socket.getInputStream();
73
this.out = socket.getOutputStream();
76
public void destroy() {
84
} catch(IOException e) {
85
// fail("Unexpected exception: " + e);
87
server.removeConnection(this);
99
} catch (SocketException ignore) {
100
} catch (IOException e) {
101
LOG.error("ServerConnection read error", e);
102
throw new RuntimeException(e.getMessage());
109
* Requests to close connection after processing this request.
111
public void connectionClose() {
116
* Requests to keep the connection alive after processing this request
117
* (must be re-issued for every request if permanent keep-alive is desired).
120
public void connectionKeepAlive() {
125
* Returns the ResponseWriter used to write the output to the socket.
127
* @return This connection's ResponseWriter
129
public ResponseWriter getWriter() {
131
return new ResponseWriter(out);
132
} catch (UnsupportedEncodingException e) {
133
throw new RuntimeException(e.toString());
138
* Returns the number of requests processed (including the current one)
139
* for this connection.
143
public int getRequestNumber() {
147
private void readRequest() throws IOException {
150
line = HttpParser.readLine(in);
151
} while(line != null && line.length() == 0);
159
requestLine = RequestLine.parseLine(line);
160
headers = HttpParser.parseHeaders(in);
161
} catch(IOException e) {
163
ErrorResponse.getInstance().getResponse(HttpStatus.SC_BAD_REQUEST).processRequest(this);
166
server.processRequest(this);
170
public Header[] getHeaders() {
171
Header[] copy = new Header[headers.length];
172
System.arraycopy(headers, 0, copy, 0, headers.length);
176
public RequestLine getRequestLine() {
180
public InputStream getInputStream() {
184
public OutputStream getOutputStream() {