⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webserver.java

📁 java语言开发的P2P流媒体系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Stream-2-Stream - Peer to peer television and radio
 * October 13, 2005 - This file has been modified from the original P2P-Radio source
 * Project homepage: http://s2s.sourceforge.net/
 * Copyright (C) 2005-2006 Jason Hooks
 */

/* 
 * P2P-Radio - Peer to peer streaming system
 * Project homepage: http://p2p-radio.sourceforge.net/
 * Copyright (C) 2003-2004 Michael Kaufmann <hallo@michael-kaufmann.ch>
 * 
 * ---------------------------------------------------------------------------
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * ---------------------------------------------------------------------------
 */

package p2pradio.webinterface;
import stream2stream.network.*;
import p2pradio.*;
import p2pradio.io.*;
import p2pradio.logging.Logger;
import p2pradio.players.*;
import p2pradio.packets.MetadataPacket;
import p2pradio.tools.StringReplacer;
import stream2stream.network.MessageDispatcher;

import java.io.*;
import java.net.*;
import java.util.*;


/**
 * A simple web server that provides a page with status information.
 *
 * @author Michael Kaufmann
 */
public class WebServer extends Thread
{
	private Peer peer;
	private Player player;
	private ServerSocket tcpSocket;
	private Socket newSocket;
	
	// Ist der WebServer bereit, um Verbindungen anzunehmen?
	private boolean isReady = false;
	private boolean shutdown;
	
	public static final int REFRESH_PERIOD = 30;
	
	
	/**
	 * Creates a Web Server.
	 * 
	 * @param peer The peer whose status information will be displayed
	 * @param player The player with additional status information (currently only {@link HttpPlayer} is supported)
	 */
	public WebServer(Peer peer, Player player)
	{
		super(Messages.getString("WebServer.THREAD_NAME")); //$NON-NLS-1$
		setDaemon(true);
		this.peer = peer;
		this.player = player;
		shutdown = false;
	}
	
