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.jingle.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 030/** 031 * A very Simple HTTP Server 032 */ 033public class HttpServer { 034 035 private static final Logger LOGGER = Logger.getLogger(HttpServer.class.getName()); 036 037 public HttpServer(int port) { 038 ServerSocket server_socket; 039 040 try { 041 042 server_socket = new ServerSocket(port); 043 LOGGER.fine("httpServer running on port " + 044 server_socket.getLocalPort()); 045 046 while (true) { 047 Socket socket = server_socket.accept(); 048 LOGGER.fine("New connection accepted " + 049 socket.getInetAddress() + 050 ":" + socket.getPort()); 051 052 try { 053 HttpRequestHandler request = 054 new HttpRequestHandler(socket); 055 056 Thread thread = new Thread(request); 057 058 thread.start(); 059 } 060 catch (Exception e) { 061 LOGGER.log(Level.FINE, "Exception", e); 062 } 063 } 064 } 065 066 catch (IOException e) { 067 LOGGER.log(Level.FINE, "Exception", e); 068 } 069 070 } 071 072 public static void main(String args[]) { 073 HttpServer httpServer = new HttpServer(Integer.parseInt(args[0])); 074 } 075 076 class HttpRequestHandler implements Runnable { 077 078 final static String CRLF = "\r\n"; 079 Socket socket; 080 InputStream input; 081 OutputStream output; 082 BufferedReader br; 083 084 public HttpRequestHandler(Socket socket) throws Exception { 085 this.socket = socket; 086 this.input = socket.getInputStream(); 087 this.output = socket.getOutputStream(); 088 this.br = new BufferedReader(new InputStreamReader(socket.getInputStream())); 089 } 090 091 public void run() { 092 try { 093 processRequest(); 094 } 095 catch (Exception e) { 096 e.printStackTrace(); 097 } 098 } 099 100 private void processRequest() throws Exception { 101 while (true) { 102 103 String headerLine = br.readLine(); 104 LOGGER.fine(headerLine); 105 if (headerLine.equals(CRLF) || headerLine.equals("")) break; 106 107 StringTokenizer s = new StringTokenizer(headerLine); 108 String temp = s.nextToken(); 109 110 if (temp.equals("GET")) { 111 112 String serverLine = "Server: Simple httpServer"; 113 String contentTypeLine = "text"; 114 String entityBody = ""; 115 String contentLengthLine; 116 String statusLine = "HTTP/1.0 200 OK" + CRLF; 117 contentLengthLine = "Content-Length: " 118 + (new Integer(entityBody.length())).toString() + CRLF; 119 contentTypeLine = "text/html"; 120 121 output.write(statusLine.getBytes()); 122 123 output.write(serverLine.getBytes()); 124 125 output.write(contentTypeLine.getBytes()); 126 output.write(contentLengthLine.getBytes()); 127 128 output.write(CRLF.getBytes()); 129 130 output.write(entityBody.getBytes()); 131 132 } 133 } 134 135 try { 136 output.close(); 137 br.close(); 138 socket.close(); 139 } 140 catch (Exception e) { 141 // Do Nothing 142 e.printStackTrace(); 143 } 144 } 145 } 146}