SRWebSocket.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #import <Foundation/Foundation.h>
  17. #import <Security/SecCertificate.h>
  18. typedef NS_ENUM(NSInteger, SRReadyState) {
  19. SR_CONNECTING = 0,
  20. SR_OPEN = 1,
  21. SR_CLOSING = 2,
  22. SR_CLOSED = 3,
  23. };
  24. typedef enum SRStatusCode : NSInteger {
  25. // 0–999: Reserved and not used.
  26. SRStatusCodeNormal = 1000,
  27. SRStatusCodeGoingAway = 1001,
  28. SRStatusCodeProtocolError = 1002,
  29. SRStatusCodeUnhandledType = 1003,
  30. // 1004 reserved.
  31. SRStatusNoStatusReceived = 1005,
  32. SRStatusCodeAbnormal = 1006,
  33. SRStatusCodeInvalidUTF8 = 1007,
  34. SRStatusCodePolicyViolated = 1008,
  35. SRStatusCodeMessageTooBig = 1009,
  36. SRStatusCodeMissingExtension = 1010,
  37. SRStatusCodeInternalError = 1011,
  38. SRStatusCodeServiceRestart = 1012,
  39. SRStatusCodeTryAgainLater = 1013,
  40. // 1014: Reserved for future use by the WebSocket standard.
  41. SRStatusCodeTLSHandshake = 1015,
  42. // 1016–1999: Reserved for future use by the WebSocket standard.
  43. // 2000–2999: Reserved for use by WebSocket extensions.
  44. // 3000–3999: Available for use by libraries and frameworks. May not be used by applications. Available for registration at the IANA via first-come, first-serve.
  45. // 4000–4999: Available for use by applications.
  46. } SRStatusCode;
  47. @class SRWebSocket;
  48. extern NSString *const SRWebSocketErrorDomain;
  49. extern NSString *const SRHTTPResponseErrorKey;
  50. #pragma mark - SRWebSocketDelegate
  51. @protocol SRWebSocketDelegate;
  52. #pragma mark - SRWebSocket
  53. @interface SRWebSocket : NSObject <NSStreamDelegate>
  54. @property (nonatomic, weak) id <SRWebSocketDelegate> delegate;
  55. @property (nonatomic, readonly) SRReadyState readyState;
  56. @property (nonatomic, readonly, retain) NSURL *url;
  57. @property (nonatomic, readonly) CFHTTPMessageRef receivedHTTPHeaders;
  58. // Optional array of cookies (NSHTTPCookie objects) to apply to the connections
  59. @property (nonatomic, readwrite) NSArray * requestCookies;
  60. // This returns the negotiated protocol.
  61. // It will be nil until after the handshake completes.
  62. @property (nonatomic, readonly, copy) NSString *protocol;
  63. // Protocols should be an array of strings that turn into Sec-WebSocket-Protocol.
  64. - (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates;
  65. - (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols;
  66. - (id)initWithURLRequest:(NSURLRequest *)request;
  67. // Some helper constructors.
  68. - (id)initWithURL:(NSURL *)url protocols:(NSArray *)protocols allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates;
  69. - (id)initWithURL:(NSURL *)url protocols:(NSArray *)protocols;
  70. - (id)initWithURL:(NSURL *)url;
  71. // Delegate queue will be dispatch_main_queue by default.
  72. // You cannot set both OperationQueue and dispatch_queue.
  73. - (void)setDelegateOperationQueue:(NSOperationQueue*) queue;
  74. - (void)setDelegateDispatchQueue:(dispatch_queue_t) queue;
  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. // SRWebSockets are intended for one-time-use only. Open should be called once and only once.
  79. - (void)open;
  80. - (void)close;
  81. - (void)closeWithCode:(NSInteger)code reason:(NSString *)reason;
  82. // Send a UTF8 String or Data.
  83. - (void)send:(id)data;
  84. // Send Data (can be nil) in a ping message.
  85. - (void)sendPing:(NSData *)data;
  86. @end
  87. #pragma mark - SRWebSocketDelegate
  88. @protocol SRWebSocketDelegate <NSObject>
  89. // message will either be an NSString if the server is using text
  90. // or NSData if the server is using binary.
  91. - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
  92. @optional
  93. - (void)webSocketDidOpen:(SRWebSocket *)webSocket;
  94. - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
  95. - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
  96. - (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload;
  97. // Return YES to convert messages sent as Text to an NSString. Return NO to skip NSData -> NSString conversion for Text messages. Defaults to YES.
  98. - (BOOL)webSocketShouldConvertTextFrameToString:(SRWebSocket *)webSocket;
  99. @end
  100. #pragma mark - NSURLRequest (SRCertificateAdditions)
  101. @interface NSURLRequest (SRCertificateAdditions)
  102. @property (nonatomic, retain, readonly) NSArray *SR_SSLPinnedCertificates;
  103. @end
  104. #pragma mark - NSMutableURLRequest (SRCertificateAdditions)
  105. @interface NSMutableURLRequest (SRCertificateAdditions)
  106. @property (nonatomic, retain) NSArray *SR_SSLPinnedCertificates;
  107. @end
  108. #pragma mark - NSRunLoop (SRWebSocket)
  109. @interface NSRunLoop (SRWebSocket)
  110. + (NSRunLoop *)SR_networkRunLoop;
  111. @end