Skip to content

Commit

Permalink
[#11057] Fix getInputStream() loop in jdk http plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jaehong-kim committed May 23, 2024
1 parent 63aa876 commit e474a15
Show file tree
Hide file tree
Showing 18 changed files with 504 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-agent-testweb-commons</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@
import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
Expand All @@ -40,6 +44,13 @@
import reactor.netty.http.client.HttpClient;
import reactor.netty.http.client.HttpClientRequest;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Comparator;
Expand Down Expand Up @@ -254,4 +265,121 @@ public Mono<String> clientReactorTimeout(ServerWebExchange exchange) {

return Mono.just("OK");
}
}

@GetMapping("/client/resttemplate")
public Mono<String> clientRestTemplate(ServerWebExchange exchange) {
RestTemplate restTemplate = new RestTemplate();
String fooResourceUrl
= "http://httpbin.org";
ResponseEntity<String> response
= restTemplate.getForEntity(fooResourceUrl + "/", String.class);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());

return Mono.just("OK");
}

@GetMapping("/client/resttemplate/https")
public Mono<String> clientRestTemplateHttps(ServerWebExchange exchange) {
RestTemplate restTemplate = new RestTemplate();
String fooResourceUrl
= "https://naver.com";
ResponseEntity<String> response
= restTemplate.getForEntity(fooResourceUrl + "/", String.class);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());

return Mono.just("OK");
}

@RequestMapping(value = "/jdk/connect")
public String get() {

final URL url = newURL("http://httpbin.org");
try {
URLConnection connection = url.openConnection();
connection.connect();
} catch (IOException e) {
return "fail";
}

return "OK";
}
@RequestMapping(value = "/jdk/connect/https")
public String getHttps() {

final URL url = newURL("https://naver.com");
try {
URLConnection connection = url.openConnection();
connection.connect();
} catch (IOException e) {
return "fail";
}

return "OK";
}
@RequestMapping(value = "/jdk/connect/duplicated")
public String getDuplicated() {

final URL url = newURL("http://httpbin.org");
try {
URLConnection connection = url.openConnection();
connection.connect();
connection.connect();
} catch (IOException e) {
return "fail";
}

return "OK";
}

@RequestMapping(value = "/jdk/connect2")
public String get2() {

final URL url = newURL("http://httpbin.org");
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();

final int responseCode = connection.getResponseCode();
final List<String> contents = readStream(connection);
} catch (IOException e) {
return "fail";
}


return "OK";
}

@RequestMapping(value = "/jdk/connect2/https")
public String get2Https() {

final URL url = newURL("https://naver.com");
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();

final int responseCode = connection.getResponseCode();
final List<String> contents = readStream(connection);
} catch (IOException e) {
return "fail";
}


return "OK";
}

private List<String> readStream(HttpURLConnection connection) throws IOException {
try (InputStream inputStream = connection.getInputStream()) {
return IOUtils.readLines(inputStream, StandardCharsets.UTF_8);
}
}

private URL newURL(String spec) {
try {
return new URL(spec);
} catch (MalformedURLException exception) {
throw new IllegalArgumentException("invalid url" + spec, exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.net.UnknownHostException;

import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.annotation;
import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.anyAnnotationValue;
import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.event;
import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.notNull;

Expand Down Expand Up @@ -71,14 +70,11 @@ public void test() throws Exception {
verifier.printMethod();

Class<?> targetClass = Class.forName("sun.net.www.protocol.http.HttpURLConnection");
Method getInputStream = targetClass.getMethod("getInputStream");
Method getInputStream = targetClass.getMethod("connect");

String destinationId = webServer.getHostAndPort();
String httpUrl = webServer.getCallHttpUrl();
verifier.verifyTraceCount(2);
verifier.verifyTrace(event("JDK_HTTPURLCONNECTOR", getInputStream, null, null, destinationId,
annotation("http.url", httpUrl),
annotation("http.resp.header", anyAnnotationValue())));
}

@Test
Expand All @@ -94,16 +90,13 @@ public void testConnectTwice() throws Exception {

Class<?> targetClass = Class.forName("sun.net.www.protocol.http.HttpURLConnection");
Method connect = targetClass.getMethod("connect");
Method getInputStream = targetClass.getMethod("getInputStream");
Method getInputStream = targetClass.getMethod("getHeaderField", int.class);

String destinationId = webServer.getHostAndPort();
String httpUrl = webServer.getCallHttpUrl();
verifier.verifyTraceCount(2);
verifier.verifyTraceCount(3);
verifier.verifyTrace(event("JDK_HTTPURLCONNECTOR", connect, null, null, destinationId,
annotation("http.url", httpUrl)));
verifier.verifyTrace(event("JDK_HTTPURLCONNECTOR", getInputStream, null, null, destinationId,
annotation("http.url", httpUrl),
annotation("http.resp.header", anyAnnotationValue())));
}

@Test
Expand Down Expand Up @@ -133,10 +126,9 @@ public void testConnecting() throws Exception {
Class<?> targetClass = Class.forName("sun.net.www.protocol.http.HttpURLConnection");
Method getInputStream = targetClass.getMethod("connect");

verifier.verifyTrace(event("JDK_HTTPURLCONNECTOR", getInputStream, null, null, "no.such.url", notNull(""), annotation("http.url", "http://no.such.url")));
verifier.verifyTrace(event("JDK_HTTPURLCONNECTOR", getInputStream, null, null, "no.such.url", notNull(""), annotation("http.url", "http://no.such.url")));

verifier.printMethod();
verifier.verifyTraceCount(0);
verifier.verifyTraceCount(2);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
*/
public class JdkHttpClientRequestAdaptor implements ClientRequestAdaptor<HttpURLConnection> {


public JdkHttpClientRequestAdaptor() {
}


@Override
public String getDestinationId(HttpURLConnection httpURLConnection) {
final URL url = httpURLConnection.getURL();
Expand All @@ -39,12 +37,12 @@ public String getDestinationId(HttpURLConnection httpURLConnection) {
final int port = url.getPort();
return getEndpoint(host, port);
}
return "Unknown";
return "UNKNOWN";
}

public static String getEndpoint(final String host, final int port) {
String getEndpoint(final String host, final int port) {
if (host == null) {
return "Unknown";
return "UNKNOWN";
}
if (port < 0) {
return host;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ public boolean containsHeader(HttpURLConnection response, String name) {

@Override
public void setHeader(HttpURLConnection response, String name, String value) {

}

@Override
public void addHeader(HttpURLConnection response, String name, String value) {

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ private JdkHttpConstants() {
}

public static final ServiceType SERVICE_TYPE = ServiceTypeFactory.of(9055, "JDK_HTTPURLCONNECTOR", "JDK_HTTPCONNECTOR", RECORD_STATISTICS);
public static final ServiceType SERVICE_TYPE_INTERNAL = ServiceTypeFactory.of(9067, "JDK_HTTPURLCONNECTOR_INTERNAL", "JDK_HTTPCONNECTOR");

public static final String TRACE_SCOPE_NAME_GET_INPUT_STREAM = "_GetInputStream";
}
Loading

0 comments on commit e474a15

Please sign in to comment.