001/** 002 * 003 * Copyright the original author or authors 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.jingleold.nat; 018 019import java.io.BufferedReader; 020import java.io.IOException; 021import java.io.InputStream; 022import java.io.InputStreamReader; 023import java.io.OutputStream; 024import java.net.ServerSocket; 025import java.net.Socket; 026import java.util.StringTokenizer; 027import java.util.logging.Level; 028import java.util.logging.Logger; 029 030import org.jivesoftware.smack.util.StringUtils; 031 032/** 033 * A very Simple HTTP Server. 034 */ 035public class HttpServer { 036 037 private static final Logger LOGGER = Logger.getLogger(HttpServer.class.getName()); 038 039 public HttpServer(int port) { 040 ServerSocket server_socket; 041 042 try { 043 044 server_socket = new ServerSocket(port); 045 LOGGER.fine("httpServer running on port " + 046 server_socket.getLocalPort()); 047 048 while (true) { 049 Socket socket = server_socket.accept(); 050 LOGGER.fine("New connection accepted " + 051 socket.getInetAddress() + 052 ":" + socket.getPort()); 053 054 try { 055 HttpRequestHandler request = 056 new HttpRequestHandler(socket); 057 058 Thread thread = new Thread(request); 059 060 thread.start(); 061 } 062 catch (Exception e) { 063 LOGGER.log(Level.FINE, "Exception", e); 064 } 065 } 066 } 067 068 catch (IOException e) { 069 LOGGER.log(Level.FINE, "Exception", e); 070 } 071 072 } 073 074 public static void main(String[] args) { 075 HttpServer httpServer = new HttpServer(Integer.parseInt(args[0])); 076 } 077 078 static class HttpRequestHandler implements Runnable { 079 080 static final String CRLF = "\r\n"; 081 Socket socket; 082 InputStream input; 083 OutputStream output; 084 BufferedReader br; 085 086 HttpRequestHandler(Socket socket) throws Exception { 087 this.socket = socket; 088 this.input = socket.getInputStream(); 089 this.output = socket.getOutputStream(); 090 this.br = new BufferedReader(new InputStreamReader(socket.getInputStream(), StringUtils.UTF8)); 091 } 092 093 @Override 094 public void run() { 095 try { 096 processRequest(); 097 } 098 catch (Exception e) { 099 LOGGER.log(Level.WARNING, "exception", e); 100 } 101 } 102 103 private void processRequest() throws Exception { 104 while (true) { 105 106 String headerLine = br.readLine(); 107 LOGGER.fine(headerLine); 108 if (headerLine.equals(CRLF) || headerLine.equals("")) break; 109 110 StringTokenizer s = new StringTokenizer(headerLine); 111 String temp = s.nextToken(); 112 113 if (temp.equals("GET")) { 114 115 String serverLine = "Server: Simple httpServer"; 116 String contentTypeLine = "text"; 117 String entityBody = ""; 118 String contentLengthLine; 119 String statusLine = "HTTP/1.0 200 OK" + CRLF; 120 contentLengthLine = "Content-Length: " 121 + entityBody.length() + CRLF; 122 contentTypeLine = "text/html"; 123 124 output.write(statusLine.getBytes(StringUtils.UTF8)); 125 126 output.write(serverLine.getBytes(StringUtils.UTF8)); 127 128 output.write(contentTypeLine.getBytes(StringUtils.UTF8)); 129 output.write(contentLengthLine.getBytes(StringUtils.UTF8)); 130 131 output.write(CRLF.getBytes(StringUtils.UTF8)); 132 133 output.write(entityBody.getBytes(StringUtils.UTF8)); 134 135 } 136 } 137 138 try { 139 output.close(); 140 br.close(); 141 socket.close(); 142 } 143 catch (Exception e) { 144 // Do Nothing 145 LOGGER.log(Level.WARNING, "exception", e); 146 } 147 } 148 } 149}