File: JavascriptCallbackMessageInspector.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (86 lines) | stat: -rw-r--r-- 4,264 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------
#pragma warning disable 1634, 1691
namespace System.ServiceModel.Dispatcher
{
    using System.Net;
    using System.Linq;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Web;
    using System.Web;
    using System.ServiceModel.Description;
    using System.Diagnostics;
    using System.ServiceModel.Diagnostics;

    class JavascriptCallbackMessageInspector : IDispatchMessageInspector
    {
        internal static readonly string applicationJavaScriptMediaType = "application/x-javascript";

        public JavascriptCallbackMessageInspector(string callbackParameterName)
        {
            this.CallbackParameterName = callbackParameterName;
            if (DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.JsonpCallbackNameSet, SR2.GetString(SR2.TraceCodeJsonpCallbackNameSet, callbackParameterName));
            }
        }

        string CallbackParameterName { get; set; }

        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            if (HttpContext.Current != null &&
                HttpContext.Current.User != null &&
                HttpContext.Current.User.Identity != null &&
                HttpContext.Current.User.Identity.IsAuthenticated)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR2.CrossDomainJavascriptAuthNotSupported));
            }
            return null;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            WebBodyFormatMessageProperty formatProperty;
            JavascriptCallbackResponseMessageProperty javascriptCallbackResponseMessageProperty = null;
            if (reply.Properties.TryGetValue<WebBodyFormatMessageProperty>(WebBodyFormatMessageProperty.Name, out formatProperty) &&
                formatProperty != null &&
                formatProperty.Format == WebContentFormat.Json)
            {
                if (!reply.Properties.TryGetValue<JavascriptCallbackResponseMessageProperty>(JavascriptCallbackResponseMessageProperty.Name, out javascriptCallbackResponseMessageProperty)
                    || javascriptCallbackResponseMessageProperty == null)
                {
                    javascriptCallbackResponseMessageProperty = WebHttpBehavior.TrySetupJavascriptCallback(this.CallbackParameterName);
                    if (javascriptCallbackResponseMessageProperty != null)
                    {
                        reply.Properties.Add(JavascriptCallbackResponseMessageProperty.Name, javascriptCallbackResponseMessageProperty);
                    }
                }
                if (javascriptCallbackResponseMessageProperty != null)
                {                    
                    HttpResponseMessageProperty property;
                    if (reply.Properties.TryGetValue<HttpResponseMessageProperty>(HttpResponseMessageProperty.Name, out property) &&
                        property != null)
                    {
                        property.Headers[HttpResponseHeader.ContentType] = applicationJavaScriptMediaType;
                        if (javascriptCallbackResponseMessageProperty.StatusCode == null)
                        {
                            javascriptCallbackResponseMessageProperty.StatusCode = property.StatusCode;
                        }
                        property.StatusCode = HttpStatusCode.OK;

                        if (property.SuppressEntityBody)
                        {
                            property.SuppressEntityBody = false;
                            Message nullJsonMessage = WebOperationContext.Current.CreateJsonResponse<object>(null);
                            nullJsonMessage.Properties.CopyProperties(reply.Properties);
                            reply = nullJsonMessage;
                        }
                    }
                }
            }
        }
    }
}