Skip to content

Commit 9ac4894

Browse files
committed
fix: search api use base64 encode
1 parent 047f679 commit 9ac4894

7 files changed

Lines changed: 63 additions & 12 deletions

File tree

extensions/intellij/src/main/java/com/voidmuse/idea/plugin/protocol/CallJavaHandlerImpl.java

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.io.BufferedReader;
4545
import java.io.File;
4646
import java.io.FileWriter;
47+
import java.io.InputStream;
4748
import java.io.InputStreamReader;
4849
import java.net.HttpURLConnection;
4950
import java.util.concurrent.ConcurrentHashMap;
@@ -53,6 +54,7 @@
5354
import java.util.List;
5455
import java.util.Locale;
5556
import java.util.Map;
57+
import java.nio.charset.StandardCharsets;
5658

5759
import static com.voidmuse.idea.plugin.protocol.CallJavaProtocol.*;
5860

@@ -450,25 +452,70 @@ private String getFileContent(Project project, String path) {
450452

451453
// 获取URL内容的实现
452454
private String getUrlContent(String urlString) {
455+
HttpURLConnection connection = null;
456+
String result = "";
453457
try {
454458
URL url = new URL(urlString);
455-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
459+
connection = (HttpURLConnection) url.openConnection();
456460
connection.setRequestMethod("GET");
461+
connection.setInstanceFollowRedirects(true);
457462
connection.setConnectTimeout(10000);
458463
connection.setReadTimeout(10000);
459464

460-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
465+
// 许多站点(含 Cloudflare/Zendesk)会拦截无UA的请求,补充常见请求头
466+
connection.setRequestProperty("User-Agent",
467+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) VoidMuse-IntelliJ/1.0 Chrome/118.0 Safari/537.36");
468+
connection.setRequestProperty("Accept",
469+
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
470+
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
471+
connection.setRequestProperty("Accept-Encoding", "gzip,deflate");
472+
// 使用站点根作为通用 Referer,有助于绕过部分拦截
473+
connection.setRequestProperty("Referer", url.getProtocol() + "://" + url.getHost() + "/");
474+
475+
int status = connection.getResponseCode();
476+
InputStream stream = (status >= 200 && status < 300)
477+
? connection.getInputStream()
478+
: connection.getErrorStream();
479+
480+
if (stream == null) {
481+
throw new java.io.IOException("HTTP " + status + " with empty body");
482+
}
483+
484+
// 处理压缩响应
485+
String contentEncoding = connection.getContentEncoding();
486+
if (contentEncoding != null) {
487+
String enc = contentEncoding.toLowerCase();
488+
if (enc.contains("gzip")) {
489+
stream = new java.util.zip.GZIPInputStream(stream);
490+
} else if (enc.contains("deflate")) {
491+
stream = new java.util.zip.InflaterInputStream(stream);
492+
}
493+
}
494+
495+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
461496
StringBuilder content = new StringBuilder();
462497
String line;
463498
while ((line = reader.readLine()) != null) {
464499
content.append(line).append("\n");
465500
}
466-
return content.toString();
501+
if (status >= 200 && status < 300) {
502+
LOG.info("Successfully fetched content from URL: " + urlString);
503+
result = content.toString();
504+
505+
} else {
506+
LOG.error("Failed to fetch content from URL: " + urlString + " with HTTP status: " + status);
507+
result = "Error: HTTP " + status + " " + content.toString();
508+
}
467509
}
468510
} catch (Exception e) {
469511
LOG.error("Error getting URL content: " + urlString, e);
470-
return "Error: " + e.getMessage();
512+
result = "Error: " + e.getMessage();
513+
} finally {
514+
if (connection != null) {
515+
connection.disconnect();
516+
}
471517
}
518+
return Base64.encode(result);
472519
}
473520

474521
// 执行命令的实现

extensions/vscode/src/extension.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,10 @@ export class VoidmuseWebViewProvider implements vscode.WebviewViewProvider {
445445
break;
446446
case "getUrlContent":
447447
try {
448-
response = await getUrlContent(arg.url);
449-
console.log(response);
448+
const urlContent = await getUrlContent(arg.url);
449+
console.log(urlContent);
450+
const encodedContent = Buffer.from(urlContent).toString('base64');
451+
response = encodedContent;
450452
} catch (error) {
451453
vscode.window.showErrorMessage("get html content error!");
452454
}

extensions/vscode/src/services/EmbeddingsSettings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class EmbeddingsSettings {
2121

2222
/**
2323
* function: 获取embeddings的模型名称,配置内容:
24-
* "global:embeddingModels": "[{\"key\":\"1752222055608\",\"name\":\"gte-Qwen2-1.5B-instruct-f16:latest\",\"provider\":\"OpenAI\",\"enabled\":true,\"modelId\":\"gte-Qwen2-1.5B-instruct-f16:latest\",\"apiKey\":\"~\",\"baseUrl\":\"https://lpfm2-ollama.yy.com\",\"isCustomModel\":true}]",
24+
* "global:embeddingModels": "[{\"key\":\"1752222055608\",\"name\":\"gte-Qwen2-1.5B-instruct-f16:latest\",\"provider\":\"OpenAI\",\"enabled\":true,\"modelId\":\"gte-Qwen2-1.5B-instruct-f16:latest\",\"apiKey\":\"~\",\"baseUrl\":\"xxx\",\"isCustomModel\":true}]",
2525
"global:selectedEmbeddingModel": "1752222055608",
2626
"global:isAutoEmbedding": "false",
2727
* 遍历embeddingModels,转换为json对象,取enabled=true的name

gui/src/api/IdeaAsyncMgr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ interface CallbackMessage {
5252
const callJavaCallback = async (message: string): Promise<void> => {
5353
try {
5454
// message is json string data, convert to corresponding object
55-
console.log(`callJavaCallback message: ${message}`);
55+
console.log(`callJavaCallback message: ${message.substring(0, 100)}`);
5656
const parsedMessage: CallbackMessage = JSON.parse(message); // Convert json string to object
5757
if (parsedMessage.requestId && IdeaAsyncMgr.pendingRequests.has(parsedMessage.requestId)) {
5858
const { onSuccess, onFailure } = IdeaAsyncMgr.pendingRequests.get(parsedMessage.requestId)!;

gui/src/api/implementations/IntelliJDE.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@ export class IntelliJIDE implements IDEInterface {
564564
IdeaAsyncMgr.sendMessage(param,
565565
function onSuccess(response) {
566566
console.log('IntelliJ getUrlContent callback success:', url);
567-
resolve(response);
567+
const decodedResponse = base64Decode(response);
568+
resolve(decodedResponse);
568569
},
569570
function onFailure(error_code, error_message) {
570571
console.error('IntelliJ getUrlContent callback failed:', error_code, error_message);

gui/src/api/implementations/VscodeIDE.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,8 @@ export class VscodeIDE implements IDEInterface {
551551
vscodeMgr.sendMessage(param,
552552
function onSuccess(response) {
553553
console.log('vscode getUrlContent callback success:', url);
554-
resolve(response);
554+
const decodedResponse = base64Decode(response);
555+
resolve(decodedResponse);
555556
},
556557
function onFailure(error_code, error_message) {
557558
console.error('vscode getUrlContent callback failed:', error_code, error_message);

gui/src/services/search/WebContentExtractor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ export class WebContentExtractor {
103103

104104
try {
105105
// Use allorigins proxy service directly
106-
const html = await this.getContentFromProxy(params.url);
107-
// const html = await this.getContentFromIDE(params.url);
106+
// const html = await this.getContentFromProxy(params.url);
107+
const html = await this.getContentFromIDE(params.url);
108108

109109
// Parse HTML content
110110
const result = this.parseHtmlContent(html, params.url, maxLength);

0 commit comments

Comments
 (0)