// will check if the country the request came from is one that has a landing page // if not we should not send response pixel /* private Boolean sendResponseByCountry(int intPageId, string countryCode) * { * * Boolean retVal = true; * if (SettingsCache.checkLandingPageByGeo(intPageId)) * { * pixLandingPagesByGEO tmpPage = SettingsCache.GetPageByGEO(intPageId, countryCode); * if (tmpPage == null) { * retVal = false; * } * } * * log.Info("sendResponseByCountry = " + retVal.ToString()); * return retVal; * * * } */ private string buildS2SURL(pixProviders provider, pixRequests req) { //split string to replace vro mprovider // for each one //take paramX from request and replace in pixel URL from provider List <string> paramsToReplace; paramsToReplace = provider.pixel_url_Text2Replace.Split(',').ToList(); string pixelURL = provider.pixel_url; int i = 1; try { foreach (string value in paramsToReplace) { if (value.Contains("#CC#")) //replacing countyrcode and any other system variables we want :-) just make sure to put in ## and implement code { pixelURL = pixelURL.Replace(value, req.countryCode); } else if (value.Contains("#UID#")) { pixelURL = pixelURL.Replace(value, req.reqGuid.ToString()); } else { pixelURL = pixelURL.Replace(value, Convert.ToString(req.GetType().GetProperty("param" + i.ToString()).GetValue(req, null))); i += 1; } } } catch (Exception e) { log.Fatal("Faild to build pixel URL from request ID: " + req.id.ToString(), e); } return(pixelURL); }
//[FromUri] NameValueCollection formData public HttpResponseMessage Get() { // initialize parameters string sParam1, sParam2, sParam3, fullurl, sRedirectURL, ERROR_PAGE_URL, ip_address, countryCode; sParam1 = sParam2 = sParam3 = fullurl = ip_address = countryCode = string.Empty; ERROR_PAGE_URL = SettingsCache.ERROR_PAGE_URL; string platform; Boolean isFatal = false; HttpResponseMessage response; Int32[] intProviderPageId; Int32 intProviderId, intPageId; //get url from request fullurl = Request.RequestUri.AbsoluteUri; pixProviders tmpProvider; pixLandingPages tmpPage; //get providerid & pageid as array of int from url intProviderPageId = Pixel.Web.Models.pixHelpers.getProviderPageID(Request); platform = Pixel.Web.Models.pixHelpers.RunningPlatform(); if (intProviderPageId != null) { intProviderId = intProviderPageId[0]; intPageId = intProviderPageId[1]; tmpProvider = SettingsCache.GetProvider(intProviderId); if (tmpProvider != null) { //look in query string for the query string params names according to what was configured in database sParam1 = Pixel.Web.Models.pixHelpers.GetQueryString(Request, tmpProvider.param1); sParam2 = Pixel.Web.Models.pixHelpers.GetQueryString(Request, tmpProvider.param2); sParam3 = Pixel.Web.Models.pixHelpers.GetQueryString(Request, tmpProvider.param3); } else { log.Fatal("Could not find provider id: " + intProviderId.ToString() + " in: " + fullurl); isFatal = true; } ip_address = Pixel.Web.Models.pixHelpers.GetIPAddress(); countryCode = Pixel.Web.Models.pixHelpers.GetLocationFromIPDB(ip_address); intPageId = SettingsCache.getRealPageID(intPageId, intProviderId); //see if we have a different page ID for that GEO intPageId = SettingsCache.getPageByGEO(intProviderId, intPageId, countryCode); tmpPage = SettingsCache.GetPage(intPageId); // //get page URL if (tmpPage != null) { /*if ((platform.ToUpper()=="MAC") && (tmpPage.pcmac.ToUpper() =="PC")){ * sRedirectURL = SettingsCache.GetPage(1005).url; * } * else{ * sRedirectURL = tmpPage.url; * }*/ sRedirectURL = tmpPage.url; } else { sRedirectURL = ERROR_PAGE_URL; log.Fatal("Could not find page id: " + intPageId.ToString() + " in " + fullurl); isFatal = true; } // log to database only good URL's if (!isFatal) { //get new guid to send landing page Guid sGuid = Guid.NewGuid(); string stringGuid = sGuid.ToString().Replace("-", ""); sRedirectURL = sRedirectURL.Replace("[UID]", stringGuid); //build a request object with all data var myRequest = new pixRequests(); myRequest.full_url = fullurl; myRequest.reqGuid = sGuid; myRequest.param1 = sParam1; myRequest.param2 = sParam2; myRequest.param3 = sParam3; myRequest.providerid = intProviderId; myRequest.pageid = intPageId; myRequest.user_ip = ip_address; myRequest.redirect_to = sRedirectURL; myRequest.platform = platform; myRequest.countryCode = countryCode; try { //insert request to database var db = new PetaPoco.Database("myConnectionString"); db.Insert(myRequest); db.CloseSharedConnection(); } catch (Exception e) { string tmp = " INSERT INTO [dbo].[Requests] ([reqGuid],[full_url],[param1],[user_ip],[redirect_to],[providerid],[pageid],[platform],[countryCode]) VALUES ("; tmp += " cast ('" + myRequest.reqGuid + "' as uniqueidentifier),'" + myRequest.full_url + "','" + myRequest.param1 + "'"; tmp += ",'" + myRequest.user_ip + "','" + myRequest.redirect_to + "'," + myRequest.providerid.ToString() + "," + myRequest.pageid.ToString(); tmp += ",'" + myRequest.platform + "','" + myRequest.countryCode + "')"; log.Fatal("failed with insert to database: " + fullurl + " /n" + tmp + " /n " + e.Message, e); sRedirectURL = ERROR_PAGE_URL; //throw e; } } // if (isFatal == false) } //if (intProviderPageId != null) else { ip_address = Pixel.Web.Models.pixHelpers.GetIPAddress(); log.Fatal("No Providerid or Pageid in Query string: " + fullurl + " , redirecting to error page. IP = " + ip_address); sRedirectURL = ERROR_PAGE_URL; } // redirect to landing page with guid response = Request.CreateResponse(); switch (SettingsCache.REDIRECT_TYPE) { case 1: response.StatusCode = HttpStatusCode.Moved; break; case 2: response.StatusCode = HttpStatusCode.Redirect; break; case 3: response.StatusCode = HttpStatusCode.RedirectKeepVerb; break; case 4: response.StatusCode = HttpStatusCode.RedirectMethod; break; } response.Headers.Location = new Uri(sRedirectURL); log.Info("Redirecting to landing page: " + sRedirectURL); return(response); }
// GET: api/s //this function will be called when installaiton is finished and we are getting a call from th DLM public string Get() { string sUID, fullUrl, pixelURL, retValue, requestCountryCode; string ip_address, res_countryCode; ip_address = res_countryCode = requestCountryCode = string.Empty; Guid tmpGuid; Int32 intProviderId; retValue = "OK"; fullUrl = Request.RequestUri.AbsoluteUri; //Get uid from request sUID = Pixel.Web.Models.pixHelpers.GetQueryString(Request, "uid"); log.Info("Got request from DLM: " + fullUrl); // if sUID cannot be converted to GUID - no point in moving forward if (Guid.TryParse(sUID, out tmpGuid)) { //get requet with equivilant sUID var db = new PetaPoco.Database("myConnectionString"); tmpRequest = db.SingleOrDefault <pixRequests>("SELECT * FROM requests WHERE reqGuid=@0", tmpGuid.ToString()); if (tmpRequest != null) { //get providerID from request intProviderId = tmpRequest.providerid; intRequestPageId = tmpRequest.pageid; requestCountryCode = tmpRequest.countryCode; //Get full provider record from cache according to provifderID tmpProvider = SettingsCache.GetProvider(intProviderId); if (tmpProvider != null) { log.Info("Found provider: " + tmpProvider.id.ToString() + " ,building response URL"); //getting server to server url and changing return param value if (intProviderId == 1008) { string countryCode = tmpRequest.countryCode.ToUpper(); tmpProvider.pixel_url = getMondoS2SURL(countryCode); } pixelURL = buildS2SURL(tmpProvider, tmpRequest); //set up parameters for responses handel var myResponse = new pixResponses(); myResponse.sentToProvider = false; // check if we need to send response to provider // if (blnSendPixel() && sendResponseByCountry(intRequestPageId, requestCountryCode)) if (blnSendPixel()) { //R_FACTOR: get R_Factor for provider/ page-provider etc. //if R_Factroe > 1 we need to send several pixels //1. need to add curent response with sent to provider = 0 //2. extract R responses that need to be sent // for each one run loop of // - build request from GUID // - build URL // - send pixel // - update record that provide sent try { //send actual response only in production environment if (Pixel.Web.Models.SettingsCache.SEND_RESPONSE) { //send response to provider WebClient proxy = new WebClient(); log.Info("Sending pixel response to Provider: " + pixelURL); var response = proxy.DownloadString(pixelURL); proxy.Dispose(); myResponse.sentToProvider = true; } } catch (Exception e) { log.Fatal("Error in sending response to provider: " + pixelURL, e); retValue = "ERROR 2"; } } // save all good response to database myResponse.full_url = fullUrl; myResponse.response_url = pixelURL; myResponse.resGuid = tmpRequest.reqGuid; myResponse.providerid = tmpRequest.providerid; myResponse.pageid = tmpRequest.pageid; myResponse.countryCode = tmpRequest.countryCode; try { db.Insert(myResponse); db.CloseSharedConnection(); } catch (Exception e) { log.Fatal("Could not insert Response to database with url: " + fullUrl, e); retValue = "ERROR 1"; } //**************************************************************************************************************************** } else { log.Fatal("Counld not find provider id in: " + fullUrl + ", notification will not be sent"); retValue = "ERROR 3"; } } else //a is null { log.Fatal("could not find a request in the database with the following GUID: " + sUID); retValue = "ERROR 4"; } } else { log.Fatal("The uid we got from DLM is empty or cannot be converted to guid : " + fullUrl); insertErrorResponse(fullUrl); retValue = "ERROR 5"; } return(retValue); }