This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from stormpath/fix-post-content-type
Version 0.1.2
- Loading branch information
Showing
38 changed files
with
421 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// <copyright file="Caching.cs" company="Stormpath, Inc."> | ||
// Copyright (c) 2016 Stormpath, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using Stormpath.Owin.Abstractions; | ||
|
||
namespace Stormpath.Owin.Middleware.Internal | ||
{ | ||
public static class Caching | ||
{ | ||
public static void AddDoNotCacheHeaders(IOwinEnvironment context) | ||
{ | ||
context.Response.OnSendingHeaders(state => | ||
{ | ||
context.Response.Headers.SetString("Cache-Control", "no-cache, no-store"); | ||
context.Response.Headers.SetString("Pragma", "no-cache"); | ||
}, null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// <copyright file="PocoBinder{T}.cs" company="Stormpath, Inc."> | ||
// Copyright (c) 2016 Stormpath, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
namespace Stormpath.Owin.Middleware.Internal | ||
{ | ||
public class PocoBinder<T> | ||
where T : new() | ||
{ | ||
private readonly Func<string, string> valueFunc; | ||
|
||
public PocoBinder(Func<string, string> valueFunc) | ||
{ | ||
this.valueFunc = valueFunc; | ||
} | ||
|
||
public T Bind() | ||
{ | ||
var result = new T(); | ||
|
||
foreach (var property in typeof(T).GetTypeInfo().DeclaredProperties) | ||
{ | ||
if (property.PropertyType != typeof(string)) | ||
{ | ||
throw new Exception($"Unsupported target property type {property.PropertyType.Name}."); | ||
} | ||
|
||
property.SetValue(result, this.valueFunc(property.Name)); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/Stormpath.Owin.Middleware/Internal/PostBodyParser{T}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// <copyright file="PostBodyParser{T}.cs" company="Stormpath, Inc."> | ||
// Copyright (c) 2016 Stormpath, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Stormpath.Owin.Abstractions; | ||
|
||
namespace Stormpath.Owin.Middleware.Internal | ||
{ | ||
public static class PostBodyParser | ||
{ | ||
public static async Task<T> ToModel<T>(IOwinEnvironment context, ContentType bodyContentType, CancellationToken cancellationToken) | ||
where T : new() | ||
{ | ||
var bodyString = await context.Request.GetBodyAsStringAsync(cancellationToken); | ||
|
||
return ToModel<T>(bodyString, bodyContentType); | ||
} | ||
|
||
public static T ToModel<T>(string body, ContentType bodyContentType) | ||
where T : new() | ||
{ | ||
if (bodyContentType == ContentType.Json) | ||
{ | ||
return Serializer.Deserialize<T>(body); | ||
} | ||
|
||
if (bodyContentType == ContentType.FormUrlEncoded) | ||
{ | ||
var formDictionary = FormContentParser.Parse(body); | ||
return new PocoBinder<T>(key => formDictionary.GetString(key)).Bind(); | ||
} | ||
|
||
// This should never happen. The logic in AbstractRoute should throw first if the Content-Type is unsupported. | ||
throw new Exception($"Unsupported Content-Type {bodyContentType.ToString()}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.