2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
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.
18 using System.Collections.Generic;
19 using System.ComponentModel;
23 namespace Tizen.WebView
26 /// This class provides methods and properties to handle a interpected request.
28 /// <since_tizen> 8 </since_tizen>
29 [EditorBrowsable(EditorBrowsableState.Never)]
30 public class RequestInterceptor {
31 private const string ResponseHeaderTemplate =
32 "HTTP/1.1 {0} {1}\r\n" +
33 "Content-Type: {2}; charset={3}\r\n" +
34 "Content-Length: {4}\r\n";
36 private IntPtr _handle;
38 internal RequestInterceptor(IntPtr handle)
44 /// The URL of the request.
46 /// <since_tizen> 8 </since_tizen>
47 [EditorBrowsable(EditorBrowsableState.Never)]
52 return new Uri(Interop.ChromiumEwk.ewk_intercept_request_url_get(_handle));
57 /// Sets headers and data for the response.
59 /// <since_tizen> 8 </since_tizen>
60 /// <param name="mimeType"> Response's mime type. </param>
61 /// <param name="encoding"> Response's character encoding. </param>
62 /// <param name="statusCode"> HTTP response status code. </param>
63 /// <param name="reasonPhrase"> HTTP response reason phrase. </param>
64 /// <param name="responseHeaders"> Headers Map from HTTP header field names to field values. </param>
65 /// <param name="data"> The streiam that provides the response's data. </param>
66 /// <exception cref="ArgumentNullException">Thrown when <paramref name="data"/> is null.</exception>
67 /// <exception cref="InvalidOperationException">Thrown when the native operation failed to set response.</exception>
68 [EditorBrowsable(EditorBrowsableState.Never)]
69 public void SetResponse(string mimeType, string encoding, int statusCode, string reasonPhrase, IDictionary<string, string> responseHeaders, Stream data)
73 throw new ArgumentNullException(nameof(data));
77 using (MemoryStream ms = new MemoryStream())
82 var headers = String.Format(ResponseHeaderTemplate, statusCode, reasonPhrase, mimeType, encoding, body.Length);
83 if (responseHeaders != null)
85 foreach(var header in responseHeaders)
87 headers += $"{header.Key}: {header.Value}\r\n";
94 fixed (byte* bodyPtr = body)
96 var ret = Interop.ChromiumEwk.ewk_intercept_request_response_set(_handle, headers, bodyPtr, (uint)body.Length);
99 throw new InvalidOperationException("Failed to set response.");
107 /// Ignores the request, so WebView will load it.
109 /// <since_tizen> 8 </since_tizen>
110 [EditorBrowsable(EditorBrowsableState.Never)]
113 Interop.ChromiumEwk.ewk_intercept_request_ignore(_handle);