Annotation of java/classes/org/w3c/www/protocol/http/HttpManager.java, revision 1.62

1.1       abaird      1: // HttpManager.java
1.62    ! ylafon      2: // $Id: HttpManager.java,v 1.61 2000/03/06 10:08:54 ylafon Exp $
1.1       abaird      3: // (c) COPYRIGHT MIT and INRIA, 1996.
                      4: // Please first read the full copyright statement in file COPYRIGHT.html
                      5: 
1.38      bmahe       6: package org.w3c.www.protocol.http ;
1.1       abaird      7: 
                      8: import java.util.*;
                      9: import java.net.*;
                     10: import java.io.*; // FIXME - DEBUG
                     11: 
1.38      bmahe      12: import org.w3c.www.mime.*;
                     13: import org.w3c.util.*;
1.1       abaird     14: 
1.25      abaird     15: class ManagerDescription {
                     16:     HttpManager manager = null;
                     17:     Properties  properties = null;
                     18: 
                     19:     final HttpManager getManager() {
                     20:        return manager;
                     21:     }
                     22: 
                     23:     final boolean sameProperties(Properties props) {
1.54      bmahe      24:        if (props.size() != properties.size())
                     25:            return false;
                     26:        Enumeration enum = props.propertyNames();
                     27:        while (enum.hasMoreElements()) {
                     28:            String name = (String) enum.nextElement();
                     29:            String prop = properties.getProperty(name);
                     30:            if ((prop == null) || (! prop.equals(props.getProperty(name))))
                     31:                return false;
                     32:        }
                     33:        return true;
1.25      abaird     34:     }
                     35: 
                     36:     ManagerDescription(HttpManager manager, Properties props) {
                     37:        this.manager    = manager;
1.54      bmahe      38:        this.properties = (Properties)props.clone();
1.25      abaird     39:     }
                     40: }
                     41: 
1.1       abaird     42: class ReplyFactory implements MimeParserFactory {
                     43:     
                     44:     public MimeHeaderHolder createHeaderHolder(MimeParser parser) {
                     45:        return new Reply(parser);
                     46:     }
                     47: 
                     48: }
                     49: 
                     50: /**
                     51:  * The client side HTTP request manager.
                     52:  * This class is the user interface (along with the other public classes of
                     53:  * this package) for the W3C client side library implementing HTTP. 
                     54:  * A typicall request is launched though the following sequence:
                     55:  * <pre>
                     56:  * HttpManager     manager = HttpManager.getManager() ;
1.36      ylafon     57:  * Request request = manager.createRequest() ;
1.1       abaird     58:  * request.setMethod(HTTP.GET) ;
                     59:  * request.setURL(new URL("http://www.w3.org/pub/WWW/"));
                     60:  * Reply    reply = manager.runRequest(request) ;
                     61:  * // Get the reply input stream that contains the actual data:
                     62:  * InputStream in = reply.getInputStream() ;
                     63:  * ...
                     64:  * </pre>
                     65:  */
                     66: 
