corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) 2006-2014, openmetaverse.org
3 * All rights reserved.
4 *
5 * - Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * - Neither the name of the openmetaverse.org nor the names
11 * of its contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26  
27 using System;
28 using System.Net;
29 using System.Security.Cryptography.X509Certificates;
30 using System.Threading;
31 using OpenMetaverse.StructuredData;
32  
33 namespace OpenMetaverse.Http
34 {
35 public class CapsClient
36 {
37 public delegate void DownloadProgressCallback(CapsClient client, int bytesReceived, int totalBytesToReceive);
38 public delegate void CompleteCallback(CapsClient client, OSD result, Exception error);
39  
40 public event DownloadProgressCallback OnDownloadProgress;
41 public event CompleteCallback OnComplete;
42  
43 public object UserData;
44  
45 protected Uri _Address;
46 protected byte[] _PostData;
47 protected X509Certificate2 _ClientCert;
48 protected string _ContentType;
49 protected HttpWebRequest _Request;
50 protected OSD _Response;
51 protected AutoResetEvent _ResponseEvent = new AutoResetEvent(false);
52  
53 public CapsClient(Uri capability)
54 : this(capability, null)
55 {
56 }
57  
58 public CapsClient(Uri capability, X509Certificate2 clientCert)
59 {
60 _Address = capability;
61 _ClientCert = clientCert;
62 }
63  
64 public void BeginGetResponse(int millisecondsTimeout)
65 {
66 BeginGetResponse(null, null, millisecondsTimeout);
67 }
68  
69 public void BeginGetResponse(OSD data, OSDFormat format, int millisecondsTimeout)
70 {
71 byte[] postData;
72 string contentType;
73  
74 switch (format)
75 {
76 case OSDFormat.Xml:
77 postData = OSDParser.SerializeLLSDXmlBytes(data);
78 contentType = "application/llsd+xml";
79 break;
80 case OSDFormat.Binary:
81 postData = OSDParser.SerializeLLSDBinary(data);
82 contentType = "application/llsd+binary";
83 break;
84 case OSDFormat.Json:
85 default:
86 postData = System.Text.Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(data));
87 contentType = "application/llsd+json";
88 break;
89 }
90  
91 BeginGetResponse(postData, contentType, millisecondsTimeout);
92 }
93  
94 public void BeginGetResponse(byte[] postData, string contentType, int millisecondsTimeout)
95 {
96 _PostData = postData;
97 _ContentType = contentType;
98  
99 if (_Request != null)
100 {
101 _Request.Abort();
102 _Request = null;
103 }
104  
105 if (postData == null)
106 {
107 // GET
108 //Logger.Log.Debug("[CapsClient] GET " + _Address);
109 _Request = CapsBase.DownloadStringAsync(_Address, _ClientCert, millisecondsTimeout, DownloadProgressHandler,
110 RequestCompletedHandler);
111 }
112 else
113 {
114 // POST
115 //Logger.Log.Debug("[CapsClient] POST (" + postData.Length + " bytes) " + _Address);
116 _Request = CapsBase.UploadDataAsync(_Address, _ClientCert, contentType, postData, millisecondsTimeout, null,
117 DownloadProgressHandler, RequestCompletedHandler);
118 }
119 }
120  
121 public OSD GetResponse(int millisecondsTimeout)
122 {
123 BeginGetResponse(millisecondsTimeout);
124 _ResponseEvent.WaitOne(millisecondsTimeout, false);
125 return _Response;
126 }
127  
128 public OSD GetResponse(OSD data, OSDFormat format, int millisecondsTimeout)
129 {
130 BeginGetResponse(data, format, millisecondsTimeout);
131 _ResponseEvent.WaitOne(millisecondsTimeout, false);
132 return _Response;
133 }
134  
135 public OSD GetResponse(byte[] postData, string contentType, int millisecondsTimeout)
136 {
137 BeginGetResponse(postData, contentType, millisecondsTimeout);
138 _ResponseEvent.WaitOne(millisecondsTimeout, false);
139 return _Response;
140 }
141  
142 public void Cancel()
143 {
144 if (_Request != null)
145 _Request.Abort();
146 }
147  
148 void DownloadProgressHandler(HttpWebRequest request, HttpWebResponse response, int bytesReceived, int totalBytesToReceive)
149 {
150 _Request = request;
151  
152 if (OnDownloadProgress != null)
153 {
154 try { OnDownloadProgress(this, bytesReceived, totalBytesToReceive); }
155 catch (Exception ex) { Logger.Log(ex.Message, Helpers.LogLevel.Error, ex); }
156 }
157 }
158  
159 void RequestCompletedHandler(HttpWebRequest request, HttpWebResponse response, byte[] responseData, Exception error)
160 {
161 _Request = request;
162  
163 OSD result = null;
164  
165 if (responseData != null)
166 {
167 try { result = OSDParser.Deserialize(responseData); }
168 catch (Exception ex) { error = ex; }
169 }
170  
171 FireCompleteCallback(result, error);
172 }
173  
174 private void FireCompleteCallback(OSD result, Exception error)
175 {
176 CompleteCallback callback = OnComplete;
177 if (callback != null)
178 {
179 try { callback(this, result, error); }
180 catch (Exception ex) { Logger.Log(ex.Message, Helpers.LogLevel.Error, ex); }
181 }
182  
183 _Response = result;
184 _ResponseEvent.Set();
185 }
186 }
187 }