Skip to content

Commit

Permalink
Manually decode the uri string
Browse files Browse the repository at this point in the history
  • Loading branch information
dilanSachi committed Jun 13, 2023
1 parent e1c4097 commit abfd693
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand All @@ -56,12 +55,10 @@
import static io.ballerina.stdlib.http.api.HttpConstants.AUTHORIZATION_HEADER;
import static io.ballerina.stdlib.http.api.HttpConstants.BEARER_AUTHORIZATION_HEADER;
import static io.ballerina.stdlib.http.api.HttpConstants.DEFAULT_HOST;
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_SCHEME;
import static io.ballerina.stdlib.http.api.HttpConstants.JWT_DECODER_CLASS_NAME;
import static io.ballerina.stdlib.http.api.HttpConstants.JWT_DECODE_METHOD_NAME;
import static io.ballerina.stdlib.http.api.HttpConstants.JWT_INFORMATION;
import static io.ballerina.stdlib.http.api.HttpConstants.REQUEST_CTX_MEMBERS;
import static io.ballerina.stdlib.http.api.HttpConstants.SCHEME_SEPARATOR;
import static io.ballerina.stdlib.http.api.HttpConstants.WHITESPACE;
import static io.ballerina.stdlib.http.api.HttpErrorType.SERVICE_NOT_FOUND_ERROR;
import static io.ballerina.stdlib.http.api.HttpUtil.getParameterTypes;
Expand Down Expand Up @@ -98,19 +95,21 @@ public static HttpService findService(HTTPServicesRegistry servicesRegistry, Htt
Map<String, Map<String, String>> matrixParams = new HashMap<>();
String uriWithoutMatrixParams = URIUtil.extractMatrixParams(rawUri, matrixParams, inboundReqMsg);

URI validatedUri = getValidatedURI(HTTP_SCHEME + SCHEME_SEPARATOR + uriWithoutMatrixParams);
String[] splittedUri = uriWithoutMatrixParams.split("\\?");
String queryString = splittedUri.length > 1 ? splittedUri[1] : "";
String rawPath = splittedUri[0];

String basePath = servicesRegistry.findTheMostSpecificBasePath(validatedUri.getRawPath(),
String basePath = servicesRegistry.findTheMostSpecificBasePath(rawPath,
servicesOnInterface, sortedServiceURIs);

if (basePath == null) {
String message = "no matching service found for path : " + validatedUri.getRawPath();
String message = "no matching service found for path : " + rawPath;
throw HttpUtil.createHttpStatusCodeError(SERVICE_NOT_FOUND_ERROR, message);
}

HttpService service = servicesOnInterface.get(basePath);
if (!forInterceptors) {
setInboundReqProperties(inboundReqMsg, validatedUri, basePath);
setInboundReqProperties(inboundReqMsg, rawPath, basePath, queryString);
inboundReqMsg.setProperty(HttpConstants.RAW_URI, rawUri);
inboundReqMsg.setProperty(HttpConstants.TO, uriWithoutMatrixParams);
inboundReqMsg.setProperty(HttpConstants.MATRIX_PARAMS, matrixParams);
Expand Down Expand Up @@ -159,18 +158,20 @@ public static InterceptorService findInterceptorService(HTTPInterceptorServicesR
inboundReqMsg.setProperty(HttpConstants.TO, uriWithoutMatrixParams);
inboundReqMsg.setProperty(HttpConstants.MATRIX_PARAMS, matrixParams);

URI validatedUri = getValidatedURI(HTTP_SCHEME + SCHEME_SEPARATOR + uriWithoutMatrixParams);
String[] splittedUri = uriWithoutMatrixParams.split("\\?");
String queryString = splittedUri.length > 1 ? splittedUri[1] : "";
String rawPath = splittedUri[0];

String basePath = servicesRegistry.findTheMostSpecificBasePath(validatedUri.getRawPath(),
String basePath = servicesRegistry.findTheMostSpecificBasePath(rawPath,
servicesOnInterface, sortedServiceURIs);

if (basePath == null) {
String message = "no matching service found for path : " + validatedUri.getRawPath();
String message = "no matching service found for path : " + rawPath;
throw HttpUtil.createHttpStatusCodeError(SERVICE_NOT_FOUND_ERROR, message);
}

InterceptorService service = servicesOnInterface.get(basePath);
setInboundReqProperties(inboundReqMsg, validatedUri, basePath);
setInboundReqProperties(inboundReqMsg, rawPath, basePath, queryString);
return service;
} catch (Exception e) {
if (!(e instanceof BError)) {
Expand All @@ -180,23 +181,13 @@ public static InterceptorService findInterceptorService(HTTPInterceptorServicesR
}
}

private static void setInboundReqProperties(HttpCarbonMessage inboundReqMsg, URI requestUri, String basePath) {
String subPath = URIUtil.getSubPath(requestUri.getRawPath(), basePath);
private static void setInboundReqProperties(HttpCarbonMessage inboundReqMsg, String rawPath,
String basePath, String rawQuery) {
String subPath = URIUtil.getSubPath(rawPath, basePath);
inboundReqMsg.setProperty(HttpConstants.BASE_PATH, basePath);
inboundReqMsg.setProperty(HttpConstants.SUB_PATH, subPath);
inboundReqMsg.setProperty(HttpConstants.QUERY_STR, requestUri.getQuery());
//store query params comes with request as it is
inboundReqMsg.setProperty(HttpConstants.RAW_QUERY_STR, requestUri.getRawQuery());
}

public static URI getValidatedURI(String uriStr) {
URI requestUri;
try {
requestUri = URI.create(uriStr);
} catch (IllegalArgumentException e) {
throw new BallerinaConnectorException(e.getMessage());
}
return requestUri;
inboundReqMsg.setProperty(HttpConstants.RAW_QUERY_STR, rawQuery);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -70,7 +71,7 @@ public static void populateQueryParamMap(String queryParamString, BMap<BString,
List<String> values = new ArrayList<>();
Set<String> uniqueValues = new HashSet<>();
for (String val : queryParamValue.split(",")) {
String decodedValue = URLDecoder.decode(val, "UTF-8");
String decodedValue = URLDecoder.decode(val, StandardCharsets.UTF_8);
if (uniqueValues.add(decodedValue)) {
values.add(decodedValue);
}
Expand Down

0 comments on commit abfd693

Please sign in to comment.