user-agent exclusion

This commit is contained in:
Arndt Brenschede 2018-10-27 21:55:59 +02:00
parent 394eff30fa
commit 9e88f37816
2 changed files with 59 additions and 30 deletions

View file

@ -63,9 +63,50 @@ public class RouteServer extends Thread
br = new BufferedReader( new InputStreamReader( clientSocket.getInputStream() , "UTF-8") );
bw = new BufferedWriter( new OutputStreamWriter( clientSocket.getOutputStream(), "UTF-8" ) );
// we just read the first line
String getline = br.readLine();
if ( getline == null || getline.startsWith("GET /favicon.ico") )
// first line
String getline = null;
String agent = null;
// more headers until first empty line
for(;;)
{
// headers
String line = br.readLine();
if ( line == null )
{
return;
}
if ( line.length() == 0 )
{
break;
}
if ( getline == null )
{
getline = line;
}
if ( line.startsWith( "User-Agent: " ) )
{
agent = line.substring( "User-Agent: ".length() );
}
}
String excludedAgents = System.getProperty( "excludedAgents" );
if ( agent != null && excludedAgents != null )
{
StringTokenizer tk = new StringTokenizer( excludedAgents, "," );
while( tk.hasMoreTokens() )
{
if ( agent.indexOf( tk.nextToken() ) >= 0 )
{
writeHttpHeader( bw );
bw.write( "Bad agent: " + agent );
bw.flush();
return;
}
}
}
if ( getline.startsWith("GET /favicon.ico") )
{
return;
}

View file

@ -84,36 +84,24 @@ public class ProfileUploadHandler
{
// Content-Type: text/plain;charset=UTF-8
int numChars = 0;
// Content-Length header is in bytes (!= characters for UTF8),
// but Reader reads characters, so don't know number of characters to read
for(;;)
{
// headers
String line = ir.readLine();
if ( line == null ) break;
// blank line before content after headers
if ( line.length() == 0 )
{
int numChars = 0;
// Content-Length header is in bytes (!= characters for UTF8),
// but Reader reads characters, so don't know number of characters to read
for(;;)
{
// read will block when false, occurs at end of stream rather than -1
if (!ir.ready()) {
try { Thread.sleep(1000); } catch( Exception e ) {}
if ( !ir.ready() ) break;
}
int c = ir.read();
if ( c == -1) break;
bw.write( c );
numChars++;
if (numChars > MAX_LENGTH)
throw new IOException("Maximum number of characters exceeded (" + MAX_LENGTH + ", " + id + ")");
}
break;
// read will block when false, occurs at end of stream rather than -1
if (!ir.ready()) {
try { Thread.sleep(1000); } catch( Exception e ) {}
if ( !ir.ready() ) break;
}
int c = ir.read();
if ( c == -1) break;
bw.write( c );
numChars++;
if (numChars > MAX_LENGTH)
throw new IOException("Maximum number of characters exceeded (" + MAX_LENGTH + ", " + id + ")");
}
}