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 enum {
21   SR_CONNECTING = 0,
22   SR_OPEN = 1,
23   SR_CLOSING = 2,
24   SR_CLOSED = 3,
25 } SRReadyState;
26 
27 typedef enum SRStatusCode : NSInteger {
28   SRStatusCodeNormal = 1000,
29   SRStatusCodeGoingAway = 1001,
30   SRStatusCodeProtocolError = 1002,
31   SRStatusCodeUnhandledType = 1003,
32   // 1004 reserved.
33   SRStatusNoStatusReceived = 1005,
34   // 1004-1006 reserved.
35   SRStatusCodeInvalidUTF8 = 1007,
36   SRStatusCodePolicyViolated = 1008,
37   SRStatusCodeMessageTooBig = 1009,
38 } SRStatusCode;
39 
40 @class SRWebSocket;
41 
42 extern NSString *const SRWebSocketErrorDomain;
43 extern NSString *const SRHTTPResponseErrorKey;
44 
45 #pragma mark - SRWebSocketDelegate
46 
47 @protocol SRWebSocketDelegate;
48 
49 #pragma mark - SRWebSocket
50 
51 @interface SRWebSocket : NSObject <NSStreamDelegate>
52 
53 @property(nonatomic, weak) id<SRWebSocketDelegate> delegate;
54 
55 @property(nonatomic, readonly) SRReadyState readyState;
56 @property(nonatomic, readonly, retain) 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 - (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols;
64 - (id)initWithURLRequest:(NSURLRequest *)request;
65 
66 // Some helper constructors.
67 - (id)initWithURL:(NSURL *)url protocols:(NSArray *)protocols;
68 - (id)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 SR_networkRunLoop] using defaultModes.
76 - (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;
77 - (void)unscheduleFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;
78 
79 // SRWebSockets 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 - SRWebSocketDelegate
94 
95 @protocol SRWebSocketDelegate <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:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
100 
101 @optional
102 
103 - (void)webSocketDidOpen:(SRWebSocket *)webSocket;
104 - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
105 - (void)webSocket:(SRWebSocket *)webSocket
106     didCloseWithCode:(NSInteger)code
107               reason:(NSString *)reason
108             wasClean:(BOOL)wasClean;
109 - (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload;
110 
111 @end
112 
113 #pragma mark - NSURLRequest (CertificateAdditions)
114 
115 @interface NSURLRequest (CertificateAdditions)
116 
117 @property(nonatomic, retain, readonly) NSArray *SR_SSLPinnedCertificates;
118 
119 @end
120 
121 #pragma mark - NSMutableURLRequest (CertificateAdditions)
122 
123 @interface NSMutableURLRequest (CertificateAdditions)
124 
125 @property(nonatomic, retain) NSArray *SR_SSLPinnedCertificates;
126 
127 @end
128 
129 #pragma mark - NSRunLoop (SRWebSocket)
130 
131 @interface NSRunLoop (SRWebSocket)
132 
133 + (NSRunLoop *)SR_networkRunLoop;
134 
135 @end
136