提交 fab0938e 编写于 作者: K Kohsuke Kawaguchi 提交者: GitHub

Merge pull request #2496 from jenkinsci/JENKINS-37223

[FIXED JENKINS-37223]
......@@ -23,6 +23,9 @@
*/
package hudson;
import java.io.ByteArrayInputStream;
import java.io.SequenceInputStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.security.interfaces.RSAPublicKey;
import javax.annotation.Nullable;
......@@ -50,7 +53,9 @@ import java.nio.channels.ServerSocketChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.lang.StringUtils;
/**
......@@ -202,7 +207,19 @@ public final class TcpSlaveAgentListener extends Thread {
new BufferedWriter(new OutputStreamWriter(s.getOutputStream(),"UTF-8")),
true); // DEPRECATED: newer protocol shouldn't use PrintWriter but should use DataOutputStream
String s = in.readUTF();
// peek the first few bytes to determine what to do with this client
byte[] head = new byte[10];
in.readFully(head);
String header = new String(head, Charsets.US_ASCII);
if (header.startsWith("GET ")) {
// this looks like an HTTP client
respondHello(header,s);
return;
}
// otherwise assume this is AgentProtocol and start from the beginning
String s = new DataInputStream(new SequenceInputStream(new ByteArrayInputStream(head),in)).readUTF();
if(s.startsWith("Protocol:")) {
String protocol = s.substring(9);
......@@ -235,6 +252,49 @@ public final class TcpSlaveAgentListener extends Thread {
}
}
/**
* Respond to HTTP request with simple diagnostics.
* Primarily used to test the low-level connectivity.
*/
private void respondHello(String header, Socket s) throws IOException {
try {
Writer o = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
if (header.startsWith("GET / ")) {
o.write("HTTP/1.0 200 OK\r\n");
o.write("Content-Type: text/plain;charset=UTF-8\r\n");
o.write("\r\n");
o.write("Jenkins-Agent-Protocols: ");
boolean first = true;
for (AgentProtocol p : AgentProtocol.all()) {
if (first) first = false;
else o.write(",");
o.write(p.getName());
}
o.write("\r\n");
o.write("Jenkins-Version: " + Jenkins.VERSION + "\r\n");
o.write("Jenkins-Session: " + Jenkins.SESSION_HASH + "\r\n");
o.write("Client: " + s.getInetAddress().getHostAddress() + "\r\n");
o.write("Server: " + s.getLocalAddress().getHostAddress() + "\r\n");
o.flush();
s.shutdownOutput();
} else {
o.write("HTTP/1.0 404 Not Found\r\n");
o.write("Content-Type: text/plain;charset=UTF-8\r\n");
o.write("\r\n");
o.write("Not Found\r\n");
o.flush();
s.shutdownOutput();
}
InputStream i = s.getInputStream();
IOUtils.copy(i, new NullOutputStream());
s.shutdownInput();
} finally {
s.close();
}
}
private void error(PrintWriter out, String msg) throws IOException {
out.println(msg);
LOGGER.log(Level.WARNING,"Connection #"+id+" is aborted: "+msg);
......
......@@ -2,8 +2,11 @@ package hudson;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.TextPage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.remoting.Base64;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
......@@ -14,14 +17,17 @@ import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import javax.annotation.Nullable;
import jenkins.model.Jenkins;
import jenkins.model.identity.InstanceIdentityProvider;
import jenkins.security.security218.ysoserial.ExecBlockingSecurityManager.ExecException;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.TestExtension;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
......@@ -43,4 +49,21 @@ public class TcpSlaveAgentListenerTest {
Page p = r.createWebClient().goTo("tcpSlaveAgentListener", "text/plain");
assertThat(p.getWebResponse().getResponseHeaderValue("X-Instance-Identity"), notNullValue());
}
@Test
public void diagnostics() throws Exception {
r.getInstance().setSlaveAgentPort(0);
int p = r.jenkins.getTcpSlaveAgentListener().getPort();
WebClient wc = r.createWebClient();
TextPage text = (TextPage) wc.getPage("http://localhost:"+p+"/");
String c = text.getContent();
assertThat(c,containsString(Jenkins.VERSION));
try {
wc.getPage("http://localhost:"+p+"/xxx");
fail("Expected 404");
} catch (FailingHttpStatusCodeException e) {
assertThat(e.getStatusCode(),equalTo(404));
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册