This repository was archived by the owner on Nov 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathConsumerRequest.cs
More file actions
254 lines (210 loc) · 8.38 KB
/
Copy pathConsumerRequest.cs
File metadata and controls
254 lines (210 loc) · 8.38 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#region License
// The MIT License
//
// Copyright (c) 2006-2008 DevDefined Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using DevDefined.OAuth.Framework;
using DevDefined.OAuth.Utility;
namespace DevDefined.OAuth.Consumer
{
public class ConsumerRequest : IConsumerRequest
{
private readonly IOAuthConsumerContext _consumerContext;
private readonly IOAuthContext _context;
private readonly IOAuthSession _oauthSession;
public ConsumerRequest(IOAuthSession oauthSession, IOAuthContext context, IOAuthConsumerContext consumerContext)
{
_oauthSession = oauthSession;
_context = context;
_consumerContext = consumerContext;
}
public IOAuthContext Context
{
get { return _context; }
}
public IConsumerResponse ToConsumerResponse()
{
return _oauthSession.RunConsumerRequest(this);
}
public virtual HttpWebRequest ToWebRequest()
{
RequestDescription description = GetRequestDescription();
var request = (HttpWebRequest)WebRequest.Create(description.Url);
request.Timeout = (int)TimeSpan.FromMinutes(3).TotalMilliseconds;
request.Method = description.Method;
request.UserAgent = _consumerContext.UserAgent;
if (!string.IsNullOrEmpty(AcceptsType))
{
request.Accept = AcceptsType;
}
DateTime? ifModifiedSinceDate = ParseIfModifiedSince(Context);
if (ifModifiedSinceDate.HasValue)
request.IfModifiedSince = ifModifiedSinceDate.Value;
if (ProxyServerUri != null)
{
request.Proxy = new WebProxy(ProxyServerUri, false);
}
if (description.ContentType == Parameters.HttpFormEncoded)
{
request.ContentType = description.ContentType;
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(description.Body);
}
}
else if (!string.IsNullOrEmpty(description.Body))
{
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(description.Body);
}
}
else if (description.RequestStream != null)
{
using (var requestStream = request.GetRequestStream())
{
description.RequestStream.CopyTo(requestStream);
}
}
if (description.Headers.Count > 0)
{
foreach (string key in description.Headers.AllKeys)
{
request.Headers[key] = description.Headers[key];
}
}
return request;
}
public RequestDescription GetRequestDescription()
{
if (string.IsNullOrEmpty(_context.Signature))
{
_consumerContext.SignContext(_context);
}
Uri uri = _context.GenerateUri();
var description = new RequestDescription
{
Url = uri,
Method = _context.RequestMethod
};
if ((_context.FormEncodedParameters != null) && (_context.FormEncodedParameters.Count > 0))
{
description.ContentType = Parameters.HttpFormEncoded;
description.Body =
UriUtility.FormatQueryString(_context.FormEncodedParameters.ToQueryParametersExcludingTokenSecret());
}
else if (!string.IsNullOrEmpty(RequestBody))
{
description.Body = UriUtility.UrlEncode(RequestBody);
}
else if (RequestStream != null)
{
description.RequestStream = RequestStream;
}
if (_consumerContext.UseHeaderForOAuthParameters)
{
description.Headers[Parameters.OAuth_Authorization_Header] = _context.GenerateOAuthParametersForHeader();
}
return description;
}
[Obsolete("Prefer ToConsumerResponse instead as this has more error handling built in")]
public HttpWebResponse ToWebResponse()
{
try
{
HttpWebRequest request = ToWebRequest();
return (HttpWebResponse)request.GetResponse();
}
catch (WebException webEx)
{
OAuthException authException;
if (WebExceptionHelper.TryWrapException(Context, webEx, out authException))
{
throw authException;
}
throw;
}
}
public IConsumerRequest SignWithoutToken()
{
EnsureRequestHasNotBeenSignedYet();
_consumerContext.SignContext(_context);
return this;
}
public IConsumerRequest SignWithToken()
{
var accessToken = _oauthSession.GetAccessToken();
return SignWithToken(accessToken);
}
public IConsumerRequest SignWithToken(IToken token, bool checkForExistingSignature = true)
{
if (checkForExistingSignature)
EnsureRequestHasNotBeenSignedYet();
_consumerContext.SignContextWithToken(_context, token);
return this;
}
public Uri ProxyServerUri { get; set; }
public string AcceptsType { get; set; }
public string RequestBody { get; set; }
public Stream RequestStream { get; set; }
public override string ToString()
{
return ToConsumerResponse().Content;
}
private void EnsureRequestHasNotBeenSignedYet()
{
if (!string.IsNullOrEmpty(_context.Signature))
{
throw Error.ThisConsumerRequestHasAlreadyBeenSigned();
}
}
public DateTime? ParseIfModifiedSince(IOAuthContext context)
{
if (context.IfModifiedSince.HasValue)
{
AssertValidIfModifiedSinceDate(context.IfModifiedSince.Value);
return context.IfModifiedSince.Value;
}
string ifModifiedSinceString = context.Headers["If-Modified-Since"];
if (string.IsNullOrEmpty(ifModifiedSinceString))
return null;
DateTime ifModifiedSinceDate;
if (DateTime.TryParse(ifModifiedSinceString, out ifModifiedSinceDate))
{
AssertValidIfModifiedSinceDate(ifModifiedSinceDate);
return ifModifiedSinceDate;
}
return null;
}
public void AssertValidIfModifiedSinceDate(DateTime date)
{
if (date < new DateTime(1753, 01, 01))
{
throw Error.IfModifiedSinceHeaderOutOfRange(date);
}
}
}
}