|
| 1 | +// |
| 2 | +// Copyright 2012 Square Inc. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +#import <Foundation/Foundation.h> |
| 18 | +#import <Security/SecCertificate.h> |
| 19 | + |
| 20 | +typedef NS_ENUM(unsigned int, RCTSRReadyState) { |
| 21 | + RCTSR_CONNECTING = 0, |
| 22 | + RCTSR_OPEN = 1, |
| 23 | + RCTSR_CLOSING = 2, |
| 24 | + RCTSR_CLOSED = 3, |
| 25 | +}; |
| 26 | + |
| 27 | +typedef NS_ENUM(NSInteger, RCTSRStatusCode) { |
| 28 | + RCTSRStatusCodeNormal = 1000, |
| 29 | + RCTSRStatusCodeGoingAway = 1001, |
| 30 | + RCTSRStatusCodeProtocolError = 1002, |
| 31 | + RCTSRStatusCodeUnhandledType = 1003, |
| 32 | + // 1004 reserved. |
| 33 | + RCTSRStatusNoStatusReceived = 1005, |
| 34 | + // 1004-1006 reserved. |
| 35 | + RCTSRStatusCodeInvalidUTF8 = 1007, |
| 36 | + RCTSRStatusCodePolicyViolated = 1008, |
| 37 | + RCTSRStatusCodeMessageTooBig = 1009, |
| 38 | +}; |
| 39 | + |
| 40 | +@class RCTSRWebSocket; |
| 41 | + |
| 42 | +extern NSString *const RCTSRWebSocketErrorDomain; |
| 43 | +extern NSString *const RCTSRHTTPResponseErrorKey; |
| 44 | + |
| 45 | +#pragma mark - RCTSRWebSocketDelegate |
| 46 | + |
| 47 | +@protocol RCTSRWebSocketDelegate; |
| 48 | + |
| 49 | +#pragma mark - RCTSRWebSocket |
| 50 | + |
| 51 | +@interface RCTSRWebSocket : NSObject <NSStreamDelegate> |
| 52 | + |
| 53 | +@property (nonatomic, weak) id<RCTSRWebSocketDelegate> delegate; |
| 54 | + |
| 55 | +@property (nonatomic, readonly) RCTSRReadyState readyState; |
| 56 | +@property (nonatomic, readonly, strong) NSURL *url; |
| 57 | + |
| 58 | +// This returns the negotiated protocol. |
| 59 | +// It will be nil until after the handshake completes. |
| 60 | +@property (nonatomic, readonly, copy) NSString *protocol; |
| 61 | + |
| 62 | +// Protocols should be an array of strings that turn into Sec-WebSocket-Protocol. |
| 63 | +- (instancetype)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray<NSString *> *)protocols NS_DESIGNATED_INITIALIZER; |
| 64 | +- (instancetype)initWithURLRequest:(NSURLRequest *)request; |
| 65 | + |
| 66 | +// Some helper constructors. |
| 67 | +- (instancetype)initWithURL:(NSURL *)url protocols:(NSArray<NSString *> *)protocols; |
| 68 | +- (instancetype)initWithURL:(NSURL *)url; |
| 69 | + |
| 70 | +// Delegate queue will be dispatch_main_queue by default. |
| 71 | +// You cannot set both OperationQueue and dispatch_queue. |
| 72 | +- (void)setDelegateOperationQueue:(NSOperationQueue *)queue; |
| 73 | +- (void)setDelegateDispatchQueue:(dispatch_queue_t)queue; |
| 74 | + |
| 75 | +// By default, it will schedule itself on +[NSRunLoop RCTSR_networkRunLoop] using defaultModes. |
| 76 | +- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode; |
| 77 | +- (void)unscheduleFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode; |
| 78 | + |
| 79 | +// RCTSRWebSockets are intended for one-time-use only. Open should be called once and only once. |
| 80 | +- (void)open; |
| 81 | + |
| 82 | +- (void)close; |
| 83 | +- (void)closeWithCode:(NSInteger)code reason:(NSString *)reason; |
| 84 | + |
| 85 | +// Send a UTF8 String or Data. |
| 86 | +- (void)send:(id)data; |
| 87 | + |
| 88 | +// Send Data (can be nil) in a ping message. |
| 89 | +- (void)sendPing:(NSData *)data; |
| 90 | + |
| 91 | +@end |
| 92 | + |
| 93 | +#pragma mark - RCTSRWebSocketDelegate |
| 94 | + |
| 95 | +@protocol RCTSRWebSocketDelegate <NSObject> |
| 96 | + |
| 97 | +// message will either be an NSString if the server is using text |
| 98 | +// or NSData if the server is using binary. |
| 99 | +- (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message; |
| 100 | + |
| 101 | +@optional |
| 102 | + |
| 103 | +- (void)webSocketDidOpen:(RCTSRWebSocket *)webSocket; |
| 104 | +- (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error; |
| 105 | +- (void)webSocket:(RCTSRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean; |
| 106 | +- (void)webSocket:(RCTSRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload; |
| 107 | + |
| 108 | +@end |
| 109 | + |
| 110 | +#pragma mark - NSURLRequest (CertificateAdditions) |
| 111 | + |
| 112 | +@interface NSURLRequest (CertificateAdditions) |
| 113 | + |
| 114 | +@property (nonatomic, readonly, copy) NSArray *RCTSR_SSLPinnedCertificates; |
| 115 | + |
| 116 | +@end |
| 117 | + |
| 118 | +#pragma mark - NSMutableURLRequest (CertificateAdditions) |
| 119 | + |
| 120 | +@interface NSMutableURLRequest (CertificateAdditions) |
| 121 | + |
| 122 | +@property (nonatomic, copy) NSArray *RCTSR_SSLPinnedCertificates; |
| 123 | + |
| 124 | +@end |
| 125 | + |
| 126 | +#pragma mark - NSRunLoop (RCTSRWebSocket) |
| 127 | + |
| 128 | +@interface NSRunLoop (RCTSRWebSocket) |
| 129 | + |
| 130 | ++ (NSRunLoop *)RCTSR_networkRunLoop; |
| 131 | + |
| 132 | +@end |
0 commit comments