mirror of https://github.com/grpc/grpc.git
commit
2e58fafe55
101 changed files with 2851 additions and 680 deletions
@ -0,0 +1,65 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_INTERNAL_CORE_SURFACE_CALL_TEST_ONLY_H |
||||
#define GRPC_INTERNAL_CORE_SURFACE_CALL_TEST_ONLY_H |
||||
|
||||
#include <grpc/grpc.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/** Return the compression algorithm from \a call.
|
||||
* |
||||
* \warning This function should \b only be used in test code. */ |
||||
grpc_compression_algorithm grpc_call_test_only_get_compression_algorithm( |
||||
grpc_call *call); |
||||
|
||||
/** Return the message flags from \a call.
|
||||
* |
||||
* \warning This function should \b only be used in test code. */ |
||||
gpr_uint32 grpc_call_test_only_get_message_flags(grpc_call *call); |
||||
|
||||
/** Returns a bitset for the encodings (compression algorithms) supported by \a
|
||||
* call's peer. |
||||
* |
||||
* To be indexed by grpc_compression_algorithm enum values. */ |
||||
gpr_uint32 grpc_call_test_only_get_encodings_accepted_by_peer(grpc_call *call); |
||||
|
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* GRPC_INTERNAL_CORE_SURFACE_CALL_TEST_ONLY_H */ |
@ -0,0 +1,93 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Threading; |
||||
|
||||
using Google.Apis.Auth.OAuth2; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Auth |
||||
{ |
||||
/// <summary> |
||||
/// Factory methods to create instances of <see cref="ChannelCredentials"/> and <see cref="CallCredentials"/> classes. |
||||
/// </summary> |
||||
public static class GrpcCredentials |
||||
{ |
||||
/// <summary> |
||||
/// Creates a <see cref="MetadataCredentials"/> instance that will obtain access tokens |
||||
/// from any credential that implements <c>ITokenAccess</c>. (e.g. <c>GoogleCredential</c>). |
||||
/// </summary> |
||||
/// <param name="credential">The credential to use to obtain access tokens.</param> |
||||
/// <returns>The <c>MetadataCredentials</c> instance.</returns> |
||||
public static MetadataCredentials Create(ITokenAccess credential) |
||||
{ |
||||
return new MetadataCredentials(AuthInterceptors.FromCredential(credential)); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Convenience method to create a <see cref="ChannelCredentials"/> instance from |
||||
/// <c>ITokenAccess</c> credential and <c>SslCredentials</c> instance. |
||||
/// </summary> |
||||
/// <param name="credential">The credential to use to obtain access tokens.</param> |
||||
/// <param name="sslCredentials">The <c>SslCredentials</c> instance.</param> |
||||
/// <returns>The channel credentials for access token based auth over a secure channel.</returns> |
||||
public static ChannelCredentials Create(ITokenAccess credential, SslCredentials sslCredentials) |
||||
{ |
||||
return ChannelCredentials.Create(sslCredentials, Create(credential)); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates an instance of <see cref="MetadataCredentials"/> that will use given access token to authenticate |
||||
/// with a gRPC service. |
||||
/// </summary> |
||||
/// <param name="accessToken">OAuth2 access token.</param> |
||||
/// /// <returns>The <c>MetadataCredentials</c> instance.</returns> |
||||
public static MetadataCredentials FromAccessToken(string accessToken) |
||||
{ |
||||
return new MetadataCredentials(AuthInterceptors.FromAccessToken(accessToken)); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Converts a <c>ITokenAccess</c> object into a <see cref="MetadataCredentials"/> object supported |
||||
/// by gRPC. |
||||
/// </summary> |
||||
/// <param name="credential"></param> |
||||
/// <returns></returns> |
||||
public static MetadataCredentials ToGrpcCredentials(this ITokenAccess credential) |
||||
{ |
||||
return GrpcCredentials.Create(credential); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,73 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Diagnostics; |
||||
using System.Runtime.InteropServices; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.Core.Tests |
||||
{ |
||||
public class ChannelCredentialsTest |
||||
{ |
||||
[Test] |
||||
public void InsecureCredentials_IsNonComposable() |
||||
{ |
||||
Assert.IsFalse(ChannelCredentials.Insecure.IsComposable); |
||||
} |
||||
|
||||
[Test] |
||||
public void ChannelCredentials_CreateComposite() |
||||
{ |
||||
var composite = ChannelCredentials.Create(new FakeChannelCredentials(true), new FakeCallCredentials()); |
||||
Assert.IsFalse(composite.IsComposable); |
||||
|
||||
Assert.Throws(typeof(ArgumentNullException), () => ChannelCredentials.Create(null, new FakeCallCredentials())); |
||||
Assert.Throws(typeof(ArgumentNullException), () => ChannelCredentials.Create(new FakeChannelCredentials(true), null)); |
||||
|
||||
// forbid composing non-composable |
||||
Assert.Throws(typeof(ArgumentException), () => ChannelCredentials.Create(new FakeChannelCredentials(false), new FakeCallCredentials())); |
||||
} |
||||
|
||||
[Test] |
||||
public void ChannelCredentials_CreateWrapped() |
||||
{ |
||||
ChannelCredentials.Create(new FakeCallCredentials()); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,142 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Threading.Tasks; |
||||
|
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Core |
||||
{ |
||||
/// <summary> |
||||
/// Client-side call credentials. Provide authorization with per-call granularity. |
||||
/// </summary> |
||||
public abstract class CallCredentials |
||||
{ |
||||
/// <summary> |
||||
/// Composes multiple multiple <c>CallCredentials</c> objects into |
||||
/// a single <c>CallCredentials</c> object. |
||||
/// </summary> |
||||
/// <param name="credentials">credentials to compose</param> |
||||
/// <returns>The new <c>CompositeCallCredentials</c></returns> |
||||
public static CallCredentials Compose(params CallCredentials[] credentials) |
||||
{ |
||||
return new CompositeCallCredentials(credentials); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates native object for the credentials. |
||||
/// </summary> |
||||
/// <returns>The native credentials.</returns> |
||||
internal abstract CredentialsSafeHandle ToNativeCredentials(); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Asynchronous authentication interceptor for <see cref="MetadataCredentials"/>. |
||||
/// </summary> |
||||
/// <param name="authUri">URL of a service to which current remote call needs to authenticate</param> |
||||
/// <param name="metadata">Metadata to populate with entries that will be added to outgoing call's headers.</param> |
||||
/// <returns></returns> |
||||
public delegate Task AsyncAuthInterceptor(string authUri, Metadata metadata); |
||||
|
||||
/// <summary> |
||||
/// Client-side credentials that delegate metadata based auth to an interceptor. |
||||
/// The interceptor is automatically invoked for each remote call that uses <c>MetadataCredentials.</c> |
||||
/// </summary> |
||||
public class MetadataCredentials : CallCredentials |
||||
{ |
||||
readonly AsyncAuthInterceptor interceptor; |
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of <c>MetadataCredentials</c> class. |
||||
/// </summary> |
||||
/// <param name="interceptor">authentication interceptor</param> |
||||
public MetadataCredentials(AsyncAuthInterceptor interceptor) |
||||
{ |
||||
this.interceptor = interceptor; |
||||
} |
||||
|
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
NativeMetadataCredentialsPlugin plugin = new NativeMetadataCredentialsPlugin(interceptor); |
||||
return plugin.Credentials; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Credentials that allow composing multiple credentials objects into one <see cref="CallCredentials"/> object. |
||||
/// </summary> |
||||
internal sealed class CompositeCallCredentials : CallCredentials |
||||
{ |
||||
readonly List<CallCredentials> credentials; |
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of <c>CompositeCallCredentials</c> class. |
||||
/// The resulting credentials object will be composite of all the credentials specified as parameters. |
||||
/// </summary> |
||||
/// <param name="credentials">credentials to compose</param> |
||||
public CompositeCallCredentials(params CallCredentials[] credentials) |
||||
{ |
||||
Preconditions.CheckArgument(credentials.Length >= 2, "Composite credentials object can only be created from 2 or more credentials."); |
||||
this.credentials = new List<CallCredentials>(credentials); |
||||
} |
||||
|
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
return ToNativeRecursive(0); |
||||
} |
||||
|
||||
// Recursive descent makes managing lifetime of intermediate CredentialSafeHandle instances easier. |
||||
// In practice, we won't usually see composites from more than two credentials anyway. |
||||
private CredentialsSafeHandle ToNativeRecursive(int startIndex) |
||||
{ |
||||
if (startIndex == credentials.Count - 1) |
||||
{ |
||||
return credentials[startIndex].ToNativeCredentials(); |
||||
} |
||||
|
||||
using (var cred1 = credentials[startIndex].ToNativeCredentials()) |
||||
using (var cred2 = ToNativeRecursive(startIndex + 1)) |
||||
{ |
||||
var nativeComposite = CredentialsSafeHandle.CreateComposite(cred1, cred2); |
||||
if (nativeComposite.IsInvalid) |
||||
{ |
||||
throw new ArgumentException("Error creating native composite credentials. Likely, this is because you are trying to compose incompatible credentials."); |
||||
} |
||||
return nativeComposite; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,238 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Threading.Tasks; |
||||
|
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Core |
||||
{ |
||||
/// <summary> |
||||
/// Client-side channel credentials. Used for creation of a secure channel. |
||||
/// </summary> |
||||
public abstract class ChannelCredentials |
||||
{ |
||||
static readonly ChannelCredentials InsecureInstance = new InsecureCredentialsImpl(); |
||||
|
||||
/// <summary> |
||||
/// Returns instance of credentials that provides no security and |
||||
/// will result in creating an unsecure channel with no encryption whatsoever. |
||||
/// </summary> |
||||
public static ChannelCredentials Insecure |
||||
{ |
||||
get |
||||
{ |
||||
return InsecureInstance; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates a new instance of <c>ChannelCredentials</c> class by composing |
||||
/// given channel credentials with call credentials. |
||||
/// </summary> |
||||
/// <param name="channelCredentials">Channel credentials.</param> |
||||
/// <param name="callCredentials">Call credentials.</param> |
||||
/// <returns>The new composite <c>ChannelCredentials</c></returns> |
||||
public static ChannelCredentials Create(ChannelCredentials channelCredentials, CallCredentials callCredentials) |
||||
{ |
||||
return new CompositeChannelCredentials(channelCredentials, callCredentials); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates a new instance of <c>ChannelCredentials</c> by wrapping |
||||
/// an instance of <c>CallCredentials</c>. |
||||
/// </summary> |
||||
/// <param name="callCredentials">Call credentials.</param> |
||||
/// <returns>The <c>ChannelCredentials</c> wrapping given call credentials.</returns> |
||||
public static ChannelCredentials Create(CallCredentials callCredentials) |
||||
{ |
||||
return new WrappedCallCredentials(callCredentials); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates native object for the credentials. May return null if insecure channel |
||||
/// should be created. |
||||
/// </summary> |
||||
/// <returns>The native credentials.</returns> |
||||
internal abstract CredentialsSafeHandle ToNativeCredentials(); |
||||
|
||||
/// <summary> |
||||
/// Returns <c>true</c> if this credential type allows being composed by <c>CompositeCredentials</c>. |
||||
/// </summary> |
||||
internal virtual bool IsComposable |
||||
{ |
||||
get { return false; } |
||||
} |
||||
|
||||
private sealed class InsecureCredentialsImpl : ChannelCredentials |
||||
{ |
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Client-side SSL credentials. |
||||
/// </summary> |
||||
public sealed class SslCredentials : ChannelCredentials |
||||
{ |
||||
readonly string rootCertificates; |
||||
readonly KeyCertificatePair keyCertificatePair; |
||||
|
||||
/// <summary> |
||||
/// Creates client-side SSL credentials loaded from |
||||
/// disk file pointed to by the GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable. |
||||
/// If that fails, gets the roots certificates from a well known place on disk. |
||||
/// </summary> |
||||
public SslCredentials() : this(null, null) |
||||
{ |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates client-side SSL credentials from |
||||
/// a string containing PEM encoded root certificates. |
||||
/// </summary> |
||||
public SslCredentials(string rootCertificates) : this(rootCertificates, null) |
||||
{ |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates client-side SSL credentials. |
||||
/// </summary> |
||||
/// <param name="rootCertificates">string containing PEM encoded server root certificates.</param> |
||||
/// <param name="keyCertificatePair">a key certificate pair.</param> |
||||
public SslCredentials(string rootCertificates, KeyCertificatePair keyCertificatePair) |
||||
{ |
||||
this.rootCertificates = rootCertificates; |
||||
this.keyCertificatePair = keyCertificatePair; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// PEM encoding of the server root certificates. |
||||
/// </summary> |
||||
public string RootCertificates |
||||
{ |
||||
get |
||||
{ |
||||
return this.rootCertificates; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Client side key and certificate pair. |
||||
/// If null, client will not use key and certificate pair. |
||||
/// </summary> |
||||
public KeyCertificatePair KeyCertificatePair |
||||
{ |
||||
get |
||||
{ |
||||
return this.keyCertificatePair; |
||||
} |
||||
} |
||||
|
||||
// Composing composite makes no sense. |
||||
internal override bool IsComposable |
||||
{ |
||||
get { return true; } |
||||
} |
||||
|
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
return CredentialsSafeHandle.CreateSslCredentials(rootCertificates, keyCertificatePair); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Credentials that allow composing one <see cref="ChannelCredentials"/> object and |
||||
/// one or more <see cref="CallCredentials"/> objects into a single <see cref="ChannelCredentials"/>. |
||||
/// </summary> |
||||
internal sealed class CompositeChannelCredentials : ChannelCredentials |
||||
{ |
||||
readonly ChannelCredentials channelCredentials; |
||||
readonly CallCredentials callCredentials; |
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of <c>CompositeChannelCredentials</c> class. |
||||
/// The resulting credentials object will be composite of all the credentials specified as parameters. |
||||
/// </summary> |
||||
/// <param name="channelCredentials">channelCredentials to compose</param> |
||||
/// <param name="callCredentials">channelCredentials to compose</param> |
||||
public CompositeChannelCredentials(ChannelCredentials channelCredentials, CallCredentials callCredentials) |
||||
{ |
||||
this.channelCredentials = Preconditions.CheckNotNull(channelCredentials); |
||||
this.callCredentials = Preconditions.CheckNotNull(callCredentials); |
||||
Preconditions.CheckArgument(channelCredentials.IsComposable, "Supplied channel credentials do not allow composition."); |
||||
} |
||||
|
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
using (var cred1 = channelCredentials.ToNativeCredentials()) |
||||
using (var cred2 = callCredentials.ToNativeCredentials()) |
||||
{ |
||||
var nativeComposite = CredentialsSafeHandle.CreateComposite(cred1, cred2); |
||||
if (nativeComposite.IsInvalid) |
||||
{ |
||||
throw new ArgumentException("Error creating native composite credentials. Likely, this is because you are trying to compose incompatible credentials."); |
||||
} |
||||
return nativeComposite; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Credentials wrapping <see cref="CallCredentials"/> as <see cref="ChannelCredentials"/>. |
||||
/// </summary> |
||||
internal sealed class WrappedCallCredentials : ChannelCredentials |
||||
{ |
||||
readonly CallCredentials callCredentials; |
||||
|
||||
/// <summary> |
||||
/// Wraps instance of <c>CallCredentials</c> as <c>ChannelCredentials</c>. |
||||
/// </summary> |
||||
/// <param name="callCredentials">credentials to wrap</param> |
||||
public WrappedCallCredentials(CallCredentials callCredentials) |
||||
{ |
||||
this.callCredentials = Preconditions.CheckNotNull(callCredentials); |
||||
} |
||||
|
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
return callCredentials.ToNativeCredentials(); |
||||
} |
||||
} |
||||
} |
@ -1,138 +0,0 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using Grpc.Core.Internal; |
||||
|
||||
namespace Grpc.Core |
||||
{ |
||||
/// <summary> |
||||
/// Client-side credentials. Used for creation of a secure channel. |
||||
/// </summary> |
||||
public abstract class Credentials |
||||
{ |
||||
static readonly Credentials InsecureInstance = new InsecureCredentialsImpl(); |
||||
|
||||
/// <summary> |
||||
/// Returns instance of credential that provides no security and |
||||
/// will result in creating an unsecure channel with no encryption whatsoever. |
||||
/// </summary> |
||||
public static Credentials Insecure |
||||
{ |
||||
get |
||||
{ |
||||
return InsecureInstance; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates native object for the credentials. May return null if insecure channel |
||||
/// should be created. |
||||
/// </summary> |
||||
/// <returns>The native credentials.</returns> |
||||
internal abstract CredentialsSafeHandle ToNativeCredentials(); |
||||
|
||||
private sealed class InsecureCredentialsImpl : Credentials |
||||
{ |
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Client-side SSL credentials. |
||||
/// </summary> |
||||
public sealed class SslCredentials : Credentials |
||||
{ |
||||
readonly string rootCertificates; |
||||
readonly KeyCertificatePair keyCertificatePair; |
||||
|
||||
/// <summary> |
||||
/// Creates client-side SSL credentials loaded from |
||||
/// disk file pointed to by the GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable. |
||||
/// If that fails, gets the roots certificates from a well known place on disk. |
||||
/// </summary> |
||||
public SslCredentials() : this(null, null) |
||||
{ |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates client-side SSL credentials from |
||||
/// a string containing PEM encoded root certificates. |
||||
/// </summary> |
||||
public SslCredentials(string rootCertificates) : this(rootCertificates, null) |
||||
{ |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates client-side SSL credentials. |
||||
/// </summary> |
||||
/// <param name="rootCertificates">string containing PEM encoded server root certificates.</param> |
||||
/// <param name="keyCertificatePair">a key certificate pair.</param> |
||||
public SslCredentials(string rootCertificates, KeyCertificatePair keyCertificatePair) |
||||
{ |
||||
this.rootCertificates = rootCertificates; |
||||
this.keyCertificatePair = keyCertificatePair; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// PEM encoding of the server root certificates. |
||||
/// </summary> |
||||
public string RootCertificates |
||||
{ |
||||
get |
||||
{ |
||||
return this.rootCertificates; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Client side key and certificate pair. |
||||
/// If null, client will not use key and certificate pair. |
||||
/// </summary> |
||||
public KeyCertificatePair KeyCertificatePair |
||||
{ |
||||
get |
||||
{ |
||||
return this.keyCertificatePair; |
||||
} |
||||
} |
||||
|
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
return CredentialsSafeHandle.CreateSslCredentials(rootCertificates, keyCertificatePair); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,113 @@ |
||||
#region Copyright notice and license |
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
#endregion |
||||
using System; |
||||
using System.Runtime.InteropServices; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
|
||||
using Grpc.Core.Logging; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Core.Internal |
||||
{ |
||||
internal delegate void NativeMetadataInterceptor(IntPtr statePtr, IntPtr serviceUrlPtr, IntPtr callbackPtr, IntPtr userDataPtr, bool isDestroy); |
||||
|
||||
internal class NativeMetadataCredentialsPlugin |
||||
{ |
||||
const string GetMetadataExceptionMsg = "Exception occured in metadata credentials plugin."; |
||||
static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<NativeMetadataCredentialsPlugin>(); |
||||
|
||||
[DllImport("grpc_csharp_ext.dll")] |
||||
static extern CredentialsSafeHandle grpcsharp_metadata_credentials_create_from_plugin(NativeMetadataInterceptor interceptor); |
||||
|
||||
[DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)] |
||||
static extern void grpcsharp_metadata_credentials_notify_from_plugin(IntPtr callbackPtr, IntPtr userData, MetadataArraySafeHandle metadataArray, StatusCode statusCode, string errorDetails); |
||||
|
||||
AsyncAuthInterceptor interceptor; |
||||
GCHandle gcHandle; |
||||
NativeMetadataInterceptor nativeInterceptor; |
||||
CredentialsSafeHandle credentials; |
||||
|
||||
public NativeMetadataCredentialsPlugin(AsyncAuthInterceptor interceptor) |
||||
{ |
||||
this.interceptor = Preconditions.CheckNotNull(interceptor, "interceptor"); |
||||
this.nativeInterceptor = NativeMetadataInterceptorHandler; |
||||
|
||||
// Make sure the callback doesn't get garbage collected until it is destroyed. |
||||
this.gcHandle = GCHandle.Alloc(this.nativeInterceptor, GCHandleType.Normal); |
||||
this.credentials = grpcsharp_metadata_credentials_create_from_plugin(nativeInterceptor); |
||||
} |
||||
|
||||
public CredentialsSafeHandle Credentials |
||||
{ |
||||
get { return credentials; } |
||||
} |
||||
|
||||
private void NativeMetadataInterceptorHandler(IntPtr statePtr, IntPtr serviceUrlPtr, IntPtr callbackPtr, IntPtr userDataPtr, bool isDestroy) |
||||
{ |
||||
if (isDestroy) |
||||
{ |
||||
gcHandle.Free(); |
||||
return; |
||||
} |
||||
|
||||
try |
||||
{ |
||||
string serviceUrl = Marshal.PtrToStringAnsi(serviceUrlPtr); |
||||
StartGetMetadata(serviceUrl, callbackPtr, userDataPtr); |
||||
} |
||||
catch (Exception e) |
||||
{ |
||||
grpcsharp_metadata_credentials_notify_from_plugin(callbackPtr, userDataPtr, MetadataArraySafeHandle.Create(Metadata.Empty), StatusCode.Unknown, GetMetadataExceptionMsg); |
||||
Logger.Error(e, GetMetadataExceptionMsg); |
||||
} |
||||
} |
||||
|
||||
private async void StartGetMetadata(string serviceUrl, IntPtr callbackPtr, IntPtr userDataPtr) |
||||
{ |
||||
try |
||||
{ |
||||
var metadata = new Metadata(); |
||||
await interceptor(serviceUrl, metadata); |
||||
|
||||
using (var metadataArray = MetadataArraySafeHandle.Create(metadata)) |
||||
{ |
||||
grpcsharp_metadata_credentials_notify_from_plugin(callbackPtr, userDataPtr, metadataArray, StatusCode.OK, null); |
||||
} |
||||
} |
||||
catch (Exception e) |
||||
{ |
||||
grpcsharp_metadata_credentials_notify_from_plugin(callbackPtr, userDataPtr, MetadataArraySafeHandle.Create(Metadata.Empty), StatusCode.Unknown, GetMetadataExceptionMsg); |
||||
Logger.Error(e, GetMetadataExceptionMsg); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,97 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Utils; |
||||
using Grpc.Testing; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.IntegrationTesting |
||||
{ |
||||
public class MetadataCredentialsTest |
||||
{ |
||||
const string Host = "localhost"; |
||||
Server server; |
||||
Channel channel; |
||||
TestService.ITestServiceClient client; |
||||
|
||||
[TestFixtureSetUp] |
||||
public void Init() |
||||
{ |
||||
var serverCredentials = new SslServerCredentials(new[] { new KeyCertificatePair(File.ReadAllText(TestCredentials.ServerCertChainPath), File.ReadAllText(TestCredentials.ServerPrivateKeyPath)) }); |
||||
server = new Server |
||||
{ |
||||
Services = { TestService.BindService(new TestServiceImpl()) }, |
||||
Ports = { { Host, ServerPort.PickUnused, serverCredentials } } |
||||
}; |
||||
server.Start(); |
||||
|
||||
var options = new List<ChannelOption> |
||||
{ |
||||
new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCredentials.DefaultHostOverride) |
||||
}; |
||||
|
||||
var asyncAuthInterceptor = new AsyncAuthInterceptor(async (authUri, metadata) => |
||||
{ |
||||
await Task.Delay(100); // make sure the operation is asynchronous. |
||||
metadata.Add("authorization", "SECRET_TOKEN"); |
||||
}); |
||||
|
||||
var clientCredentials = ChannelCredentials.Create( |
||||
new SslCredentials(File.ReadAllText(TestCredentials.ClientCertAuthorityPath)), |
||||
new MetadataCredentials(asyncAuthInterceptor)); |
||||
channel = new Channel(Host, server.Ports.Single().BoundPort, clientCredentials, options); |
||||
client = TestService.NewClient(channel); |
||||
} |
||||
|
||||
[TestFixtureTearDown] |
||||
public void Cleanup() |
||||
{ |
||||
channel.ShutdownAsync().Wait(); |
||||
server.ShutdownAsync().Wait(); |
||||
} |
||||
|
||||
[Test] |
||||
public void MetadataCredentials() |
||||
{ |
||||
var response = client.UnaryCall(new SimpleRequest { ResponseSize = 10 }); |
||||
Assert.AreEqual(10, response.Payload.Body.Length); |
||||
} |
||||
} |
||||
} |
@ -1,132 +0,0 @@ |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
// Message definitions to be used by integration test service definitions. |
||||
|
||||
syntax = "proto3"; |
||||
|
||||
package grpc.testing; |
||||
|
||||
// The type of payload that should be returned. |
||||
enum PayloadType { |
||||
// Compressable text format. |
||||
COMPRESSABLE = 0; |
||||
|
||||
// Uncompressable binary format. |
||||
UNCOMPRESSABLE = 1; |
||||
|
||||
// Randomly chosen from all other formats defined in this enum. |
||||
RANDOM = 2; |
||||
} |
||||
|
||||
// A block of data, to simply increase gRPC message size. |
||||
message Payload { |
||||
// The type of data in body. |
||||
PayloadType type = 1; |
||||
// Primary contents of payload. |
||||
bytes body = 2; |
||||
} |
||||
|
||||
// Unary request. |
||||
message SimpleRequest { |
||||
// Desired payload type in the response from the server. |
||||
// If response_type is RANDOM, server randomly chooses one from other formats. |
||||
PayloadType response_type = 1; |
||||
|
||||
// Desired payload size in the response from the server. |
||||
// If response_type is COMPRESSABLE, this denotes the size before compression. |
||||
int32 response_size = 2; |
||||
|
||||
// Optional input payload sent along with the request. |
||||
Payload payload = 3; |
||||
|
||||
// Whether SimpleResponse should include username. |
||||
bool fill_username = 4; |
||||
|
||||
// Whether SimpleResponse should include OAuth scope. |
||||
bool fill_oauth_scope = 5; |
||||
} |
||||
|
||||
// Unary response, as configured by the request. |
||||
message SimpleResponse { |
||||
// Payload to increase message size. |
||||
Payload payload = 1; |
||||
// The user the request came from, for verifying authentication was |
||||
// successful when the client expected it. |
||||
string username = 2; |
||||
// OAuth scope. |
||||
string oauth_scope = 3; |
||||
} |
||||
|
||||
// Client-streaming request. |
||||
message StreamingInputCallRequest { |
||||
// Optional input payload sent along with the request. |
||||
Payload payload = 1; |
||||
|
||||
// Not expecting any payload from the response. |
||||
} |
||||
|
||||
// Client-streaming response. |
||||
message StreamingInputCallResponse { |
||||
// Aggregated size of payloads received from the client. |
||||
int32 aggregated_payload_size = 1; |
||||
} |
||||
|
||||
// Configuration for a particular response. |
||||
message ResponseParameters { |
||||
// Desired payload sizes in responses from the server. |
||||
// If response_type is COMPRESSABLE, this denotes the size before compression. |
||||
int32 size = 1; |
||||
|
||||
// Desired interval between consecutive responses in the response stream in |
||||
// microseconds. |
||||
int32 interval_us = 2; |
||||
} |
||||
|
||||
// Server-streaming request. |
||||
message StreamingOutputCallRequest { |
||||
// Desired payload type in the response from the server. |
||||
// If response_type is RANDOM, the payload from each response in the stream |
||||
// might be of different types. This is to simulate a mixed type of payload |
||||
// stream. |
||||
PayloadType response_type = 1; |
||||
|
||||
// Configuration for each expected response message. |
||||
repeated ResponseParameters response_parameters = 2; |
||||
|
||||
// Optional input payload sent along with the request. |
||||
Payload payload = 3; |
||||
} |
||||
|
||||
// Server-streaming response, as configured by the request and parameters. |
||||
message StreamingOutputCallResponse { |
||||
// Payload to increase response size. |
||||
Payload payload = 1; |
||||
} |
@ -1,71 +0,0 @@ |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
// An integration test service that covers all the method signature permutations |
||||
// of unary/streaming requests/responses. |
||||
syntax = "proto3"; |
||||
|
||||
import "empty.proto"; |
||||
import "messages.proto"; |
||||
|
||||
package grpc.testing; |
||||
|
||||
// A simple service to test the various types of RPCs and experiment with |
||||
// performance with various types of payload. |
||||
service TestService { |
||||
// One empty request followed by one empty response. |
||||
rpc EmptyCall(grpc.testing.Empty) returns (grpc.testing.Empty); |
||||
|
||||
// One request followed by one response. |
||||
rpc UnaryCall(SimpleRequest) returns (SimpleResponse); |
||||
|
||||
// One request followed by a sequence of responses (streamed download). |
||||
// The server returns the payload with client desired type and sizes. |
||||
rpc StreamingOutputCall(StreamingOutputCallRequest) |
||||
returns (stream StreamingOutputCallResponse); |
||||
|
||||
// A sequence of requests followed by one response (streamed upload). |
||||
// The server returns the aggregated size of client payload as the result. |
||||
rpc StreamingInputCall(stream StreamingInputCallRequest) |
||||
returns (StreamingInputCallResponse); |
||||
|
||||
// A sequence of requests with each request served by the server immediately. |
||||
// As one request could lead to multiple responses, this interface |
||||
// demonstrates the idea of full duplexing. |
||||
rpc FullDuplexCall(stream StreamingOutputCallRequest) |
||||
returns (stream StreamingOutputCallResponse); |
||||
|
||||
// A sequence of requests followed by a sequence of responses. |
||||
// The server buffers all the client requests and then serves them in order. A |
||||
// stream of responses are returned to the client when the server starts with |
||||
// first request. |
||||
rpc HalfDuplexCall(stream StreamingOutputCallRequest) |
||||
returns (stream StreamingOutputCallResponse); |
||||
} |
@ -0,0 +1,9 @@ |
||||
HTTP/2 Interop Tests |
||||
==== |
||||
|
||||
This is a suite of tests that check a server to see if it plays nicely with other HTTP/2 clients. To run, just type: |
||||
|
||||
`go test -spec :1234` |
||||
|
||||
Where ":1234" is the ip:port of a running server. |
||||
|
@ -0,0 +1,6 @@ |
||||
// http2interop project doc.go
|
||||
|
||||
/* |
||||
http2interop document |
||||
*/ |
||||
package http2interop |
@ -0,0 +1,11 @@ |
||||
package http2interop |
||||
|
||||
import ( |
||||
"io" |
||||
) |
||||
|
||||
type Frame interface { |
||||
GetHeader() *FrameHeader |
||||
ParsePayload(io.Reader) error |
||||
MarshalBinary() ([]byte, error) |
||||
} |
@ -0,0 +1,109 @@ |
||||
package http2interop |
||||
|
||||
import ( |
||||
"encoding/binary" |
||||
"fmt" |
||||
"io" |
||||
) |
||||
|
||||
type FrameHeader struct { |
||||
Length int |
||||
Type FrameType |
||||
Flags byte |
||||
Reserved Reserved |
||||
StreamID |
||||
} |
||||
|
||||
type Reserved bool |
||||
|
||||
func (r Reserved) String() string { |
||||
if r { |
||||
return "R" |
||||
} |
||||
return "" |
||||
} |
||||
|
||||
func (fh *FrameHeader) Parse(r io.Reader) error { |
||||
buf := make([]byte, 9) |
||||
if _, err := io.ReadFull(r, buf); err != nil { |
||||
return err |
||||
} |
||||
return fh.UnmarshalBinary(buf) |
||||
} |
||||
|
||||
func (fh *FrameHeader) UnmarshalBinary(b []byte) error { |
||||
if len(b) != 9 { |
||||
return fmt.Errorf("Invalid frame header length %d", len(b)) |
||||
} |
||||
*fh = FrameHeader{ |
||||
Length: int(b[0])<<16 | int(b[1])<<8 | int(b[2]), |
||||
Type: FrameType(b[3]), |
||||
Flags: b[4], |
||||
Reserved: Reserved(b[5]>>7 == 1), |
||||
StreamID: StreamID(binary.BigEndian.Uint32(b[5:9]) & 0x7fffffff), |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func (fh *FrameHeader) MarshalBinary() ([]byte, error) { |
||||
buf := make([]byte, 9, 9+fh.Length) |
||||
|
||||
if fh.Length > 0xFFFFFF || fh.Length < 0 { |
||||
return nil, fmt.Errorf("Invalid frame header length: %d", fh.Length) |
||||
} |
||||
if fh.StreamID < 0 { |
||||
return nil, fmt.Errorf("Invalid Stream ID: %v", fh.StreamID) |
||||
} |
||||
|
||||
buf[0], buf[1], buf[2] = byte(fh.Length>>16), byte(fh.Length>>8), byte(fh.Length) |
||||
buf[3] = byte(fh.Type) |
||||
buf[4] = fh.Flags |
||||
binary.BigEndian.PutUint32(buf[5:], uint32(fh.StreamID)) |
||||
|
||||
return buf, nil |
||||
} |
||||
|
||||
type StreamID int32 |
||||
|
||||
type FrameType byte |
||||
|
||||
func (ft FrameType) String() string { |
||||
switch ft { |
||||
case DataFrameType: |
||||
return "DATA" |
||||
case HeadersFrameType: |
||||
return "HEADERS" |
||||
case PriorityFrameType: |
||||
return "PRIORITY" |
||||
case ResetStreamFrameType: |
||||
return "RST_STREAM" |
||||
case SettingsFrameType: |
||||
return "SETTINGS" |
||||
case PushPromiseFrameType: |
||||
return "PUSH_PROMISE" |
||||
case PingFrameType: |
||||
return "PING" |
||||
case GoAwayFrameType: |
||||
return "GOAWAY" |
||||
case WindowUpdateFrameType: |
||||
return "WINDOW_UPDATE" |
||||
case ContinuationFrameType: |
||||
return "CONTINUATION" |
||||
default: |
||||
return fmt.Sprintf("UNKNOWN(%d)", byte(ft)) |
||||
} |
||||
} |
||||
|
||||
// Types
|
||||
const ( |
||||
DataFrameType FrameType = 0 |
||||
HeadersFrameType FrameType = 1 |
||||
PriorityFrameType FrameType = 2 |
||||
ResetStreamFrameType FrameType = 3 |
||||
SettingsFrameType FrameType = 4 |
||||
PushPromiseFrameType FrameType = 5 |
||||
PingFrameType FrameType = 6 |
||||
GoAwayFrameType FrameType = 7 |
||||
WindowUpdateFrameType FrameType = 8 |
||||
ContinuationFrameType FrameType = 9 |
||||
) |
@ -0,0 +1,245 @@ |
||||
package http2interop |
||||
|
||||
import ( |
||||
"crypto/tls" |
||||
"fmt" |
||||
"io" |
||||
"log" |
||||
) |
||||
|
||||
const ( |
||||
Preface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" |
||||
) |
||||
|
||||
func parseFrame(r io.Reader) (Frame, error) { |
||||
fh := FrameHeader{} |
||||
if err := fh.Parse(r); err != nil { |
||||
return nil, err |
||||
} |
||||
var f Frame |
||||
switch fh.Type { |
||||
case PingFrameType: |
||||
f = &PingFrame{ |
||||
Header: fh, |
||||
} |
||||
case SettingsFrameType: |
||||
f = &SettingsFrame{ |
||||
Header: fh, |
||||
} |
||||
default: |
||||
f = &UnknownFrame{ |
||||
Header: fh, |
||||
} |
||||
} |
||||
if err := f.ParsePayload(r); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return f, nil |
||||
} |
||||
|
||||
func streamFrame(w io.Writer, f Frame) error { |
||||
raw, err := f.MarshalBinary() |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if _, err := w.Write(raw); err != nil { |
||||
return err |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func getHttp2Conn(addr string) (*tls.Conn, error) { |
||||
config := &tls.Config{ |
||||
InsecureSkipVerify: true, |
||||
NextProtos: []string{"h2"}, |
||||
} |
||||
|
||||
conn, err := tls.Dial("tcp", addr, config) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return conn, nil |
||||
} |
||||
|
||||
func testClientShortSettings(addr string, length int) error { |
||||
c, err := getHttp2Conn(addr) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
defer c.Close() |
||||
|
||||
if _, err := c.Write([]byte(Preface)); err != nil { |
||||
return err |
||||
} |
||||
|
||||
// Bad, settings, non multiple of 6
|
||||
sf := &UnknownFrame{ |
||||
Header: FrameHeader{ |
||||
Type: SettingsFrameType, |
||||
}, |
||||
Data: make([]byte, length), |
||||
} |
||||
if err := streamFrame(c, sf); err != nil { |
||||
return err |
||||
} |
||||
|
||||
for { |
||||
frame, err := parseFrame(c) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
log.Println(frame) |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func testClientPrefaceWithStreamId(addr string) error { |
||||
c, err := getHttp2Conn(addr) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
defer c.Close() |
||||
|
||||
// Good so far
|
||||
if _, err := c.Write([]byte(Preface)); err != nil { |
||||
return err |
||||
} |
||||
|
||||
// Bad, settings do not have ids
|
||||
sf := &SettingsFrame{ |
||||
Header: FrameHeader{ |
||||
StreamID: 1, |
||||
}, |
||||
} |
||||
if err := streamFrame(c, sf); err != nil { |
||||
return err |
||||
} |
||||
|
||||
for { |
||||
frame, err := parseFrame(c) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
log.Println(frame) |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func testUnknownFrameType(addr string) error { |
||||
c, err := getHttp2Conn(addr) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
defer c.Close() |
||||
|
||||
if _, err := c.Write([]byte(Preface)); err != nil { |
||||
return err |
||||
} |
||||
|
||||
// Send some settings, which are part of the client preface
|
||||
sf := &SettingsFrame{} |
||||
if err := streamFrame(c, sf); err != nil { |
||||
return err |
||||
} |
||||
|
||||
// Write a bunch of invalid frame types.
|
||||
for ft := ContinuationFrameType + 1; ft != 0; ft++ { |
||||
fh := &UnknownFrame{ |
||||
Header: FrameHeader{ |
||||
Type: ft, |
||||
}, |
||||
} |
||||
if err := streamFrame(c, fh); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
|
||||
pf := &PingFrame{ |
||||
Data: []byte("01234567"), |
||||
} |
||||
if err := streamFrame(c, pf); err != nil { |
||||
return err |
||||
} |
||||
|
||||
for { |
||||
frame, err := parseFrame(c) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if npf, ok := frame.(*PingFrame); !ok { |
||||
continue |
||||
} else { |
||||
if string(npf.Data) != string(pf.Data) || npf.Header.Flags&PING_ACK == 0 { |
||||
return fmt.Errorf("Bad ping %+v", *npf) |
||||
} |
||||
return nil |
||||
} |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func testShortPreface(addr string, prefacePrefix string) error { |
||||
c, err := getHttp2Conn(addr) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
defer c.Close() |
||||
|
||||
if _, err := c.Write([]byte(prefacePrefix)); err != nil { |
||||
return err |
||||
} |
||||
|
||||
buf := make([]byte, 256) |
||||
for ; err == nil; _, err = c.Read(buf) { |
||||
} |
||||
// TODO: maybe check for a GOAWAY?
|
||||
return err |
||||
} |
||||
|
||||
func testTLSMaxVersion(addr string, version uint16) error { |
||||
config := &tls.Config{ |
||||
InsecureSkipVerify: true, |
||||
NextProtos: []string{"h2"}, |
||||
MaxVersion: version, |
||||
} |
||||
conn, err := tls.Dial("tcp", addr, config) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
defer conn.Close() |
||||
|
||||
buf := make([]byte, 256) |
||||
if n, err := conn.Read(buf); err != nil { |
||||
if n != 0 { |
||||
return fmt.Errorf("Expected no bytes to be read, but was %d", n) |
||||
} |
||||
return err |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func testTLSApplicationProtocol(addr string) error { |
||||
config := &tls.Config{ |
||||
InsecureSkipVerify: true, |
||||
NextProtos: []string{"h2c"}, |
||||
} |
||||
conn, err := tls.Dial("tcp", addr, config) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
defer conn.Close() |
||||
|
||||
buf := make([]byte, 256) |
||||
if n, err := conn.Read(buf); err != nil { |
||||
if n != 0 { |
||||
return fmt.Errorf("Expected no bytes to be read, but was %d", n) |
||||
} |
||||
return err |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,50 @@ |
||||
package http2interop |
||||
|
||||
import ( |
||||
"crypto/tls" |
||||
"flag" |
||||
"io" |
||||
"os" |
||||
"testing" |
||||
) |
||||
|
||||
var ( |
||||
serverSpec = flag.String("spec", ":50051", "The server spec to test") |
||||
) |
||||
|
||||
func TestShortPreface(t *testing.T) { |
||||
for i := 0; i < len(Preface)-1; i++ { |
||||
if err := testShortPreface(*serverSpec, Preface[:i]+"X"); err != io.EOF { |
||||
t.Error("Expected an EOF but was", err) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestUnknownFrameType(t *testing.T) { |
||||
if err := testUnknownFrameType(*serverSpec); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
} |
||||
|
||||
func TestTLSApplicationProtocol(t *testing.T) { |
||||
if err := testTLSApplicationProtocol(*serverSpec); err != io.EOF { |
||||
t.Fatal("Expected an EOF but was", err) |
||||
} |
||||
} |
||||
|
||||
func TestTLSMaxVersion(t *testing.T) { |
||||
if err := testTLSMaxVersion(*serverSpec, tls.VersionTLS11); err != io.EOF { |
||||
t.Fatal("Expected an EOF but was", err) |
||||
} |
||||
} |
||||
|
||||
func TestClientPrefaceWithStreamId(t *testing.T) { |
||||
if err := testClientPrefaceWithStreamId(*serverSpec); err != io.EOF { |
||||
t.Fatal("Expected an EOF but was", err) |
||||
} |
||||
} |
||||
|
||||
func TestMain(m *testing.M) { |
||||
flag.Parse() |
||||
os.Exit(m.Run()) |
||||
} |
@ -0,0 +1,65 @@ |
||||
package http2interop |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io" |
||||
) |
||||
|
||||
type PingFrame struct { |
||||
Header FrameHeader |
||||
Data []byte |
||||
} |
||||
|
||||
const ( |
||||
PING_ACK = 0x01 |
||||
) |
||||
|
||||
func (f *PingFrame) GetHeader() *FrameHeader { |
||||
return &f.Header |
||||
} |
||||
|
||||
func (f *PingFrame) ParsePayload(r io.Reader) error { |
||||
raw := make([]byte, f.Header.Length) |
||||
if _, err := io.ReadFull(r, raw); err != nil { |
||||
return err |
||||
} |
||||
return f.UnmarshalPayload(raw) |
||||
} |
||||
|
||||
func (f *PingFrame) UnmarshalPayload(raw []byte) error { |
||||
if f.Header.Length != len(raw) { |
||||
return fmt.Errorf("Invalid Payload length %d != %d", f.Header.Length, len(raw)) |
||||
} |
||||
if f.Header.Length != 8 { |
||||
return fmt.Errorf("Invalid Payload length %d", f.Header.Length) |
||||
} |
||||
|
||||
f.Data = []byte(string(raw)) |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func (f *PingFrame) MarshalPayload() ([]byte, error) { |
||||
if len(f.Data) != 8 { |
||||
return nil, fmt.Errorf("Invalid Payload length %d", len(f.Data)) |
||||
} |
||||
return []byte(string(f.Data)), nil |
||||
} |
||||
|
||||
func (f *PingFrame) MarshalBinary() ([]byte, error) { |
||||
payload, err := f.MarshalPayload() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
f.Header.Length = len(payload) |
||||
f.Header.Type = PingFrameType |
||||
header, err := f.Header.MarshalBinary() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
header = append(header, payload...) |
||||
|
||||
return header, nil |
||||
} |
@ -0,0 +1,109 @@ |
||||
package http2interop |
||||
|
||||
import ( |
||||
"encoding/binary" |
||||
"fmt" |
||||
"io" |
||||
) |
||||
|
||||
const ( |
||||
SETTINGS_ACK = 1 |
||||
) |
||||
|
||||
type SettingsFrame struct { |
||||
Header FrameHeader |
||||
Params []SettingsParameter |
||||
} |
||||
|
||||
type SettingsIdentifier uint16 |
||||
|
||||
const ( |
||||
SettingsHeaderTableSize SettingsIdentifier = 1 |
||||
SettingsEnablePush SettingsIdentifier = 2 |
||||
SettingsMaxConcurrentStreams SettingsIdentifier = 3 |
||||
SettingsInitialWindowSize SettingsIdentifier = 4 |
||||
SettingsMaxFrameSize SettingsIdentifier = 5 |
||||
SettingsMaxHeaderListSize SettingsIdentifier = 6 |
||||
) |
||||
|
||||
func (si SettingsIdentifier) String() string { |
||||
switch si { |
||||
case SettingsHeaderTableSize: |
||||
return "HEADER_TABLE_SIZE" |
||||
case SettingsEnablePush: |
||||
return "ENABLE_PUSH" |
||||
case SettingsMaxConcurrentStreams: |
||||
return "MAX_CONCURRENT_STREAMS" |
||||
case SettingsInitialWindowSize: |
||||
return "INITIAL_WINDOW_SIZE" |
||||
case SettingsMaxFrameSize: |
||||
return "MAX_FRAME_SIZE" |
||||
case SettingsMaxHeaderListSize: |
||||
return "MAX_HEADER_LIST_SIZE" |
||||
default: |
||||
return fmt.Sprintf("UNKNOWN(%d)", uint16(si)) |
||||
} |
||||
} |
||||
|
||||
type SettingsParameter struct { |
||||
Identifier SettingsIdentifier |
||||
Value uint32 |
||||
} |
||||
|
||||
func (f *SettingsFrame) GetHeader() *FrameHeader { |
||||
return &f.Header |
||||
} |
||||
|
||||
func (f *SettingsFrame) ParsePayload(r io.Reader) error { |
||||
raw := make([]byte, f.Header.Length) |
||||
if _, err := io.ReadFull(r, raw); err != nil { |
||||
return err |
||||
} |
||||
return f.UnmarshalPayload(raw) |
||||
} |
||||
|
||||
func (f *SettingsFrame) UnmarshalPayload(raw []byte) error { |
||||
if f.Header.Length != len(raw) { |
||||
return fmt.Errorf("Invalid Payload length %d != %d", f.Header.Length, len(raw)) |
||||
} |
||||
|
||||
if f.Header.Length%6 != 0 { |
||||
return fmt.Errorf("Invalid Payload length %d", f.Header.Length) |
||||
} |
||||
|
||||
f.Params = make([]SettingsParameter, 0, f.Header.Length/6) |
||||
for i := 0; i < len(raw); i += 6 { |
||||
f.Params = append(f.Params, SettingsParameter{ |
||||
Identifier: SettingsIdentifier(binary.BigEndian.Uint16(raw[i : i+2])), |
||||
Value: binary.BigEndian.Uint32(raw[i+2 : i+6]), |
||||
}) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func (f *SettingsFrame) MarshalPayload() ([]byte, error) { |
||||
raw := make([]byte, 0, len(f.Params)*6) |
||||
for i, p := range f.Params { |
||||
binary.BigEndian.PutUint16(raw[i*6:i*6+2], uint16(p.Identifier)) |
||||
binary.BigEndian.PutUint32(raw[i*6+2:i*6+6], p.Value) |
||||
} |
||||
return raw, nil |
||||
} |
||||
|
||||
func (f *SettingsFrame) MarshalBinary() ([]byte, error) { |
||||
payload, err := f.MarshalPayload() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
f.Header.Length = len(payload) |
||||
f.Header.Type = SettingsFrameType |
||||
header, err := f.Header.MarshalBinary() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
header = append(header, payload...) |
||||
|
||||
return header, nil |
||||
} |
@ -0,0 +1,54 @@ |
||||
package http2interop |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io" |
||||
) |
||||
|
||||
type UnknownFrame struct { |
||||
Header FrameHeader |
||||
Data []byte |
||||
} |
||||
|
||||
func (f *UnknownFrame) GetHeader() *FrameHeader { |
||||
return &f.Header |
||||
} |
||||
|
||||
func (f *UnknownFrame) ParsePayload(r io.Reader) error { |
||||
raw := make([]byte, f.Header.Length) |
||||
if _, err := io.ReadFull(r, raw); err != nil { |
||||
return err |
||||
} |
||||
return f.UnmarshalPayload(raw) |
||||
} |
||||
|
||||
func (f *UnknownFrame) UnmarshalPayload(raw []byte) error { |
||||
if f.Header.Length != len(raw) { |
||||
return fmt.Errorf("Invalid Payload length %d != %d", f.Header.Length, len(raw)) |
||||
} |
||||
|
||||
f.Data = []byte(string(raw)) |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func (f *UnknownFrame) MarshalPayload() ([]byte, error) { |
||||
return []byte(string(f.Data)), nil |
||||
} |
||||
|
||||
func (f *UnknownFrame) MarshalBinary() ([]byte, error) { |
||||
f.Header.Length = len(f.Data) |
||||
buf, err := f.Header.MarshalBinary() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
payload, err := f.MarshalPayload() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
buf = append(buf, payload...) |
||||
|
||||
return buf, nil |
||||
} |
@ -0,0 +1,21 @@ |
||||
@rem Performs nuget restore step for C/C++. |
||||
|
||||
setlocal |
||||
|
||||
@rem enter repo root |
||||
cd /d %~dp0\..\.. |
||||
|
||||
@rem Location of nuget.exe |
||||
set NUGET=C:\nuget\nuget.exe |
||||
|
||||
if exist %NUGET% ( |
||||
%NUGET% restore vsprojects/grpc.sln || goto :error |
||||
) |
||||
|
||||
endlocal |
||||
|
||||
goto :EOF |
||||
|
||||
:error |
||||
echo Failed! |
||||
exit /b %errorlevel% |
@ -0,0 +1,22 @@ |
||||
@rem Performs nuget restore step for C#. |
||||
|
||||
setlocal |
||||
|
||||
@rem enter repo root |
||||
cd /d %~dp0\..\.. |
||||
|
||||
@rem Location of nuget.exe |
||||
set NUGET=C:\nuget\nuget.exe |
||||
|
||||
if exist %NUGET% ( |
||||
%NUGET% restore vsprojects/grpc_csharp_ext.sln || goto :error |
||||
%NUGET% restore src/csharp/Grpc.sln || goto :error |
||||
) |
||||
|
||||
endlocal |
||||
|
||||
goto :EOF |
||||
|
||||
:error |
||||
echo Failed! |
||||
exit /b %errorlevel% |
@ -0,0 +1,41 @@ |
||||
#!/bin/bash |
||||
# Copyright 2015, Google Inc. |
||||
# All rights reserved. |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
set -ex |
||||
|
||||
# cd to gRPC csharp directory |
||||
cd $(dirname $0)/../../src/csharp |
||||
|
||||
root=`pwd` |
||||
|
||||
if [ -n "$NUGET" ] |
||||
then |
||||
$NUGET restore Grpc.sln |
||||
fi |
@ -0,0 +1,39 @@ |
||||
#!/bin/bash |
||||
|
||||
# Copyright 2015, Google Inc. |
||||
# All rights reserved. |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
set -ex |
||||
|
||||
export GRPC_CONFIG=${CONFIG:-opt} |
||||
|
||||
# change to grpc's ruby directory |
||||
cd $(dirname $0)/../../src/ruby |
||||
|
||||
bundle install |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue