2013-02-07 15:19:47 +01:00
|
|
|
/**
|
2014-02-19 10:38:30 +01:00
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
2007-03-24 18:54:23 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-02-07 15:19:47 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2007-03-24 18:54:23 +01:00
|
|
|
*
|
2013-02-07 15:19:47 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-03-24 18:54:23 +01:00
|
|
|
*
|
2013-02-07 15:19:47 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2007-03-24 18:54:23 +01:00
|
|
|
*/
|
2014-08-20 00:25:57 +02:00
|
|
|
package org.jivesoftware.smackx.jingleold.nat;
|
2007-03-24 18:54:23 +01:00
|
|
|
|
2008-10-30 22:20:29 +01:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
2008-05-20 00:54:19 +02:00
|
|
|
import java.net.ServerSocket;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.util.StringTokenizer;
|
2014-02-19 10:38:30 +01:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2008-10-30 22:20:29 +01:00
|
|
|
|
2007-03-24 18:54:23 +01:00
|
|
|
/**
|
|
|
|
* A very Simple HTTP Server
|
|
|
|
*/
|
|
|
|
public class HttpServer {
|
|
|
|
|
2014-02-19 10:38:30 +01:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(HttpServer.class.getName());
|
2008-10-30 22:20:29 +01:00
|
|
|
|
|
|
|
public HttpServer(int port) {
|
2007-03-24 18:54:23 +01:00
|
|
|
ServerSocket server_socket;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
server_socket = new ServerSocket(port);
|
2014-02-19 10:38:30 +01:00
|
|
|
LOGGER.fine("httpServer running on port " +
|
2007-03-24 18:54:23 +01:00
|
|
|
server_socket.getLocalPort());
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
Socket socket = server_socket.accept();
|
2014-02-19 10:38:30 +01:00
|
|
|
LOGGER.fine("New connection accepted " +
|
2007-03-24 18:54:23 +01:00
|
|
|
socket.getInetAddress() +
|
|
|
|
":" + socket.getPort());
|
|
|
|
|
|
|
|
try {
|
|
|
|
HttpRequestHandler request =
|
|
|
|
new HttpRequestHandler(socket);
|
|
|
|
|
|
|
|
Thread thread = new Thread(request);
|
|
|
|
|
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2014-02-19 10:38:30 +01:00
|
|
|
LOGGER.log(Level.FINE, "Exception", e);
|
2007-03-24 18:54:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (IOException e) {
|
2014-02-19 10:38:30 +01:00
|
|
|
LOGGER.log(Level.FINE, "Exception", e);
|
2007-03-24 18:54:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-15 23:16:18 +02:00
|
|
|
public static void main(String[] args) {
|
2007-03-24 18:54:23 +01:00
|
|
|
HttpServer httpServer = new HttpServer(Integer.parseInt(args[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
class HttpRequestHandler implements Runnable {
|
|
|
|
|
|
|
|
final static String CRLF = "\r\n";
|
|
|
|
Socket socket;
|
|
|
|
InputStream input;
|
|
|
|
OutputStream output;
|
|
|
|
BufferedReader br;
|
|
|
|
|
|
|
|
public HttpRequestHandler(Socket socket) throws Exception {
|
|
|
|
this.socket = socket;
|
|
|
|
this.input = socket.getInputStream();
|
|
|
|
this.output = socket.getOutputStream();
|
|
|
|
this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
processRequest();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void processRequest() throws Exception {
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
String headerLine = br.readLine();
|
2014-02-19 10:38:30 +01:00
|
|
|
LOGGER.fine(headerLine);
|
2007-03-24 18:54:23 +01:00
|
|
|
if (headerLine.equals(CRLF) || headerLine.equals("")) break;
|
|
|
|
|
|
|
|
StringTokenizer s = new StringTokenizer(headerLine);
|
|
|
|
String temp = s.nextToken();
|
|
|
|
|
|
|
|
if (temp.equals("GET")) {
|
|
|
|
|
|
|
|
String serverLine = "Server: Simple httpServer";
|
|
|
|
String contentTypeLine = "text";
|
|
|
|
String entityBody = "";
|
|
|
|
String contentLengthLine;
|
|
|
|
String statusLine = "HTTP/1.0 200 OK" + CRLF;
|
|
|
|
contentLengthLine = "Content-Length: "
|
|
|
|
+ (new Integer(entityBody.length())).toString() + CRLF;
|
|
|
|
contentTypeLine = "text/html";
|
|
|
|
|
|
|
|
output.write(statusLine.getBytes());
|
|
|
|
|
|
|
|
output.write(serverLine.getBytes());
|
|
|
|
|
|
|
|
output.write(contentTypeLine.getBytes());
|
|
|
|
output.write(contentLengthLine.getBytes());
|
|
|
|
|
|
|
|
output.write(CRLF.getBytes());
|
|
|
|
|
|
|
|
output.write(entityBody.getBytes());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
output.close();
|
|
|
|
br.close();
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// Do Nothing
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|