-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathXmlRpcClientProtocol.cs
executable file
·1063 lines (977 loc) · 30.5 KB
/
XmlRpcClientProtocol.cs
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
XML-RPC.NET library
Copyright (c) 2001-2006, Charles Cook <[email protected]>
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.
*/
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace CookComputing.XmlRpc
{
public class XmlRpcClientProtocol : Component, IXmlRpcProxy
{
#region Instance Variables
private bool _allowAutoRedirect = true;
#if (!COMPACT_FRAMEWORK)
private string _connectionGroupName = null;
#endif
#if (!COMPACT_FRAMEWORK && !FX1_0)
private bool _expect100Continue = false;
private bool _enableCompression = false;
#endif
private ICredentials _credentials = null;
private WebHeaderCollection _headers = new WebHeaderCollection();
private int _indentation = 2;
private bool _keepAlive = true;
private XmlRpcNonStandard _nonStandard = XmlRpcNonStandard.None;
private bool _preAuthenticate = false;
private Version _protocolVersion = HttpVersion.Version11;
private IWebProxy _proxy = null;
#if (!COMPACT_FRAMEWORK)
private CookieCollection _responseCookies;
private WebHeaderCollection _responseHeaders;
#endif
private int _timeout = 100000;
private string _url = null;
private string _userAgent = "XML-RPC.NET";
private bool _useEmptyParamsTag = true;
private bool _useIndentation = true;
private bool _useIntTag = false;
private bool _useStringTag = true;
private Encoding _xmlEncoding = null;
private string _xmlRpcMethod = null;
#if (!COMPACT_FRAMEWORK)
private X509CertificateCollection _clientCertificates
= new X509CertificateCollection();
private CookieContainer _cookies = new CookieContainer();
#endif
#endregion
private Guid _id = Util.NewGuid();
#if (!COMPACT_FRAMEWORK)
public XmlRpcClientProtocol(System.ComponentModel.IContainer container)
{
container.Add(this);
InitializeComponent();
}
#endif
public XmlRpcClientProtocol()
{
InitializeComponent();
}
public object Invoke(
MethodBase mb,
params object[] Parameters)
{
return Invoke(this, mb as MethodInfo, Parameters);
}
public object Invoke(
MethodInfo mi,
params object[] Parameters)
{
return Invoke(this, mi, Parameters);
}
public object Invoke(
string MethodName,
params object[] Parameters)
{
return Invoke(this, MethodName, Parameters);
}
public object Invoke(
Object clientObj,
string methodName,
params object[] parameters)
{
MethodInfo mi = GetMethodInfoFromName(clientObj, methodName, parameters);
return Invoke(this, mi, parameters);
}
public object Invoke(
Object clientObj,
MethodInfo mi,
params object[] parameters)
{
#if (!COMPACT_FRAMEWORK)
_responseHeaders = null;
_responseCookies = null;
#endif
WebRequest webReq = null;
object reto = null;
try
{
string useUrl = GetEffectiveUrl(clientObj);
webReq = GetWebRequest(new Uri(useUrl));
XmlRpcRequest req = MakeXmlRpcRequest(webReq, mi, parameters,
clientObj, _xmlRpcMethod, _id);
SetProperties(webReq);
SetRequestHeaders(_headers, webReq);
#if (!COMPACT_FRAMEWORK)
SetClientCertificates(_clientCertificates, webReq);
#endif
Stream serStream = null;
Stream reqStream = null;
bool logging = (RequestEvent != null);
if (!logging)
serStream = reqStream = webReq.GetRequestStream();
else
serStream = new MemoryStream(2000);
try
{
XmlRpcSerializer serializer = new XmlRpcSerializer();
if (_xmlEncoding != null)
serializer.XmlEncoding = _xmlEncoding;
serializer.UseIndentation = _useIndentation;
serializer.Indentation = _indentation;
serializer.NonStandard = _nonStandard;
serializer.UseStringTag = _useStringTag;
serializer.UseIntTag = _useIntTag;
serializer.UseEmptyParamsTag = _useEmptyParamsTag;
serializer.SerializeRequest(serStream, req);
if (logging)
{
reqStream = webReq.GetRequestStream();
serStream.Position = 0;
Util.CopyStream(serStream, reqStream);
reqStream.Flush();
serStream.Position = 0;
OnRequest(new XmlRpcRequestEventArgs(req.proxyId, req.number,
serStream));
}
}
finally
{
if (reqStream != null)
reqStream.Close();
}
HttpWebResponse webResp = GetWebResponse(webReq) as HttpWebResponse;
#if (!COMPACT_FRAMEWORK)
_responseCookies = webResp.Cookies;
_responseHeaders = webResp.Headers;
#endif
Stream respStm = null;
Stream deserStream;
logging = (ResponseEvent != null);
try
{
respStm = webResp.GetResponseStream();
if (!logging)
{
deserStream = respStm;
}
else
{
deserStream = new MemoryStream(2000);
Util.CopyStream(respStm, deserStream);
deserStream.Flush();
deserStream.Position = 0;
}
#if (!COMPACT_FRAMEWORK && !FX1_0)
deserStream = MaybeDecompressStream((HttpWebResponse)webResp,
deserStream);
#endif
try
{
XmlRpcResponse resp = ReadResponse(req, webResp, deserStream, null);
reto = resp.retVal;
}
finally
{
if (logging)
{
deserStream.Position = 0;
OnResponse(new XmlRpcResponseEventArgs(req.proxyId, req.number,
deserStream));
}
}
}
finally
{
if (respStm != null)
respStm.Close();
}
}
finally
{
if (webReq != null)
webReq = null;
}
return reto;
}
#region Properties
public bool AllowAutoRedirect
{
get { return _allowAutoRedirect; }
set { _allowAutoRedirect = value; }
}
#if (!COMPACT_FRAMEWORK)
[Browsable(false)]
public X509CertificateCollection ClientCertificates
{
get { return _clientCertificates; }
}
#endif
#if (!COMPACT_FRAMEWORK)
public string ConnectionGroupName
{
get { return _connectionGroupName; }
set { _connectionGroupName = value; }
}
#endif
[Browsable(false)]
public ICredentials Credentials
{
get { return _credentials; }
set { _credentials = value; }
}
#if (!COMPACT_FRAMEWORK && !FX1_0)
public bool EnableCompression
{
get { return _enableCompression; }
set { _enableCompression = value; }
}
#endif
[Browsable(false)]
public WebHeaderCollection Headers
{
get { return _headers; }
}
#if (!COMPACT_FRAMEWORK && !FX1_0)
public bool Expect100Continue
{
get { return _expect100Continue; }
set { _expect100Continue = value; }
}
#endif
#if (!COMPACT_FRAMEWORK)
public CookieContainer CookieContainer
{
get { return _cookies; }
}
#endif
public Guid Id
{
get { return _id; }
}
public int Indentation
{
get { return _indentation; }
set { _indentation = value; }
}
public bool KeepAlive
{
get { return _keepAlive; }
set { _keepAlive = value; }
}
public XmlRpcNonStandard NonStandard
{
get { return _nonStandard; }
set { _nonStandard = value; }
}
public bool PreAuthenticate
{
get { return _preAuthenticate; }
set { _preAuthenticate = value; }
}
[Browsable(false)]
public System.Version ProtocolVersion
{
get { return _protocolVersion; }
set { _protocolVersion = value; }
}
[Browsable(false)]
public IWebProxy Proxy
{
get { return _proxy; }
set { _proxy = value; }
}
#if (!COMPACT_FRAMEWORK)
public CookieCollection ResponseCookies
{
get { return _responseCookies; }
}
#endif
#if (!COMPACT_FRAMEWORK)
public WebHeaderCollection ResponseHeaders
{
get { return _responseHeaders; }
}
#endif
public int Timeout
{
get { return _timeout; }
set { _timeout = value; }
}
public string Url
{
get { return _url; }
set { _url = value; }
}
public bool UseEmptyParamsTag
{
get { return _useEmptyParamsTag; }
set { _useEmptyParamsTag = value; }
}
public bool UseIndentation
{
get { return _useIndentation; }
set { _useIndentation = value; }
}
public bool UseIntTag
{
get { return _useIntTag; }
set { _useIntTag = value; }
}
public string UserAgent
{
get { return _userAgent; }
set { _userAgent = value; }
}
public bool UseStringTag
{
get { return _useStringTag; }
set { _useStringTag = value; }
}
[Browsable(false)]
public Encoding XmlEncoding
{
get { return _xmlEncoding; }
set { _xmlEncoding = value; }
}
public string XmlRpcMethod
{
get { return _xmlRpcMethod; }
set { _xmlRpcMethod = value; }
}
#endregion
public void SetProperties(WebRequest webReq)
{
if (_proxy != null)
webReq.Proxy = _proxy;
HttpWebRequest httpReq = (HttpWebRequest)webReq;
httpReq.UserAgent = _userAgent;
httpReq.ProtocolVersion = _protocolVersion;
httpReq.KeepAlive = _keepAlive;
#if (!COMPACT_FRAMEWORK)
httpReq.CookieContainer = _cookies;
#endif
#if (!COMPACT_FRAMEWORK && !FX1_0)
httpReq.ServicePoint.Expect100Continue = _expect100Continue;
#endif
httpReq.AllowAutoRedirect = _allowAutoRedirect;
webReq.Timeout = Timeout;
#if (!COMPACT_FRAMEWORK)
webReq.ConnectionGroupName = this._connectionGroupName;
#endif
webReq.Credentials = Credentials;
webReq.PreAuthenticate = PreAuthenticate;
// Compact Framework sets this to false by default
(webReq as HttpWebRequest).AllowWriteStreamBuffering = true;
#if (!COMPACT_FRAMEWORK && !FX1_0)
if (_enableCompression)
webReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
#endif
}
private void SetRequestHeaders(
WebHeaderCollection headers,
WebRequest webReq)
{
foreach (string key in headers)
{
webReq.Headers.Add(key, headers[key]);
}
}
#if (!COMPACT_FRAMEWORK)
private void SetClientCertificates(
X509CertificateCollection certificates,
WebRequest webReq)
{
foreach (X509Certificate certificate in certificates)
{
HttpWebRequest httpReq = (HttpWebRequest)webReq;
httpReq.ClientCertificates.Add(certificate);
}
}
#endif
XmlRpcRequest MakeXmlRpcRequest(WebRequest webReq, MethodInfo mi,
object[] parameters, object clientObj, string xmlRpcMethod,
Guid proxyId)
{
webReq.Method = "POST";
webReq.ContentType = "text/xml";
string rpcMethodName = GetRpcMethodName(clientObj, mi);
XmlRpcRequest req = new XmlRpcRequest(rpcMethodName, parameters, mi,
xmlRpcMethod, proxyId);
return req;
}
XmlRpcResponse ReadResponse(
XmlRpcRequest req,
WebResponse webResp,
Stream respStm,
Type returnType)
{
HttpWebResponse httpResp = (HttpWebResponse)webResp;
if (httpResp.StatusCode != HttpStatusCode.OK)
{
// status 400 is used for errors caused by the client
// status 500 is used for server errors (not server application
// errors which are returned as fault responses)
if (httpResp.StatusCode == HttpStatusCode.BadRequest)
throw new XmlRpcException(httpResp.StatusDescription);
else
throw new XmlRpcServerException(httpResp.StatusDescription);
}
XmlRpcSerializer serializer = new XmlRpcSerializer();
serializer.NonStandard = _nonStandard;
Type retType = returnType;
if (retType == null)
retType = req.mi.ReturnType;
XmlRpcResponse xmlRpcResp
= serializer.DeserializeResponse(respStm, retType);
return xmlRpcResp;
}
MethodInfo GetMethodInfoFromName(object clientObj, string methodName,
object[] parameters)
{
Type[] paramTypes = new Type[0];
if (parameters != null)
{
paramTypes = new Type[parameters.Length];
for (int i = 0; i < paramTypes.Length; i++)
{
if (parameters[i] == null)
throw new XmlRpcNullParameterException("Null parameters are invalid");
paramTypes[i] = parameters[i].GetType();
}
}
Type type = clientObj.GetType();
MethodInfo mi = type.GetMethod(methodName, paramTypes);
if (mi == null)
{
try
{
mi = type.GetMethod(methodName);
}
catch (System.Reflection.AmbiguousMatchException)
{
throw new XmlRpcInvalidParametersException("Method parameters match "
+ "the signature of more than one method");
}
if (mi == null)
throw new Exception(
"Invoke on non-existent or non-public proxy method");
else
throw new XmlRpcInvalidParametersException("Method parameters do "
+ "not match signature of any method called " + methodName);
}
return mi;
}
string GetRpcMethodName(object clientObj, MethodInfo mi)
{
string rpcMethod;
string MethodName = mi.Name;
Attribute attr = Attribute.GetCustomAttribute(mi,
typeof(XmlRpcBeginAttribute));
if (attr != null)
{
rpcMethod = ((XmlRpcBeginAttribute)attr).Method;
if (rpcMethod == "")
{
if (!MethodName.StartsWith("Begin") || MethodName.Length <= 5)
throw new Exception(String.Format(
"method {0} has invalid signature for begin method",
MethodName));
rpcMethod = MethodName.Substring(5);
}
return rpcMethod;
}
// if no XmlRpcBegin attribute, must have XmlRpcMethod attribute
attr = Attribute.GetCustomAttribute(mi, typeof(XmlRpcMethodAttribute));
if (attr == null)
{
throw new Exception("missing method attribute");
}
XmlRpcMethodAttribute xrmAttr = attr as XmlRpcMethodAttribute;
rpcMethod = xrmAttr.Method;
if (rpcMethod == "")
{
rpcMethod = mi.Name;
}
return rpcMethod;
}
public IAsyncResult BeginInvoke(
MethodBase mb,
object[] parameters,
AsyncCallback callback,
object outerAsyncState)
{
return BeginInvoke(mb as MethodInfo, parameters, this, callback,
outerAsyncState);
}
public IAsyncResult BeginInvoke(
MethodInfo mi,
object[] parameters,
AsyncCallback callback,
object outerAsyncState)
{
return BeginInvoke(mi, parameters, this, callback,
outerAsyncState);
}
public IAsyncResult BeginInvoke(
string methodName,
object[] parameters,
object clientObj,
AsyncCallback callback,
object outerAsyncState)
{
MethodInfo mi = GetMethodInfoFromName(clientObj, methodName, parameters);
return BeginInvoke(mi, parameters, this, callback,
outerAsyncState);
}
public IAsyncResult BeginInvoke(
MethodInfo mi,
object[] parameters,
object clientObj,
AsyncCallback callback,
object outerAsyncState)
{
string useUrl = GetEffectiveUrl(clientObj);
WebRequest webReq = GetWebRequest(new Uri(useUrl));
XmlRpcRequest xmlRpcReq = MakeXmlRpcRequest(webReq, mi,
parameters, clientObj, _xmlRpcMethod, _id);
SetProperties(webReq);
SetRequestHeaders(_headers, webReq);
#if (!COMPACT_FRAMEWORK)
SetClientCertificates(_clientCertificates, webReq);
#endif
Encoding useEncoding = null;
if (_xmlEncoding != null)
useEncoding = _xmlEncoding;
XmlRpcAsyncResult asr = new XmlRpcAsyncResult(this, xmlRpcReq,
useEncoding, _useEmptyParamsTag, _useIndentation, _indentation,
_useIntTag, _useStringTag, webReq, callback, outerAsyncState, 0);
webReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),
asr);
if (!asr.IsCompleted)
asr.CompletedSynchronously = false;
return asr;
}
static void GetRequestStreamCallback(IAsyncResult asyncResult)
{
XmlRpcAsyncResult clientResult
= (XmlRpcAsyncResult)asyncResult.AsyncState;
clientResult.CompletedSynchronously = asyncResult.CompletedSynchronously;
try
{
Stream serStream = null;
Stream reqStream = null;
bool logging = (clientResult.ClientProtocol.RequestEvent != null);
if (!logging)
{
serStream = reqStream
= clientResult.Request.EndGetRequestStream(asyncResult);
}
else
serStream = new MemoryStream(2000);
try
{
XmlRpcRequest req = clientResult.XmlRpcRequest;
XmlRpcSerializer serializer = new XmlRpcSerializer();
if (clientResult.XmlEncoding != null)
serializer.XmlEncoding = clientResult.XmlEncoding;
serializer.UseEmptyParamsTag = clientResult.UseEmptyParamsTag;
serializer.UseIndentation = clientResult.UseIndentation;
serializer.Indentation = clientResult.Indentation;
serializer.UseIntTag = clientResult.UseIntTag;
serializer.UseStringTag = clientResult.UseStringTag;
serializer.SerializeRequest(serStream, req);
if (logging)
{
reqStream = clientResult.Request.EndGetRequestStream(asyncResult);
serStream.Position = 0;
Util.CopyStream(serStream, reqStream);
reqStream.Flush();
serStream.Position = 0;
clientResult.ClientProtocol.OnRequest(
new XmlRpcRequestEventArgs(req.proxyId, req.number, serStream));
}
}
finally
{
if (reqStream != null)
reqStream.Close();
}
clientResult.Request.BeginGetResponse(
new AsyncCallback(GetResponseCallback), clientResult);
}
catch (Exception ex)
{
ProcessAsyncException(clientResult, ex);
}
}
static void GetResponseCallback(IAsyncResult asyncResult)
{
XmlRpcAsyncResult result = (XmlRpcAsyncResult)asyncResult.AsyncState;
result.CompletedSynchronously = asyncResult.CompletedSynchronously;
try
{
result.Response = result.ClientProtocol.GetWebResponse(result.Request,
asyncResult);
}
catch (Exception ex)
{
ProcessAsyncException(result, ex);
if (result.Response == null)
return;
}
ReadAsyncResponse(result);
}
static void ReadAsyncResponse(XmlRpcAsyncResult result)
{
if (result.Response.ContentLength == 0)
{
result.Complete();
return;
}
try
{
result.ResponseStream = result.Response.GetResponseStream();
ReadAsyncResponseStream(result);
}
catch (Exception ex)
{
ProcessAsyncException(result, ex);
}
}
static void ReadAsyncResponseStream(XmlRpcAsyncResult result)
{
IAsyncResult asyncResult;
do
{
byte[] buff = result.Buffer;
long contLen = result.Response.ContentLength;
if (buff == null)
{
if (contLen == -1)
result.Buffer = new Byte[1024];
else
result.Buffer = new Byte[contLen];
}
else
{
if (contLen != -1 && contLen > result.Buffer.Length)
result.Buffer = new Byte[contLen];
}
buff = result.Buffer;
asyncResult = result.ResponseStream.BeginRead(buff, 0, buff.Length,
new AsyncCallback(ReadResponseCallback), result);
if (!asyncResult.CompletedSynchronously)
return;
}
while (!(ProcessAsyncResponseStreamResult(result, asyncResult)));
}
static bool ProcessAsyncResponseStreamResult(XmlRpcAsyncResult result,
IAsyncResult asyncResult)
{
int endReadLen = result.ResponseStream.EndRead(asyncResult);
long contLen = result.Response.ContentLength;
bool completed;
if (endReadLen == 0)
completed = true;
else if (contLen > 0 && endReadLen == contLen)
{
result.ResponseBufferedStream = new MemoryStream(result.Buffer);
completed = true;
}
else
{
if (result.ResponseBufferedStream == null)
{
result.ResponseBufferedStream = new MemoryStream(result.Buffer.Length);
}
result.ResponseBufferedStream.Write(result.Buffer, 0, endReadLen);
completed = false;
}
if (completed)
result.Complete();
return completed;
}
static void ReadResponseCallback(IAsyncResult asyncResult)
{
XmlRpcAsyncResult result = (XmlRpcAsyncResult)asyncResult.AsyncState;
result.CompletedSynchronously = asyncResult.CompletedSynchronously;
if (asyncResult.CompletedSynchronously)
return;
try
{
bool completed = ProcessAsyncResponseStreamResult(result, asyncResult);
if (!completed)
ReadAsyncResponseStream(result);
}
catch (Exception ex)
{
ProcessAsyncException(result, ex);
}
}
static void ProcessAsyncException(XmlRpcAsyncResult clientResult,
Exception ex)
{
WebException webex = ex as WebException;
if (webex != null && webex.Response != null)
{
clientResult.Response = webex.Response;
return;
}
if (clientResult.IsCompleted)
throw new Exception("error during async processing");
clientResult.Complete(ex);
}
public object EndInvoke(
IAsyncResult asr)
{
return EndInvoke(asr, null);
}
public object EndInvoke(
IAsyncResult asr,
Type returnType)
{
object reto = null;
Stream responseStream = null;
try
{
XmlRpcAsyncResult clientResult = (XmlRpcAsyncResult)asr;
if (clientResult.Exception != null)
throw clientResult.Exception;
if (clientResult.EndSendCalled)
throw new Exception("dup call to EndSend");
clientResult.EndSendCalled = true;
HttpWebResponse webResp = (HttpWebResponse)clientResult.WaitForResponse();
#if (!COMPACT_FRAMEWORK)
clientResult._responseCookies = webResp.Cookies;
clientResult._responseHeaders = webResp.Headers;
#endif
responseStream = clientResult.ResponseBufferedStream;
if (ResponseEvent != null)
{
OnResponse(new XmlRpcResponseEventArgs(
clientResult.XmlRpcRequest.proxyId,
clientResult.XmlRpcRequest.number,
responseStream));
responseStream.Position = 0;
}
#if (!COMPACT_FRAMEWORK && !FX1_0)
responseStream = MaybeDecompressStream((HttpWebResponse)webResp,
responseStream);
#endif
XmlRpcResponse resp = ReadResponse(clientResult.XmlRpcRequest,
webResp, responseStream, returnType);
reto = resp.retVal;
}
finally
{
if (responseStream != null)
responseStream.Close();
}
return reto;
}
string GetEffectiveUrl(object clientObj)
{
Type type = clientObj.GetType();
// client can either have define URI in attribute or have set it
// via proxy's ServiceURI property - but must exist by now
string useUrl = "";
if (Url == "" || Url == null)
{
Attribute urlAttr = Attribute.GetCustomAttribute(type,
typeof(XmlRpcUrlAttribute));
if (urlAttr != null)
{
XmlRpcUrlAttribute xrsAttr = urlAttr as XmlRpcUrlAttribute;
useUrl = xrsAttr.Uri;
}
}
else
{
useUrl = Url;
}
if (useUrl == "")
{
throw new XmlRpcMissingUrl("Proxy XmlRpcUrl attribute or Url "
+ "property not set.");
}
return useUrl;
}
#region Introspection Methods
[XmlRpcMethod("system.listMethods")]
public string[] SystemListMethods()
{
return (string[])Invoke("SystemListMethods", new Object[0]);
}
[XmlRpcMethod("system.listMethods")]
public IAsyncResult BeginSystemListMethods(
AsyncCallback Callback,
object State)
{
return BeginInvoke("SystemListMethods", new object[0], this, Callback,
State);
}
public string[] EndSystemListMethods(IAsyncResult AsyncResult)
{
return (string[])EndInvoke(AsyncResult);
}
[XmlRpcMethod("system.methodSignature")]
public object[] SystemMethodSignature(string MethodName)
{
return (object[])Invoke("SystemMethodSignature",
new Object[] { MethodName });
}
[XmlRpcMethod("system.methodSignature")]
public IAsyncResult BeginSystemMethodSignature(
string MethodName,
AsyncCallback Callback,
object State)
{
return BeginInvoke("SystemMethodSignature",
new Object[] { MethodName }, this, Callback, State);
}
public Array EndSystemMethodSignature(IAsyncResult AsyncResult)
{
return (Array)EndInvoke(AsyncResult);
}
[XmlRpcMethod("system.methodHelp")]
public string SystemMethodHelp(string MethodName)
{
return (string)Invoke("SystemMethodHelp",
new Object[] { MethodName });
}
[XmlRpcMethod("system.methodHelp")]
public IAsyncResult BeginSystemMethodHelp(
string MethodName,
AsyncCallback Callback,
object State)
{
return BeginInvoke("SystemMethodHelp",
new Object[] { MethodName }, this, Callback, State);
}
public string EndSystemMethodHelp(IAsyncResult AsyncResult)
{
return (string)EndInvoke(AsyncResult);
}
#endregion
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
#endregion
protected virtual WebRequest GetWebRequest(Uri uri)
{
WebRequest req = WebRequest.Create(uri);
return req;
}
protected virtual WebResponse GetWebResponse(WebRequest request)
{
WebResponse ret = null;
try
{
ret = request.GetResponse();
}
catch (WebException ex)
{
if (ex.Response == null)
throw;
ret = ex.Response;
}
return ret;
}
#if (!COMPACT_FRAMEWORK && !FX1_0)
// support for gzip and deflate
protected Stream MaybeDecompressStream(HttpWebResponse httpWebResp,
Stream respStream)
{
Stream decodedStream;
string contentEncoding = httpWebResp.ContentEncoding.ToLower();
string coen = httpWebResp.Headers["Content-Encoding"];
if (contentEncoding.Contains("gzip"))
{
decodedStream = new System.IO.Compression.GZipStream(respStream,
System.IO.Compression.CompressionMode.Decompress);
}
else if (contentEncoding.Contains("deflate"))
{
decodedStream = new System.IO.Compression.DeflateStream(respStream,
System.IO.Compression.CompressionMode.Decompress);
}