2005-04-07 21:19:42 +02:00
// httpd.java
// -----------------------
2008-07-20 19:14:51 +02:00
// (C) by Michael Peter Christen; mc@yacy.net
2005-04-07 21:19:42 +02:00
// first published on http://www.anomic.de
// Frankfurt, Germany, 2004
2005-06-09 12:07:02 +02:00
//
2008-03-05 14:29:42 +01:00
// $LastChangedDate$
// $LastChangedRevision$
// $LastChangedBy$
2005-04-07 21:19:42 +02:00
//
// 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 de.anomic.http ;
2008-08-08 03:40:28 +02:00
import java.io.ByteArrayInputStream ;
2005-06-09 12:07:02 +02:00
import java.io.ByteArrayOutputStream ;
2006-12-16 22:40:59 +01:00
import java.io.CharArrayWriter ;
2005-06-09 12:07:02 +02:00
import java.io.File ;
import java.io.FileInputStream ;
2005-05-05 07:32:19 +02:00
import java.io.IOException ;
2005-09-22 12:30:55 +02:00
import java.io.InputStream ;
2005-06-09 12:07:02 +02:00
import java.io.OutputStream ;
import java.io.PrintStream ;
2007-02-02 22:20:53 +01:00
import java.io.UnsupportedEncodingException ;
2005-05-05 07:32:19 +02:00
import java.net.InetAddress ;
2005-06-09 12:07:02 +02:00
import java.net.MalformedURLException ;
2008-08-08 03:40:28 +02:00
import java.net.URLDecoder ;
2007-02-02 22:20:53 +01:00
import java.net.URLEncoder ;
2005-05-09 00:36:26 +02:00
import java.util.Arrays ;
2005-06-09 12:07:02 +02:00
import java.util.Date ;
2005-05-05 07:32:19 +02:00
import java.util.HashMap ;
2005-05-09 00:36:26 +02:00
import java.util.HashSet ;
2005-06-09 12:07:02 +02:00
import java.util.Iterator ;
2008-08-08 03:40:28 +02:00
import java.util.List ;
2005-05-05 07:32:19 +02:00
import java.util.Properties ;
import java.util.StringTokenizer ;
2005-04-19 08:55:57 +02:00
2008-08-08 03:40:28 +02:00
import org.apache.commons.fileupload.FileItem ;
import org.apache.commons.fileupload.FileItemFactory ;
import org.apache.commons.fileupload.FileUpload ;
import org.apache.commons.fileupload.FileUploadBase ;
import org.apache.commons.fileupload.FileUploadException ;
import org.apache.commons.fileupload.RequestContext ;
import org.apache.commons.fileupload.disk.DiskFileItemFactory ;
2008-05-04 12:53:04 +02:00
import org.apache.commons.httpclient.ChunkedInputStream ;
2007-05-20 15:29:12 +02:00
import de.anomic.data.htmlTools ;
2006-09-30 00:27:20 +02:00
import de.anomic.data.userDB ;
2007-02-05 20:46:50 +01:00
import de.anomic.kelondro.kelondroBase64Order ;
2006-09-30 00:27:20 +02:00
import de.anomic.plasma.plasmaSwitchboard ;
2005-06-09 12:07:02 +02:00
import de.anomic.server.serverByteBuffer ;
2005-05-05 07:32:19 +02:00
import de.anomic.server.serverCodings ;
import de.anomic.server.serverCore ;
2007-07-24 02:46:17 +02:00
import de.anomic.server.serverDomains ;
2005-06-09 12:07:02 +02:00
import de.anomic.server.serverFileUtils ;
2005-05-05 07:32:19 +02:00
import de.anomic.server.serverHandler ;
import de.anomic.server.serverObjects ;
import de.anomic.server.serverSwitch ;
2005-06-09 12:07:02 +02:00
import de.anomic.server.logging.serverLog ;
2007-09-05 11:01:35 +02:00
import de.anomic.yacy.yacyURL ;
2005-08-30 23:10:39 +02:00
2005-06-09 12:07:02 +02:00
/ * *
* Instances of this class can be passed as argument to the serverCore .
* The generic server dispatches HTTP commands and calls the
* method GET , HEAD or POST in this class
* these methods parse the command line and decide wether to call
* a proxy servlet or a file server servlet
* /
2008-08-02 15:57:00 +02:00
public final class httpd implements serverHandler , Cloneable {
2005-06-09 12:07:02 +02:00
2007-02-05 20:46:50 +01:00
/ * *
* < p > < code > public static final String < strong > ADMIN_ACCOUNT_B64MD5 < / strong > = " adminAccountBase64MD5 " < / code > < / p >
* < p > Name of the setting holding the authentification hash for the static < code > admin < / code > - account . It is calculated
* by first encoding < code > username : password < / code > as Base64 and hashing it using { @link serverCodings # encodeMD5Hex ( String ) } . < / p >
* /
public static final String ADMIN_ACCOUNT_B64MD5 = " adminAccountBase64MD5 " ;
2007-01-06 12:05:50 +01:00
public static final int ERRORCASE_MESSAGE = 4 ;
public static final int ERRORCASE_FILE = 5 ;
2008-05-06 01:13:47 +02:00
public static httpdAlternativeDomainNames alternativeResolver = null ;
2007-01-06 12:05:50 +01:00
2005-06-09 12:07:02 +02:00
/ * *
* A hashset containing extensions that indicate content that should not be transported
* using zipped content encoding
* @see # shallTransportZipped ( String )
* /
2006-03-23 21:12:23 +01:00
//TODO: Load this from a file
2008-01-28 19:21:08 +01:00
private static final HashSet < String > disallowZippedContentEncoding = new HashSet < String > ( Arrays . asList ( new String [ ] {
2007-07-15 14:55:41 +02:00
" .gz " , " .tgz " , " .jpg " , " .jpeg " , " .gif " , " .zip " , " .rar " , " .bz2 " , " .lha " , " .jar " , " .rpm " , " .arc " , " .arj " , " .wmv " , " .png " , " .ico " , " .bmp "
2005-06-09 12:07:02 +02:00
} ) ) ;
2005-04-07 21:19:42 +02:00
// static objects
public static final String vDATE = " <<REPL>> " ;
public static final String copyright = " [ HTTP SERVER: AnomicHTTPD v " + vDATE + " by Michael Christen / www.anomic.de ] " ;
public static final String hline = " ------------------------------------------------------------------------------- " ;
2005-06-09 12:07:02 +02:00
2008-08-02 15:57:00 +02:00
public static final HashMap < String , String > reverseMappingCache = new HashMap < String , String > ( ) ;
private static volatile plasmaSwitchboard switchboard = null ;
2005-04-07 21:19:42 +02:00
private static String virtualHost = null ;
2005-06-09 12:07:02 +02:00
public static boolean keepAliveSupport = false ;
2008-01-28 19:21:08 +01:00
private static HashMap < String , Long > YaCyHopAccessRequester = new HashMap < String , Long > ( ) ;
private static HashMap < String , Long > YaCyHopAccessTargets = new HashMap < String , Long > ( ) ;
2005-06-09 12:07:02 +02:00
2005-04-07 21:19:42 +02:00
// class objects
private serverCore . Session session ; // holds the session object of the calling class
private InetAddress userAddress ; // the address of the client
2005-06-09 12:07:02 +02:00
2005-04-07 21:19:42 +02:00
// for authentication
2005-10-07 15:49:07 +02:00
private boolean use_proxyAccounts = false ;
private boolean proxyAccounts_init = false ; // is use_proxyAccounts set?
2005-04-07 21:19:42 +02:00
private String serverAccountBase64MD5 ;
private String clientIP ;
2007-02-09 16:25:10 +01:00
private boolean allowProxy ;
private boolean allowServer ;
private boolean allowYaCyHop ;
2005-04-19 08:55:57 +02:00
// the connection properties
2005-04-20 08:41:52 +02:00
private final Properties prop = new Properties ( ) ;
2005-04-19 08:55:57 +02:00
2005-06-09 12:07:02 +02:00
private int emptyRequestCount = 0 ;
private int keepAliveRequestCount = 0 ;
// needed for logging
2008-08-08 03:40:28 +02:00
private final static serverLog log = new serverLog ( " HTTPD " ) ;
2005-10-07 15:49:07 +02:00
2005-04-07 21:19:42 +02:00
// class methods
2008-08-02 14:12:04 +02:00
public httpd ( final serverSwitch < ? > s ) {
2005-04-07 21:19:42 +02:00
// handler info
2005-10-07 15:49:07 +02:00
httpd . switchboard = ( plasmaSwitchboard ) s ;
2005-04-19 08:55:57 +02:00
httpd . virtualHost = switchboard . getConfig ( " fileHost " , " localhost " ) ;
2005-04-07 21:19:42 +02:00
// authentication: by default none
2005-10-07 15:49:07 +02:00
this . proxyAccounts_init = false ;
2005-04-07 21:19:42 +02:00
this . serverAccountBase64MD5 = null ;
this . clientIP = null ;
2005-06-09 12:07:02 +02:00
// configuring keep alive support
keepAliveSupport = Boolean . valueOf ( switchboard . getConfig ( " connectionKeepAliveSupport " , " false " ) ) . booleanValue ( ) ;
2005-04-07 21:19:42 +02:00
}
2005-10-12 10:17:43 +02:00
public Properties getConProp ( ) {
return this . prop ;
}
2005-06-09 12:07:02 +02:00
/ * *
* Can be used to reset this { @link serverHandler } oject so that
* it can be reused for further connections
* @see de . anomic . server . serverHandler # reset ( )
* /
2005-04-19 08:55:57 +02:00
public void reset ( ) {
this . session = null ;
this . userAddress = null ;
this . allowProxy = false ;
this . allowServer = false ;
2007-01-31 01:09:51 +01:00
this . allowYaCyHop = false ;
2005-10-07 15:49:07 +02:00
this . proxyAccounts_init = false ;
2005-04-19 08:55:57 +02:00
this . serverAccountBase64MD5 = null ;
this . clientIP = null ;
2005-04-20 08:41:52 +02:00
this . prop . clear ( ) ;
2005-06-09 12:07:02 +02:00
this . emptyRequestCount = 0 ;
this . keepAliveRequestCount = 0 ;
2005-04-19 08:55:57 +02:00
}
2008-03-05 14:29:42 +01:00
/ * *
2005-06-09 12:07:02 +02:00
* Must be called at least once , but can be called again to re - use the object .
* @see de . anomic . server . serverHandler # initSession ( de . anomic . server . serverCore . Session )
* /
2008-08-02 14:12:04 +02:00
public void initSession ( final serverCore . Session newsession ) throws IOException {
2005-12-06 22:21:14 +01:00
this . session = newsession ;
2005-04-07 21:19:42 +02:00
this . userAddress = session . userAddress ; // client InetAddress
2008-03-05 14:29:42 +01:00
this . clientIP = this . userAddress . getHostAddress ( ) ;
2005-06-09 12:07:02 +02:00
if ( this . userAddress . isAnyLocalAddress ( ) ) this . clientIP = " localhost " ;
2008-05-25 20:35:38 +02:00
if ( this . clientIP . startsWith ( " 0:0:0:0:0:0:0:1 " ) ) this . clientIP = " localhost " ;
2008-05-25 22:30:37 +02:00
if ( this . clientIP . startsWith ( " 127. " ) ) this . clientIP = " localhost " ;
2008-03-05 14:41:54 +01:00
final String proxyClient = switchboard . getConfig ( " proxyClient " , " * " ) ;
final String serverClient = switchboard . getConfig ( " serverClient " , " * " ) ;
2008-03-05 14:29:42 +01:00
2005-06-09 12:07:02 +02:00
this . allowProxy = ( proxyClient . equals ( " * " ) ) ? true : match ( this . clientIP , proxyClient ) ;
this . allowServer = ( serverClient . equals ( " * " ) ) ? true : match ( this . clientIP , serverClient ) ;
2007-01-31 01:09:51 +01:00
this . allowYaCyHop = switchboard . getConfigBool ( " YaCyHop " , false ) ;
2008-03-05 14:29:42 +01:00
2005-06-09 12:07:02 +02:00
// check if we want to allow this socket to connect us
2007-01-31 01:09:51 +01:00
if ( ! ( this . allowProxy | | this . allowServer | | this . allowYaCyHop ) ) {
2008-03-05 14:41:54 +01:00
final String errorMsg = " CONNECTION FROM " + this . userAddress . getHostName ( ) + " [ " + this . clientIP + " ] FORBIDDEN " ;
2005-06-09 12:07:02 +02:00
this . log . logWarning ( errorMsg ) ;
throw new IOException ( errorMsg ) ;
}
2008-03-05 14:29:42 +01:00
2005-10-07 15:49:07 +02:00
this . proxyAccounts_init = false ;
2005-06-09 12:07:02 +02:00
this . serverAccountBase64MD5 = null ;
2005-04-07 21:19:42 +02:00
}
2008-03-05 14:29:42 +01:00
2008-08-02 14:12:04 +02:00
private static boolean match ( final String key , final String latch ) {
2005-06-09 12:07:02 +02:00
// the latch is a comma-separated list of patterns
// each pattern may contain one wildcard-character '*' which matches anything
2008-08-02 14:12:04 +02:00
final StringTokenizer st = new StringTokenizer ( latch , " , " ) ;
2005-06-09 12:07:02 +02:00
String pattern ;
while ( st . hasMoreTokens ( ) ) {
pattern = st . nextToken ( ) ;
2005-07-04 01:33:25 +02:00
if ( key . matches ( pattern ) ) return true ;
/ *
2005-06-09 12:07:02 +02:00
pos = pattern . indexOf ( " * " ) ;
if ( pos < 0 ) {
// no wild card: exact match
if ( key . equals ( pattern ) ) return true ;
} else {
// wild card: match left and right side of pattern
if ( ( key . startsWith ( pattern . substring ( 0 , pos ) ) ) & &
( key . endsWith ( pattern . substring ( pos + 1 ) ) ) ) return true ;
}
2005-07-04 01:33:25 +02:00
* /
2005-06-09 12:07:02 +02:00
}
return false ;
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
2005-04-07 21:19:42 +02:00
public String greeting ( ) { // OBLIGATORIC FUNCTION
2005-06-09 12:07:02 +02:00
// a response line upon connection is send to client
// if no response line is wanted, return "" or null
return null ;
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
2008-08-02 14:12:04 +02:00
public String error ( final Throwable e ) { // OBLIGATORIC FUNCTION
2005-06-09 12:07:02 +02:00
// return string in case of any error that occurs during communication
// is always (but not only) called if an IO-dependent exception occurrs.
2005-08-30 23:32:59 +02:00
this . log . logSevere ( " Unexpected Error. " + e . getClass ( ) . getName ( ) , e ) ;
2008-08-02 14:12:04 +02:00
final String message = e . getMessage ( ) ;
2007-03-08 17:15:40 +01:00
if ( message . indexOf ( " heap space " ) > 0 ) e . printStackTrace ( ) ;
return " 501 Exception occurred: " + message ;
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
/ * *
2008-05-20 00:17:53 +02:00
* This function is used to determine if a persistent connection was requested by the client .
2005-06-09 12:07:02 +02:00
* @param header the received http - headers
* @return < code > true < / code > if a persistent connection was requested or < code > false < / code > otherwise
* /
2008-08-02 14:12:04 +02:00
private boolean handlePersistentConnection ( final httpHeader header ) {
2005-06-09 12:07:02 +02:00
if ( ! keepAliveSupport ) {
2005-09-20 23:49:47 +02:00
this . prop . put ( httpHeader . CONNECTION_PROP_PERSISTENT , " close " ) ;
2005-06-09 12:07:02 +02:00
return false ;
}
// getting the http version that is used by the client
2008-08-02 14:12:04 +02:00
final String httpVersion = this . prop . getProperty ( httpHeader . CONNECTION_PROP_HTTP_VER , " HTTP/0.9 " ) ;
2005-06-09 12:07:02 +02:00
// managing keep-alive: in HTTP/0.9 and HTTP/1.0 every connection is closed
// afterwards. In HTTP/1.1 (and above, in the future?) connections are
// persistent by default, but closed with the "Connection: close"
// property.
2006-10-28 14:31:48 +02:00
boolean persistent = ! ( httpVersion . equals ( httpHeader . HTTP_VERSION_0_9 ) | | httpVersion . equals ( httpHeader . HTTP_VERSION_1_0 ) ) ;
2006-06-20 07:22:55 +02:00
if ( ( ( String ) header . get ( httpHeader . CONNECTION , " keep-alive " ) ) . toLowerCase ( ) . indexOf ( " close " ) ! = - 1 | |
( ( String ) header . get ( httpHeader . PROXY_CONNECTION , " keep-alive " ) ) . toLowerCase ( ) . indexOf ( " close " ) ! = - 1 ) {
2005-06-09 12:07:02 +02:00
persistent = false ;
}
2008-08-02 14:12:04 +02:00
final String transferEncoding = ( String ) header . get ( httpHeader . TRANSFER_ENCODING , " identity " ) ;
final boolean isPostRequest = this . prop . getProperty ( httpHeader . CONNECTION_PROP_METHOD ) . equals ( httpHeader . METHOD_POST ) ;
final boolean hasContentLength = header . containsKey ( httpHeader . CONTENT_LENGTH ) ;
final boolean hasTransferEncoding = header . containsKey ( httpHeader . TRANSFER_ENCODING ) & & ! transferEncoding . equalsIgnoreCase ( " identity " ) ;
2006-11-09 06:59:56 +01:00
2005-06-09 12:07:02 +02:00
// if the request does not contain a content-length we have to close the connection
// independently of the value of the connection header
2006-11-09 06:59:56 +01:00
if ( persistent & & isPostRequest & & ! ( hasContentLength | | hasTransferEncoding ) )
this . prop . put ( httpHeader . CONNECTION_PROP_PERSISTENT , " close " ) ;
2005-09-20 23:49:47 +02:00
else this . prop . put ( httpHeader . CONNECTION_PROP_PERSISTENT , persistent ? " keep-alive " : " close " ) ;
2005-06-09 12:07:02 +02:00
return persistent ;
}
2008-08-02 14:12:04 +02:00
public static int staticAdminAuthenticated ( final String authorization , final serverSwitch < ? > sw ) {
2008-05-15 13:26:43 +02:00
// the authorization string must be given with the truncated 6 bytes at the beginning
if ( authorization = = null ) return 1 ;
2007-02-05 20:46:50 +01:00
//if (authorization.length() < 6) return 1; // no authentication information given
2008-08-02 14:12:04 +02:00
final String adminAccountBase64MD5 = sw . getConfig ( ADMIN_ACCOUNT_B64MD5 , " " ) ;
2008-05-20 00:17:53 +02:00
if ( adminAccountBase64MD5 . length ( ) = = 0 ) return 2 ; // no password stored
2007-02-05 20:46:50 +01:00
if ( adminAccountBase64MD5 . equals ( serverCodings . encodeMD5Hex ( authorization ) ) ) return 4 ; // hard-authenticated, all ok
2007-12-10 03:04:48 +01:00
return 1 ;
2007-02-05 20:46:50 +01:00
}
2008-08-02 14:12:04 +02:00
private boolean handleServerAuthentication ( final httpHeader header ) throws IOException {
2005-06-09 12:07:02 +02:00
// getting the http version that is used by the client
2008-08-02 14:12:04 +02:00
final String httpVersion = this . prop . getProperty ( httpHeader . CONNECTION_PROP_HTTP_VER , " HTTP/0.9 " ) ;
2005-06-09 12:07:02 +02:00
// reading the authentication settings from switchboard
if ( this . serverAccountBase64MD5 = = null )
this . serverAccountBase64MD5 = switchboard . getConfig ( " serverAccountBase64MD5 " , " " ) ;
if ( this . serverAccountBase64MD5 . length ( ) > 0 ) {
2008-08-02 14:12:04 +02:00
final String auth = header . get ( httpHeader . AUTHORIZATION ) ;
2005-06-09 12:07:02 +02:00
if ( auth = = null ) {
// authorization requested, but no authorizeation given in header. Ask for authenticate:
2007-12-14 20:17:54 +01:00
this . session . out . write ( ( httpVersion + " 401 log-in required " + serverCore . CRLF_STRING +
httpHeader . WWW_AUTHENTICATE + " : Basic realm= \" log-in \" " + serverCore . CRLF_STRING +
serverCore . CRLF_STRING ) . getBytes ( ) ) ;
2005-06-09 12:07:02 +02:00
this . session . out . write ( ( httpHeader . CONTENT_LENGTH + " : 0 \ r \ n " ) . getBytes ( ) ) ;
this . session . out . write ( " \ r \ n " . getBytes ( ) ) ;
return false ;
} else if ( ! this . serverAccountBase64MD5 . equals ( serverCodings . encodeMD5Hex ( auth . trim ( ) . substring ( 6 ) ) ) ) {
// wrong password given: ask for authenticate again
2008-08-08 03:40:28 +02:00
log . logInfo ( " Wrong log-in for account 'server' in HTTPD.GET " + this . prop . getProperty ( " PATH " ) + " from IP " + this . clientIP ) ;
2007-12-14 20:17:54 +01:00
this . session . out . write ( ( httpVersion + " 401 log-in required " + serverCore . CRLF_STRING +
2005-06-09 12:07:02 +02:00
httpHeader . WWW_AUTHENTICATE + " : Basic realm= \" log-in \" " +
2007-12-14 20:17:54 +01:00
serverCore . CRLF_STRING ) . getBytes ( ) ) ;
2005-06-09 12:07:02 +02:00
this . session . out . write ( ( httpHeader . CONTENT_LENGTH + " : 0 \ r \ n " ) . getBytes ( ) ) ;
this . session . out . write ( " \ r \ n " . getBytes ( ) ) ;
2008-04-05 15:17:16 +02:00
this . session . out . flush ( ) ;
2005-06-09 12:07:02 +02:00
return false ;
}
}
return true ;
}
2008-08-02 14:12:04 +02:00
private boolean handleYaCyHopAuthentication ( final httpHeader header ) {
2007-02-09 16:25:10 +01:00
// check if the user has allowed that his/her peer is used for hops
if ( ! this . allowYaCyHop ) return false ;
// proxy hops must identify with 4 criteria:
// the accessed port must not be port 80
2008-08-02 14:12:04 +02:00
final String host = this . prop . getProperty ( httpHeader . CONNECTION_PROP_HOST ) ;
2007-02-09 16:25:10 +01:00
if ( host = = null ) return false ;
int pos ;
if ( ( pos = host . indexOf ( " : " ) ) < 0 ) {
// default port 80
return false ; // not allowed
}
2008-08-02 15:57:00 +02:00
if ( Integer . parseInt ( host . substring ( pos + 1 ) ) = = 80 ) return false ;
2007-02-05 20:46:50 +01:00
// the access path must be into the yacy protocol path; it must start with 'yacy'
if ( ! ( this . prop . getProperty ( httpHeader . CONNECTION_PROP_PATH , " " ) . startsWith ( " /yacy/ " ) ) ) return false ;
// the accessing client must identify with user:password, where
// user = addressed peer name
// pw = addressed peer hash (b64-hash)
2008-08-02 14:12:04 +02:00
final String auth = ( String ) header . get ( httpHeader . PROXY_AUTHORIZATION , " xxxxxx " ) ;
2008-05-06 01:13:47 +02:00
if ( alternativeResolver ! = null ) {
2008-08-02 14:12:04 +02:00
final String test = kelondroBase64Order . standardCoder . encodeString ( alternativeResolver . myName ( ) + " : " + alternativeResolver . myID ( ) ) ;
2008-05-06 01:13:47 +02:00
if ( ! test . equals ( auth . trim ( ) . substring ( 6 ) ) ) return false ;
}
2007-02-05 20:46:50 +01:00
// the accessing client must use a yacy user-agent
2007-02-09 16:25:10 +01:00
if ( ! ( ( ( String ) header . get ( httpHeader . USER_AGENT , " " ) ) . startsWith ( " yacy " ) ) ) return false ;
// furthermore, YaCy hops must not exceed a specific access frequency
// check access requester frequency: protection against DoS against this peer
2008-08-02 14:12:04 +02:00
final String requester = this . prop . getProperty ( httpHeader . CONNECTION_PROP_CLIENTIP ) ;
2007-02-09 16:25:10 +01:00
if ( requester = = null ) return false ;
if ( lastAccessDelta ( YaCyHopAccessRequester , requester ) < 10000 ) return false ;
2008-08-06 21:43:12 +02:00
YaCyHopAccessRequester . put ( requester , Long . valueOf ( System . currentTimeMillis ( ) ) ) ;
2007-02-09 16:25:10 +01:00
// check access target frequecy: protection against DoS from a single peer by several different requesters
if ( lastAccessDelta ( YaCyHopAccessTargets , host ) < 3000 ) return false ;
2008-08-06 21:43:12 +02:00
YaCyHopAccessTargets . put ( host , Long . valueOf ( System . currentTimeMillis ( ) ) ) ;
2007-02-09 16:25:10 +01:00
// passed all tests
return true ;
2007-02-05 20:46:50 +01:00
}
2008-08-02 14:12:04 +02:00
private static long lastAccessDelta ( final HashMap < String , Long > accessTable , final String domain ) {
final Long lastAccess = accessTable . get ( domain ) ;
2007-02-09 16:25:10 +01:00
if ( lastAccess = = null ) return Long . MAX_VALUE ; // never accessed
return System . currentTimeMillis ( ) - lastAccess . longValue ( ) ;
}
2008-08-02 14:12:04 +02:00
private boolean handleProxyAuthentication ( final httpHeader header ) throws IOException {
2005-06-09 12:07:02 +02:00
// getting the http version that is used by the client
2008-08-02 14:12:04 +02:00
final String httpVersion = this . prop . getProperty ( " HTTP " , " HTTP/0.9 " ) ;
2005-06-09 12:07:02 +02:00
// reading the authentication settings from switchboard
2008-03-05 14:29:42 +01:00
if ( ! this . proxyAccounts_init ) {
2008-08-06 21:43:12 +02:00
this . use_proxyAccounts = switchboard . getConfigBool ( " use_proxyAccounts " , false ) ;
2005-10-07 15:49:07 +02:00
this . proxyAccounts_init = true ; // is initialised
}
2005-06-09 12:07:02 +02:00
2005-10-07 15:49:07 +02:00
if ( this . use_proxyAccounts ) {
2008-08-02 14:12:04 +02:00
final String auth = ( String ) header . get ( httpHeader . PROXY_AUTHORIZATION , " xxxxxx " ) ;
2005-10-14 12:53:50 +02:00
userDB . Entry entry = switchboard . userDB . ipAuth ( this . clientIP ) ;
if ( entry = = null ) {
entry = switchboard . userDB . proxyAuth ( auth , this . clientIP ) ;
}
if ( entry ! = null ) {
2008-08-02 14:12:04 +02:00
final int returncode = entry . surfRight ( ) ;
2006-05-11 15:12:35 +02:00
if ( returncode = = userDB . Entry . PROXY_ALLOK ) {
2005-10-14 12:53:50 +02:00
return true ;
}
2008-08-02 14:12:04 +02:00
final serverObjects tp = new serverObjects ( ) ;
2006-05-11 15:12:35 +02:00
if ( returncode = = userDB . Entry . PROXY_TIMELIMIT_REACHED ) {
tp . put ( " limit " , " 1 " ) ; //time per day
tp . put ( " limit_timelimit " , entry . getTimeLimit ( ) ) ;
sendRespondError ( this . prop , this . session . out , 403 , " Internet-Timelimit reached " , new File ( " proxymsg/proxylimits.inc " ) , tp , null ) ;
} else if ( returncode = = userDB . Entry . PROXY_NORIGHT ) {
tp . put ( " limit " , " 0 " ) ;
sendRespondError ( this . prop , this . session . out , 403 , " Proxy use forbidden " , new File ( " proxymsg/proxylimits.inc " ) , tp , null ) ;
}
2005-12-07 00:51:29 +01:00
return false ;
2005-10-07 15:49:07 +02:00
}
// ask for authenticate
2007-12-14 20:17:54 +01:00
this . session . out . write ( ( httpVersion + " 407 Proxy Authentication Required " + serverCore . CRLF_STRING +
httpHeader . PROXY_AUTHENTICATE + " : Basic realm= \" log-in \" " + serverCore . CRLF_STRING ) . getBytes ( ) ) ;
2005-10-07 15:49:07 +02:00
this . session . out . write ( ( httpHeader . CONTENT_LENGTH + " : 0 \ r \ n " ) . getBytes ( ) ) ;
this . session . out . write ( " \ r \ n " . getBytes ( ) ) ;
2008-04-05 15:17:16 +02:00
this . session . out . flush ( ) ;
2005-10-07 15:49:07 +02:00
return false ;
2005-12-07 00:51:29 +01:00
}
return true ;
2005-06-09 12:07:02 +02:00
}
2008-08-02 14:12:04 +02:00
public Boolean UNKNOWN ( final String requestLine ) throws IOException {
2005-06-09 12:07:02 +02:00
int pos ;
String unknownCommand = null , args = null ;
if ( ( pos = requestLine . indexOf ( " " ) ) > 0 ) {
unknownCommand = requestLine . substring ( 0 , pos ) ;
args = requestLine . substring ( pos + 1 ) ;
} else {
unknownCommand = requestLine ;
args = " " ;
}
2005-09-20 23:49:47 +02:00
parseRequestLine ( unknownCommand , args ) ;
2005-12-05 01:17:12 +01:00
//String httpVersion = this.prop.getProperty(httpHeader.CONNECTION_PROP_HTTP_VER,"HTTP/0.9");
2005-06-09 12:07:02 +02:00
sendRespondError ( this . prop , this . session . out , 0 , 501 , null , unknownCommand + " method not implemented " , null ) ;
return serverCore . TERMINATE_CONNECTION ;
}
2008-08-02 14:12:04 +02:00
public Boolean EMPTY ( final String arg ) throws IOException {
2005-06-09 12:07:02 +02:00
if ( + + this . emptyRequestCount > 10 ) return serverCore . TERMINATE_CONNECTION ;
return serverCore . RESUME_CONNECTION ;
}
2008-08-02 15:57:00 +02:00
public Boolean TRACE ( ) throws IOException {
2005-06-09 12:07:02 +02:00
sendRespondError ( this . prop , this . session . out , 0 , 501 , null , " TRACE method not implemented " , null ) ;
return serverCore . TERMINATE_CONNECTION ;
}
2008-08-02 15:57:00 +02:00
public Boolean OPTIONS ( ) throws IOException {
2005-06-09 12:07:02 +02:00
sendRespondError ( this . prop , this . session . out , 0 , 501 , null , " OPTIONS method not implemented " , null ) ;
return serverCore . TERMINATE_CONNECTION ;
}
2008-08-02 14:12:04 +02:00
public Boolean GET ( final String arg ) {
2005-06-09 12:07:02 +02:00
try {
// parsing the http request line
2005-09-20 23:49:47 +02:00
parseRequestLine ( httpHeader . METHOD_GET , arg ) ;
2005-06-09 12:07:02 +02:00
// we now know the HTTP version. depending on that, we read the header
2008-08-02 14:12:04 +02:00
final String httpVersion = this . prop . getProperty ( httpHeader . CONNECTION_PROP_HTTP_VER , httpHeader . HTTP_VERSION_0_9 ) ;
final httpHeader header = ( httpVersion . equals ( httpHeader . HTTP_VERSION_0_9 ) )
2006-11-04 17:33:02 +01:00
? new httpHeader ( reverseMappingCache )
: httpHeader . readHeader ( this . prop , this . session ) ;
2005-06-09 12:07:02 +02:00
// handling transparent proxy support
2005-11-02 18:56:26 +01:00
httpHeader . handleTransparentProxySupport ( header , this . prop , virtualHost , httpdProxyHandler . isTransparentProxy ) ;
2005-06-09 12:07:02 +02:00
// determines if the connection should be kept alive
handlePersistentConnection ( header ) ;
2005-09-20 23:49:47 +02:00
if ( this . prop . getProperty ( httpHeader . CONNECTION_PROP_HOST ) . equals ( virtualHost ) ) {
2005-06-09 12:07:02 +02:00
// pass to server
if ( this . allowServer ) {
2007-09-05 11:01:35 +02:00
if ( this . handleServerAuthentication ( header ) ) {
httpdFileHandler . doGet ( this . prop , header , this . session . out ) ;
2005-05-17 10:25:04 +02:00
}
2005-04-07 21:19:42 +02:00
} else {
2005-06-09 12:07:02 +02:00
// not authorized through firewall blocking (ip does not match filter)
2007-12-14 20:17:54 +01:00
this . session . out . write ( ( httpVersion + " 403 refused (IP not granted) " + serverCore . CRLF_STRING + serverCore . CRLF_STRING + " you are not allowed to connect to this server, because you are using the non-granted IP " + clientIP + " . allowed are only connections that match with the following filter: " + switchboard . getConfig ( " serverClient " , " * " ) + serverCore . CRLF_STRING ) . getBytes ( ) ) ;
2005-06-09 12:07:02 +02:00
return serverCore . TERMINATE_CONNECTION ;
}
} else {
// pass to proxy
2007-02-05 20:46:50 +01:00
if ( ( ( this . allowYaCyHop ) & & ( handleYaCyHopAuthentication ( header ) ) ) | |
( ( this . allowProxy ) & & ( handleProxyAuthentication ( header ) ) ) ) {
2007-08-09 23:58:38 +02:00
httpdProxyHandler . doGet ( this . prop , header , this . session . out ) ;
2005-06-09 12:07:02 +02:00
} else {
// not authorized through firewall blocking (ip does not match filter)
2007-12-14 20:17:54 +01:00
this . session . out . write ( ( httpVersion + " 403 refused (IP not granted) " + serverCore . CRLF_STRING + serverCore . CRLF_STRING + " you are not allowed to connect to this proxy, because you are using the non-granted IP " + clientIP + " . allowed are only connections that match with the following filter: " + switchboard . getConfig ( " proxyClient " , " * " ) + serverCore . CRLF_STRING ) . getBytes ( ) ) ;
2005-06-09 12:07:02 +02:00
return serverCore . TERMINATE_CONNECTION ;
2005-04-07 21:19:42 +02:00
}
2005-05-17 10:25:04 +02:00
}
2005-06-09 12:07:02 +02:00
2005-09-20 23:49:47 +02:00
return this . prop . getProperty ( httpHeader . CONNECTION_PROP_PERSISTENT ) . equals ( " keep-alive " ) ? serverCore . RESUME_CONNECTION : serverCore . TERMINATE_CONNECTION ;
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) {
2005-06-28 13:27:31 +02:00
logUnexpectedError ( e ) ;
2005-06-09 12:07:02 +02:00
return serverCore . TERMINATE_CONNECTION ;
2005-09-30 18:02:58 +02:00
} finally {
this . doUserAccounting ( this . prop ) ;
2005-06-09 12:07:02 +02:00
}
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
2008-08-02 14:12:04 +02:00
private void logUnexpectedError ( final Exception e ) {
2005-06-28 13:27:31 +02:00
if ( e instanceof InterruptedException ) {
this . log . logInfo ( " Interruption detected " ) ;
} else {
2008-08-02 14:12:04 +02:00
final String errorMsg = e . getMessage ( ) ;
2005-06-29 11:50:48 +02:00
if ( errorMsg ! = null ) {
if ( errorMsg . startsWith ( " Socket closed " ) ) {
this . log . logInfo ( " httpd shutdown detected ... " ) ;
2005-06-29 12:05:38 +02:00
} else if ( ( errorMsg . startsWith ( " Broken pipe " ) | | errorMsg . startsWith ( " Connection reset " ) ) ) {
2005-06-29 11:50:48 +02:00
// client closed the connection, so we just end silently
this . log . logInfo ( " Client unexpectedly closed connection " ) ;
2006-10-19 13:10:56 +02:00
} else if ( errorMsg . equals ( " 400 Bad request " ) ) {
this . log . logInfo ( " Bad client request. " ) ;
2005-06-29 12:05:38 +02:00
} else {
2005-08-30 23:32:59 +02:00
this . log . logSevere ( " Unexpected Error. " + e . getClass ( ) . getName ( ) + " : " + e . getMessage ( ) , e ) ;
2005-06-29 11:50:48 +02:00
}
2005-06-28 13:27:31 +02:00
} else {
2005-08-30 23:32:59 +02:00
this . log . logSevere ( " Unexpected Error. " + e . getClass ( ) . getName ( ) , e ) ;
2005-06-28 13:27:31 +02:00
}
}
}
2008-08-02 14:12:04 +02:00
public Boolean HEAD ( final String arg ) {
2005-06-09 12:07:02 +02:00
try {
2005-09-20 23:49:47 +02:00
parseRequestLine ( httpHeader . METHOD_HEAD , arg ) ;
2005-06-09 12:07:02 +02:00
// we now know the HTTP version. depending on that, we read the header
httpHeader header ;
2008-08-02 14:12:04 +02:00
final String httpVersion = this . prop . getProperty ( httpHeader . CONNECTION_PROP_HTTP_VER , httpHeader . HTTP_VERSION_0_9 ) ;
2006-11-04 17:33:02 +01:00
if ( httpVersion . equals ( httpHeader . HTTP_VERSION_0_9 ) ) header = new httpHeader ( reverseMappingCache ) ;
2005-09-20 23:49:47 +02:00
else header = httpHeader . readHeader ( this . prop , this . session ) ;
2005-06-09 12:07:02 +02:00
// handle transparent proxy support
2005-11-02 18:56:26 +01:00
httpHeader . handleTransparentProxySupport ( header , this . prop , virtualHost , httpdProxyHandler . isTransparentProxy ) ;
2005-06-09 12:07:02 +02:00
// determines if the connection should be kept alive
2005-12-05 10:15:05 +01:00
handlePersistentConnection ( header ) ;
2005-06-09 12:07:02 +02:00
// return multi-line message
2006-11-04 17:33:02 +01:00
if ( this . prop . getProperty ( httpHeader . CONNECTION_PROP_HOST ) . equals ( virtualHost ) ) {
2005-06-09 12:07:02 +02:00
// pass to server
if ( allowServer ) {
if ( handleServerAuthentication ( header ) ) {
2007-08-09 23:58:38 +02:00
httpdFileHandler . doHead ( prop , header , this . session . out ) ;
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
} else {
// not authorized through firewall blocking (ip does not match filter)
session . out . write ( ( httpVersion + " 403 refused (IP not granted) " +
2007-12-14 20:17:54 +01:00
serverCore . CRLF_STRING ) . getBytes ( ) ) ;
2005-06-09 12:07:02 +02:00
return serverCore . TERMINATE_CONNECTION ;
}
} else {
// pass to proxy
2007-02-05 20:46:50 +01:00
if ( ( ( this . allowYaCyHop ) & & ( handleYaCyHopAuthentication ( header ) ) ) | |
( ( this . allowProxy ) & & ( handleProxyAuthentication ( header ) ) ) ) {
2007-08-09 23:58:38 +02:00
httpdProxyHandler . doHead ( prop , header , this . session . out ) ;
2005-06-09 12:07:02 +02:00
} else {
// not authorized through firewall blocking (ip does not match filter)
session . out . write ( ( httpVersion + " 403 refused (IP not granted) " +
2007-12-14 20:17:54 +01:00
serverCore . CRLF_STRING ) . getBytes ( ) ) ;
2005-06-09 12:07:02 +02:00
return serverCore . TERMINATE_CONNECTION ;
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
}
2005-09-20 23:49:47 +02:00
return this . prop . getProperty ( httpHeader . CONNECTION_PROP_PERSISTENT ) . equals ( " keep-alive " ) ? serverCore . RESUME_CONNECTION : serverCore . TERMINATE_CONNECTION ;
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) {
2005-06-28 13:27:31 +02:00
logUnexpectedError ( e ) ;
2005-06-09 12:07:02 +02:00
return serverCore . TERMINATE_CONNECTION ;
2005-09-30 18:02:58 +02:00
} finally {
this . doUserAccounting ( this . prop ) ;
}
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
2008-08-02 14:12:04 +02:00
public Boolean POST ( final String arg ) {
2005-06-09 12:07:02 +02:00
try {
2005-09-20 23:49:47 +02:00
parseRequestLine ( httpHeader . METHOD_POST , arg ) ;
2005-06-09 12:07:02 +02:00
// we now know the HTTP version. depending on that, we read the header
httpHeader header ;
2008-08-02 14:12:04 +02:00
final String httpVersion = this . prop . getProperty ( httpHeader . CONNECTION_PROP_HTTP_VER , httpHeader . HTTP_VERSION_0_9 ) ;
2006-11-04 17:33:02 +01:00
if ( httpVersion . equals ( httpHeader . HTTP_VERSION_0_9 ) ) header = new httpHeader ( reverseMappingCache ) ;
2005-09-20 23:49:47 +02:00
else header = httpHeader . readHeader ( this . prop , this . session ) ;
2005-06-09 12:07:02 +02:00
2008-05-04 12:53:04 +02:00
// handle transfer-coding
final InputStream sessionIn ;
final String transferEncoding = header . get ( httpHeader . TRANSFER_ENCODING ) ;
if ( transferEncoding ! = null ) {
if ( ! httpHeader . HTTP_VERSION_1_1 . equals ( httpVersion ) ) {
this . log . logWarning ( " client " + session . getName ( ) + " uses transfer-coding with HTTP version " + httpVersion + " ! " ) ;
}
if ( " chunked " . equalsIgnoreCase ( header . get ( httpHeader . TRANSFER_ENCODING ) ) ) {
sessionIn = new ChunkedInputStream ( this . session . in ) ;
} else {
// "A server which receives an entity-body with a transfer-coding it does
// not understand SHOULD return 501 (Unimplemented), and close the
// connection." [RFC 2616, section 3.6]
session . out . write ( ( httpVersion + " 501 transfer-encoding not implemented " + serverCore . CRLF_STRING + serverCore . CRLF_STRING + " you send a transfer-encoding to this server, which is not supported: " + transferEncoding + serverCore . CRLF_STRING ) . getBytes ( ) ) ;
return serverCore . TERMINATE_CONNECTION ;
}
} else {
sessionIn = this . session . in ;
}
2005-06-09 12:07:02 +02:00
// handle transparent proxy support
2005-11-02 18:56:26 +01:00
httpHeader . handleTransparentProxySupport ( header , this . prop , virtualHost , httpdProxyHandler . isTransparentProxy ) ;
2005-06-09 12:07:02 +02:00
// determines if the connection should be kept alive
2005-12-05 10:15:05 +01:00
handlePersistentConnection ( header ) ;
2005-06-09 12:07:02 +02:00
// return multi-line message
2006-11-04 17:33:02 +01:00
if ( prop . getProperty ( httpHeader . CONNECTION_PROP_HOST ) . equals ( virtualHost ) ) {
2005-06-09 12:07:02 +02:00
// pass to server
if ( allowServer ) {
2007-09-05 11:01:35 +02:00
if ( handleServerAuthentication ( header ) ) {
2008-05-04 12:53:04 +02:00
httpdFileHandler . doPost ( prop , header , this . session . out , sessionIn ) ;
2005-05-17 10:25:04 +02:00
}
2005-04-07 21:19:42 +02:00
} else {
2005-06-09 12:07:02 +02:00
// not authorized through firewall blocking (ip does not match filter)
2007-12-14 20:17:54 +01:00
session . out . write ( ( httpVersion + " 403 refused (IP not granted) " + serverCore . CRLF_STRING + serverCore . CRLF_STRING + " you are not allowed to connect to this server, because you are using the non-granted IP " + clientIP + " . allowed are only connections that match with the following filter: " + switchboard . getConfig ( " serverClient " , " * " ) + serverCore . CRLF_STRING ) . getBytes ( ) ) ;
2005-06-09 12:07:02 +02:00
return serverCore . TERMINATE_CONNECTION ;
}
} else {
// pass to proxy
2007-02-05 20:46:50 +01:00
if ( ( ( this . allowYaCyHop ) & & ( handleYaCyHopAuthentication ( header ) ) ) | |
( ( this . allowProxy ) & & ( handleProxyAuthentication ( header ) ) ) ) {
2008-05-04 12:53:04 +02:00
httpdProxyHandler . doPost ( prop , header , this . session . out , sessionIn ) ;
2005-06-09 12:07:02 +02:00
} else {
// not authorized through firewall blocking (ip does not match filter)
2007-12-14 20:17:54 +01:00
session . out . write ( ( httpVersion + " 403 refused (IP not granted) " + serverCore . CRLF_STRING + serverCore . CRLF_STRING + " you are not allowed to connect to this proxy, because you are using the non-granted IP " + clientIP + " . allowed are only connections that match with the following filter: " + switchboard . getConfig ( " proxyClient " , " * " ) + serverCore . CRLF_STRING ) . getBytes ( ) ) ;
2005-06-09 12:07:02 +02:00
return serverCore . TERMINATE_CONNECTION ;
2005-04-07 21:19:42 +02:00
}
2005-05-17 10:25:04 +02:00
}
2005-06-09 12:07:02 +02:00
//return serverCore.RESUME_CONNECTION;
2005-09-20 23:49:47 +02:00
return this . prop . getProperty ( httpHeader . CONNECTION_PROP_PERSISTENT ) . equals ( " keep-alive " ) ? serverCore . RESUME_CONNECTION : serverCore . TERMINATE_CONNECTION ;
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) {
2005-06-28 13:27:31 +02:00
logUnexpectedError ( e ) ;
2005-06-09 12:07:02 +02:00
return serverCore . TERMINATE_CONNECTION ;
2005-09-30 18:02:58 +02:00
} finally {
this . doUserAccounting ( this . prop ) ;
}
2005-06-09 12:07:02 +02:00
}
public Boolean CONNECT ( String arg ) throws IOException {
// establish a ssh-tunneled http connection
2005-09-08 16:48:32 +02:00
// this is to support https
2005-06-09 12:07:02 +02:00
// parse HTTP version
int pos = arg . indexOf ( " " ) ;
String httpVersion = " HTTP/1.0 " ;
if ( pos > = 0 ) {
httpVersion = arg . substring ( pos + 1 ) ;
arg = arg . substring ( 0 , pos ) ;
}
2005-09-20 23:49:47 +02:00
prop . setProperty ( httpHeader . CONNECTION_PROP_HTTP_VER , httpVersion ) ;
2005-06-09 12:07:02 +02:00
2005-09-08 16:48:32 +02:00
// parse hostname and port
2005-09-20 23:49:47 +02:00
prop . setProperty ( httpHeader . CONNECTION_PROP_HOST , arg ) ;
2005-06-09 12:07:02 +02:00
pos = arg . indexOf ( " : " ) ;
int port = 443 ;
if ( pos > = 0 ) {
port = Integer . parseInt ( arg . substring ( pos + 1 ) ) ;
2008-08-02 15:57:00 +02:00
//the offcut: arg = arg.substring(0, pos);
2005-09-08 16:48:32 +02:00
}
2005-06-09 12:07:02 +02:00
2005-09-08 16:48:32 +02:00
// setting other connection properties
2005-09-20 23:49:47 +02:00
prop . setProperty ( httpHeader . CONNECTION_PROP_CLIENTIP , this . clientIP ) ;
prop . setProperty ( httpHeader . CONNECTION_PROP_METHOD , httpHeader . METHOD_CONNECT ) ;
prop . setProperty ( httpHeader . CONNECTION_PROP_PATH , " / " ) ;
prop . setProperty ( httpHeader . CONNECTION_PROP_EXT , " " ) ;
prop . setProperty ( httpHeader . CONNECTION_PROP_URL , " " ) ;
2005-06-09 12:07:02 +02:00
// parse remaining lines
2008-08-02 14:12:04 +02:00
final httpHeader header = httpHeader . readHeader ( this . prop , this . session ) ;
2005-09-08 16:48:32 +02:00
if ( ! ( allowProxy ) ) {
// not authorized through firewall blocking (ip does not match filter)
2007-12-14 20:17:54 +01:00
session . out . write ( ( httpVersion + " 403 refused (IP not granted) " + serverCore . CRLF_STRING + serverCore . CRLF_STRING + " you are not allowed to connect to this proxy, because you are using the non-granted IP " + clientIP + " . allowed are only connections that match with the following filter: " + switchboard . getConfig ( " proxyClient " , " * " ) + serverCore . CRLF_STRING ) . getBytes ( ) ) ;
2005-09-08 16:48:32 +02:00
return serverCore . TERMINATE_CONNECTION ;
}
2005-06-09 12:07:02 +02:00
2005-10-14 14:51:56 +02:00
if ( port ! = 443 & & switchboard . getConfig ( " secureHttps " , " true " ) . equals ( " true " ) ) {
2005-06-09 12:07:02 +02:00
// security: connection only to ssl port
// we send a 403 (forbidden) error back
session . out . write ( ( httpVersion + " 403 Connection to non-443 forbidden " +
2007-12-14 20:17:54 +01:00
serverCore . CRLF_STRING + serverCore . CRLF_STRING ) . getBytes ( ) ) ;
2005-05-17 10:25:04 +02:00
return serverCore . TERMINATE_CONNECTION ;
}
2005-06-09 12:07:02 +02:00
2005-05-17 10:25:04 +02:00
// pass to proxy
2007-02-05 20:46:50 +01:00
if ( ( ( this . allowYaCyHop ) & & ( handleYaCyHopAuthentication ( header ) ) ) | |
2007-01-31 01:09:51 +01:00
( ( this . allowProxy ) & & ( this . handleProxyAuthentication ( header ) ) ) ) {
2007-08-09 23:58:38 +02:00
httpdProxyHandler . doConnect ( prop , header , this . session . in , this . session . out ) ;
2005-05-17 10:25:04 +02:00
} else {
// not authorized through firewall blocking (ip does not match filter)
2007-12-14 20:17:54 +01:00
session . out . write ( ( httpVersion + " 403 refused (IP not granted) " + serverCore . CRLF_STRING + serverCore . CRLF_STRING + " you are not allowed to connect to this proxy, because you are using the non-granted IP " + clientIP + " . allowed are only connections that match with the following filter: " + switchboard . getConfig ( " proxyClient " , " * " ) + serverCore . CRLF_STRING ) . getBytes ( ) ) ;
2005-05-17 10:25:04 +02:00
}
2005-06-09 12:07:02 +02:00
return serverCore . TERMINATE_CONNECTION ;
2005-05-17 10:25:04 +02:00
}
2005-04-07 21:19:42 +02:00
2008-08-02 14:12:04 +02:00
private final void parseRequestLine ( final String cmd , final String s ) {
2005-04-19 08:55:57 +02:00
2005-09-20 23:49:47 +02:00
// parsing the header
httpHeader . parseRequestLine ( cmd , s , this . prop , virtualHost ) ;
2005-09-05 10:01:54 +02:00
2007-06-11 16:05:20 +02:00
// track the request
2008-08-02 14:12:04 +02:00
final String path = this . prop . getProperty ( httpHeader . CONNECTION_PROP_URL ) ;
final String args = this . prop . getProperty ( httpHeader . CONNECTION_PROP_ARGS , " " ) ;
2007-06-11 16:05:20 +02:00
switchboard . track ( this . userAddress . getHostName ( ) , ( args . length ( ) > 0 ) ? path + " ? " + args : path ) ;
2005-09-20 23:49:47 +02:00
// reseting the empty request counter
2005-06-09 12:07:02 +02:00
this . emptyRequestCount = 0 ;
2008-05-17 01:33:59 +02:00
// counting the amount of received requests within this permanent connection
2005-09-20 23:49:47 +02:00
this . prop . setProperty ( httpHeader . CONNECTION_PROP_KEEP_ALIVE_COUNT , Integer . toString ( + + this . keepAliveRequestCount ) ) ;
2005-06-09 12:07:02 +02:00
2005-09-20 23:49:47 +02:00
// setting the client-IP
this . prop . setProperty ( httpHeader . CONNECTION_PROP_CLIENTIP , this . clientIP ) ;
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
2005-04-07 21:19:42 +02:00
// some static methods that needs to be used from any CGI
// and also by the httpdFileHandler
// but this belongs to the protocol handler, this class.
2005-06-09 12:07:02 +02:00
2008-08-02 14:12:04 +02:00
public static int parseArgs ( final serverObjects args , final InputStream in , final int length ) throws IOException {
2005-06-09 12:07:02 +02:00
// this is a quick hack using a previously coded parseMultipart based on a buffer
// should be replaced sometime by a 'right' implementation
2005-09-22 12:30:55 +02:00
byte [ ] buffer = null ;
// parsing post request bodies with a given length
if ( length ! = - 1 ) {
buffer = new byte [ length ] ;
2008-08-06 21:43:12 +02:00
int bytesRead = in . read ( buffer ) ;
assert bytesRead = = buffer . length ;
2005-09-22 12:30:55 +02:00
// parsing post request bodies which are gzip content-encoded
} else {
ByteArrayOutputStream bout = new ByteArrayOutputStream ( ) ;
serverFileUtils . copy ( in , bout ) ;
buffer = bout . toByteArray ( ) ;
bout . close ( ) ; bout = null ;
}
2008-08-02 14:12:04 +02:00
final int argc = parseArgs ( args , new String ( buffer , " UTF-8 " ) ) ;
2005-06-09 12:07:02 +02:00
buffer = null ;
return argc ;
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
2008-08-02 14:12:04 +02:00
public static int parseArgs ( final serverObjects args , String argsString ) {
2005-06-09 12:07:02 +02:00
// this parses a arg string that can either be attached to a URL query
// or can be given as result of a post method
// the String argsString is supposed to be constructed as
// <key1>=<value1>'&'<key2>=<value2>'&'<key3>=<value3>
// the calling function must strip off a possible leading '?' char
if ( argsString . length ( ) = = 0 ) return 0 ;
argsString = argsString + " & " ; // for technical reasons
int sep ;
int eqp ;
int argc = 0 ;
// Textfield1=default+value+Textfield+1&Textfield2=default+value+Textfield+2&selection1=sel1&selection2=othervalue1&selection2=sel2&selection3=sel3&Menu1=SubEnry11&radio1=button1&check1=button2&check1=button3&hidden1=&sButton1=enter+%281%29
while ( argsString . length ( ) > 0 ) {
eqp = argsString . indexOf ( " = " ) ;
sep = argsString . indexOf ( " & " ) ;
if ( ( eqp < = 0 ) | | ( sep < = 0 ) ) break ;
// resulting equations are inserted into the property args with leading '&'
2007-10-24 23:38:19 +02:00
args . put ( parseArg ( argsString . substring ( 0 , eqp ) ) , parseArg ( argsString . substring ( eqp + 1 , sep ) ) ) ;
2005-06-09 12:07:02 +02:00
argsString = argsString . substring ( sep + 1 ) ;
argc + + ;
}
// we return the number of parsed arguments
return argc ;
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
2007-02-02 22:20:53 +01:00
/ * *
* < p > This method basically does the same as { @link URLDecoder # decode ( String , String ) URLDecoder . decode ( s , " UTF-8 " ) }
* would do with the exception of more lazyness in regard to current browser implementations as they do not
* always comply with the standards . < / p >
* < p > The following replacements are performed on the input - < code > String < / code > : < / p >
* < ul >
* < li > ' < code > + < / code > ' - characters are replaced by space
* < li > ( supbsequent ( in the case of encoded unicode - chars ) ) ' < code > % HH < / code > ' - entities are replaced by their
* respective < code > char < / code > - representation < / li >
* < li > ' < code > % uHHHH < / code > ' - entities ( sent by IE although rejected by the W3C ) are replaced by their respective
* < code > char < / code > - representation < / li >
* < li > < strong > TODO < / strong > : < code > chars < / code > already encoded in UTF - 8 are url - encoded and re - decoded due to internal restrictions ,
* which slows down this method unnecessarily < / li >
* < / ul >
*
* @param s the URL - encoded < code > String < / code > to decode , note that the encoding used to URL - encode the original
* < code > String < / code > has to be UTF - 8 ( i . e . the " <code>accept-charset</code> " - property of HTML
* < code > & lt ; form & gt ; < / code > - elements )
* @return the " normal " Java - < code > String < / code > ( UTF - 8 ) represented by the input or < code > null < / code >
* if the passed argument < code > encoding < / code > is not supported
* /
2005-04-07 21:19:42 +02:00
private static String parseArg ( String s ) {
2005-06-09 12:07:02 +02:00
int pos = 0 ;
2008-08-02 14:12:04 +02:00
final ByteArrayOutputStream baos = new ByteArrayOutputStream ( s . length ( ) ) ;
2007-02-02 22:20:53 +01:00
2005-06-09 12:07:02 +02:00
while ( pos < s . length ( ) ) {
if ( s . charAt ( pos ) = = '+' ) {
2006-01-12 22:14:39 +01:00
baos . write ( ' ' ) ;
2005-04-20 09:39:40 +02:00
pos + + ;
2005-06-09 12:07:02 +02:00
} else if ( s . charAt ( pos ) = = '%' ) {
2007-02-02 22:20:53 +01:00
try {
if ( s . length ( ) > = pos + 6 & & ( s . charAt ( pos + 1 ) = = 'u' | | s . charAt ( pos + 1 ) = = 'U' ) ) {
// non-standard encoding of IE for unicode-chars
2008-08-02 14:12:04 +02:00
final int bh = Integer . parseInt ( s . substring ( pos + 2 , pos + 4 ) , 16 ) ;
final int bl = Integer . parseInt ( s . substring ( pos + 4 , pos + 6 ) , 16 ) ;
2007-02-02 22:20:53 +01:00
// TODO: needs conversion from UTF-16 to UTF-8
baos . write ( bh ) ;
baos . write ( bl ) ;
pos + = 6 ;
} else if ( s . length ( ) > = pos + 3 ) {
baos . write ( Integer . parseInt ( s . substring ( pos + 1 , pos + 3 ) , 16 ) ) ;
pos + = 3 ;
} else {
baos . write ( s . charAt ( pos + + ) ) ;
}
2008-08-02 14:12:04 +02:00
} catch ( final NumberFormatException e ) {
2007-02-02 22:20:53 +01:00
baos . write ( s . charAt ( pos + + ) ) ;
}
} else if ( s . charAt ( pos ) > 127 ) {
// Unicode chars sent by client, see http://www.w3.org/International/O-URL-code.html
try {
// don't write anything but url-encode the unicode char
s = s . substring ( 0 , pos ) + URLEncoder . encode ( s . substring ( pos , pos + 1 ) , " UTF-8 " ) + s . substring ( pos + 1 ) ;
2008-08-02 14:12:04 +02:00
} catch ( final UnsupportedEncodingException e ) { return null ; }
2005-06-09 12:07:02 +02:00
} else {
2006-01-12 22:14:39 +01:00
baos . write ( s . charAt ( pos + + ) ) ;
2005-06-09 12:07:02 +02:00
}
}
2007-02-02 22:20:53 +01:00
try {
return new String ( baos . toByteArray ( ) , " UTF-8 " ) ;
2008-08-02 14:12:04 +02:00
} catch ( final UnsupportedEncodingException e ) { return null ; }
2007-02-02 22:20:53 +01:00
}
2007-01-06 12:05:50 +01:00
// 06.01.2007: decode HTML entities by [FB]
public static String decodeHtmlEntities ( String s ) {
// replace all entities defined in wikiCode.characters and htmlentities
2007-07-19 17:32:10 +02:00
s = htmlTools . decodeHtml2Unicode ( s ) ;
2007-01-06 12:05:50 +01:00
// replace all other
2008-08-02 14:12:04 +02:00
final CharArrayWriter b = new CharArrayWriter ( s . length ( ) ) ;
2007-01-06 12:05:50 +01:00
int end ;
for ( int i = 0 ; i < s . length ( ) ; i + + ) {
if ( s . charAt ( i ) = = '&' & & ( end = s . indexOf ( ';' , i + 1 ) ) > i ) {
if ( s . charAt ( i + 1 ) = = '#' ) { // Ӓ symbols
b . write ( Integer . parseInt ( s . substring ( i + 2 , end ) ) ) ;
i + = end - i ;
} else { // 'named' smybols
2008-08-08 03:40:28 +02:00
log . logFine ( " discovered yet unimplemented HTML entity ' " + s . substring ( i , end + 1 ) + " ' " ) ;
2007-01-06 12:05:50 +01:00
b . write ( s . charAt ( i ) ) ;
}
} else {
b . write ( s . charAt ( i ) ) ;
}
}
return b . toString ( ) ;
}
2005-06-09 12:07:02 +02:00
2008-08-08 03:40:28 +02:00
/ * *
* parses the message accordingly to RFC 1867 using " Commons FileUpload " ( http : //commons.apache.org/fileupload/)
*
* @author danielr
* @since 07 . 08 . 2008
2008-08-10 00:44:17 +02:00
* @param header
* hier muss ARGC gesetzt werden !
2008-08-08 03:40:28 +02:00
* @param args
* @param in
* @param length
* @return
* @throws IOException
* /
@SuppressWarnings ( " unchecked " )
2008-08-10 00:44:17 +02:00
public static HashMap < String , byte [ ] > parseMultipart ( final httpHeader header , final serverObjects args , final InputStream in , final int length )
throws IOException {
// read all data from network in memory
byte [ ] buffer = serverFileUtils . read ( in ) ;
// parse data in memory
RequestContext request = new yacyContextRequest ( header , new ByteArrayInputStream ( buffer ) ) ;
// check information
if ( ! FileUploadBase . isMultipartContent ( request ) ) {
throw new IOException ( " the request is not a multipart-message! " ) ;
}
// format information for further usage
FileItemFactory factory = new DiskFileItemFactory ( ) ;
FileUpload upload = new FileUpload ( factory ) ;
List < FileItem > items ;
try {
items = upload . parseRequest ( request ) ;
} catch ( FileUploadException e ) {
throw new IOException ( " FileUploadException " + e . getMessage ( ) ) ;
}
final HashMap < String , byte [ ] > files = new HashMap < String , byte [ ] > ( ) ;
int formFieldCount = 0 ;
for ( FileItem item : items ) {
if ( item . isFormField ( ) ) {
// simple text
if ( item . getContentType ( ) = = null | | ! item . getContentType ( ) . contains ( " charset " ) ) {
// old yacy clients use their local default charset, on most systems UTF-8 (I hope ;)
args . put ( item . getFieldName ( ) , item . getString ( " UTF-8 " ) ) ;
} else {
// use default encoding (given as header or ISO-8859-1)
args . put ( item . getFieldName ( ) , item . getString ( ) ) ;
}
formFieldCount + + ;
} else {
// file
args . put ( item . getFieldName ( ) , item . getName ( ) ) ;
final byte [ ] fileContent = serverFileUtils . read ( item . getInputStream ( ) ) ;
item . getInputStream ( ) . close ( ) ;
files . put ( item . getFieldName ( ) , fileContent ) ;
}
}
header . put ( " ARGC " , String . valueOf ( items . size ( ) ) ) ; // store argument count
return files ;
2005-04-07 21:19:42 +02:00
}
2008-08-08 03:40:28 +02:00
// // FIXME this is a quick hack using a previously coded parseMultipart based on a buffer
// // should be replaced sometime by a 'right' implementation
//
// byte[] buffer = null;
//
// // parsing post request bodies with a given length
// if (length != -1) {
// buffer = new byte[length];
// int c, a = 0;
// while (a < length) {
// c = in.read(buffer, a, length - a);
// if (c <= 0) break;
// a += c;
// }
// // parsing post request bodies which are gzip content-encoded
// } else {
// serverByteBuffer bout = new serverByteBuffer();
// serverFileUtils.copy(in,bout);
// buffer = bout.getBytes();
// bout.close(); bout = null;
// }
//
// //System.out.println("MULTIPART-BUFFER=" + new String(buffer));
// final HashMap<String, byte[]> files = parseMultipart(header, args, buffer);
// buffer = null;
// return files;
// }
//
// public static HashMap<String, byte[]> parseMultipart(final httpHeader header, final serverObjects args, final byte[] buffer) throws IOException {
// // we parse a multipart message and put results into the properties
// // find/identify boundary marker
// //System.out.println("DEBUG parseMultipart = <<" + new String(buffer) + ">>");
// final String s = header.get(httpHeader.CONTENT_TYPE);
// if (s == null) return null;
// int q;
// int p = s.toLowerCase().indexOf("boundary=");
// if (p < 0) throw new IOException("boundary marker in multipart not found");
// // boundaries start with additional leading "--", see RFC1867
// final byte[] boundary = ("--" + s.substring(p + 9)).getBytes();
//
// // eat up first boundary
// // the buffer must start with a boundary
// byte[] line = readLine(0, buffer);
// int pos = nextPos;
// if ((line == null) || (!(equals(line, 0, boundary, 0, boundary.length))))
// throw new IOException("boundary not recognized: " + ((line == null) ? "NULL" : new String(line, "UTF-8")) + ", boundary = " + new String(boundary));
//
// // we need some constants
// final byte[] namec = "name=".getBytes();
// final byte[] filenamec = "filename=".getBytes();
// //byte[] semicolonc = (new String(";")).getBytes();
// final byte[] quotec = new byte[] {(byte) '"'};
//
// // now loop over boundaries
// byte [] name;
// byte [] filename;
// final HashMap<String, byte[]> files = new HashMap<String, byte[]>();
// int argc = 0;
// //System.out.println("DEBUG: parsing multipart body:" + new String(buffer));
// while (pos < buffer.length) { // boundary enumerator
// // here the 'pos' marker points to the first line in a section after a boundary line
// line = readLine(pos, buffer); pos = nextPos;
// // termination if line is empty
// if (line.length == 0) break;
// // find name tag in line
// p = indexOf(0, line, namec);
// if (p < 0) throw new IOException("tag name in marker section not found: '" + new String(line, "UTF-8") + "'"); // a name tag must always occur
// p += namec.length + 1; // first position of name value
// q = indexOf(p, line, quotec);
// if (q < 0) throw new IOException("missing quote in name tag: '" + new String(line, "UTF-8") + "'");
// name = new byte[q - p];
// java.lang.System.arraycopy(line, p, name, 0, q - p);
// // if this line has also a filename attribute, read it
// p = indexOf(q, line, filenamec);
// if (p > 0) {
// p += filenamec.length + 1; // first position of name value
// q = indexOf(p, line, quotec);
// if (q < 0) {
// log.logWarning("quote of filename tag not found, searching in next line");
// // append next line to this
// final byte[] nextline = readLine(pos, buffer); pos = nextPos;
// final byte[] holeLine = new byte[line.length + nextline.length];
// System.arraycopy(line, 0, holeLine, 0, line.length);
// System.arraycopy(nextline, 0, holeLine, line.length, nextline.length);
// p = indexOf(q, holeLine, quotec);
// if(p > 0)
// throw new IOException("missing quote in filename tag: '" + new String(line) + "'");
// }
// filename = new byte[q - p];
// java.lang.System.arraycopy(line, p, filename, 0, q - p);
// } else filename = null;
// // we have what we need. more information lines may follow, but we omit parsing them
// // we just skip until an empty line is reached
// while (pos < buffer.length) { // line skiping
// line = readLine(pos, buffer); pos = nextPos;
// if ((line == null) || (line.length == 0)) break;
// }
// // depending on the filename tag exsistence, read now either a value for the name
// // or a complete uploaded file
// // to know the exact length of the value, we must identify the next boundary
// p = indexOf(pos, buffer, boundary);
//
// // if we can't find another boundary, then this is an error in the input
// if (p < 0) {
// log.logSevere("ERROR in PUT body: no ending boundary. probably missing values");
// break;
// }
//
// // we don't know if the value is terminated by LF, CR or CRLF
// // (it's suppose to be CRLF, but we want to be lazy about wrong terminations)
// if (buffer[p - 2] == serverCore.CR) // ERROR: IndexOutOfBounds: -2
// /* CRLF */ q = p - 2;
// else
// /* CR or LF only */ q = p - 1;
// // the above line is wrong if we uploaded a file that has a CR as it's last byte
// // and the client's line termination symbol is only a CR or LF (which would be incorrect)
// // the value is between 'pos' and 'q', while the next marker is 'p'
// line = new byte[q - pos];
// java.lang.System.arraycopy(buffer, pos, line, 0, q - pos);
// // in the 'line' variable we have now either a normal value or an uploadef file
// if (filename == null) {
// args.put(new String(name, "UTF-8"), new String(line, "UTF-8"));
// } else {
// // we store the file in a hashtable.
// // we use the same key to address the file in the hashtable as we
// // use to address the filename in the properties, but without leading '&'
// args.put(new String(name, "UTF-8"), new String(filename, "UTF-8"));
// files.put(new String(name, "UTF-8"), line);
// }
// argc++;
// // finally, read the next boundary line
// line = readLine(p, buffer);
// pos = nextPos;
// }
// header.put("ARGC", Integer.toString(argc)); // store argument count
// return files;
// }
2005-04-07 21:19:42 +02:00
2005-06-09 12:07:02 +02:00
/ *
- - - - - - - - - - - - 1090358578442
Content - Disposition : form - data ; name = " youare "
Ty2F86ekSWM5
- - - - - - - - - - - - 1090358578442
Content - Disposition : form - data ; name = " key "
6EkPPOl7
- - - - - - - - - - - - 1090358578442
Content - Disposition : form - data ; name = " iam "
HnTvzwV7SCJR
- - - - - - - - - - - - 1090358578442
Content - Disposition : form - data ; name = " process "
permission
- - - - - - - - - - - - 1090358578442
* /
2008-08-08 03:40:28 +02:00
/ * *
* wraps the request into a org . apache . commons . fileupload . RequestContext
*
* @author danielr
* @since 07 . 08 . 2008
* /
private static class yacyContextRequest implements RequestContext {
private final httpHeader header ;
private final InputStream inStream ;
/ * *
* creates a new yacyContextRequest
*
* @param header
* @param in
* /
public yacyContextRequest ( httpHeader header , InputStream in ) {
this . header = header ;
this . inStream = in ;
}
/ *
* ( non - Javadoc )
*
* @see
* org . apache . commons . fileupload . RequestContext # getCharacterEncoding ( )
* /
@Override
public String getCharacterEncoding ( ) {
return header . getCharacterEncoding ( ) ;
}
/ *
* ( non - Javadoc )
*
* @see org . apache . commons . fileupload . RequestContext # getContentLength ( )
* /
@Override
public int getContentLength ( ) {
return ( int ) header . contentLength ( ) ;
}
/ *
* ( non - Javadoc )
*
* @see org . apache . commons . fileupload . RequestContext # getContentType ( )
* /
@Override
public String getContentType ( ) {
return header . get ( httpHeader . CONTENT_TYPE ) ;
}
/ *
* ( non - Javadoc )
*
* @see org . apache . commons . fileupload . RequestContext # getInputStream ( )
* /
@Override
public InputStream getInputStream ( ) throws IOException {
return inStream ;
}
}
static int nextPos = - 1 ;
2008-08-02 14:12:04 +02:00
private static byte [ ] readLine ( final int start , final byte [ ] array ) {
2005-06-09 12:07:02 +02:00
// read a string from an array; line ending is always CRLF
// but we are also fuzzy with that: may also be only CR or LF
2007-12-14 20:17:54 +01:00
// if no remaining CR, CRLF or LF can be found, return null
2005-06-09 12:07:02 +02:00
if ( start > array . length ) return null ;
2007-12-14 20:17:54 +01:00
int pos = indexOf ( start , array , serverCore . CRLF ) ; nextPos = pos + 2 ;
if ( pos < 0 ) { pos = indexOf ( start , array , new byte [ ] { serverCore . CR } ) ; nextPos = pos + 1 ; }
if ( pos < 0 ) { pos = indexOf ( start , array , new byte [ ] { serverCore . LF } ) ; nextPos = pos + 1 ; }
2005-06-09 12:07:02 +02:00
if ( pos < 0 ) { nextPos = start ; return null ; }
2008-08-02 14:12:04 +02:00
final byte [ ] result = new byte [ pos - start ] ;
2005-06-09 12:07:02 +02:00
java . lang . System . arraycopy ( array , start , result , 0 , pos - start ) ;
return result ;
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
2008-08-02 14:12:04 +02:00
public static int indexOf ( final int start , final byte [ ] array , final byte [ ] pattern ) {
2005-06-09 12:07:02 +02:00
// return a position of a pattern in an array
if ( start > array . length - pattern . length ) return - 1 ;
if ( pattern . length = = 0 ) return start ;
for ( int pos = start ; pos < = array . length - pattern . length ; pos + + )
if ( ( array [ pos ] = = pattern [ 0 ] ) & & ( equals ( array , pos , pattern , 0 , pattern . length ) ) )
return pos ;
return - 1 ;
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
2008-08-02 14:12:04 +02:00
public static boolean equals ( final byte [ ] a , final int aoff , final byte [ ] b , final int boff , final int len ) {
2005-06-09 12:07:02 +02:00
//System.out.println("equals: a = " + new String(a) + ", aoff = " + aoff + ", b = " + new String(b) + ", boff = " + boff + ", length = " + len);
if ( ( aoff + len > a . length ) | | ( boff + len > b . length ) ) return false ;
for ( int i = 0 ; i < len ; i + + ) if ( a [ aoff + i ] ! = b [ boff + i ] ) return false ;
//System.out.println("TRUE!");
return true ;
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
2008-03-10 00:48:24 +01:00
public httpd clone ( ) {
2007-08-09 23:58:38 +02:00
return new httpd ( switchboard ) ;
2005-04-07 21:19:42 +02:00
}
2005-06-09 12:07:02 +02:00
public static final void sendRespondBody (
2008-08-02 14:12:04 +02:00
final OutputStream respond ,
final byte [ ] body
2005-06-09 12:07:02 +02:00
) throws IOException {
respond . write ( body ) ;
respond . flush ( ) ;
}
2005-10-10 12:33:09 +02:00
2005-06-09 12:07:02 +02:00
public static final void sendRespondError (
2008-08-02 14:12:04 +02:00
final Properties conProp ,
final OutputStream respond ,
final int errorcase ,
final int httpStatusCode ,
final String httpStatusText ,
final String detailedErrorMsg ,
final Throwable stackTrace
2005-06-09 12:07:02 +02:00
) throws IOException {
2005-10-10 12:33:09 +02:00
sendRespondError (
conProp ,
respond ,
errorcase ,
httpStatusCode ,
2005-10-13 09:29:14 +02:00
httpStatusText ,
2005-10-10 12:33:09 +02:00
detailedErrorMsg ,
null ,
2005-10-13 09:29:14 +02:00
null ,
2006-06-12 14:12:21 +02:00
stackTrace ,
null
2005-10-10 12:33:09 +02:00
) ;
}
public static final void sendRespondError (
2008-08-02 14:12:04 +02:00
final Properties conProp ,
final OutputStream respond ,
final int httpStatusCode ,
final String httpStatusText ,
final File detailedErrorMsgFile ,
final serverObjects detailedErrorMsgValues ,
final Throwable stackTrace
2005-10-10 12:33:09 +02:00
) throws IOException {
sendRespondError (
conProp ,
respond ,
2005-10-13 09:29:14 +02:00
5 ,
2005-10-10 12:33:09 +02:00
httpStatusCode ,
httpStatusText ,
2005-10-13 09:29:14 +02:00
null ,
2005-10-10 12:33:09 +02:00
detailedErrorMsgFile ,
detailedErrorMsgValues ,
2006-06-12 14:12:21 +02:00
stackTrace ,
null
2005-10-10 12:33:09 +02:00
) ;
}
2006-06-12 14:12:21 +02:00
public static final void sendRespondError (
2008-08-02 14:12:04 +02:00
final Properties conProp ,
final OutputStream respond ,
final int errorcase ,
final int httpStatusCode ,
2005-10-10 12:33:09 +02:00
String httpStatusText ,
2008-08-02 14:12:04 +02:00
final String detailedErrorMsgText ,
final Object detailedErrorMsgFile ,
final serverObjects detailedErrorMsgValues ,
final Throwable stackTrace ,
2006-06-12 14:12:21 +02:00
httpHeader header
2005-10-10 12:33:09 +02:00
) throws IOException {
2005-06-09 12:07:02 +02:00
FileInputStream fis = null ;
2005-07-07 15:58:54 +02:00
ByteArrayOutputStream o = null ;
2005-06-09 12:07:02 +02:00
try {
// setting the proper http status message
2008-08-02 14:12:04 +02:00
final String httpVersion = conProp . getProperty ( httpHeader . CONNECTION_PROP_HTTP_VER , " HTTP/1.1 " ) ;
2005-06-09 12:07:02 +02:00
if ( ( httpStatusText = = null ) | | ( httpStatusText . length ( ) = = 0 ) ) {
if ( httpVersion . equals ( " HTTP/1.0 " ) & & httpHeader . http1_0 . containsKey ( Integer . toString ( httpStatusCode ) ) )
2008-03-05 14:29:42 +01:00
httpStatusText = httpHeader . http1_0 . get ( Integer . toString ( httpStatusCode ) ) ;
2005-06-09 12:07:02 +02:00
else if ( httpVersion . equals ( " HTTP/1.1 " ) & & httpHeader . http1_1 . containsKey ( Integer . toString ( httpStatusCode ) ) )
2008-03-05 14:29:42 +01:00
httpStatusText = httpHeader . http1_1 . get ( Integer . toString ( httpStatusCode ) ) ;
2005-06-09 12:07:02 +02:00
else httpStatusText = " Unknown " ;
}
// generating the desired request url
2005-09-20 23:49:47 +02:00
String host = conProp . getProperty ( httpHeader . CONNECTION_PROP_HOST ) ;
2008-08-02 14:12:04 +02:00
final String path = conProp . getProperty ( httpHeader . CONNECTION_PROP_PATH , " / " ) ;
final String args = conProp . getProperty ( httpHeader . CONNECTION_PROP_ARGS ) ;
final String method = conProp . getProperty ( httpHeader . CONNECTION_PROP_METHOD ) ;
2005-06-09 12:07:02 +02:00
2008-08-02 14:12:04 +02:00
int port = 80 ;
2008-08-02 15:57:00 +02:00
final int pos = host . indexOf ( " : " ) ;
2005-06-09 12:07:02 +02:00
if ( pos ! = - 1 ) {
port = Integer . parseInt ( host . substring ( pos + 1 ) ) ;
host = host . substring ( 0 , pos ) ;
}
String urlString ;
try {
2007-09-05 11:01:35 +02:00
urlString = ( new yacyURL ( ( method . equals ( httpHeader . METHOD_CONNECT ) ? " https " : " http " ) , host , port , ( args = = null ) ? path : path + " ? " + args ) ) . toString ( ) ;
2008-08-02 14:12:04 +02:00
} catch ( final MalformedURLException e ) {
2007-11-14 20:14:53 +01:00
urlString = " invalid URL " ;
}
2005-06-09 12:07:02 +02:00
// set rewrite values
2008-08-02 14:12:04 +02:00
final serverObjects tp = new serverObjects ( ) ;
2007-11-14 20:14:53 +01:00
2005-07-07 15:58:54 +02:00
// tp.put("host", serverCore.publicIP().getHostAddress());
2005-09-30 08:15:22 +02:00
// tp.put("port", switchboard.getConfig("port", "8080"));
2007-11-14 20:14:53 +01:00
2008-08-02 14:12:04 +02:00
final String clientIP = conProp . getProperty ( httpHeader . CONNECTION_PROP_CLIENTIP , " 127.0.0.1 " ) ;
2007-11-14 20:14:53 +01:00
2005-09-30 08:15:22 +02:00
// check if ip is local ip address
2008-08-02 14:12:04 +02:00
final InetAddress hostAddress = serverDomains . dnsResolve ( clientIP ) ;
2005-11-07 11:57:54 +01:00
if ( hostAddress = = null ) {
2007-07-24 02:46:17 +02:00
tp . put ( " host " , serverDomains . myPublicLocalIP ( ) . getHostAddress ( ) ) ;
2007-11-14 20:14:53 +01:00
tp . put ( " port " , serverCore . getPortNr ( switchboard . getConfig ( " port " , " 8080 " ) ) ) ;
2005-11-07 11:57:54 +01:00
} else if ( hostAddress . isSiteLocalAddress ( ) | | hostAddress . isLoopbackAddress ( ) ) {
2007-07-24 02:46:17 +02:00
tp . put ( " host " , serverDomains . myPublicLocalIP ( ) . getHostAddress ( ) ) ;
2006-02-17 11:53:20 +01:00
tp . put ( " port " , serverCore . getPortNr ( switchboard . getConfig ( " port " , " 8080 " ) ) ) ;
2005-11-07 11:57:54 +01:00
} else {
2007-07-24 02:46:17 +02:00
tp . put ( " host " , serverDomains . myPublicIP ( ) ) ;
2008-05-06 01:13:47 +02:00
tp . put ( " port " , Integer . toString ( serverCore . getPortNr ( switchboard . getConfig ( " port " , " 8080 " ) ) ) ) ;
2007-11-14 20:14:53 +01:00
}
// if peer has public address it will be used
2008-05-06 01:13:47 +02:00
if ( alternativeResolver ! = null ) {
tp . put ( " extAddress " , alternativeResolver . myIP ( ) + " : " + alternativeResolver . myPort ( ) ) ;
2007-11-14 20:14:53 +01:00
}
// otherwise the local ip address will be used
else {
tp . put ( " extAddress " , tp . get ( " host " , " 127.0.0.1 " ) + " : " + tp . get ( " port " , " 8080 " ) ) ;
}
2005-09-30 08:15:22 +02:00
2008-05-06 01:13:47 +02:00
tp . put ( " peerName " , ( alternativeResolver = = null ) ? " " : alternativeResolver . myName ( ) ) ;
2007-11-14 20:14:53 +01:00
tp . put ( " errorMessageType " , errorcase ) ;
2005-06-09 12:07:02 +02:00
tp . put ( " httpStatus " , Integer . toString ( httpStatusCode ) + " " + httpStatusText ) ;
2005-09-20 23:49:47 +02:00
tp . put ( " requestMethod " , conProp . getProperty ( httpHeader . CONNECTION_PROP_METHOD ) ) ;
2005-06-09 12:07:02 +02:00
tp . put ( " requestURL " , urlString ) ;
2007-11-14 20:14:53 +01:00
2005-10-13 09:29:14 +02:00
switch ( errorcase ) {
2007-01-06 12:05:50 +01:00
case ERRORCASE_FILE :
2008-01-06 20:23:38 +01:00
tp . put ( " errorMessageType_file " , ( detailedErrorMsgFile = = null ) ? " " : detailedErrorMsgFile . toString ( ) ) ;
2007-01-17 05:20:19 +01:00
if ( ( detailedErrorMsgValues ! = null ) & & ( detailedErrorMsgValues . size ( ) > 0 ) ) {
2005-10-10 12:33:09 +02:00
// rewriting the value-names and add the proper name prefix:
2008-08-02 14:12:04 +02:00
final Iterator < String > nameIter = detailedErrorMsgValues . keySet ( ) . iterator ( ) ;
2005-10-10 12:33:09 +02:00
while ( nameIter . hasNext ( ) ) {
2008-08-02 14:12:04 +02:00
final String name = nameIter . next ( ) ;
2007-01-17 05:20:19 +01:00
tp . put ( " errorMessageType_ " + name , detailedErrorMsgValues . get ( name ) ) ;
2005-10-10 12:33:09 +02:00
}
2005-10-13 09:29:14 +02:00
}
break ;
2008-05-04 12:53:04 +02:00
case ERRORCASE_MESSAGE :
2005-10-13 09:29:14 +02:00
default :
2008-05-04 12:53:04 +02:00
tp . put ( " errorMessageType_detailedErrorMsg " , ( detailedErrorMsgText = = null ) ? " " : detailedErrorMsgText . replaceAll ( " \ n " , " <br /> " ) ) ;
2005-10-13 09:29:14 +02:00
break ;
2005-10-10 12:33:09 +02:00
}
2005-06-09 12:07:02 +02:00
// building the stacktrace
2005-07-07 15:58:54 +02:00
if ( stackTrace ! = null ) {
tp . put ( " printStackTrace " , 1 ) ;
2008-08-02 14:12:04 +02:00
final serverByteBuffer errorMsg = new serverByteBuffer ( 100 ) ;
2005-06-09 12:07:02 +02:00
stackTrace . printStackTrace ( new PrintStream ( errorMsg ) ) ;
2007-01-17 05:20:19 +01:00
tp . put ( " printStackTrace_exception " , stackTrace . toString ( ) ) ;
tp . put ( " printStackTrace_stacktrace " , new String ( errorMsg . getBytes ( ) , " UTF-8 " ) ) ;
2005-06-09 12:07:02 +02:00
} else {
2007-01-17 05:20:19 +01:00
tp . put ( " printStackTrace " , 0 ) ;
2005-06-09 12:07:02 +02:00
}
2005-08-23 13:32:36 +02:00
// Generated Tue, 23 Aug 2005 11:19:14 GMT by brain.wg (squid/2.5.STABLE3)
// adding some system information
2008-08-02 14:12:04 +02:00
final String systemDate = HttpClient . dateString ( new Date ( ) ) ;
2007-01-17 05:20:19 +01:00
tp . put ( " date " , systemDate ) ;
2005-08-23 13:32:36 +02:00
2005-06-09 12:07:02 +02:00
// rewrite the file
2008-08-02 14:12:04 +02:00
final File htRootPath = new File ( switchboard . getRootPath ( ) , switchboard . getConfig ( " htRootPath " , " htroot " ) ) ;
2005-07-07 15:58:54 +02:00
httpTemplate . writeTemplate (
fis = new FileInputStream ( new File ( htRootPath , " /proxymsg/error.html " ) ) ,
o = new ByteArrayOutputStream ( ) ,
tp ,
" -UNRESOLVED_PATTERN- " . getBytes ( )
) ;
2008-08-02 14:12:04 +02:00
final byte [ ] result = o . toByteArray ( ) ;
2005-06-16 10:34:52 +02:00
o . close ( ) ; o = null ;
2005-06-09 12:07:02 +02:00
2006-06-12 14:12:21 +02:00
if ( header = = null )
2008-05-27 15:11:06 +02:00
header = new httpHeader ( ) ;
header . put ( httpHeader . CONNECTION_PROP_PROXY_RESPOND_STATUS , Integer . toString ( httpStatusCode ) ) ;
2005-08-23 13:32:36 +02:00
header . put ( httpHeader . DATE , systemDate ) ;
2005-06-09 12:07:02 +02:00
header . put ( httpHeader . CONTENT_TYPE , " text/html " ) ;
2005-08-03 04:02:39 +02:00
header . put ( httpHeader . CONTENT_LENGTH , Integer . toString ( result . length ) ) ;
2005-06-09 12:07:02 +02:00
header . put ( httpHeader . PRAGMA , " no-cache " ) ;
sendRespondHeader ( conProp , respond , httpVersion , httpStatusCode , httpStatusText , header ) ;
2006-02-16 12:45:32 +01:00
if ( ! method . equals ( httpHeader . METHOD_HEAD ) ) {
// write the array to the client
2008-03-14 01:16:04 +01:00
serverFileUtils . copy ( result , respond ) ;
2006-02-16 12:45:32 +01:00
}
2005-06-09 12:07:02 +02:00
respond . flush ( ) ;
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) {
2005-06-09 12:07:02 +02:00
throw new IOException ( e . getMessage ( ) ) ;
} finally {
2008-08-02 14:12:04 +02:00
if ( fis ! = null ) try { fis . close ( ) ; } catch ( final Exception e ) { }
if ( o ! = null ) try { o . close ( ) ; } catch ( final Exception e ) { }
2005-06-09 12:07:02 +02:00
}
}
public static final void sendRespondHeader (
2008-08-02 14:12:04 +02:00
final Properties conProp ,
final OutputStream respond ,
final String httpVersion ,
final int httpStatusCode ,
final String httpStatusText ,
final long contentLength
2005-06-09 12:07:02 +02:00
) throws IOException {
sendRespondHeader ( conProp , respond , httpVersion , httpStatusCode , httpStatusText , null , contentLength , null , null , null , null , null ) ;
}
public static final void sendRespondHeader (
2008-08-02 14:12:04 +02:00
final Properties conProp ,
final OutputStream respond ,
final String httpVersion ,
final int httpStatusCode ,
final String httpStatusText ,
final String contentType ,
final long contentLength ,
final Date moddate ,
final Date expires ,
final httpHeader headers ,
final String contentEnc ,
final String transferEnc
2005-06-09 12:07:02 +02:00
) throws IOException {
2006-01-13 23:50:04 +01:00
sendRespondHeader ( conProp , respond , httpVersion , httpStatusCode , httpStatusText , contentType , contentLength , moddate , expires , headers , contentEnc , transferEnc , true ) ;
2005-12-07 14:26:27 +01:00
}
public static final void sendRespondHeader (
2008-08-02 14:12:04 +02:00
final Properties conProp ,
final OutputStream respond ,
final String httpVersion ,
final int httpStatusCode ,
final String httpStatusText ,
2005-12-07 14:26:27 +01:00
String contentType ,
2008-08-02 14:12:04 +02:00
final long contentLength ,
2005-12-07 14:26:27 +01:00
Date moddate ,
2008-08-02 14:12:04 +02:00
final Date expires ,
2006-01-13 23:50:04 +01:00
httpHeader headers ,
2008-08-02 14:12:04 +02:00
final String contentEnc ,
final String transferEnc ,
final boolean nocache
2005-12-07 14:26:27 +01:00
) throws IOException {
2005-06-09 12:07:02 +02:00
2008-08-02 14:12:04 +02:00
final String reqMethod = conProp . getProperty ( httpHeader . CONNECTION_PROP_METHOD ) ;
2006-02-15 13:31:52 +01:00
if ( ( transferEnc ! = null ) & & ! httpVersion . equals ( httpHeader . HTTP_VERSION_1_1 ) ) {
2007-06-26 17:06:23 +02:00
throw new IllegalArgumentException ( " Transfer encoding is only supported for http/1.1 connections. The current connection version is " + httpVersion ) ;
2006-02-15 13:31:52 +01:00
}
2006-02-16 11:54:47 +01:00
2006-02-15 13:31:52 +01:00
if ( ! reqMethod . equals ( httpHeader . METHOD_HEAD ) ) {
if ( ! conProp . getProperty ( httpHeader . CONNECTION_PROP_PERSISTENT , " close " ) . equals ( " close " ) ) {
if ( transferEnc = = null & & contentLength < 0 ) {
2007-06-26 16:37:10 +02:00
throw new IllegalArgumentException ( " Message MUST contain a Content-Length or a non-identity transfer-coding header field. " ) ;
2006-02-15 13:31:52 +01:00
}
}
if ( transferEnc ! = null & & contentLength > = 0 ) {
throw new IllegalArgumentException ( " Messages MUST NOT include both a Content-Length header field and a non-identity transfer-coding. " ) ;
}
}
if ( headers = = null ) headers = new httpHeader ( ) ;
2008-08-02 14:12:04 +02:00
final Date now = new Date ( System . currentTimeMillis ( ) ) ;
2005-06-09 12:07:02 +02:00
headers . put ( httpHeader . SERVER , " AnomicHTTPD (www.anomic.de) " ) ;
2008-04-05 15:17:16 +02:00
headers . put ( httpHeader . DATE , HttpClient . dateString ( now ) ) ;
2006-02-15 13:31:52 +01:00
if ( moddate . after ( now ) ) moddate = now ;
2008-04-05 15:17:16 +02:00
headers . put ( httpHeader . LAST_MODIFIED , HttpClient . dateString ( moddate ) ) ;
2005-06-09 12:07:02 +02:00
2005-12-09 18:35:45 +01:00
if ( nocache ) {
2005-12-10 03:11:24 +01:00
if ( httpVersion . toUpperCase ( ) . equals ( httpHeader . HTTP_VERSION_1_1 ) ) headers . put ( httpHeader . CACHE_CONTROL , " no-cache " ) ;
2005-12-09 18:35:45 +01:00
else headers . put ( httpHeader . PRAGMA , " no-cache " ) ;
}
2006-03-07 16:58:50 +01:00
if ( contentType = = null )
contentType = " text/html; charset=UTF-8 " ;
else if ( contentType . startsWith ( " text/ " ) & & contentType . toLowerCase ( ) . indexOf ( " charset= " ) = = - 1 )
contentType + = " ; charset=UTF-8 " ;
headers . put ( httpHeader . CONTENT_TYPE , contentType ) ;
2005-06-09 12:07:02 +02:00
if ( contentLength > 0 ) headers . put ( httpHeader . CONTENT_LENGTH , Long . toString ( contentLength ) ) ;
2006-01-13 22:29:04 +01:00
//if (cookie != null) headers.put(httpHeader.SET_COOKIE, cookie);
2008-04-05 15:17:16 +02:00
if ( expires ! = null ) headers . put ( httpHeader . EXPIRES , HttpClient . dateString ( expires ) ) ;
2005-06-09 12:07:02 +02:00
if ( contentEnc ! = null ) headers . put ( httpHeader . CONTENT_ENCODING , contentEnc ) ;
if ( transferEnc ! = null ) headers . put ( httpHeader . TRANSFER_ENCODING , transferEnc ) ;
2006-01-13 23:50:04 +01:00
sendRespondHeader ( conProp , respond , httpVersion , httpStatusCode , httpStatusText , headers ) ;
2005-06-09 12:07:02 +02:00
}
public static final void sendRespondHeader (
2008-08-02 14:12:04 +02:00
final Properties conProp ,
final OutputStream respond ,
final String httpVersion ,
final int httpStatusCode ,
final httpHeader header
2005-06-09 12:07:02 +02:00
) throws IOException {
sendRespondHeader ( conProp , respond , httpVersion , httpStatusCode , null , header ) ;
}
2006-01-13 23:50:04 +01:00
2006-01-13 22:29:04 +01:00
public static final void sendRespondHeader (
2008-08-02 14:12:04 +02:00
final Properties conProp ,
final OutputStream respond ,
2006-01-13 22:29:04 +01:00
String httpVersion ,
2008-08-02 14:12:04 +02:00
final int httpStatusCode ,
2006-01-13 22:29:04 +01:00
String httpStatusText ,
2006-01-13 23:50:04 +01:00
httpHeader header
2006-01-13 22:29:04 +01:00
) throws IOException {
2005-06-09 12:07:02 +02:00
if ( respond = = null ) throw new NullPointerException ( " The outputstream must not be null. " ) ;
if ( conProp = = null ) throw new NullPointerException ( " The connection property structure must not be null. " ) ;
2006-10-28 14:31:48 +02:00
if ( httpVersion = = null ) httpVersion = conProp . getProperty ( httpHeader . CONNECTION_PROP_HTTP_VER , httpHeader . HTTP_VERSION_1_1 ) ;
2005-10-18 09:45:27 +02:00
if ( header = = null ) header = new httpHeader ( ) ;
2005-06-09 12:07:02 +02:00
try {
2006-02-11 16:21:04 +01:00
if ( ( httpStatusText = = null ) | | ( httpStatusText . length ( ) = = 0 ) ) {
if ( httpVersion . equals ( httpHeader . HTTP_VERSION_1_0 ) & & httpHeader . http1_0 . containsKey ( Integer . toString ( httpStatusCode ) ) )
2008-03-05 14:29:42 +01:00
httpStatusText = httpHeader . http1_0 . get ( Integer . toString ( httpStatusCode ) ) ;
2006-02-11 16:21:04 +01:00
else if ( httpVersion . equals ( httpHeader . HTTP_VERSION_1_1 ) & & httpHeader . http1_1 . containsKey ( Integer . toString ( httpStatusCode ) ) )
2008-03-05 14:29:42 +01:00
httpStatusText = httpHeader . http1_1 . get ( Integer . toString ( httpStatusCode ) ) ;
2006-02-11 16:21:04 +01:00
else httpStatusText = " Unknown " ;
}
2008-08-02 14:12:04 +02:00
final StringBuffer headerStringBuffer = new StringBuffer ( 560 ) ;
2006-02-11 16:21:04 +01:00
2006-02-16 11:27:21 +01:00
// "HTTP/0.9" does not have a status line or header in the response
2006-10-28 14:31:48 +02:00
if ( ! httpVersion . toUpperCase ( ) . equals ( httpHeader . HTTP_VERSION_0_9 ) ) {
2006-02-16 11:27:21 +01:00
// write status line
headerStringBuffer . append ( httpVersion ) . append ( " " )
. append ( Integer . toString ( httpStatusCode ) ) . append ( " " )
. append ( httpStatusText ) . append ( " \ r \ n " ) ;
2005-12-09 18:35:45 +01:00
// prepare header
if ( ! header . containsKey ( httpHeader . DATE ) )
2008-04-05 15:17:16 +02:00
header . put ( httpHeader . DATE , HttpClient . dateString ( new Date ( ) ) ) ;
2005-12-09 18:35:45 +01:00
if ( ! header . containsKey ( httpHeader . CONTENT_TYPE ) )
2006-02-16 22:33:41 +01:00
header . put ( httpHeader . CONTENT_TYPE , " text/html; charset=UTF-8 " ) ; // fix this
2005-12-09 18:35:45 +01:00
if ( ! header . containsKey ( httpHeader . CONNECTION ) & & conProp . containsKey ( httpHeader . CONNECTION_PROP_PERSISTENT ) )
header . put ( httpHeader . CONNECTION , conProp . getProperty ( httpHeader . CONNECTION_PROP_PERSISTENT ) ) ;
if ( ! header . containsKey ( httpHeader . PROXY_CONNECTION ) & & conProp . containsKey ( httpHeader . CONNECTION_PROP_PERSISTENT ) )
header . put ( httpHeader . PROXY_CONNECTION , conProp . getProperty ( httpHeader . CONNECTION_PROP_PERSISTENT ) ) ;
if ( conProp . containsKey ( httpHeader . CONNECTION_PROP_PERSISTENT ) & &
conProp . getProperty ( httpHeader . CONNECTION_PROP_PERSISTENT ) . equals ( " keep-alive " ) & &
! header . containsKey ( httpHeader . TRANSFER_ENCODING ) & &
! header . containsKey ( httpHeader . CONTENT_LENGTH ) )
header . put ( httpHeader . CONTENT_LENGTH , " 0 " ) ;
// adding some yacy specific headers
header . put ( httpHeader . X_YACY_KEEP_ALIVE_REQUEST_COUNT , conProp . getProperty ( httpHeader . CONNECTION_PROP_KEEP_ALIVE_COUNT ) ) ;
header . put ( httpHeader . X_YACY_ORIGINAL_REQUEST_LINE , conProp . getProperty ( httpHeader . CONNECTION_PROP_REQUESTLINE ) ) ;
header . put ( httpHeader . X_YACY_PREVIOUS_REQUEST_LINE , conProp . getProperty ( httpHeader . CONNECTION_PROP_PREV_REQUESTLINE ) ) ;
2006-02-11 16:21:04 +01:00
2006-01-13 22:29:04 +01:00
//read custom headers
2006-01-13 23:50:04 +01:00
/ *
2006-01-13 22:29:04 +01:00
if ( requestProperties ! = null )
{
2006-01-13 23:50:04 +01:00
httpHeader outgoingHeader = requestProperties . getOutgoingHeader ( ) ;
if ( outgoingHeader ! = null )
{ * /
2008-08-02 14:12:04 +02:00
final Iterator < httpHeader . Entry > it = header . getCookies ( ) ;
2008-01-28 19:21:08 +01:00
while ( it . hasNext ( ) ) {
2006-01-13 22:29:04 +01:00
//Append user properties to the main String
//TODO: Should we check for user properites. What if they intersect properties that are already in header?
2008-08-02 14:12:04 +02:00
final httpHeader . Entry e = it . next ( ) ;
2006-01-13 22:29:04 +01:00
headerStringBuffer . append ( e . getKey ( ) ) . append ( " : " ) . append ( e . getValue ( ) ) . append ( " \ r \ n " ) ;
}
2006-01-13 23:50:04 +01:00
/ *
}
} * /
2005-12-09 18:35:45 +01:00
// write header
2008-08-02 14:12:04 +02:00
final Iterator < String > i = header . keySet ( ) . iterator ( ) ;
2005-12-09 18:35:45 +01:00
String key ;
char tag ;
int count ;
//System.out.println("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
while ( i . hasNext ( ) ) {
2008-01-28 19:21:08 +01:00
key = i . next ( ) ;
2005-12-09 18:35:45 +01:00
tag = key . charAt ( 0 ) ;
if ( ( tag ! = '*' ) & & ( tag ! = '#' ) ) { // '#' in key is reserved for proxy attributes as artificial header values
count = header . keyCount ( key ) ;
for ( int j = 0 ; j < count ; j + + ) {
headerStringBuffer . append ( key ) . append ( " : " ) . append ( ( String ) header . getSingle ( key , j ) ) . append ( " \ r \ n " ) ;
}
//System.out.println("#" + key + ": " + value);
}
}
2006-02-16 12:07:17 +01:00
// end header
headerStringBuffer . append ( " \ r \ n " ) ;
// sending headers to the client
respond . write ( headerStringBuffer . toString ( ) . getBytes ( ) ) ;
// flush stream
respond . flush ( ) ;
2005-06-09 12:07:02 +02:00
}
2005-09-20 23:49:47 +02:00
conProp . put ( httpHeader . CONNECTION_PROP_PROXY_RESPOND_HEADER , header ) ;
conProp . put ( httpHeader . CONNECTION_PROP_PROXY_RESPOND_STATUS , Integer . toString ( httpStatusCode ) ) ;
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) {
2005-06-09 12:07:02 +02:00
// any interruption may be caused be network error or because the user has closed
// the windows during transmission. We simply pass it as IOException
throw new IOException ( e . getMessage ( ) ) ;
}
}
2008-08-02 14:12:04 +02:00
public static boolean shallTransportZipped ( final String path ) {
2005-06-09 12:07:02 +02:00
if ( ( path = = null ) | | ( path . length ( ) = = 0 ) ) return true ;
int pos ;
if ( ( pos = path . lastIndexOf ( " . " ) ) ! = - 1 ) {
return ! disallowZippedContentEncoding . contains ( path . substring ( pos ) . toLowerCase ( ) ) ;
}
return true ;
}
2008-08-02 14:12:04 +02:00
public void doUserAccounting ( final Properties conProps ) {
2005-09-30 18:02:58 +02:00
// TODO: validation of conprop fields
// httpHeader.CONNECTION_PROP_USER
// httpHeader.CONNECTION_PROP_CLIENTIP
// httpHeader.CONNECTION_PROP_PROXY_RESPOND_SIZE
// httpHeader.CONNECTION_PROP_PROXY_RESPOND_STATUS
}
2005-06-09 12:07:02 +02:00
2008-08-02 14:12:04 +02:00
public static boolean isThisSeedIP ( final String hostName ) {
2006-04-25 07:29:20 +02:00
if ( ( hostName = = null ) | | ( hostName . length ( ) = = 0 ) ) return false ;
// getting ip address and port of this seed
2008-05-06 01:13:47 +02:00
if ( alternativeResolver = = null ) return false ;
2006-04-25 07:29:20 +02:00
// resolve ip addresses
2008-08-02 14:12:04 +02:00
final InetAddress seedInetAddress = serverDomains . dnsResolve ( alternativeResolver . myIP ( ) ) ;
final InetAddress hostInetAddress = serverDomains . dnsResolve ( hostName ) ;
2006-05-15 12:50:10 +02:00
if ( seedInetAddress = = null | | hostInetAddress = = null ) return false ;
2006-04-25 07:29:20 +02:00
// if it's equal, the hostname points to this seed
return ( seedInetAddress . equals ( hostInetAddress ) ) ;
}
2008-08-02 14:12:04 +02:00
public static boolean isThisHostIP ( final String hostName ) {
2005-10-12 10:17:43 +02:00
if ( ( hostName = = null ) | | ( hostName . length ( ) = = 0 ) ) return false ;
boolean isThisHostIP = false ;
try {
2005-11-07 11:57:54 +01:00
// final InetAddress clientAddress = InetAddress.getByName(hostName);
2007-07-24 02:46:17 +02:00
final InetAddress clientAddress = serverDomains . dnsResolve ( hostName ) ;
2005-11-07 11:57:54 +01:00
if ( clientAddress = = null ) return false ;
2005-10-12 10:17:43 +02:00
if ( clientAddress . isAnyLocalAddress ( ) | | clientAddress . isLoopbackAddress ( ) ) return true ;
final InetAddress [ ] localAddress = InetAddress . getAllByName ( InetAddress . getLocalHost ( ) . getHostName ( ) ) ;
for ( int i = 0 ; i < localAddress . length ; i + + ) {
if ( localAddress [ i ] . equals ( clientAddress ) ) {
isThisHostIP = true ;
break ;
}
}
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) { }
2005-10-12 10:17:43 +02:00
return isThisHostIP ;
}
2008-08-02 14:12:04 +02:00
public static boolean isThisHostIP ( final InetAddress clientAddress ) {
2005-10-12 10:17:43 +02:00
if ( clientAddress = = null ) return false ;
boolean isThisHostIP = false ;
try {
if ( clientAddress . isAnyLocalAddress ( ) | | clientAddress . isLoopbackAddress ( ) ) return true ;
final InetAddress [ ] localAddress = InetAddress . getAllByName ( InetAddress . getLocalHost ( ) . getHostName ( ) ) ;
for ( int i = 0 ; i < localAddress . length ; i + + ) {
if ( localAddress [ i ] . equals ( clientAddress ) ) {
isThisHostIP = true ;
break ;
}
}
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) { }
2005-10-12 10:17:43 +02:00
return isThisHostIP ;
}
2008-08-02 14:12:04 +02:00
public static boolean isThisHostName ( final String hostName ) {
2005-10-12 10:17:43 +02:00
if ( ( hostName = = null ) | | ( hostName . length ( ) = = 0 ) ) return false ;
try {
final int idx = hostName . indexOf ( " : " ) ;
final String dstHost = ( idx ! = - 1 ) ? hostName . substring ( 0 , idx ) . trim ( ) : hostName . trim ( ) ;
2008-08-06 21:43:12 +02:00
final Integer dstPort = ( idx ! = - 1 ) ? Integer . valueOf ( hostName . substring ( idx + 1 ) . trim ( ) ) : Integer . valueOf ( 80 ) ;
2005-10-12 10:17:43 +02:00
// if the hostname endswith thisPeerName.yacy ...
2008-08-02 14:12:04 +02:00
final String alternativeAddress = ( alternativeResolver = = null ) ? null : alternativeResolver . myAlternativeAddress ( ) ;
2008-05-06 01:13:47 +02:00
if ( ( alternativeAddress ! = null ) & & ( dstHost . endsWith ( alternativeAddress ) ) ) {
2005-10-12 10:17:43 +02:00
return true ;
/ *
* If the port number is equal to the yacy port and the IP address is an address of this host . . .
* Please note that yacy is listening to all interfaces of this host
* /
2006-04-25 07:29:20 +02:00
} else if (
// check if the destination port is equal to the port yacy is listening to
2008-08-06 21:43:12 +02:00
dstPort . equals ( Integer . valueOf ( serverCore . getPortNr ( switchboard . getConfig ( " port " , " 8080 " ) ) ) ) & &
2006-04-25 07:29:20 +02:00
(
// check if the destination host is our local IP address
isThisHostIP ( dstHost ) | |
// check if the destination host is our seed ip address
isThisSeedIP ( dstHost )
)
) {
2005-10-12 10:17:43 +02:00
return true ;
}
2008-08-02 14:12:04 +02:00
} catch ( final Exception e ) { }
2005-10-12 10:17:43 +02:00
return false ;
2005-10-18 09:45:27 +02:00
}
2005-04-07 21:19:42 +02:00
}