blob: a230646073d00e029ffd05cc48b25877977db29c [file] [log] [blame]
tkchin@webrtc.org87776a82014-12-09 19:32:35 +00001//
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
20typedef enum {
Jonas Olssona4d87372019-07-05 19:08:33 +020021 SR_CONNECTING = 0,
22 SR_OPEN = 1,
23 SR_CLOSING = 2,
24 SR_CLOSED = 3,
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000025} SRReadyState;
26
27typedef enum SRStatusCode : NSInteger {
Jonas Olssona4d87372019-07-05 19:08:33 +020028 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,
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000038} SRStatusCode;
39
40@class SRWebSocket;
41
42extern NSString *const SRWebSocketErrorDomain;
43extern NSString *const SRHTTPResponseErrorKey;
44
45#pragma mark - SRWebSocketDelegate
46
47@protocol SRWebSocketDelegate;
48
49#pragma mark - SRWebSocket
50
51@interface SRWebSocket : NSObject <NSStreamDelegate>
52
Jonas Olssona4d87372019-07-05 19:08:33 +020053@property(nonatomic, weak) id<SRWebSocketDelegate> delegate;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000054
Jonas Olssona4d87372019-07-05 19:08:33 +020055@property(nonatomic, readonly) SRReadyState readyState;
56@property(nonatomic, readonly, retain) NSURL *url;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000057
58// This returns the negotiated protocol.
59// It will be nil until after the handshake completes.
Jonas Olssona4d87372019-07-05 19:08:33 +020060@property(nonatomic, readonly, copy) NSString *protocol;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000061
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.
Jonas Olssona4d87372019-07-05 19:08:33 +020072- (void)setDelegateOperationQueue:(NSOperationQueue *)queue;
73- (void)setDelegateDispatchQueue:(dispatch_queue_t)queue;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000074
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;
Jonas Olssona4d87372019-07-05 19:08:33 +0200105- (void)webSocket:(SRWebSocket *)webSocket
106 didCloseWithCode:(NSInteger)code
107 reason:(NSString *)reason
108 wasClean:(BOOL)wasClean;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000109- (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload;
110
111@end
112
113#pragma mark - NSURLRequest (CertificateAdditions)
114
115@interface NSURLRequest (CertificateAdditions)
116
Jonas Olssona4d87372019-07-05 19:08:33 +0200117@property(nonatomic, retain, readonly) NSArray *SR_SSLPinnedCertificates;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000118
119@end
120
121#pragma mark - NSMutableURLRequest (CertificateAdditions)
122
123@interface NSMutableURLRequest (CertificateAdditions)
124
Jonas Olssona4d87372019-07-05 19:08:33 +0200125@property(nonatomic, retain) NSArray *SR_SSLPinnedCertificates;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000126
127@end
128
129#pragma mark - NSRunLoop (SRWebSocket)
130
131@interface NSRunLoop (SRWebSocket)
132
133+ (NSRunLoop *)SR_networkRunLoop;
134
135@end