📄 httptransport.java
字号:
" for reply" );
else
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "Client GET Message sent, no reply wanted" );
// get the reply if we were asked to.
if ( wantReply ) {
String response = getResult(ip, (type == BlockingSend) );
if (response != null) {
String httpHeader = getHttpHeader( response );
// Strip <HTML> tags
int indexFirst = response.indexOf("<HTML>\n", httpHeader.length() );
int indexLast = response.lastIndexOf("</HTML>");
if ((indexFirst >= 0) && (indexLast > 0)) {
reply = response.substring(indexFirst + 7, indexLast);
} else {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "Client GET got invalid reply" );
}
} else {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Client GET wanted reply, but didn't get one");
}
}
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("HttpGet to '" + url + "' failed ", e);
} finally {
closeSocket( socket, ip, op );
}
return reply;
}
/**
* Open a socket given a URL.
* This opens a socket to the proxy as needed.
*
* @param url the URL to process
* @return Socket which was opened for the URL
**/
private Socket openSocket( URL url ) {
Socket socket = null;
String hostName = null;
int port = -1 ;
// process the url to get host name and port
try {
String peerName = null;
String cmd = null;
// Should we talk to the proxy or go direct
if (proxyHost != null) {
// via proxy
hostName = proxyHost.getHost();
port = proxyHost.getPort();
cmd = "n/a";
peerName = "n/a";
} else {
// going direct
hostName = url.getHost();
port = url.getPort();
port = (-1 == port) ? 80 : port;
String path = url.getFile();
// find end of command part
int indexStart = path.indexOf( '/', 1 );
// what is command ?
cmd = path.substring( 1, indexStart );
// find the clientid
indexStart = path.indexOf("//", indexStart + 1 ) ;
int indexStop = path.indexOf("/", indexStart + 2 );
if( (-1 == indexStart) || (-1 == indexStop) )
peerName = "n/a";
else
peerName = path.substring(indexStart + 2, indexStop);
}
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("openSocket to " + hostName + ":" + port +
" (cmd='" + cmd + "' clientId='" + peerName + "')" );
} catch ( Exception urlProcess ) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn( "openSocket URL processing failed. ", urlProcess );
}
// Resolve the hostName to an addr
InetAddress addr = null;
try {
addr = InetAddress.getByName( hostName );
} catch ( UnknownHostException notKnown ) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn( "cannot resolve (DNS) " + hostName );
return null;
}
if (addr == null) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("host name '" + hostName + "' resolved to localhost" );
return null;
}
// Open the socket
try {
socket = new Socket( addr, port, usingInterface, 0 );
} catch( ConnectException connectFailure ) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn( "Connect to " + addr.getHostAddress() + ":" + port +
" on interface " + usingInterface.getHostAddress() +
" failed : " + connectFailure.toString() );
return null;
} catch ( Exception generalFailure ) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn( "Connect to " + addr.getHostAddress() + ":" + port +
" on interface " + usingInterface.getHostAddress() +
" failed : " + generalFailure.toString() );
return null;
}
// Set socket options
try {
socket.setSoLinger(true, LingerDelay);
socket.setSoTimeout(SocketTimeout);
} catch (Exception e) {
// These are options so we don't fail if they cannot be set.
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("setting socket options failed ", e);
}
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "Connected to " +
socket.getInetAddress().getHostAddress() + ":" +
socket.getPort() +
" on interface " +
socket.getLocalAddress().getHostAddress() + ":" +
socket.getLocalPort() );
return socket;
}
private HttpMessage register(String clientId) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Registering client " + clientId);
// First create spool directory for this new client
// XXX: some security will have to be checked here.
// That will be done when the general security model
// will be completed. lomax@jxta.org
if (initDir(HttpSpool + File.separatorChar + clientId)) {
// Cleanup the mailbox first.
cleanupClient (clientId);
return HttpMessage.OKMESSAGE;
} else {
return HttpMessage.FAILEDMESSAGE;
}
}
private synchronized void recordConnection(String server,
ServerConnection conn) {
try {
servers.put(server, conn);
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("recordConnection failed", e);
}
}
private synchronized void forgetConnection(String server) {
try {
servers.remove(server);
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("forgetConnection failed", e);
}
}
private synchronized boolean connectedServer(String server) {
Object conn = null;
conn = servers.get(server);
return conn != null;
}
/**
* Send an HTTP response on the specified output stream. For responses with
* no content, len may be zero and buffers null.
*
* FIXME 20010919 bondolo@jxta.org It might be nice to send
* response codes other than "200 OK".
*
* @param outputStream The stream we are going to send the response on.
* @param buffers A vector of byte arrays containing the data for the entity
* being transmitted. May be null iff <code>len</code> is zero
* @param len The length of the entity to be included with the response.
* <code>buffers</code> should contain exactly this many bytes.
* @return boolean true if the response was sent otherwise false.
**/
static boolean sendResponse(OutputStream outputStream, Vector buffers, long len ) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("sendResponse starts");
if( len < 0 ) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "len must be >=0" );
throw new IllegalArgumentException( "len must be >=0" );
}
if ( (len != 0) && (buffers == null) ) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "buffers cannot be null for non-zero len response" );
throw new IllegalArgumentException( "buffers cannot be null for non-zero len response" );
}
if( null == outputStream ) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "outputstream cannot be null" );
throw new IllegalArgumentException( "outputstream cannot be null" );
}
if( null == buffers ) {
buffers = new Vector( 1 );
}
try {
int contentLen = (int) len;
// if there is an entity, wrap it.
if( len > 0 ) {
contentLen += 16;
buffers.insertElementAt( "<HTML>\n".getBytes(), 0 );
buffers.addElement( "\n</HTML>\n".getBytes() );
}
// build the header and insert it.
String reponseHeader = "HTTP/1.0 200 OK\r\n" +
"Content-type: application/octet-stream\r\n" +
"Content-length: " + Integer.toString( contentLen ) + "\r\n" +
"\r\n";
buffers.insertElementAt( reponseHeader.getBytes(), 0 );
for (int i = 0; i < buffers.size(); ++i) {
outputStream.write((byte[]) buffers.elementAt(i));
}
outputStream.flush();
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Finished response");
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("sendResponse exception ", e);
return false;
}
return true;
}
/**
* Handle cleanup for a socket by flushing the outputstream and closing it,
* closing the inputstream and closing the socket.
*
* @param inputSocket the socket to close
* @param inputStream its associated inputStream
* @param outputStream its associated outputStream
*
**/
static void closeSocket(Socket inputSocket, InputStream inputStream, OutputStream outputStream) {
if (outputStream != null) {
try {
outputStream.flush();
outputStream.close();
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("closeSocket [1] failed.", e);
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("closeSocket [2] failed.", e);
}
}
if (inputSocket != null) {
try {
inputSocket.close();
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("closeSokcet [3] failed.", e);
}
}
}
/**
* Get the next outgoing message for the client named by clientId
* @param clientId the client we are sending to.
* @param blocking is there a persistent connection to peer?
* @param inputSocket the socket of the persistent connection
* @param inputStream the inputstream for the persistent connection
*
**/
private HttpMessage processOutgoing(String clientId,
boolean blocking,
Socket inputSocket,
InputStream inputStream) {
OutputStream outputStream = null;
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("processOutgoing for client " + clientId );
try {
outputStream = inputSocket.getOutputStream();
} catch( IOException failed ) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn( "couldnt get outputstream for response" );
return null;
}
HttpServer.ClientConnection client = null;
if (!blocking) {
// The client is explicitily requesting to run the "rec" protocol.
// Remove the connection and close as necessary
client = server.getClientConnection(clientId);
if( null != client ) {
client.close();
client = null;
}
} else {
// establish a connection for this peer.
// We either have never had a previous connection to this peer or
// somehow the connection got dropped. Regardless, a new connection
// has been requestsed.
server.addClientConnection(clientId,
inputSocket,
inputStream,
outputStream );
}
// First see if there is a pending in the spooler.
String dn = HttpSpool + File.separatorChar + clientId;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -