A connection to the SimpleHttpServer.
| Method from org.apache.commons.httpclient.server.SimpleHttpServerConnection Detail: |
public synchronized void close() {
try {
if (socket != null) {
in.close();
out.close();
socket.close();
socket = null;
}
} catch (IOException e) {
}
}
|
public InputStream getInputStream() {
return this.in;
}
|
public OutputStream getOutputStream() {
return this.out;
}
|
public int getSocketTimeout() throws SocketException {
return this.socket.getSoTimeout();
}
|
public ResponseWriter getWriter() throws UnsupportedEncodingException {
return new ResponseWriter(out);
}
Returns the ResponseWriter used to write the output to the socket. |
public boolean isKeepAlive() {
return this.keepAlive;
}
|
public synchronized boolean isOpen() {
return this.socket != null;
}
|
public SimpleRequest readRequest() throws IOException {
try {
String line = null;
do {
line = HttpParser.readLine(in, HTTP_ELEMENT_CHARSET);
} while (line != null && line.length() == 0);
if (line == null) {
setKeepAlive(false);
return null;
}
SimpleRequest request = new SimpleRequest(
RequestLine.parseLine(line),
HttpParser.parseHeaders(this.in, HTTP_ELEMENT_CHARSET),
this.in);
return request;
} catch (IOException e) {
close();
throw e;
}
}
|
public SimpleResponse readResponse() throws IOException {
try {
String line = null;
do {
line = HttpParser.readLine(in, HTTP_ELEMENT_CHARSET);
} while (line != null && line.length() == 0);
if (line == null) {
setKeepAlive(false);
return null;
}
SimpleResponse response = new SimpleResponse(
new StatusLine(line),
HttpParser.parseHeaders(this.in, HTTP_ELEMENT_CHARSET),
this.in);
return response;
} catch (IOException e) {
close();
throw e;
}
}
|
public void setKeepAlive(boolean b) {
this.keepAlive = b;
}
|
public void setSocketTimeout(int timeout) throws SocketException {
this.socket.setSoTimeout(timeout);
}
|
public void writeRequest(SimpleRequest request) throws IOException {
if (request == null) {
return;
}
ResponseWriter writer = new ResponseWriter(this.out, HTTP_ELEMENT_CHARSET);
writer.println(request.getRequestLine().toString());
Iterator item = request.getHeaderIterator();
while (item.hasNext()) {
Header header = (Header) item.next();
writer.print(header.toExternalForm());
}
writer.println();
writer.flush();
OutputStream outsream = this.out;
InputStream content = request.getBody();
if (content != null) {
Header transferenc = request.getFirstHeader("Transfer-Encoding");
if (transferenc != null) {
request.removeHeaders("Content-Length");
if (transferenc.getValue().indexOf("chunked") != -1) {
outsream = new ChunkedOutputStream(outsream);
}
}
byte[] tmp = new byte[4096];
int i = 0;
while ((i = content.read(tmp)) >= 0) {
outsream.write(tmp, 0, i);
}
if (outsream instanceof ChunkedOutputStream) {
((ChunkedOutputStream)outsream).finish();
}
}
outsream.flush();
}
|
public void writeResponse(SimpleResponse response) throws IOException {
if (response == null) {
return;
}
ResponseWriter writer = new ResponseWriter(this.out, HTTP_ELEMENT_CHARSET);
writer.println(response.getStatusLine());
Iterator item = response.getHeaderIterator();
while (item.hasNext()) {
Header header = (Header) item.next();
writer.print(header.toExternalForm());
}
writer.println();
writer.flush();
OutputStream outsream = this.out;
InputStream content = response.getBody();
if (content != null) {
Header transferenc = response.getFirstHeader("Transfer-Encoding");
if (transferenc != null) {
response.removeHeaders("Content-Length");
if (transferenc.getValue().indexOf("chunked") != -1) {
outsream = new ChunkedOutputStream(outsream);
}
}
byte[] tmp = new byte[1024];
int i = 0;
while ((i = content.read(tmp)) >= 0) {
outsream.write(tmp, 0, i);
}
if (outsream instanceof ChunkedOutputStream) {
((ChunkedOutputStream)outsream).finish();
}
}
outsream.flush();
}
|