/*
2 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/examples/org/apache/http/examples/ElementalHttpServer.java $
3 * $Revision: 744516 $
4 * $Date: 2009-02-14 17:38:14 +0100 (Sat, 14 Feb 2009) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements. See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership. The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License. You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied. See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation. For more
27 * information on the Apache Software Foundation, please see
28 *
29 *
30 */
31
32 package org.apache.http.examples;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.io.InterruptedIOException;
37 import java.io.OutputStream;
38 import java.io.OutputStreamWriter;
39 import java.net.ServerSocket;
40 import java.net.Socket;
41 import java.net.URLDecoder;
42 import java.util.Locale;
43
44 import org.apache.http.ConnectionClosedException;
45 import org.apache.http.HttpEntity;
46 import org.apache.http.HttpEntityEnclosingRequest;
47 import org.apache.http.HttpException;
48 import org.apache.http.HttpRequest;
49 import org.apache.http.HttpResponse;
50 import org.apache.http.HttpServerConnection;
51 import org.apache.http.HttpStatus;
52 import org.apache.http.MethodNotSupportedException;
53 import org.apache.http.entity.ContentProducer;
54 import org.apache.http.entity.EntityTemplate;
55 import org.apache.http.entity.FileEntity;
56 import org.apache.http.impl.DefaultConnectionReuseStrategy;
57 import org.apache.http.impl.DefaultHttpResponseFactory;
58 import org.apache.http.impl.DefaultHttpServerConnection;
59 import org.apache.http.params.BasicHttpParams;
60 import org.apache.http.params.CoreConnectionPNames;
61 import org.apache.http.params.HttpParams;
62 import org.apache.http.params.CoreProtocolPNames;
63 import org.apache.http.protocol.BasicHttpProcessor;
64 import org.apache.http.protocol.HttpContext;
65 import org.apache.http.protocol.BasicHttpContext;
66 import org.apache.http.protocol.HttpRequestHandler;
67 import org.apache.http.protocol.HttpRequestHandlerRegistry;
68 import org.apache.http.protocol.HttpService;
69 import org.apache.http.protocol.ResponseConnControl;
70 import org.apache.http.protocol.ResponseContent;
71 import org.apache.http.protocol.ResponseDate;
72 import org.apache.http.protocol.ResponseServer;
73 import org.apache.http.util.EntityUtils;
74
75 /**
76 * Basic, yet fully functional and spec compliant, HTTP/1.1 file server.
77 *
78 * Please note the purpose of this application is demonstrate the usage of HttpCore APIs.
79 * It is NOT intended to demonstrate the most efficient way of building an HTTP file server.
80 *
81 *
82 * @version $Revision: 744516 $
83 */
84 public class ElementalHttpServer {
85
86 public static void main(String[] args) throws Exception {
87 if (args.length < 1) {
88 System.err.println("Please specify document root directory");
89 System.exit(1);
90 }
91 Thread t = new RequestListenerThread(8080, args[0]);
92 t.setDaemon(false);
93 t.start();
94 }
95
96 static class HttpFileHandler implements HttpRequestHandler {
97
98 private final String docRoot;
99
100 public HttpFileHandler(final String docRoot) {
101 super();
102 this.docRoot = docRoot;
103 }
104
105 public void handle(
106 final HttpRequest request,
107 final HttpResponse response,
108 final HttpContext context) throws HttpException, IOException {
109
110 String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
111 if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
112 throw new MethodNotSupportedException(method + " method not supported");
113 }
114 String target = request.getRequestLine().getUri();
115
116 if (request instanceof HttpEntityEnclosingRequest) {
117 HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
118 byte[] entityContent = EntityUtils.toByteArray(entity);
119 System.out.println("Incoming entity content (bytes): " + entityContent.length);
120 }
121
122 final File file = new File(this.docRoot, URLDecoder.decode(target));
123 if (!file.exists()) {
124
125 response.setStatusCode(HttpStatus.SC_NOT_FOUND);
126 EntityTemplate body = new EntityTemplate(new ContentProducer() {
127
128 public void writeTo(final OutputStream outstream) throws IOException {
129 OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
130 writer.write("