Skip to content

Commit 6c6fd14

Browse files
committed
Разовая инициализация свойств веб-запроса и ответа, плюс рефакторинг возвращаемых типов
1 parent c8b5347 commit 6c6fd14

File tree

3 files changed

+52
-35
lines changed

3 files changed

+52
-35
lines changed

src/OneScript.Web.Server/HttpRequestWrapper.cs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,34 @@ This Source Code Form is subject to the terms of the
55
at http://mozilla.org/MPL/2.0/.
66
----------------------------------------------------------*/
77
using Microsoft.AspNetCore.Http;
8-
using OneScript.Commons;
98
using OneScript.Contexts;
10-
using OneScript.StandardLibrary;
119
using OneScript.StandardLibrary.Binary;
12-
using OneScript.Web.Server;
13-
using OneScript.StandardLibrary.Http;
14-
using OneScript.StandardLibrary.Processes;
15-
using OneScript.StandardLibrary.Text;
1610
using OneScript.Values;
1711
using ScriptEngine.Machine;
1812
using ScriptEngine.Machine.Contexts;
19-
using System;
20-
using System.Text;
21-
using System.Threading.Tasks;
2213
using OneScript.StandardLibrary.Collections;
23-
using OneScript.Types;
2414

2515
namespace OneScript.Web.Server
2616
{
2717
[ContextClass("HTTPСервисЗапрос", "HTTPServiceRequest")]
2818
public class HttpRequestWrapper : AutoContext<HttpRequestWrapper>
2919
{
3020
private readonly HttpRequest _request;
21+
private readonly PropertyWrappersCollection _wrappers = new ();
3122

3223
public HttpRequestWrapper(HttpRequest request)
3324
{
3425
_request = request;
3526
}
3627

3728
[ContextProperty("Параметры", "Parameters", CanWrite = false)]
38-
public FixedMapImpl Query => _request.Query.ToFixedMap();
29+
public FixedMapImpl Query => _wrappers.Get(nameof(Query), () => _request.Query.ToFixedMap());
3930

4031
[ContextProperty("ЕстьФормыВТипеКонтента", "HasFormContentType", CanWrite = false)]
41-
public IValue HasFormContentType => BslBooleanValue.Create(_request.HasFormContentType);
32+
public bool HasFormContentType => _request.HasFormContentType;
4233

4334
[ContextProperty("Тело", "Body", CanWrite = false)]
44-
public GenericStream Body => new(_request.Body);
35+
public GenericStream Body => _wrappers.Get(nameof(Body), () => new GenericStream(_request.Body));
4536

4637
[ContextProperty("ТипКонтента", "ContentType", CanWrite = false)]
4738
public IValue ContentType
@@ -68,13 +59,13 @@ public IValue ContentLength
6859
}
6960

7061
[ContextProperty("Куки", "Cookie", CanWrite = false)]
71-
public RequestCookieCollectionWrapper Cookies => new(_request.Cookies);
62+
public RequestCookieCollectionWrapper Cookies => _wrappers.Get(nameof(Cookies), () => new RequestCookieCollectionWrapper(_request.Cookies));
7263

7364
[ContextProperty("Заголовки", "Headers", CanWrite = false)]
74-
public HeaderDictionaryWrapper Headers => new(_request.Headers);
65+
public HeaderDictionaryWrapper Headers => _wrappers.Get(nameof(Headers), () => new HeaderDictionaryWrapper(_request.Headers));
7566

7667
[ContextProperty("Протокол", "Protocol", CanWrite = false)]
77-
public IValue Protocol => BslStringValue.Create(_request.Protocol);
68+
public string Protocol => _request.Protocol;
7869

7970
[ContextProperty("СтрокаПараметров", "ParametersString", CanWrite = false)]
8071
public IValue QueryString
@@ -125,21 +116,21 @@ public IValue Host
125116
}
126117

127118
[ContextProperty("ЭтоHttps", "IsHttps", CanWrite = false)]
128-
public IValue IsHttps => BslBooleanValue.Create(_request.IsHttps);
119+
public bool IsHttps => _request.IsHttps;
129120

130121
[ContextProperty("Схема", "Scheme", CanWrite = false)]
131-
public IValue Scheme => BslStringValue.Create(_request.Scheme);
122+
public string Scheme => _request.Scheme;
132123

