-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharePointProxyModule.cs
More file actions
209 lines (165 loc) · 7.01 KB
/
SharePointProxyModule.cs
File metadata and controls
209 lines (165 loc) · 7.01 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Copyright (c) Microsoft Corporation
All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.IO;
using System.Web.Configuration;
using System.Web.SessionState;
namespace mammerla.SharePointIntegration
{
public class SharePointProxyModule : IHttpHandler, IReadOnlySessionState
{
public bool IsReusable
{
get
{
return true;
}
}
public void ProcessRequest(HttpContext context)
{
String subPath = context.Request.Url.ToString();
bool isClientSvcRequest = false;
int vtiBinIndex = subPath.IndexOf("_vti_bin");
if (vtiBinIndex >= 0)
{
subPath = subPath.Substring(vtiBinIndex);
}
else
{
int apiIndex = subPath.IndexOf("_api");
subPath = subPath.Substring(apiIndex);
}
String destinationUrl = context.Request.Headers["x-destination-url"];
if (destinationUrl == null)
{
destinationUrl = context.Request.QueryString["MS.SP.url"];
if (destinationUrl != null)
{
int vtiBinIndexInDestUrl = destinationUrl.IndexOf("_vti_bin");
if (vtiBinIndexInDestUrl >= 0)
{
subPath = destinationUrl.Substring(vtiBinIndexInDestUrl);
if (subPath.IndexOf("client.svc", StringComparison.InvariantCultureIgnoreCase) >= 0)
{
isClientSvcRequest = true;
}
}
if (vtiBinIndexInDestUrl >= 0)
{
destinationUrl = destinationUrl.Substring(0, vtiBinIndexInDestUrl - 1);
}
}
}
if (destinationUrl == null)
{
context.Response.AddHeader("x-sharepointproxy-error", "no.x-destination-url.header.set");
context.Response.StatusCode = 500;
return;
}
int indexOfVtiBinInSubPath = subPath.IndexOf("_vti_bin/client/", StringComparison.InvariantCultureIgnoreCase);
if (indexOfVtiBinInSubPath >= 0)
{
subPath = subPath.Substring(0, indexOfVtiBinInSubPath) + "_vti_bin/client.svc";
}
SharePointProxyAuthMode authMode = Configuration.AuthMode;
if (Configuration.ContextUrl != null)
{
if (UrlUtilities.ServerNamesAreEqual(destinationUrl, Configuration.ContextUrl))
{
authMode = Configuration.AuthModeToMy;
}
}
String url = null;
if (destinationUrl.IndexOf("_api") >= 0)
{
url = UrlUtilities.EnsurePathEndsWithSlash(destinationUrl);
}
else
{
url = UrlUtilities.EnsurePathEndsWithSlash(destinationUrl) + subPath;
}
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(url);
if (authMode == SharePointProxyAuthMode.OAuth)
{
object accessToken = null;
try
{
accessToken = SharePointManager.Current.AccessToken;
}
catch (NullReferenceException)
{
;
}
if (accessToken == null)
{
context.Response.AddHeader("x-sharepointproxy-error", "no.oauth.session.token.set.for" + destinationUrl + "|" + Configuration.ContextUrl + "|" + subPath + "|" + UrlUtilities.GetCanonicalServerNameFromFullUrl(destinationUrl) + "|" + UrlUtilities.GetCanonicalServerNameFromFullUrl(Configuration.ContextUrl) + "|" + UrlUtilities.ServerNamesAreEqual(destinationUrl, Configuration.ContextUrl) + "|" + WebConfigurationManager.AppSettings.Get("SharePointProxyAuthModeToMy"));
context.Response.StatusCode = 500;
return;
}
context.Response.AddHeader("x-token-added", (String)accessToken);
hwr.Headers.Add("Authorization", "Bearer " + (String)accessToken);
}
else
{
hwr.UseDefaultCredentials = true;
}
String[] keys = context.Request.Headers.AllKeys;
hwr.Method = context.Request.HttpMethod;
Stream clientRequestStream = context.Request.GetBufferlessInputStream();
if (hwr.Method != "GET")
{
Stream sharePointRequestStream = hwr.GetRequestStream();
int b = clientRequestStream.ReadByte();
while (b >= 0)
{
sharePointRequestStream.WriteByte((byte)b);
b = clientRequestStream.ReadByte();
}
}
foreach (String key in keys)
{
String keyLower = key.ToLower();
if (key == "Accept")
{
hwr.Accept = context.Request.Headers[key];
}
else if (keyLower == "x-requestdigest")
{
hwr.Headers[key] = context.Request.Headers[key];
}
}
hwr.ContentType = context.Request.ContentType;
WebResponse wresp = null;
try
{
wresp = hwr.GetResponse();
}
catch (WebException we)
{
context.Response.AddHeader("x-sharepointproxy-error", "request.failed.for" + destinationUrl + "|" + subPath + "|" + UrlUtilities.GetCanonicalServerNameFromFullUrl(destinationUrl) + "|" + we.Message + "|" + authMode.ToString());
context.Response.StatusCode = 500;
return;
}
Stream sharePointResponseStream = wresp.GetResponseStream();
Stream clientResponseStream = context.Response.OutputStream;
context.Response.ContentType = "application/json";
int br = sharePointResponseStream.ReadByte();
while (br >= 0)
{
clientResponseStream.WriteByte((byte)br);
br = sharePointResponseStream.ReadByte();
}
}
}
}