Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populating Cookies using requestData headers #642

Merged
merged 10 commits into from
Oct 1, 2021
Merged

Conversation

satvu
Copy link
Member

@satvu satvu commented Sep 27, 2021

First iteration of populating cookies in the HttpRequestData using the headers.

  • Lazy Initialization
  • Tested with no cookies, single cookie, and multiple (3) cookies
  • Sample apps edited for testing the issue and will be reverted to original state before merging
  • Edited .csproj and launch files to set up a working debugging environment (will revert these changes back before merging)

@satvu satvu requested a review from brettsam September 27, 2021 20:54
@satvu satvu linked an issue Sep 27, 2021 that may be closed by this pull request
samples/FunctionApp/Function3/Function3.cs Outdated Show resolved Hide resolved
@@ -1,8 +0,0 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this file causing a problem? If not, we should revert these changes to avoid increasing the scope of the PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/Azure/azure-functions-dotnet-worker/pull/616/files#diff-5ed6fe086b82963a1fc843c092daf5ee6e9c266a2c270cdfdd864380568d5f4a

I was making changes from this PR to allow local debugging - will revert when I no longer need to debug it.

src/DotNetWorker.Core/Http/HttpCookie.cs Outdated Show resolved Hide resolved
src/DotNetWorker.Grpc/Http/GrpcHttpRequestData.cs Outdated Show resolved Hide resolved
src/DotNetWorker.Grpc/Http/GrpcHttpRequestData.cs Outdated Show resolved Hide resolved
src/DotNetWorker.Grpc/Http/GrpcHttpRequestData.cs Outdated Show resolved Hide resolved
src/DotNetWorker.Grpc/Http/GrpcHttpRequestData.cs Outdated Show resolved Hide resolved
public static IReadOnlyCollection<IHttpCookie> CookieStringsToHttpCookie(KeyValuePair<string, IEnumerable<string>> cookieStrings)
{
// cookieStrings.Value comes in the format "Cookie_1=value;Cookie_2=value;Cookie_3=value"
var separateCookies = cookieStrings.Value.First().ToString().Split(";");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the changes proposed above, you should be able to modify this method to work directly with the string.

src/DotNetWorker.Grpc/Http/GrpcHttpRequestData.cs Outdated Show resolved Hide resolved
});
}

private IReadOnlyCollection<IHttpCookie> ToHttpCookies(IEnumerable<string> cookieString)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: arg name sounds singular while the type is a collection. May be you can change the type to string and pass the first item in line 41 ?

if (cookieString != null && cookieString.Any())
{

return ToHttpCookies(cookieString.First().ToString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the ToString() call really needed ? cookieString.First() is returning a string.


public GrpcHttpRequestData(RpcHttp httpData, FunctionContext functionContext)
: base(functionContext)
{
_httpData = httpData ?? throw new ArgumentNullException(nameof(httpData));
_cookies = new Lazy<IReadOnlyCollection<IHttpCookie>>(() =>
{
if(Headers is null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Add a whitespace after if. (Visual studio has a"fix formatting" quick action item which will do these kind of things for you. Keep cursor here and press Ctrl + . )

@@ -66,7 +102,13 @@ public override Stream Body

public override HttpHeadersCollection Headers => _headers ??= new HttpHeadersCollection(_httpData.NullableHeaders.Select(h => new KeyValuePair<string, string>(h.Key, h.Value.Value)));
Copy link
Member

@kshyju kshyju Sep 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated question: Should we consider bringing the lazy loading behavior to Headers property as well ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});
}

private IReadOnlyCollection<IHttpCookie> ToHttpCookies(string cookieString)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Consider moving the private method down, after all public members.

@satvu satvu merged commit 00921e6 into main Oct 1, 2021
@satvu satvu deleted the sarahvu/process-cookies branch October 1, 2021 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HttpRequestData.Cookies does not contain request cookies
3 participants