133124
[ContextProperty("Метод", "Method", CanWrite = false)]
134-
public IValue Method => BslStringValue.Create(_request.Method);
125+
public string Method => _request.Method;
135126

136127
[ContextProperty("Форма", "Form", CanWrite = false)]
137128
public IValue Form
138129
{
139130
get
140131
{
141132
if (_request.HasFormContentType)
142-
return new FormCollectionWrapper(_request.Form);
133+
return _wrappers.Get(nameof(Form), () => new FormCollectionWrapper(_request.Form));
143134
else
144135
return BslUndefinedValue.Instance;
145136
}

src/OneScript.Web.Server/HttpResponseWrapper.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ This Source Code Form is subject to the terms of the
44
was not distributed with this file, You can obtain one
55
at http://mozilla.org/MPL/2.0/.
66
----------------------------------------------------------*/
7+
78
using Microsoft.AspNetCore.Http;
8-
using Microsoft.Net.Http.Headers;
9-
using Newtonsoft.Json;
109
using OneScript.Contexts;
1110
using OneScript.StandardLibrary.Binary;
12-
using OneScript.Web.Server;
1311
using OneScript.StandardLibrary.Json;
1412
using OneScript.StandardLibrary.Text;
1513
using OneScript.Values;
@@ -19,11 +17,11 @@ This Source Code Form is subject to the terms of the
1917

2018
namespace OneScript.Web.Server
2119
{
22-
2320
[ContextClass("HTTPСервисОтвет", "HTTPServiceResponse")]
24-
public class HttpResponseWrapper: AutoContext<HttpResponseWrapper>
21+
public class HttpResponseWrapper : AutoContext<HttpResponseWrapper>
2522
{
2623
private readonly HttpResponse _response;
24+
private readonly PropertyWrappersCollection _wrappers = new();
2725

2826
public HttpResponseWrapper(HttpResponse response)
2927
{
@@ -60,23 +58,21 @@ public IValue ContentLength
6058
}
6159

6260
[ContextProperty("Тело", "Body", CanWrite = false)]
63-
public GenericStream Body => new(_response.Body);
61+
public GenericStream Body => _wrappers.Get(nameof(Body), () => new GenericStream(_response.Body));
6462

6563
[ContextProperty("Заголовки", "Headers", CanWrite = false)]
66-
public HeaderDictionaryWrapper Headers => new HeaderDictionaryWrapper(_response.Headers);
64+
public HeaderDictionaryWrapper Headers =>
65+
_wrappers.Get(nameof(Headers), () => new HeaderDictionaryWrapper(_response.Headers));
6766

6867
[ContextProperty("КодСостояния", "StatusCode")]
69-
public IValue StatusCode
68+
public int StatusCode
7069
{
71-
get => BslNumericValue.Create(_response.StatusCode);
72-
set
73-
{
74-
_response.StatusCode = (int)value.AsNumber();
75-
}
70+
get => _response.StatusCode;
71+
set => _response.StatusCode = value;
7672
}
7773

7874
[ContextProperty("Куки", "Cookie", CanWrite = false)]
79-
public ResponseCookiesWrapper Cookies => new ResponseCookiesWrapper(_response.Cookies);
75+
public ResponseCookiesWrapper Cookies => _wrappers.Get(nameof(Cookies), () => new ResponseCookiesWrapper(_response.Cookies));
8076

8177
[ContextMethod("Записать", "Write")]
8278
public void Write(string strData, IValue encoding = null)
@@ -105,4 +101,4 @@ public void WriteJson(IValue obj, IValue encoding = null)
105101
_response.WriteAsync(data, enc).Wait();
106102
}
107103
}
108-
}
104+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
8+
using System;
9+
using System.Collections.Generic;
10+
11+
namespace OneScript.Web.Server;
12+
13+
/// <summary>
14+
/// Класс, обеспечивающий создание оберток свойств запроса один только раз.
15+
/// </summary>
16+
public class PropertyWrappersCollection
17+
{
18+
private readonly Dictionary<string, object> _objects = new();
19+
20+
public T Get<T>(string propName, Func<T> factory)
21+
{
22+
if (_objects.TryGetValue(propName, out var value))
23+
return (T)value;
24+
25+
value = factory();
26+
_objects.Add(propName, value);
27+
28+
return (T)value;
29+
}
30+
}

0 commit comments

Comments
 (0)