Skip to content

Commit 601e95d

Browse files
committed
Replace Request.Items access with SetItem/SetTrue/IsSet
1 parent 899eee1 commit 601e95d

37 files changed

Lines changed: 137 additions & 100 deletions

ServiceStack/src/ServiceStack.Mvc/RazorFormat.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,13 @@ public async Task<bool> ProcessRequestAsync(IRequest req, object dto, Stream out
292292
if (explicitView == null && (req.Dto == null || dto == null))
293293
return false;
294294

295-
if (req.Items.ContainsKey(RenderException))
295+
if (req.IsSet(RenderException))
296296
return false;
297297

298298
var errorStatus = dto.GetResponseStatus() ??
299299
(dto is Exception ex ? ex.ToResponseStatus() : null);
300300
if (errorStatus?.ErrorCode != null)
301-
req.Items[Keywords.ErrorStatus] = errorStatus;
301+
req.SetItem(Keywords.ErrorStatus, errorStatus);
302302

303303
var viewNames = new List<string>();
304304
if (explicitView != null)

ServiceStack/src/ServiceStack.Server/AutoQueryFeature.AutoCrud.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ internal struct ExecValue(object? id, long? rowsUpdated)
897897

898898
private object? ExecAndReturnResponse<Table>(CrudContext context, Func<CrudContext,ExecValue> fn)
899899
{
900-
var ignoreEvent = context.Request.Items.ContainsKey(Keywords.IgnoreEvent);
900+
var ignoreEvent = context.Request.IsSet(Keywords.IgnoreEvent);
901901
var trans = context.Events != null && !ignoreEvent && !context.Db.InTransaction()
902902
? context.Db.OpenTransaction()
903903
: null;
@@ -958,7 +958,7 @@ internal struct ExecValue(object? id, long? rowsUpdated)
958958

959959
private async Task<object?> ExecAndReturnResponseAsync<Table>(CrudContext context, Func<CrudContext,Task<ExecValue>> fn)
960960
{
961-
var ignoreEvent = context.Request.Items.ContainsKey(Keywords.IgnoreEvent);
961+
var ignoreEvent = context.Request.IsSet(Keywords.IgnoreEvent);
962962
var trans = context.Events != null && !ignoreEvent && !context.Db.InTransaction()
963963
? context.Db.OpenTransaction()
964964
: null;

ServiceStack/src/ServiceStack/Auth/ApiKeyAuthProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public override async Task<object> AuthenticateAsync(IServiceBase authService, I
246246
if (response != null)
247247
return response;
248248

249-
authService.Request.Items[Keywords.ApiKey] = apiKey;
249+
authService.Request.SetItem(Keywords.ApiKey, apiKey);
250250

251251
return new AuthenticateResponse
252252
{
@@ -321,7 +321,7 @@ public virtual async Task PreAuthenticateWithApiKeyAsync(IRequest req, IResponse
321321
var apiSessionKey = GetSessionKey(apiKey.Id);
322322
if (await HasCachedSessionAsync(req, apiSessionKey).ConfigAwait())
323323
{
324-
req.Items[Keywords.ApiKey] = apiKey;
324+
req.SetItem(Keywords.ApiKey, apiKey);
325325
return;
326326
}
327327

@@ -350,7 +350,7 @@ public virtual async Task<bool> HasCachedSessionAsync(IRequest req, string apiSe
350350

351351
if (session != null)
352352
{
353-
req.Items[Keywords.Session] = session;
353+
req.SetItem(Keywords.Session, session);
354354
return true;
355355
}
356356
}

ServiceStack/src/ServiceStack/Auth/AuthProviderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static void SaveSession(this IAuthProvider provider, IServiceBase authSer
6666
}
6767
else
6868
{
69-
authService.Request.Items[Keywords.Session] = session;
69+
authService.Request.SetItem(Keywords.Session, session);
7070
}
7171
}
7272

@@ -79,7 +79,7 @@ public static async Task SaveSessionAsync(this IAuthProvider provider, IServiceB
7979
}
8080
else
8181
{
82-
authService.Request.Items[Keywords.Session] = session;
82+
authService.Request.SetItem(Keywords.Session, session);
8383
}
8484
}
8585

ServiceStack/src/ServiceStack/Auth/AuthenticateService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public async Task<object> PostAsync(Authenticate request)
373373
ReferrerUrl = referrerUrl,
374374
Session = session,
375375
AlreadyAuthenticated = alreadyAuthenticated,
376-
DidAuthenticate = Request.Items.ContainsKey(Keywords.DidAuthenticate),
376+
DidAuthenticate = Request.IsSet(Keywords.DidAuthenticate),
377377
};
378378

379379
foreach (var responseFilter in AuthResponseFilters)

ServiceStack/src/ServiceStack/Auth/JwtAuthProviderReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ protected virtual async Task<bool> AuthenticateBearerTokenAsync(IRequest req, IR
584584
}
585585

586586
var session = await CreateSessionFromPayloadAsync(req, jwtPayload);
587-
req.Items[Keywords.Session] = session;
587+
req.SetItem(Keywords.Session, session);
588588
return true;
589589
}
590590

@@ -604,7 +604,7 @@ protected virtual async Task<bool> AuthenticateBearerTokenAsync(IRequest req, IR
604604
}
605605

606606
var session = await CreateSessionFromPayloadAsync(req, jwtPayload);
607-
req.Items[Keywords.Session] = session;
607+
req.SetItem(Keywords.Session, session);
608608
return true;
609609
}
610610
}

