Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename HttpRequestWrapper to HttpRequestContext and use own models in it and hooks interfaces #1330

Merged
merged 5 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.vertx.core.Future;
import org.prebid.server.cookie.UidsCookie;
import org.prebid.server.cookie.proto.Uids;
import org.prebid.server.model.HttpRequestWrapper;
import org.prebid.server.model.HttpRequestContext;
import org.prebid.server.settings.model.Account;

/**
Expand All @@ -24,7 +24,7 @@ public interface BidResponsePostProcessor {
* @param account {@link Account} fetched from request
* @return a {@link Future} with (possibly modified) auction result
*/
Future<BidResponse> postProcess(HttpRequestWrapper httpRequest,
Future<BidResponse> postProcess(HttpRequestContext httpRequest,
UidsCookie uidsCookie,
BidRequest bidRequest,
BidResponse bidResponse,
Expand All @@ -42,7 +42,7 @@ static BidResponsePostProcessor noOp() {
*/
class NoOpBidResponsePostProcessor implements BidResponsePostProcessor {
@Override
public Future<BidResponse> postProcess(HttpRequestWrapper httpRequest,
public Future<BidResponse> postProcess(HttpRequestContext httpRequest,
UidsCookie uidsCookie,
BidRequest bidRequest,
BidResponse bidResponse,
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/prebid/server/auction/ExchangeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.iab.openrtb.response.SeatBid;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -60,6 +59,7 @@
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.metric.MetricName;
import org.prebid.server.metric.Metrics;
import org.prebid.server.model.CaseInsensitiveMultiMap;
import org.prebid.server.proto.openrtb.ext.ExtPrebidBidders;
import org.prebid.server.proto.openrtb.ext.request.BidAdjustmentMediaType;
import org.prebid.server.proto.openrtb.ext.request.ExtApp;
Expand Down Expand Up @@ -1013,7 +1013,7 @@ private Future<BidderResponse> invokeHooksAndRequestBids(AuctionContext auctionC
Timeout timeout,
BidderAliases aliases) {

final MultiMap headers = auctionContext.getHttpRequest().getHeaders();
final CaseInsensitiveMultiMap headers = auctionContext.getHttpRequest().getHeaders();
final boolean debugEnabled = auctionContext.getDebugContext().isDebugEnabled();

return hookStageExecutor.executeBidderRequestStage(bidderRequest, auctionContext)
Expand All @@ -1028,7 +1028,7 @@ private Future<BidderResponse> requestBidsOrRejectBidder(
HookStageExecutionResult<BidderRequestPayload> hookStageResult,
BidderRequest bidderRequest,
Timeout timeout,
MultiMap requestHeaders,
CaseInsensitiveMultiMap requestHeaders,
boolean debugEnabled,
BidderAliases aliases) {

Expand Down Expand Up @@ -1059,7 +1059,7 @@ private BidderResponse rejectBidderResponseOrProceed(HookStageExecutionResult<Bi
*/
private Future<BidderResponse> requestBids(BidderRequest bidderRequest,
Timeout timeout,
MultiMap requestHeaders,
CaseInsensitiveMultiMap requestHeaders,
boolean debugEnabled,
BidderAliases aliases) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package org.prebid.server.auction;

import de.malkusch.whoisServerList.publicSuffixList.PublicSuffixList;
import io.vertx.core.MultiMap;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.model.HttpRequestWrapper;
import org.prebid.server.model.CaseInsensitiveMultiMap;
import org.prebid.server.model.HttpRequestContext;
import org.prebid.server.util.HttpUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
Expand All @@ -28,7 +29,7 @@ public ImplicitParametersExtractor(PublicSuffixList psl) {
* Determines Referer by checking 'url_override' request parameter, or if it's empty 'Referer' header. Then if
* result is not blank and missing 'http://' prefix appends it.
*/
public String refererFrom(HttpRequestWrapper request) {
public String refererFrom(HttpRequestContext request) {
final String urlOverride = request.getQueryParams().get("url_override");
final String url = StringUtils.isNotBlank(urlOverride) ? urlOverride
: StringUtils.trimToNull(request.getHeaders().get(HttpUtil.REFERER_HEADER));
Expand Down Expand Up @@ -61,14 +62,22 @@ public String domainFrom(String host) throws PreBidException {
/**
* Determines IP-Address candidates by checking http headers and remote host address.
*/
public List<String> ipFrom(MultiMap headers, String host) {
public List<String> ipFrom(CaseInsensitiveMultiMap headers, String host) {
return ipFrom(headers::get, host);
}

public List<String> ipFrom(io.vertx.core.MultiMap headers, String host) {
return ipFrom(headers::get, host);
}

private List<String> ipFrom(Function<String, String> headerGetter, String host) {
final List<String> candidates = new ArrayList<>();
candidates.add(headers.get("True-Client-IP"));
final String xff = headers.get("X-Forwarded-For");
candidates.add(headerGetter.apply("True-Client-IP"));
final String xff = headerGetter.apply("X-Forwarded-For");
if (xff != null) {
candidates.addAll(Arrays.asList(xff.split(",")));
}
candidates.add(headers.get("X-Real-IP"));
candidates.add(headerGetter.apply("X-Real-IP"));
candidates.add(host);

return candidates.stream()
Expand All @@ -80,15 +89,15 @@ public List<String> ipFrom(MultiMap headers, String host) {
/**
* Determines User-Agent by checking 'User-Agent' http header.
*/
public String uaFrom(HttpRequestWrapper request) {
public String uaFrom(HttpRequestContext request) {
return StringUtils.trimToNull(request.getHeaders().get(HttpUtil.USER_AGENT_HEADER));
}

/**
* Determines the value of 'secure' flag by checking if 'X-Forwarded-Proto' contains 'value' or if HTTP request
* scheme is 'https'. Returns 1 if one of these conditions evaluates to true or null otherwise.
*/
public Integer secureFrom(HttpRequestWrapper httpRequest) {
public Integer secureFrom(HttpRequestContext httpRequest) {
return StringUtils.equalsIgnoreCase(httpRequest.getHeaders().get("X-Forwarded-Proto"), "https")
|| StringUtils.equalsIgnoreCase(httpRequest.getScheme(), "https")
? 1 : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.prebid.server.geolocation.model.GeoInfo;
import org.prebid.server.hooks.execution.model.HookExecutionContext;
import org.prebid.server.metric.MetricName;
import org.prebid.server.model.HttpRequestWrapper;
import org.prebid.server.model.HttpRequestContext;
import org.prebid.server.privacy.model.PrivacyContext;
import org.prebid.server.settings.model.Account;

Expand All @@ -20,7 +20,7 @@
@Value
public class AuctionContext {

HttpRequestWrapper httpRequest;
HttpRequestContext httpRequest;

UidsCookie uidsCookie;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.iab.openrtb.request.Site;
import com.iab.openrtb.request.User;
import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.web.RoutingContext;
Expand All @@ -30,8 +29,9 @@
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.metric.MetricName;
import org.prebid.server.model.CaseInsensitiveMultiMap;
import org.prebid.server.model.Endpoint;
import org.prebid.server.model.HttpRequestWrapper;
import org.prebid.server.model.HttpRequestContext;
import org.prebid.server.privacy.ccpa.Ccpa;
import org.prebid.server.privacy.gdpr.TcfDefinerService;
import org.prebid.server.proto.openrtb.ext.request.ExtMediaTypePriceGranularity;
Expand Down Expand Up @@ -149,7 +149,7 @@ public Future<AuctionContext> fromRequest(RoutingContext routingContext, long st
* Creates {@link BidRequest} and sets properties which were not set explicitly by the client, but can be
* updated by values derived from headers and other request attributes.
*/
private Future<BidRequest> parseBidRequest(HttpRequestWrapper httpRequest) {
private Future<BidRequest> parseBidRequest(HttpRequestContext httpRequest) {
final String tagId = httpRequest.getQueryParams().get(TAG_ID_REQUEST_PARAM);
if (StringUtils.isBlank(tagId)) {
return Future.failedFuture(new InvalidRequestException("AMP requests require an AMP tag_id"));
Expand Down Expand Up @@ -181,7 +181,7 @@ private Future<BidRequest> updateBidRequest(AuctionContext auctionContext) {
final Account account = auctionContext.getAccount();
final String accountId = account != null ? account.getId() : null;

final HttpRequestWrapper httpRequest = auctionContext.getHttpRequest();
final HttpRequestContext httpRequest = auctionContext.getHttpRequest();

return storedRequestProcessor.processAmpRequest(accountId, storedRequestId, receivedBidRequest)
.map(bidRequest -> validateStoredBidRequest(storedRequestId, bidRequest))
Expand Down Expand Up @@ -233,7 +233,7 @@ private static BidRequest validateStoredBidRequest(String tagId, BidRequest bidR
* - Sets {@link BidRequest}.test = 1 if it was passed in {@link RoutingContext}
* - Updates {@link BidRequest}.ext.prebid.amp.data with all query parameters
*/
private BidRequest fillExplicitParameters(BidRequest bidRequest, HttpRequestWrapper httpRequest) {
private BidRequest fillExplicitParameters(BidRequest bidRequest, HttpRequestContext httpRequest) {
final List<Imp> imps = bidRequest.getImp();
// Force HTTPS as AMP requires it, but pubs can forget to set it.
final Imp imp = imps.get(0);
Expand Down Expand Up @@ -309,15 +309,15 @@ private BidRequest fillExplicitParameters(BidRequest bidRequest, HttpRequestWrap
/**
* Returns debug flag from request query string if it is equal to either 0 or 1, or null if otherwise.
*/
private static Integer debugFromQueryStringParam(HttpRequestWrapper httpRequest) {
private static Integer debugFromQueryStringParam(HttpRequestContext httpRequest) {
final String debug = httpRequest.getQueryParams().get(DEBUG_REQUEST_PARAM);
return Objects.equals(debug, "1") ? Integer.valueOf(1) : Objects.equals(debug, "0") ? 0 : null;
}

/**
* Extracts parameters from http request and overrides corresponding attributes in {@link BidRequest}.
*/
private BidRequest overrideParameters(BidRequest bidRequest, HttpRequestWrapper httpRequest, List<String> errors) {
private BidRequest overrideParameters(BidRequest bidRequest, HttpRequestContext httpRequest, List<String> errors) {
final String requestConsentParam = httpRequest.getQueryParams().get(CONSENT_PARAM);
final String requestGdprConsentParam = httpRequest.getQueryParams().get(GDPR_CONSENT_PARAM);
final String consentString = ObjectUtils.firstNonNull(requestConsentParam, requestGdprConsentParam);
Expand Down Expand Up @@ -397,7 +397,7 @@ private Targeting parseTargeting(ObjectNode targetingNode) {
}
}

private Site overrideSite(Site site, HttpRequestWrapper httpRequest) {
private Site overrideSite(Site site, HttpRequestContext httpRequest) {
final String canonicalUrl = canonicalUrl(httpRequest);
final String accountId = httpRequest.getQueryParams().get(ACCOUNT_REQUEST_PARAM);

Expand Down Expand Up @@ -431,15 +431,15 @@ private Site overrideSite(Site site, HttpRequestWrapper httpRequest) {
return null;
}

private static String canonicalUrl(HttpRequestWrapper httpRequest) {
private static String canonicalUrl(HttpRequestContext httpRequest) {
try {
return HttpUtil.decodeUrl(httpRequest.getQueryParams().get(CURL_REQUEST_PARAM));
} catch (IllegalArgumentException e) {
return null;
}
}

private Imp overrideImp(Imp imp, HttpRequestWrapper httpRequest, ObjectNode targetingNode) {
private Imp overrideImp(Imp imp, HttpRequestContext httpRequest, ObjectNode targetingNode) {
final String tagId = httpRequest.getQueryParams().get(SLOT_REQUEST_PARAM);
final Banner banner = imp.getBanner();
final List<Format> overwrittenFormats = banner != null
Expand All @@ -458,7 +458,7 @@ private Imp overrideImp(Imp imp, HttpRequestWrapper httpRequest, ObjectNode targ
/**
* Creates formats from request parameters to override origin amp banner formats.
*/
private static List<Format> createOverrideBannerFormats(HttpRequestWrapper httpRequest, List<Format> formats) {
private static List<Format> createOverrideBannerFormats(HttpRequestContext httpRequest, List<Format> formats) {
final int overrideWidth = parseIntParamOrZero(httpRequest, OW_REQUEST_PARAM);
final int width = parseIntParamOrZero(httpRequest, W_REQUEST_PARAM);
final int overrideHeight = parseIntParamOrZero(httpRequest, OH_REQUEST_PARAM);
Expand All @@ -473,7 +473,7 @@ private static List<Format> createOverrideBannerFormats(HttpRequestWrapper httpR
: updateFormatsFromParams(formats, width, height);
}

private static Integer parseIntParamOrZero(HttpRequestWrapper httpRequest, String name) {
private static Integer parseIntParamOrZero(HttpRequestContext httpRequest, String name) {
return parseIntOrZero(httpRequest.getQueryParams().get(name));
}

Expand Down Expand Up @@ -538,7 +538,7 @@ private static Banner overrideBanner(Banner banner, List<Format> formats) {
: banner;
}

private static Long overrideTimeout(Long tmax, HttpRequestWrapper httpRequest) {
private static Long overrideTimeout(Long tmax, HttpRequestContext httpRequest) {
final String timeoutQueryParam = httpRequest.getQueryParams().get(TIMEOUT_REQUEST_PARAM);
if (timeoutQueryParam == null) {
return null;
Expand Down Expand Up @@ -623,11 +623,11 @@ private static List<Format> parseMultiSizeParam(String ms) {
return formats;
}

private static Map<String, String> updateAmpData(ExtRequestPrebid prebid, HttpRequestWrapper httpRequest) {
private static Map<String, String> updateAmpData(ExtRequestPrebid prebid, HttpRequestContext httpRequest) {
final ExtRequestPrebidAmp amp = prebid != null ? prebid.getAmp() : null;
final Map<String, String> existingAmpData = amp != null ? amp.getData() : null;

final MultiMap queryParams = httpRequest.getQueryParams();
final CaseInsensitiveMultiMap queryParams = httpRequest.getQueryParams();
if (queryParams.isEmpty()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.metric.MetricName;
import org.prebid.server.model.Endpoint;
import org.prebid.server.model.HttpRequestWrapper;
import org.prebid.server.model.HttpRequestContext;
import org.prebid.server.settings.model.Account;

import java.io.IOException;
Expand Down Expand Up @@ -117,7 +117,7 @@ private String extractAndValidateBody(RoutingContext context) {
return body;
}

private Future<BidRequest> parseBidRequest(HttpRequestWrapper httpRequest, List<String> errors) {
private Future<BidRequest> parseBidRequest(HttpRequestContext httpRequest, List<String> errors) {
try {
final JsonNode bidRequestNode = bodyAsJsonNode(httpRequest.getBody());

Expand Down Expand Up @@ -153,7 +153,7 @@ private BidRequest jsonNodeAsBidRequest(JsonNode bidRequestNode) {
private Future<BidRequest> updateBidRequest(AuctionContext auctionContext) {
final Account account = auctionContext.getAccount();
final BidRequest bidRequest = auctionContext.getBidRequest();
final HttpRequestWrapper httpRequest = auctionContext.getHttpRequest();
final HttpRequestContext httpRequest = auctionContext.getHttpRequest();

return storedRequestProcessor.processStoredRequests(account.getId(), bidRequest)
.map(resolvedBidRequest -> paramsResolver.resolve(resolvedBidRequest, httpRequest, timeoutResolver))
Expand Down
Loading