Skip to content

Commit e637e3f

Browse files
committed
fix #697
1 parent 6ae19d0 commit e637e3f

File tree

7 files changed

+118
-110
lines changed

7 files changed

+118
-110
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77
[![捐赠][badge:donate-zh]][shuzijun-donate]
88
[![内推][badge:referrals]][shuzijun-referrals]
99

10+
## 8.10.0
11+
12+
### Added
13+
14+
### Changed
15+
16+
### Deprecated
17+
18+
### Fixed
19+
- fix [#697](https://github.com/shuzijun/leetcode-editor/issues/697)
20+
21+
### Removed
22+
23+
1024
## 8.9.0
1125

1226
### Added

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ targetCompatibility = 11
1515

1616

1717
repositories {
18+
mavenLocal()
19+
maven { url 'https://jitpack.io' }
1820
mavenCentral()
1921
}
2022

2123
dependencies {
24+
api 'com.shuzijun:lc-sdk:0.0.2'
2225
api 'com.alibaba:fastjson:1.2.47'
2326
api 'org.jsoup:jsoup:1.11.3'
2427
api('io.sentry:sentry:1.7.9') {

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pluginGroup = com.shuzijun.leetcode
22
pluginName = leetcode-editor
3-
pluginVersion = 8.9
3+
pluginVersion = 8.10
44

55
pluginSinceBuild = 203.0
66
pluginUntilBuild =

src/main/java/com/shuzijun/leetcode/plugin/manager/CodeTopManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private static List<Tag> getCompany() {
4040
List<Tag> tags = new ArrayList<>();
4141

4242
HttpResponse response = HttpRequest.builderGet(CodeTopURLUtils.getCompanies()).request();
43-
if (response != null && response.getStatusCode() == 200) {
43+
if (response.getStatusCode() == 200) {
4444
try {
4545
String body = response.getBody();
4646
if (StringUtils.isNotBlank(body)) {
@@ -150,7 +150,7 @@ private static PageInfo<CodeTopQuestionView> getQuestionService(Project project,
150150
pageInfo.setRowTotal(pageObject.getInteger("count"));
151151
pageInfo.setRows(questionList);
152152
} else {
153-
LogUtils.LOG.error("Request question list failed, status:" + response == null ? "" : response.getStatusCode());
153+
LogUtils.LOG.error("Request question list failed, status:" + response.getStatusCode());
154154
throw new RuntimeException("Request question list failed");
155155
}
156156

src/main/java/com/shuzijun/leetcode/plugin/utils/HttpRequestUtils.java

Lines changed: 94 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@
33
import com.google.common.cache.Cache;
44
import com.google.common.cache.CacheBuilder;
55
import com.intellij.openapi.project.Project;
6-
import com.intellij.util.io.HttpRequests;
6+
import com.intellij.util.net.HttpConfigurable;
7+
import com.intellij.util.net.IdeaWideAuthenticator;
8+
import com.intellij.util.net.IdeaWideProxySelector;
9+
import com.shuzijun.lc.LcClient;
10+
import com.shuzijun.lc.errors.LcException;
11+
import com.shuzijun.lc.http.DefaultExecutoHttp;
12+
import com.shuzijun.lc.http.HttpClient;
713
import com.shuzijun.leetcode.plugin.model.HttpRequest;
8-
import org.apache.commons.lang3.StringUtils;
9-
import org.apache.http.HttpHeaders;
14+
import okhttp3.Credentials;
15+
import okhttp3.OkHttpClient;
1016
import org.jetbrains.annotations.NotNull;
1117

12-
import java.io.IOException;
13-
import java.net.*;
18+
import java.net.CookieHandler;
19+
import java.net.CookieManager;
20+
import java.net.HttpCookie;
21+
import java.net.PasswordAuthentication;
22+
import java.util.Arrays;
1423
import java.util.List;
1524
import java.util.Map;
25+
import java.util.Objects;
1626
import java.util.concurrent.TimeUnit;
1727

1828
/**
@@ -22,6 +32,9 @@ public class HttpRequestUtils {
2232

2333
private static final Cache<String, HttpResponse> httpResponseCache = CacheBuilder.newBuilder().expireAfterWrite(30, TimeUnit.SECONDS).build();
2434

35+
private static MyExecutorHttp executorHttp = new MyExecutorHttp();
36+
private static LcClient enLcClient = LcClient.builder(HttpClient.SiteEnum.EN).executorHttp(executorHttp).build();
37+
private static LcClient cnLcClient = LcClient.builder(HttpClient.SiteEnum.CN).executorHttp(executorHttp).build();
2538
private static final CookieManager cookieManager = new CookieManager(null, (uri, cookie) -> {
2639
if (uri == null || cookie == null || uri.getHost().equals("hm.baidu.com")) {
2740
return false;
@@ -33,18 +46,36 @@ public class HttpRequestUtils {
3346
CookieHandler.setDefault(cookieManager);
3447
}
3548

49+
private static HttpResponse buildResp(com.shuzijun.lc.http.HttpResponse response, HttpResponse httpResponse) {
50+
httpResponse.setUrl(response.getHttpRequest().getUrl());
51+
httpResponse.setStatusCode(response.getStatusCode());
52+
httpResponse.setBody(response.getBody());
53+
return httpResponse;
54+
}
55+
56+
private static Map<String, String> getHeader(String url) {
57+
if (url.contains(HttpClient.SiteEnum.EN.defaultEndpoint)){
58+
return enLcClient.getClient().getHeader();
59+
} else {
60+
return cnLcClient.getClient().getHeader();
61+
}
62+
}
63+
3664
@NotNull
3765
public static HttpResponse executeGet(HttpRequest httpRequest) {
3866

3967
return CacheProcessor.processor(httpRequest, request -> {
68+
4069
HttpResponse httpResponse = new HttpResponse();
4170
try {
42-
HttpRequests.request(request.getUrl()).
43-
throwStatusCodeException(false).
44-
tuner(new HttpRequestTuner(request)).
45-
connect(new HttpResponseProcessor(request, httpResponse));
71+
com.shuzijun.lc.http.HttpRequest.HttpRequestBuilder builder = com.shuzijun.lc.http.HttpRequest.
72+
builderGet(request.getUrl()).body(request.getBody()).addHeader(getHeader(request.getUrl()));
73+
if (request.getHeader() != null) {
74+
builder.addHeader(request.getHeader());
75+
}
76+
return buildResp(executorHttp.executeGet(builder.build()), httpResponse);
4677

47-
} catch (IOException e) {
78+
} catch (LcException e) {
4879
LogUtils.LOG.error("HttpRequestUtils request error:", e);
4980
httpResponse.setStatusCode(-1);
5081
}
@@ -59,11 +90,13 @@ public static HttpResponse executePost(HttpRequest httpRequest) {
5990
return CacheProcessor.processor(httpRequest, request -> {
6091
HttpResponse httpResponse = new HttpResponse();
6192
try {
62-
HttpRequests.post(request.getUrl(), request.getContentType())
63-
.throwStatusCodeException(false)
64-
.tuner(new HttpRequestTuner(request))
65-
.connect(new HttpResponseProcessor(request, httpResponse));
66-
} catch (IOException e) {
93+
com.shuzijun.lc.http.HttpRequest.HttpRequestBuilder builder = com.shuzijun.lc.http.HttpRequest.
94+
builderPost(request.getUrl(), request.getContentType()).body(request.getBody()).addHeader(getHeader(request.getUrl()));
95+
if (request.getHeader() != null) {
96+
builder.addHeader(request.getHeader());
97+
}
98+
return buildResp(executorHttp.executePost(builder.build()), httpResponse);
99+
} catch (LcException e) {
67100
LogUtils.LOG.error("HttpRequestUtils request error:", e);
68101
httpResponse.setStatusCode(-1);
69102
}
@@ -75,11 +108,13 @@ public static HttpResponse executePut(HttpRequest httpRequest) {
75108
return CacheProcessor.processor(httpRequest, request -> {
76109
HttpResponse httpResponse = new HttpResponse();
77110
try {
78-
HttpRequests.put(request.getUrl(), request.getContentType())
79-
.throwStatusCodeException(false)
80-
.tuner(new HttpRequestTuner(request))
81-
.connect(new HttpResponseProcessor(request, httpResponse));
82-
} catch (IOException e) {
111+
com.shuzijun.lc.http.HttpRequest.HttpRequestBuilder builder = com.shuzijun.lc.http.HttpRequest.
112+
builderPut(request.getUrl(), request.getContentType()).body(request.getBody()).addHeader(getHeader(request.getUrl()));
113+
if (request.getHeader() != null) {
114+
builder.addHeader(request.getHeader());
115+
}
116+
return buildResp(executorHttp.executePut(builder.build()), httpResponse);
117+
} catch (LcException e) {
83118
LogUtils.LOG.error("HttpRequestUtils request error:", e);
84119
httpResponse.setStatusCode(-1);
85120
}
@@ -88,17 +123,8 @@ public static HttpResponse executePut(HttpRequest httpRequest) {
88123
}
89124

90125
public static String getToken() {
91-
if (cookieManager.getCookieStore().getCookies() == null) {
92-
return null;
93-
}
94-
for (HttpCookie cookie : cookieManager.getCookieStore().getCookies()) {
95-
if (StringUtils.isNotBlank(cookie.getDomain()) &&
96-
cookie.getDomain().toLowerCase().contains(URLUtils.getLeetcodeHost()) && "csrftoken".equals(cookie.getName())) {
97-
return cookie.getValue();
98-
}
99-
100-
}
101-
return null;
126+
Map<String,String> headerMap = getHeader(URLUtils.getLeetcodeHost());
127+
return headerMap.get("x-csrftoken");
102128
}
103129

104130
public static boolean isLogin(Project project) {
@@ -110,87 +136,15 @@ public static boolean isLogin(Project project) {
110136
}
111137

112138
public static void setCookie(List<HttpCookie> cookieList) {
113-
114-
cookieManager.getCookieStore().removeAll();
115-
for (HttpCookie cookie : cookieList) {
116-
cookie.setVersion(0);
117-
cookieManager.getCookieStore().add(null, cookie);
118-
}
139+
enLcClient.getClient().cookieStore().clearCookie(URLUtils.getLeetcodeHost());
140+
enLcClient.getClient().cookieStore().addCookie(URLUtils.getLeetcodeHost(),cookieList);
119141
}
120142

121143
public static void resetHttpclient() {
122-
cookieManager.getCookieStore().removeAll();
123-
}
124-
125-
126-
private static void defaultHeader(HttpRequest httpRequest) {
127-
Map<String, String> header = httpRequest.getHeader();
128-
header.putIfAbsent(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36");
129-
header.putIfAbsent(HttpHeaders.ACCEPT, "*/*");
130-
//header.putIfAbsent(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate, br");
131-
header.putIfAbsent(HttpHeaders.ACCEPT_LANGUAGE, "zh-CN,zh;q=0.9");
132-
header.putIfAbsent("origin", URLUtils.getLeetcodeUrl());
133-
//header.putIfAbsent(":authority", URLUtils.getLeetcodeHost());
134-
//header.putIfAbsent(":scheme", "https");
135-
}
136-
137-
private static class HttpRequestTuner implements HttpRequests.ConnectionTuner {
138-
139-
private final HttpRequest httpRequest;
140-
141-
public HttpRequestTuner(HttpRequest httpRequest) {
142-
this.httpRequest = httpRequest;
143-
}
144-
145-
@Override
146-
public void tune(@NotNull URLConnection urlConnection) throws IOException {
147-
148-
if (StringUtils.isNotBlank(getToken()) && (urlConnection.getURL().toString().contains(URLUtils.leetcode) || urlConnection.getURL().toString().contains(URLUtils.leetcodecn))) {
149-
urlConnection.addRequestProperty("x-csrftoken", getToken());
150-
}
151-
urlConnection.addRequestProperty("referer", urlConnection.getURL().toString());
152-
//urlConnection.addRequestProperty(":path", urlConnection.getURL().getPath());
153-
154-
defaultHeader(httpRequest);
155-
httpRequest.getHeader().forEach(urlConnection::addRequestProperty);
156-
}
144+
enLcClient.getClient().cookieStore().clearCookie(URLUtils.getLeetcodeHost());
157145
}
158146

159147

160-
private static class HttpResponseProcessor implements HttpRequests.RequestProcessor<HttpResponse> {
161-
162-
private final HttpRequest httpRequest;
163-
private final HttpResponse httpResponse;
164-
165-
public HttpResponseProcessor(HttpRequest httpRequest, HttpResponse httpResponse) {
166-
this.httpRequest = httpRequest;
167-
this.httpResponse = httpResponse;
168-
}
169-
170-
@Override
171-
public HttpResponse process(@NotNull HttpRequests.Request request) throws IOException {
172-
173-
if (StringUtils.isNoneBlank(httpRequest.getBody())) {
174-
request.write(httpRequest.getBody());
175-
}
176-
177-
URLConnection urlConnection = request.getConnection();
178-
179-
if (!(urlConnection instanceof HttpURLConnection)) {
180-
httpResponse.setStatusCode(-1);
181-
return httpResponse;
182-
} else {
183-
httpResponse.setStatusCode(((HttpURLConnection) urlConnection).getResponseCode());
184-
}
185-
httpResponse.setUrl(urlConnection.getURL().toString());
186-
try {
187-
httpResponse.setBody(request.readString());
188-
} catch (IOException ignore) {
189-
}
190-
return httpResponse;
191-
}
192-
}
193-
194148
private static class CacheProcessor {
195149
public static HttpResponse processor(HttpRequest httpRequest, HttpRequestUtils.Callable<HttpResponse> callable) {
196150

@@ -222,4 +176,38 @@ private interface Callable<V> {
222176
V call(HttpRequest request);
223177
}
224178

179+
180+
private static class MyExecutorHttp extends DefaultExecutoHttp {
181+
@Override
182+
public OkHttpClient getRequestClient() {
183+
final HttpConfigurable httpConfigurable = HttpConfigurable.getInstance();
184+
if (!httpConfigurable.USE_HTTP_PROXY && !httpConfigurable.USE_PROXY_PAC) {
185+
return super.getRequestClient();
186+
}
187+
final IdeaWideProxySelector ideaWideProxySelector = new IdeaWideProxySelector(httpConfigurable);
188+
OkHttpClient.Builder builder = super.getRequestClient().newBuilder().proxySelector(ideaWideProxySelector);
189+
if (httpConfigurable.PROXY_AUTHENTICATION) {
190+
final IdeaWideAuthenticator ideaWideAuthenticator = new IdeaWideAuthenticator(httpConfigurable);
191+
final okhttp3.Authenticator proxyAuthenticator = getProxyAuthenticator(ideaWideAuthenticator);
192+
builder.proxyAuthenticator(proxyAuthenticator);
193+
}
194+
return builder.build();
195+
}
196+
197+
private okhttp3.Authenticator getProxyAuthenticator(IdeaWideAuthenticator ideaWideAuthenticator) {
198+
okhttp3.Authenticator proxyAuthenticator = null;
199+
200+
if (Objects.nonNull(ideaWideAuthenticator)) {
201+
proxyAuthenticator = (route, response) -> {
202+
final PasswordAuthentication authentication = ideaWideAuthenticator.getPasswordAuthentication();
203+
final String credential = Credentials.basic(authentication.getUserName(), Arrays.toString(authentication.getPassword()));
204+
return response.request().newBuilder()
205+
.header("Proxy-Authorization", credential)
206+
.build();
207+
};
208+
}
209+
return proxyAuthenticator;
210+
}
211+
}
225212
}
213+

src/main/java/com/shuzijun/leetcode/plugin/utils/URLUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class URLUtils {
2323
private static String leetcodeSubmissions = "/submissions/detail/";
2424
private static String leetcodeTags = "/problems/api/tags/";
2525
private static String leetcodeFavorites = "/problems/api/favorites/";
26-
private static String leetcodeVerify = "/problemset/all/";
26+
private static String leetcodeVerify = "/problemset/";
2727
private static String leetcodeProgress = "/api/progress/all/";
2828
private static String leetcodeSession = "/session/";
2929
private static String leetcodeCardInfo = "/problems/api/card-info/";

src/main/java/com/shuzijun/leetcode/plugin/window/navigator/AllNavigatorTable.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ public Object getValue(QuestionView question, int columnIndex) {
133133

134134
if (columnIndex == 2) {
135135
Integer level = question.getLevel();
136+
if (level == null) {
137+
return null;
138+
}
136139
if (level == 1) {
137140
return "Easy";
138141
} else if (level == 2) {

0 commit comments

Comments
 (0)