1.13      abaird     67: public class HttpManager implements PropertyMonitoring {
1.45      ylafon     68: 
                     69:     private static final boolean debug = false;
                     70: 
1.17      abaird     71:     private static final 
1.38      bmahe      72:     String DEFAULT_SERVER_CLASS = "org.w3c.www.protocol.http.HttpBasicServer";
1.17      abaird     73: 
                     74:     /**
                     75:      * The name of the property indicating the class of HttpServer to use.
                     76:      */
                     77:     public static final
1.38      bmahe      78:     String SERVER_CLASS_P = "org.w3c.www.protocol.http.server";
1.17      abaird     79: 
1.2       abaird     80:     /**
                     81:      * The name of the property containing the ProprequestFilter to launch.
                     82:      */
1.12      abaird     83:     public static final 
1.38      bmahe      84:     String FILTERS_PROP_P = "org.w3c.www.protocol.http.filters";
1.5       abaird     85:     /**
1.43      ylafon     86:      * The maximum number of simultaneous connectionlrus.
1.11      abaird     87:      */
                     88:     public static final
1.38      bmahe      89:     String CONN_MAX_P = "org.w3c.www.protocol.http.connections.max";
1.11      abaird     90:     /**
1.35      ylafon     91:      * The SO_TIMEOUT of the client socket.
                     92:      */
                     93:     public static final
1.38      bmahe      94:     String TIMEOUT_P = "org.w3c.www.protocol.http.connections.timeout";
1.35      ylafon     95:     /**
1.5       abaird     96:      * Header properties - The allowed drift for getting cached resources.
                     97:      */
                     98:     public static final 
1.38      bmahe      99:     String MAX_STALE_P = "org.w3c.www.protocol.http.cacheControl.maxStale";
1.5       abaird    100:     /**
                    101:      * Header properties - The minium freshness required on cached resources.
                    102:      */
                    103:     public static final
1.38      bmahe     104:     String MIN_FRESH_P = "org.w3c.www.protocol.http.cacheControl.minFresh";
1.5       abaird    105:     /**
                    106:      * Header properties - Set the only if cached flag on requests.
                    107:      */
                    108:     public static final 
1.39      ylafon    109:     String ONLY_IF_CACHED_P=
                    110:                          "org.w3c.www.protocol.http.cacheControl.onlyIfCached";
1.5       abaird    111:     /**
                    112:      * Header properties - Set the user agent.
                    113:      */
                    114:     public static final 
1.38      bmahe     115:     String USER_AGENT_P = "org.w3c.www.protocol.http.userAgent";
1.5       abaird    116:     /**
                    117:      * Header properties - Set the accept header.
                    118:      */
                    119:     public static final 
1.38      bmahe     120:     String ACCEPT_P = "org.w3c.www.protocol.http.accept";
1.5       abaird    121:     /**
                    122:      * Header properties - Set the accept language.
                    123:      */
                    124:     public static final 
1.38      bmahe     125:     String ACCEPT_LANGUAGE_P = "org.w3c.www.protocol.http.acceptLanguage";
1.5       abaird    126:     /**
                    127:      * Header properties - Set the accept encodings.
                    128:      */
                    129:     public static final 
1.38      bmahe     130:     String ACCEPT_ENCODING_P = "org.w3c.www.protocol.http.acceptEncoding";
1.5       abaird    131:     /**
1.62    ! ylafon    132:      * Header properties - are we parsing answers in a lenient way?
        !           133:      */
        !           134:     public static final 
        !           135:     String LENIENT_P = "org.w3c.www.protocol.http.lenient";
        !           136:     /**
1.5       abaird    137:      * Header properties - Should we use a proxy ?
                    138:      */
                    139:     public static final
1.12      abaird    140:     String PROXY_SET_P = "proxySet";
1.5       abaird    141:     /**
                    142:      * Header properties - What is the proxy host name.
                    143:      */
                    144:     public static final
1.12      abaird    145:     String PROXY_HOST_P = "proxyHost";
1.5       abaird    146:     /**
                    147:      * Header properties - What is the proxy port number.
                    148:      */
                    149:     public static final
1.12      abaird    150:     String PROXY_PORT_P = "proxyPort";
1.2       abaird    151: 
1.7       abaird    152:     /**
                    153:      * The default value for the <code>Accept</code> header.
                    154:      */
                    155:     public static final
                    156:     String DEFAULT_ACCEPT = "*/*";
                    157:     /**
                    158:      * The default value for the <code>User-Agent</code> header.
                    159:      */
                    160:     public static final
1.61      ylafon    161:     String DEFAULT_USER_AGENT = "Jigsaw/2.1.1";
1.7       abaird    162: 
1.24      abaird    163:     /**
                    164:      * This array keeps track of all the created managers.
                    165:      * A new manager (kind of HTTP client side context) is created for each
                    166:      * diffferent set of properties.
                    167:      */
1.25      abaird    168:     private static ManagerDescription managers[] = new ManagerDescription[4];
1.1       abaird    169:     
                    170:     /**
1.17      abaird    171:      * The class to instantiate to create new HttpServer instances.
                    172:      */
                    173:     protected Class serverclass = null;
                    174:     /**
1.13      abaird    175:      * The properties we initialized from.
                    176:      */
                    177:     ObservableProperties props = null;
                    178:     /**
1.1       abaird    179:      * The server this manager knows about, indexed by FQDN of target servers.
                    180:      */
                    181:     protected Hashtable servers = null;
                    182:     /**
                    183:      * The template request (the request we will clone to create new requests)
                    184:      */
1.4       abaird    185:     protected Request template = null ;
                    186:     /**
1.9       abaird    187:      * The LRU list of connections.
1.4       abaird    188:      */
1.9       abaird    189:     protected LRUList connectionsLru = null;
1.1       abaird    190:     /**
                    191:      * The filter engine attached to this manager.
                    192:      */
                    193:     FilterEngine filteng = null;
                    194: 
1.35      ylafon    195:     protected int timeout = 300000;
1.9       abaird    196:     protected int conn_count = 0;
                    197:     protected int conn_max = 5;
1.62    ! ylafon    198:     protected boolean lenient = true;
1.9       abaird    199: 
1.1       abaird    200:     /**
1.13      abaird    201:      * Update the proxy configuration to match current properties setting.
                    202:      * @return A boolean, <strong>true</strong> if change was done,
                    203:      * <strong>false</strong> otherwise.
                    204:      */
                    205: 
                    206:     protected boolean updateProxy() {
                    207:        boolean set = props.getBoolean(PROXY_SET_P, false);
                    208:        if ( set ) {
                    209:            // Wow using a proxy now !
                    210:            String host  = props.getString(PROXY_HOST_P, null);
                    211:            int    port  = props.getInteger(PROXY_PORT_P, -1);
                    212:            URL    proxy = null;
                    213:            try {
                    214:                proxy   = new URL("http", host, port, "/");
                    215:            } catch (Exception ex) {
                    216:                return false;
                    217:            }
                    218:            // Now if a proxy...
1.55      bmahe     219:            if (( proxy != null ) && (proxy.getHost() != null))
1.24      abaird    220:                template.setProxy(proxy);
1.13      abaird    221:        } else {
1.24      abaird    222:            template.setProxy(null);
1.13      abaird    223:        }
                    224:        return true;
                    225:     }
                    226: 
                    227:     /**
1.24      abaird    228:      * Get this manager properties.
                    229:      * @return An ObservableProperties instance.
                    230:      */
                    231: 
                    232:     public final ObservableProperties getProperties() {
                    233:        return props;
                    234:     }
                    235: 
                    236:     /**
1.13      abaird    237:      * PropertyMonitoring implementation - Update properties on the fly !
                    238:      * @param name The name of the property that has changed.
                    239:      * @return A boolean, <strong>true</strong> if change is accepted,
                    240:      * <strong>false</strong> otherwise.
                    241:      */
                    242: 
                    243:     public boolean propertyChanged(String name) {
1.24      abaird    244:        Request tpl = template;
1.13      abaird    245:        if ( name.equals(FILTERS_PROP_P) ) {
1.37      ylafon    246:            // FIXME
                    247:            return true;
                    248:            // return false;
1.35      ylafon    249:        } else if ( name.equals(TIMEOUT_P) ) {
                    250:            setTimeout(props.getInteger(TIMEOUT_P, timeout));
                    251:            return true;
1.13      abaird    252:        } else if ( name.equals(CONN_MAX_P) ) {
                    253:            setMaxConnections(props.getInteger(CONN_MAX_P, conn_max));
                    254:            return true;
                    255:        } else if ( name.equals(MAX_STALE_P) ) {
                    256:            int ival = props.getInteger(MAX_STALE_P, -1);
                    257:            if ( ival >= 0 )
                    258:                tpl.setMaxStale(ival);
                    259:            return true;
                    260:        } else if ( name.equals(MIN_FRESH_P) ) {
                    261:            int ival = props.getInteger(MIN_FRESH_P, -1);
                    262:            if ( ival >= 0 )
                    263:                tpl.setMinFresh(ival);
                    264:            return true;
1.62    ! ylafon    265:        } else if ( name.equals(LENIENT_P) ) {
        !           266:            lenient = props.getBoolean(ONLY_IF_CACHED_P, lenient);
        !           267:            return true;
        !           268:        } if ( name.equals(ONLY_IF_CACHED_P) ) {
1.13      abaird    269:            tpl.setOnlyIfCached(props.getBoolean(ONLY_IF_CACHED_P, false));
                    270:            return true;
                    271:        } else if ( name.equals(USER_AGENT_P) ) {
                    272:            tpl.setValue("user-agent"
                    273:                         , props.getString(USER_AGENT_P
                    274:                                           , DEFAULT_USER_AGENT));
                    275:            return true;
                    276:        } else if ( name.equals(ACCEPT_P) ) {
                    277:            tpl.setValue("accept" 
                    278:                         , props.getString(ACCEPT_P, DEFAULT_ACCEPT));
                    279:            return true;
                    280:        } else if ( name.equals(ACCEPT_LANGUAGE_P) ) {
                    281:            String sval = props.getString(ACCEPT_LANGUAGE_P, null);
                    282:            if ( sval != null )
                    283:                tpl.setValue("accept-language", sval);
                    284:            return true;
                    285:        } else if ( name.equals(ACCEPT_ENCODING_P) ) {
                    286:            String sval = props.getString(ACCEPT_ENCODING_P, null);
                    287:            if ( sval != null )
                    288:                tpl.setValue("accept-encoding", sval);
                    289:            return true;
                    290:        } else if ( name.equals(PROXY_SET_P)
                    291:                    || name.equals(PROXY_HOST_P)
                    292:                    || name.equals(PROXY_PORT_P) ) {
                    293:            return updateProxy();
                    294:        } else {
                    295:            return true;
                    296:        } 
                    297:     }
                    298: 
                    299:     /**
1.4       abaird    300:      * Allow the manager to interact with the user if needed.
                    301:      * This will, for example, allow prompting for paswords, etc.
                    302:      * @param onoff Turn interaction on or off.
                    303:      */
                    304: 
                    305:     public void setAllowUserInteraction(boolean onoff) {
                    306:        template.setAllowUserInteraction(onoff);
                    307:     }
                    308: 
                    309:     /**
1.1       abaird    310:      * Get an instance of the HTTP manager.
                    311:      * This method returns an actual instance of the HTTP manager. It may
                    312:      * return different managers, if it decides to distribute the load on
                    313:      * different managers (avoid the HttpManager being a bottleneck).
                    314:      * @return An application wide instance of the HTTP manager.
                    315:      */
                    316: 
1.24      abaird    317:     public static synchronized HttpManager getManager(Properties p) {
                    318:        // Does such a manager exists already ?
                    319:        for (int i = 0 ; i < managers.length ; i++) {
                    320:            if ( managers[i] == null )
                    321:                continue;
1.54      bmahe     322:            if ( managers[i].sameProperties(p) )
1.25      abaird    323:                return managers[i].getManager();
1.24      abaird    324:        }
                    325:        // Get the props we will initialize from:
1.28      abaird    326:        ObservableProperties props = null;
1.24      abaird    327:        if ( p instanceof ObservableProperties )
1.28      abaird    328:            props = (ObservableProperties) p;
1.24      abaird    329:        else
1.28      abaird    330:            props = new ObservableProperties(p);
                    331:        // Create a new manager for this set of properties:
                    332:        HttpManager manager = new HttpManager() ;
                    333:        manager.props = props;
1.24      abaird    334:        // Initialize this new manager filters:
                    335:        String filters[] = props.getStringArray(FILTERS_PROP_P, null);
                    336:        if ( filters != null ) {
                    337:            for (int i = 0 ; i < filters.length ; i++) {
                    338:                try {
                    339:                    Class c = Class.forName(filters[i]);
                    340:                    PropRequestFilter f = null;
                    341:                    f = (PropRequestFilter) c.newInstance();
                    342:                    f.initialize(manager);
                    343:                } catch (PropRequestFilterException ex) {
                    344:                    System.out.println("Couldn't initialize filter \""
                    345:                                       + filters[i]
                    346:                                       + "\" init failed: "
                    347:                                        + ex.getMessage());
                    348:                } catch (Exception ex) {
                    349:                    System.err.println("Error initializing prop filters:");
                    350:                    System.err.println("Coulnd't initialize ["
                    351:                                        + filters[i]
                    352:                                       + "]: " + ex.getMessage());
                    353:                    ex.printStackTrace();
                    354:                    System.exit(1);
1.2       abaird    355:                }
                    356:            }
1.24      abaird    357:        }
                    358:        // The factory to create MIME reply holders:
                    359:        manager.factory = new ReplyFactory();
                    360:        // The class to create HttpServer instances from
                    361:        String c = props.getString(SERVER_CLASS_P, DEFAULT_SERVER_CLASS);
                    362:        try {
1.34      bmahe     363:          manager.serverclass = Class.forName(c);
1.24      abaird    364:        } catch (Exception ex) {
                    365:            System.err.println("Unable to initialize HttpManager: ");
                    366:            System.err.println("Class \""+c+"\" not found, from property "
                    367:                                + SERVER_CLASS_P);
                    368:            ex.printStackTrace();
                    369:            System.exit(1);
                    370:        }
                    371:        // Setup the template request:
                    372:        Request tpl = manager.template;
                    373:        // Set some default headers value (from props)
                    374:        // Check for a proxy ?
                    375:        manager.updateProxy();
                    376:        // CacheControl, only-if-cached
                    377:        tpl.setOnlyIfCached(props.getBoolean(ONLY_IF_CACHED_P, false));
                    378:        // CacheControl, maxstale
                    379:        int ival = props.getInteger(MAX_STALE_P, -1);
                    380:        if ( ival >= 0 )
                    381:            tpl.setMaxStale(ival);
                    382:        // CacheControl, minfresh:
                    383:        ival = props.getInteger(MIN_FRESH_P, -1);
                    384:        if ( ival >= 0 )
                    385:            tpl.setMinFresh(ival);
1.62    ! ylafon    386:        // general, lenient
        !           387:        manager.lenient = props.getBoolean(LENIENT_P, true);
1.24      abaird    388:        // General, User agent
                    389:        tpl.setValue("user-agent"
                    390:                     , props.getString(USER_AGENT_P
                    391:                                       , DEFAULT_USER_AGENT));
                    392:        // General, Accept
                    393:        tpl.setValue("accept" 
                    394:                     , props.getString(ACCEPT_P, DEFAULT_ACCEPT));
                    395:        // General, Accept-Language
                    396:        String sval = props.getString(ACCEPT_LANGUAGE_P, null);
                    397:        if ( sval != null )
                    398:            tpl.setValue("accept-language", sval);
                    399:        // General, Accept-Encoding
                    400:        sval = props.getString(ACCEPT_ENCODING_P, null);
                    401:        if ( sval != null )
                    402:            tpl.setValue("accept-encoding", sval);
                    403:        // Maximum number of allowed connections:
                    404:        manager.conn_max = props.getInteger(CONN_MAX_P, 5);
1.58      ylafon    405:        // timeout value
1.59      ylafon    406:        manager.timeout = props.getInteger(TIMEOUT_P, manager.timeout);
1.24      abaird    407:        // Register ourself as a property observer:
                    408:        props.registerObserver(manager);
                    409:        // Register that manager in our knwon managers:
                    410:        for (int i = 0 ; i < managers.length ; i++) {
                    411:            if ( managers[i] == null ) {
1.25      abaird    412:                managers[i] = new ManagerDescription(manager, p);
1.24      abaird    413:                return manager;
1.17      abaird    414:            }
1.1       abaird    415:        }
1.25      abaird    416:        ManagerDescription nm[] = new ManagerDescription[managers.length << 1];
                    417:        System.arraycopy(managers, 0, nm, 0, managers.length);
                    418:        nm[managers.length] = new ManagerDescription(manager, p);
                    419:        managers = nm;
1.1       abaird    420:        return manager;
                    421:     }
                    422: 
1.24      abaird    423:     public static HttpManager getManager() {
                    424:        return getManager(System.getProperties());
                    425:     }
1.1       abaird    426: 
                    427:     /**
1.32      abaird    428:      * Get the String key for the server instance handling that request.
                    429:      * This method takes care of any proxy setting (it will return the key
                    430:      * to the proxy when required.)
                    431:      * @return A uniq identifier for the handling server, as a String.
                    432:      */
                    433: 
                    434:     public final String getServerKey(Request request) {
                    435:        URL    proxy  = request.getProxy();
                    436:        URL    target = request.getURL();
                    437:        String key   = null;
                    438:        if ( proxy != null ) {
                    439:            return ((proxy.getPort() == 80)
                    440:                    ? proxy.getHost().toLowerCase()
                    441:                    : (proxy.getHost().toLowerCase()+":"+proxy.getPort()));
                    442:        } else {
                    443:            return ((target.getPort() == 80)
                    444:                    ? target.getHost().toLowerCase()
                    445:                    : (target.getHost().toLowerCase()+":"+target.getPort()));
                    446:        }
                    447:     }
                    448: 
                    449:     /**
1.1       abaird    450:      * Get the appropriate server object for handling request to given target.
1.32      abaird    451:      * @param key The server's key, as returned by <code>getServerKey</code>.
1.1       abaird    452:      * @return An object complying to the HttpServer interface.
                    453:      * @exception HttpException If the given host name couldn't be resolved.
                    454:      */
                    455: 
1.5       abaird    456:     protected synchronized HttpServer lookupServer(String host, int port)
1.1       abaird    457:        throws HttpException
                    458:     {
1.5       abaird    459:        int    p  = (port == -1) ? 80 : port;
1.32      abaird    460:        String id = ((p == 80) 
                    461:                     ? host.toLowerCase() 
                    462:                     : (host.toLowerCase() +":"+p));
1.1       abaird    463:        // Check for an existing server:
                    464:        HttpServer server = (HttpServer) servers.get(id);
                    465:        if ( server != null )
                    466:            return server;
                    467:        // Create and register a new server:
1.17      abaird    468:        try {
                    469:            server = (HttpServer) serverclass.newInstance();
                    470:        } catch (Exception ex) {
                    471:            String msg = ("Unable to create an instance of \""
                    472:                          + serverclass.getName()
                    473:                          + "\", invalid config, check the "
                    474:                          + SERVER_CLASS_P + " property.");
1.21      abaird    475:            throw new HttpException(ex, msg);
1.17      abaird    476:        }
1.35      ylafon    477:        server.initialize(this, new HttpServerState(server), host, p, timeout);
1.1       abaird    478:        servers.put(id, server);
                    479:        return server;
                    480:     }
1.5       abaird    481: 
1.1       abaird    482:     /**
1.60      ylafon    483:      * The given connection is about to be used.
                    484:      * Update our list of available servers.
                    485:      * @param conn The idle connection.
                    486:      */
                    487: 
                    488:     public void notifyUse(HttpConnection conn) {
                    489:        if (debug)
                    490:            System.out.println("+++ connection used");
                    491:        connectionsLru.remove(conn);
                    492:     }
                    493: 
                    494:     /**
1.9       abaird    495:      * The given connection can be reused, but is now idle.
                    496:      * @param conn The connection that is now idle.
1.4       abaird    497:      */
                    498: 
1.60      ylafon    499:     public synchronized void notifyIdle(HttpConnection conn) {
1.45      ylafon    500:        if (debug)
                    501:            System.out.println("+++ connection idle");
1.9       abaird    502:        connectionsLru.toHead(conn);
1.43      ylafon    503:        HttpServerState ss = conn.getServer().getState();
                    504:        ss.registerConnection(conn);
1.47      ylafon    505:        notify();
1.4       abaird    506:     }
                    507: 
                    508:     /**
1.9       abaird    509:      * The given connection has just been created.
                    510:      * @param conn The newly created connection.
                    511:      */
                    512: 
                    513:     protected synchronized void notifyConnection(HttpConnection conn) {
1.45      ylafon    514:        if (debug)
                    515:            System.out.println("+++ notify conn_count " + (conn_count+1)
                    516:                               + " / " + conn_max);
1.9       abaird    517:        if ( ++conn_count > conn_max )
                    518:            closeAnyConnection();
                    519:     }
                    520: 
                    521:     /**
                    522:      * The given connection has been deleted.
                    523:      * @param conn The deleted connection.
                    524:      */
                    525: 
1.60      ylafon    526:     protected void deleteConnection(HttpConnection conn) {
1.9       abaird    527:        HttpServerState ss = conn.getServer().getState();
                    528:        ss.deleteConnection(conn);
1.60      ylafon    529:        synchronized(this) {
                    530:            --conn_count;
                    531:            if (debug)
                    532:                System.out.println("+++ delete conn_count: " + conn_count);
                    533:            notify();
                    534:        }
1.9       abaird    535:     }
                    536: 
                    537:     protected synchronized boolean tooManyConnections() {
1.51      ylafon    538:        return conn_count >= conn_max;
1.9       abaird    539:     }
                    540: 
                    541:     /**
                    542:      * Try reusing one of the idle connection of that server, if any.
                    543:      * @param server The target server.
                    544:      * @return An currently idle connection to the given server.
1.4       abaird    545:      */
                    546: 
1.60      ylafon    547:     protected HttpConnection getConnection(HttpServer server) {
1.9       abaird    548:        HttpServerState ss = server.getState();
1.60      ylafon    549:        return ss.getConnection();
1.9       abaird    550:     }
                    551: 
1.47      ylafon    552:     /**
                    553:      * Wait for a connection to come up.
                    554:      * @param server, the target server.
1.48      bmahe     555:      * @exception InterruptedException If interrupted..
1.47      ylafon    556:      */
                    557: 
1.9       abaird    558:     protected synchronized void waitForConnection(HttpServer server)
                    559:        throws InterruptedException
                    560:     {
1.60      ylafon    561:        wait();
1.4       abaird    562:     }
                    563: 
                    564:     /**
1.60      ylafon    565:      * Close some connections, but pickling the least recently used ones.
1.45      ylafon    566:      * One third of the max number of connection is cut. This is done to 
                    567:      * eliminate the old connections that should be broken already.
                    568:      * (no Socket.isAlive());
1.9       abaird    569:      * @return A boolean, <strong>true</strong> if a connection was closed
                    570:      * <strong>false</strong> otherwise.
1.4       abaird    571:      */
                    572: 
1.60      ylafon    573:     protected boolean closeAnyConnection() {
1.45      ylafon    574:        boolean saved = false;
1.47      ylafon    575:        int max = Math.max(conn_max/3, 1);
1.45      ylafon    576:        for (int i=0; i < max; i++) {
                    577:            HttpConnection conn = (HttpConnection) connectionsLru.removeTail();
                    578:            if ( conn != null ) {
                    579:                conn.close();
                    580:                deleteConnection(conn);
                    581:                if (debug)
                    582:                    System.out.println("+++ close request");
1.60      ylafon    583:                    saved = true;
1.47      ylafon    584:            } else
                    585:                break;
1.9       abaird    586:        }
1.45      ylafon    587:        return saved;
1.4       abaird    588:     }
                    589: 
                    590:     /**
1.1       abaird    591:      * One of our server handler wants to open a connection.
                    592:      * @param block A boolean indicating whether we should block the calling
                    593:      * thread until a token is available (otherwise, the method will just
                    594:      * peek at the connection count, and return the appropriate result).
                    595:      * @return A boolean, <strong>true</strong> if the connection can be
                    596:      * opened straight, <strong>false</strong> otherwise.
                    597:      */
                    598: 
1.9       abaird    599:     protected boolean negotiateConnection(HttpServer server) {
                    600:        HttpServerState ss = server.getState();
1.10      abaird    601:        if ( ! tooManyConnections() ) {
1.4       abaird    602:            return true;
1.10      abaird    603:        } else if ( ss.notEnoughConnections() ) {
                    604:            return closeAnyConnection();
1.9       abaird    605:        } else if ( servers.size() > conn_max ) {
                    606:            return closeAnyConnection();
1.4       abaird    607:        }
1.9       abaird    608:        return false;
1.60      ylafon    609:     }
                    610: 
                    611:     /**
                    612:      * A new client connection has been established.
                    613:      * This method will try to maintain a maximum number of established
                    614:      * connections, by closing idle connections when possible.
                    615:      * @param server The server that has established a new connection.
                    616:      */
                    617: 
                    618:     protected final synchronized void incrConnCount(HttpServer server) {
                    619:        if ( ++conn_count > conn_max )
                    620:            closeAnyConnection();
                    621:        if (debug)
                    622:            System.out.println("+++ incr conn_count: " + conn_count);
                    623:     }
                    624: 
                    625:     /**
                    626:      * Decrement the number of established connections.
                    627:      * @param server The server that has closed one connection to its target.
                    628:      */
                    629: 
                    630:     protected final synchronized void decrConnCount(HttpServer server) {
                    631:        --conn_count;
                    632:        if (debug)
                    633:            System.out.println("+++ decr conn_count: " + conn_count);
1.1       abaird    634:     }
                    635: 
                    636:     /**
                    637:      * Run the given request, in synchronous mode.
                    638:      * This method will launch the given request, and block the calling thread
                    639:      * until the response headers are available.
                    640:      * @param request The request to run.
                    641:      * @return An instance of Reply, containing all the reply 
                    642:      * informations.
1.42      bmahe     643:      * @exception HttpException If something failed during request processing.
1.1       abaird    644:      */
                    645:     
                    646:     public Reply runRequest(Request request)
                    647:        throws HttpException
                    648:     {
1.19      abaird    649:        Reply reply  = null;
                    650:        int   fcalls = 0;
1.1       abaird    651:        // Now run through the ingoing filters:
                    652:        RequestFilter filters[] = filteng.run(request);
                    653:        if ( filters != null ) {
                    654:            for (int i = 0 ; i < filters.length ; i++) {
1.19      abaird    655:                if ((reply = filters[fcalls].ingoingFilter(request)) != null)
                    656:                    break;
                    657:                fcalls++;
1.1       abaird    658:            }
                    659:        }
1.16      abaird    660:        // Locate the appropriate target server:
1.31      abaird    661:        URL target = request.getURL();
1.19      abaird    662:        if ( reply == null ) {
1.32      abaird    663:            HttpServer srv = null;
1.22      ylafon    664:            boolean    rtry ;
1.21      abaird    665:            do {
1.22      ylafon    666:                rtry = false;
1.21      abaird    667:                try {
1.32      abaird    668:                    URL proxy  = request.getProxy();
                    669:                    if ( proxy != null ) 
                    670:                        srv = lookupServer(proxy.getHost(), proxy.getPort());
                    671:                    else
                    672:                        srv = lookupServer(target.getHost(), target.getPort());
1.30      abaird    673:                    request.setServer(srv);
1.21      abaird    674:                    reply = srv.runRequest(request);
                    675:                } catch (HttpException ex) {
                    676:                    for (int i = 0; i < fcalls; i++)
                    677:                        rtry = rtry || filters[i].exceptionFilter(request, ex);
1.23      abaird    678:                    if ( ! rtry )
1.22      ylafon    679:                        throw ex;
1.30      abaird    680:                } finally {
                    681:                    request.unsetServer();
1.21      abaird    682:                }
                    683:            } while (rtry);
1.16      abaird    684:        }
1.1       abaird    685:        // Apply the filters on the way back:
                    686:        if ( filters != null ) {
1.19      abaird    687:            while (--fcalls >= 0) {
                    688:                Reply frep = filters[fcalls].outgoingFilter(request, reply);
                    689:                if ( frep != null ) {
                    690:                    reply = frep;
                    691:                    break;
                    692:                }
1.3       abaird    693:            }
1.1       abaird    694:        }
                    695:        return reply;
                    696:     }
                    697: 
                    698:     /**
                    699:      * Get this manager's reply factory.
                    700:      * The Reply factory is used when prsing incomming reply from servers, it
                    701:      * decides what object will be created to hold the actual reply from the 
                    702:      * server.
                    703:      * @return An object compatible with the MimeParserFactory interface.
                    704:      */
                    705: 
                    706:     MimeParserFactory factory = null ;
                    707: 
1.9       abaird    708:     public MimeParserFactory getReplyFactory() {
1.1       abaird    709:        return factory;
                    710:     }
                    711: 
                    712:     /**
                    713:      * Add a new request filter.
                    714:      * Request filters are called <em>before</em> a request is launched, and
                    715:      * <em>after</em> the reply headers are available. They allow applications
                    716:      * to setup specific request headers (such as PICS, or PEP stuff) on the
                    717:      * way in, and check the reply on the way out.
                    718:      * <p>Request filters are application wide: if their scope matches
                    719:      * the current request, then they will always be aplied.
                    720:      * <p>Filter scopes are defined inclusively and exclusively
1.15      abaird    721:      * @param incs The URL domains for which the filter should be triggered.
                    722:      * @param exs The URL domains for which the filter should not be triggered.
1.1       abaird    723:      * @param filter The request filter to add.
                    724:      */
                    725: 
                    726:     public void setFilter(URL incs[], URL exs[], RequestFilter filter) {
                    727:        if ( incs != null ) {
                    728:            for (int i = 0 ; i < incs.length ; i++)
                    729:                filteng.setFilter(incs[i], true, filter);
                    730:        }
                    731:        if ( exs != null ) {
                    732:            for (int i = 0 ; i < exs.length ; i++)
                    733:                filteng.setFilter(exs[i], false, filter);
                    734:        }
                    735:        return;
                    736:     }
                    737: 
1.15      abaird    738:     /**
                    739:      * Add a global filter.
                    740:      * The given filter will <em>always</em> be invoked.
                    741:      * @param filter The filter to install.
                    742:      */
                    743:     
1.1       abaird    744:     public void setFilter(RequestFilter filter) {
                    745:        filteng.setFilter(filter);
                    746:     }
                    747: 
                    748:     /**
1.15      abaird    749:      * Find back an instance of a global filter.
                    750:      * This methods allow external classes to get a pointer to installed
                    751:      * filters of a given class.
                    752:      * @param cls The class of the filter to look for.
                    753:      * @return A RequestFilter instance, or <strong>null</strong> if not
                    754:      * found.
1.1       abaird    755:      */
                    756: 
1.15      abaird    757:     public RequestFilter getGlobalFilter(Class cls) {
                    758:        return filteng.getGlobalFilter(cls);
1.1       abaird    759:     }
                    760: 
                    761:     /**
                    762:      * Create a new default outgoing request.
                    763:      * This method should <em>always</em> be used to create outgoing requests.
                    764:      * It will initialize the request with appropriate default values for 
                    765:      * the various headers, and make sure that the request is enhanced by
                    766:      * the registered request filters.
                    767:      * @return An instance of Request, suitable to be launched.
                    768:      */
                    769: 
                    770:     public Request createRequest() {
                    771:        return (Request) template.getClone() ;
                    772:     }
                    773: 
                    774:     /**
                    775:      * Global settings - Set the max number of allowed connections.
                    776:      * Set the maximum number of simultaneous connections that can remain
                    777:      * opened. The manager will take care of queuing requests if this number
                    778:      * is reached.
                    779:      * <p>This value defaults to the value of the 
1.38      bmahe     780:      * <code>org.w3c.www.http.maxConnections</code> property.
1.1       abaird    781:      * @param max_conn The allowed maximum simultaneous open connections.
                    782:      */
                    783: 
1.13      abaird    784:     public synchronized void setMaxConnections(int max_conn) {
                    785:        this.conn_max = max_conn;
1.35      ylafon    786:     }
                    787: 
                    788:     /**
                    789:      * Global settings - Set the timeout on the socket
                    790:      *
                    791:      * <p>This value defaults to the value of the 
1.38      bmahe     792:      * <code>org.w3c.www.http.Timeout</code> property.
1.35      ylafon    793:      * @param timeout The allowed maximum microsecond before a timeout.
                    794:      */
                    795: 
                    796:     public synchronized void setTimeout(int timeout) {
                    797:        this.timeout = timeout;
                    798:        Enumeration e = servers.elements();
                    799:        while (e.hasMoreElements()) {
                    800:            ((HttpServer) e.nextElement()).setTimeout(timeout);
                    801:        }
1.62    ! ylafon    802:     }
        !           803: 
        !           804:     /**
        !           805:      * Global settings - set the HTTP parsing lenient or not.
        !           806:      * @param lenient, true by default, false to detect wrong servers
        !           807:      */
        !           808:     public void setLenient(boolean lenient) {
        !           809:        this.lenient = lenient;
        !           810:     }
        !           811: 
        !           812:     /**
        !           813:      * Is this manager parsing headers in a lenient way?
        !           814:      * @return A boolean.
        !           815:      */
        !           816:     public boolean isLenient() {
        !           817:        return lenient;
1.1       abaird    818:     }
                    819: 
                    820:     /**
                    821:      * Global settings - Set an optional proxy to use.
                    822:      * Set the proxy to which all requests should be targeted. If the
1.38      bmahe     823:      * <code>org.w3c.www.http.proxy</code> property is defined, it will be
1.1       abaird    824:      * used as the default value.
                    825:      * @param proxy The URL for the proxy to use.
                    826:      */
                    827: 
                    828:     public void setProxy(URL proxy) {
1.5       abaird    829:        template.setProxy(proxy);
1.30      abaird    830:     }
                    831: 
                    832:     /**
                    833:      * Does this manager uses a proxy to fulfill requests ?
                    834:      * @return A boolean.
                    835:      */
                    836: 
                    837:     public boolean usingProxy() {
                    838:        return template.hasProxy();
1.1       abaird    839:     }
                    840: 
                    841:     /**
                    842:      * Global settings - Set the request timeout.
                    843:      * Once a request has been emited, the HttpManager will sit for this 
                    844:      * given number of milliseconds before the request is declared to have
                    845:      * timed-out.
                    846:      * <p>This timeout value defaults to the value of the
1.38      bmahe     847:      * <code>org.w3c.www.http.requestTimeout</code> property value.
1.1       abaird    848:      * @param ms The timeout value in milliseconds.
                    849:      */
                    850: 
                    851:     public void setRequestTimeout(int ms) {
                    852:     }
                    853: 
                    854:     /**
                    855:      * Global settings - Define a global request header.
                    856:      * Set a default value for some request header. Once defined, the
                    857:      * header will automatically be defined on <em>all</em> outgoing requests
                    858:      * created through the <code>createRequest</code> request.
                    859:      * @param name The name of the header, case insensitive.
                    860:      * @param value It's default value.
                    861:      */
                    862:     
                    863:     public void setGlobalHeader(String name, String value) {
                    864:        template.setValue(name, value);
                    865:     }
                    866: 
1.18      abaird    867:     /**
                    868:      * Global settings - Get a global request header default value.
                    869:      * @param name The name of the header to get.
                    870:      * @return The value for that header, as a String, or <strong>
                    871:      * null</strong> if undefined.
                    872:      */
                    873: 
1.1       abaird    874:     public String getGlobalHeader(String name) {
                    875:        return template.getValue(name);
1.18      abaird    876:     }
                    877: 
                    878:     
                    879:     /**
                    880:      * Dump all in-memory cached state to persistent storage.
                    881:      */
                    882: 
                    883:     public void sync() {
                    884:        filteng.sync();
1.1       abaird    885:     }
                    886: 
                    887:     /**
                    888:      * Create a new HttpManager.
1.33      abaird    889:      * FIXME Making this method protected breaks the static method
                    890:      * to create HttpManager instances (should use a factory here)
1.1       abaird    891:      * @param props The properties from which the manager should initialize 
                    892:      * itself, or <strong>null</strong> if none are available.
                    893:      */
                    894: 
1.33      abaird    895:     protected HttpManager() {
1.9       abaird    896:        this.template       = new Request(this);
                    897:        this.servers        = new Hashtable();
                    898:        this.filteng        = new FilterEngine();
                    899:        this.connectionsLru = new SyncLRUList();
1.1       abaird    900:     }
                    901: 
                    902:     /**
                    903:      * DEBUGGING !
                    904:      */
                    905: 
                    906:     public static void main(String args[]) {
                    907:        try {
                    908:            // Get the manager, and define some global headers:
                    909:            HttpManager manager = HttpManager.getManager();
                    910:            manager.setGlobalHeader("User-Agent", "Jigsaw/1.0a");
                    911:            manager.setGlobalHeader("Accept", "*/*;q=1.0");
                    912:            manager.setGlobalHeader("Accept-Encoding", "gzip");
1.34      bmahe     913:            PropRequestFilter filter = 
1.38      bmahe     914:              new org.w3c.www.protocol.http.cookies.CookieFilter();
1.34      bmahe     915:            filter.initialize(manager);
1.45      ylafon    916:            PropRequestFilter pdebug = 
1.38      bmahe     917:              new org.w3c.www.protocol.http.DebugFilter();
1.45      ylafon    918:            pdebug.initialize(manager);
1.1       abaird    919:            Request request = manager.createRequest();
                    920:            request.setURL(new URL(args[0]));
                    921:            request.setMethod("GET");
                    922:            Reply       reply   = manager.runRequest(request);
1.34      bmahe     923:            //Display some infos:
1.1       abaird    924:            System.out.println("last-modified: "+reply.getLastModified());
                    925:            System.out.println("length       : "+reply.getContentLength());
                    926:            // Display the returned body:
                    927:            InputStream in = reply.getInputStream();
                    928:            byte buf[] = new byte[4096];
                    929:            int  cnt   = 0;
                    930:            while ((cnt = in.read(buf)) > 0) 
1.56      ylafon    931:              System.out.print(new String(buf, 0, cnt));
1.1       abaird    932:            System.out.println("-");
                    933:            in.close();
1.34      bmahe     934:            manager.sync();
1.1       abaird    935:        } catch (Exception ex) {
                    936:            ex.printStackTrace();
                    937:        }
                    938:        System.exit(1);
                    939:     }
                    940: }
1.34      bmahe     941: 
                    942: 
                    943: 

Webmaster