private void DecorateHttpEvent(HttpMethod method, RequestContext requestContext, HttpResponse response, HttpEvent httpEvent) { httpEvent.HttpResponseStatus = (int)response.StatusCode; httpEvent.UserAgent = response.UserAgent; httpEvent.HttpMethod = method.Method; IDictionary <string, string> headersAsDictionary = response.HeadersAsDictionary; if (headersAsDictionary.ContainsKey("x-ms-request-id") && headersAsDictionary["x-ms-request-id"] != null) { httpEvent.RequestIdHeader = headersAsDictionary["x-ms-request-id"]; } if (headersAsDictionary.ContainsKey("x-ms-clitelem") && headersAsDictionary["x-ms-clitelem"] != null) { XmsCliTelemInfo xmsCliTeleminfo = new XmsCliTelemInfoParser().ParseXMsTelemHeader( headersAsDictionary["x-ms-clitelem"], requestContext.Logger); if (xmsCliTeleminfo != null) { httpEvent.TokenAge = xmsCliTeleminfo.TokenAge; httpEvent.SpeInfo = xmsCliTeleminfo.SpeInfo; httpEvent.ServerErrorCode = xmsCliTeleminfo.ServerErrorCode; httpEvent.ServerSubErrorCode = xmsCliTeleminfo.ServerSubErrorCode; } } }
public void XmsClientTelemInfoParseTest_XmsCliTelemInfoCorrectFormat() { // Act - Parse correctly formatted header var responseHeaders = new Dictionary <string, string> { { "x-ms-clitelem", "1,0,0,," } }; var xmsCliTeleminfo = new XmsCliTelemInfoParser().ParseXMsTelemHeader(responseHeaders["x-ms-clitelem"], _coreLogger); // Assert Assert.AreEqual(xmsCliTeleminfo.Version, "1"); Assert.AreEqual(xmsCliTeleminfo.ServerErrorCode, "0"); Assert.AreEqual(xmsCliTeleminfo.ServerSubErrorCode, "0"); Assert.AreEqual(xmsCliTeleminfo.TokenAge, ""); Assert.AreEqual(xmsCliTeleminfo.SpeInfo, ""); }
public void XmsClientTelemInfoParseTest_IncorrectHeaderVersion() { // Act - Parse wrong version of header - should be "1" var responseHeaders = new Dictionary <string, string> { { "x-ms-clitelem", "3,0,0,," } }; var xmsCliTeleminfo = new XmsCliTelemInfoParser().ParseXMsTelemHeader(responseHeaders["x-ms-clitelem"], _coreLogger); // Assert _coreLogger.Received().Warning( Arg.Is( string.Format( CultureInfo.InvariantCulture, TelemetryError.XmsUnrecognizedHeaderVersion, "3"))); }
public void XmsClientTelemInfoParseTest_IncorrectFormat() { // Act - Parse malformed header - 6 values var responseHeaders = new Dictionary <string, string> { { "x-ms-clitelem", "1,2,3,4,5,6" } }; var xmsCliTeleminfo = new XmsCliTelemInfoParser().ParseXMsTelemHeader(responseHeaders["x-ms-clitelem"], _coreLogger); // Assert _coreLogger.Received().Warning( Arg.Is( string.Format( CultureInfo.InvariantCulture, TelemetryError.XmsCliTelemMalformed, responseHeaders["x-ms-clitelem"]))); }
internal async Task <T> ExecuteRequestAsync <T>(Uri endPoint, HttpMethod method, RequestContext requestContext) { bool addCorrelationId = requestContext != null && !string.IsNullOrEmpty(requestContext.Logger.CorrelationId.ToString()); if (addCorrelationId) { _headers.Add(OAuth2Header.CorrelationId, requestContext.Logger.CorrelationId.ToString()); _headers.Add(OAuth2Header.RequestCorrelationIdInResponse, "true"); } HttpResponse response = null; var endpointUri = CreateFullEndpointUri(endPoint); var httpEvent = new HttpEvent() { HttpPath = endpointUri, QueryParams = endpointUri.Query }; using (_telemetryManager.CreateTelemetryHelper(requestContext.TelemetryRequestId, requestContext.ClientId, httpEvent)) { if (method == HttpMethod.Post) { response = await _httpManager.SendPostAsync(endpointUri, _headers, _bodyParameters, requestContext) .ConfigureAwait(false); } else { response = await _httpManager.SendGetAsync(endpointUri, _headers, requestContext).ConfigureAwait(false); } httpEvent.HttpResponseStatus = (int)response.StatusCode; httpEvent.UserAgent = response.UserAgent; httpEvent.HttpMethod = method.Method; IDictionary <string, string> headersAsDictionary = response.HeadersAsDictionary; if (headersAsDictionary.ContainsKey("x-ms-request-id") && headersAsDictionary["x-ms-request-id"] != null) { httpEvent.RequestIdHeader = headersAsDictionary["x-ms-request-id"]; } if (headersAsDictionary.ContainsKey("x-ms-clitelem") && headersAsDictionary["x-ms-clitelem"] != null) { XmsCliTelemInfo xmsCliTeleminfo = new XmsCliTelemInfoParser().ParseXMsTelemHeader(headersAsDictionary["x-ms-clitelem"], requestContext); if (xmsCliTeleminfo != null) { httpEvent.TokenAge = xmsCliTeleminfo.TokenAge; httpEvent.SpeInfo = xmsCliTeleminfo.SpeInfo; httpEvent.ServerErrorCode = xmsCliTeleminfo.ServerErrorCode; httpEvent.ServerSubErrorCode = xmsCliTeleminfo.ServerSubErrorCode; } } if (response.StatusCode != HttpStatusCode.OK) { try { httpEvent.OauthErrorCode = JsonHelper.DeserializeFromJson <MsalTokenResponse>(response.Body).Error; } catch (SerializationException) // in the rare case we get an error response we cannot deserialize { throw MsalExceptionFactory.GetServiceException( CoreErrorCodes.NonParsableOAuthError, CoreErrorMessages.NonParsableOAuthError, response); } } } return(CreateResponse <T>(response, requestContext, addCorrelationId)); }
internal async Task <T> ExecuteRequestAsync <T>(Uri endPoint, HttpMethod method, RequestContext requestContext) { bool addCorrelationId = requestContext != null && !string.IsNullOrEmpty(requestContext.Logger.CorrelationId.ToString()); if (addCorrelationId) { _headers.Add(OAuth2Header.CorrelationId, requestContext.Logger.CorrelationId.ToString()); _headers.Add(OAuth2Header.RequestCorrelationIdInResponse, "true"); } if (!string.IsNullOrWhiteSpace(requestContext.Logger.ClientName)) { _headers.Add(OAuth2Header.AppName, requestContext.Logger.ClientName); } if (!string.IsNullOrWhiteSpace(requestContext.Logger.ClientVersion)) { _headers.Add(OAuth2Header.AppVer, requestContext.Logger.ClientVersion); } HttpResponse response = null; Uri endpointUri = CreateFullEndpointUri(endPoint); var httpEvent = new HttpEvent(requestContext.TelemetryCorrelationId) { HttpPath = endpointUri, QueryParams = endpointUri.Query }; using (_telemetryManager.CreateTelemetryHelper(httpEvent)) { if (method == HttpMethod.Post) { response = await _httpManager.SendPostAsync(endpointUri, _headers, _bodyParameters, requestContext.Logger) .ConfigureAwait(false); } else { response = await _httpManager.SendGetAsync(endpointUri, _headers, requestContext.Logger).ConfigureAwait(false); } httpEvent.HttpResponseStatus = (int)response.StatusCode; httpEvent.UserAgent = response.UserAgent; httpEvent.HttpMethod = method.Method; IDictionary <string, string> headersAsDictionary = response.HeadersAsDictionary; if (headersAsDictionary.ContainsKey("x-ms-request-id") && headersAsDictionary["x-ms-request-id"] != null) { httpEvent.RequestIdHeader = headersAsDictionary["x-ms-request-id"]; } if (headersAsDictionary.ContainsKey("x-ms-clitelem") && headersAsDictionary["x-ms-clitelem"] != null) { XmsCliTelemInfo xmsCliTeleminfo = new XmsCliTelemInfoParser().ParseXMsTelemHeader( headersAsDictionary["x-ms-clitelem"], requestContext.Logger); if (xmsCliTeleminfo != null) { httpEvent.TokenAge = xmsCliTeleminfo.TokenAge; httpEvent.SpeInfo = xmsCliTeleminfo.SpeInfo; httpEvent.ServerErrorCode = xmsCliTeleminfo.ServerErrorCode; httpEvent.ServerSubErrorCode = xmsCliTeleminfo.ServerSubErrorCode; } } if (response.StatusCode != HttpStatusCode.OK) { try { httpEvent.OauthErrorCode = MsalError.UnknownError; // In cases where the end-point is not found (404) response.body will be empty. // CreateResponse handles throwing errors - in the case of HttpStatusCode <> and ErrorResponse will be created. if (!string.IsNullOrWhiteSpace(response.Body)) { var msalTokenResponse = JsonHelper.DeserializeFromJson <MsalTokenResponse>(response.Body); if (msalTokenResponse != null) { httpEvent.OauthErrorCode = msalTokenResponse.Error; } } } catch (SerializationException) // in the rare case we get an error response we cannot deserialize { // CreateErrorResponse does the same validation. Will be logging the error there. } } } return(CreateResponse <T>(response, requestContext, addCorrelationId)); }