Scoop API

scoop:scoop.http.client:0.1.0

src/redirect/plan.scoop

Selected production source · src/redirect/plan.scoop

1package scoop.http.client
3import scoop.http.*
4import scoop.url.Url
5import scoop.url.UrlParseException
7internal class ClientRedirect(val request: HttpRequest, val history: HttpRedirect)
9internal fun planClientRedirect(
10 request: HttpRequest,
11 head: HttpResponseHead,
12 options: HttpRedirectOptions,
13 history: List<HttpRedirect>,
14 webSocket: Boolean = false
15): ClientRedirect? {
16 if (
17 !options.enabled ||
18 (
19 head.status !=
20 301U &&
21 head.status !=
22 302U &&
23 head.status !=
24 303U &&
25 head.status !=
26 307U &&
27 head.status !=
28 308U
29 )
30 )
31 return None
32 val location =
33 try {
34 HttpLocation.parseFields(head.fields)
35 }
36 catch (error: HttpFieldParseException) {
37 throw HttpRedirectException(
38 HttpRedirectFailure.InvalidLocation,
39 request.url,
40 head,
41 cause = Some(error)
42 )
43 }
44 if (!clientPresent(location))
45 return None
46 val resolved =
47 try {
48 location!!.resolve(request.url)
49 }
50 catch (error: UrlParseException) {
51 throw HttpRedirectException(
52 HttpRedirectFailure.InvalidLocation,
53 request.url,
54 head,
55 cause = Some(error)
56 )
57 }
58 val destination =
59 if (webSocket) {
60 if (
61 resolved.scheme !=
62 Some("http") &&
63 resolved.scheme !=
64 Some("https") &&
65 resolved.scheme !=
66 Some("ws") &&
67 resolved.scheme !=
68 Some("wss")
69 )
70 throw HttpRedirectException(
71 HttpRedirectFailure.UnsupportedScheme,
72 request.url,
73 head,
74 Some(resolved)
75 )
76 try {
77 clientWebSocketHttpUrl(resolved)
78 }
79 catch (error: IllegalArgumentException) {
80 throw HttpRedirectException(
81 HttpRedirectFailure.InvalidLocation,
82 request.url,
83 head,
84 Some(resolved),
85 Some(error)
86 )
87 }
88 } else
89 resolved
90 if (destination.scheme != Some("http") && destination.scheme != Some("https"))
91 throw HttpRedirectException(
92 HttpRedirectFailure.UnsupportedScheme,
93 request.url,
94 head,
95 Some(destination)
96 )
97 if (
98 !options.allowHttpsToHttp &&
99 request.origin.scheme ==
100 "https" &&
101 destination.scheme ==
102 Some("http")
103 )
104 throw HttpRedirectException(
105 HttpRedirectFailure.HttpsDowngrade,
106 request.url,
107 head,
108 Some(destination)
109 )
110 val origin =
111 try {
112 destination.httpOrigin()
113 }
114 catch (error: UrlParseException) {
115 throw HttpRedirectException(
116 HttpRedirectFailure.InvalidLocation,
117 request.url,
118 head,
119 Some(destination),
120 Some(error)
121 )
122 }
123 if (history.size >= options.maxRedirects)
124 throw HttpRedirectException(
125 HttpRedirectFailure.LimitExceeded,
126 request.url,
127 head,
128 Some(destination)
129 )
130 val droppingBody =
131 (head.status == 303U && request.method != "HEAD") ||
132 ((head.status == 301U || head.status == 302U) && request.method == "POST")
133 val method =
134 if (droppingBody)
135 "GET"
136 else
137 request.method
138 if (sameRedirectTarget(request.url, request.method, destination, method))
139 throw HttpRedirectException(HttpRedirectFailure.Loop, request.url, head, Some(destination))
140 for (previous in history) {
141 if (sameRedirectTarget(previous.url, previous.method, destination, method))
142 throw HttpRedirectException(
143 HttpRedirectFailure.Loop,
144 request.url,
145 head,
146 Some(destination)
147 )
148 }
149 if (!droppingBody && !request.body.isReplayable)
150 throw HttpRedirectException(
151 HttpRedirectFailure.BodyNotReplayable,
152 request.url,
153 head,
154 Some(destination)
155 )
156 val fields = redirectedClientFields(request, droppingBody, origin != request.origin)
157 val next =
158 httpRequest(destination) {
159 this.method = method
160 this.fields = fields
161 this.body =
162 if (droppingBody)
163 RequestBody.Empty
164 else
165 request.body
166 this.options =
167 if (droppingBody)
168 redirectedClientOptions(request.options)
169 else
170 request.options
171 }
172 return Some(
173 ClientRedirect(next, HttpRedirect(request.url, request.method, head, destination, method))
174 )
177private fun sameRedirectTarget(
178 first: Url,
179 firstMethod: String,
180 second: Url,
181 secondMethod: String
182): Boolean =
183 firstMethod ==
184 secondMethod &&
185 first.httpOrigin() ==
186 second.httpOrigin() &&
187 first.httpRequestTarget() ==
188 second.httpRequestTarget()