ServiceStack/src/ServiceStack/Auth/NetCoreIdentityAuthProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public virtual async Task PreAuthenticateAsync(IRequest req, IResponse res)
127127
return;
128128

129129
session = await ConvertPrincipalToSessionAsync(req, claimsPrincipal).ConfigAwait();
130-
req.Items[Keywords.Session] = session;
130+
req.SetItem(Keywords.Session, session);
131131
}
132132

133133
public async Task<IAuthSession> ConvertPrincipalToSessionAsync(IRequest req, ClaimsPrincipal claimsPrincipal, CancellationToken token=default)

ServiceStack/src/ServiceStack/AuthenticateAttribute.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ public static bool Authenticate(IRequest req, object requestDto=null, IAuthSessi
111111

112112
req.PopulateFromRequestIfHasSessionId(requestDto);
113113

114-
if (!req.Items.ContainsKey(Keywords.HasPreAuthenticated))
114+
if (!req.IsSet(Keywords.HasPreAuthenticated))
115115
{
116116
var mockResponse = new BasicRequest().Response;
117-
req.Items[Keywords.HasPreAuthenticated] = true;
117+
req.SetTrue(Keywords.HasPreAuthenticated);
118118
foreach (var authWithRequest in authProviders.OfType<IAuthWithRequest>())
119119
{
120120
authWithRequest.PreAuthenticateAsync(req, mockResponse).Wait();
@@ -148,11 +148,11 @@ public static async Task<bool> AuthenticateAsync(IRequest req, object requestDto
148148

149149
req.PopulateFromRequestIfHasSessionId(requestDto);
150150

151-
if (!req.Items.ContainsKey(Keywords.HasPreAuthenticated))
151+
if (!req.IsSet(Keywords.HasPreAuthenticated))
152152
{
153153
//Unauthorized or invalid requests will terminate the response and return false
154154
var mockResponse = new BasicRequest().Response;
155-
req.Items[Keywords.HasPreAuthenticated] = true;
155+
req.SetTrue(Keywords.HasPreAuthenticated);
156156
foreach (var authWithRequest in authProviders.OfType<IAuthWithRequest>())
157157
{
158158
await authWithRequest.PreAuthenticateAsync(req, mockResponse).ConfigAwait();
@@ -210,9 +210,9 @@ internal static async Task PreAuthenticateAsync(IRequest req, IEnumerable<IAuthP
210210
}
211211

212212
//Call before GetSession so Exceptions can bubble
213-
if (!req.Items.ContainsKey(Keywords.HasPreAuthenticated))
213+
if (!req.IsSet(Keywords.HasPreAuthenticated))
214214
{
215-
req.Items[Keywords.HasPreAuthenticated] = true;
215+
req.SetTrue(Keywords.HasPreAuthenticated);
216216
foreach (var authWithRequest in authProviders.OfType<IAuthWithRequest>())
217217
{
218218
await authWithRequest.PreAuthenticateAsync(req, req.Response).ConfigAwait();
@@ -232,7 +232,7 @@ protected virtual Task HandleShortCircuitedErrors(IRequest req, IResponse res, o
232232
HttpStatusCode statusCode, string statusDescription=null)
233233
{
234234
if (HtmlRedirect != null)
235-
req.Items[nameof(AuthFeature.HtmlRedirect)] = HtmlRedirect;
235+
req.SetItem(nameof(AuthFeature.HtmlRedirect), HtmlRedirect);
236236

237237
return HostContext.AppHost.HandleShortCircuitedErrors(req, res, requestDto, statusCode, statusDescription);
238238
}

ServiceStack/src/ServiceStack/BlazorServerUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static GatewayRequest ToGatewayRequest(this HostState hostState)
118118
RawUrl = hostState.AbsoluteUri,
119119
PathInfo = "/",
120120
});
121-
to.Items[Keywords.Session] = hostState.Session.FromAuthUserSession();
121+
to.SetItem(Keywords.Session, hostState.Session.FromAuthUserSession());
122122
foreach (var cookie in hostState.Cookies)
123123
{
124124
to.Cookies.Add(new(cookie.Name, cookie.ToCookie()));

ServiceStack/src/ServiceStack/CacheResponseAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public override async Task ExecuteAsync(IRequest req, IResponse res, object requ
128128
if (await req.HandleValidCache(cacheInfo).ConfigAwait())
129129
return;
130130

131-
req.Items[Keywords.CacheInfo] = cacheInfo;
131+
req.SetItem(Keywords.CacheInfo, cacheInfo);
132132
}
133133
}
134134

0 commit comments

Comments
 (0)