Skip to content

Commit 418237c

Browse files
committed
Add support for Content-Length header parameter support using property source BODY.$LENGTH
1 parent f840a76 commit 418237c

9 files changed

Lines changed: 20 additions & 3 deletions

File tree

shared/Http/SpecialHttpParameterName.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ internal static class SpecialHttpParameterName
55
public const string Body = "body";
66
public const string MediaType = "mediaType";
77
public const string FileName = "fileName";
8+
public const string Length = "length";
89
}
910
}

shared/ParameterSources/BodyParameterSource.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ internal sealed class BodyParameterSource : ActionParameterSourceDefinition<Body
66
public const string RawPropertyName = "$RAW";
77
public const string MediaTypePropertyName = "$MEDIATYPE";
88
public const string FileNamePropertyName = "$FILENAME";
9+
public const string LengthPropertyName = "$LENGTH";
910
}
1011
}

src/Dibix.Http.Host/Model/HttpRequestDescriptor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public HttpRequestDescriptor(HttpRequest request)
2828

2929
public string? GetBodyFileName() => _request.GetTypedHeaders().ContentDisposition?.FileName.Value;
3030

31+
public long? GetBodyLength() => _request.ContentLength;
32+
3133
public IEnumerable<string> GetHeaderValues(string name) => _request.Headers[name];
3234

3335
public IEnumerable<string> GetAcceptLanguageValues() => _request.GetTypedHeaders().AcceptLanguage.Select(x => x.Value.Value).Where(x => x != null).Select(x => x!);

src/Dibix.Http.Server.AspNet/HttpRequestMessageDescriptor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public HttpRequestMessageDescriptor(HttpRequestMessage request)
2424

2525
public string GetBodyFileName() => RequestMessage.Content?.Headers.ContentDisposition?.FileName;
2626

27+
public long? GetBodyLength() => RequestMessage.Content?.Headers.ContentLength;
28+
2729
public IEnumerable<string> GetHeaderValues(string name) => RequestMessage.Headers.TryGetValues(name, out IEnumerable<string> values) ? values : [];
2830

2931
public IEnumerable<string> GetAcceptLanguageValues() => RequestMessage.Headers.AcceptLanguage.Select(x => x.Value);

src/Dibix.Http.Server/Model/IHttpRequestDescriptor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public interface IHttpRequestDescriptor
1111
Stream GetBody();
1212
string GetBodyMediaType();
1313
string GetBodyFileName();
14+
long? GetBodyLength();
1415
IEnumerable<string> GetHeaderValues(string name);
1516
IEnumerable<string> GetAcceptLanguageValues();
1617
ClaimsPrincipal GetUser();

src/Dibix.Http.Server/Providers/BodyParameterSourceProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public override void Resolve(IHttpParameterResolutionContext context)
3232
break;
3333
}
3434

35+
case BodyParameterSource.LengthPropertyName:
36+
{
37+
Expression getBodyLengthCall = Expression.Call(context.RequestParameter, nameof(IHttpRequestDescriptor.GetBodyLength), Type.EmptyTypes);
38+
context.ResolveUsingValue(getBodyLengthCall);
39+
break;
40+
}
41+
3542
default:
3643
{
3744
Type instanceType = context.ActionMetadata.SafeGetBodyContract();

src/Dibix.Sdk.CodeGeneration/Lookup/ActionTargetDefinitionResolver.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected static bool IsUserParameter(ActionParameterSourceDefinition source, st
101101
location = ActionParameterLocation.Path;
102102
return true;
103103

104-
case BodyParameterSource _:
104+
case BodyParameterSource _ when propertyName != BodyParameterSource.LengthPropertyName:
105105
location = ActionParameterLocation.Body;
106106
return true;
107107

@@ -169,6 +169,7 @@ private ActionParameter CreateActionParameter(string name, TypeReference type, b
169169
{
170170
BodyParameterSource.MediaTypePropertyName => SpecialHttpParameterName.MediaType,
171171
BodyParameterSource.FileNamePropertyName => SpecialHttpParameterName.FileName,
172+
BodyParameterSource.LengthPropertyName => SpecialHttpParameterName.Length,
172173
_ => apiParameterName
173174
};
174175
break;

src/Dibix.Sdk.CodeGeneration/Output/ApiClientWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ protected static void AddMethod(ActionDefinition action, CodeGenerationContext c
7272
// No request body contract members
7373
case ActionParameterLocation.Body when parameter.ApiParameterName is not (SpecialHttpParameterName.Body
7474
or SpecialHttpParameterName.MediaType
75-
or SpecialHttpParameterName.FileName):
75+
or SpecialHttpParameterName.FileName
76+
or SpecialHttpParameterName.Length):
7677

7778
// Will be handled by SecurityScheme/IHttpAuthorizationProvider
7879
case ActionParameterLocation.Header when parameter.ApiParameterName == "Authorization" || action.SecuritySchemes.Requirements.Any(x => x.Scheme.SchemeName == parameter.ApiParameterName):

src/Dibix.Sdk.CodeGeneration/Registration/ControllerDefinitionProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,8 @@ private void CollectAuthorization(JToken templateNameValue, JObject authorizatio
614614
JToken targetValue = targetProperty.Value;
615615
SourceLocation targetLocation = targetValue.GetSourceInfo();
616616
AuthorizationBehavior authorizationBehavior = CreateActionDefinition(targetValue, targetLocation, explicitParameters, pathParameters, bodyParameters, requestBody: null, actionTargetDefinitionFactory: actionTarget => new AuthorizationBehavior(actionDefinition, actionTarget));
617-
actionDefinition.Authorization.Add(authorizationBehavior);
617+
if (authorizationBehavior != null)
618+
actionDefinition.Authorization.Add(authorizationBehavior);
618619
}
619620

620621
private JObject ApplyAuthorizationTemplate(JToken templateNameValue, JObject authorizationTemplateReference)

0 commit comments

Comments
 (0)