From 5edc2a0bb3539ba386d9927739210b4ef5edcbea Mon Sep 17 00:00:00 2001 From: WalkerWatch Date: Fri, 29 Jul 2016 16:40:25 -0400 Subject: [PATCH] Chapter 22 revisions and misc adoc updates. Signed-off-by: WalkerWatch --- .../administration/jmx/jetty-jconsole.adoc | 2 +- .../contexts/temporary-directories.adoc | 4 +- .../clients/http/http-client-api.adoc | 190 +++++------------- .../http/http-client-authentication.adoc | 28 +-- .../clients/http/http-client-cookie.adoc | 19 +- .../clients/http/http-client-intro.adoc | 82 +++----- .../clients/http/http-client-proxy.adoc | 30 +-- .../clients/http/http-client-transport.adoc | 42 ++-- 8 files changed, 122 insertions(+), 275 deletions(-) diff --git a/jetty-documentation/src/main/asciidoc/administration/jmx/jetty-jconsole.adoc b/jetty-documentation/src/main/asciidoc/administration/jmx/jetty-jconsole.adoc index dc9f56919ee..db859005ebd 100644 --- a/jetty-documentation/src/main/asciidoc/administration/jmx/jetty-jconsole.adoc +++ b/jetty-documentation/src/main/asciidoc/administration/jmx/jetty-jconsole.adoc @@ -20,7 +20,7 @@ JConsole and the Java Mission Control (JMX) are graphical tools; they allow you to remotely manage and monitor your server and web application status using JMX. When following the instructions given below, please also ensure that you make any necessary changes to any anti-virus software you may be using which may prevent JConsole or JMC from running. -===== Starting Jetty Standalone +==== Starting Jetty Standalone The simplest way to enable support is to add the JMX-Remote support module to your `{$jetty.base}`. diff --git a/jetty-documentation/src/main/asciidoc/configuring/contexts/temporary-directories.adoc b/jetty-documentation/src/main/asciidoc/configuring/contexts/temporary-directories.adoc index c91cb3e9617..327fb69b5cd 100644 --- a/jetty-documentation/src/main/asciidoc/configuring/contexts/temporary-directories.adoc +++ b/jetty-documentation/src/main/asciidoc/configuring/contexts/temporary-directories.adoc @@ -77,7 +77,7 @@ context.setAttribute("org.eclipse.jetty.webapp.basetempdir", "/tmp/foo"); There are several ways to use a particular directory as the temporary directory: -====== Call WebAppContext.setTempDirectory(String dir) +===== Call WebAppContext.setTempDirectory(String dir) As before this can be accomplished with an xml file or directly in code. Here's an example of setting the temp directory in xml: [source, xml, subs="{sub-order}"] @@ -101,7 +101,7 @@ context.setWar("foo.war"); context.setTempDirectory(new File("/some/dir/foo")); ---- -====== Set the javax.servlet.context.tempdir context attribute +===== Set the javax.servlet.context.tempdir context attribute You should set this context attribute with the name of directory you want to use as the temp directory. Again, you can do this in xml: [source, xml, subs="{sub-order}"] diff --git a/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-api.adoc b/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-api.adoc index 1f8a8cee716..7dd06439e88 100644 --- a/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-api.adoc +++ b/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-api.adoc @@ -20,25 +20,19 @@ [[http-client-blocking]] ==== Blocking APIs -The simpler way to perform a HTTP request is the following: +The simple way to perform a HTTP request is the following: [source, java, subs="{sub-order}"] ---- ContentResponse response = httpClient.GET("http://domain.com/path?query"); ---- -Method `HttpClient.GET(...)` performs a HTTP GET request to the given URI and -returns a `ContentResponse` when the request/response conversation completes -successfully. +The method `HttpClient.GET(...)` performs a HTTP `GET` request to the given URI and returns a `ContentResponse` when the request/response conversation completes successfully. -The `ContentResponse` object contains the HTTP response information: status -code, headers and possibly a content. -The content length is limited by default to 2 MiB; for larger content see -xref:http-client-response-content[]. +The `ContentResponse` object contains the HTTP response information: status code, headers and possibly content. +The content length is limited by default to 2 MiB; for larger content see xref:http-client-response-content[]. -If you want to customize the request, for example by issuing a HEAD request -instead of a GET, and simulating a browser user agent, you can do it in this -way: +If you want to customize the request, for example by issuing a `HEAD` request instead of a `GET`, and simulating a browser user agent, you can do it in this way: [source, java, subs="{sub-order}"] ---- @@ -58,13 +52,10 @@ request.agent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:17.0) Gecko/20100101 F ContentResponse response = request.send(); ---- -You first create a request object using `httpClient.newRequest(...)`, and then -you customize it using the fluent API style (that is, chained invocation of -methods on the request object). -When the request object is customized, you call `Request.send()` that produces -the `ContentResponse` when the request/response conversation is complete. +You first create a request object using `httpClient.newRequest(...)`, and then you customize it using the fluent API style (that is, a chained invocation of methods on the request object). +When the request object is customized, you call `request.send()` that produces the `ContentResponse` when the request/response conversation is complete. -Simple POST requests also have a shortcut method: +Simple `POST` requests also have a shortcut method: [source, java, subs="{sub-order}"] ---- @@ -73,14 +64,10 @@ ContentResponse response = httpClient.POST("http://domain.com/entity/1") .send(); ---- -The POST parameter values added via the `param()` method are automatically -URL-encoded. +The `POST` parameter values added via the `param()` method are automatically URL-encoded. -Jetty's HTTP client automatically follows redirects, so automatically handles -the typical web pattern http://en.wikipedia.org/wiki/Post/Redirect/Get[POST/Redirect/GET], -and the response object contains the content of the response of the GET request. -Following redirects is a feature that you can enable/disable on a per-request -basis or globally. +Jetty's HTTP client automatically follows redirects, so it handles the typical web pattern http://en.wikipedia.org/wiki/Post/Redirect/Get[POST/Redirect/GET], and the response object contains the content of the response of the `GET` request. +Following redirects is a feature that you can enable/disable on a per-request basis or globally. File uploads also require one line, and make use of JDK 7′s `java.nio.file` classes: @@ -92,8 +79,7 @@ ContentResponse response = httpClient.newRequest("http://domain.com/upload") .send(); ---- -It is possible to impose a total timeout for the request/response conversation -using the `Request.timeout(...)` method, in this way: +It is possible to impose a total timeout for the request/response conversation using the `Request.timeout(...)` method as follows: [source, java, subs="{sub-order}"] ---- @@ -102,69 +88,38 @@ ContentResponse response = httpClient.newRequest("http://domain.com/path?query") .send(); ---- -In the example above, when the 5 seconds expire, the request is aborted and a -`java.util.concurrent.TimeoutException` is thrown. +In the example above, when the 5 seconds expire, the request is aborted and a `java.util.concurrent.TimeoutException` is thrown. [[http-client-async]] ==== Non-Blocking APIs -So far we have shown how to use Jetty HTTP client in a blocking style, that is -the thread that issues the request blocks until the request/response conversation -is complete. +So far we have shown how to use Jetty HTTP client in a blocking style - that is, the thread that issues the request blocks until the request/response conversation is complete. -In this section we will look at Jetty's HTTP client non-blocking, asynchronous, -APIs that are perfectly suited for large content downloads, for parallel -processing of requests/responses and in all those cases where performance and -efficient thread and resource utilization is a key factor. +This section will look at Jetty's HTTP client non-blocking, asynchronous APIs that are perfectly suited for large content downloads, for parallel processing of requests/responses and in cases where performance and efficient thread and resource utilization is a key factor. -The asynchronous APIs rely heavily on listeners that are invoked at various stages -of request and response processing. +The asynchronous APIs rely heavily on listeners that are invoked at various stages of request and response processing. These listeners are implemented by applications and may perform any kind of logic. -The implementation invokes these listeners in the same thread that is used to -process the request or response. -Therefore, if the application code in these listeners takes a long time to execute, -the request or response processing is delayed until the listener returns. +The implementation invokes these listeners in the same thread that is used to process the request or response. +Therefore, if the application code in these listeners takes a long time to execute, the request or response processing is delayed until the listener returns. -If you need to execute application code that takes long time inside a listener, -you must spawn your own thread, and remember to deep copy any data provided by -the listener that you will need in your code, because when the listener returns -the data it provides may be recycled/cleared/destroyed. +If you need to execute application code that takes long time inside a listener, you must spawn your own thread and remember to deep copy any data provided by the listener that you will need in your code, because when the listener returns the data it provides may be recycled/cleared/destroyed. -Request and response processing are executed by two different threads and -therefore may happen concurrently. -A typical example of this concurrent processing is an echo server, where a -large upload may be concurrent with the large download echoed back. -As a side note, remember that responses may be processed and completed _before_ -requests; a typical example is a large upload that triggers a quick response - -for example an error - by the server: the response may arrive and be completed -while the request content is still being uploaded. +Request and response processing are executed by two different threads and therefore may happen concurrently. +A typical example of this concurrent processing is an echo server, where a large upload may be concurrent with the large download echoed back. +As a side note, remember that responses may be processed and completed _before_ requests; a typical example is a large upload that triggers a quick response - for example an error - by the server: the response may arrive and be completed while the request content is still being uploaded. -The application thread that calls `Request.send(Response.CompleteListener)` -performs the processing of the request until either the request is fully -processed or until it would block on I/O, then it returns (and therefore never -blocks). -If it would block on I/O, the thread asks the I/O system to emit an event when -the I/O will be ready to continue, then returns. -When such an event is fired, a thread taken from the `HttpClient` thread pool -will resume the processing of the request. +The application thread that calls `Request.send(Response.CompleteListener)` performs the processing of the request until either the request is fully processed or until it would block on I/O, then it returns (and therefore never blocks). +If it would block on I/O, the thread asks the I/O system to emit an event when the I/O will be ready to continue, then returns. +When such an event is fired, a thread taken from the `HttpClient` thread pool will resume the processing of the request. -Response are processed from the I/O thread that fires the event that bytes are -ready to be read. -Response processing continues until either the response is fully processed or -until it would block for I/O. -If it would block for I/O, the thread asks the I/O system to emit an event when -the I/O will be ready to continue, then returns. -When such an event is fired, a thread taken from the `HttpClient` thread pool -will resume the processing of the response. +Response are processed from the I/O thread that fires the event that bytes are ready to be read. +Response processing continues until either the response is fully processed or until it would block for I/O. +If it would block for I/O, the thread asks the I/O system to emit an event when the I/O will be ready to continue, then returns. +When such an event is fired, a thread taken from the `HttpClient` thread pool will resume the processing of the response. -When the request and the response are both fully processed, the thread that -finished the last processing (usually the thread that processes the response, -but may also be the thread that processes the request - if the request takes -more time than the response to be processed) is used to dequeue the next -request for the same destination and processes it. +When the request and the response are both fully processed, the thread that finished the last processing (usually the thread that processes the response, but may also be the thread that processes the request - if the request takes more time than the response to be processed) is used to de-queue the next request for the same destination and processes it. -A simple asynchronous GET request that discards the response content can be -written in this way: +A simple asynchronous `GET` request that discards the response content can be written in this way: [source, java, subs="{sub-order}"] ---- @@ -179,10 +134,7 @@ httpClient.newRequest("http://domain.com/path") }); ---- -Method `Request.send(Response.CompleteListener)` returns `void` and does not -block; the `Response.CompleteListener` provided as a parameter is notified when -the request/response conversation is complete, and the `Result` parameter -allows you to access the response object. +Method `Request.send(Response.CompleteListener)` returns `void` and does not block; the `Response.CompleteListener` provided as a parameter is notified when the request/response conversation is complete, and the `Result` parameter allows you to access the response object. You can write the same code using JDK 8′s lambda expressions: @@ -192,8 +144,7 @@ httpClient.newRequest("http://domain.com/path") .send(result -> { /* Your logic here */ }); ---- -You can impose a total timeout for the request/response conversation in the -same way used by the synchronous API: +You can impose a total timeout for the request/response conversation in the same way used by the synchronous API: [source, java, subs="{sub-order}"] ---- @@ -202,12 +153,9 @@ Request request = httpClient.newRequest("http://domain.com/path") .send(result -> { /* Your logic here */ }); ---- -The example above will impose a total timeout of 3 seconds on the request/response -conversation. +The example above will impose a total timeout of 3 seconds on the request/response conversation. -The HTTP client APIs use listeners extensively to provide hooks for all possible -request and response events, and with JDK 8′s lambda expressions they’re even -more fun to use: +The HTTP client APIs use listeners extensively to provide hooks for all possible request and response events, and with JDK 8′s lambda expressions they are even more fun to use: [source, java, subs="{sub-order}"] ---- @@ -226,14 +174,9 @@ httpClient.newRequest("http://domain.com/path") .send(result -> { ... }); ---- -This makes Jetty HTTP client suitable for HTTP load testing because, for example, -you can accurately time every step of the request/response conversation (thus -knowing where the request/response time is really spent). +This makes Jetty HTTP client suitable for HTTP load testing because, for example, you can accurately time every step of the request/response conversation (thus knowing where the request/response time is really spent). -Have a look at the link:{JDURL}/org/eclipse/jetty/client/api/Request.Listener.html[`Request.Listener`] -class to know about request events, and to the -link:{JDURL}/org/eclipse/jetty/client/api/Response.Listener.html[`Response.Listener`] -class to know about response events. +Have a look at the link:{JDURL}/org/eclipse/jetty/client/api/Request.Listener.html[`Request.Listener`] class to know about request events, and to the link:{JDURL}/org/eclipse/jetty/client/api/Response.Listener.html[`Response.Listener`] class to know about response events. [[http-client-content]] ==== Content Handling @@ -241,12 +184,9 @@ class to know about response events. [[http-client-request-content]] ===== Request Content Handling -Jetty's HTTP client provides a number of utility classes off the shelf to handle -request content. +Jetty's HTTP client provides a number of utility classes off the shelf to handle request content. -You can provide request content as `String`, `byte[]`, `ByteBuffer`, -`java.nio.file.Path`, `InputStream`, and provide your own implementation of -`org.eclipse.jetty.client.api.ContentProvider`. +You can provide request content as `String`, `byte[]`, `ByteBuffer`, `java.nio.file.Path`, `InputStream`, and provide your own implementation of `org.eclipse.jetty.client.api.ContentProvider`. Here’s an example that provides the request content using `java.nio.file.Paths`: [source, java, subs="{sub-order}"] @@ -277,12 +217,9 @@ ContentResponse response = httpClient.newRequest("http://domain.com/upload") .send(); ---- -Since `InputStream` is blocking, then also the send of the request will block -if the input stream blocks, even in case of usage of the asynchronous -`HttpClient` APIs. +Since `InputStream` is blocking, then also the send of the request will block if the input stream blocks, even in case of usage of the asynchronous `HttpClient` APIs. -If you have already read the content in memory, you can pass it as a `byte[]` -using the `BytesContentProvider` utility class: +If you have already read the content in memory, you can pass it as a `byte[]` using the `BytesContentProvider` utility class: [source, java, subs="{sub-order}"] ---- @@ -293,9 +230,7 @@ ContentResponse response = httpClient.newRequest("http://domain.com/upload") .send(); ---- -If the request content is not immediately available, but your application will -be notified of the content to send, you can use `DeferredContentProvider` in -this way: +If the request content is not immediately available, but your application will be notified of the content to send, you can use `DeferredContentProvider` in this way: [source, java, subs="{sub-order}"] ---- @@ -326,17 +261,12 @@ content.offer(ByteBuffer.wrap(bytes)); content.close(); ---- -While the request content is awaited and consequently uploaded by the client -application, the server may be able to respond (at least with the response -headers) completely asynchronously. In this case, `Response.Listener` callbacks -will be invoked before the request is fully sent. -This allows fine-grained control of the request/response conversation: for -example the server may reject contents that are too big, send a response to the -client, which in turn may stop the content upload. +While the request content is awaited and consequently uploaded by the client application, the server may be able to respond (at least with the response headers) completely asynchronously. +In this case, `Response.Listener` callbacks will be invoked before the request is fully sent. +This allows fine-grained control of the request/response conversation: for example the server may reject contents that are too big, send a response to the client, which in turn may stop the content upload. Another way to provide request content is by using an `OutputStreamContentProvider`, -which allows applications to write request content when it is available to the -`OutputStream` provided by `OutputStreamContentProvider`: +which allows applications to write request content when it is available to the `OutputStream` provided by `OutputStreamContentProvider`: [source, java, subs="{sub-order}"] ---- @@ -370,13 +300,9 @@ try (OutputStream output = content.getOutputStream()) Jetty HTTP client allows applications to handle response content in different ways. -The first way is to buffer the response content in memory; this is done when -using the blocking APIs (see xref:http-client-blocking[]) and the content is -buffered within a `ContentResponse` up to 2 MiB. +The first way is to buffer the response content in memory; this is done when using the blocking APIs (see xref:http-client-blocking[]) and the content is buffered within a `ContentResponse` up to 2 MiB. -If you want to control the length of the response content (for example limiting -to values smaller than the default of 2 MiB), then you can use a -`org.eclipse.jetty.client.util.FutureResponseListener`in this way: +If you want to control the length of the response content (for example limiting to values smaller than the default of 2 MiB), then you can use a `org.eclipse.jetty.client.util.FutureResponseListener` in this way: [source, java, subs="{sub-order}"] ---- @@ -390,11 +316,9 @@ request.send(listener); ContentResponse response = listener.get(5, TimeUnit.SECONDS); ---- -If the response content length is exceeded, the response will be aborted, and -an exception will be thrown by method `get()`. +If the response content length is exceeded, the response will be aborted, and an exception will be thrown by method `get()`. -If you are using the asynchronous APIs (see xref:http-client-async[]), you can -use the `BufferingResponseListener` utility class: +If you are using the asynchronous APIs (see xref:http-client-async[]), you can use the `BufferingResponseListener` utility class: [source, java, subs="{sub-order}"] ---- @@ -414,14 +338,9 @@ httpClient.newRequest("http://domain.com/path") }); ---- -The second way is the most efficient (because it avoids content copies) and -allows you to specify a `Response.ContentListener`, or a subclass, to handle -the content as soon as it arrives. -In the example below, `Response.Listener.Adapter` is a class that implements -both `Response.ContentListener` and `Response.CompleteListener` and can -be passed to `Request.send()`. -Jetty's HTTP client will invoke the `onContent()` method zero or more times -(until there is content), and finally invoke the `onComplete()` method. +The second way is the most efficient (because it avoids content copies) and allows you to specify a `Response.ContentListener`, or a subclass, to handle the content as soon as it arrives. +In the example below, `Response.Listener.Adapter` is a class that implements both `Response.ContentListener` and `Response.CompleteListener` and can be passed to `Request.send()`. +Jetty's HTTP client will invoke the `onContent()` method zero or more times (until there is content), and finally invoke the `onComplete()` method. [source, java, subs="{sub-order}"] ---- @@ -437,8 +356,7 @@ ContentResponse response = httpClient }); ---- -The third way allows you to wait for the response and then stream the content -using the `InputStreamResponseListener` utility class: +The third way allows you to wait for the response and then stream the content using the `InputStreamResponseListener` utility class: [source, java, subs="{sub-order}"] ---- diff --git a/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-authentication.adoc b/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-authentication.adoc index 68ee54e717d..44849f84e02 100644 --- a/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-authentication.adoc +++ b/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-authentication.adoc @@ -17,8 +17,7 @@ [[http-client-authentication]] === Authentication Support -Jetty's HTTP client supports the "Basic" and "Digest" authentication mechanisms -defined by https://tools.ietf.org/html/rfc7235[RFC 7235]. +Jetty's HTTP client supports the "Basic" and "Digest" authentication mechanisms defined by link:https://tools.ietf.org/html/rfc7235[RFC 7235]. You can configure authentication credentials in the HTTP client instance as follows: @@ -39,11 +38,8 @@ ContentResponse response = httpClient .get(5, TimeUnit.SECONDS); ---- -Jetty's HTTP client tests authentication credentials against the challenge(s) -the server issues, and if they match it automatically sends the right -authentication headers to the server for authentication. -If the authentication is successful, it caches the result and reuses it for -subsequent requests for the same domain and matching URIs. +Jetty's HTTP client tests authentication credentials against the challenge(s) the server issues, and if they match it automatically sends the right authentication headers to the server for authentication. +If the authentication is successful, it caches the result and reuses it for subsequent requests for the same domain and matching URIs. The HTTP conversation for a successful match is the following: @@ -59,21 +55,16 @@ Application HttpClient Server |<-- 200 ---|------------ 200 ------------| ---- -The application does not receive events related to the response with code 401, -they are handled internally by `HttpClient` which produces a request similar -to the original but with the correct `Authorization` header, and then relays -the response with code 200 to the application. +The application does not receive events related to the response with code 401, they are handled internally by `HttpClient` which produces a request similar to the original but with the correct `Authorization` header, and then relays the response with code 200 to the application. -Successful authentications are cached, but it is possible to clear them in -order to force authentication again: +Successful authentications are cached, but it is possible to clear them in order to force authentication again: [source, java, subs="{sub-order}"] ---- httpClient.getAuthenticationStore().clearAuthenticationResults(); ---- -Authentications may be preempted to avoid the additional roundtrip due to the -server challenge in this way: +Authentications may be preempted to avoid the additional roundtrip due to the server challenge in this way: [source, java, subs="{sub-order}"] ---- @@ -82,9 +73,6 @@ URI uri = URI.create("http://domain.com/secure"); auth.addAuthenticationResult(new BasicAuthentication.BasicResult(uri, "username", "password")); ---- -In this way, the original request is enriched by `HttpClient` immediately with -the `Authorization` header, and the server should respond with a 200 and the -resource content rather than with the 401 and the challenge. +In this way, the original request is enriched by `HttpClient` immediately with the `Authorization` header, and the server should respond with a 200 and the resource content rather than with the 401 and the challenge. -See also the <> -for further information about how authentication works with HTTP proxies. +See also the link:#http-client-proxy-authentication[proxy authentication section] for further information about how authentication works with HTTP proxies. diff --git a/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-cookie.adoc b/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-cookie.adoc index a041c90b434..fc59f8c034c 100644 --- a/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-cookie.adoc +++ b/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-cookie.adoc @@ -18,14 +18,10 @@ === Cookies Support Jetty HTTP client supports cookies out of the box. -The `HttpClient` instance receives cookies from HTTP responses and stores them -in a `java.net.CookieStore`, a class that is part of the JDK. -When new requests are made, the cookie store is consulted and if there are -matching cookies (that is, cookies that are not expired and that match domain -and path of the request) then they are added to the requests. +The `HttpClient` instance receives cookies from HTTP responses and stores them in a `java.net.CookieStore`, a class that is part of the JDK. +When new requests are made, the cookie store is consulted and if there are matching cookies (that is, cookies that are not expired and that match domain and path of the request) then they are added to the requests. -Applications can programmatically access the cookie store to find the cookies -that have been set: +Applications can programmatically access the cookie store to find the cookies that have been set: [source, java, subs="{sub-order}"] ---- @@ -65,16 +61,14 @@ for (HttpCookie cookie : cookies) cookieStore.remove(uri, cookie); ---- -If you want to totally disable cookie handling, you can install a -`HttpCookieStore.Empty` instance in this way: +If you want to totally disable cookie handling, you can install a `HttpCookieStore.Empty` instance in this way: [source, java, subs="{sub-order}"] ---- httpClient.setCookieStore(new HttpCookieStore.Empty()); ---- -You can enable cookie filtering by installing a cookie store that performs the -filtering logic in this way: +You can enable cookie filtering by installing a cookie store that performs the filtering logic in this way: [source, java, subs="{sub-order}"] ---- @@ -91,5 +85,4 @@ public class GoogleOnlyCookieStore extends HttpCookieStore } ---- -The example above will retain only cookies that come from the `google.com` -domain or sub-domains. +The example above will retain only cookies that come from the `google.com` domain or sub-domains. diff --git a/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-intro.adoc b/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-intro.adoc index dbe9dec9b26..b9d09d3168f 100644 --- a/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-intro.adoc +++ b/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-intro.adoc @@ -17,42 +17,29 @@ [[http-client-intro]] === Introduction -The Jetty HTTP client module provides easy-to-use APIs and utility classes to -perform HTTP (or HTTPS) requests. +The Jetty HTTP client module provides easy-to-use APIs and utility classes to perform HTTP (or HTTPS) requests. -Jetty's HTTP client is non-blocking and asynchronous. It offers an asynchronous -API that never blocks for I/O, making it very efficient in thread utilization -and well suited for high performance scenarios such as load testing or parallel -computation. +Jetty's HTTP client is non-blocking and asynchronous. +It offers an asynchronous API that never blocks for I/O, making it very efficient in thread utilization and well suited for high performance scenarios such as load testing or parallel computation. -However, when all you need to do is to perform a GET request to a resource, -Jetty's HTTP client offers also a synchronous API, that is a programming interface -where the thread that issued the request blocks until the request/response -conversation is complete. +However, when all you need to do is to perform a `GET` request to a resource, Jetty's HTTP client offers also a synchronous API; a programming interface +where the thread that issued the request blocks until the request/response conversation is complete. -Jetty's HTTP client supports different <>: -HTTP/1.1, FastCGI and HTTP/2. -This means that the semantic of a HTTP request (that is, "GET me the resource -`/index.html`") can be carried over the network in different formats. +Jetty's HTTP client supports different link:#http-client-transport[transports]: HTTP/1.1, FastCGI and HTTP/2. +This means that the semantic of a HTTP request (that is, " `GET` me the resource `/index.html` ") can be carried over the network in different formats. The most common and default format is HTTP/1.1. -However, Jetty's HTTP client can carry the same request using the FastCGI format -or the new HTTP/2 format. +That said, Jetty's HTTP client can carry the same request using the FastCGI format or the new HTTP/2 format. -The FastCGI transport is heavily used in Jetty's <> -that allows Jetty to work as a reverse proxy to PHP (exactly like Apache or -Nginx do) and therefore be able to serve - for example - WordPress websites. +The FastCGI transport is heavily used in Jetty's link:#fastcgi[FastCGI support] that allows Jetty to work as a reverse proxy to PHP (exactly like Apache or Nginx do) and therefore be able to serve - for example - WordPress websites. -The HTTP/2 transport allows Jetty's HTTP client to perform requests using HTTP/2 -to HTTP/2 enabled web sites, see also Jetty's <>. +The HTTP/2 transport allows Jetty's HTTP client to perform requests using HTTP/2 to HTTP/2 enabled web sites, see also Jetty's link:#http2[HTTP/2 support]. -Out of the box features that you get with the Jetty HTTP client are: +Out of the box features that you get with the Jetty HTTP client include: -* Redirect support; redirect codes such as 302 or 303 are automatically followed -* Cookies support; cookies sent by servers are stored and sent back to servers - in matching requests -* Authentication support; HTTP "Basic" and "Digest" authentications are supported, - others are pluggable -* Forward proxy support; HTTP proxying and SOCKS4 proxying. +* Redirect support - redirect codes such as 302 or 303 are automatically followed. +* Cookies support - cookies sent by servers are stored and sent back to servers in matching requests. +* Authentication support - HTTP "Basic" and "Digest" authentications are supported, others are pluggable. +* Forward proxy support - HTTP proxying and SOCKS4 proxying. [[http-client-init]] ==== Starting HttpClient @@ -60,12 +47,10 @@ Out of the box features that you get with the Jetty HTTP client are: The main class is named `org.eclipse.jetty.client.HttpClient`. You can think of a `HttpClient` instance as a browser instance. -Like a browser, it can make requests to different domains, it manages -redirects, cookies and authentication, you can configure it with a proxy, and +Like a browser it can make requests to different domains, it manages redirects, cookies and authentication, you can configure it with a proxy, and it provides you with the responses to the requests you make. -In order to use `HttpClient`, you must instantiate it, configure it, and then -start it: +In order to use `HttpClient`, you must instantiate it, configure it, and then start it: [source, java, subs="{sub-order}"] ---- @@ -79,24 +64,17 @@ httpClient.setFollowRedirects(false); httpClient.start(); ---- -You may create multiple instances of `HttpClient`, but typically one instance -is enough for an application. -The reason to create multiple instances of `HttpClient` is that you want to -specify different configuration parameters (for example, one instance is -configured with a forward proxy while another is not), or because you want the -two instances to behave like two different browsers and hence have different -cookies, different authentication credentials and so on, or because you want to -use different transports. +You may create multiple instances of `HttpClient`, but typically one instance is enough for an application. +There are several reasons for having multiple `HttpClient` instances including, but not limited to: -When you create a `HttpClient` instance using the parameterless constructor, -you will only be able to perform plain HTTP requests, and you will not be able -to perform HTTPS requests. +* You want to specify different configuration parameters (for example, one instance is configured with a forward proxy while another is not) +* You want the two instances to behave like two different browsers and hence have different cookies, different authentication credentials...etc. +* You want to use different transports -In order to perform HTTPS requests, you should create first a -link:{JDURL}/org/eclipse/jetty/util/ssl/SslContextFactory.html[`SslContextFactory`], -configure it, and pass it to `HttpClient`'s constructor. -When created with a `SslContextFactory`, the `HttpClient` will be able to perform -both HTTP and HTTPS requests to any domain. +When you create a `HttpClient` instance using the parameterless constructor, you will only be able to perform plain HTTP requests and you will not be able to perform HTTPS requests. + +In order to perform HTTPS requests, you should create first a link:{JDURL}/org/eclipse/jetty/util/ssl/SslContextFactory.html[`SslContextFactory`], configure it, and pass it to the `HttpClient` constructor. +When created with a `SslContextFactory`, the `HttpClient` will be able to perform both HTTP and HTTPS requests to any domain. [source, java, subs="{sub-order}"] ---- @@ -115,15 +93,11 @@ httpClient.start(); ==== Stopping HttpClient -It is recommended that when your application stops, you also stop the `HttpClient` -instance (or instances) that you are using. +It is recommended that when your application stops, you also stop the `HttpClient` instance (or instances) that you are using. [source, java, subs="{sub-order}"] ---- httpClient.stop(); ---- -Stopping `HttpClient` makes sure that the memory it holds (for example, -authentication credentials, cookies, etc.) is released, and that the thread -pool and scheduler are properly stopped allowing all threads used by -`HttpClient` to exit. +Stopping `HttpClient` makes sure that the memory it holds (for example, authentication credentials, cookies, etc.) is released, and that the thread pool and scheduler are properly stopped allowing all threads used by `HttpClient` to exit. diff --git a/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-proxy.adoc b/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-proxy.adoc index ce176a3027a..ccf682b9a4d 100644 --- a/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-proxy.adoc +++ b/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-proxy.adoc @@ -19,12 +19,10 @@ Jetty's HTTP client can be configured to use proxies to connect to destinations. -Two types of proxies are available out of the box: a HTTP proxy (provided by -class `org.eclipse.jetty.client.HttpProxy`) and a SOCKS 4 proxy (provided by -class `org.eclipse.jetty.client.Socks4Proxy`). +Two types of proxies are available out of the box: a HTTP proxy (provided by class `org.eclipse.jetty.client.HttpProxy`) and a SOCKS 4 proxy (provided by class `org.eclipse.jetty.client.Socks4Proxy`). Other implementations may be written by subclassing `ProxyConfiguration.Proxy`. -A typical configuration is the following: +The following is a typical configuration: [source, java, subs="{sub-order}"] ---- @@ -40,22 +38,16 @@ proxyConfig.getProxies().add(proxy); ContentResponse response = httpClient.GET(uri); ---- -You specify the proxy host and port, and optionally also the addresses that -you do not want to be proxied, and then add the proxy configuration on the -`ProxyConfiguration` instance. +You specify the proxy host and port, and optionally also the addresses that you do not want to be proxied, and then add the proxy configuration on the `ProxyConfiguration` instance. -Configured in this way, `HttpClient` makes requests to the HTTP proxy -(for plain-text HTTP requests) or establishes a tunnel via HTTP CONNECT -(for encrypted HTTPS requests). +Configured in this way, `HttpClient` makes requests to the HTTP proxy (for plain-text HTTP requests) or establishes a tunnel via `HTTP CONNECT` (for encrypted HTTPS requests). [[http-client-proxy-authentication]] ==== Proxy Authentication Support -Jetty's HTTP client support proxy authentication in the same way it supports -<>. +Jetty's HTTP client support proxy authentication in the same way it supports link:#http-client-authentication[server authentication]. -In the example below, the proxy requires Basic authentication, but the server -requires Digest authentication, and therefore: +In the example below, the proxy requires Basic authentication, but the server requires Digest authentication, and therefore: [source, java, subs="{sub-order}"] ---- @@ -80,8 +72,7 @@ ContentResponse response = httpClient.newRequest(serverURI) .get(5, TimeUnit.SECONDS); ---- -The HTTP conversation for successful authentications on both the proxy and the -server is the following: +The HTTP conversation for successful authentications on both the proxy and the server is the following: ---- Application HttpClient Proxy Server @@ -105,9 +96,6 @@ Application HttpClient Proxy Server |<-- 200 ---|<------------ 200 --------------|<--------- 200 ----------| ---- -The application does not receive events related to the responses with code 407 -and 401 since they are handled internally by `HttpClient`. +The application does not receive events related to the responses with code 407 and 401 since they are handled internally by `HttpClient`. -Similarly to the <>, the -proxy authentication result and the server authentication result can be -preempted to avoid, respectively, the 407 and 401 roundtrips. +Similarly to the link:#http-client-authentication[authentication section], the proxy authentication result and the server authentication result can be preempted to avoid, respectively, the 407 and 401 roundtrips. diff --git a/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-transport.adoc b/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-transport.adoc index e8ec2bbcfe7..d08293207fe 100644 --- a/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-transport.adoc +++ b/jetty-documentation/src/main/asciidoc/development/clients/http/http-client-transport.adoc @@ -17,25 +17,18 @@ [[http-client-transport]] === Pluggable Transports -Jetty's HTTP client can be configured to use different transports to carry -the semantic of HTTP requests and responses. +Jetty's HTTP client can be configured to use different transports to carry the semantic of HTTP requests and responses. -This means that the intention of a client to request resource `/index.html` -using the `GET` method can be carried over the network in different formats. +This means that the intention of a client to request resource `/index.html` using the `GET` method can be carried over the network in different formats. -A HTTP client transport is the component that is in charge of converting -a high-level, semantic, HTTP requests such as "GET resource /index.html" -into the specific format understood by the server (for example, HTTP/2), -and to convert the server response from the specific format (HTTP/2) into -high-level, semantic objects that can be used by applications. +A HTTP client transport is the component that is in charge of converting a high-level, semantic, HTTP requests such as "GET resource /index.html" into the specific format understood by the server (for example, HTTP/2), and to convert the server response from the specific format (HTTP/2) into high-level, semantic objects that can be used by applications. In this way, applications are not aware of the actual protocol being used. -They can write their logic against a high-level API that hides the details -of the specific protocol being used over the network. +This allows them to write their logic against a high-level API that hides the details of the specific protocol being used over the network. -The most common protocol format is HTTP/1.1, a text-based protocol with -lines separated by `\r\n`: +The most common protocol format is HTTP/1.1, a text-based protocol with lines separated by `\r\n`: +[source, screen, subs="{sub-order}"] ---- GET /index.html HTTP/1.1\r\n Host: domain.com\r\n @@ -45,6 +38,7 @@ Host: domain.com\r\n However, the same request can be made using FastCGI, a binary protocol: +[source, screen, subs="{sub-order}"] ---- x01 x01 x00 x01 x00 x08 x00 x00 x00 x01 x01 x00 x00 x00 x00 x00 @@ -56,8 +50,7 @@ x0C x0B D O C U M E ... ---- -Similarly, HTTP/2 is a binary protocol that transports the same information -in a yet different format. +Similarly, HTTP/2 is a binary protocol that transports the same information in a yet different format. ==== HTTP/1.1 Transport @@ -70,8 +63,7 @@ HttpClient client = new HttpClient(); client.start(); ---- -If you want to customize the HTTP/1.1 transport, you can explicitly configure -`HttpClient` in this way: +If you want to customize the HTTP/1.1 transport, you can explicitly configure `HttpClient` in this way: [source, java, subs="{sub-order}"] ---- @@ -82,8 +74,7 @@ HttpClient client = new HttpClient(transport, null); client.start(); ---- -The example above allows you to customize the number of NIO selectors that -`HttpClient` will be using. +The example above allows you to customize the number of NIO selectors that `HttpClient` will be using. ==== HTTP/2 Transport @@ -99,11 +90,9 @@ HttpClient client = new HttpClient(transport, null); client.start(); ---- -`HTTP2Client` is the lower-level client that provides an API based on HTTP/2 -concepts such as _sessions_, _streams_ and _frames_ that are specific to HTTP/2. +`HTTP2Client` is the lower-level client that provides an API based on HTTP/2 concepts such as _sessions_, _streams_ and _frames_ that are specific to HTTP/2. -`HttpClientTransportOverHTTP2` uses `HTTP2Client` to format high-level semantic -HTTP requests ("GET resource /index.html") into the HTTP/2 specific format. +`HttpClientTransportOverHTTP2` uses `HTTP2Client` to format high-level semantic HTTP requests ("GET resource /index.html") into the HTTP/2 specific format. ==== FastCGI Transport @@ -119,9 +108,6 @@ HttpClient client = new HttpClient(transport, null); client.start(); ---- -In order to make requests using the FastCGI transport, you need to have a -FastCGI server such as https://en.wikipedia.org/wiki/PHP#PHPFPM[PHP-FPM] -(see also http://php.net/manual/en/install.fpm.php). +In order to make requests using the FastCGI transport, you need to have a FastCGI server such as https://en.wikipedia.org/wiki/PHP#PHPFPM[PHP-FPM] (see also http://php.net/manual/en/install.fpm.php). -The FastCGI transport is primarily used by Jetty's <> -to serve PHP pages (for example WordPress). +The FastCGI transport is primarily used by Jetty's link:#fastcgi[FastCGI support] to serve PHP pages (WordPress for example).