Skip to content

Commit

Permalink
fix get HttpRequestBase thread safe issues through BaseHttpMethod (al…
Browse files Browse the repository at this point in the history
  • Loading branch information
Maijh97 authored Jul 29, 2020
1 parent 5b76fd0 commit e9152a9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.nacos.common.http;

import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.http.handler.ResponseHandler;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query;
Expand Down Expand Up @@ -97,19 +98,11 @@ protected HttpRequestBase build(String url, Header header, String method) throws

protected HttpRequestBase build(String url, Header header, Object body, String method) throws Exception {

BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method);
httpMethod.init(url);
httpMethod.initHeader(header);
httpMethod.initEntity(body, header.getValue("Content-Type"));
return httpMethod.getRequestBase();
}

private Header convertHeader(org.apache.http.Header[] headers) {
final Header nHeader = Header.newInstance();
for (org.apache.http.Header header : headers) {
nHeader.addParam(header.getName(), header.getValue());
}
return nHeader;
final BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method);
final HttpRequestBase httpRequestBase = httpMethod.init(url);
HttpUtils.initRequestHeader(httpRequestBase, header);
HttpUtils.initRequestEntity(httpRequestBase, body, header.getValue(HttpHeaderConsts.CONTENT_TYPE));
return httpRequestBase;
}

public static class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,9 @@

package com.alibaba.nacos.common.http;

import com.alibaba.nacos.common.http.handler.RequestHandler;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.utils.HttpMethod;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.nacos.common.utils.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
Expand All @@ -39,9 +27,6 @@
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;

/**
* Base http method.
Expand Down Expand Up @@ -139,78 +124,18 @@ protected HttpRequestBase createRequest(String url) {

private String name;

private HttpRequest requestBase;

BaseHttpMethod(String name) {
this.name = name;
}

public void init(String url) {
requestBase = createRequest(url);
public HttpRequestBase init(String url) {
return createRequest(url);
}

protected HttpRequestBase createRequest(String url) {
throw new UnsupportedOperationException();
}

/**
* Init http header.
*
* @param header header
*/
public void initHeader(Header header) {
Iterator<Map.Entry<String, String>> iterator = header.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
requestBase.setHeader(entry.getKey(), entry.getValue());
}
}

/**
* Init http entity.
*
* @param body body
* @param mediaType mediaType {@link ContentType}
* @throws Exception exception
*/
public void initEntity(Object body, String mediaType) throws Exception {
if (body == null) {
return;
}
if (requestBase instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestBase;
ContentType contentType = ContentType.create(mediaType);
StringEntity entity = new StringEntity(RequestHandler.parse(body), contentType);
request.setEntity(entity);
}
}

/**
* Init request from entity map.
*
* @param body body map
* @param charset charset of entity
* @throws Exception exception
*/
public void initFromEntity(Map<String, String> body, String charset) throws Exception {
if (body == null || body.isEmpty()) {
return;
}
List<NameValuePair> params = new ArrayList<NameValuePair>(body.size());
for (Map.Entry<String, String> entry : body.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
if (requestBase instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestBase;
HttpEntity entity = new UrlEncodedFormEntity(params, charset);
request.setEntity(entity);
}
}

public HttpRequestBase getRequestBase() {
return (HttpRequestBase) requestBase;
}

/**
* Value of {@link BaseHttpMethod}.
*
Expand Down
68 changes: 68 additions & 0 deletions common/src/main/java/com/alibaba/nacos/common/http/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,25 @@

package com.alibaba.nacos.common.http;

import com.alibaba.nacos.common.http.handler.RequestHandler;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query;
import com.alibaba.nacos.common.utils.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand All @@ -40,6 +51,63 @@ public final class HttpUtils {

private static final Pattern CONTEXT_PATH_MATCH = Pattern.compile("(\\/)\\1+");

/**
* Init http header.
*
* @param requestBase requestBase {@link HttpRequestBase}
* @param header header
*/
public static void initRequestHeader(HttpRequestBase requestBase, Header header) {
Iterator<Map.Entry<String, String>> iterator = header.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
requestBase.setHeader(entry.getKey(), entry.getValue());
}
}

/**
* Init http entity.
*
* @param requestBase requestBase {@link HttpRequestBase}
* @param body body
* @param mediaType mediaType {@link ContentType}
* @throws Exception exception
*/
public static void initRequestEntity(HttpRequestBase requestBase, Object body, String mediaType) throws Exception {
if (body == null) {
return;
}
if (requestBase instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestBase;
ContentType contentType = ContentType.create(mediaType);
StringEntity entity = new StringEntity(RequestHandler.parse(body), contentType);
request.setEntity(entity);
}
}

/**
* Init request from entity map.
*
* @param requestBase requestBase {@link HttpRequestBase}
* @param body body map
* @param charset charset of entity
* @throws Exception exception
*/
public static void initRequestFromEntity(HttpRequestBase requestBase, Map<String, String> body, String charset) throws Exception {
if (body == null || body.isEmpty()) {
return;
}
List<NameValuePair> params = new ArrayList<NameValuePair>(body.size());
for (Map.Entry<String, String> entry : body.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
if (requestBase instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestBase;
HttpEntity entity = new UrlEncodedFormEntity(params, charset);
request.setEntity(entity);
}
}

/**
* Build URL.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.http.BaseHttpMethod;
import com.alibaba.nacos.common.http.HttpClientConfig;
import com.alibaba.nacos.common.http.HttpUtils;
import com.alibaba.nacos.common.http.client.response.DefaultClientHttpResponse;
import com.alibaba.nacos.common.http.client.response.HttpClientResponse;
import com.alibaba.nacos.common.http.param.Header;
Expand Down Expand Up @@ -56,19 +57,18 @@ public HttpClientResponse execute(URI uri, String httpMethod, RequestHttpEntity
}

static HttpRequestBase build(URI uri, String method, RequestHttpEntity requestHttpEntity) throws Exception {
Header headers = requestHttpEntity.getHeaders();
BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method);
httpMethod.init(uri.toString());
httpMethod.initHeader(headers);
final Header headers = requestHttpEntity.getHeaders();
final BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method);
final HttpRequestBase httpRequestBase = httpMethod.init(uri.toString());
HttpUtils.initRequestHeader(httpRequestBase, headers);
if (MediaType.APPLICATION_FORM_URLENCODED.equals(headers.getValue(HttpHeaderConsts.CONTENT_TYPE))
&& requestHttpEntity.getBody() instanceof Map) {
httpMethod.initFromEntity((Map<String, String>) requestHttpEntity.getBody(), headers.getCharset());
HttpUtils.initRequestFromEntity(httpRequestBase, (Map<String, String>) requestHttpEntity.getBody(), headers.getCharset());
} else {
httpMethod.initEntity(requestHttpEntity.getBody(), headers.getValue(HttpHeaderConsts.CONTENT_TYPE));
HttpUtils.initRequestEntity(httpRequestBase, requestHttpEntity.getBody(), headers.getValue(HttpHeaderConsts.CONTENT_TYPE));
}
HttpRequestBase requestBase = httpMethod.getRequestBase();
replaceDefaultConfig(requestBase, requestHttpEntity.getHttpClientConfig());
return requestBase;
replaceDefaultConfig(httpRequestBase, requestHttpEntity.getHttpClientConfig());
return httpRequestBase;
}

/**
Expand Down

0 comments on commit e9152a9

Please sign in to comment.