Map headers = request.getHeaders();
String user_agent = (String)headers.get( "user-agent" );
String client_info = (String)headers.get( "x-av-client-info" );
InetSocketAddress client_address = request.getClientAddress2();
boolean handled = false;
if ( user_agent != null ){
String lc_agent = user_agent.toLowerCase();
if ( lc_agent.contains( "playstation 3")){
handlePS3( client_address );
handled = true;
}else if ( lc_agent.contains( "xbox")){
handleXBox( client_address );
handled = true;
}else if ( lc_agent.contains( "nintendo wii")){
handleWii( client_address );
handled = true;
}else if ( lc_agent.contains( "motorola")) {
// Linux/2.6.29 UPnP/1.0 Motorola-DLNA-Stack-DLNADOC/1.50
handleGeneric( client_address, "motorola", "Motorola DLNA" );
handled = true;
}else if ( lc_agent.contains( "sec_hhp")) {
Matcher match = Pattern.compile("SEC_HHP_(.*)/").matcher(user_agent);
if (match.find()) {
String name = match.group(1);
handleGeneric(client_address, "SEC_HPP_" + name, name);
handled = true;
}
}
}
if ( client_info != null ){
String lc_info = client_info.toLowerCase();
if ( lc_info.contains( "playstation 3")){
handlePS3( client_address );
handled = true;
}
}
if ( !handled ){
handled = manager.browseReceived( request, browser_args );
}
if ( !handled ){
String source = (String)browser_args.get( "source" );
if ( source != null && source.equalsIgnoreCase( "http" )){
handleBrowser( client_address );
handled = true;
}
}
//if (!handled && user_agent != null && !user_agent.contains("Azureus")) {
// handleGeneric(client_address, user_agent == null ? "null": user_agent, user_agent);
//}
/*
System.out.println(
"Received browse: " + request.getClientAddress() +
", agent=" + user_agent +
", info=" + client_info );
*/
DeviceImpl[] devices = manager.getDevices();
final List<DeviceMediaRendererImpl> browse_devices = new ArrayList<DeviceMediaRendererImpl>();
boolean restrict_access = false;
for ( DeviceImpl device: devices ){
if ( device instanceof DeviceMediaRendererImpl ){
DeviceMediaRendererImpl renderer = (DeviceMediaRendererImpl)device;
InetAddress device_address = renderer.getAddress();
try{
if ( device_address != null ){
// just test on IP, should be OK
if ( device_address.equals( client_address.getAddress())){
if ( renderer.canFilterFilesView()){
boolean skip = false;
if ( renderer.canRestrictAccess()){
String restriction = renderer.getAccessRestriction().trim();
if ( restriction.length() > 0 ){
String x = client_address.getAddress().getHostAddress();
skip = true;
String[] ips = restriction.split( "," );
for ( String ip: ips ){
if ( ip.startsWith( "-" )){
ip = ip.substring(1);
if ( ip.equals( x )){
break;
}
}else{
if ( ip.startsWith( "+" )){
ip = ip.substring(1);
}
if ( ip.equals( x )){
skip = false;
break;
}
}
}
}
}
if ( skip ){
restrict_access = true;
String host = client_address.getAddress().getHostAddress();
synchronized( access_logs){
if ( !access_logs.contains( host )){
access_logs.add( host );
manager.log( "Ignoring browse from " + host + " due to access restriction for '" + renderer.getName() + "'" );
}
}
}
browse_devices.add( renderer );
renderer.browseReceived();
}
}
}
}catch( Throwable e ){
Debug.out( e );
}
}
}
Map<String,Object> result = new HashMap<String, Object>();
if ( browse_devices.size() > 0 ){
synchronized( unassociated_devices ){
unassociated_devices.remove( client_address.getAddress() );
}
final boolean f_restrict_access = restrict_access;
result.put(
"filter",
new AzureusContentFilter()
{
public boolean
isVisible(
AzureusContentDownload download,
Map<String,Object> browse_args )
{
if ( f_restrict_access ){
return( false );
}
boolean visible = false;
for ( DeviceUPnPImpl device: browse_devices ){
if ( device.isVisible( download )){
visible = true;
}
}
return( visible );
}
public boolean
isVisible(
AzureusContentFile file,
Map<String,Object> browse_args )
{
if ( f_restrict_access ){
return( false );
}
boolean visible = false;
for ( DeviceUPnPImpl device: browse_devices ){
if ( device.isVisible( file )){
visible = true;
}
}
return( visible );
}
});
}else{
if ( request.getHeader().substring(0,4).equalsIgnoreCase( "POST" )){
synchronized( unassociated_devices ){
unassociated_devices.put( client_address.getAddress(), user_agent );
}
}
}
return( result );