Skip to content

Commit

Permalink
Merge pull request Esri#388 from MikeTschudi/master
Browse files Browse the repository at this point in the history
Proposal for Esri#386 and Esri#387
  • Loading branch information
bsvensson authored Sep 13, 2016
2 parents 94def03 + a276032 commit 6eda169
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions DotNet/proxy.ashx
Original file line number Diff line number Diff line change
Expand Up @@ -263,21 +263,19 @@ public class proxy : IHttpHandler {
context.Application["token_for_" + serverUrl.Url] = token;
context.Application.UnLock();
}
}

//name by which token parameter is passed (if url actually came from the list)
tokenParamName = serverUrl != null ? serverUrl.TokenParamName : null;

if (String.IsNullOrEmpty(tokenParamName))
tokenParamName = "token";
//name by which token parameter is passed (if url actually came from the list)
tokenParamName = serverUrl != null ? serverUrl.TokenParamName : null;

requestUri = addTokenToUri(requestUri, token, tokenParamName);
if (String.IsNullOrEmpty(tokenParamName))
tokenParamName = "token";
}
}

//forwarding original request
System.Net.WebResponse serverResponse = null;
try {
serverResponse = forwardToServer(context, requestUri, postBody, credentials);
serverResponse = forwardToServer(context, addTokenToUri(requestUri, token, tokenParamName), postBody, credentials);
} catch (System.Net.WebException webExc) {

string errorMsg = webExc.Message + " " + uri;
Expand Down Expand Up @@ -336,16 +334,16 @@ public class proxy : IHttpHandler {
fetchAndPassBackToClient(serverResponse, response, true);
}
}
// Use instead of response.End() to avoid the "Exception thrown: 'System.Threading.ThreadAbortException' in mscorlib.dll" error

// Use instead of response.End() to avoid the "Exception thrown: 'System.Threading.ThreadAbortException' in mscorlib.dll" error
// that appears in the output of Visual Studio. response.End() appears to only really be necessary if you need to end the thread immediately
// (i.e. no more code is processed). Since this call is at the end of the main subroutine we can safely call ApplicationInstance.CompleteRequest()
// and avoid unnecessary exceptions.
// Sources:
// http://stackoverflow.com/questions/14590812/what-is-the-difference-between-use-cases-for-using-response-endfalse-vs-appl
// http://weblogs.asp.net/hajan/why-not-to-use-httpresponse-close-and-httpresponse-end
// http://stackoverflow.com/questions/1087777/is-response-end-considered-harmful

context.ApplicationInstance.CompleteRequest();
}

Expand Down Expand Up @@ -405,7 +403,7 @@ public class proxy : IHttpHandler {
log(TraceLevel.Verbose, "Adjusting Content-Type for WMS OGC: " + fromResponse.ContentType );
} else {
toResponse.ContentType = fromResponse.ContentType;
}
}
}

private bool fetchAndPassBackToClient(System.Net.WebResponse serverResponse, HttpResponse clientResponse, bool ignoreAuthenticationErrors) {
Expand All @@ -420,7 +418,7 @@ public class proxy : IHttpHandler {
if (
!ignoreAuthenticationErrors
&& strResponse.Contains("error")
&& (strResponse.Contains("\"code\": 498") || strResponse.Contains("\"code\": 499") || strResponse.Contains("\"code\":498") || strResponse.Contains("\"code\":499"))
&& Regex.Match(strResponse, "\"code\"\\s*:\\s*49[89]").Success
)
return true;

Expand Down Expand Up @@ -790,13 +788,33 @@ public class proxy : IHttpHandler {
String value = "";
if (i > -1) {
value = text.Substring(text.IndexOf(':', i) + 1).Trim();

value = value.Length > 0 && value[0] == '"' ?
value.Substring(1, value.IndexOf('"', 1) - 1):
value = value.Substring(0, Math.Max(0, Math.Min(Math.Min(value.IndexOf(","), value.IndexOf("]")), value.IndexOf("}"))));
// Get the rest of a quoted string
value.Substring(1, Math.Max(0, value.IndexOf('"', 1) - 1)) :
// Get a string up to the closest comma, bracket, or brace
value = value.Substring(0,
Math.Min(
value.Length,
Math.Min(
indexOf_HighFlag(value, ","),
Math.Min(
indexOf_HighFlag(value, "]"),
indexOf_HighFlag(value, "}")
)
)
)
);
}
return value;
}

private int indexOf_HighFlag(string text, string key) {
int i = text.IndexOf(key);
if (i < 0) i = Int32.MaxValue;
return i;
}

private void cleanUpRatemap(ConcurrentDictionary<string, RateMeter> ratemap) {
foreach (string key in ratemap.Keys){
RateMeter rate = ratemap[key];
Expand Down

0 comments on commit 6eda169

Please sign in to comment.