浏览代码

支持无限次数的重定向

drake 4 年之前
父节点
当前提交
073b876ef4

+ 0 - 68
kalle/src/main/java/com/yanzhenjie/kalle/connect/http/RedirectInterceptor.java

@@ -1,68 +0,0 @@
-/*
- * Copyright (C) 2018 Drake, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.yanzhenjie.kalle.connect.http;
-
-import com.yanzhenjie.kalle.BodyRequest;
-import com.yanzhenjie.kalle.Headers;
-import com.yanzhenjie.kalle.Request;
-import com.yanzhenjie.kalle.RequestMethod;
-import com.yanzhenjie.kalle.Response;
-import com.yanzhenjie.kalle.Url;
-import com.yanzhenjie.kalle.UrlRequest;
-import com.yanzhenjie.kalle.connect.Interceptor;
-import com.yanzhenjie.kalle.util.IOUtils;
-
-import java.io.IOException;
-
-import static com.yanzhenjie.kalle.Headers.KEY_COOKIE;
-
-/**
- * Created by Zhenjie Yan on 2018/3/6.
- */
-public class RedirectInterceptor implements Interceptor {
-
-    public RedirectInterceptor() {
-    }
-
-    @Override
-    public Response intercept(Chain chain) throws IOException {
-        Request request = chain.request();
-        Response response = chain.proceed(request);
-        if (response.isRedirect()) {
-            Url oldUrl = request.url();
-            Url url = oldUrl.location(response.headers().getLocation());
-            Headers headers = request.headers();
-            headers.remove(KEY_COOKIE);
-
-            RequestMethod method = request.method();
-            Request newRequest;
-            if (method.allowBody()) {
-                newRequest = BodyRequest.newBuilder(url, method)
-                        .setHeaders(headers)
-                        .setParams(request.copyParams())
-                        .body(request.body())
-                        .build();
-            } else {
-                newRequest = UrlRequest.newBuilder(url, method)
-                        .setHeaders(headers)
-                        .build();
-            }
-            IOUtils.closeQuietly(response);
-            return chain.proceed(newRequest);
-        }
-        return response;
-    }
-}

+ 60 - 0
kalle/src/main/java/com/yanzhenjie/kalle/connect/http/RedirectInterceptor.kt

@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2018 Drake, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.yanzhenjie.kalle.connect.http
+
+import com.yanzhenjie.kalle.*
+import com.yanzhenjie.kalle.connect.Interceptor
+import com.yanzhenjie.kalle.util.IOUtils
+import java.io.IOException
+
+class RedirectInterceptor : Interceptor {
+
+    @Throws(IOException::class)
+    override fun intercept(chain: Chain): Response {
+        val request = chain.request()
+        val response = chain.proceed(request)
+        return chain.redirect(request, response)
+    }
+
+    /**
+     * 重定向
+     */
+    private fun Chain.redirect(request: Request, response: Response): Response {
+        return if (response.isRedirect) {
+            val oldUrl = request.url()
+            val url = oldUrl.location(response.headers().location)
+            val headers = request.headers()
+            headers.remove(Headers.KEY_COOKIE)
+            val method = request.method()
+            val newRequest: Request
+            newRequest = if (method.allowBody()) {
+                BodyRequest.newBuilder(url, method)
+                    .setHeaders(headers)
+                    .setParams(request.copyParams())
+                    .body(request.body())
+                    .build()
+            } else {
+                UrlRequest.newBuilder(url, method)
+                    .setHeaders(headers)
+                    .build()
+            }
+            IOUtils.closeQuietly(response)
+            redirect(newRequest, proceed(newRequest))
+        } else response
+    }
+
+
+}