Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: third_party/grpc/src/objective-c/GRPCClient/private/GRPCRequestHeaders.m

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #import "GRPCRequestHeaders.h"
35
36 #import <Foundation/Foundation.h>
37
38 #import "NSDictionary+GRPC.h"
39
40 // Used by the setter.
41 static void CheckIsNonNilASCII(NSString *name, NSString* value) {
42 if (!value) {
43 [NSException raise:NSInvalidArgumentException format:@"%@ cannot be nil", na me];
44 }
45 if (![value canBeConvertedToEncoding:NSASCIIStringEncoding]) {
46 [NSException raise:NSInvalidArgumentException
47 format:@"%@ %@ contains non-ASCII characters", name, value];
48 }
49 }
50
51 // Precondition: key isn't nil.
52 static void CheckKeyValuePairIsValid(NSString *key, id value) {
53 if ([key hasSuffix:@"-bin"]) {
54 if (![value isKindOfClass:NSData.class]) {
55 [NSException raise:NSInvalidArgumentException
56 format:@"Expected NSData value for header %@ ending in \"-bin\ ", "
57 @"instead got %@", key, value];
58 }
59 } else {
60 if (![value isKindOfClass:NSString.class]) {
61 [NSException raise:NSInvalidArgumentException
62 format:@"Expected NSString value for header %@ not ending in \ "-bin\", "
63 @"instead got %@", key, value];
64 }
65 CheckIsNonNilASCII(@"Text header value", (NSString *)value);
66 }
67 }
68
69 @implementation GRPCRequestHeaders {
70 __weak GRPCCall *_call;
71 // The NSMutableDictionary superclass doesn't hold any storage (so that people can implement their
72 // own in subclasses). As that's not the reason we're subclassing, we just del egate storage to the
73 // default NSMutableDictionary subclass returned by the cluster (e.g. __NSDict ionaryM on iOS 9).
74 NSMutableDictionary *_delegate;
75 }
76
77 - (instancetype)init {
78 return [self initWithCall:nil];
79 }
80
81 - (instancetype)initWithCapacity:(NSUInteger)numItems {
82 return [self init];
83 }
84
85 - (instancetype)initWithCoder:(NSCoder *)aDecoder {
86 return [self init];
87 }
88
89 - (instancetype)initWithCall:(GRPCCall *)call {
90 return [self initWithCall:call storage:[NSMutableDictionary dictionary]];
91 }
92
93 // Designated initializer
94 - (instancetype)initWithCall:(GRPCCall *)call storage:(NSMutableDictionary *)sto rage {
95 // TODO(jcanizales): Throw if call or storage are nil.
96 if ((self = [super init])) {
97 _call = call;
98 _delegate = storage;
99 }
100 return self;
101 }
102
103 - (instancetype)initWithObjects:(const id _Nonnull __unsafe_unretained *)object s
104 forKeys:(const id<NSCopying> _Nonnull __unsafe_unretain ed *)keys
105 count:(NSUInteger)cnt {
106 return [self init];
107 }
108
109 - (void)checkCallIsNotStarted {
110 if (_call.state != GRXWriterStateNotStarted) {
111 [NSException raise:@"Invalid modification"
112 format:@"Cannot modify request headers after call is started"];
113 }
114 }
115
116 - (id)objectForKey:(NSString *)key {
117 return _delegate[key.lowercaseString];
118 }
119
120 - (void)setObject:(id)obj forKey:(NSString *)key {
121 [self checkCallIsNotStarted];
122 CheckIsNonNilASCII(@"Header name", key);
123 key = key.lowercaseString;
124 CheckKeyValuePairIsValid(key, obj);
125 _delegate[key] = obj;
126 }
127
128 - (void)removeObjectForKey:(NSString *)key {
129 [self checkCallIsNotStarted];
130 [_delegate removeObjectForKey:key.lowercaseString];
131 }
132
133 - (NSUInteger)count {
134 return _delegate.count;
135 }
136
137 - (NSEnumerator * _Nonnull)keyEnumerator {
138 return [_delegate keyEnumerator];
139 }
140
141 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698