3737import com .sun .jna .platform .win32 .WinNT ;
3838import com .sun .jna .ptr .IntByReference ;
3939
40+ import okhttp3 .HttpUrl ;
41+ import okhttp3 .OkHttpClient ;
42+ import okhttp3 .Request ;
43+ import okhttp3 .Response ;
44+ import okhttp3 .logging .HttpLoggingInterceptor ;
45+ import okhttp3 .logging .LoggingEventListener ;
46+ import okio .Timeout ;
47+
4048class NamedPipeSocketTest {
4149
4250 @ Test
@@ -49,6 +57,111 @@ void canConnect() throws IOException {
4957 }
5058 }
5159
60+ @ Test
61+ @ EnabledOnOs (OS .WINDOWS )
62+ void canPingViaSocket () throws IOException {
63+ try (NamedPipeSocket namedPipeSocket = new NamedPipeSocket ()) {
64+ namedPipeSocket .connect (new InetSocketAddress (
65+ getByAddress (namedPipeSocket .encodeHostname ("//./pipe/docker_engine" ), new byte []{0 , 0 , 0 , 0 }),
66+ 0 ));
67+
68+ OutputStream os = namedPipeSocket .getOutputStream ();
69+
70+ List <String > headers = new ArrayList <>();
71+ headers .add ("GET /_ping HTTP/1.1" );
72+ headers .add ("Content-Type: application/json" );
73+ headers .add ("Accept: application/json" );
74+ // headers.add("Host: localhost");
75+ headers .add ("Host: 2f2f2e2f706970652f646f636b65725f656e67696e65.socket" );
76+ headers .add ("Connection: Keep-Alive" );
77+ headers .add ("Accept-Encoding: gzip" );
78+ headers .add ("User-Agent: NamedPipeSocketTest" );
79+
80+ try {
81+ for (String line : headers ) {
82+ System .out .println (">>|" + line );
83+ os .write (line .getBytes (StandardCharsets .UTF_8 ));
84+ os .write ("\n " .getBytes (StandardCharsets .UTF_8 ));
85+ }
86+ os .write ("\n " .getBytes (StandardCharsets .UTF_8 ));
87+ os .flush ();
88+ System .out .println ("[TEST] Sent request headers" );
89+ Thread .sleep (2000 );
90+ } catch (IOException e ) {
91+ e .printStackTrace ();
92+ } catch (InterruptedException e ) {
93+ e .printStackTrace ();
94+ }
95+
96+ InputStream is = namedPipeSocket .getInputStream ();
97+ Thread readerThread = new Thread (() -> {
98+ try {
99+ byte [] buffer = new byte [8024 ];
100+ int bytesRead ;
101+ while ((bytesRead = is .read (buffer )) != -1 ) {
102+ System .out .println ("Read: " + new String (buffer , 0 , bytesRead , StandardCharsets .UTF_8 ));
103+ }
104+ Thread .sleep (500 );
105+ } catch (Exception e ) {
106+ throw new RuntimeException (e );
107+ }
108+ });
109+ readerThread .start ();
110+
111+ try {
112+ Thread .sleep (1000 );
113+ } catch (InterruptedException e ) {
114+ e .printStackTrace ();
115+ } finally {
116+ os .close ();
117+ }
118+ System .out .println ("Closing reader" );
119+ if (is != null ) {
120+ try {
121+ is .close ();
122+ } catch (IOException ignored ) {
123+ }
124+ }
125+ }
126+ }
127+
128+ @ Test
129+ @ EnabledOnOs (OS .WINDOWS )
130+ void canPingViaOkHttp () throws IOException {
131+ NamedPipeSocketFactory namedPipeSocketFactory = new NamedPipeSocketFactory ();
132+ OkHttpClient .Builder builder = new OkHttpClient .Builder ()
133+ .socketFactory (namedPipeSocketFactory )
134+ .dns (namedPipeSocketFactory )
135+ .connectTimeout (new Timeout ().timeout (10 , TimeUnit .SECONDS ).timeoutNanos () / 1000 , TimeUnit .MILLISECONDS )
136+ .callTimeout (new Timeout ().timeout (30 , TimeUnit .SECONDS ).timeoutNanos () / 1000 , TimeUnit .MILLISECONDS )
137+ .readTimeout (new Timeout ().timeout (30 , TimeUnit .SECONDS ).timeoutNanos () / 1000 , TimeUnit .MILLISECONDS )
138+ .writeTimeout (new Timeout ().timeout (30 , TimeUnit .SECONDS ).timeoutNanos () / 1000 , TimeUnit .MILLISECONDS )
139+ .addInterceptor (new HttpLoggingInterceptor ().setLevel (HttpLoggingInterceptor .Level .BODY ))
140+ .eventListenerFactory (new LoggingEventListener .Factory ());
141+
142+ HttpUrl url = new HttpUrl .Builder ()
143+ .scheme ("http" )
144+ .host (new NamedPipeSocket ().encodeHostname ("//./pipe/docker_engine" ))
145+ .addPathSegments ("_ping" )
146+ .build ();
147+
148+ Request request = new Request .Builder ()
149+ .get ()
150+ .url (url )
151+ .addHeader ("Content-Type" , "application/json" )
152+ .addHeader ("Accept" , "application/json" )
153+ .build ();
154+
155+ OkHttpClient client = builder .build ();
156+ Response response = client .newCall (request ).execute ();
157+ if (!response .isSuccessful ()) {
158+ response .close ();
159+ } else {
160+ String content = response .toString ();
161+ System .out .println ("Got: " + content );
162+ }
163+ }
164+
52165 @ Test
53166 @ EnabledOnOs (OS .WINDOWS )
54167 public void testInProcessServerClient () throws Exception {
@@ -253,6 +366,7 @@ void testPlainHijacked() throws Exception {
253366
254367 Thread readerThread = new Thread (() -> {
255368 try (InputStream inputStream = namedPipeSocket .getInputStream ()) {
369+ Thread .sleep (1000 );
256370 byte [] buf = new byte [1024 ];
257371 int read ;
258372 while ((read = inputStream .read (buf )) != -1 ) {
0 commit comments