SRWebSocket.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 enum {
  19. SR_CONNECTING = 0,
  20. SR_OPEN = 1,
  21. SR_CLOSING = 2,
  22. SR_CLOSED = 3,
  23. } SRReadyState;
  24. typedef enum SRStatusCode : NSInteger {
  25. SRStatusCodeNormal = 1000,
  26. SRStatusCodeGoingAway = 1001,
  27. SRStatusCodeProtocolError = 1002,
  28. SRStatusCodeUnhandledType = 1003,
  29. // 1004 reserved.
  30. SRStatusNoStatusReceived = 1005,
  31. // 1004-1006 reserved.
  32. SRStatusCodeInvalidUTF8 = 1007,
  33. SRStatusCodePolicyViolated = 1008,
  34. SRStatusCodeMessageTooBig = 1009,
  35. } SRStatusCode;
  36. @class SRWebSocket;
  37. extern NSString *const SRWebSocketErrorDomain;
  38. extern NSString *const SRHTTPResponseErrorKey;
  39. #pragma mark - SRWebSocketDelegate
  40. @protocol SRWebSocketDelegate;
  41. #pragma mark - SRWebSocket
  42. @interface SRWebSocket : NSObject <NSStreamDelegate>
  43. @property(nonatomic, weak) id<SRWebSocketDelegate> delegate;
  44. @property(nonatomic, readonly) SRReadyState readyState;
  45. @property(nonatomic, readonly, retain) NSURL *url;
  46. // This returns the negotiated protocol.
  47. // It will be nil until after the handshake completes.
  48. @property(nonatomic, readonly, copy) NSString *protocol;
  49. // Protocols should be an array of strings that turn into Sec-WebSocket-Protocol.
  50. - (id)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray *)protocols;
  51. - (id)initWithURLRequest:(NSURLRequest *)request;
  52. // Some helper constructors.
  53. - (id)initWithURL:(NSURL *)url protocols:(NSArray *)protocols;
  54. - (id)initWithURL:(NSURL *)url;
  55. // Delegate queue will be dispatch_main_queue by default.
  56. // You cannot set both OperationQueue and dispatch_queue.
  57. - (void)setDelegateOperationQueue:(NSOperationQueue *)queue;
  58. - (void)setDelegateDispatchQueue:(dispatch_queue_t)queue;
  59. // By default, it will schedule itself on +[NSRunLoop SR_networkRunLoop] using defaultModes.
  60. - (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;
  61. - (void)unscheduleFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;
  62. // SRWebSockets are intended for one-time-use only. Open should be called once and only once.
  63. - (void)open;
  64. - (void)close;
  65. - (void)closeWithCode:(NSInteger)code reason:(NSString *)reason;
  66. // Send a UTF8 String or Data.
  67. - (void)send:(id)data;
  68. // Send Data (can be nil) in a ping message.
  69. - (void)sendPing:(NSData *)data;
  70. @end
  71. #pragma mark - SRWebSocketDelegate
  72. @protocol SRWebSocketDelegate <NSObject>
  73. // message will either be an NSString if the server is using text
  74. // or NSData if the server is using binary.
  75. - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
  76. @optional
  77. - (void)webSocketDidOpen:(SRWebSocket *)webSocket;
  78. - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
  79. - (void)webSocket:(SRWebSocket *)webSocket
  80. didCloseWithCode:(NSInteger)code
  81. reason:(NSString *)reason
  82. wasClean:(BOOL)wasClean;
  83. - (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload;
  84. @end
  85. #pragma mark - NSURLRequest (CertificateAdditions)
  86. @interface NSURLRequest (CertificateAdditions)
  87. @property(nonatomic, retain, readonly) NSArray *SR_SSLPinnedCertificates;
  88. @end
  89. #pragma mark - NSMutableURLRequest (CertificateAdditions)
  90. @interface NSMutableURLRequest (CertificateAdditions)
  91. @property(nonatomic, retain) NSArray *SR_SSLPinnedCertificates;
  92. @end
  93. #pragma mark - NSRunLoop (SRWebSocket)
  94. @interface NSRunLoop (SRWebSocket)
  95. + (NSRunLoop *)SR_networkRunLoop;
  96. @end