Skip to content

Commit

Permalink
api request parse host
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSen-qn committed Jul 19, 2024
1 parent 0cb9ec1 commit 10778d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/main/java/com/qiniu/storage/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import com.qiniu.http.Client;
import com.qiniu.http.MethodType;
import com.qiniu.http.RequestStreamBody;
import com.qiniu.util.Auth;
import com.qiniu.util.Json;
import com.qiniu.util.StringMap;
import com.qiniu.util.StringUtils;
import com.qiniu.util.*;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
Expand Down Expand Up @@ -461,7 +458,16 @@ public String getHost() throws QiniuException {
}

void setHost(String host) {
this.host = host;
URL tmpUrl = UrlUtils.parseHost(host);
if (tmpUrl == null) {
this.host = null;
return;
}

this.host = tmpUrl.getHost();
if (tmpUrl.getPort() >= 0) {
this.port = tmpUrl.getPort();
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/qiniu/util/UrlUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.qiniu.util;

import java.io.CharArrayWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.BitSet;

Expand Down Expand Up @@ -191,6 +193,19 @@ public static String removeHostScheme(String host) {
return host;
}

public static URL parseHost(String host) {
if (StringUtils.isNullOrEmpty(host)) {
return null;
}

String tmpHost = setHostScheme(host, true);
try {
return new URL(tmpHost);
} catch (MalformedURLException e) {
return null;
}
}


/**
* 如果 host 包含 scheme 则优先使用 host 中包含的 scheme
Expand Down

0 comments on commit 10778d4

Please sign in to comment.