	public void run()
	{
		try
		{
			tcpSocket = new ServerSocket(peer.getSocketAddress().getPort() + Peer.WEBINTERFACE_PORT_OFFSET);
		}
		catch (Exception e)
		{
			if (!shutdown)
				Logger.warning("WebServer", "COULD_NOT_CREATE_TCP_SERVERSOCKET", e); //$NON-NLS-1$ //$NON-NLS-2$
			return;
		}
		
		Logger.finer("WebServer", "WebServer.THREAD_RUNNING_PORT_NUMBER", new Integer(tcpSocket.getLocalPort())); //$NON-NLS-1$ //$NON-NLS-2$
		
		isReady = true;
		
		while(!shutdown)
		{	
			newSocket = null;
			HttpInputStream inputStream = null;
			OutputStream outputStream = null;
			
			try
			{
				isReady = true;
				newSocket = tcpSocket.accept();
			}
			catch (IOException e)
			{
				if (shutdown)
					return;
				Logger.fine("WebServer", "IO_ERROR", e); //$NON-NLS-1$ //$NON-NLS-2$
				continue;
			}
			
			try
			{
				inputStream = new HttpInputStream(newSocket.getInputStream());
				outputStream = new BufferedOutputStream(newSocket.getOutputStream());
				BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
				NewLineWriter writer = new NewLineWriter(new OutputStreamWriter(outputStream), NewLineWriter.CRLF);
				
				// Die Anfrage lesen
				// Beispiel: "GET / HTTP/1.0"
							
				String line = reader.readLine();
				if (shutdown)
					return;
				if (line == null)
				{
					Logger.fine("WebServer", "WebServer.STREAM_FROM_BROWSER_ENDED"); //$NON-NLS-1$ //$NON-NLS-2$
					continue;
				}
	
				line = line.trim();
						
				int firstSpacePos = line.indexOf(" "); //$NON-NLS-1$
				if (firstSpacePos == -1)
				{
					// Ung黮tige Anfrage
					Logger.fine("WebServer", "INVALID_REQUEST"); //$NON-NLS-1$ //$NON-NLS-2$
					continue;
				}
				
				String method = line.substring(0, firstSpacePos).toLowerCase();
				
				if (!(method.equals("get") || method.equals("head"))) //$NON-NLS-1$ //$NON-NLS-2$
				{
					// Ung黮tige Anfrage
					Logger.fine("WebServer", "INVALID_REQUEST"); //$NON-NLS-1$ //$NON-NLS-2$
					continue;
				}
			
				int secondSpacePos = line.indexOf(" ", firstSpacePos+1); //$NON-NLS-1$
				if (secondSpacePos == -1)
				{
					// Kein zweites Leerzeichen
					Logger.fine("WebServer", "INVALID_REQUEST"); //$NON-NLS-1$ //$NON-NLS-2$
					continue;
				}
				
				
				String requestURL = line.substring(firstSpacePos+1, secondSpacePos).toLowerCase();
				if (requestURL.length() == 0)
				{
					// Ung黮tige URL
					Logger.fine("WebServer", "INVALID_REQUEST"); //$NON-NLS-1$ //$NON-NLS-2$
					continue;
				}
				
				String protocol = line.substring(secondSpacePos+1, line.length()).toLowerCase();
				protocol = protocol.trim();
					
				// Version entfernen (statt "HTTP/1.0" nur "HTTP")
				int slashPos = protocol.indexOf("/"); //$NON-NLS-1$
				if (slashPos != -1)
				{
					protocol = protocol.substring(0, slashPos);
				}
				
				if (!protocol.equals("http")) //$NON-NLS-1$
				{
					// Unbekanntes Protokoll
					Logger.fine("WebServer", "UNKNOWN_PROTOCOL", protocol); //$NON-NLS-1$ //$NON-NLS-2$
					return;
				}
				
				
				// Alle Headerzeilen 黚erlesen
				
				boolean error = false;
				
				while (!shutdown)
				{
					line = reader.readLine();
					if(shutdown)
						return;
					if ((line == null) || (line.length() == 0))
					{
						break;
					}
					
					int colonPos = line.indexOf(":"); //$NON-NLS-1$
					
					if (colonPos == -1)
					{
						// Kein Doppelpunkt kommt vor - es kann keine
						// Headerzeile mehr sein
						error = true;
						break;
					}
			
					/*
					String key = line.substring(0, colonPos);
					String value = line.substring(colonPos+1, line.length());
					key = key.trim();
					value = value.trim();
				
					String keyLowerCase = key.toLowerCase();
						
					if (keyLowerCase.equals("user-agent"))
					{
						debugMessage("Zugriff von: " + value);
					}
					*/
				}
				if (shutdown)
					return;
				
				if (error)
				{
					// N鋍hste Anfrage
					continue;				
				}
	
				// Anfrage bearbeiten 
				
				if (method.equals("get")) //$NON-NLS-1$
				{
					// URL spielt keine Rolle
					printInformation(writer);
					if (shutdown)return;
				}									
				else
				if (method.equals("head")) //$NON-NLS-1$
				{
					writer.writeln("HTTP/1.0 200 OK"); //$NON-NLS-1$
					writer.writeln("Content-type: text/html"); //$NON-NLS-1$
					writer.writeln();
				
					writer.flush();
					if (shutdown)return;
					
					continue;
				}
			}
			catch (IOException e)
			{
				if (shutdown)
					return;
				Logger.fine("WebServer", "IO_ERROR", e); //$NON-NLS-1$ //$NON-NLS-2$
			}
			catch (Exception e)
			{
				if (shutdown)
					return;
				Logger.severe("WebServer", "INTERNAL_ERROR", e); //$NON-NLS-1$ //$NON-NLS-2$
			}
			finally
			{
				if (inputStream != null)
				{
					try
					{
						inputStream.close();
					}
					catch (IOException e)
					{
					}
				}
				
				if (outputStream != null)
				{
					try
					{
						outputStream.close();
					}
					catch (IOException e)
					{
					}
				}
				
				if (newSocket != null)
				{
					try
					{
						newSocket.close();
					}
					catch (IOException e)
					{
					}
				}
			}
		}
	}
	
	protected void printInformation(NewLineWriter writer) throws IOException
	{
		writer.writeln("HTTP/1.0 200 OK"); //$NON-NLS-1$
		writer.writeln("Content-type: text/html"); //$NON-NLS-1$
		writer.writeln();
		
		writer.setNewLineMark(NewLineWriter.LF);
		writer.writeln("<HTML>"); //$NON-NLS-1$
		writer.writeln("<HEAD>"); //$NON-NLS-1$
		writer.writeln("<TITLE>" + textToHTML(Radio.s2sName) + "</TITLE>");				 //$NON-NLS-1$ //$NON-NLS-2$
		writer.writeln("<STYLE TYPE=\"text/css\">"); //$NON-NLS-1$

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -