Scoop API

scoop:scoop.http.client:0.1.0

src/redirect/options.scoop

Selected production source · src/redirect/options.scoop

1package scoop.http.client
3import scoop.http.HttpResponseHead
4import scoop.url.Url
6/**
7 * Redirect enablement, maximum hops, and explicit permission for HTTPS-to-HTTP downgrade.
8 */
9public class HttpRedirectOptions internal constructor(
10 /**
11 * Whether eligible redirect responses are followed.
12 */
13 public val enabled: Boolean = true,
14 /**
15 * The maximum number of redirect hops; must be nonnegative.
16 */
17 public val maxRedirects: Long = 10L,
18 /**
19 * Whether a secure request may follow a redirect to unencrypted HTTP.
20 */
21 public val allowHttpsToHttp: Boolean = false
22) {
23 init {
24 if (maxRedirects < 0L)
25 throw IllegalArgumentException(Some("negative redirect limit"))
26 }
29/**
30 * One completed redirect step with original request, response, and selected next URL/method.
31 */
32public class HttpRedirect internal constructor(
33 /**
34 * The URL that returned the redirect.
35 */
36 public val url: Url,
37 /**
38 * The method used for the redirecting request.
39 */
40 public val method: String,
41 /**
42 * The redirect response head.
43 */
44 public val head: HttpResponseHead,
45 /**
46 * The resolved URL selected for the next request.
47 */
48 public val nextUrl: Url,
49 /**
50 * The method selected after redirect rewriting rules.
51 */
52 public val nextMethod: String
55/**
56 * The reason a redirect cannot be followed.
57 */
58public enum HttpRedirectFailure {
59 /**
60 * Location is malformed or ambiguous.
61 */
62 InvalidLocation,
63 /**
64 * The target is not a supported HTTP scheme.
65 */
66 UnsupportedScheme,
67 /**
68 * An HTTPS-to-HTTP redirect is forbidden by policy.
69 */
70 HttpsDowngrade,
71 /**
72 * The configured redirect count was exceeded.
73 */
74 LimitExceeded,
75 /**
76 * The request sequence revisited a redirect target.
77 */
78 Loop,
79 /**
80 * The selected redirected method requires a body that cannot be replayed.
81 */
82 BodyNotReplayable
85/**
86 * A rejected redirect, retaining the source response, destination when known, and underlying
87 * cause.
88 */
89public class HttpRedirectException internal constructor(
90 /**
91 * The redirect failure category.
92 */
93 public val reason: HttpRedirectFailure,
94 /**
95 * The URL that returned the rejected redirect.
96 */
97 public val url: Url,
98 /**
99 * The redirect response head.
100 */
101 public val head: HttpResponseHead,
102 /**
103 * The resolved destination when it could be determined.
104 */
105 public val destination: Url? = None,
106 /**
107 * The underlying failure, if any.
108 */
109 public val cause: Throwable? = None
110): Exception(Some(redirectFailureMessage(reason)))
112private fun redirectFailureMessage(reason: HttpRedirectFailure): String =
113 when (reason) {
114 case InvalidLocation -> "invalid HTTP redirect location"
115 case UnsupportedScheme -> "HTTP redirect requires http or https"
116 case HttpsDowngrade -> "HTTPS redirect to HTTP is disabled"
117 case LimitExceeded -> "HTTP redirect limit exceeded"
118 case Loop -> "HTTP redirect loop"
119 case BodyNotReplayable -> "HTTP redirect requires a replayable request body"
120 }