96c0e6a77f96d46cf7d7821a1abc38e42e65176a
[platform/core/csapi/tizenfx.git] / src / Tizen.WebView / Tizen.WebView / RequestInterceptor.cs
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using System.IO;
21 using System.Text;
22
23 namespace Tizen.WebView
24 {
25     /// <summary>
26     /// This class provides methods and properties to handle a interpected request.
27     /// </summary>
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";
35
36         private IntPtr _handle;
37
38         internal RequestInterceptor(IntPtr handle)
39         {
40             _handle = handle;
41         }
42
43         /// <summary>
44         /// The URL of the request.
45         /// </summary>
46         /// <since_tizen> 8 </since_tizen>
47         [EditorBrowsable(EditorBrowsableState.Never)]
48         public Uri Url
49         {
50             get
51             {
52                 return new Uri(Interop.ChromiumEwk.ewk_intercept_request_url_get(_handle));
53             }
54         }
55
56         /// <summary>
57         /// Sets headers and data for the response.
58         /// </summary>
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)
70         {
71             if (data == null)
72             {
73                 throw new ArgumentNullException(nameof(data));
74             }
75
76             byte[] body;
77             using (MemoryStream ms = new MemoryStream())
78             {
79                 data.CopyTo(ms);
80                 body = ms.ToArray();
81
82                 var headers = String.Format(ResponseHeaderTemplate, statusCode, reasonPhrase, mimeType, encoding, body.Length);
83                 if (responseHeaders != null)
84                 {
85                     foreach(var header in responseHeaders)
86                     {
87                         headers += $"{header.Key}: {header.Value}\r\n";
88                     }
89                 }
90                 headers += "\r\n";
91
92                 unsafe
93                 {
94                     fixed (byte* bodyPtr = body)
95                     {
96                         var ret = Interop.ChromiumEwk.ewk_intercept_request_response_set(_handle, headers, bodyPtr, (uint)body.Length);
97                         if (!ret)
98                         {
99                             throw new InvalidOperationException("Failed to set response.");
100                         }
101                     }
102                 }
103             }
104         }
105
106         /// <summary>
107         /// Ignores the request, so WebView will load it.
108         /// </summary>
109         /// <since_tizen> 8 </since_tizen>
110         [EditorBrowsable(EditorBrowsableState.Never)]
111         public void Ignore()
112         {
113             Interop.ChromiumEwk.ewk_intercept_request_ignore(_handle);
114         }
115     }
116 }