2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using System.Runtime.InteropServices;
26 /// It is a class for http request interceptor of web view.
28 [EditorBrowsable(EditorBrowsableState.Never)]
29 public class WebHttpRequestInterceptor : Disposable
31 private HandleRef interceptorHandle;
33 internal WebHttpRequestInterceptor(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
35 IntPtr ip = Interop.WebHttpRequestInterceptorPtr.Get(SwigCPtr);
36 interceptorHandle = new HandleRef(this, ip);
39 /// This will not be public opened.
40 /// <param name="swigCPtr"></param>
41 [EditorBrowsable(EditorBrowsableState.Never)]
42 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
44 Interop.WebHttpRequestInterceptorPtr.DeleteWebHttpRequestInterceptorPtr(swigCPtr);
48 /// Gets url of intercepted request.
50 [EditorBrowsable(EditorBrowsableState.Never)]
55 return Interop.WebHttpRequestInterceptor.GetUrl(interceptorHandle);
60 /// Gets method of intercepted http request, for example, GET, POST, etc.
62 [EditorBrowsable(EditorBrowsableState.Never)]
67 return Interop.WebHttpRequestInterceptor.GetMethod(interceptorHandle);
72 /// Gets headers of intercepted http request.
73 /// Headers is a map with string key-value pairs,
74 /// for example, "Accept: text/plain", "Accept-Charset: utf-8", etc.
76 [EditorBrowsable(EditorBrowsableState.Never)]
77 public IDictionary<string, string> Headers
81 IntPtr mapPtr = Interop.WebHttpRequestInterceptor.GetHeaders(interceptorHandle);
82 IDictionary<string, string> dictionary = new Dictionary<string, string>();
83 PropertyMap map = new PropertyMap(mapPtr, true);
84 for (uint i = 0; i < map.Count(); i++)
86 using (PropertyKey key = map.GetKeyAt(i))
88 if (key.Type == PropertyKey.KeyType.String)
91 using (PropertyValue mapValue = map.GetValue(i))
93 if (mapValue.Get(out outValue))
95 dictionary.Add(key.StringKey, outValue);
107 /// Ignores the http request.
108 /// When application doesn't have a response for intercepted request,
109 /// this function would be called which notifies engine to proceed with normal resource loading.
110 /// It can be called only INSIDE WebContext.HttpRequestIntercepted.
111 /// After it called, any further call on WebHttpRequestInterceptor results in undefined behavior.
113 [EditorBrowsable(EditorBrowsableState.Never)]
116 bool result = Interop.WebHttpRequestInterceptor.Ignore(interceptorHandle);
117 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
122 /// Sets status code and status text of response for intercepted request.
123 /// This function can be used inside or outside WebContext.HttpRequestIntercepted.
124 /// <param name="statusCode">Status code of response</param>
125 /// <param name="customStatusText">Status text of response</param>
127 [EditorBrowsable(EditorBrowsableState.Never)]
128 public bool SetResponseStatus(int statusCode, string customStatusText)
130 bool result = Interop.WebHttpRequestInterceptor.SetResponseStatus(interceptorHandle, statusCode, customStatusText);
131 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
136 /// Adds HTTP header to response for intercepted request.
137 /// This function can be used inside or outside WebContext.HttpRequestIntercepted.
138 /// <param name="fieldName">Key of response header</param>
139 /// <param name="fieldValue">Value of response header</param>
141 [EditorBrowsable(EditorBrowsableState.Never)]
142 public bool AddResponseHeader(string fieldName, string fieldValue)
144 bool result = Interop.WebHttpRequestInterceptor.AddResponseHeader(interceptorHandle, fieldName, fieldValue);
145 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
150 /// Adds HTTP headers to response for intercepted request.
151 /// This function can be used inside or outside WebContext.HttpRequestIntercepted.
152 /// <param name="headers">Map of response headers</param>
154 [EditorBrowsable(EditorBrowsableState.Never)]
155 public bool AddResponseHeaders(IDictionary<string, string> headers)
159 PropertyMap headerMap = new PropertyMap();
160 foreach (KeyValuePair<string, string> kvp in headers)
162 using (PropertyValue value = new PropertyValue(kvp.Value))
164 headerMap.Add(kvp.Key, value);
167 bool result = Interop.WebHttpRequestInterceptor.AddResponseHeaders(interceptorHandle, PropertyMap.getCPtr(headerMap));
169 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
174 /// Writes whole response body at once.
175 /// To call it, application should have full response body ready for the intercepted request.
176 /// This function can be used inside or outside WebContext.HttpRequestIntercepted.
177 /// After this call, any further call on WebHttpRequestInterceptor results in undefined behavior.
178 /// <param name="body">Contents of response</param>
180 [EditorBrowsable(EditorBrowsableState.Never)]
181 public bool SetResponseBody(string body)
185 bool result = Interop.WebHttpRequestInterceptor.AddResponseBody(interceptorHandle, body, (uint)body.Length);
186 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
191 /// Writes whole response body with headers at once.
192 /// To call it, application should have full response headers and body ready for the intercepted request.
193 /// This function can be used inside or outside WebContext.HttpRequestIntercepted.
194 /// After this call, any further call on WebHttpRequestInterceptor results in undefined behavior.
195 /// <param name="headers">Headers of response</param>
196 /// <param name="body">Contents of response</param>
198 [EditorBrowsable(EditorBrowsableState.Never)]
199 public bool SetResponse(string headers, string body)
203 bool result = Interop.WebHttpRequestInterceptor.AddResponse(interceptorHandle, headers, body, (uint)body.Length);
204 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
209 /// Writes a part of response body.
210 /// This function can be called only OUTSIDE WebContext.HttpRequestIntercepted.
211 /// If it returns false, handling the request is done.
212 /// Any further calls result in undefined behavior.
213 /// User should always check return value, because response to this request might not be needed any more,
214 /// and the function can return false even though user still has data to write.
216 /// After writing full response body in chunks using this function,
217 /// call it again with null as chunk, to signal that response body is finished.
218 /// <param name="chunk">Chunk of response</param>
220 [EditorBrowsable(EditorBrowsableState.Never)]
221 public bool WriteResponseChunk(string chunk)
226 length = chunk.Length;
228 bool result = Interop.WebHttpRequestInterceptor.WriteResponseChunk(interceptorHandle, chunk, (uint)length);
229 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();