📄 httpdestination.java
字号:
catch (InterruptedException e) { Log.ignore(e); } } } /* ------------------------------------------------------------------------------- */ public void onException(Throwable throwable) { synchronized (this) { _pendingConnections--; if (_queue.size()>0) { HttpExchange ex=_queue.removeFirst(); ex.getEventListener().onException(throwable); ex.setStatus(HttpExchange.STATUS_EXCEPTED); } } } /* ------------------------------------------------------------------------------- */ public void onNewConnection(HttpConnection connection) throws IOException { HttpConnection q_connection=null; synchronized (this) { _pendingConnections--; _connections.add(connection); if (_newConnection>0) { q_connection=connection; _newConnection--; } else if (_queue.size()==0) { _idle.add(connection); } else { HttpExchange ex=_queue.removeFirst(); connection.send(ex); } } if (q_connection!=null) { try { _newQueue.put(q_connection); } catch (InterruptedException e) { Log.ignore(e); } } } /* ------------------------------------------------------------------------------- */ public void returnConnection(HttpConnection connection, boolean close) throws IOException { if (connection.isReserved()) connection.setReserved(false); if (close) { try { connection.close(); } catch(IOException e) { Log.ignore(e); } } if (!_client.isStarted()) return; if (!close && connection.getEndPoint().isOpen()) { synchronized (this) { if (_queue.size()==0) { connection.setLast(System.currentTimeMillis()); _idle.add(connection); } else { HttpExchange ex = _queue.removeFirst(); connection.send(ex); } this.notifyAll(); } } else { synchronized (this) { _connections.remove(connection); if (!_queue.isEmpty()) startNewConnection(); } } } /* ------------------------------------------------------------ */ public void send(HttpExchange ex) throws IOException { LinkedList<String> listeners = _client.getRegisteredListeners(); if (listeners != null) { // Add registered listeners, fail if we can't load them for (int i = listeners.size(); i > 0; --i) { String listenerClass = listeners.get(i - 1); try { Class listener = Class.forName(listenerClass); Constructor constructor = listener.getDeclaredConstructor(HttpDestination.class, HttpExchange.class); HttpEventListener elistener = (HttpEventListener) constructor.newInstance(this, ex); ex.setEventListener(elistener); } catch (Exception e) { Log.debug(e); throw new IOException("Unable to instantiate registered listener for destination: " + listenerClass ); } } } // Security is supported by default and should be the first consulted if ( _client.hasRealms() ) { ex.setEventListener( new SecurityListener( this, ex ) ); } doSend(ex); } /* ------------------------------------------------------------ */ public void resend(HttpExchange ex) throws IOException { ex.getEventListener().onRetry(); doSend(ex); } /* ------------------------------------------------------------ */ protected void doSend(HttpExchange ex) throws IOException { // add cookies // TODO handle max-age etc. if (_cookies!=null) { StringBuilder buf=null; for (Cookie cookie : _cookies) { if (buf==null) buf=new StringBuilder(); else buf.append("; "); buf.append(cookie.getName()); // TODO quotes buf.append("="); buf.append(cookie.getValue()); // TODO quotes } if (buf!=null) ex.addRequestHeader(HttpHeaders.COOKIE,buf.toString()); } // Add any known authorizations if (_authorizations!=null) { Authorization auth= (Authorization)_authorizations.match(ex.getURI()); if (auth !=null) ((Authorization)auth).setCredentials(ex); } synchronized(this) { //System.out.println( "Sending: " + ex.toString() ); HttpConnection connection=null; if (_queue.size()>0 || (connection=getIdleConnection())==null || !connection.send(ex)) { _queue.add(ex); if (_connections.size()+_pendingConnections <_maxConnections) { startNewConnection(); } } } } /* ------------------------------------------------------------ */ public synchronized String toString() { return "HttpDestination@" + hashCode() + "//" + _address.getHost() + ":" + _address.getPort() + "(" + _connections.size() + "," + _idle.size() + "," + _queue.size() + ")"; } /* ------------------------------------------------------------ */ public synchronized String toDetailString() { StringBuilder b = new StringBuilder(); b.append(toString()); b.append('\n'); synchronized(this) { for (HttpConnection connection : _connections) { if (connection._exchange!=null) { b.append(connection.toDetailString()); if (_idle.contains(connection)) b.append(" IDLE"); b.append('\n'); } } } b.append("--"); b.append('\n'); return b.toString(); } /* ------------------------------------------------------------ */ public void setProxy(Address proxy) { _proxy=proxy; } /* ------------------------------------------------------------ */ public Address getProxy() { return _proxy; } /* ------------------------------------------------------------ */ public Authorization getProxyAuthentication() { return _proxyAuthentication; } /* ------------------------------------------------------------ */ public void setProxyAuthentication(Authorization authentication) { _proxyAuthentication = authentication; } /* ------------------------------------------------------------ */ public boolean isProxied() { return _proxy!=null; } /* ------------------------------------------------------------ */ public void close() throws IOException { synchronized (this) { for (HttpConnection connection : _connections) { connection.close(); } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -