Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Grpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tci-gateway-module
Grpc
Commits
05640a58
Commit
05640a58
authored
9 years ago
by
Jorge Canizales
Browse files
Options
Downloads
Patches
Plain Diff
Simpler code, better exceptions.
parent
030e5d0c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/objective-c/GRPCClient/GRPCRequestHeaders.m
+35
-50
35 additions, 50 deletions
src/objective-c/GRPCClient/GRPCRequestHeaders.m
with
35 additions
and
50 deletions
src/objective-c/GRPCClient/GRPCRequestHeaders.m
+
35
−
50
View file @
05640a58
...
...
@@ -38,25 +38,33 @@
#import "GRPCCall.h"
#import "NSDictionary+GRPC.h"
// Used by the setters.
static
NSString
*
NormalizeKey
(
NSString
*
key
)
{
if
([
key
canBeConvertedToEncoding
:
NSASCIIStringEncoding
])
{
return
[
key
lowercaseString
];
}
else
{
return
nil
;
if
(
!
key
)
{
[
NSException
raise
:
NSInvalidArgumentException
format
:
@"Key cannot be nil"
];
}
if
(
!
[
key
canBeConvertedToEncoding
:
NSASCIIStringEncoding
])
{
[
NSException
raise
:
NSInvalidArgumentException
format:
@"Key %@ contains non-ASCII characters"
,
key
];
}
return
key
.
lowercaseString
;
}
static
bool
IsKeyValuePairValid
(
NSString
*
key
,
id
value
)
{
// Precondition: key isn't nil.
static
void
CheckKeyValuePairIsValid
(
NSString
*
key
,
id
value
)
{
if
([
key
hasSuffix
:
@"-bin"
])
{
if
(
!
[
value
isKindOfClass
:[
NSData
class
]])
{
return
false
;
if
(
!
[
value
isKindOfClass
:
NSData
.
class
])
{
[
NSException
raise
:
NSInvalidArgumentException
format:
@"Expected NSData value for header %@ ending in
\"
-bin
\"
, "
@"instead got %@"
,
key
,
value
];
}
}
else
{
if
(
!
[
value
isKindOfClass
:[
NSString
class
]])
{
return
false
;
if
(
!
[
value
isKindOfClass
:
NSString
.
class
])
{
[
NSException
raise
:
NSInvalidArgumentException
format:
@"Expected NSString value for header %@ not ending in
\"
-bin
\"
, "
@"instead got %@"
,
key
,
value
];
}
}
return
true
;
}
@implementation
GRPCRequestHeaders
{
...
...
@@ -72,55 +80,32 @@ static bool IsKeyValuePairValid(NSString *key, id value) {
return
self
;
}
-
(
id
)
objectForKeyedSubscript
:(
NSString
*
)
key
{
NSString
*
normalizedKey
=
NormalizeKey
(
key
);
if
(
normalizedKey
)
{
return
_proxy
[
normalizedKey
];
}
else
{
return
[
NSNull
null
];
-
(
void
)
checkCallIsNotStarted
{
if
(
_call
.
state
!=
GRXWriterStateNotStarted
)
{
[
NSException
raise
:
@"Invalid modification"
format:
@"Cannot modify request headers after call is started"
];
}
}
-
(
id
)
objectForKeyedSubscript
:(
NSString
*
)
key
{
return
_proxy
[
key
.
lowercaseString
];
}
-
(
void
)
setObject
:(
id
)
obj
forKeyedSubscript
:(
NSString
*
)
key
{
if
(
_call
.
state
==
GRXWriterStateNotStarted
)
{
NSString
*
normalizedKey
=
NormalizeKey
(
key
);
if
(
normalizedKey
)
{
if
(
IsKeyValuePairValid
(
key
,
obj
))
{
_proxy
[
normalizedKey
]
=
obj
;
}
else
{
[
NSException
raise
:
@"Invalid key/value pair"
format:
@"Key %@ could not be added with value %@"
,
key
,
obj
];
}
}
else
{
[
NSException
raise
:
@"Invalid key"
format
:
@"Key %@ contains illegal characters"
,
key
];
}
}
else
{
[
NSException
raise
:
@"Invalid modification"
format:
@"Cannot modify request metadata after call is started"
];
}
[
self
checkCallIsNotStarted
];
key
=
NormalizeKey
(
key
);
CheckKeyValuePairIsValid
(
key
,
obj
);
_proxy
[
key
]
=
obj
;
}
-
(
void
)
removeObjectForKey
:(
NSString
*
)
aKey
{
if
(
_call
.
state
==
GRXWriterStateNotStarted
)
{
NSString
*
normalizedKey
=
NormalizeKey
(
aKey
);
if
(
normalizedKey
)
{
[
_proxy
removeObjectForKey
:
normalizedKey
];
}
else
{
[
NSException
raise
:
@"Invalid key"
format
:
@"Key %@ contains illegal characters"
,
aKey
];
}
}
else
{
[
NSException
raise
:
@"Invalid modification"
format:
@"Cannot modify request metadata after call is started"
];
}
-
(
void
)
removeObjectForKey
:(
NSString
*
)
key
{
[
self
checkCallIsNotStarted
];
[
_proxy
removeObjectForKey
:
NormalizeKey
(
key
)];
}
-
(
void
)
removeAllObjects
{
if
(
_call
.
state
==
GRXWriterStateNotStarted
)
{
[
_proxy
removeAllObjects
];
}
else
{
[
NSException
raise
:
@"Invalid modification"
format:
@"Cannot modify request metadata after call is started"
];
}
[
self
checkCallIsNotStarted
];
[
_proxy
removeAllObjects
];
}
// TODO(jcanizales): Just forward all invocations?
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment